From 8597e1fd2d4014aa8fe7c1848a48cba455eb8070 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 26 May 2005 19:27:50 +0000 Subject: [PATCH] nohup now redirects a tty stdin to an unreadable fd instead of closing it. --- ChangeLog | 9 ++++++++- NEWS | 3 ++- doc/coreutils.texi | 4 +++- src/nohup.c | 27 +++++++++++++++++++++------ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9b2a7f4fbd..36b9431459 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,14 @@ -2005-05-26 Jim Meyering +2005-05-26 Paul Eggert * Version 5.3.1. + * NEWS: nohup now redirects a tty stdin to an unreadable fd + instead of closing it. + * doc/coreutils.texi (nohup invocation): Document this. + * src/nohup.c (main): Implement this. + +2005-05-26 Jim Meyering + * src/expr.c (toarith): Fix a sign error introduced on 2005-01-14. Reported by David Alan Gilbert. * tests/expr/basic: Add tests using arithmetic on negative integers. diff --git a/NEWS b/NEWS index 9a7b12cc2c..d94f24346e 100644 --- a/NEWS +++ b/NEWS @@ -143,7 +143,8 @@ GNU coreutils NEWS -*- outline -*- join now supports a NUL field separator, e.g., "join -t '\0'". join now detects and reports incompatible options, e.g., "join -t x -t y", - nohup now closes stdin if it is a terminal, unless POSIXLY_CORRECT is set. + If stdin is a terminal, nohup now closes it and then reopens it with an + unreadable file descriptor. (This step is skipped if POSIXLY_CORRECT is set.) This prevents the command from tying up an OpenSSH session after you logout. stat -f -c %S outputs the fundamental block size (used for block counts). diff --git a/doc/coreutils.texi b/doc/coreutils.texi index f0ad8d8bcf..3cbd7b1219 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -12571,7 +12571,9 @@ descriptor as the (possibly-redirected) standard output. @vindex POSIXLY_CORRECT If standard input is a terminal, it is closed so that terminal sessions do not mistakenly consider the terminal to be used by the -command. However, this step is skipped if @env{POSIXLY_CORRECT} is +command. To avoid glitches in poorly-written programs standard input +is then reopened with an innocuous file descriptor that cannot be read +from. However, these steps are skipped if @env{POSIXLY_CORRECT} is set since @acronym{POSIX} requires standard input to be left alone. @command{nohup} does not automatically put the command it runs in the diff --git a/src/nohup.c b/src/nohup.c index ba8b3c1f38..b4208fd746 100644 --- a/src/nohup.c +++ b/src/nohup.c @@ -75,6 +75,7 @@ int main (int argc, char **argv) { int saved_stderr_fd = STDERR_FILENO; + bool nohup_out = false; initialize_main (&argc, &argv); program_name = argv[0]; @@ -96,12 +97,6 @@ main (int argc, char **argv) usage (NOHUP_FAILURE); } - /* If standard input is a tty, close it. POSIX requires nohup to - leave standard input alone, but that's less useful in practice as - it causes a "nohup foo & exit" session to hang with OpenSSH. */ - if (!getenv ("POSIXLY_CORRECT") && isatty (STDIN_FILENO)) - close (STDIN_FILENO); - /* If standard output is a tty, redirect it (appending) to a file. First try nohup.out, then $HOME/nohup.out. */ if (isatty (STDOUT_FILENO)) @@ -143,6 +138,7 @@ main (int argc, char **argv) error (0, 0, _("appending output to %s"), quote (file)); free (in_home); + nohup_out = true; } /* If standard error is a tty, redirect it to stdout. */ @@ -168,6 +164,25 @@ main (int argc, char **argv) } } + /* If standard input is a tty, replace it with a file descriptor + that exists but gives you an error if you try to read it. POSIX + requires nohup to leave standard input alone, but that's less + useful in practice as it causes a "nohup foo & exit" session to + hang with OpenSSH. */ + if (!getenv ("POSIXLY_CORRECT") && isatty (STDIN_FILENO)) + { + close (STDIN_FILENO); + if (nohup_out) + dup (STDOUT_FILENO); + else + { + /* This doesn't give you an error on older systems if you're + root, but there's no portable way to fix this and it's + not worth worrying about these days. */ + open ("/", O_RDONLY); + } + } + signal (SIGHUP, SIG_IGN); { -- 2.47.3