summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2005-07-03 07:18:48 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2005-07-03 07:18:48 +0000
commit2eeccaacd9fcb677ed963b5f2f6a7908f87a5125 (patch)
tree1c2667cb58efb19d2a19232d676e0ba4e159f9e2 /src
parent3fff9c1441fb00e94c5e7e660a2b9891820891b3 (diff)
downloadcoreutils-2eeccaacd9fcb677ed963b5f2f6a7908f87a5125.tar.xz
Include fd-reopen.h.
Include unistd--.h, not unistd-safer.h. (main): Use fd_reopen to simplify code. When replacing stdin, use "/dev/null" not "/", as that's less likely to go wrong these days. (main): Use dup, not dup_safer.
Diffstat (limited to 'src')
-rw-r--r--src/nohup.c44
1 files changed, 13 insertions, 31 deletions
diff --git a/src/nohup.c b/src/nohup.c
index cf9782de6..6cf9c9b78 100644
--- a/src/nohup.c
+++ b/src/nohup.c
@@ -28,9 +28,10 @@
#include "cloexec.h"
#include "error.h"
#include "filenamecat.h"
+#include "fd-reopen.h"
#include "long-options.h"
#include "quote.h"
-#include "unistd-safer.h"
+#include "unistd--.h"
#define PROGRAM_NAME "nohup"
@@ -75,7 +76,6 @@ int
main (int argc, char **argv)
{
int saved_stderr_fd = STDERR_FILENO;
- bool nohup_out = false;
initialize_main (&argc, &argv);
program_name = argv[0];
@@ -97,6 +97,14 @@ main (int argc, char **argv)
usage (NOHUP_FAILURE);
}
+ /* 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))
+ fd_reopen (STDIN_FILENO, "/dev/null", O_WRONLY, 0);
+
/* If standard output is a tty, redirect it (appending) to a file.
First try nohup.out, then $HOME/nohup.out. */
if (isatty (STDOUT_FILENO))
@@ -106,7 +114,7 @@ main (int argc, char **argv)
int flags = O_CREAT | O_WRONLY | O_APPEND;
mode_t mode = S_IRUSR | S_IWUSR;
mode_t umask_value = umask (~mode);
- int fd = open (file, flags, mode);
+ int fd = fd_reopen (STDOUT_FILENO, file, flags, mode);
if (fd < 0)
{
@@ -115,7 +123,7 @@ main (int argc, char **argv)
if (home)
{
in_home = file_name_concat (home, file, NULL);
- fd = open (in_home, flags, mode);
+ fd = fd_reopen (STDOUT_FILENO, in_home, flags, mode);
}
if (fd < 0)
{
@@ -130,15 +138,8 @@ main (int argc, char **argv)
}
umask (umask_value);
-
- /* Redirect standard output to the file. */
- if (fd != STDOUT_FILENO
- && (dup2 (fd, STDOUT_FILENO) < 0 || close (fd) != 0))
- error (NOHUP_FAILURE, errno, _("failed to redirect standard output"));
-
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. */
@@ -148,7 +149,7 @@ main (int argc, char **argv)
if execve fails. It's no big deal if this dup fails. It might
not change anything, and at worst, it'll lead to suppression of
the post-failed-execve diagnostic. */
- saved_stderr_fd = dup_safer (STDERR_FILENO);
+ saved_stderr_fd = dup (STDERR_FILENO);
if (0 <= saved_stderr_fd
&& set_cloexec_flag (saved_stderr_fd, true) != 0)
@@ -164,25 +165,6 @@ 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);
{