From b45b2daef9db8bb5be7881928f2abfc832573027 Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Sun, 23 Feb 2014 21:51:19 +0100 Subject: [PATCH] startup: Check return value of open /dev/null. Don't fclose stdin/stdout/stderr. Open /dev/null for standard fds earlier, so a failure can be reported. Do not fclose stdin/stdout/stderr but just overwrite the fds with /dev/null. --- src/ircd.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/ircd.c b/src/ircd.c index d168ea8c..f68ac9b2 100644 --- a/src/ircd.c +++ b/src/ircd.c @@ -155,6 +155,19 @@ ircd_shutdown(const char *reason) static void print_startup(int pid) { + int fd; + + close(1); + fd = open("/dev/null", O_RDWR); + if (fd == -1) { + perror("open /dev/null"); + exit(EXIT_FAILURE); + } + if (fd == 0) + fd = dup(fd); + if (fd != 1) + abort(); + inotice("now running in %s mode from %s as pid %d ...", !server_state_foreground ? "background" : "foreground", ConfigFileEntry.dpath, pid); @@ -163,12 +176,10 @@ print_startup(int pid) * -- jilles */ if (!server_state_foreground) write(0, ".", 1); - fclose(stdin); - fclose(stdout); - fclose(stderr); - open("/dev/null", O_RDWR); - dup2(0, 1); - dup2(0, 2); + if (dup2(1, 0) == -1) + abort(); + if (dup2(1, 2) == -1) + abort(); } /*