2 * Copyright (C) Joerg Lenneis 2003
3 * Copyright (C) Frank Lahm 2010
5 * All Rights Reserved. See COPYING.
13 # define _XOPEN_SOURCE 600
15 #ifndef __EXTENSIONS__
16 # define __EXTENSIONS__
27 #include <sys/param.h>
28 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/select.h>
36 #include <atalk/logger.h>
37 #include <atalk/util.h>
38 #include <atalk/cnid_dbd_private.h>
44 /* Length of the space taken up by a padded control message of length len */
46 #define CMSG_SPACE(len) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len))
51 time_t tm; /* When respawned last */
55 static int control_fd;
57 static struct connection *fd_table;
58 static int fd_table_size;
59 static int fds_in_use = 0;
62 static void invalidate_fd(int fd)
68 for (i = 0; i != fds_in_use; i++)
69 if (fd_table[i].fd == fd)
72 assert(i < fds_in_use);
75 fd_table[i] = fd_table[fds_in_use];
76 fd_table[fds_in_use].fd = -1;
83 * Check for client requests. We keep up to fd_table_size open descriptors in
84 * fd_table. If the table is full and we get a new descriptor via
85 * control_fd, we close a random decriptor in the table to make space. The
86 * affected client will automatically reconnect. For an EOF (descriptor is
87 * closed by the client, so a read here returns 0) comm_rcv will take care of
88 * things and clean up fd_table. The same happens for any read/write errors.
91 static int check_fd(time_t timeout, const sigset_t *sigmask, time_t *now)
98 int maxfd = control_fd;
102 FD_SET(control_fd, &readfds);
104 for (i = 0; i != fds_in_use; i++) {
105 FD_SET(fd_table[i].fd, &readfds);
106 if (maxfd < fd_table[i].fd)
107 maxfd = fd_table[i].fd;
112 if ((ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tv, sigmask)) < 0) {
115 LOG(log_error, logtype_cnid, "error in select: %s",strerror(errno));
127 if (FD_ISSET(control_fd, &readfds)) {
130 fd = recv_fd(control_fd, 0);
134 if (fds_in_use < fd_table_size) {
135 fd_table[fds_in_use].fd = fd;
136 fd_table[fds_in_use].tm = t;
141 for (i = 0; i != fds_in_use; i++) {
142 if (older <= fd_table[i].tm) {
143 older = fd_table[i].tm;
147 close(fd_table[l].fd);
154 for (i = 0; i != fds_in_use; i++) {
155 if (FD_ISSET(fd_table[i].fd, &readfds)) {
157 return fd_table[i].fd;
160 /* We should never get here */
164 int comm_init(struct db_param *dbp, int ctrlfd, int clntfd)
169 fd_table_size = dbp->fd_table_size;
171 if ((fd_table = malloc(fd_table_size * sizeof(struct connection))) == NULL) {
172 LOG(log_error, logtype_cnid, "Out of memory");
175 for (i = 0; i != fd_table_size; i++)
181 /* this one dump core in recvmsg, great */
182 if ( setsockopt(control_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
183 LOG(log_error, logtype_cnid, "setsockopt SO_PASSCRED %s", strerror(errno));
187 /* push the first client fd */
188 fd_table[fds_in_use].fd = clntfd;
203 int comm_rcv(struct cnid_dbd_rqst *rqst, time_t timeout, const sigset_t *sigmask, time_t *now)
208 if ((cur_fd = check_fd(timeout, sigmask, now)) < 0)
214 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got data on fd %u", cur_fd);
216 if (setnonblock(cur_fd, 1) != 0) {
217 LOG(log_error, logtype_cnid, "comm_rcv: setnonblock: %s", strerror(errno));
221 nametmp = rqst->name;
222 if ((b = readt(cur_fd, rqst, sizeof(struct cnid_dbd_rqst), 1, CNID_DBD_TIMEOUT))
223 != sizeof(struct cnid_dbd_rqst)) {
225 LOG(log_error, logtype_cnid, "error reading message header: %s", strerror(errno));
226 invalidate_fd(cur_fd);
227 rqst->name = nametmp;
230 rqst->name = nametmp;
231 if (rqst->namelen && readt(cur_fd, rqst->name, rqst->namelen, 1, CNID_DBD_TIMEOUT)
233 LOG(log_error, logtype_cnid, "error reading message name: %s", strerror(errno));
234 invalidate_fd(cur_fd);
237 /* We set this to make life easier for logging. None of the other stuff
238 needs zero terminated strings. */
239 rqst->name[rqst->namelen] = '\0';
241 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got %u bytes", b + rqst->namelen);
248 int comm_snd(struct cnid_dbd_rply *rply)
255 if (!rply->namelen) {
256 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
257 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
258 invalidate_fd(cur_fd);
265 iov[0].iov_base = rply;
266 iov[0].iov_len = sizeof(struct cnid_dbd_rply);
267 iov[1].iov_base = rply->name;
268 iov[1].iov_len = rply->namelen;
269 towrite = sizeof(struct cnid_dbd_rply) +rply->namelen;
271 if (writev(cur_fd, iov, 2) != towrite) {
272 LOG(log_error, logtype_cnid, "error writing message : %s", strerror(errno));
273 invalidate_fd(cur_fd);
277 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
278 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
279 invalidate_fd(cur_fd);
282 if (write(cur_fd, rply->name, rply->namelen) != rply->namelen) {
283 LOG(log_error, logtype_cnid, "error writing message name: %s", strerror(errno));
284 invalidate_fd(cur_fd);