From a657724f442899ec1ce16c8cfe9e63e8e9d861ff Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 18 Apr 2024 13:25:44 +0930 Subject: [PATCH] ccan/io: extended errors API so caller can be told if we fail accept() Signed-off-by: Rusty Russell --- ccan/io/io.c | 13 ++++++++++++- ccan/io/io.h | 11 +++++++++++ ccan/io/poll.c | 8 +++++--- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/ccan/io/io.c b/ccan/io/io.c index 12df06d8..3d00de04 100644 --- a/ccan/io/io.c +++ b/ccan/io/io.c @@ -15,6 +15,7 @@ void *io_loop_return; struct io_plan io_conn_freed; +static bool io_extended_errors; struct io_listener *io_new_listener_(const tal_t *ctx, int fd, struct io_plan *(*init)(struct io_conn *, @@ -540,7 +541,7 @@ struct io_plan *io_sock_shutdown(struct io_conn *conn) /* And leave unset .*/ return &conn->plan[IO_IN]; } - + bool io_flush_sync(struct io_conn *conn) { struct io_plan *plan = &conn->plan[IO_OUT]; @@ -576,3 +577,13 @@ again: io_fd_block(io_conn_fd(conn), false); return ok; } + +void io_set_extended_errors(bool state) +{ + io_extended_errors = state; +} + +bool io_get_extended_errors(void) +{ + return io_extended_errors; +} diff --git a/ccan/io/io.h b/ccan/io/io.h index eeb5e36e..cc92be2c 100644 --- a/ccan/io/io.h +++ b/ccan/io/io.h @@ -113,6 +113,8 @@ void io_set_finish_(struct io_conn *conn, * (tal'ocated off @ctx) and pass that to init(). Note that if there is * an error on this file descriptor, it will be freed. * + * Note: if the accept fails (usually due to EMFILE), init() will be called + * wth * Returns NULL on error (and sets errno). * * Example: @@ -823,4 +825,13 @@ int (*io_poll_override(int (*poll)(struct pollfd *fds, nfds_t nfds, int timeout) */ const void *io_have_fd(int fd, bool *listener); +/** + * io_set_extended_errors - enable callbacks for errors. + * @state: true or false. + * + * Defaults false for compatibility. See io_new_conn for what this changes. + */ +void io_set_extended_errors(bool state); +bool io_get_extended_errors(void); + #endif /* CCAN_IO_H */ diff --git a/ccan/io/poll.c b/ccan/io/poll.c index 634f83d2..7fe9e2c5 100644 --- a/ccan/io/poll.c +++ b/ccan/io/poll.c @@ -270,10 +270,12 @@ static void accept_conn(struct io_listener *l) { int fd = accept(l->fd.fd, NULL, NULL); - /* FIXME: What to do here? */ - if (fd < 0) + if (fd < 0) { + /* If they've enabled it, this is how we tell them */ + if (io_get_extended_errors()) + l->init(NULL, l->arg); return; - + } io_new_conn(l->ctx, fd, l->init, l->arg); } -- 2.39.5