hash-set, linkedhash-set: Reduce code duplication.
[gnulib.git] / lib / execute.c
blob1194dd61302615397e8bc5a45435cd8940e0815a
1 /* Creation of autonomous subprocesses.
2 Copyright (C) 2001-2004, 2006-2018 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 <https://www.gnu.org/licenses/>. */
19 #include <config.h>
21 /* Specification. */
22 #include "execute.h"
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdbool.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #include <unistd.h>
31 #include "error.h"
32 #include "fatal-signal.h"
33 #include "wait-process.h"
34 #include "gettext.h"
36 #define _(str) gettext (str)
38 #if defined _WIN32 && ! defined __CYGWIN__
40 /* Native Windows API. */
41 # include <process.h>
42 # include "w32spawn.h"
44 #else
46 /* Unix API. */
47 # include <spawn.h>
49 #endif
52 #if defined EINTR && (defined _WIN32 && ! defined __CYGWIN__)
54 /* EINTR handling for close(), open().
55 These functions can return -1/EINTR even though we don't have any
56 signal handlers set up, namely when we get interrupted via SIGSTOP. */
58 static int
59 nonintr_close (int fd)
61 int retval;
64 retval = close (fd);
65 while (retval < 0 && errno == EINTR);
67 return retval;
69 #undef close /* avoid warning related to gnulib module unistd */
70 #define close nonintr_close
72 static int
73 nonintr_open (const char *pathname, int oflag, mode_t mode)
75 int retval;
78 retval = open (pathname, oflag, mode);
79 while (retval < 0 && errno == EINTR);
81 return retval;
83 #undef open /* avoid warning on VMS */
84 #define open nonintr_open
86 #endif
89 /* Execute a command, optionally redirecting any of the three standard file
90 descriptors to /dev/null. Return its exit code.
91 If it didn't terminate correctly, exit if exit_on_error is true, otherwise
92 return 127.
93 If slave_process is true, the child process will be terminated when its
94 creator receives a catchable fatal signal. */
95 int
96 execute (const char *progname,
97 const char *prog_path, char **prog_argv,
98 bool ignore_sigpipe,
99 bool null_stdin, bool null_stdout, bool null_stderr,
100 bool slave_process, bool exit_on_error,
101 int *termsigp)
103 #if defined _WIN32 && ! defined __CYGWIN__
105 /* Native Windows API. */
106 int orig_stdin;
107 int orig_stdout;
108 int orig_stderr;
109 int exitcode;
110 int nullinfd;
111 int nulloutfd;
113 /* FIXME: Need to free memory allocated by prepare_spawn. */
114 prog_argv = prepare_spawn (prog_argv);
116 /* Save standard file handles of parent process. */
117 if (null_stdin)
118 orig_stdin = dup_safer_noinherit (STDIN_FILENO);
119 if (null_stdout)
120 orig_stdout = dup_safer_noinherit (STDOUT_FILENO);
121 if (null_stderr)
122 orig_stderr = dup_safer_noinherit (STDERR_FILENO);
123 exitcode = -1;
125 /* Create standard file handles of child process. */
126 nullinfd = -1;
127 nulloutfd = -1;
128 if ((!null_stdin
129 || ((nullinfd = open ("NUL", O_RDONLY, 0)) >= 0
130 && (nullinfd == STDIN_FILENO
131 || (dup2 (nullinfd, STDIN_FILENO) >= 0
132 && close (nullinfd) >= 0))))
133 && (!(null_stdout || null_stderr)
134 || ((nulloutfd = open ("NUL", O_RDWR, 0)) >= 0
135 && (!null_stdout
136 || nulloutfd == STDOUT_FILENO
137 || dup2 (nulloutfd, STDOUT_FILENO) >= 0)
138 && (!null_stderr
139 || nulloutfd == STDERR_FILENO
140 || dup2 (nulloutfd, STDERR_FILENO) >= 0)
141 && ((null_stdout && nulloutfd == STDOUT_FILENO)
142 || (null_stderr && nulloutfd == STDERR_FILENO)
143 || close (nulloutfd) >= 0))))
144 /* Use spawnvpe and pass the environment explicitly. This is needed if
145 the program has modified the environment using putenv() or [un]setenv().
146 On Windows, programs have two environments, one in the "environment
147 block" of the process and managed through SetEnvironmentVariable(), and
148 one inside the process, in the location retrieved by the 'environ'
149 macro. When using spawnvp() without 'e', the child process inherits a
150 copy of the environment block - ignoring the effects of putenv() and
151 [un]setenv(). */
153 exitcode = spawnvpe (P_WAIT, prog_path, (const char **) prog_argv,
154 (const char **) environ);
155 if (exitcode < 0 && errno == ENOEXEC)
157 /* prog is not a native executable. Try to execute it as a
158 shell script. Note that prepare_spawn() has already prepended
159 a hidden element "sh.exe" to prog_argv. */
160 --prog_argv;
161 exitcode = spawnvpe (P_WAIT, prog_argv[0], (const char **) prog_argv,
162 (const char **) environ);
165 if (nulloutfd >= 0)
166 close (nulloutfd);
167 if (nullinfd >= 0)
168 close (nullinfd);
170 /* Restore standard file handles of parent process. */
171 if (null_stderr)
172 undup_safer_noinherit (orig_stderr, STDERR_FILENO);
173 if (null_stdout)
174 undup_safer_noinherit (orig_stdout, STDOUT_FILENO);
175 if (null_stdin)
176 undup_safer_noinherit (orig_stdin, STDIN_FILENO);
178 if (termsigp != NULL)
179 *termsigp = 0;
181 if (exitcode == -1)
183 if (exit_on_error || !null_stderr)
184 error (exit_on_error ? EXIT_FAILURE : 0, errno,
185 _("%s subprocess failed"), progname);
186 return 127;
189 return exitcode;
191 #else
193 /* Unix API. */
194 /* Note about 127: Some errors during posix_spawnp() cause the function
195 posix_spawnp() to return an error code; some other errors cause the
196 subprocess to exit with return code 127. It is implementation
197 dependent which error is reported which way. We treat both cases as
198 equivalent. */
199 sigset_t blocked_signals;
200 posix_spawn_file_actions_t actions;
201 bool actions_allocated;
202 posix_spawnattr_t attrs;
203 bool attrs_allocated;
204 int err;
205 pid_t child;
207 if (slave_process)
209 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
210 block_fatal_signals ();
212 actions_allocated = false;
213 attrs_allocated = false;
214 if ((err = posix_spawn_file_actions_init (&actions)) != 0
215 || (actions_allocated = true,
216 (null_stdin
217 && (err = posix_spawn_file_actions_addopen (&actions,
218 STDIN_FILENO,
219 "/dev/null", O_RDONLY,
221 != 0)
222 || (null_stdout
223 && (err = posix_spawn_file_actions_addopen (&actions,
224 STDOUT_FILENO,
225 "/dev/null", O_RDWR,
227 != 0)
228 || (null_stderr
229 && (err = posix_spawn_file_actions_addopen (&actions,
230 STDERR_FILENO,
231 "/dev/null", O_RDWR,
233 != 0)
234 || (slave_process
235 && ((err = posix_spawnattr_init (&attrs)) != 0
236 || (attrs_allocated = true,
237 (err = posix_spawnattr_setsigmask (&attrs,
238 &blocked_signals))
239 != 0
240 || (err = posix_spawnattr_setflags (&attrs,
241 POSIX_SPAWN_SETSIGMASK))
242 != 0)))
243 || (err = posix_spawnp (&child, prog_path, &actions,
244 attrs_allocated ? &attrs : NULL, prog_argv,
245 environ))
246 != 0))
248 if (actions_allocated)
249 posix_spawn_file_actions_destroy (&actions);
250 if (attrs_allocated)
251 posix_spawnattr_destroy (&attrs);
252 if (slave_process)
253 unblock_fatal_signals ();
254 if (termsigp != NULL)
255 *termsigp = 0;
256 if (exit_on_error || !null_stderr)
257 error (exit_on_error ? EXIT_FAILURE : 0, err,
258 _("%s subprocess failed"), progname);
259 return 127;
261 posix_spawn_file_actions_destroy (&actions);
262 if (attrs_allocated)
263 posix_spawnattr_destroy (&attrs);
264 if (slave_process)
266 register_slave_subprocess (child);
267 unblock_fatal_signals ();
270 return wait_subprocess (child, progname, ignore_sigpipe, null_stderr,
271 slave_process, exit_on_error, termsigp);
273 #endif