1 /* Creation of autonomous subprocesses.
2 Copyright (C) 2001-2004, 2006-2011 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 _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
40 /* Native Woe32 API. */
42 # include "w32spawn.h"
51 /* The results of open() in this file are not used with fchdir,
52 therefore save some unnecessary work in fchdir.c. */
59 /* EINTR handling for close(), open().
60 These functions can return -1/EINTR even though we don't have any
61 signal handlers set up, namely when we get interrupted via SIGSTOP. */
64 nonintr_close (int fd)
70 while (retval < 0 && errno == EINTR);
74 #define close nonintr_close
77 nonintr_open (const char *pathname, int oflag, mode_t mode)
82 retval = open (pathname, oflag, mode);
83 while (retval < 0 && errno == EINTR);
87 #undef open /* avoid warning on VMS */
88 #define open nonintr_open
93 /* Execute a command, optionally redirecting any of the three standard file
94 descriptors to /dev/null. Return its exit code.
95 If it didn't terminate correctly, exit if exit_on_error is true, otherwise
97 If slave_process is true, the child process will be terminated when its
98 creator receives a catchable fatal signal. */
100 execute (const char *progname,
101 const char *prog_path, char **prog_argv,
103 bool null_stdin, bool null_stdout, bool null_stderr,
104 bool slave_process, bool exit_on_error,
107 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
109 /* Native Woe32 API. */
117 /* FIXME: Need to free memory allocated by prepare_spawn. */
118 prog_argv = prepare_spawn (prog_argv);
120 /* Save standard file handles of parent process. */
122 orig_stdin = dup_safer_noinherit (STDIN_FILENO);
124 orig_stdout = dup_safer_noinherit (STDOUT_FILENO);
126 orig_stderr = dup_safer_noinherit (STDERR_FILENO);
129 /* Create standard file handles of child process. */
133 || ((nullinfd = open ("NUL", O_RDONLY, 0)) >= 0
134 && (nullinfd == STDIN_FILENO
135 || (dup2 (nullinfd, STDIN_FILENO) >= 0
136 && close (nullinfd) >= 0))))
137 && (!(null_stdout || null_stderr)
138 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
140 || nulloutfd == STDOUT_FILENO
141 || dup2 (nulloutfd, STDOUT_FILENO) >= 0)
143 || nulloutfd == STDERR_FILENO
144 || dup2 (nulloutfd, STDERR_FILENO) >= 0)
145 && ((null_stdout && nulloutfd == STDOUT_FILENO)
146 || (null_stderr && nulloutfd == STDERR_FILENO)
147 || close (nulloutfd) >= 0))))
148 /* Use spawnvpe and pass the environment explicitly. This is needed if
149 the program has modified the environment using putenv() or [un]setenv().
150 On Windows, programs have two environments, one in the "environment
151 block" of the process and managed through SetEnvironmentVariable(), and
152 one inside the process, in the location retrieved by the 'environ'
153 macro. When using spawnvp() without 'e', the child process inherits a
154 copy of the environment block - ignoring the effects of putenv() and
157 exitcode = spawnvpe (P_WAIT, prog_path, (const char **) prog_argv,
158 (const char **) environ);
159 if (exitcode < 0 && errno == ENOEXEC)
161 /* prog is not an native executable. Try to execute it as a
162 shell script. Note that prepare_spawn() has already prepended
163 a hidden element "sh.exe" to prog_argv. */
165 exitcode = spawnvpe (P_WAIT, prog_argv[0], (const char **) prog_argv,
166 (const char **) environ);
174 /* Restore standard file handles of parent process. */
176 undup_safer_noinherit (orig_stderr, STDERR_FILENO);
178 undup_safer_noinherit (orig_stdout, STDOUT_FILENO);
180 undup_safer_noinherit (orig_stdin, STDIN_FILENO);
182 if (termsigp != NULL)
187 if (exit_on_error || !null_stderr)
188 error (exit_on_error ? EXIT_FAILURE : 0, errno,
189 _("%s subprocess failed"), progname);
198 /* Note about 127: Some errors during posix_spawnp() cause the function
199 posix_spawnp() to return an error code; some other errors cause the
200 subprocess to exit with return code 127. It is implementation
201 dependent which error is reported which way. We treat both cases as
203 sigset_t blocked_signals;
204 posix_spawn_file_actions_t actions;
205 bool actions_allocated;
206 posix_spawnattr_t attrs;
207 bool attrs_allocated;
213 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
214 block_fatal_signals ();
216 actions_allocated = false;
217 attrs_allocated = false;
218 if ((err = posix_spawn_file_actions_init (&actions)) != 0
219 || (actions_allocated = true,
221 && (err = posix_spawn_file_actions_addopen (&actions,
223 "/dev/null", O_RDONLY,
227 && (err = posix_spawn_file_actions_addopen (&actions,
233 && (err = posix_spawn_file_actions_addopen (&actions,
239 && ((err = posix_spawnattr_init (&attrs)) != 0
240 || (attrs_allocated = true,
241 (err = posix_spawnattr_setsigmask (&attrs,
244 || (err = posix_spawnattr_setflags (&attrs,
245 POSIX_SPAWN_SETSIGMASK))
247 || (err = posix_spawnp (&child, prog_path, &actions,
248 attrs_allocated ? &attrs : NULL, prog_argv,
252 if (actions_allocated)
253 posix_spawn_file_actions_destroy (&actions);
255 posix_spawnattr_destroy (&attrs);
257 unblock_fatal_signals ();
258 if (termsigp != NULL)
260 if (exit_on_error || !null_stderr)
261 error (exit_on_error ? EXIT_FAILURE : 0, err,
262 _("%s subprocess failed"), progname);
265 posix_spawn_file_actions_destroy (&actions);
267 posix_spawnattr_destroy (&attrs);
270 register_slave_subprocess (child);
271 unblock_fatal_signals ();
274 return wait_subprocess (child, progname, ignore_sigpipe, null_stderr,
275 slave_process, exit_on_error, termsigp);