diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2001-10-24 17:28:17 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2001-10-24 17:28:17 +0000 |
commit | b63d94eeb32cdb0f74aee4d49dd45475cb621be8 (patch) | |
tree | 11d5d97b952a624af67c4317d58d6f556f430da9 /usr.sbin | |
parent | 3304ae07e128acca74b601e043df6f543a59509f (diff) |
When becoming a daemon, dup stdin, stdout, and stderr to /dev/null
Change an unsafe vfork() to fork()
Fix dup2() usage--must check for oldd == newd case and no need to close oldd
Fixes annoying messages from sendmail about stdout being closed.
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/cron/cron.c | 12 | ||||
-rw-r--r-- | usr.sbin/cron/crontab.c | 8 | ||||
-rw-r--r-- | usr.sbin/cron/do_command.c | 25 | ||||
-rw-r--r-- | usr.sbin/cron/pathnames.h | 6 | ||||
-rw-r--r-- | usr.sbin/cron/popen.c | 14 |
5 files changed, 40 insertions, 25 deletions
diff --git a/usr.sbin/cron/cron.c b/usr.sbin/cron/cron.c index a2ce0eba63a..5cc8a5458f5 100644 --- a/usr.sbin/cron/cron.c +++ b/usr.sbin/cron/cron.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cron.c,v 1.15 2001/08/11 20:47:14 millert Exp $ */ +/* $OpenBSD: cron.c,v 1.16 2001/10/24 17:28:16 millert Exp $ */ /* Copyright 1988,1990,1993,1994 by Paul Vixie * All rights reserved */ @@ -21,7 +21,7 @@ */ #if !defined(lint) && !defined(LINT) -static char rcsid[] = "$OpenBSD: cron.c,v 1.15 2001/08/11 20:47:14 millert Exp $"; +static char rcsid[] = "$OpenBSD: cron.c,v 1.16 2001/10/24 17:28:16 millert Exp $"; #endif #define MAIN_PROGRAM @@ -57,6 +57,7 @@ int main(int argc, char *argv[]) { cron_db database; struct sigaction sact; + int fd; ProgramName = argv[0]; @@ -105,6 +106,13 @@ main(int argc, char *argv[]) { /* child process */ log_it("CRON",getpid(),"STARTUP","fork ok"); (void) setsid(); + if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { + (void) dup2(fd, STDIN); + (void) dup2(fd, STDOUT); + (void) dup2(fd, STDERR); + if (fd > STDERR) + (void) close(fd); + } break; default: /* parent process should just die */ diff --git a/usr.sbin/cron/crontab.c b/usr.sbin/cron/crontab.c index fee68e0670a..e3cc15b630a 100644 --- a/usr.sbin/cron/crontab.c +++ b/usr.sbin/cron/crontab.c @@ -1,4 +1,4 @@ -/* $OpenBSD: crontab.c,v 1.22 2001/08/19 18:30:38 millert Exp $ */ +/* $OpenBSD: crontab.c,v 1.23 2001/10/24 17:28:16 millert Exp $ */ /* Copyright 1988,1990,1993,1994 by Paul Vixie * All rights reserved */ @@ -21,7 +21,7 @@ */ #if !defined(lint) && !defined(LINT) -static char rcsid[] = "$OpenBSD: crontab.c,v 1.22 2001/08/19 18:30:38 millert Exp $"; +static char rcsid[] = "$OpenBSD: crontab.c,v 1.23 2001/10/24 17:28:16 millert Exp $"; #endif /* crontab - install and manage per-user crontab files @@ -303,8 +303,8 @@ edit_cmd(void) { } fprintf(stderr, "no crontab for %s - using an empty one\n", User); - if (!(f = fopen("/dev/null", "r"))) { - perror("/dev/null"); + if (!(f = fopen(_PATH_DEVNULL, "r"))) { + perror(_PATH_DEVNULL); exit(ERROR_EXIT); } } diff --git a/usr.sbin/cron/do_command.c b/usr.sbin/cron/do_command.c index 120aaacd905..c3c70bcf4a4 100644 --- a/usr.sbin/cron/do_command.c +++ b/usr.sbin/cron/do_command.c @@ -1,4 +1,4 @@ -/* $OpenBSD: do_command.c,v 1.10 2001/02/18 19:48:33 millert Exp $ */ +/* $OpenBSD: do_command.c,v 1.11 2001/10/24 17:28:16 millert Exp $ */ /* Copyright 1988,1990,1993,1994 by Paul Vixie * All rights reserved */ @@ -21,7 +21,7 @@ */ #if !defined(lint) && !defined(LINT) -static char rcsid[] = "$OpenBSD: do_command.c,v 1.10 2001/02/18 19:48:33 millert Exp $"; +static char rcsid[] = "$OpenBSD: do_command.c,v 1.11 2001/10/24 17:28:16 millert Exp $"; #endif #include "cron.h" @@ -139,7 +139,7 @@ child_process(entry *e, user *u) { /* fork again, this time so we can exec the user's command. */ - switch (vfork()) { + switch (fork()) { case -1: log_it("CRON", getpid(), "error", "can't vfork"); exit(ERROR_EXIT); @@ -182,14 +182,15 @@ child_process(entry *e, user *u) { /* grandchild process. make std{in,out} be the ends of * pipes opened by our daddy; make stderr go to stdout. */ - close(STDIN); dup2(stdin_pipe[READ_PIPE], STDIN); - close(STDOUT); dup2(stdout_pipe[WRITE_PIPE], STDOUT); - close(STDERR); dup2(STDOUT, STDERR); - - /* close the pipes we just dup'ed. The resources will remain. - */ - close(stdin_pipe[READ_PIPE]); - close(stdout_pipe[WRITE_PIPE]); + if (stdin_pipe[READ_PIPE] != STDIN) { + dup2(stdin_pipe[READ_PIPE], STDIN); + close(stdin_pipe[READ_PIPE]); + } + if (stdout_pipe[WRITE_PIPE] != STDOUT) { + close(STDOUT); + dup2(stdout_pipe[WRITE_PIPE], STDOUT); + } + dup2(STDOUT, STDERR); /* set our directory, uid and gid. Set gid first, since once * we set uid, we've lost root privledges. @@ -483,6 +484,8 @@ child_process(entry *e, user *u) { (long)getpid(), children)) pid = wait(&waiter); if (pid < OK) { + if (errno == EINTR) + continue; Debug(DPROC, ("[%ld] no more grandchildren--mail written?\n", (long)getpid())) diff --git a/usr.sbin/cron/pathnames.h b/usr.sbin/cron/pathnames.h index e01e0321843..ed727d6cf5d 100644 --- a/usr.sbin/cron/pathnames.h +++ b/usr.sbin/cron/pathnames.h @@ -1,4 +1,4 @@ -/* $OpenBSD: pathnames.h,v 1.3 2001/02/18 19:48:36 millert Exp $ */ +/* $OpenBSD: pathnames.h,v 1.4 2001/10/24 17:28:16 millert Exp $ */ /* Copyright 1993,1994 by Paul Vixie * All rights reserved @@ -82,3 +82,7 @@ #ifndef _PATH_TMP # define _PATH_TMP "/tmp" #endif + +#ifndef _PATH_DEVNULL +# define _PATH_DEVNULL "/dev/null" +#endif diff --git a/usr.sbin/cron/popen.c b/usr.sbin/cron/popen.c index bac4b39c765..2e922a11067 100644 --- a/usr.sbin/cron/popen.c +++ b/usr.sbin/cron/popen.c @@ -1,4 +1,4 @@ -/* $OpenBSD: popen.c,v 1.11 2001/10/01 19:19:09 millert Exp $ */ +/* $OpenBSD: popen.c,v 1.12 2001/10/24 17:28:16 millert Exp $ */ /* * Copyright (c) 1988, 1993, 1994 @@ -38,7 +38,7 @@ #if 0 static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 4/6/94"; #else -static char rcsid[] = "$OpenBSD: popen.c,v 1.11 2001/10/01 19:19:09 millert Exp $"; +static char rcsid[] = "$OpenBSD: popen.c,v 1.12 2001/10/24 17:28:16 millert Exp $"; #endif #endif /* not lint */ @@ -153,15 +153,15 @@ cron_popen(program, type, e) } closelog(); if (*type == 'r') { - if (pdes[1] != STDOUT_FILENO) { - dup2(pdes[1], STDOUT_FILENO); + if (pdes[1] != STDOUT) { + dup2(pdes[1], STDOUT); (void)close(pdes[1]); } - dup2(STDOUT_FILENO, STDERR_FILENO); /* stderr too! */ + dup2(STDOUT, STDERR); /* stderr too! */ (void)close(pdes[0]); } else { - if (pdes[0] != STDIN_FILENO) { - dup2(pdes[0], STDIN_FILENO); + if (pdes[0] != STDIN) { + dup2(pdes[0], STDIN); (void)close(pdes[0]); } (void)close(pdes[1]); |