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>
31 #include <atalk/compat.h>
37 /* Length of the space taken up by a padded control message of length len */
39 #define CMSG_SPACE(len) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len))
44 time_t tm; /* When respawned last */
48 static int control_fd;
50 static struct connection *fd_table;
51 static int fd_table_size;
52 static int fds_in_use = 0;
55 static void invalidate_fd(int fd)
61 for (i = 0; i != fds_in_use; i++)
62 if (fd_table[i].fd == fd)
65 assert(i < fds_in_use);
68 fd_table[i] = fd_table[fds_in_use];
69 fd_table[fds_in_use].fd = -1;
76 * Check for client requests. We keep up to fd_table_size open descriptors in
77 * fd_table. If the table is full and we get a new descriptor via
78 * control_fd, we close a random decriptor in the table to make space. The
79 * affected client will automatically reconnect. For an EOF (descriptor is
80 * closed by the client, so a read here returns 0) comm_rcv will take care of
81 * things and clean up fd_table. The same happens for any read/write errors.
84 static int check_fd(time_t timeout, const sigset_t *sigmask, time_t *now)
91 int maxfd = control_fd;
95 FD_SET(control_fd, &readfds);
97 for (i = 0; i != fds_in_use; i++) {
98 FD_SET(fd_table[i].fd, &readfds);
99 if (maxfd < fd_table[i].fd)
100 maxfd = fd_table[i].fd;
105 if ((ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tv, sigmask)) < 0) {
108 LOG(log_error, logtype_cnid, "error in select: %s",strerror(errno));
120 if (FD_ISSET(control_fd, &readfds)) {
123 fd = recv_fd(control_fd, 0);
127 if (fds_in_use < fd_table_size) {
128 fd_table[fds_in_use].fd = fd;
129 fd_table[fds_in_use].tm = t;
134 for (i = 0; i != fds_in_use; i++) {
135 if (older <= fd_table[i].tm) {
136 older = fd_table[i].tm;
140 close(fd_table[l].fd);
147 for (i = 0; i != fds_in_use; i++) {
148 if (FD_ISSET(fd_table[i].fd, &readfds)) {
150 return fd_table[i].fd;
153 /* We should never get here */
157 int comm_init(struct db_param *dbp, int ctrlfd, int clntfd)
162 fd_table_size = dbp->fd_table_size;
164 if ((fd_table = malloc(fd_table_size * sizeof(struct connection))) == NULL) {
165 LOG(log_error, logtype_cnid, "Out of memory");
168 for (i = 0; i != fd_table_size; i++)
174 /* this one dump core in recvmsg, great */
175 if ( setsockopt(control_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
176 LOG(log_error, logtype_cnid, "setsockopt SO_PASSCRED %s", strerror(errno));
180 /* push the first client fd */
181 fd_table[fds_in_use].fd = clntfd;
196 int comm_rcv(struct cnid_dbd_rqst *rqst, time_t timeout, const sigset_t *sigmask, time_t *now)
201 if ((cur_fd = check_fd(timeout, sigmask, now)) < 0)
207 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got data on fd %u", cur_fd);
209 if (setnonblock(cur_fd, 1) != 0) {
210 LOG(log_error, logtype_cnid, "comm_rcv: setnonblock: %s", strerror(errno));
214 nametmp = (char *)rqst->name;
215 if ((b = readt(cur_fd, rqst, sizeof(struct cnid_dbd_rqst), 1, CNID_DBD_TIMEOUT))
216 != sizeof(struct cnid_dbd_rqst)) {
218 LOG(log_error, logtype_cnid, "error reading message header: %s", strerror(errno));
219 invalidate_fd(cur_fd);
220 rqst->name = nametmp;
223 rqst->name = nametmp;
224 if (rqst->namelen && readt(cur_fd, (char *)rqst->name, rqst->namelen, 1, CNID_DBD_TIMEOUT)
226 LOG(log_error, logtype_cnid, "error reading message name: %s", strerror(errno));
227 invalidate_fd(cur_fd);
230 /* We set this to make life easier for logging. None of the other stuff
231 needs zero terminated strings. */
232 ((char *)(rqst->name))[rqst->namelen] = '\0';
234 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got %u bytes", b + rqst->namelen);
241 int comm_snd(struct cnid_dbd_rply *rply)
248 if (!rply->namelen) {
249 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
250 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
251 invalidate_fd(cur_fd);
258 iov[0].iov_base = rply;
259 iov[0].iov_len = sizeof(struct cnid_dbd_rply);
260 iov[1].iov_base = rply->name;
261 iov[1].iov_len = rply->namelen;
262 towrite = sizeof(struct cnid_dbd_rply) +rply->namelen;
264 if (writev(cur_fd, iov, 2) != towrite) {
265 LOG(log_error, logtype_cnid, "error writing message : %s", strerror(errno));
266 invalidate_fd(cur_fd);
270 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
271 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
272 invalidate_fd(cur_fd);
275 if (write(cur_fd, rply->name, rply->namelen) != rply->namelen) {
276 LOG(log_error, logtype_cnid, "error writing message name: %s", strerror(errno));
277 invalidate_fd(cur_fd);