diff -upk.orig sysklogd-1.4.2.orig/klogd.c sysklogd-1.4.2/klogd.c --- sysklogd-1.4.2.orig/klogd.c 2005-08-18 11:36:23 +0000 +++ sysklogd-1.4.2/klogd.c 2005-08-18 12:29:52 +0000 @@ -278,6 +278,10 @@ _syscall3(int,ksyslog,int, type, char *, #define ksyslog klogctl #endif +#ifndef _PATH_DEVNULL +#define _PATH_DEVNULL "/dev/null" +#endif + #define LOG_BUFFER_SIZE 4096 #define LOG_LINE_LENGTH 1000 @@ -1072,19 +1076,35 @@ int main(argc, argv) { if (!check_pid(PidFile)) { - if ( fork() == 0 ) + pid_t pid; + int fl; + + if ( (fl = open(_PATH_DEVNULL, O_RDWR)) < 0 ) + { + fprintf(stderr, "klogd: %s: %s\n", + _PATH_DEVNULL, strerror(errno)); + exit(1); + } + + if ( (pid = fork()) == -1 ) + { + fputs("klogd: fork failed.\n", stderr); + exit(1); + } else if ( pid == 0 ) { - auto int fl; int num_fds = getdtablesize(); /* This is the child closing its file descriptors. */ - for (fl= 0; fl <= num_fds; ++fl) + if ( dup2(fl, 0) != 0 || + ((!use_output || strcmp(output, "-")) && + dup2(fl, 1) != 1) || + dup2(fl, 2) != 2) { - if ( fileno(stdout) == fl && use_output ) - if ( strcmp(output, "-") == 0 ) - continue; - close(fl); + fputs("klogd: dup2 failed.\n", stderr); + exit(1); } + for (fl= 3; fl <= num_fds; ++fl) + close(fl); setsid(); } diff -upk.orig sysklogd-1.4.2.orig/syslogd.c sysklogd-1.4.2/syslogd.c --- sysklogd-1.4.2.orig/syslogd.c 2005-08-18 12:02:53 +0000 +++ sysklogd-1.4.2/syslogd.c 2005-08-18 12:37:31 +0000 @@ -583,6 +583,10 @@ static char sccsid[] = "@(#)syslogd.c 5. #define _PATH_LOG "/dev/log" #endif +#ifndef _PATH_DEVNULL +#define _PATH_DEVNULL "/dev/null" +#endif + char *ConfFile = _PATH_LOGCONF; char *PidFile = _PATH_LOGPID; char ctty[] = _PATH_CONSOLE; @@ -925,8 +929,22 @@ int main(argc, argv) dprintf("Checking pidfile.\n"); if (!check_pid(PidFile)) { + pid_t pid; + + if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 0) + { + fprintf (stderr, "syslogd: %s: %s\n", + _PATH_DEVNULL, strerror(errno)); + exit(1); + } + signal (SIGTERM, doexit); - if (fork()) { + if ((pid = fork()) == -1) + { + fputs("syslogd: fork failed.\n", stderr); + exit(1); + } else if (pid) + { /* * Parent process */ @@ -942,7 +960,12 @@ int main(argc, argv) exit(1); } num_fds = getdtablesize(); - for (i= 0; i < num_fds; i++) + if (dup2(fd, 0) != 0 || dup2(fd, 1) != 1 || dup2(fd, 2) != 2) + { + fputs("syslogd: dup2 failed.\n", stderr); + exit(1); + } + for (i= 3; i < num_fds; i++) (void) close(i); untty(); }