1 /* Creation of autonomous subprocesses.
2 Copyright (C) 2001-2004, 2006-2008 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 #include "fatal-signal.h"
33 #include "wait-process.h"
36 #define _(str) gettext (str)
38 #if defined _MSC_VER || defined __MINGW32__
40 /* Native Woe32 API. */
42 # include "w32spawn.h"
58 # define STDIN_FILENO 0
61 # define STDOUT_FILENO 1
64 # define STDERR_FILENO 2
67 /* The results of open() in this file are not used with fchdir,
68 therefore save some unnecessary work in fchdir.c. */
75 /* EINTR handling for close(), open().
76 These functions can return -1/EINTR even though we don't have any
77 signal handlers set up, namely when we get interrupted via SIGSTOP. */
80 nonintr_close (int fd)
86 while (retval < 0 && errno == EINTR);
90 #define close nonintr_close
93 nonintr_open (const char *pathname, int oflag, mode_t mode)
98 retval = open (pathname, oflag, mode);
99 while (retval < 0 && errno == EINTR);
103 #undef open /* avoid warning on VMS */
104 #define open nonintr_open
109 /* Execute a command, optionally redirecting any of the three standard file
110 descriptors to /dev/null. Return its exit code.
111 If it didn't terminate correctly, exit if exit_on_error is true, otherwise
113 If slave_process is true, the child process will be terminated when its
114 creator receives a catchable fatal signal. */
116 execute (const char *progname,
117 const char *prog_path, char **prog_argv,
119 bool null_stdin, bool null_stdout, bool null_stderr,
120 bool slave_process, bool exit_on_error,
123 #if defined _MSC_VER || defined __MINGW32__
125 /* Native Woe32 API. */
133 /* FIXME: Need to free memory allocated by prepare_spawn. */
134 prog_argv = prepare_spawn (prog_argv);
136 /* Save standard file handles of parent process. */
138 orig_stdin = dup_noinherit (STDIN_FILENO);
140 orig_stdout = dup_noinherit (STDOUT_FILENO);
142 orig_stderr = dup_noinherit (STDERR_FILENO);
145 /* Create standard file handles of child process. */
149 || ((nullinfd = open ("NUL", O_RDONLY, 0)) >= 0
150 && (nullinfd == STDIN_FILENO
151 || (dup2 (nullinfd, STDIN_FILENO) >= 0
152 && close (nullinfd) >= 0))))
153 && (!(null_stdout || null_stderr)
154 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
156 || nulloutfd == STDOUT_FILENO
157 || dup2 (nulloutfd, STDOUT_FILENO) >= 0)
159 || nulloutfd == STDERR_FILENO
160 || dup2 (nulloutfd, STDERR_FILENO) >= 0)
161 && ((null_stdout && nulloutfd == STDOUT_FILENO)
162 || (null_stderr && nulloutfd == STDERR_FILENO)
163 || close (nulloutfd) >= 0))))
164 /* Use spawnvpe and pass the environment explicitly. This is needed if
165 the program has modified the environment using putenv() or [un]setenv().
166 On Windows, programs have two environments, one in the "environment
167 block" of the process and managed through SetEnvironmentVariable(), and
168 one inside the process, in the location retrieved by the 'environ'
169 macro. When using spawnvp() without 'e', the child process inherits a
170 copy of the environment block - ignoring the effects of putenv() and
173 exitcode = spawnvpe (P_WAIT, prog_path, prog_argv, environ);
174 if (exitcode < 0 && errno == ENOEXEC)
176 /* prog is not an native executable. Try to execute it as a
177 shell script. Note that prepare_spawn() has already prepended
178 a hidden element "sh.exe" to prog_argv. */
180 exitcode = spawnvpe (P_WAIT, prog_argv[0], prog_argv, environ);
188 /* Restore standard file handles of parent process. */
190 dup2 (orig_stderr, STDERR_FILENO), close (orig_stderr);
192 dup2 (orig_stdout, STDOUT_FILENO), close (orig_stdout);
194 dup2 (orig_stdin, STDIN_FILENO), close (orig_stdin);
196 if (termsigp != NULL)
201 if (exit_on_error || !null_stderr)
202 error (exit_on_error ? EXIT_FAILURE : 0, errno,
203 _("%s subprocess failed"), progname);
212 /* Note about 127: Some errors during posix_spawnp() cause the function
213 posix_spawnp() to return an error code; some other errors cause the
214 subprocess to exit with return code 127. It is implementation
215 dependent which error is reported which way. We treat both cases as
218 sigset_t blocked_signals;
219 posix_spawn_file_actions_t actions;
220 bool actions_allocated;
221 posix_spawnattr_t attrs;
222 bool attrs_allocated;
232 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
233 block_fatal_signals ();
235 actions_allocated = false;
236 attrs_allocated = false;
237 if ((err = posix_spawn_file_actions_init (&actions)) != 0
238 || (actions_allocated = true,
240 && (err = posix_spawn_file_actions_addopen (&actions,
242 "/dev/null", O_RDONLY,
246 && (err = posix_spawn_file_actions_addopen (&actions,
252 && (err = posix_spawn_file_actions_addopen (&actions,
258 && ((err = posix_spawnattr_init (&attrs)) != 0
259 || (attrs_allocated = true,
260 (err = posix_spawnattr_setsigmask (&attrs,
263 || (err = posix_spawnattr_setflags (&attrs,
264 POSIX_SPAWN_SETSIGMASK))
266 || (err = posix_spawnp (&child, prog_path, &actions,
267 attrs_allocated ? &attrs : NULL, prog_argv,
271 if (actions_allocated)
272 posix_spawn_file_actions_destroy (&actions);
274 posix_spawnattr_destroy (&attrs);
276 unblock_fatal_signals ();
277 if (termsigp != NULL)
279 if (exit_on_error || !null_stderr)
280 error (exit_on_error ? EXIT_FAILURE : 0, err,
281 _("%s subprocess failed"), progname);
284 posix_spawn_file_actions_destroy (&actions);
286 posix_spawnattr_destroy (&attrs);
289 block_fatal_signals ();
290 /* Use vfork() instead of fork() for efficiency. */
291 if ((child = vfork ()) == 0)
293 /* Child process code. */
298 || ((nullinfd = open ("/dev/null", O_RDONLY, 0)) >= 0
299 && (nullinfd == STDIN_FILENO
300 || (dup2 (nullinfd, STDIN_FILENO) >= 0
301 && close (nullinfd) >= 0))))
302 && (!(null_stdout || null_stderr)
303 || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
305 || nulloutfd == STDOUT_FILENO
306 || dup2 (nulloutfd, STDOUT_FILENO) >= 0)
308 || nulloutfd == STDERR_FILENO
309 || dup2 (nulloutfd, STDERR_FILENO) >= 0)
310 && ((null_stdout && nulloutfd == STDOUT_FILENO)
311 || (null_stderr && nulloutfd == STDERR_FILENO)
312 || close (nulloutfd) >= 0)))
313 && (!slave_process || (unblock_fatal_signals (), true)))
314 execvp (prog_path, prog_argv);
320 unblock_fatal_signals ();
321 if (termsigp != NULL)
323 if (exit_on_error || !null_stderr)
324 error (exit_on_error ? EXIT_FAILURE : 0, errno,
325 _("%s subprocess failed"), progname);
331 register_slave_subprocess (child);
332 unblock_fatal_signals ();
335 return wait_subprocess (child, progname, ignore_sigpipe, null_stderr,
336 slave_process, exit_on_error, termsigp);