tests: adjust memory limits in head-c.sh
[coreutils.git] / src / nohup.c
blob11ccf3fdb3b7fb6d4f20158781aaab9e8a1b7ad2
1 /* nohup -- run a command immune to hangups, with output to a non-tty
2 Copyright (C) 2003-2016 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 "unistd--.h"
34 #define PROGRAM_NAME "nohup"
36 #define AUTHORS proper_name ("Jim Meyering")
38 /* Exit statuses. */
39 enum
41 /* 'nohup' itself failed. */
42 POSIX_NOHUP_FAILURE = 127
45 void
46 usage (int status)
48 if (status != EXIT_SUCCESS)
49 emit_try_help ();
50 else
52 printf (_("\
53 Usage: %s COMMAND [ARG]...\n\
54 or: %s OPTION\n\
55 "),
56 program_name, program_name);
58 fputs (_("\
59 Run COMMAND, ignoring hangup signals.\n\
60 \n\
61 "), stdout);
62 fputs (HELP_OPTION_DESCRIPTION, stdout);
63 fputs (VERSION_OPTION_DESCRIPTION, stdout);
64 printf (_("\n\
65 If standard input is a terminal, redirect it from an unreadable file.\n\
66 If standard output is a terminal, append output to 'nohup.out' if possible,\n\
67 '$HOME/nohup.out' otherwise.\n\
68 If standard error is a terminal, redirect it to standard output.\n\
69 To save output to FILE, use '%s COMMAND > FILE'.\n"),
70 program_name);
71 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
72 emit_ancillary_info (PROGRAM_NAME);
74 exit (status);
77 int
78 main (int argc, char **argv)
80 int out_fd = STDOUT_FILENO;
81 int saved_stderr_fd = STDERR_FILENO;
82 bool ignoring_input;
83 bool redirecting_stdout;
84 bool stdout_is_closed;
85 bool redirecting_stderr;
86 int exit_internal_failure;
88 initialize_main (&argc, &argv);
89 set_program_name (argv[0]);
90 setlocale (LC_ALL, "");
91 bindtextdomain (PACKAGE, LOCALEDIR);
92 textdomain (PACKAGE);
94 /* POSIX 2008 requires that internal failure give status 127; unlike
95 for env, exec, nice, time, and xargs where it requires internal
96 failure give something in the range 1-125. For consistency with
97 other tools, fail with EXIT_CANCELED unless POSIXLY_CORRECT. */
98 exit_internal_failure = (getenv ("POSIXLY_CORRECT")
99 ? POSIX_NOHUP_FAILURE : EXIT_CANCELED);
100 initialize_exit_failure (exit_internal_failure);
101 atexit (close_stdout);
103 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
104 usage, AUTHORS, (char const *) NULL);
105 if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
106 usage (exit_internal_failure);
108 if (argc <= optind)
110 error (0, 0, _("missing operand"));
111 usage (exit_internal_failure);
114 ignoring_input = isatty (STDIN_FILENO);
115 redirecting_stdout = isatty (STDOUT_FILENO);
116 stdout_is_closed = (!redirecting_stdout && errno == EBADF);
117 redirecting_stderr = isatty (STDERR_FILENO);
119 /* If standard input is a tty, replace it with /dev/null if possible.
120 Note that it is deliberately opened for *writing*,
121 to ensure any read evokes an error. */
122 if (ignoring_input)
124 if (fd_reopen (STDIN_FILENO, "/dev/null", O_WRONLY, 0) < 0)
125 error (exit_internal_failure, errno,
126 _("failed to render standard input unusable"));
127 if (!redirecting_stdout && !redirecting_stderr)
128 error (0, 0, _("ignoring input"));
131 /* If standard output is a tty, redirect it (appending) to a file.
132 First try nohup.out, then $HOME/nohup.out. If standard error is
133 a tty and standard output is closed, open nohup.out or
134 $HOME/nohup.out without redirecting anything. */
135 if (redirecting_stdout || (redirecting_stderr && stdout_is_closed))
137 char *in_home = NULL;
138 char const *file = "nohup.out";
139 int flags = O_CREAT | O_WRONLY | O_APPEND;
140 mode_t mode = S_IRUSR | S_IWUSR;
141 mode_t umask_value = umask (~mode);
142 out_fd = (redirecting_stdout
143 ? fd_reopen (STDOUT_FILENO, file, flags, mode)
144 : open (file, flags, mode));
146 if (out_fd < 0)
148 int saved_errno = errno;
149 char const *home = getenv ("HOME");
150 if (home)
152 in_home = file_name_concat (home, file, NULL);
153 out_fd = (redirecting_stdout
154 ? fd_reopen (STDOUT_FILENO, in_home, flags, mode)
155 : open (in_home, flags, mode));
157 if (out_fd < 0)
159 int saved_errno2 = errno;
160 error (0, saved_errno, _("failed to open %s"), quoteaf (file));
161 if (in_home)
162 error (0, saved_errno2, _("failed to open %s"),
163 quoteaf (in_home));
164 return exit_internal_failure;
166 file = in_home;
169 umask (umask_value);
170 error (0, 0,
171 _(ignoring_input
172 ? N_("ignoring input and appending output to %s")
173 : N_("appending output to %s")),
174 quoteaf (file));
175 free (in_home);
178 /* If standard error is a tty, redirect it. */
179 if (redirecting_stderr)
181 /* Save a copy of stderr before redirecting, so we can use the original
182 if execve fails. It's no big deal if this dup fails. It might
183 not change anything, and at worst, it'll lead to suppression of
184 the post-failed-execve diagnostic. */
185 saved_stderr_fd = dup (STDERR_FILENO);
187 if (0 <= saved_stderr_fd
188 && set_cloexec_flag (saved_stderr_fd, true) != 0)
189 error (exit_internal_failure, errno,
190 _("failed to set the copy of stderr to close on exec"));
192 if (!redirecting_stdout)
193 error (0, 0,
194 _(ignoring_input
195 ? N_("ignoring input and redirecting stderr to stdout")
196 : N_("redirecting stderr to stdout")));
198 if (dup2 (out_fd, STDERR_FILENO) < 0)
199 error (exit_internal_failure, errno,
200 _("failed to redirect standard error"));
202 if (stdout_is_closed)
203 close (out_fd);
206 /* error() flushes stderr, but does not check for write failure.
207 Normally, we would catch this via our atexit() hook of
208 close_stdout, but execvp() gets in the way. If stderr
209 encountered a write failure, there is no need to try calling
210 error() again, particularly since we may have just changed the
211 underlying fd out from under stderr. */
212 if (ferror (stderr))
213 return exit_internal_failure;
215 signal (SIGHUP, SIG_IGN);
217 char **cmd = argv + optind;
218 execvp (*cmd, cmd);
219 int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
220 int saved_errno = errno;
222 /* The execve failed. Output a diagnostic to stderr only if:
223 - stderr was initially redirected to a non-tty, or
224 - stderr was initially directed to a tty, and we
225 can dup2 it to point back to that same tty.
226 In other words, output the diagnostic if possible, but only if
227 it will go to the original stderr. */
228 if (dup2 (saved_stderr_fd, STDERR_FILENO) == STDERR_FILENO)
229 error (0, saved_errno, _("failed to run command %s"), quoteaf (*cmd));
231 return exit_status;