2 * Copyright (C) Joerg Lenneis 2003
3 * Copyright (C) Frank Lahm 2010
5 * All Rights Reserved. See COPYING.
17 #include <sys/param.h>
18 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/select.h>
27 #include <atalk/logger.h>
28 #include <atalk/util.h>
29 #include <atalk/cnid_dbd_private.h>
35 /* Length of the space taken up by a padded control message of length len */
37 #define CMSG_SPACE(len) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len))
42 time_t tm; /* When respawned last */
46 static int control_fd;
48 static struct connection *fd_table;
49 static int fd_table_size;
50 static int fds_in_use = 0;
53 static void invalidate_fd(int fd)
59 for (i = 0; i != fds_in_use; i++)
60 if (fd_table[i].fd == fd)
63 assert(i < fds_in_use);
66 fd_table[i] = fd_table[fds_in_use];
67 fd_table[fds_in_use].fd = -1;
74 * Check for client requests. We keep up to fd_table_size open descriptors in
75 * fd_table. If the table is full and we get a new descriptor via
76 * control_fd, we close a random decriptor in the table to make space. The
77 * affected client will automatically reconnect. For an EOF (descriptor is
78 * closed by the client, so a read here returns 0) comm_rcv will take care of
79 * things and clean up fd_table. The same happens for any read/write errors.
82 static int check_fd(time_t timeout, const sigset_t *sigmask, time_t *now)
89 int maxfd = control_fd;
93 FD_SET(control_fd, &readfds);
95 for (i = 0; i != fds_in_use; i++) {
96 FD_SET(fd_table[i].fd, &readfds);
97 if (maxfd < fd_table[i].fd)
98 maxfd = fd_table[i].fd;
103 if ((ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tv, sigmask)) < 0) {
106 LOG(log_error, logtype_cnid, "error in select: %s",strerror(errno));
118 if (FD_ISSET(control_fd, &readfds)) {
121 fd = recv_fd(control_fd, 0);
125 if (fds_in_use < fd_table_size) {
126 fd_table[fds_in_use].fd = fd;
127 fd_table[fds_in_use].tm = t;
132 for (i = 0; i != fds_in_use; i++) {
133 if (older <= fd_table[i].tm) {
134 older = fd_table[i].tm;
138 close(fd_table[l].fd);
145 for (i = 0; i != fds_in_use; i++) {
146 if (FD_ISSET(fd_table[i].fd, &readfds)) {
148 return fd_table[i].fd;
151 /* We should never get here */
155 int comm_init(struct db_param *dbp, int ctrlfd, int clntfd)
160 fd_table_size = dbp->fd_table_size;
162 if ((fd_table = malloc(fd_table_size * sizeof(struct connection))) == NULL) {
163 LOG(log_error, logtype_cnid, "Out of memory");
166 for (i = 0; i != fd_table_size; i++)
172 /* this one dump core in recvmsg, great */
173 if ( setsockopt(control_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
174 LOG(log_error, logtype_cnid, "setsockopt SO_PASSCRED %s", strerror(errno));
178 /* push the first client fd */
179 fd_table[fds_in_use].fd = clntfd;
194 int comm_rcv(struct cnid_dbd_rqst *rqst, time_t timeout, const sigset_t *sigmask, time_t *now)
199 if ((cur_fd = check_fd(timeout, sigmask, now)) < 0)
205 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got data on fd %u", cur_fd);
207 if (setnonblock(cur_fd, 1) != 0) {
208 LOG(log_error, logtype_cnid, "comm_rcv: setnonblock: %s", strerror(errno));
212 nametmp = (char *)rqst->name;
213 if ((b = readt(cur_fd, rqst, sizeof(struct cnid_dbd_rqst), 1, CNID_DBD_TIMEOUT))
214 != sizeof(struct cnid_dbd_rqst)) {
216 LOG(log_error, logtype_cnid, "error reading message header: %s", strerror(errno));
217 invalidate_fd(cur_fd);
218 rqst->name = nametmp;
221 rqst->name = nametmp;
222 if (rqst->namelen && readt(cur_fd, (char *)rqst->name, rqst->namelen, 1, CNID_DBD_TIMEOUT)
224 LOG(log_error, logtype_cnid, "error reading message name: %s", strerror(errno));
225 invalidate_fd(cur_fd);
228 /* We set this to make life easier for logging. None of the other stuff
229 needs zero terminated strings. */
230 ((char *)(rqst->name))[rqst->namelen] = '\0';
232 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got %u bytes", b + rqst->namelen);
239 int comm_snd(struct cnid_dbd_rply *rply)
246 if (!rply->namelen) {
247 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
248 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
249 invalidate_fd(cur_fd);
256 iov[0].iov_base = rply;
257 iov[0].iov_len = sizeof(struct cnid_dbd_rply);
258 iov[1].iov_base = rply->name;
259 iov[1].iov_len = rply->namelen;
260 towrite = sizeof(struct cnid_dbd_rply) +rply->namelen;
262 if (writev(cur_fd, iov, 2) != towrite) {
263 LOG(log_error, logtype_cnid, "error writing message : %s", strerror(errno));
264 invalidate_fd(cur_fd);
268 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
269 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
270 invalidate_fd(cur_fd);
273 if (write(cur_fd, rply->name, rply->namelen) != rply->namelen) {
274 LOG(log_error, logtype_cnid, "error writing message name: %s", strerror(errno));
275 invalidate_fd(cur_fd);