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);
20 static bool add_fd(struct fd *fd, short events)
24 pollfds = tal_arr(NULL, struct pollfd, 8);
27 fds = tal_arr(pollfds, struct fd *, 8);
33 if (num_fds + 1 > max_fds) {
34 size_t num = max_fds * 2;
36 if (!tal_resize(&pollfds, num))
38 if (!tal_resize(&fds, num))
43 pollfds[num_fds].events = events;
44 /* In case it's idle. */
46 pollfds[num_fds].fd = -fd->fd;
48 pollfds[num_fds].fd = fd->fd;
49 pollfds[num_fds].revents = 0; /* In case we're iterating now */
51 fd->backend_info = num_fds;
59 static void del_fd(struct fd *fd)
61 size_t n = fd->backend_info;
65 if (pollfds[n].events)
67 if (n != num_fds - 1) {
68 /* Move last one over us. */
69 pollfds[n] = pollfds[num_fds-1];
70 fds[n] = fds[num_fds-1];
71 assert(fds[n]->backend_info == num_fds-1);
72 fds[n]->backend_info = n;
73 } else if (num_fds == 1) {
74 /* Free everything when no more fds. */
75 pollfds = tal_free(pollfds);
80 fd->backend_info = -1;
82 /* Closing a local socket doesn't wake poll() because other end
83 * has them open. See 2.6. When should I use shutdown()?
84 * in http://www.faqs.org/faqs/unix-faq/socket/ */
85 shutdown(fd->fd, SHUT_RDWR);
90 bool add_listener(struct io_listener *l)
92 if (!add_fd(&l->fd, POLLIN))
97 void remove_from_always(struct io_conn *conn)
99 list_del_init(&conn->always);
102 void backend_new_closing(struct io_conn *conn)
104 /* In case it's on always list, remove it. */
105 list_del_init(&conn->always);
106 list_add_tail(&closing, &conn->closing);
109 void backend_new_always(struct io_conn *conn)
111 /* In case it's already in always list. */
112 list_del(&conn->always);
113 list_add_tail(&always, &conn->always);
116 void backend_new_plan(struct io_conn *conn)
118 struct pollfd *pfd = &pollfds[conn->fd.backend_info];
124 if (conn->plan[IO_IN].status == IO_POLLING)
125 pfd->events |= POLLIN;
126 if (conn->plan[IO_OUT].status == IO_POLLING)
127 pfd->events |= POLLOUT;
131 pfd->fd = conn->fd.fd;
133 pfd->fd = -conn->fd.fd;
137 void backend_wake(const void *wait)
141 for (i = 0; i < num_fds; i++) {
144 /* Ignore listeners */
145 if (fds[i]->listener)
149 if (c->plan[IO_IN].status == IO_WAITING
150 && c->plan[IO_IN].arg.u1.const_vp == wait)
151 io_do_wakeup(c, IO_IN);
153 if (c->plan[IO_OUT].status == IO_WAITING
154 && c->plan[IO_OUT].arg.u1.const_vp == wait)
155 io_do_wakeup(c, IO_OUT);
159 bool add_conn(struct io_conn *c)
161 return add_fd(&c->fd, 0);
164 static void del_conn(struct io_conn *conn)
168 /* Saved by io_close */
169 errno = conn->plan[IO_IN].arg.u1.s;
170 conn->finish(conn, conn->finish_arg);
175 void del_listener(struct io_listener *l)
180 static void accept_conn(struct io_listener *l)
182 int fd = accept(l->fd.fd, NULL, NULL);
184 /* FIXME: What to do here? */
188 io_new_conn(l->ctx, fd, l->init, l->arg);
191 /* It's OK to miss some, as long as we make progress. */
192 static bool close_conns(void)
195 struct io_conn *conn;
197 while ((conn = list_pop(&closing, struct io_conn, closing)) != NULL) {
198 assert(conn->plan[IO_IN].status == IO_CLOSING);
199 assert(conn->plan[IO_OUT].status == IO_CLOSING);
207 static bool handle_always(void)
210 struct io_conn *conn;
212 while ((conn = list_pop(&always, struct io_conn, always)) != NULL) {
213 assert(conn->plan[IO_IN].status == IO_ALWAYS
214 || conn->plan[IO_OUT].status == IO_ALWAYS);
216 /* Re-initialize, for next time. */
217 list_node_init(&conn->always);
224 /* This is the main loop. */
225 void *io_loop(struct timers *timers, struct timer **expired)
229 /* if timers is NULL, expired must be. If not, not. */
230 assert(!timers == !expired);
232 /* Make sure this is NULL if we exit for some other reason. */
236 while (!io_loop_return) {
237 int i, r, ms_timeout = -1;
240 /* Could have started/finished more. */
244 if (handle_always()) {
245 /* Could have started/finished more. */
249 /* Everything closed? */
253 /* You can't tell them all to go to sleep! */
257 struct timeabs now, first;
261 /* Call functions for expired timers. */
262 *expired = timers_expire(timers, now);
266 /* Now figure out how long to wait for the next one. */
267 if (timer_earliest(timers, &first)) {
269 next = time_to_msec(time_between(first, now));
273 ms_timeout = INT_MAX;
277 r = poll(pollfds, num_fds, ms_timeout);
281 for (i = 0; i < num_fds && !io_loop_return; i++) {
282 struct io_conn *c = (void *)fds[i];
283 int events = pollfds[i].revents;
288 if (fds[i]->listener) {
289 if (events & POLLIN) {
290 accept_conn((void *)c);
293 } else if (events & (POLLIN|POLLOUT)) {
296 } else if (events & (POLLHUP|POLLNVAL|POLLERR)) {
306 ret = io_loop_return;
307 io_loop_return = NULL;