X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fio%2Fpoll.c;h=b005a97e4b1d07879a4e281a8d48276baaac2fa1;hp=6738f720ec0782f7ade4d67f1f4eba30e8f4b3ef;hb=e2d15a2bcb78dba323c4a807543fcc0cb1b03ef0;hpb=31c816a6a9a2037d8860d56814835d9ac488d52f diff --git a/ccan/io/poll.c b/ccan/io/poll.c index 6738f720..b005a97e 100644 --- a/ccan/io/poll.c +++ b/ccan/io/poll.c @@ -17,6 +17,7 @@ static struct fd **fds = NULL; static LIST_HEAD(closing); static LIST_HEAD(always); static struct timemono (*nowfn)(void) = time_mono; +static int (*pollfn)(struct pollfd *fds, nfds_t nfds, int timeout) = poll; struct timemono (*io_time_override(struct timemono (*now)(void)))(void) { @@ -25,6 +26,13 @@ struct timemono (*io_time_override(struct timemono (*now)(void)))(void) return old; } +int (*io_poll_override(int (*poll)(struct pollfd *fds, nfds_t nfds, int timeout)))(struct pollfd *, nfds_t, int) +{ + int (*old)(struct pollfd *fds, nfds_t nfds, int timeout) = pollfn; + pollfn = poll; + return old; +} + static bool add_fd(struct fd *fd, short events) { if (!max_fds) { @@ -86,13 +94,6 @@ static void del_fd(struct fd *fd) } num_fds--; fd->backend_info = -1; - - /* Closing a local socket doesn't wake poll() because other end - * has them open. See 2.6. When should I use shutdown()? - * in http://www.faqs.org/faqs/unix-faq/socket/ */ - shutdown(fd->fd, SHUT_RDWR); - - close(fd->fd); } static void destroy_listener(struct io_listener *l) @@ -129,9 +130,11 @@ void backend_new_plan(struct io_conn *conn) num_waiting--; pfd->events = 0; - if (conn->plan[IO_IN].status == IO_POLLING) + if (conn->plan[IO_IN].status == IO_POLLING_NOTSTARTED + || conn->plan[IO_IN].status == IO_POLLING_STARTED) pfd->events |= POLLIN; - if (conn->plan[IO_OUT].status == IO_POLLING) + if (conn->plan[IO_OUT].status == IO_POLLING_NOTSTARTED + || conn->plan[IO_OUT].status == IO_POLLING_STARTED) pfd->events |= POLLOUT; if (pfd->events) { @@ -164,11 +167,12 @@ void backend_wake(const void *wait) } } -static void destroy_conn(struct io_conn *conn) +static void destroy_conn(struct io_conn *conn, bool close_fd) { int saved_errno = errno; - close(conn->fd.fd); + if (close_fd) + close(conn->fd.fd); del_fd(&conn->fd); /* In case it's on always list, remove it. */ list_del_init(&conn->always); @@ -180,14 +184,25 @@ static void destroy_conn(struct io_conn *conn) } } +static void destroy_conn_close_fd(struct io_conn *conn) +{ + destroy_conn(conn, true); +} + bool add_conn(struct io_conn *c) { if (!add_fd(&c->fd, 0)) return false; - tal_add_destructor(c, destroy_conn); + tal_add_destructor(c, destroy_conn_close_fd); return true; } +void cleanup_conn_without_close(struct io_conn *conn) +{ + tal_del_destructor(conn, destroy_conn_close_fd); + destroy_conn(conn, false); +} + static void accept_conn(struct io_listener *l) { int fd = accept(l->fd.fd, NULL, NULL); @@ -264,9 +279,14 @@ void *io_loop(struct timers *timers, struct timer **expired) } } - r = poll(pollfds, num_fds, ms_timeout); - if (r < 0) + r = pollfn(pollfds, num_fds, ms_timeout); + if (r < 0) { + /* Signals shouldn't break us, unless they set + * io_loop_return. */ + if (errno == EINTR) + continue; break; + } for (i = 0; i < num_fds && !io_loop_return; i++) { struct io_conn *c = (void *)fds[i]; @@ -276,9 +296,14 @@ void *io_loop(struct timers *timers, struct timer **expired) break; if (fds[i]->listener) { + struct io_listener *l = (void *)fds[i]; if (events & POLLIN) { - accept_conn((void *)c); + accept_conn(l); + r--; + } else if (events & (POLLHUP|POLLNVAL|POLLERR)) { r--; + errno = EBADF; + io_close_listener(l); } } else if (events & (POLLIN|POLLOUT)) { r--;