From 60e31637d7545a22844a3c0f46833031bce4295f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 14 Mar 2017 13:17:15 +1030 Subject: [PATCH] io/fdpass: add flag to close fd after sending. Signed-off-by: Rusty Russell --- ccan/io/fdpass/fdpass.c | 4 ++++ ccan/io/fdpass/fdpass.h | 11 ++++++----- ccan/io/fdpass/test/run.c | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ccan/io/fdpass/fdpass.c b/ccan/io/fdpass/fdpass.c index 11208a9b..63c46223 100644 --- a/ccan/io/fdpass/fdpass.c +++ b/ccan/io/fdpass/fdpass.c @@ -12,17 +12,21 @@ static int do_fd_send(int fd, struct io_plan_arg *arg) return 0; return -1; } + if (arg->u2.s) + close(arg->u1.s); return 1; } struct io_plan *io_send_fd_(struct io_conn *conn, int fd, + bool fdclose, struct io_plan *(*next)(struct io_conn *, void *), void *next_arg) { struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT); arg->u1.s = fd; + arg->u2.s = fdclose; return io_set_plan(conn, IO_OUT, do_fd_send, next, next_arg); } diff --git a/ccan/io/fdpass/fdpass.h b/ccan/io/fdpass/fdpass.h index 366ff352..533bc840 100644 --- a/ccan/io/fdpass/fdpass.h +++ b/ccan/io/fdpass/fdpass.h @@ -7,6 +7,7 @@ * io_send_fd - output plan to send a file descriptor * @conn: the connection that plan is for. * @fd: the file descriptor to pass. + * @fdclose: true to close fd after successful sending. * @next: function to call output is done. * @arg: @next argument * @@ -22,17 +23,17 @@ * Example: * static struct io_plan *fd_to_conn(struct io_conn *conn, int fd) * { - * // Write fd, then close. - * return io_send_fd(conn, fd, io_close_cb, NULL); + * // Write fd, then close conn. + * return io_send_fd(conn, fd, false, io_close_cb, NULL); * } */ -#define io_send_fd(conn, fd, next, arg) \ - io_send_fd_((conn), (fd), \ +#define io_send_fd(conn, fd, fdclose, next, arg) \ + io_send_fd_((conn), (fd), (fdclose), \ typesafe_cb_preargs(struct io_plan *, void *, \ (next), (arg), struct io_conn *), \ (arg)) struct io_plan *io_send_fd_(struct io_conn *conn, - int fd, + int fd, bool fdclose, struct io_plan *(*next)(struct io_conn *, void *), void *arg); diff --git a/ccan/io/fdpass/test/run.c b/ccan/io/fdpass/test/run.c index d2021e82..642db084 100644 --- a/ccan/io/fdpass/test/run.c +++ b/ccan/io/fdpass/test/run.c @@ -28,7 +28,7 @@ static struct io_plan *try_writing(struct io_conn *conn, int *pfd) static struct io_plan *send_fd(struct io_conn *conn, int *pfd) { - return io_send_fd(conn, pfd[0], try_writing, pfd); + return io_send_fd(conn, pfd[0], true, try_writing, pfd); } int main(void) -- 2.39.5