all: replace most uses of quotearg_colon() with quote()
[coreutils.git] / src / nohup.c
blob8cdacedb8a62453dafe0bc303d3136e90776122e
1 /* nohup -- run a command immune to hangups, with output to a non-tty
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Jim Meyering */
19 #include <config.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <signal.h>
25 #include "system.h"
27 #include "cloexec.h"
28 #include "error.h"
29 #include "filenamecat.h"
30 #include "fd-reopen.h"
31 #include "long-options.h"
32 #include "quote.h"
33 #include "unistd--.h"
35 #define PROGRAM_NAME "nohup"
37 #define AUTHORS proper_name ("Jim Meyering")
39 /* Exit statuses. */
40 enum
42 /* 'nohup' itself failed. */
43 POSIX_NOHUP_FAILURE = 127
46 void
47 usage (int status)
49 if (status != EXIT_SUCCESS)
50 emit_try_help ();
51 else
53 printf (_("\
54 Usage: %s COMMAND [ARG]...\n\
55 or: %s OPTION\n\
56 "),
57 program_name, program_name);
59 fputs (_("\
60 Run COMMAND, ignoring hangup signals.\n\
61 \n\
62 "), stdout);
63 fputs (HELP_OPTION_DESCRIPTION, stdout);
64 fputs (VERSION_OPTION_DESCRIPTION, stdout);
65 printf (_("\n\
66 If standard input is a terminal, redirect it from an unreadable file.\n\
67 If standard output is a terminal, append output to 'nohup.out' if possible,\n\
68 '$HOME/nohup.out' otherwise.\n\
69 If standard error is a terminal, redirect it to standard output.\n\
70 To save output to FILE, use '%s COMMAND > FILE'.\n"),
71 program_name);
72 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
73 emit_ancillary_info (PROGRAM_NAME);
75 exit (status);
78 int
79 main (int argc, char **argv)
81 int out_fd = STDOUT_FILENO;
82 int saved_stderr_fd = STDERR_FILENO;
83 bool ignoring_input;
84 bool redirecting_stdout;
85 bool stdout_is_closed;
86 bool redirecting_stderr;
87 int exit_internal_failure;
89 initialize_main (&argc, &argv);
90 set_program_name (argv[0]);
91 setlocale (LC_ALL, "");
92 bindtextdomain (PACKAGE, LOCALEDIR);
93 textdomain (PACKAGE);
95 /* POSIX 2008 requires that internal failure give status 127; unlike
96 for env, exec, nice, time, and xargs where it requires internal
97 failure give something in the range 1-125. For consistency with
98 other tools, fail with EXIT_CANCELED unless POSIXLY_CORRECT. */
99 exit_internal_failure = (getenv ("POSIXLY_CORRECT")
100 ? POSIX_NOHUP_FAILURE : EXIT_CANCELED);
101 initialize_exit_failure (exit_internal_failure);
102 atexit (close_stdout);
104 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
105 usage, AUTHORS, (char const *) NULL);
106 if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
107 usage (exit_internal_failure);
109 if (argc <= optind)
111 error (0, 0, _("missing operand"));
112 usage (exit_internal_failure);
115 ignoring_input = isatty (STDIN_FILENO);
116 redirecting_stdout = isatty (STDOUT_FILENO);
117 stdout_is_closed = (!redirecting_stdout && errno == EBADF);
118 redirecting_stderr = isatty (STDERR_FILENO);
120 /* If standard input is a tty, replace it with /dev/null if possible.
121 Note that it is deliberately opened for *writing*,
122 to ensure any read evokes an error. */
123 if (ignoring_input)
125 if (fd_reopen (STDIN_FILENO, "/dev/null", O_WRONLY, 0) < 0)
126 error (exit_internal_failure, errno,
127 _("failed to render standard input unusable"));
128 if (!redirecting_stdout && !redirecting_stderr)
129 error (0, 0, _("ignoring input"));
132 /* If standard output is a tty, redirect it (appending) to a file.
133 First try nohup.out, then $HOME/nohup.out. If standard error is
134 a tty and standard output is closed, open nohup.out or
135 $HOME/nohup.out without redirecting anything. */
136 if (redirecting_stdout || (redirecting_stderr && stdout_is_closed))
138 char *in_home = NULL;
139 char const *file = "nohup.out";
140 int flags = O_CREAT | O_WRONLY | O_APPEND;
141 mode_t mode = S_IRUSR | S_IWUSR;
142 mode_t umask_value = umask (~mode);
143 out_fd = (redirecting_stdout
144 ? fd_reopen (STDOUT_FILENO, file, flags, mode)
145 : open (file, flags, mode));
147 if (out_fd < 0)
149 int saved_errno = errno;
150 char const *home = getenv ("HOME");
151 if (home)
153 in_home = file_name_concat (home, file, NULL);
154 out_fd = (redirecting_stdout
155 ? fd_reopen (STDOUT_FILENO, in_home, flags, mode)
156 : open (in_home, flags, mode));
158 if (out_fd < 0)
160 int saved_errno2 = errno;
161 error (0, saved_errno, _("failed to open %s"), quote (file));
162 if (in_home)
163 error (0, saved_errno2, _("failed to open %s"),
164 quote (in_home));
165 return exit_internal_failure;
167 file = in_home;
170 umask (umask_value);
171 error (0, 0,
172 _(ignoring_input
173 ? N_("ignoring input and appending output to %s")
174 : N_("appending output to %s")),
175 quote (file));
176 free (in_home);
179 /* If standard error is a tty, redirect it. */
180 if (redirecting_stderr)
182 /* Save a copy of stderr before redirecting, so we can use the original
183 if execve fails. It's no big deal if this dup fails. It might
184 not change anything, and at worst, it'll lead to suppression of
185 the post-failed-execve diagnostic. */
186 saved_stderr_fd = dup (STDERR_FILENO);
188 if (0 <= saved_stderr_fd
189 && set_cloexec_flag (saved_stderr_fd, true) != 0)
190 error (exit_internal_failure, errno,
191 _("failed to set the copy of stderr to close on exec"));
193 if (!redirecting_stdout)
194 error (0, 0,
195 _(ignoring_input
196 ? N_("ignoring input and redirecting stderr to stdout")
197 : N_("redirecting stderr to stdout")));
199 if (dup2 (out_fd, STDERR_FILENO) < 0)
200 error (exit_internal_failure, errno,
201 _("failed to redirect standard error"));
203 if (stdout_is_closed)
204 close (out_fd);
207 /* error() flushes stderr, but does not check for write failure.
208 Normally, we would catch this via our atexit() hook of
209 close_stdout, but execvp() gets in the way. If stderr
210 encountered a write failure, there is no need to try calling
211 error() again, particularly since we may have just changed the
212 underlying fd out from under stderr. */
213 if (ferror (stderr))
214 return exit_internal_failure;
216 signal (SIGHUP, SIG_IGN);
218 char **cmd = argv + optind;
219 execvp (*cmd, cmd);
220 int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
221 int saved_errno = errno;
223 /* The execve failed. Output a diagnostic to stderr only if:
224 - stderr was initially redirected to a non-tty, or
225 - stderr was initially directed to a tty, and we
226 can dup2 it to point back to that same tty.
227 In other words, output the diagnostic if possible, but only if
228 it will go to the original stderr. */
229 if (dup2 (saved_stderr_fd, STDERR_FILENO) == STDERR_FILENO)
230 error (0, saved_errno, _("failed to run command %s"), quote (*cmd));
232 return exit_status;