diff options
author | Alexander Bluhm <bluhm@cvs.openbsd.org> | 2018-09-14 18:17:47 +0000 |
---|---|---|
committer | Alexander Bluhm <bluhm@cvs.openbsd.org> | 2018-09-14 18:17:47 +0000 |
commit | 877658b9f1e65312d2ce472d866532275894d17f (patch) | |
tree | 2b4dc406021501f8fde60b419d3be823c22a4cae /usr.bin | |
parent | cc95cb9957fd3b32421cbfe2e99e1781d61bf575 (diff) |
Do not leak a file descriptor when opening nohup.out. Make sure
that stdout and stderr are not closed.
from Nan Xiao
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/nohup/nohup.c | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/usr.bin/nohup/nohup.c b/usr.bin/nohup/nohup.c index d411a0c3c2a..6f01691c2ab 100644 --- a/usr.bin/nohup/nohup.c +++ b/usr.bin/nohup/nohup.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nohup.c,v 1.17 2018/04/26 12:42:51 guenther Exp $ */ +/* $OpenBSD: nohup.c,v 1.18 2018/09/14 18:17:46 bluhm Exp $ */ /* $NetBSD: nohup.c,v 1.6 1995/08/31 23:35:25 jtc Exp $ */ /* @@ -78,13 +78,14 @@ main(int argc, char *argv[]) if (argc < 2) usage(); - if (isatty(STDOUT_FILENO)) + if (isatty(STDOUT_FILENO) || errno == EBADF) dofile(); if (pledge("stdio exec", NULL) == -1) err(1, "pledge"); - if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) { + if ((isatty(STDERR_FILENO) || errno == EBADF) && + dup2(STDOUT_FILENO, STDERR_FILENO) == -1) { /* may have just closed stderr */ (void)fprintf(stdin, "nohup: %s\n", strerror(errno)); exit(EXIT_MISC); @@ -125,6 +126,8 @@ dupit: (void)lseek(fd, (off_t)0, SEEK_END); if (dup2(fd, STDOUT_FILENO) == -1) err(EXIT_MISC, NULL); + if (fd > STDERR_FILENO) + (void)close(fd); (void)fprintf(stderr, "sending output to %s\n", p); } |