isrv.c 7.4 KB

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