From eca55cee3c6dd4c01369876274b7e52d32d2d3de Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 20 Feb 2018 15:46:54 +1030 Subject: [PATCH] subd: handle stdin being closed (eg. --daemon). We need to do a more complex dance if stdin was important. Fixes: #1016 Signed-off-by: Rusty Russell --- lightningd/subd.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lightningd/subd.c b/lightningd/subd.c index c2c46177d..ab2a4ea31 100644 --- a/lightningd/subd.c +++ b/lightningd/subd.c @@ -157,7 +157,7 @@ static int subd(const char *dir, const char *name, goto close_execfail_fail; if (childpid == 0) { - int fdnum = 3, i; + int fdnum = 3, i, stdin_is_now = STDIN_FILENO; long max; size_t num_args; char *args[] = { NULL, NULL, NULL, NULL, NULL }; @@ -167,6 +167,8 @@ static int subd(const char *dir, const char *name, // msg = STDIN if (childmsg[1] != STDIN_FILENO) { + /* Do we need to move STDIN out the way? */ + stdin_is_now = dup(STDIN_FILENO); if (!move_fd(childmsg[1], STDIN_FILENO)) goto child_errno_fail; } @@ -181,9 +183,11 @@ static int subd(const char *dir, const char *name, /* Dup any extra fds up first. */ if (ap) { while ((fd = va_arg(*ap, int *)) != NULL) { - /* If this were stdin, dup2 closed! */ - assert(*fd != STDIN_FILENO); - if (!move_fd(*fd, fdnum)) + int actual_fd = *fd; + /* If this were stdin, we moved it above! */ + if (actual_fd == STDIN_FILENO) + actual_fd = stdin_is_now; + if (!move_fd(actual_fd, fdnum)) goto child_errno_fail; fdnum++; }