Skip to content

Commit

Permalink
daemon: if one of the standard fds is missing open it to /dev/null
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Lederhofer <matled@gmx.net>
Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information
Matthias Lederhofer authored and Junio C Hamano committed Jul 14, 2006
1 parent ad8b4f5 commit 258e93a
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,18 @@ static int service_loop(int socknum, int *socklist)
}
}

/* if any standard file descriptor is missing open it to /dev/null */
static void sanitize_stdfds(void)
{
int fd = open("/dev/null", O_RDWR, 0);
while (fd != -1 && fd < 2)
fd = dup(fd);
if (fd == -1)
die("open /dev/null or dup failed: %s", strerror(errno));
if (fd > 2)
close(fd);
}

static int serve(int port)
{
int socknum, *socklist;
Expand Down Expand Up @@ -773,5 +785,7 @@ int main(int argc, char **argv)
return execute(peer);
}

sanitize_stdfds();

return serve(port);
}

0 comments on commit 258e93a

Please sign in to comment.