3
0

isrv.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 Denys Vlasenko
  7. *
  8. * Licensed under GPLv2, see file LICENSE in this source tree.
  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); /* unsuccessful peer start */
  161. }
  162. static void handle_fd_set(isrv_state_t *state, fd_set *fds, int (*h)(int, void **))
  163. {
  164. enum { LONG_CNT = sizeof(fd_set) / sizeof(long) };
  165. int fds_pos;
  166. int fd, peer;
  167. /* need to know value at _the beginning_ of this routine */
  168. int fd_cnt = FD_COUNT;
  169. BUILD_BUG_ON(LONG_CNT * sizeof(long) != sizeof(fd_set));
  170. fds_pos = 0;
  171. while (1) {
  172. /* Find next nonzero bit */
  173. while (fds_pos < LONG_CNT) {
  174. if (((long*)fds)[fds_pos] == 0) {
  175. fds_pos++;
  176. continue;
  177. }
  178. /* Found non-zero word */
  179. fd = fds_pos * sizeof(long)*8; /* word# -> bit# */
  180. while (1) {
  181. if (FD_ISSET(fd, fds)) {
  182. FD_CLR(fd, fds);
  183. goto found_fd;
  184. }
  185. fd++;
  186. }
  187. }
  188. break; /* all words are zero */
  189. found_fd:
  190. if (fd >= fd_cnt) { /* paranoia */
  191. DPRINTF("handle_fd_set: fd > fd_cnt?? (%d > %d)",
  192. fd, fd_cnt);
  193. break;
  194. }
  195. DPRINTF("handle_fd_set: fd %d is active", fd);
  196. peer = FD2PEER[fd];
  197. if (peer < 0)
  198. continue; /* peer is already gone */
  199. if (peer == 0) {
  200. handle_accept(state, fd);
  201. continue;
  202. }
  203. DPRINTF("h(fd:%d)", fd);
  204. if (h(fd, &PARAM_TBL[peer])) {
  205. /* this peer is gone */
  206. remove_peer(state, peer);
  207. } else if (TIMEOUT) {
  208. TIMEO_TBL[peer] = monotonic_sec();
  209. }
  210. }
  211. }
  212. static void handle_timeout(isrv_state_t *state, int (*do_timeout)(void **))
  213. {
  214. int n, peer;
  215. peer = PEER_COUNT-1;
  216. /* peer 0 is not checked */
  217. while (peer > 0) {
  218. DPRINTF("peer %d: time diff %d", peer,
  219. (int)(CURTIME - TIMEO_TBL[peer]));
  220. if ((CURTIME - TIMEO_TBL[peer]) >= TIMEOUT) {
  221. DPRINTF("peer %d: do_timeout()", peer);
  222. n = do_timeout(&PARAM_TBL[peer]);
  223. if (n)
  224. remove_peer(state, peer);
  225. }
  226. peer--;
  227. }
  228. }
  229. /* Driver */
  230. void isrv_run(
  231. int listen_fd,
  232. int (*new_peer)(isrv_state_t *state, int fd),
  233. int (*do_rd)(int fd, void **),
  234. int (*do_wr)(int fd, void **),
  235. int (*do_timeout)(void **),
  236. int timeout,
  237. int linger_timeout)
  238. {
  239. isrv_state_t *state = xzalloc(sizeof(*state));
  240. state->new_peer = new_peer;
  241. state->timeout = timeout;
  242. /* register "peer" #0 - it will accept new connections */
  243. isrv_register_peer(state, NULL);
  244. isrv_register_fd(state, /*peer:*/ 0, listen_fd);
  245. isrv_want_rd(state, listen_fd);
  246. /* remember flags to make blocking<->nonblocking switch faster */
  247. /* (suppress gcc warning "cast from ptr to int of different size") */
  248. PARAM_TBL[0] = (void*)(ptrdiff_t)(fcntl(listen_fd, F_GETFL));
  249. while (1) {
  250. struct timeval tv;
  251. fd_set rd;
  252. fd_set wr;
  253. fd_set *wrp = NULL;
  254. int n;
  255. tv.tv_sec = timeout;
  256. if (PEER_COUNT <= 1)
  257. tv.tv_sec = linger_timeout;
  258. tv.tv_usec = 0;
  259. rd = state->rd;
  260. if (WR_COUNT) {
  261. wr = state->wr;
  262. wrp = &wr;
  263. }
  264. DPRINTF("run: select(FD_COUNT:%d,timeout:%d)...",
  265. FD_COUNT, (int)tv.tv_sec);
  266. n = select(FD_COUNT, &rd, wrp, NULL, tv.tv_sec ? &tv : NULL);
  267. DPRINTF("run: ...select:%d", n);
  268. if (n < 0) {
  269. if (errno != EINTR)
  270. bb_perror_msg("select");
  271. continue;
  272. }
  273. if (n == 0 && linger_timeout && PEER_COUNT <= 1)
  274. break;
  275. if (timeout) {
  276. time_t t = monotonic_sec();
  277. if (t != CURTIME) {
  278. CURTIME = t;
  279. handle_timeout(state, do_timeout);
  280. }
  281. }
  282. if (n > 0) {
  283. handle_fd_set(state, &rd, do_rd);
  284. if (wrp)
  285. handle_fd_set(state, wrp, do_wr);
  286. }
  287. }
  288. DPRINTF("run: bailout");
  289. /* NB: accept socket is not closed. Caller is to decide what to do */
  290. }