poll.c 6.0 KB

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