bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: nohup definition should be changed


From: Paul Eggert
Subject: Re: nohup definition should be changed
Date: Thu, 26 May 2005 12:30:25 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux)

address@hidden (James Youngman) writes:

> For extra safety I would suggest opening "/" for input instead of
> "/dev/null".

OK.  Or, better yet, use the existing nohup.out file descriptor since
it cannot be read from.  We can fall back on "/" if there's no
nohup.out.  I installed this:

2005-05-26  Paul Eggert  <address@hidden>

        * 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.

Index: NEWS
===================================================================
RCS file: /fetish/cu/NEWS,v
retrieving revision 1.289
diff -p -u -r1.289 NEWS
--- NEWS        13 May 2005 08:16:53 -0000      1.289
+++ NEWS        26 May 2005 19:21:46 -0000
@@ -143,7 +143,8 @@ GNU coreutils NEWS                      
   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: doc/coreutils.texi
===================================================================
RCS file: /fetish/cu/doc/coreutils.texi,v
retrieving revision 1.257
diff -p -u -r1.257 coreutils.texi
--- doc/coreutils.texi  12 May 2005 09:24:27 -0000      1.257
+++ doc/coreutils.texi  26 May 2005 19:21:49 -0000
@@ -12571,7 +12571,9 @@ descriptor as the (possibly-redirected) 
 @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: src/nohup.c
===================================================================
RCS file: /fetish/cu/src/nohup.c,v
retrieving revision 1.26
diff -p -u -r1.26 nohup.c
--- src/nohup.c 14 May 2005 07:58:37 -0000      1.26
+++ src/nohup.c 26 May 2005 19:21:49 -0000
@@ -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 won't give you a read 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);
 
   {




reply via email to

[Prev in Thread] Current Thread [Next in Thread]