1 /* vi: set sw=4 ts=4: */
3 * Generic non-forking server infrastructure.
4 * Intended to make writing telnetd-type servers easier.
6 * Copyright (C) 2007 Denis Vlasenko
8 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
17 #define DPRINTF(args...) bb_error_msg(args)
19 #define DPRINTF(args...) ((void)0)
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)
30 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
35 #define monotonic_time() (time(NULL))
38 /* Opaque structure */
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 */
45 int (*new_peer)(isrv_state_t *state, int fd);
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)
64 void isrv_want_rd(isrv_state_t *state, int fd)
66 FD_SET(fd, &state->rd);
70 void isrv_want_wr(isrv_state_t *state, int fd)
72 if (!FD_ISSET(fd, &state->wr)) {
74 FD_SET(fd, &state->wr);
79 void isrv_dont_want_rd(isrv_state_t *state, int fd)
81 FD_CLR(fd, &state->rd);
85 void isrv_dont_want_wr(isrv_state_t *state, int fd)
87 if (FD_ISSET(fd, &state->wr)) {
89 FD_CLR(fd, &state->wr);
94 int isrv_register_fd(isrv_state_t *state, int peer, int fd)
98 DPRINTF("register_fd(peer:%d,fd:%d)", peer, fd);
100 if (FD_COUNT >= FD_SETSIZE) return -1;
101 if (FD_COUNT <= fd) {
105 DPRINTF("register_fd: FD_COUNT %d", FD_COUNT);
107 FD2PEER = xrealloc(FD2PEER, FD_COUNT * sizeof(FD2PEER[0]));
108 while (n < fd) FD2PEER[n++] = -1;
111 DPRINTF("register_fd: FD2PEER[%d] = %d", fd, peer);
118 void isrv_close_fd(isrv_state_t *state, int fd)
120 DPRINTF("close_fd(%d)", fd);
123 isrv_dont_want_rd(state, fd);
124 if (WR_COUNT) isrv_dont_want_wr(state, fd);
127 if (fd == FD_COUNT-1) {
128 do fd--; while (fd >= 0 && FD2PEER[fd] == -1);
131 DPRINTF("close_fd: FD_COUNT %d", FD_COUNT);
133 FD2PEER = xrealloc(FD2PEER, FD_COUNT * sizeof(FD2PEER[0]));
138 int isrv_register_peer(isrv_state_t *state, void *param)
142 if (PEER_COUNT >= FD_SETSIZE) return -1;
145 DPRINTF("register_peer: PEER_COUNT %d", PEER_COUNT);
147 PARAM_TBL = xrealloc(PARAM_TBL, PEER_COUNT * sizeof(PARAM_TBL[0]));
148 PARAM_TBL[n] = param;
150 TIMEO_TBL = xrealloc(TIMEO_TBL, PEER_COUNT * sizeof(TIMEO_TBL[0]));
151 TIMEO_TBL[n] = CURTIME;
156 static void remove_peer(isrv_state_t *state, int peer)
161 DPRINTF("remove_peer(%d)", peer);
165 if (FD2PEER[fd] == peer) {
166 isrv_close_fd(state, fd);
170 if (FD2PEER[fd] > peer)
176 DPRINTF("remove_peer: PEER_COUNT %d", PEER_COUNT);
178 movesize = (PEER_COUNT - peer) * sizeof(void*);
180 memcpy(&PARAM_TBL[peer], &PARAM_TBL[peer+1], movesize);
182 memcpy(&TIMEO_TBL[peer], &TIMEO_TBL[peer+1], movesize);
184 PARAM_TBL = xrealloc(PARAM_TBL, PEER_COUNT * sizeof(PARAM_TBL[0]));
186 TIMEO_TBL = xrealloc(TIMEO_TBL, PEER_COUNT * sizeof(TIMEO_TBL[0]));
189 static void handle_accept(isrv_state_t *state, int fd)
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]));
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");
205 DPRINTF("new_peer(%d)", newfd);
206 n = state->new_peer(state, newfd);
208 remove_peer(state, n); /* unsuccesful peer start */
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 **))
214 enum { LONG_CNT = sizeof(fd_set) / sizeof(long) };
217 /* need to know value at _the beginning_ of this routine */
218 int fd_cnt = FD_COUNT;
220 if (LONG_CNT * sizeof(long) != sizeof(fd_set))
221 BUG_sizeof_fd_set_is_strange();
225 /* Find next nonzero bit */
226 while (fds_pos < LONG_CNT) {
227 if (((long*)fds)[fds_pos] == 0) {
231 /* Found non-zero word */
232 fd = fds_pos * sizeof(long)*8; /* word# -> bit# */
234 if (FD_ISSET(fd, fds)) {
241 break; /* all words are zero */
243 if (fd >= fd_cnt) { /* paranoia */
244 DPRINTF("handle_fd_set: fd > fd_cnt?? (%d > %d)",
248 DPRINTF("handle_fd_set: fd %d is active", fd);
251 continue; /* peer is already gone */
253 handle_accept(state, fd);
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();
266 static void handle_timeout(isrv_state_t *state, int (*do_timeout)(void **))
270 /* peer 0 is not checked */
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]);
278 remove_peer(state, peer);
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 **),
294 isrv_state_t *state = xzalloc(sizeof(*state));
295 state->new_peer = new_peer;
296 state->timeout = timeout;
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, 0));
315 tv.tv_sec = linger_timeout;
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);
330 bb_perror_msg("select");
334 if (n == 0 && linger_timeout && PEER_COUNT <= 1)
338 time_t t = monotonic_time();
341 handle_timeout(state, do_timeout);
345 handle_fd_set(state, &rd, do_rd);
347 handle_fd_set(state, wrp, do_wr);
350 DPRINTF("run: bailout");
351 /* NB: accept socket is not closed. Caller is to decide what to do */