isrv.c 7.8 KB

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