From 3aa68cf40f5ee0ce23c083809cb1b3f4d3ca5394 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 30 Jun 2022 15:12:35 +0930 Subject: [PATCH] ccan/io: add debugging helper for seeing if module is in charge of an fd. Signed-off-by: Rusty Russell --- ccan/io/io.h | 9 +++++++++ ccan/io/poll.c | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/ccan/io/io.h b/ccan/io/io.h index 4e3b4e93..eeb5e36e 100644 --- a/ccan/io/io.h +++ b/ccan/io/io.h @@ -814,4 +814,13 @@ struct timemono (*io_time_override(struct timemono (*now)(void)))(void); */ int (*io_poll_override(int (*poll)(struct pollfd *fds, nfds_t nfds, int timeout)))(struct pollfd *, nfds_t, int); +/** + * io_have_fd - do we own this file descriptor? + * @fd: the file descriptor. + * @listener: if non-NULL, set to true if it's a listening socket (io_listener). + * + * Returns NULL if we don't own it, otherwise a struct io_conn * or struct io_listener *. + */ +const void *io_have_fd(int fd, bool *listener); + #endif /* CCAN_IO_H */ diff --git a/ccan/io/poll.c b/ccan/io/poll.c index 4cc9f4b7..634f83d2 100644 --- a/ccan/io/poll.c +++ b/ccan/io/poll.c @@ -464,3 +464,15 @@ void *io_loop(struct timers *timers, struct timer **expired) return ret; } + +const void *io_have_fd(int fd, bool *listener) +{ + for (size_t i = 0; i < num_fds; i++) { + if (fds[i]->fd != fd) + continue; + if (listener) + *listener = fds[i]->listener; + return fds[i]; + } + return NULL; +} -- 2.39.2