poll.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* Licensed under LGPLv2.1+ - see LICENSE file for details */
  2. #include "io.h"
  3. #include "backend.h"
  4. #include <assert.h>
  5. #include <poll.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <limits.h>
  10. #include <errno.h>
  11. #include <ccan/time/time.h>
  12. #include <ccan/timer/timer.h>
  13. static size_t num_fds = 0, max_fds = 0, num_waiting = 0;
  14. static struct pollfd *pollfds = NULL;
  15. static struct fd **fds = NULL;
  16. static LIST_HEAD(closing);
  17. static LIST_HEAD(always);
  18. static struct timemono (*nowfn)(void) = time_mono;
  19. static int (*pollfn)(struct pollfd *fds, nfds_t nfds, int timeout) = poll;
  20. struct timemono (*io_time_override(struct timemono (*now)(void)))(void)
  21. {
  22. struct timemono (*old)(void) = nowfn;
  23. nowfn = now;
  24. return old;
  25. }
  26. int (*io_poll_override(int (*poll)(struct pollfd *fds, nfds_t nfds, int timeout)))(struct pollfd *, nfds_t, int)
  27. {
  28. int (*old)(struct pollfd *fds, nfds_t nfds, int timeout) = pollfn;
  29. pollfn = poll;
  30. return old;
  31. }
  32. static bool add_fd(struct fd *fd, short events)
  33. {
  34. if (!max_fds) {
  35. assert(num_fds == 0);
  36. pollfds = tal_arr(NULL, struct pollfd, 8);
  37. if (!pollfds)
  38. return false;
  39. fds = tal_arr(pollfds, struct fd *, 8);
  40. if (!fds)
  41. return false;
  42. max_fds = 8;
  43. }
  44. if (num_fds + 1 > max_fds) {
  45. size_t num = max_fds * 2;
  46. if (!tal_resize(&pollfds, num))
  47. return false;
  48. if (!tal_resize(&fds, num))
  49. return false;
  50. max_fds = num;
  51. }
  52. pollfds[num_fds].events = events;
  53. /* In case it's idle. */
  54. if (!events)
  55. pollfds[num_fds].fd = -fd->fd;
  56. else
  57. pollfds[num_fds].fd = fd->fd;
  58. pollfds[num_fds].revents = 0; /* In case we're iterating now */
  59. fds[num_fds] = fd;
  60. fd->backend_info = num_fds;
  61. num_fds++;
  62. if (events)
  63. num_waiting++;
  64. return true;
  65. }
  66. static void del_fd(struct fd *fd)
  67. {
  68. size_t n = fd->backend_info;
  69. assert(n != -1);
  70. assert(n < num_fds);
  71. if (pollfds[n].events)
  72. num_waiting--;
  73. if (n != num_fds - 1) {
  74. /* Move last one over us. */
  75. pollfds[n] = pollfds[num_fds-1];
  76. fds[n] = fds[num_fds-1];
  77. assert(fds[n]->backend_info == num_fds-1);
  78. fds[n]->backend_info = n;
  79. } else if (num_fds == 1) {
  80. /* Free everything when no more fds. */
  81. pollfds = tal_free(pollfds);
  82. fds = NULL;
  83. max_fds = 0;
  84. }
  85. num_fds--;
  86. fd->backend_info = -1;
  87. }
  88. static void destroy_listener(struct io_listener *l)
  89. {
  90. close(l->fd.fd);
  91. del_fd(&l->fd);
  92. }
  93. bool add_listener(struct io_listener *l)
  94. {
  95. if (!add_fd(&l->fd, POLLIN))
  96. return false;
  97. tal_add_destructor(l, destroy_listener);
  98. return true;
  99. }
  100. void remove_from_always(struct io_conn *conn)
  101. {
  102. list_del_init(&conn->always);
  103. }
  104. void backend_new_always(struct io_conn *conn)
  105. {
  106. /* In case it's already in always list. */
  107. list_del(&conn->always);
  108. list_add_tail(&always, &conn->always);
  109. }
  110. void backend_new_plan(struct io_conn *conn)
  111. {
  112. struct pollfd *pfd = &pollfds[conn->fd.backend_info];
  113. if (pfd->events)
  114. num_waiting--;
  115. pfd->events = 0;
  116. if (conn->plan[IO_IN].status == IO_POLLING)
  117. pfd->events |= POLLIN;
  118. if (conn->plan[IO_OUT].status == IO_POLLING)
  119. pfd->events |= POLLOUT;
  120. if (pfd->events) {
  121. num_waiting++;
  122. pfd->fd = conn->fd.fd;
  123. } else {
  124. pfd->fd = -conn->fd.fd;
  125. }
  126. }
  127. void backend_wake(const void *wait)
  128. {
  129. unsigned int i;
  130. for (i = 0; i < num_fds; i++) {
  131. struct io_conn *c;
  132. /* Ignore listeners */
  133. if (fds[i]->listener)
  134. continue;
  135. c = (void *)fds[i];
  136. if (c->plan[IO_IN].status == IO_WAITING
  137. && c->plan[IO_IN].arg.u1.const_vp == wait)
  138. io_do_wakeup(c, IO_IN);
  139. if (c->plan[IO_OUT].status == IO_WAITING
  140. && c->plan[IO_OUT].arg.u1.const_vp == wait)
  141. io_do_wakeup(c, IO_OUT);
  142. }
  143. }
  144. static void destroy_conn(struct io_conn *conn, bool close_fd)
  145. {
  146. int saved_errno = errno;
  147. if (close_fd)
  148. close(conn->fd.fd);
  149. del_fd(&conn->fd);
  150. /* In case it's on always list, remove it. */
  151. list_del_init(&conn->always);
  152. /* errno saved/restored by tal_free itself. */
  153. if (conn->finish) {
  154. errno = saved_errno;
  155. conn->finish(conn, conn->finish_arg);
  156. }
  157. }
  158. static void destroy_conn_close_fd(struct io_conn *conn)
  159. {
  160. destroy_conn(conn, true);
  161. }
  162. bool add_conn(struct io_conn *c)
  163. {
  164. if (!add_fd(&c->fd, 0))
  165. return false;
  166. tal_add_destructor(c, destroy_conn_close_fd);
  167. return true;
  168. }
  169. void cleanup_conn_without_close(struct io_conn *conn)
  170. {
  171. tal_del_destructor(conn, destroy_conn_close_fd);
  172. destroy_conn(conn, false);
  173. }
  174. static void accept_conn(struct io_listener *l)
  175. {
  176. int fd = accept(l->fd.fd, NULL, NULL);
  177. /* FIXME: What to do here? */
  178. if (fd < 0)
  179. return;
  180. io_new_conn(l->ctx, fd, l->init, l->arg);
  181. }
  182. static bool handle_always(void)
  183. {
  184. bool ret = false;
  185. struct io_conn *conn;
  186. while ((conn = list_pop(&always, struct io_conn, always)) != NULL) {
  187. assert(conn->plan[IO_IN].status == IO_ALWAYS
  188. || conn->plan[IO_OUT].status == IO_ALWAYS);
  189. /* Re-initialize, for next time. */
  190. list_node_init(&conn->always);
  191. io_do_always(conn);
  192. ret = true;
  193. }
  194. return ret;
  195. }
  196. /* This is the main loop. */
  197. void *io_loop(struct timers *timers, struct timer **expired)
  198. {
  199. void *ret;
  200. /* if timers is NULL, expired must be. If not, not. */
  201. assert(!timers == !expired);
  202. /* Make sure this is NULL if we exit for some other reason. */
  203. if (expired)
  204. *expired = NULL;
  205. while (!io_loop_return) {
  206. int i, r, ms_timeout = -1;
  207. if (handle_always()) {
  208. /* Could have started/finished more. */
  209. continue;
  210. }
  211. /* Everything closed? */
  212. if (num_fds == 0)
  213. break;
  214. /* You can't tell them all to go to sleep! */
  215. assert(num_waiting);
  216. if (timers) {
  217. struct timemono now, first;
  218. now = nowfn();
  219. /* Call functions for expired timers. */
  220. *expired = timers_expire(timers, now);
  221. if (*expired)
  222. break;
  223. /* Now figure out how long to wait for the next one. */
  224. if (timer_earliest(timers, &first)) {
  225. uint64_t next;
  226. next = time_to_msec(timemono_between(first, now));
  227. if (next < INT_MAX)
  228. ms_timeout = next;
  229. else
  230. ms_timeout = INT_MAX;
  231. }
  232. }
  233. r = pollfn(pollfds, num_fds, ms_timeout);
  234. if (r < 0)
  235. break;
  236. for (i = 0; i < num_fds && !io_loop_return; i++) {
  237. struct io_conn *c = (void *)fds[i];
  238. int events = pollfds[i].revents;
  239. if (r == 0)
  240. break;
  241. if (fds[i]->listener) {
  242. struct io_listener *l = (void *)fds[i];
  243. if (events & POLLIN) {
  244. accept_conn(l);
  245. r--;
  246. } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
  247. r--;
  248. errno = EBADF;
  249. io_close_listener(l);
  250. }
  251. } else if (events & (POLLIN|POLLOUT)) {
  252. r--;
  253. io_ready(c, events);
  254. } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
  255. r--;
  256. errno = EBADF;
  257. io_close(c);
  258. }
  259. }
  260. }
  261. ret = io_loop_return;
  262. io_loop_return = NULL;
  263. return ret;
  264. }