diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -166,6 +166,7 @@ static int sigcatch[] = { SIGHUP, SIGINT, SIGQUIT, SIGALRM, SIGTERM, SIGCHLD }; static struct filed consfile; /* Console */ +static int nulldesc; /* /dev/null descriptor */ static bool Debug; /* debug flag */ static bool Foreground = false; /* Run in foreground, instead of daemonizing */ @@ -667,8 +668,6 @@ } else if (Debug) setlinebuf(stdout); - (void)strlcpy(bootfile, getbootfile(), sizeof(bootfile)); - kq = kqueue(); if (kq == -1) { pidfile_remove(pfh); @@ -688,13 +687,6 @@ err(1, "failed to apply signal mask"); } - (void)alarm(TIMERINTVL); - - /* tuck my process id away */ - pidfile_write(pfh); - - dprintf("off & running....\n"); - STAILQ_FOREACH(sl, &shead, next) { EV_SET(&ev, sl->sl_socket, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, sl); @@ -715,6 +707,18 @@ (void)strlcpy(consfile.fu_fname, _PATH_CONSOLE + sizeof(_PATH_DEV) - 1, sizeof(consfile.fu_fname)); + nulldesc = open(_PATH_DEVNULL, O_RDWR); + if (nulldesc < 0) + dprintf("can't open %s (%d)\n", _PATH_DEVNULL, errno); + + (void)strlcpy(bootfile, getbootfile(), sizeof(bootfile)); + (void)alarm(TIMERINTVL); + + /* tuck my process id away */ + pidfile_write(pfh); + + dprintf("off & running....\n"); + init(false); for (;;) { switch (kevent(kq, NULL, 0, &ev, 1, needdofsync ? &ts : tsp)) { @@ -758,7 +762,6 @@ reapchild(ev.ident); continue; } - } } @@ -2225,6 +2228,8 @@ close(sl->sl_dirfd); } } + if (nulldesc >= 0) + close(nulldesc); pidfile_remove(pfh); exit(1); @@ -3126,7 +3131,6 @@ static int waitdaemon(int maxwait) { - int fd; int status; pid_t pid, childpid; @@ -3157,12 +3161,10 @@ return (-1); (void)chdir("/"); - if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { - (void)dup2(fd, STDIN_FILENO); - (void)dup2(fd, STDOUT_FILENO); - (void)dup2(fd, STDERR_FILENO); - if (fd > STDERR_FILENO) - (void)close(fd); + if (nulldesc >= 0) { + (void)dup2(nulldesc, STDIN_FILENO); + (void)dup2(nulldesc, STDOUT_FILENO); + (void)dup2(nulldesc, STDERR_FILENO); } return (getppid()); } @@ -3455,7 +3457,7 @@ static int p_open(const char *prog, int *rpd) { - int nulldesc, pfd[2], pd; + int pfd[2], pd; pid_t pid; sigset_t sigset = { }; char *argv[4]; /* sh -c cmd NULL */ @@ -3463,13 +3465,9 @@ if (pipe(pfd) == -1) return (-1); - if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1) - /* we are royally screwed anyway */ - return (-1); switch ((pid = pdfork(&pd, PD_CLOEXEC))) { case -1: - close(nulldesc); return (-1); case 0: @@ -3493,14 +3491,15 @@ } dup2(pfd[0], STDIN_FILENO); - dup2(nulldesc, STDOUT_FILENO); - dup2(nulldesc, STDERR_FILENO); + if (nulldesc >= 0) { + dup2(nulldesc, STDOUT_FILENO); + dup2(nulldesc, STDERR_FILENO); + } closefrom(STDERR_FILENO + 1); (void)execvp(_PATH_BSHELL, argv); _exit(255); } - close(nulldesc); close(pfd[0]); /* * Avoid blocking on a hung pipe. With O_NONBLOCK, we are