nohup now redirects a tty stdin to an unreadable fd instead of closing it.
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 26 May 2005 19:27:50 +0000 (19:27 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 26 May 2005 19:27:50 +0000 (19:27 +0000)
ChangeLog
NEWS
doc/coreutils.texi
src/nohup.c

index 9b2a7f4..36b9431 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,7 +1,14 @@
-2005-05-26  Jim Meyering  <jim@meyering.net>
+2005-05-26  Paul Eggert  <eggert@cs.ucla.edu>
 
        * 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  <jim@meyering.net>
+
        * 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 9a7b12c..d94f243 100644 (file)
--- 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).
index f0ad8d8..3cbd7b1 100644 (file)
@@ -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
index ba8b3c1..b4208fd 100644 (file)
@@ -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);
 
   {