Index | Thread | Search

From:
mcq <mcq@disroot.org>
Subject:
nohup: fix error messages when stderr unavailable
To:
tech@openbsd.org
Date:
Fri, 06 Feb 2026 17:12:19 -0800

Download raw body.

Thread
Hello bugs@, 

i've found bugs in the binary nohup. 

when stderr is closed or in a tty when nohup fails the error message is lost. 

two bugs fixed: 

1. fprintf writes to stdin instead of stdout when dup2 fails 

2. errx writes to stderr (which is closed) when nohup fails 

Both now write to stdout when stderr is unavailble 

tested with stderr closed (in a terminal emulator,) and a TTY, also in a read only directory. 

diff: 

--- nohup.c.orig        Fri Feb  6 18:15:16 2026
+++ nohup.c     Fri Feb  6 18:38:03 2026
@@ -87,7 +87,7 @@
        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));
+               (void)fprintf(stdout, "nohup: %s\n", strerror(errno));
                exit(EXIT_MISC);
        }

@@ -120,6 +120,12 @@
                if ((fd = open(p = path, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0)
                        goto dupit;
        }
+
+       if (isatty(STDERR_FILENO) || errno == EBADF) {
+               (void)fprintf(stdout, "nohup: can't open a nohup.out file\n");
+               exit(EXIT_MISC);
+       }
+
        errx(EXIT_MISC, "can't open a nohup.out file");

 dupit: 

i also attached it.
--- nohup.c.orig	Fri Feb  6 18:15:16 2026
+++ nohup.c	Fri Feb  6 18:38:03 2026
@@ -87,7 +87,7 @@
 	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));
+		(void)fprintf(stdout, "nohup: %s\n", strerror(errno)); 
 		exit(EXIT_MISC);
 	}
 
@@ -120,6 +120,12 @@
 		if ((fd = open(p = path, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0)
 			goto dupit;
 	}
+
+	if (isatty(STDERR_FILENO) || errno == EBADF) { 
+		(void)fprintf(stdout, "nohup: can't open a nohup.out file\n");
+		exit(EXIT_MISC);
+	}
+
 	errx(EXIT_MISC, "can't open a nohup.out file");
 
 dupit: