2 * Copyright (C) Joerg Lenneis 2003
3 * Copyright (C) Frank Lahm 2010
5 * All Rights Reserved. See COPYING.
12 #include <atalk/standards.h>
19 #include <sys/param.h>
20 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/select.h>
28 #include <atalk/logger.h>
29 #include <atalk/util.h>
30 #include <atalk/cnid_dbd_private.h>
36 /* Length of the space taken up by a padded control message of length len */
38 #define CMSG_SPACE(len) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len))
43 time_t tm; /* When respawned last */
47 static int control_fd;
49 static struct connection *fd_table;
50 static int fd_table_size;
51 static int fds_in_use = 0;
54 static void invalidate_fd(int fd)
60 for (i = 0; i != fds_in_use; i++)
61 if (fd_table[i].fd == fd)
64 assert(i < fds_in_use);
67 fd_table[i] = fd_table[fds_in_use];
68 fd_table[fds_in_use].fd = -1;
75 * Check for client requests. We keep up to fd_table_size open descriptors in
76 * fd_table. If the table is full and we get a new descriptor via
77 * control_fd, we close a random decriptor in the table to make space. The
78 * affected client will automatically reconnect. For an EOF (descriptor is
79 * closed by the client, so a read here returns 0) comm_rcv will take care of
80 * things and clean up fd_table. The same happens for any read/write errors.
83 static int check_fd(time_t timeout, const sigset_t *sigmask, time_t *now)
90 int maxfd = control_fd;
94 FD_SET(control_fd, &readfds);
96 for (i = 0; i != fds_in_use; i++) {
97 FD_SET(fd_table[i].fd, &readfds);
98 if (maxfd < fd_table[i].fd)
99 maxfd = fd_table[i].fd;
104 if ((ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tv, sigmask)) < 0) {
107 LOG(log_error, logtype_cnid, "error in select: %s",strerror(errno));
119 if (FD_ISSET(control_fd, &readfds)) {
122 fd = recv_fd(control_fd, 0);
126 if (fds_in_use < fd_table_size) {
127 fd_table[fds_in_use].fd = fd;
128 fd_table[fds_in_use].tm = t;
133 for (i = 0; i != fds_in_use; i++) {
134 if (older <= fd_table[i].tm) {
135 older = fd_table[i].tm;
139 close(fd_table[l].fd);
146 for (i = 0; i != fds_in_use; i++) {
147 if (FD_ISSET(fd_table[i].fd, &readfds)) {
149 return fd_table[i].fd;
152 /* We should never get here */
156 int comm_init(struct db_param *dbp, int ctrlfd, int clntfd)
161 fd_table_size = dbp->fd_table_size;
163 if ((fd_table = malloc(fd_table_size * sizeof(struct connection))) == NULL) {
164 LOG(log_error, logtype_cnid, "Out of memory");
167 for (i = 0; i != fd_table_size; i++)
173 /* this one dump core in recvmsg, great */
174 if ( setsockopt(control_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
175 LOG(log_error, logtype_cnid, "setsockopt SO_PASSCRED %s", strerror(errno));
179 /* push the first client fd */
180 fd_table[fds_in_use].fd = clntfd;
195 int comm_rcv(struct cnid_dbd_rqst *rqst, time_t timeout, const sigset_t *sigmask, time_t *now)
200 if ((cur_fd = check_fd(timeout, sigmask, now)) < 0)
206 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got data on fd %u", cur_fd);
208 if (setnonblock(cur_fd, 1) != 0) {
209 LOG(log_error, logtype_cnid, "comm_rcv: setnonblock: %s", strerror(errno));
213 nametmp = rqst->name;
214 if ((b = readt(cur_fd, rqst, sizeof(struct cnid_dbd_rqst), 1, CNID_DBD_TIMEOUT))
215 != sizeof(struct cnid_dbd_rqst)) {
217 LOG(log_error, logtype_cnid, "error reading message header: %s", strerror(errno));
218 invalidate_fd(cur_fd);
219 rqst->name = nametmp;
222 rqst->name = nametmp;
223 if (rqst->namelen && readt(cur_fd, rqst->name, rqst->namelen, 1, CNID_DBD_TIMEOUT)
225 LOG(log_error, logtype_cnid, "error reading message name: %s", strerror(errno));
226 invalidate_fd(cur_fd);
229 /* We set this to make life easier for logging. None of the other stuff
230 needs zero terminated strings. */
231 rqst->name[rqst->namelen] = '\0';
233 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got %u bytes", b + rqst->namelen);
240 int comm_snd(struct cnid_dbd_rply *rply)
247 if (!rply->namelen) {
248 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
249 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
250 invalidate_fd(cur_fd);
257 iov[0].iov_base = rply;
258 iov[0].iov_len = sizeof(struct cnid_dbd_rply);
259 iov[1].iov_base = rply->name;
260 iov[1].iov_len = rply->namelen;
261 towrite = sizeof(struct cnid_dbd_rply) +rply->namelen;
263 if (writev(cur_fd, iov, 2) != towrite) {
264 LOG(log_error, logtype_cnid, "error writing message : %s", strerror(errno));
265 invalidate_fd(cur_fd);
269 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
270 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
271 invalidate_fd(cur_fd);
274 if (write(cur_fd, rply->name, rply->namelen) != rply->namelen) {
275 LOG(log_error, logtype_cnid, "error writing message name: %s", strerror(errno));
276 invalidate_fd(cur_fd);