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;
73 * non-blocking drop-in replacement for read with timeout using select
75 * @param socket (r) must be nonblocking !
76 * @param data (rw) buffer for the read data
77 * @param lenght (r) how many bytes to read
78 * @param timeout (r) number of seconds to try reading
80 * @returns number of bytes actually read or -1 on fatal error
82 static ssize_t readt(int socket, void *data, const size_t length, int timeout)
92 while (stored < length) {
93 len = read(socket, (u_int8_t *) data + stored, length - stored);
103 FD_SET(socket, &rfds);
104 while ((ret = select(socket + 1, &rfds, NULL, NULL, &tv)) < 1) {
107 LOG(log_warning, logtype_cnid, "select timeout 1s");
110 LOG(log_error, logtype_cnid, "select: %s", strerror(errno));
116 LOG(log_error, logtype_cnid, "read: %s", strerror(errno));
128 static int recv_cred(int fd)
133 struct cmsghdr *cmsgp = NULL;
134 char buf[CMSG_SPACE(sizeof(int))];
137 memset(&msgh,0,sizeof(msgh));
138 memset(buf,0,sizeof(buf));
140 msgh.msg_name = NULL;
141 msgh.msg_namelen = 0;
146 iov[0].iov_base = dbuf;
147 iov[0].iov_len = sizeof(dbuf);
149 msgh.msg_control = buf;
150 msgh.msg_controllen = sizeof(buf);
153 ret = recvmsg(fd ,&msgh,0);
154 } while ( ret == -1 && errno == EINTR );
160 for ( cmsgp = CMSG_FIRSTHDR(&msgh); cmsgp != NULL; cmsgp = CMSG_NXTHDR(&msgh,cmsgp) ) {
161 if ( cmsgp->cmsg_level == SOL_SOCKET && cmsgp->cmsg_type == SCM_RIGHTS ) {
162 return *(int *) CMSG_DATA(cmsgp);
166 if ( ret == sizeof (int) )
167 errno = *(int *)dbuf; /* Rcvd errno */
169 errno = ENOENT; /* Default errno */
175 * Check for client requests. We keep up to fd_table_size open descriptors in
176 * fd_table. If the table is full and we get a new descriptor via
177 * control_fd, we close a random decriptor in the table to make space. The
178 * affected client will automatically reconnect. For an EOF (descriptor is
179 * closed by the client, so a read here returns 0) comm_rcv will take care of
180 * things and clean up fd_table. The same happens for any read/write errors.
183 static int check_fd(time_t timeout, const sigset_t *sigmask, time_t *now)
190 int maxfd = control_fd;
194 FD_SET(control_fd, &readfds);
196 for (i = 0; i != fds_in_use; i++) {
197 FD_SET(fd_table[i].fd, &readfds);
198 if (maxfd < fd_table[i].fd)
199 maxfd = fd_table[i].fd;
204 if ((ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tv, sigmask)) < 0) {
207 LOG(log_error, logtype_cnid, "error in select: %s",strerror(errno));
219 if (FD_ISSET(control_fd, &readfds)) {
222 fd = recv_cred(control_fd);
226 if (fds_in_use < fd_table_size) {
227 fd_table[fds_in_use].fd = fd;
228 fd_table[fds_in_use].tm = t;
233 for (i = 0; i != fds_in_use; i++) {
234 if (older <= fd_table[i].tm) {
235 older = fd_table[i].tm;
239 close(fd_table[l].fd);
246 for (i = 0; i != fds_in_use; i++) {
247 if (FD_ISSET(fd_table[i].fd, &readfds)) {
249 return fd_table[i].fd;
252 /* We should never get here */
256 int comm_init(struct db_param *dbp, int ctrlfd, int clntfd)
261 fd_table_size = dbp->fd_table_size;
263 if ((fd_table = malloc(fd_table_size * sizeof(struct connection))) == NULL) {
264 LOG(log_error, logtype_cnid, "Out of memory");
267 for (i = 0; i != fd_table_size; i++)
273 /* this one dump core in recvmsg, great */
274 if ( setsockopt(control_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
275 LOG(log_error, logtype_cnid, "setsockopt SO_PASSCRED %s", strerror(errno));
279 /* push the first client fd */
280 fd_table[fds_in_use].fd = clntfd;
295 int comm_rcv(struct cnid_dbd_rqst *rqst, time_t timeout, const sigset_t *sigmask, time_t *now)
300 if ((cur_fd = check_fd(timeout, sigmask, now)) < 0)
306 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got data on fd %u", cur_fd);
308 if (setnonblock(cur_fd, 1) != 0) {
309 LOG(log_error, logtype_cnid, "comm_rcv: setnonblock: %s", strerror(errno));
313 nametmp = rqst->name;
314 if ((b = readt(cur_fd, rqst, sizeof(struct cnid_dbd_rqst), CNID_DBD_TIMEOUT))
315 != sizeof(struct cnid_dbd_rqst)) {
317 LOG(log_error, logtype_cnid, "error reading message header: %s", strerror(errno));
318 invalidate_fd(cur_fd);
319 rqst->name = nametmp;
322 rqst->name = nametmp;
323 if (rqst->namelen && readt(cur_fd, rqst->name, rqst->namelen, CNID_DBD_TIMEOUT)
325 LOG(log_error, logtype_cnid, "error reading message name: %s", strerror(errno));
326 invalidate_fd(cur_fd);
329 /* We set this to make life easier for logging. None of the other stuff
330 needs zero terminated strings. */
331 rqst->name[rqst->namelen] = '\0';
333 LOG(log_maxdebug, logtype_cnid, "comm_rcv: got %u bytes", b + rqst->namelen);
340 int comm_snd(struct cnid_dbd_rply *rply)
347 if (!rply->namelen) {
348 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
349 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
350 invalidate_fd(cur_fd);
357 iov[0].iov_base = rply;
358 iov[0].iov_len = sizeof(struct cnid_dbd_rply);
359 iov[1].iov_base = rply->name;
360 iov[1].iov_len = rply->namelen;
361 towrite = sizeof(struct cnid_dbd_rply) +rply->namelen;
363 if (writev(cur_fd, iov, 2) != towrite) {
364 LOG(log_error, logtype_cnid, "error writing message : %s", strerror(errno));
365 invalidate_fd(cur_fd);
369 if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
370 LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
371 invalidate_fd(cur_fd);
374 if (write(cur_fd, rply->name, rply->namelen) != rply->namelen) {
375 LOG(log_error, logtype_cnid, "error writing message name: %s", strerror(errno));
376 invalidate_fd(cur_fd);