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__
22 /* for u_short, u_char, etc. */
23 # define __BSD_VISIBLE 1
31 #include <sys/param.h>
32 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/select.h>
40 #include <atalk/logger.h>
41 #include <atalk/util.h>
42 #include <atalk/cnid_dbd_private.h>
48 /* Length of the space taken up by a padded control message of length len */
50 #define CMSG_SPACE(len) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len))
55 time_t tm; /* When respawned last */
59 static int control_fd;
61 static struct connection *fd_table;
62 static int fd_table_size;
63 static int fds_in_use = 0;
66 static void invalidate_fd(int fd)
72 for (i = 0; i != fds_in_use; i++)
73 if (fd_table[i].fd == fd)
76 assert(i < fds_in_use);
79 fd_table[i] = fd_table[fds_in_use];
80 fd_table[fds_in_use].fd = -1;
87 * Check for client requests. We keep up to fd_table_size open descriptors in
88 * fd_table. If the table is full and we get a new descriptor via
89 * control_fd, we close a random decriptor in the table to make space. The
90 * affected client will automatically reconnect. For an EOF (descriptor is
91 * closed by the client, so a read here returns 0) comm_rcv will take care of
92 * things and clean up fd_table. The same happens for any read/write errors.
95 static int check_fd(time_t timeout, const sigset_t *sigmask, time_t *now)
102 int maxfd = control_fd;
106 FD_SET(control_fd, &readfds);
108 for (i = 0; i != fds_in_use; i++) {
109 FD_SET(fd_table[i].fd, &readfds);
110 if (maxfd < fd_table[i].fd)
111 maxfd = fd_table[i].fd;
116 if ((ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tv, sigmask)) < 0) {
119 LOG(log_error, logtype_cnid, "error in select: %s",strerror(errno));
131 if (FD_ISSET(control_fd, &readfds)) {
134 fd = recv_fd(control_fd, 0);
138 if (fds_in_use < fd_table_size) {
139 fd_table[fds_in_use].fd = fd;
140 fd_table[fds_in_use].tm = t;
145 for (i = 0; i != fds_in_use; i++) {
146 if (older <= fd_table[i].tm) {
147 older = fd_table[i].tm;
151 close(fd_table[l].fd);
158 for (i = 0; i != fds_in_use; i++) {
159 if (FD_ISSET(fd_table[i].fd, &readfds)) {
161 return fd_table[i].fd;
164 /* We should never get here */
168 int comm_init(struct db_param *dbp, int ctrlfd, int clntfd)
173 fd_table_size = dbp->fd_table_size;
175 if ((fd_table = malloc(fd_table_size * sizeof(struct connection))) == NULL) {
176 LOG(log_error, logtype_cnid, "Out of memory");
179 for (i = 0; i != fd_table_size; i++)
185 /* this one dump core in recvmsg, great */
186 if ( setsockopt(control_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
187 LOG(log_error, logtype_cnid, "setsockopt SO_PASSCRED %s", strerror(errno));
191 /* push the first client fd */
192 fd_table[fds_in_use].fd = clntfd;
207 int comm_rcv(struct cnid_dbd_rqst *rqst, time_t timeout, const sigset_t *sigmask, time_t *now)
212 if ((cur_fd = check_fd(timeout, sigmask, now)) < 0)
218 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got data on fd %u", cur_fd);
220 if (setnonblock(cur_fd, 1) != 0) {
221 LOG(log_error, logtype_cnid, "comm_rcv: setnonblock: %s", strerror(errno));
225 nametmp = rqst->name;
226 if ((b = readt(cur_fd, rqst, sizeof(struct cnid_dbd_rqst), 1, CNID_DBD_TIMEOUT))
227 != sizeof(struct cnid_dbd_rqst)) {
229 LOG(log_error, logtype_cnid, "error reading message header: %s", strerror(errno));
230 invalidate_fd(cur_fd);
231 rqst->name = nametmp;
234 rqst->name = nametmp;
235 if (rqst->namelen && readt(cur_fd, rqst->name, rqst->namelen, 1, CNID_DBD_TIMEOUT)
237 LOG(log_error, logtype_cnid, "error reading message name: %s", strerror(errno));
238 invalidate_fd(cur_fd);
241 /* We set this to make life easier for logging. None of the other stuff
242 needs zero terminated strings. */
243 rqst->name[rqst->namelen] = '\0';
245 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got %u bytes", b + rqst->namelen);
252 int comm_snd(struct cnid_dbd_rply *rply)
259 if (!rply->namelen) {
260 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
261 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
262 invalidate_fd(cur_fd);
269 iov[0].iov_base = rply;
270 iov[0].iov_len = sizeof(struct cnid_dbd_rply);
271 iov[1].iov_base = rply->name;
272 iov[1].iov_len = rply->namelen;
273 towrite = sizeof(struct cnid_dbd_rply) +rply->namelen;
275 if (writev(cur_fd, iov, 2) != towrite) {
276 LOG(log_error, logtype_cnid, "error writing message : %s", strerror(errno));
277 invalidate_fd(cur_fd);
281 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
282 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
283 invalidate_fd(cur_fd);
286 if (write(cur_fd, rply->name, rply->namelen) != rply->namelen) {
287 LOG(log_error, logtype_cnid, "error writing message name: %s", strerror(errno));
288 invalidate_fd(cur_fd);