]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/comm.c
Merge master
[netatalk.git] / etc / cnid_dbd / comm.c
1 /*
2  * Copyright (C) Joerg Lenneis 2003
3  * Copyright (C) Frank Lahm 2010
4  *
5  * All Rights Reserved.  See COPYING.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <unistd.h>
17 #include <sys/param.h>
18 #include <sys/types.h>
19 #include <sys/time.h>
20 #include <sys/uio.h>
21 #define _XPG4_2 1
22 #include <sys/socket.h>
23 #include <sys/select.h>
24 #include <assert.h>
25 #include <time.h>
26
27 #include <atalk/logger.h>
28 #include <atalk/util.h>
29 #include <atalk/cnid_dbd_private.h>
30
31 #include "db_param.h"
32 #include "usockfd.h"
33 #include "comm.h"
34
35 /* Length of the space taken up by a padded control message of length len */
36 #ifndef CMSG_SPACE
37 #define CMSG_SPACE(len) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len))
38 #endif
39
40
41 struct connection {
42     time_t tm;                    /* When respawned last */
43     int    fd;
44 };
45
46 static int   control_fd;
47 static int   cur_fd;
48 static struct connection *fd_table;
49 static int  fd_table_size;
50 static int  fds_in_use = 0;
51
52
53 static void invalidate_fd(int fd)
54 {
55     int i;
56
57     if (fd == control_fd)
58         return;
59     for (i = 0; i != fds_in_use; i++)
60         if (fd_table[i].fd == fd)
61             break;
62
63     assert(i < fds_in_use);
64
65     fds_in_use--;
66     fd_table[i] = fd_table[fds_in_use];
67     fd_table[fds_in_use].fd = -1;
68     close(fd);
69     return;
70 }
71
72
73 /*
74  *  Check for client requests. We keep up to fd_table_size open descriptors in
75  *  fd_table. If the table is full and we get a new descriptor via
76  *  control_fd, we close a random decriptor in the table to make space. The
77  *  affected client will automatically reconnect. For an EOF (descriptor is
78  *  closed by the client, so a read here returns 0) comm_rcv will take care of
79  *  things and clean up fd_table. The same happens for any read/write errors.
80  */
81
82 static int check_fd(time_t timeout, const sigset_t *sigmask, time_t *now)
83 {
84     int fd;
85     fd_set readfds;
86     struct timespec tv;
87     int ret;
88     int i;
89     int maxfd = control_fd;
90     time_t t;
91
92     FD_ZERO(&readfds);
93     FD_SET(control_fd, &readfds);
94
95     for (i = 0; i != fds_in_use; i++) {
96         FD_SET(fd_table[i].fd, &readfds);
97         if (maxfd < fd_table[i].fd)
98             maxfd = fd_table[i].fd;
99     }
100
101     tv.tv_nsec = 0;
102     tv.tv_sec  = timeout;
103     if ((ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tv, sigmask)) < 0) {
104         if (errno == EINTR)
105             return 0;
106         LOG(log_error, logtype_cnid, "error in select: %s",strerror(errno));
107         return -1;
108     }
109
110     time(&t);
111     if (now)
112         *now = t;
113
114     if (!ret)
115         return 0;
116
117
118     if (FD_ISSET(control_fd, &readfds)) {
119         int    l = 0;
120
121         fd = recv_fd(control_fd, 0);
122         if (fd < 0) {
123             return -1;
124         }
125         if (fds_in_use < fd_table_size) {
126             fd_table[fds_in_use].fd = fd;
127             fd_table[fds_in_use].tm = t;
128             fds_in_use++;
129         } else {
130             time_t older = t;
131
132             for (i = 0; i != fds_in_use; i++) {
133                 if (older <= fd_table[i].tm) {
134                     older = fd_table[i].tm;
135                     l = i;
136                 }
137             }
138             close(fd_table[l].fd);
139             fd_table[l].fd = fd;
140             fd_table[l].tm = t;
141         }
142         return 0;
143     }
144
145     for (i = 0; i != fds_in_use; i++) {
146         if (FD_ISSET(fd_table[i].fd, &readfds)) {
147             fd_table[i].tm = t;
148             return fd_table[i].fd;
149         }
150     }
151     /* We should never get here */
152     return 0;
153 }
154
155 int comm_init(struct db_param *dbp, int ctrlfd, int clntfd)
156 {
157     int i;
158
159     fds_in_use = 0;
160     fd_table_size = dbp->fd_table_size;
161
162     if ((fd_table = malloc(fd_table_size * sizeof(struct connection))) == NULL) {
163         LOG(log_error, logtype_cnid, "Out of memory");
164         return -1;
165     }
166     for (i = 0; i != fd_table_size; i++)
167         fd_table[i].fd = -1;
168     /* from dup2 */
169     control_fd = ctrlfd;
170 #if 0
171     int b = 1;
172     /* this one dump core in recvmsg, great */
173     if ( setsockopt(control_fd, SOL_SOCKET, SO_PASSCRED, &b, sizeof (b)) < 0) {
174         LOG(log_error, logtype_cnid, "setsockopt SO_PASSCRED %s",  strerror(errno));
175         return -1;
176     }
177 #endif
178     /* push the first client fd */
179     fd_table[fds_in_use].fd = clntfd;
180     fds_in_use++;
181
182     return 0;
183 }
184
185 /* ------------
186    nbe of clients
187 */
188 int comm_nbe(void)
189 {
190     return fds_in_use;
191 }
192
193 /* ------------ */
194 int comm_rcv(struct cnid_dbd_rqst *rqst, time_t timeout, const sigset_t *sigmask, time_t *now)
195 {
196     char *nametmp;
197     int b;
198
199     if ((cur_fd = check_fd(timeout, sigmask, now)) < 0)
200         return -1;
201
202     if (!cur_fd)
203         return 0;
204
205     LOG(log_maxdebug, logtype_cnid, "comm_rcv: got data on fd %u", cur_fd);
206
207     if (setnonblock(cur_fd, 1) != 0) {
208         LOG(log_error, logtype_cnid, "comm_rcv: setnonblock: %s", strerror(errno));
209         return -1;
210     }
211
212     nametmp = (char *)rqst->name;
213     if ((b = readt(cur_fd, rqst, sizeof(struct cnid_dbd_rqst), 1, CNID_DBD_TIMEOUT))
214         != sizeof(struct cnid_dbd_rqst)) {
215         if (b)
216             LOG(log_error, logtype_cnid, "error reading message header: %s", strerror(errno));
217         invalidate_fd(cur_fd);
218         rqst->name = nametmp;
219         return 0;
220     }
221     rqst->name = nametmp;
222     if (rqst->namelen && readt(cur_fd, (char *)rqst->name, rqst->namelen, 1, CNID_DBD_TIMEOUT)
223         != rqst->namelen) {
224         LOG(log_error, logtype_cnid, "error reading message name: %s", strerror(errno));
225         invalidate_fd(cur_fd);
226         return 0;
227     }
228     /* We set this to make life easier for logging. None of the other stuff
229        needs zero terminated strings. */
230     ((char *)(rqst->name))[rqst->namelen] = '\0';
231
232     LOG(log_maxdebug, logtype_cnid, "comm_rcv: got %u bytes", b + rqst->namelen);
233
234     return 1;
235 }
236
237 /* ------------ */
238 #define USE_WRITEV
239 int comm_snd(struct cnid_dbd_rply *rply)
240 {
241 #ifdef USE_WRITEV
242     struct iovec iov[2];
243     size_t towrite;
244 #endif
245
246     if (!rply->namelen) {
247         if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
248             LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
249             invalidate_fd(cur_fd);
250             return 0;
251         }
252         return 1;
253     }
254 #ifdef USE_WRITEV
255
256     iov[0].iov_base = rply;
257     iov[0].iov_len = sizeof(struct cnid_dbd_rply);
258     iov[1].iov_base = rply->name;
259     iov[1].iov_len = rply->namelen;
260     towrite = sizeof(struct cnid_dbd_rply) +rply->namelen;
261
262     if (writev(cur_fd, iov, 2) != towrite) {
263         LOG(log_error, logtype_cnid, "error writing message : %s", strerror(errno));
264         invalidate_fd(cur_fd);
265         return 0;
266     }
267 #else
268     if (write(cur_fd, rply, sizeof(struct cnid_dbd_rply)) != sizeof(struct cnid_dbd_rply)) {
269         LOG(log_error, logtype_cnid, "error writing message header: %s", strerror(errno));
270         invalidate_fd(cur_fd);
271         return 0;
272     }
273     if (write(cur_fd, rply->name, rply->namelen) != rply->namelen) {
274         LOG(log_error, logtype_cnid, "error writing message name: %s", strerror(errno));
275         invalidate_fd(cur_fd);
276         return 0;
277     }
278 #endif
279     return 1;
280 }
281
282