2 * $Id: comm.c,v 1.6 2009-10-19 08:09:07 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, time_t *now)
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));
181 if (FD_ISSET(control_fd, &readfds)) {
184 fd = recv_cred(control_fd);
188 if (fds_in_use < fd_table_size) {
189 fd_table[fds_in_use].fd = fd;
190 fd_table[fds_in_use].tm = t;
195 for (i = 0; i != fds_in_use; i++) {
196 if (older <= fd_table[i].tm) {
197 older = fd_table[i].tm;
201 close(fd_table[l].fd);
208 for (i = 0; i != fds_in_use; i++) {
209 if (FD_ISSET(fd_table[i].fd, &readfds)) {
211 return fd_table[i].fd;
214 /* We should never get here */
218 int comm_init(struct db_param *dbp, int ctrlfd, int clntfd)
223 fd_table_size = dbp->fd_table_size;
225 if ((fd_table = malloc(fd_table_size * sizeof(struct connection))) == NULL) {
226 LOG(log_error, logtype_cnid, "Out of memory");
229 for (i = 0; i != fd_table_size; i++)
235 /* this one dump core in recvmsg, great */
236 if ( setsockopt(control_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
237 LOG(log_error, logtype_cnid, "setsockopt SO_PASSCRED %s", strerror(errno));
241 /* push the first client fd */
242 fd_table[fds_in_use].fd = clntfd;
257 int comm_rcv(struct cnid_dbd_rqst *rqst, time_t timeout, const sigset_t *sigmask, time_t *now)
262 if ((cur_fd = check_fd(timeout, sigmask, now)) < 0)
268 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got data on fd %u", cur_fd);
270 nametmp = rqst->name;
271 if ((b = read(cur_fd, rqst, sizeof(struct cnid_dbd_rqst))) != sizeof(struct cnid_dbd_rqst)) {
273 LOG(log_error, logtype_cnid, "error reading message header: %s", strerror(errno));
274 invalidate_fd(cur_fd);
275 rqst->name = nametmp;
278 rqst->name = nametmp;
279 if (rqst->namelen && read(cur_fd, rqst->name, rqst->namelen) != rqst->namelen) {
280 LOG(log_error, logtype_cnid, "error reading message name: %s", strerror(errno));
281 invalidate_fd(cur_fd);
284 /* We set this to make life easier for logging. None of the other stuff
285 needs zero terminated strings. */
286 rqst->name[rqst->namelen] = '\0';
288 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got %u bytes", b + rqst->namelen);
295 int comm_snd(struct cnid_dbd_rply *rply)
302 if (!rply->namelen) {
303 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
304 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
305 invalidate_fd(cur_fd);
312 iov[0].iov_base = rply;
313 iov[0].iov_len = sizeof(struct cnid_dbd_rply);
314 iov[1].iov_base = rply->name;
315 iov[1].iov_len = rply->namelen;
316 towrite = sizeof(struct cnid_dbd_rply) +rply->namelen;
318 if (writev(cur_fd, iov, 2) != towrite) {
319 LOG(log_error, logtype_cnid, "error writing message : %s", strerror(errno));
320 invalidate_fd(cur_fd);
324 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
325 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
326 invalidate_fd(cur_fd);
329 if (write(cur_fd, rply->name, rply->namelen) != rply->namelen) {
330 LOG(log_error, logtype_cnid, "error writing message name: %s", strerror(errno));
331 invalidate_fd(cur_fd);