Browse Source

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 <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
eca55cee3c
  1. 12
      lightningd/subd.c

12
lightningd/subd.c

@ -157,7 +157,7 @@ static int subd(const char *dir, const char *name,
goto close_execfail_fail; goto close_execfail_fail;
if (childpid == 0) { if (childpid == 0) {
int fdnum = 3, i; int fdnum = 3, i, stdin_is_now = STDIN_FILENO;
long max; long max;
size_t num_args; size_t num_args;
char *args[] = { NULL, NULL, NULL, NULL, NULL }; char *args[] = { NULL, NULL, NULL, NULL, NULL };
@ -167,6 +167,8 @@ static int subd(const char *dir, const char *name,
// msg = STDIN // msg = STDIN
if (childmsg[1] != STDIN_FILENO) { 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)) if (!move_fd(childmsg[1], STDIN_FILENO))
goto child_errno_fail; goto child_errno_fail;
} }
@ -181,9 +183,11 @@ static int subd(const char *dir, const char *name,
/* Dup any extra fds up first. */ /* Dup any extra fds up first. */
if (ap) { if (ap) {
while ((fd = va_arg(*ap, int *)) != NULL) { while ((fd = va_arg(*ap, int *)) != NULL) {
/* If this were stdin, dup2 closed! */ int actual_fd = *fd;
assert(*fd != STDIN_FILENO); /* If this were stdin, we moved it above! */
if (!move_fd(*fd, fdnum)) if (actual_fd == STDIN_FILENO)
actual_fd = stdin_is_now;
if (!move_fd(actual_fd, fdnum))
goto child_errno_fail; goto child_errno_fail;
fdnum++; fdnum++;
} }

Loading…
Cancel
Save