-/* $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 $ */
/*
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);
(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);
}