sort: pacify GCC 12 false positive
[coreutils.git] / src / nohup.c
blob47d74dd477198667d4e24155b860b88e98f3320c
1 /* nohup -- run a command immune to hangups, with output to a non-tty
2 Copyright (C) 2003-2022 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Jim Meyering */
19 #include <config.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <signal.h>
24 #include "system.h"
26 #include "error.h"
27 #include "filenamecat.h"
28 #include "fd-reopen.h"
29 #include "long-options.h"
30 #include "unistd--.h"
32 #define PROGRAM_NAME "nohup"
34 #define AUTHORS proper_name ("Jim Meyering")
36 /* Exit statuses. */
37 enum
39 /* 'nohup' itself failed. */
40 POSIX_NOHUP_FAILURE = 127
43 void
44 usage (int status)
46 if (status != EXIT_SUCCESS)
47 emit_try_help ();
48 else
50 printf (_("\
51 Usage: %s COMMAND [ARG]...\n\
52 or: %s OPTION\n\
53 "),
54 program_name, program_name);
56 fputs (_("\
57 Run COMMAND, ignoring hangup signals.\n\
58 \n\
59 "), stdout);
60 fputs (HELP_OPTION_DESCRIPTION, stdout);
61 fputs (VERSION_OPTION_DESCRIPTION, stdout);
62 printf (_("\n\
63 If standard input is a terminal, redirect it from an unreadable file.\n\
64 If standard output is a terminal, append output to 'nohup.out' if possible,\n\
65 '$HOME/nohup.out' otherwise.\n\
66 If standard error is a terminal, redirect it to standard output.\n\
67 To save output to FILE, use '%s COMMAND > FILE'.\n"),
68 program_name);
69 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
70 emit_ancillary_info (PROGRAM_NAME);
72 exit (status);
75 int
76 main (int argc, char **argv)
78 int out_fd = STDOUT_FILENO;
79 int saved_stderr_fd = STDERR_FILENO;
80 bool ignoring_input;
81 bool redirecting_stdout;
82 bool stdout_is_closed;
83 bool redirecting_stderr;
84 int exit_internal_failure;
86 initialize_main (&argc, &argv);
87 set_program_name (argv[0]);
88 setlocale (LC_ALL, "");
89 bindtextdomain (PACKAGE, LOCALEDIR);
90 textdomain (PACKAGE);
92 /* POSIX 2008 requires that internal failure give status 127; unlike
93 for env, exec, nice, time, and xargs where it requires internal
94 failure give something in the range 1-125. For consistency with
95 other tools, fail with EXIT_CANCELED unless POSIXLY_CORRECT. */
96 exit_internal_failure = (getenv ("POSIXLY_CORRECT")
97 ? POSIX_NOHUP_FAILURE : EXIT_CANCELED);
98 initialize_exit_failure (exit_internal_failure);
99 atexit (close_stdout);
101 parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
102 Version, false, usage, AUTHORS,
103 (char const *) NULL);
105 if (argc <= optind)
107 error (0, 0, _("missing operand"));
108 usage (exit_internal_failure);
111 ignoring_input = isatty (STDIN_FILENO);
112 redirecting_stdout = isatty (STDOUT_FILENO);
113 stdout_is_closed = (!redirecting_stdout && errno == EBADF);
114 redirecting_stderr = isatty (STDERR_FILENO);
116 /* If standard input is a tty, replace it with /dev/null if possible.
117 Note that it is deliberately opened for *writing*,
118 to ensure any read evokes an error. */
119 if (ignoring_input)
121 if (fd_reopen (STDIN_FILENO, "/dev/null", O_WRONLY, 0) < 0)
122 error (exit_internal_failure, errno,
123 _("failed to render standard input unusable"));
124 if (!redirecting_stdout && !redirecting_stderr)
125 error (0, 0, _("ignoring input"));
128 /* If standard output is a tty, redirect it (appending) to a file.
129 First try nohup.out, then $HOME/nohup.out. If standard error is
130 a tty and standard output is closed, open nohup.out or
131 $HOME/nohup.out without redirecting anything. */
132 if (redirecting_stdout || (redirecting_stderr && stdout_is_closed))
134 char *in_home = NULL;
135 char const *file = "nohup.out";
136 int flags = O_CREAT | O_WRONLY | O_APPEND;
137 mode_t mode = S_IRUSR | S_IWUSR;
138 mode_t umask_value = umask (~mode);
139 out_fd = (redirecting_stdout
140 ? fd_reopen (STDOUT_FILENO, file, flags, mode)
141 : open (file, flags, mode));
143 if (out_fd < 0)
145 int saved_errno = errno;
146 char const *home = getenv ("HOME");
147 if (home)
149 in_home = file_name_concat (home, file, NULL);
150 out_fd = (redirecting_stdout
151 ? fd_reopen (STDOUT_FILENO, in_home, flags, mode)
152 : open (in_home, flags, mode));
154 if (out_fd < 0)
156 int saved_errno2 = errno;
157 error (0, saved_errno, _("failed to open %s"), quoteaf (file));
158 if (in_home)
159 error (0, saved_errno2, _("failed to open %s"),
160 quoteaf (in_home));
161 return exit_internal_failure;
163 file = in_home;
166 umask (umask_value);
167 error (0, 0,
168 _(ignoring_input
169 ? N_("ignoring input and appending output to %s")
170 : N_("appending output to %s")),
171 quoteaf (file));
172 free (in_home);
175 /* If standard error is a tty, redirect it. */
176 if (redirecting_stderr)
178 /* Save a copy of stderr before redirecting, so we can use the original
179 if execve fails. It's no big deal if this dup fails. It might
180 not change anything, and at worst, it'll lead to suppression of
181 the post-failed-execve diagnostic. */
182 saved_stderr_fd = fcntl (STDERR_FILENO, F_DUPFD_CLOEXEC,
183 STDERR_FILENO + 1);
185 if (!redirecting_stdout)
186 error (0, 0,
187 _(ignoring_input
188 ? N_("ignoring input and redirecting stderr to stdout")
189 : N_("redirecting stderr to stdout")));
191 if (dup2 (out_fd, STDERR_FILENO) < 0)
192 error (exit_internal_failure, errno,
193 _("failed to redirect standard error"));
195 if (stdout_is_closed)
196 close (out_fd);
199 /* error() flushes stderr, but does not check for write failure.
200 Normally, we would catch this via our atexit() hook of
201 close_stdout, but execvp() gets in the way. If stderr
202 encountered a write failure, there is no need to try calling
203 error() again, particularly since we may have just changed the
204 underlying fd out from under stderr. */
205 if (ferror (stderr))
206 return exit_internal_failure;
208 signal (SIGHUP, SIG_IGN);
210 char **cmd = argv + optind;
211 execvp (*cmd, cmd);
212 int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
213 int saved_errno = errno;
215 /* The execve failed. Output a diagnostic to stderr only if:
216 - stderr was initially redirected to a non-tty, or
217 - stderr was initially directed to a tty, and we
218 can dup2 it to point back to that same tty.
219 In other words, output the diagnostic if possible, but only if
220 it will go to the original stderr. */
221 if (dup2 (saved_stderr_fd, STDERR_FILENO) == STDERR_FILENO)
222 error (0, saved_errno, _("failed to run command %s"), quoteaf (*cmd));
224 return exit_status;