libbb,crond,lash: fix getopt32 (don't know how it managed to slip through)
[oweals/busybox.git] / networking / isrv.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Generic non-forking server infrastructure.
4  * Intended to make writing telnetd-type servers easier.
5  *
6  * Copyright (C) 2007 Denis Vlasenko
7  *
8  * Licensed under GPL version 2, see file LICENSE in this tarball for details.
9  */
10
11 #include "libbb.h"
12 #include "isrv.h"
13
14 #define DEBUG 0
15
16 #if DEBUG
17 #define DPRINTF(args...) bb_error_msg(args)
18 #else
19 #define DPRINTF(args...) ((void)0)
20 #endif
21
22 /* Helpers */
23
24 /* Even if _POSIX_MONOTONIC_CLOCK is defined, this
25  * may require librt */
26 #if 0 /*def _POSIX_MONOTONIC_CLOCK*/
27 static time_t monotonic_time(void)
28 {
29         struct timespec ts;
30         if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
31                 time(&ts.tv_sec);
32         return ts.tv_sec;
33 }
34 #else
35 #define monotonic_time() (time(NULL))
36 #endif
37
38 /* Opaque structure */
39
40 struct isrv_state_t {
41         short  *fd2peer; /* one per registered fd */
42         void  **param_tbl; /* one per registered peer */
43         /* one per registered peer; doesn't exist if !timeout */
44         time_t *timeo_tbl;
45         int   (*new_peer)(isrv_state_t *state, int fd);
46         time_t  curtime;
47         int     timeout;
48         int     fd_count;
49         int     peer_count;
50         int     wr_count;
51         fd_set  rd;
52         fd_set  wr;
53 };
54 #define FD2PEER    (state->fd2peer)
55 #define PARAM_TBL  (state->param_tbl)
56 #define TIMEO_TBL  (state->timeo_tbl)
57 #define CURTIME    (state->curtime)
58 #define TIMEOUT    (state->timeout)
59 #define FD_COUNT   (state->fd_count)
60 #define PEER_COUNT (state->peer_count)
61 #define WR_COUNT   (state->wr_count)
62
63 /* callback */
64 void isrv_want_rd(isrv_state_t *state, int fd)
65 {
66         FD_SET(fd, &state->rd);
67 }
68
69 /* callback */
70 void isrv_want_wr(isrv_state_t *state, int fd)
71 {
72         if (!FD_ISSET(fd, &state->wr)) {
73                 WR_COUNT++;
74                 FD_SET(fd, &state->wr);
75         }
76 }
77
78 /* callback */
79 void isrv_dont_want_rd(isrv_state_t *state, int fd)
80 {
81         FD_CLR(fd, &state->rd);
82 }
83
84 /* callback */
85 void isrv_dont_want_wr(isrv_state_t *state, int fd)
86 {
87         if (FD_ISSET(fd, &state->wr)) {
88                 WR_COUNT--;
89                 FD_CLR(fd, &state->wr);
90         }
91 }
92
93 /* callback */
94 int isrv_register_fd(isrv_state_t *state, int peer, int fd)
95 {
96         int n;
97
98         DPRINTF("register_fd(peer:%d,fd:%d)", peer, fd);
99
100         if (FD_COUNT >= FD_SETSIZE) return -1;
101         if (FD_COUNT <= fd) {
102                 n = FD_COUNT;
103                 FD_COUNT = fd + 1;
104
105                 DPRINTF("register_fd: FD_COUNT %d", FD_COUNT);
106
107                 FD2PEER = xrealloc(FD2PEER, FD_COUNT * sizeof(FD2PEER[0]));
108                 while (n < fd) FD2PEER[n++] = -1;
109         }
110
111         DPRINTF("register_fd: FD2PEER[%d] = %d", fd, peer);
112
113         FD2PEER[fd] = peer;
114         return 0;
115 }
116
117 /* callback */
118 void isrv_close_fd(isrv_state_t *state, int fd)
119 {
120         DPRINTF("close_fd(%d)", fd);
121
122         close(fd);
123         isrv_dont_want_rd(state, fd);
124         if (WR_COUNT) isrv_dont_want_wr(state, fd);
125
126         FD2PEER[fd] = -1;
127         if (fd == FD_COUNT-1) {
128                 do fd--; while (fd >= 0 && FD2PEER[fd] == -1);
129                 FD_COUNT = fd + 1;
130
131                 DPRINTF("close_fd: FD_COUNT %d", FD_COUNT);
132
133                 FD2PEER = xrealloc(FD2PEER, FD_COUNT * sizeof(FD2PEER[0]));
134         }
135 }
136
137 /* callback */
138 int isrv_register_peer(isrv_state_t *state, void *param)
139 {
140         int n;
141
142         if (PEER_COUNT >= FD_SETSIZE) return -1;
143         n = PEER_COUNT++;
144
145         DPRINTF("register_peer: PEER_COUNT %d", PEER_COUNT);
146
147         PARAM_TBL = xrealloc(PARAM_TBL, PEER_COUNT * sizeof(PARAM_TBL[0]));
148         PARAM_TBL[n] = param;
149         if (TIMEOUT) {
150                 TIMEO_TBL = xrealloc(TIMEO_TBL, PEER_COUNT * sizeof(TIMEO_TBL[0]));
151                 TIMEO_TBL[n] = CURTIME;
152         }
153         return n;
154 }
155
156 static void remove_peer(isrv_state_t *state, int peer)
157 {
158         int movesize;
159         int fd;
160
161         DPRINTF("remove_peer(%d)", peer);
162
163         fd = FD_COUNT - 1;
164         while (fd >= 0) {
165                 if (FD2PEER[fd] == peer) {
166                         isrv_close_fd(state, fd);
167                         fd--;
168                         continue;
169                 }
170                 if (FD2PEER[fd] > peer)
171                         FD2PEER[fd]--;
172                 fd--;
173         }
174
175         PEER_COUNT--;
176         DPRINTF("remove_peer: PEER_COUNT %d", PEER_COUNT);
177
178         movesize = (PEER_COUNT - peer) * sizeof(void*);
179         if (movesize > 0) {
180                 memcpy(&PARAM_TBL[peer], &PARAM_TBL[peer+1], movesize);
181                 if (TIMEOUT)
182                         memcpy(&TIMEO_TBL[peer], &TIMEO_TBL[peer+1], movesize);
183         }
184         PARAM_TBL = xrealloc(PARAM_TBL, PEER_COUNT * sizeof(PARAM_TBL[0]));
185         if (TIMEOUT)
186                 TIMEO_TBL = xrealloc(TIMEO_TBL, PEER_COUNT * sizeof(TIMEO_TBL[0]));
187 }
188
189 static void handle_accept(isrv_state_t *state, int fd)
190 {
191         int n, newfd;
192
193         /* suppress gcc warning "cast from ptr to int of different size" */
194         fcntl(fd, F_SETFL, (int)(ptrdiff_t)(PARAM_TBL[0]) | O_NONBLOCK);
195         newfd = accept(fd, NULL, 0);
196         fcntl(fd, F_SETFL, (int)(ptrdiff_t)(PARAM_TBL[0]));
197         if (newfd < 0) {
198                 if (errno == EAGAIN) return;
199                 /* Most probably someone gave us wrong fd type
200                  * (for example, non-socket). Don't want
201                  * to loop forever. */
202                 bb_perror_msg_and_die("accept");
203         }
204
205         DPRINTF("new_peer(%d)", newfd);
206         n = state->new_peer(state, newfd);
207         if (n)
208                 remove_peer(state, n); /* unsuccesful peer start */
209 }
210
211 void BUG_sizeof_fd_set_is_strange(void);
212 static void handle_fd_set(isrv_state_t *state, fd_set *fds, int (*h)(int, void **))
213 {
214         enum { LONG_CNT = sizeof(fd_set) / sizeof(long) };
215         int fds_pos;
216         int fd, peer;
217         /* need to know value at _the beginning_ of this routine */
218         int fd_cnt = FD_COUNT;
219
220         if (LONG_CNT * sizeof(long) != sizeof(fd_set))
221                 BUG_sizeof_fd_set_is_strange();
222
223         fds_pos = 0;
224         while (1) {
225                 /* Find next nonzero bit */
226                 while (fds_pos < LONG_CNT) {
227                         if (((long*)fds)[fds_pos] == 0) {
228                                 fds_pos++;
229                                 continue;
230                         }
231                         /* Found non-zero word */
232                         fd = fds_pos * sizeof(long)*8; /* word# -> bit# */
233                         while (1) {
234                                 if (FD_ISSET(fd, fds)) {
235                                         FD_CLR(fd, fds);
236                                         goto found_fd;
237                                 }
238                                 fd++;
239                         }
240                 }
241                 break; /* all words are zero */
242  found_fd:
243                 if (fd >= fd_cnt) { /* paranoia */
244                         DPRINTF("handle_fd_set: fd > fd_cnt?? (%d > %d)",
245                                         fd, fd_cnt);
246                         break;
247                 }
248                 DPRINTF("handle_fd_set: fd %d is active", fd);
249                 peer = FD2PEER[fd];
250                 if (peer < 0)
251                         continue; /* peer is already gone */
252                 if (peer == 0) {
253                         handle_accept(state, fd);
254                         continue;
255                 }
256                 DPRINTF("h(fd:%d)", fd);
257                 if (h(fd, &PARAM_TBL[peer])) {
258                         /* this peer is gone */
259                         remove_peer(state, peer);
260                 } else if (TIMEOUT) {
261                         TIMEO_TBL[peer] = monotonic_time();
262                 }
263         }
264 }
265
266 static void handle_timeout(isrv_state_t *state, int (*do_timeout)(void **))
267 {
268         int n, peer;
269         peer = PEER_COUNT-1;
270         /* peer 0 is not checked */
271         while (peer > 0) {
272                 DPRINTF("peer %d: time diff %d", peer,
273                                 (int)(CURTIME - TIMEO_TBL[peer]));
274                 if ((CURTIME - TIMEO_TBL[peer]) >= TIMEOUT) {
275                         DPRINTF("peer %d: do_timeout()", peer);
276                         n = do_timeout(&PARAM_TBL[peer]);
277                         if (n)
278                                 remove_peer(state, peer);
279                 }
280                 peer--;
281         }
282 }
283
284 /* Driver */
285 void isrv_run(
286         int listen_fd,
287         int (*new_peer)(isrv_state_t *state, int fd),
288         int (*do_rd)(int fd, void **),
289         int (*do_wr)(int fd, void **),
290         int (*do_timeout)(void **),
291         int timeout,
292         int linger_timeout)
293 {
294         isrv_state_t *state = xzalloc(sizeof(*state));
295         state->new_peer = new_peer;
296         state->timeout  = timeout;
297
298         /* register "peer" #0 - it will accept new connections */
299         isrv_register_peer(state, NULL);
300         isrv_register_fd(state, /*peer:*/ 0, listen_fd);
301         isrv_want_rd(state, listen_fd);
302         /* remember flags to make blocking<->nonblocking switch faster */
303         /* (suppress gcc warning "cast from ptr to int of different size") */
304         PARAM_TBL[0] = (void*)(ptrdiff_t)(fcntl(listen_fd, F_GETFL));
305
306         while (1) {
307                 struct timeval tv;
308                 fd_set rd;
309                 fd_set wr;
310                 fd_set *wrp = NULL;
311                 int n;
312
313                 tv.tv_sec = timeout;
314                 if (PEER_COUNT <= 1)
315                         tv.tv_sec = linger_timeout;
316                 tv.tv_usec = 0;
317                 rd = state->rd;
318                 if (WR_COUNT) {
319                         wr = state->wr;
320                         wrp = &wr;
321                 }
322
323                 DPRINTF("run: select(FD_COUNT:%d,timeout:%d)...",
324                                 FD_COUNT, (int)tv.tv_sec);
325                 n = select(FD_COUNT, &rd, wrp, NULL, tv.tv_sec ? &tv : NULL);
326                 DPRINTF("run: ...select:%d", n);
327
328                 if (n < 0) {
329                         if (errno != EINTR)
330                                 bb_perror_msg("select");
331                         continue;
332                 }
333
334                 if (n == 0 && linger_timeout && PEER_COUNT <= 1)
335                         break;
336
337                 if (timeout) {
338                         time_t t = monotonic_time();
339                         if (t != CURTIME) {
340                                 CURTIME = t;
341                                 handle_timeout(state, do_timeout);
342                         }
343                 }
344                 if (n > 0) {
345                         handle_fd_set(state, &rd, do_rd);
346                         if (wrp)
347                                 handle_fd_set(state, wrp, do_wr);
348                 }
349         }
350         DPRINTF("run: bailout");
351         /* NB: accept socket is not closed. Caller is to decide what to do */
352 }