From: bluhm Date: Fri, 14 Sep 2018 18:17:46 +0000 (+0000) Subject: Do not leak a file descriptor when opening nohup.out. Make sure X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=3b521a6ec66f2a4de4e5b2e6eefba3e9b7a9da9d;p=openbsd Do not leak a file descriptor when opening nohup.out. Make sure that stdout and stderr are not closed. from Nan Xiao --- 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); }