From 35ec22b9465566aa9794feefcc28faa6ef298c2f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 4 Jul 2017 10:16:01 +0930 Subject: [PATCH] ccan: update to expose io_fd_block() from ccan/io. Signed-off-by: Rusty Russell --- ccan/README | 2 +- ccan/ccan/io/backend.h | 1 - ccan/ccan/io/benchmarks/run-different-speed.c | 2 +- ccan/ccan/io/benchmarks/run-length-prefix.c | 2 +- ccan/ccan/io/io.c | 15 +++-- ccan/ccan/io/io.h | 21 +++++++ ccan/ccan/io/poll.c | 10 +++- ccan/ccan/io/test/run-41-io_poll_override.c | 57 +++++++++++++++++++ 8 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 ccan/ccan/io/test/run-41-io_poll_override.c diff --git a/ccan/README b/ccan/README index 54b413cdf..ad8207e97 100644 --- a/ccan/README +++ b/ccan/README @@ -1,3 +1,3 @@ CCAN imported from http://ccodearchive.net. -CCAN version: init-2380-gd00c9d1b +CCAN version: init-2382-g3ffb94e9 diff --git a/ccan/ccan/io/backend.h b/ccan/ccan/io/backend.h index 3e158a366..c8ceb4e86 100644 --- a/ccan/ccan/io/backend.h +++ b/ccan/ccan/io/backend.h @@ -2,7 +2,6 @@ #ifndef CCAN_IO_BACKEND_H #define CCAN_IO_BACKEND_H #include -#include #include "io_plan.h" #include diff --git a/ccan/ccan/io/benchmarks/run-different-speed.c b/ccan/ccan/io/benchmarks/run-different-speed.c index 5ee15ea6d..c540f48d3 100644 --- a/ccan/ccan/io/benchmarks/run-different-speed.c +++ b/ccan/ccan/io/benchmarks/run-different-speed.c @@ -60,7 +60,7 @@ static void create_clients(struct sockaddr_un *addr, int waitfd) if (connect(sock[i], (void *)addr, sizeof(*addr)) != 0) err(1, "connecting socket"); /* Make nonblocking. */ - fcntl(sock[i], F_SETFD, fcntl(sock[i], F_GETFD)|O_NONBLOCK); + io_fd_block(sock[i], false); done[i] = 0; } diff --git a/ccan/ccan/io/benchmarks/run-length-prefix.c b/ccan/ccan/io/benchmarks/run-length-prefix.c index d88e3afaa..60de3bad0 100644 --- a/ccan/ccan/io/benchmarks/run-length-prefix.c +++ b/ccan/ccan/io/benchmarks/run-length-prefix.c @@ -66,7 +66,7 @@ static void create_clients(struct sockaddr_un *addr, int waitfd) if (connect(sock[i], (void *)addr, sizeof(*addr)) != 0) err(1, "connecting socket"); /* Make nonblocking. */ - fcntl(sock[i], F_SETFD, fcntl(sock[i], F_GETFD)|O_NONBLOCK); + io_fd_block(sock[i], false); done[i] = 0; } diff --git a/ccan/ccan/io/io.c b/ccan/ccan/io/io.c index 12b248902..cc2b1afa3 100644 --- a/ccan/ccan/io/io.c +++ b/ccan/ccan/io/io.c @@ -72,16 +72,19 @@ static bool next_plan(struct io_conn *conn, struct io_plan *plan) return true; } -static void set_blocking(int fd, bool block) +bool io_fd_block(int fd, bool block) { int flags = fcntl(fd, F_GETFL); + if (flags == -1) + return false; + if (block) flags &= ~O_NONBLOCK; else flags |= O_NONBLOCK; - fcntl(fd, F_SETFL, flags); + return fcntl(fd, F_SETFL, flags) != -1; } struct io_conn *io_new_conn_(const tal_t *ctx, int fd, @@ -103,7 +106,7 @@ struct io_conn *io_new_conn_(const tal_t *ctx, int fd, return tal_free(conn); /* Keep our I/O async. */ - set_blocking(fd, false); + io_fd_block(fd, false); /* We start with out doing nothing, and in doing our init. */ conn->plan[IO_OUT].status = IO_UNSET; @@ -436,7 +439,7 @@ struct io_plan *io_close_cb(struct io_conn *conn, void *next_arg) struct io_plan *io_close_taken_fd(struct io_conn *conn) { - set_blocking(conn->fd.fd, true); + io_fd_block(conn->fd.fd, true); cleanup_conn_without_close(conn); return io_close(conn); @@ -507,7 +510,7 @@ bool io_flush_sync(struct io_conn *conn) return true; /* Synchronous please. */ - set_blocking(io_conn_fd(conn), true); + io_fd_block(io_conn_fd(conn), true); again: switch (plan->io(conn->fd.fd, &plan->arg)) { @@ -527,6 +530,6 @@ again: abort(); } - set_blocking(io_conn_fd(conn), false); + io_fd_block(io_conn_fd(conn), false); return ok; } diff --git a/ccan/ccan/io/io.h b/ccan/ccan/io/io.h index fe42b5373..1e4e80e7d 100644 --- a/ccan/ccan/io/io.h +++ b/ccan/ccan/io/io.h @@ -4,6 +4,7 @@ #include #include #include +#include #include struct timers; @@ -691,6 +692,16 @@ int io_conn_fd(const struct io_conn *conn); */ bool io_flush_sync(struct io_conn *conn); +/** + * io_fd_block - helper to set an fd blocking/nonblocking. + * @fd: the file descriptor + * @block: true to set blocking, false to set non-blocking. + * + * Generally only fails is @fd isn't a valid file descriptor, otherwise + * returns true. + */ +bool io_fd_block(int fd, bool block); + /** * io_time_override - override the normal call for time. * @nowfn: the function to call. @@ -701,4 +712,14 @@ bool io_flush_sync(struct io_conn *conn); */ struct timemono (*io_time_override(struct timemono (*now)(void)))(void); +/** + * io_poll_override - override the normal call for poll. + * @pollfn: the function to call. + * + * io usually uses poll() internally, but this forces it to use your + * function (eg. for debugging, suppressing fds, or polling on others unknown + * to ccan/io). Returns the old one. + */ +int (*io_poll_override(int (*poll)(struct pollfd *fds, nfds_t nfds, int timeout)))(struct pollfd *, nfds_t, int); + #endif /* CCAN_IO_H */ diff --git a/ccan/ccan/io/poll.c b/ccan/ccan/io/poll.c index 043feff74..a4e83ed76 100644 --- a/ccan/ccan/io/poll.c +++ b/ccan/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) { @@ -269,7 +277,7 @@ void *io_loop(struct timers *timers, struct timer **expired) } } - r = poll(pollfds, num_fds, ms_timeout); + r = pollfn(pollfds, num_fds, ms_timeout); if (r < 0) break; diff --git a/ccan/ccan/io/test/run-41-io_poll_override.c b/ccan/ccan/io/test/run-41-io_poll_override.c new file mode 100644 index 000000000..0a62e2d3b --- /dev/null +++ b/ccan/ccan/io/test/run-41-io_poll_override.c @@ -0,0 +1,57 @@ +#include +/* Include the C files directly. */ +#include +#include +#include +#include +#include + +#define PORT "65020" + +/* Should be looking to read from one fd. */ +static int mypoll(struct pollfd *fds, nfds_t nfds, int timeout) +{ + ok1(nfds == 1); + ok1(fds[0].fd >= 0); + ok1(fds[0].events & POLLIN); + ok1(!(fds[0].events & POLLOUT)); + + /* Pretend it's readable. */ + fds[0].revents = POLLIN; + return 1; +} + +static int check_cant_read(int fd, struct io_plan_arg *arg) +{ + char c; + ssize_t ret = read(fd, &c, 1); + + ok1(errno == EAGAIN || errno == EWOULDBLOCK); + ok1(ret == -1); + + return 1; +} + +static struct io_plan *setup_conn(struct io_conn *conn, void *unused) +{ + /* Need to get this to mark it IO_POLLING */ + io_plan_arg(conn, IO_IN); + return io_set_plan(conn, IO_IN, check_cant_read, io_close_cb, NULL); +} + +int main(void) +{ + int fds[2]; + + plan_tests(8); + + pipe(fds); + ok1(io_poll_override(mypoll) == poll); + + io_new_conn(NULL, fds[0], setup_conn, NULL); + ok1(io_loop(NULL, NULL) == NULL); + close(fds[1]); + + /* This exits depending on whether all tests passed */ + return exit_status(); +}