1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
8 #include <sys/socket.h>
11 #include <ccan/time/time.h>
12 #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 LIST_HEAD(closing);
18 static LIST_HEAD(always);
19 static struct timemono (*nowfn)(void) = time_mono;
20 static int (*pollfn)(struct pollfd *fds, nfds_t nfds, int timeout) = poll;
22 struct timemono (*io_time_override(struct timemono (*now)(void)))(void)
24 struct timemono (*old)(void) = nowfn;
29 int (*io_poll_override(int (*poll)(struct pollfd *fds, nfds_t nfds, int timeout)))(struct pollfd *, nfds_t, int)
31 int (*old)(struct pollfd *fds, nfds_t nfds, int timeout) = pollfn;
36 static bool add_fd(struct fd *fd, short events)
40 pollfds = tal_arr(NULL, struct pollfd, 8);
43 fds = tal_arr(pollfds, struct fd *, 8);
49 if (num_fds + 1 > max_fds) {
50 size_t num = max_fds * 2;
52 if (!tal_resize(&pollfds, num))
54 if (!tal_resize(&fds, num))
59 pollfds[num_fds].events = events;
60 /* In case it's idle. */
62 pollfds[num_fds].fd = -fd->fd;
64 pollfds[num_fds].fd = fd->fd;
65 pollfds[num_fds].revents = 0; /* In case we're iterating now */
67 fd->backend_info = num_fds;
75 static void del_fd(struct fd *fd)
77 size_t n = fd->backend_info;
81 if (pollfds[n].events)
83 if (n != num_fds - 1) {
84 /* Move last one over us. */
85 pollfds[n] = pollfds[num_fds-1];
86 fds[n] = fds[num_fds-1];
87 assert(fds[n]->backend_info == num_fds-1);
88 fds[n]->backend_info = n;
89 } else if (num_fds == 1) {
90 /* Free everything when no more fds. */
91 pollfds = tal_free(pollfds);
96 fd->backend_info = -1;
99 static void destroy_listener(struct io_listener *l)
105 bool add_listener(struct io_listener *l)
107 if (!add_fd(&l->fd, POLLIN))
109 tal_add_destructor(l, destroy_listener);
113 void remove_from_always(struct io_conn *conn)
115 list_del_init(&conn->always);
118 void backend_new_always(struct io_conn *conn)
120 /* In case it's already in always list. */
121 list_del(&conn->always);
122 list_add_tail(&always, &conn->always);
125 void backend_new_plan(struct io_conn *conn)
127 struct pollfd *pfd = &pollfds[conn->fd.backend_info];
133 if (conn->plan[IO_IN].status == IO_POLLING)
134 pfd->events |= POLLIN;
135 if (conn->plan[IO_OUT].status == IO_POLLING)
136 pfd->events |= POLLOUT;
140 pfd->fd = conn->fd.fd;
142 pfd->fd = -conn->fd.fd;
146 void backend_wake(const void *wait)
150 for (i = 0; i < num_fds; i++) {
153 /* Ignore listeners */
154 if (fds[i]->listener)
158 if (c->plan[IO_IN].status == IO_WAITING
159 && c->plan[IO_IN].arg.u1.const_vp == wait)
160 io_do_wakeup(c, IO_IN);
162 if (c->plan[IO_OUT].status == IO_WAITING
163 && c->plan[IO_OUT].arg.u1.const_vp == wait)
164 io_do_wakeup(c, IO_OUT);
168 static void destroy_conn(struct io_conn *conn, bool close_fd)
170 int saved_errno = errno;
175 /* In case it's on always list, remove it. */
176 list_del_init(&conn->always);
178 /* errno saved/restored by tal_free itself. */
181 conn->finish(conn, conn->finish_arg);
185 static void destroy_conn_close_fd(struct io_conn *conn)
187 destroy_conn(conn, true);
190 bool add_conn(struct io_conn *c)
192 if (!add_fd(&c->fd, 0))
194 tal_add_destructor(c, destroy_conn_close_fd);
198 void cleanup_conn_without_close(struct io_conn *conn)
200 tal_del_destructor(conn, destroy_conn_close_fd);
201 destroy_conn(conn, false);
204 static void accept_conn(struct io_listener *l)
206 int fd = accept(l->fd.fd, NULL, NULL);
208 /* FIXME: What to do here? */
212 io_new_conn(l->ctx, fd, l->init, l->arg);
215 static bool handle_always(void)
218 struct io_conn *conn;
220 while ((conn = list_pop(&always, struct io_conn, always)) != NULL) {
221 assert(conn->plan[IO_IN].status == IO_ALWAYS
222 || conn->plan[IO_OUT].status == IO_ALWAYS);
224 /* Re-initialize, for next time. */
225 list_node_init(&conn->always);
232 /* This is the main loop. */
233 void *io_loop(struct timers *timers, struct timer **expired)
237 /* if timers is NULL, expired must be. If not, not. */
238 assert(!timers == !expired);
240 /* Make sure this is NULL if we exit for some other reason. */
244 while (!io_loop_return) {
245 int i, r, ms_timeout = -1;
247 if (handle_always()) {
248 /* Could have started/finished more. */
252 /* Everything closed? */
256 /* You can't tell them all to go to sleep! */
260 struct timemono now, first;
264 /* Call functions for expired timers. */
265 *expired = timers_expire(timers, now);
269 /* Now figure out how long to wait for the next one. */
270 if (timer_earliest(timers, &first)) {
272 next = time_to_msec(timemono_between(first, now));
276 ms_timeout = INT_MAX;
280 r = pollfn(pollfds, num_fds, ms_timeout);
284 for (i = 0; i < num_fds && !io_loop_return; i++) {
285 struct io_conn *c = (void *)fds[i];
286 int events = pollfds[i].revents;
291 if (fds[i]->listener) {
292 struct io_listener *l = (void *)fds[i];
293 if (events & POLLIN) {
296 } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
299 io_close_listener(l);
301 } else if (events & (POLLIN|POLLOUT)) {
304 } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
312 ret = io_loop_return;
313 io_loop_return = NULL;