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;
72 static int recv_cred(int fd)
77 struct cmsghdr *cmsgp = NULL;
78 char buf[CMSG_SPACE(sizeof(int))];
81 memset(&msgh,0,sizeof(msgh));
82 memset(buf,0,sizeof(buf));
90 iov[0].iov_base = dbuf;
91 iov[0].iov_len = sizeof(dbuf);
93 msgh.msg_control = buf;
94 msgh.msg_controllen = sizeof(buf);
97 ret = recvmsg(fd ,&msgh,0);
98 } while ( ret == -1 && errno == EINTR );
104 for ( cmsgp = CMSG_FIRSTHDR(&msgh); cmsgp != NULL; cmsgp = CMSG_NXTHDR(&msgh,cmsgp) ) {
105 if ( cmsgp->cmsg_level == SOL_SOCKET && cmsgp->cmsg_type == SCM_RIGHTS ) {
106 return *(int *) CMSG_DATA(cmsgp);
110 if ( ret == sizeof (int) )
111 errno = *(int *)dbuf; /* Rcvd errno */
113 errno = ENOENT; /* Default errno */
119 * Check for client requests. We keep up to fd_table_size open descriptors in
120 * fd_table. If the table is full and we get a new descriptor via
121 * control_fd, we close a random decriptor in the table to make space. The
122 * affected client will automatically reconnect. For an EOF (descriptor is
123 * closed by the client, so a read here returns 0) comm_rcv will take care of
124 * things and clean up fd_table. The same happens for any read/write errors.
127 static int check_fd(time_t timeout, const sigset_t *sigmask, time_t *now)
134 int maxfd = control_fd;
138 FD_SET(control_fd, &readfds);
140 for (i = 0; i != fds_in_use; i++) {
141 FD_SET(fd_table[i].fd, &readfds);
142 if (maxfd < fd_table[i].fd)
143 maxfd = fd_table[i].fd;
148 if ((ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tv, sigmask)) < 0) {
151 LOG(log_error, logtype_cnid, "error in select: %s",strerror(errno));
163 if (FD_ISSET(control_fd, &readfds)) {
166 fd = recv_cred(control_fd);
170 if (fds_in_use < fd_table_size) {
171 fd_table[fds_in_use].fd = fd;
172 fd_table[fds_in_use].tm = t;
177 for (i = 0; i != fds_in_use; i++) {
178 if (older <= fd_table[i].tm) {
179 older = fd_table[i].tm;
183 close(fd_table[l].fd);
190 for (i = 0; i != fds_in_use; i++) {
191 if (FD_ISSET(fd_table[i].fd, &readfds)) {
193 return fd_table[i].fd;
196 /* We should never get here */
200 int comm_init(struct db_param *dbp, int ctrlfd, int clntfd)
205 fd_table_size = dbp->fd_table_size;
207 if ((fd_table = malloc(fd_table_size * sizeof(struct connection))) == NULL) {
208 LOG(log_error, logtype_cnid, "Out of memory");
211 for (i = 0; i != fd_table_size; i++)
217 /* this one dump core in recvmsg, great */
218 if ( setsockopt(control_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
219 LOG(log_error, logtype_cnid, "setsockopt SO_PASSCRED %s", strerror(errno));
223 /* push the first client fd */
224 fd_table[fds_in_use].fd = clntfd;
239 int comm_rcv(struct cnid_dbd_rqst *rqst, time_t timeout, const sigset_t *sigmask, time_t *now)
244 if ((cur_fd = check_fd(timeout, sigmask, now)) < 0)
250 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got data on fd %u", cur_fd);
252 if (setnonblock(cur_fd, 1) != 0) {
253 LOG(log_error, logtype_cnid, "comm_rcv: setnonblock: %s", strerror(errno));
257 nametmp = rqst->name;
258 if ((b = readt(cur_fd, rqst, sizeof(struct cnid_dbd_rqst), 1, CNID_DBD_TIMEOUT))
259 != sizeof(struct cnid_dbd_rqst)) {
261 LOG(log_error, logtype_cnid, "error reading message header: %s", strerror(errno));
262 invalidate_fd(cur_fd);
263 rqst->name = nametmp;
266 rqst->name = nametmp;
267 if (rqst->namelen && readt(cur_fd, rqst->name, rqst->namelen, 1, CNID_DBD_TIMEOUT)
269 LOG(log_error, logtype_cnid, "error reading message name: %s", strerror(errno));
270 invalidate_fd(cur_fd);
273 /* We set this to make life easier for logging. None of the other stuff
274 needs zero terminated strings. */
275 rqst->name[rqst->namelen] = '\0';
277 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got %u bytes", b + rqst->namelen);
284 int comm_snd(struct cnid_dbd_rply *rply)
291 if (!rply->namelen) {
292 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
293 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
294 invalidate_fd(cur_fd);
301 iov[0].iov_base = rply;
302 iov[0].iov_len = sizeof(struct cnid_dbd_rply);
303 iov[1].iov_base = rply->name;
304 iov[1].iov_len = rply->namelen;
305 towrite = sizeof(struct cnid_dbd_rply) +rply->namelen;
307 if (writev(cur_fd, iov, 2) != towrite) {
308 LOG(log_error, logtype_cnid, "error writing message : %s", strerror(errno));
309 invalidate_fd(cur_fd);
313 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
314 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
315 invalidate_fd(cur_fd);
318 if (write(cur_fd, rply->name, rply->namelen) != rply->namelen) {
319 LOG(log_error, logtype_cnid, "error writing message name: %s", strerror(errno));
320 invalidate_fd(cur_fd);