2 * $Id: comm.c,v 1.5 2009-10-18 17:50:13 didg Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * All Rights Reserved. See COPYING.
21 #include <sys/param.h>
23 #include <sys/socket.h>
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h>
29 #ifdef HAVE_SYS_TIME_H
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
41 #include <sys/select.h>
46 #include <atalk/logger.h>
47 #include <atalk/cnid_dbd_private.h>
53 /* Length of the space taken up by a padded control message of length len */
55 #define CMSG_SPACE(len) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len))
60 time_t tm; /* When respawned last */
64 static int control_fd;
66 static struct connection *fd_table;
67 static int fd_table_size;
68 static int fds_in_use = 0;
71 static void invalidate_fd(int fd)
77 for (i = 0; i != fds_in_use; i++)
78 if (fd_table[i].fd == fd)
81 assert(i < fds_in_use);
84 fd_table[i] = fd_table[fds_in_use];
85 fd_table[fds_in_use].fd = -1;
90 static int recv_cred(int fd)
95 struct cmsghdr *cmsgp = NULL;
96 char buf[CMSG_SPACE(sizeof(int))];
99 memset(&msgh,0,sizeof(msgh));
100 memset(buf,0,sizeof(buf));
102 msgh.msg_name = NULL;
103 msgh.msg_namelen = 0;
108 iov[0].iov_base = dbuf;
109 iov[0].iov_len = sizeof(dbuf);
111 msgh.msg_control = buf;
112 msgh.msg_controllen = sizeof(buf);
115 ret = recvmsg(fd ,&msgh,0);
116 } while ( ret == -1 && errno == EINTR );
122 for ( cmsgp = CMSG_FIRSTHDR(&msgh); cmsgp != NULL; cmsgp = CMSG_NXTHDR(&msgh,cmsgp) ) {
123 if ( cmsgp->cmsg_level == SOL_SOCKET && cmsgp->cmsg_type == SCM_RIGHTS ) {
124 return *(int *) CMSG_DATA(cmsgp);
128 if ( ret == sizeof (int) )
129 errno = *(int *)dbuf; /* Rcvd errno */
131 errno = ENOENT; /* Default errno */
137 * Check for client requests. We keep up to fd_table_size open descriptors in
138 * fd_table. If the table is full and we get a new descriptor via
139 * control_fd, we close a random decriptor in the table to make space. The
140 * affected client will automatically reconnect. For an EOF (descriptor is
141 * closed by the client, so a read here returns 0) comm_rcv will take care of
142 * things and clean up fd_table. The same happens for any read/write errors.
145 static int check_fd(time_t timeout, const sigset_t *sigmask)
152 int maxfd = control_fd;
156 FD_SET(control_fd, &readfds);
158 for (i = 0; i != fds_in_use; i++) {
159 FD_SET(fd_table[i].fd, &readfds);
160 if (maxfd < fd_table[i].fd)
161 maxfd = fd_table[i].fd;
166 if ((ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tv, sigmask)) < 0) {
169 LOG(log_error, logtype_cnid, "error in select: %s",strerror(errno));
178 if (FD_ISSET(control_fd, &readfds)) {
181 fd = recv_cred(control_fd);
185 if (fds_in_use < fd_table_size) {
186 fd_table[fds_in_use].fd = fd;
187 fd_table[fds_in_use].tm = t;
192 for (i = 0; i != fds_in_use; i++) {
193 if (older <= fd_table[i].tm) {
194 older = fd_table[i].tm;
198 close(fd_table[l].fd);
205 for (i = 0; i != fds_in_use; i++) {
206 if (FD_ISSET(fd_table[i].fd, &readfds)) {
208 return fd_table[i].fd;
211 /* We should never get here */
215 int comm_init(struct db_param *dbp, int ctrlfd, int clntfd)
220 fd_table_size = dbp->fd_table_size;
222 if ((fd_table = malloc(fd_table_size * sizeof(struct connection))) == NULL) {
223 LOG(log_error, logtype_cnid, "Out of memory");
226 for (i = 0; i != fd_table_size; i++)
232 /* this one dump core in recvmsg, great */
233 if ( setsockopt(control_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
234 LOG(log_error, logtype_cnid, "setsockopt SO_PASSCRED %s", strerror(errno));
238 /* push the first client fd */
239 fd_table[fds_in_use].fd = clntfd;
254 int comm_rcv(struct cnid_dbd_rqst *rqst, time_t timeout, const sigset_t *sigmask)
259 if ((cur_fd = check_fd(timeout, sigmask)) < 0)
264 nametmp = rqst->name;
265 if ((b = read(cur_fd, rqst, sizeof(struct cnid_dbd_rqst))) != sizeof(struct cnid_dbd_rqst)) {
267 LOG(log_error, logtype_cnid, "error reading message header: %s", strerror(errno));
268 invalidate_fd(cur_fd);
269 rqst->name = nametmp;
272 rqst->name = nametmp;
273 if (rqst->namelen && read(cur_fd, rqst->name, rqst->namelen) != rqst->namelen) {
274 LOG(log_error, logtype_cnid, "error reading message name: %s", strerror(errno));
275 invalidate_fd(cur_fd);
278 /* We set this to make life easier for logging. None of the other stuff
279 needs zero terminated strings. */
280 rqst->name[rqst->namelen] = '\0';
287 int comm_snd(struct cnid_dbd_rply *rply)
294 if (!rply->namelen) {
295 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
296 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
297 invalidate_fd(cur_fd);
304 iov[0].iov_base = rply;
305 iov[0].iov_len = sizeof(struct cnid_dbd_rply);
306 iov[1].iov_base = rply->name;
307 iov[1].iov_len = rply->namelen;
308 towrite = sizeof(struct cnid_dbd_rply) +rply->namelen;
310 if (writev(cur_fd, iov, 2) != towrite) {
311 LOG(log_error, logtype_cnid, "error writing message : %s", strerror(errno));
312 invalidate_fd(cur_fd);
316 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
317 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
318 invalidate_fd(cur_fd);
321 if (write(cur_fd, rply->name, rply->namelen) != rply->namelen) {
322 LOG(log_error, logtype_cnid, "error writing message name: %s", strerror(errno));
323 invalidate_fd(cur_fd);