execute, spawn-pipe: Make multithread-safe on native Windows.
[gnulib.git] / lib / execute.c
blob149cc894546ed0a87969cb61b7b384b0aca3734c
1 /* Creation of autonomous subprocesses.
2 Copyright (C) 2001-2004, 2006-2020 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 # if GNULIB_MSVC_NOTHROW
42 # include "msvc-nothrow.h"
43 # else
44 # include <io.h>
45 # endif
46 # include <process.h>
47 # include "windows-spawn.h"
49 #else
51 /* Unix API. */
52 # include <spawn.h>
54 #endif
57 #if defined EINTR && (defined _WIN32 && ! defined __CYGWIN__)
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. */
63 static int
64 nonintr_close (int fd)
66 int retval;
69 retval = close (fd);
70 while (retval < 0 && errno == EINTR);
72 return retval;
74 #undef close /* avoid warning related to gnulib module unistd */
75 #define close nonintr_close
77 static int
78 nonintr_open (const char *pathname, int oflag, mode_t mode)
80 int retval;
83 retval = open (pathname, oflag, mode);
84 while (retval < 0 && errno == EINTR);
86 return retval;
88 #undef open /* avoid warning on VMS */
89 #define open nonintr_open
91 #endif
94 int
95 execute (const char *progname,
96 const char *prog_path, char **prog_argv,
97 bool ignore_sigpipe,
98 bool null_stdin, bool null_stdout, bool null_stderr,
99 bool slave_process, bool exit_on_error,
100 int *termsigp)
102 #if defined _WIN32 && ! defined __CYGWIN__
104 /* Native Windows API. */
106 /* FIXME: Need to free memory allocated by prepare_spawn. */
107 prog_argv = prepare_spawn (prog_argv);
109 int exitcode = -1;
111 /* Create standard file handles of child process. */
112 int nullinfd = -1;
113 int nulloutfd = -1;
114 if ((!null_stdin
115 || (nullinfd = open ("NUL", O_RDONLY, 0)) >= 0)
116 && (!(null_stdout || null_stderr)
117 || (nulloutfd = open ("NUL", O_RDWR, 0)) >= 0))
118 /* Pass the environment explicitly. This is needed if the program has
119 modified the environment using putenv() or [un]setenv(). On Windows,
120 processes have two environments, one in the "environment block" of the
121 process and managed through SetEnvironmentVariable(), and one inside the
122 process, in the location retrieved by the 'environ' macro. If we were
123 to pass NULL, the child process would inherit a copy of the environment
124 block - ignoring the effects of putenv() and [un]setenv(). */
126 HANDLE stdin_handle =
127 (HANDLE) _get_osfhandle (null_stdin ? nullinfd : STDIN_FILENO);
128 HANDLE stdout_handle =
129 (HANDLE) _get_osfhandle (null_stdout ? nulloutfd : STDOUT_FILENO);
130 HANDLE stderr_handle =
131 (HANDLE) _get_osfhandle (null_stderr ? nulloutfd : STDERR_FILENO);
133 exitcode = spawnpvech (P_WAIT, prog_path, (const char **) prog_argv,
134 (const char **) environ, NULL,
135 stdin_handle, stdout_handle, stderr_handle);
136 if (exitcode == -1 && errno == ENOEXEC)
138 /* prog is not a native executable. Try to execute it as a
139 shell script. Note that prepare_spawn() has already prepended
140 a hidden element "sh.exe" to prog_argv. */
141 --prog_argv;
142 exitcode = spawnpvech (P_WAIT, prog_argv[0], (const char **) prog_argv,
143 (const char **) environ, NULL,
144 stdin_handle, stdout_handle, stderr_handle);
147 if (nulloutfd >= 0)
148 close (nulloutfd);
149 if (nullinfd >= 0)
150 close (nullinfd);
152 if (termsigp != NULL)
153 *termsigp = 0;
155 if (exitcode == -1)
157 if (exit_on_error || !null_stderr)
158 error (exit_on_error ? EXIT_FAILURE : 0, errno,
159 _("%s subprocess failed"), progname);
160 return 127;
163 return exitcode;
165 #else
167 /* Unix API. */
168 /* Note about 127: Some errors during posix_spawnp() cause the function
169 posix_spawnp() to return an error code; some other errors cause the
170 subprocess to exit with return code 127. It is implementation
171 dependent which error is reported which way. We treat both cases as
172 equivalent. */
173 sigset_t blocked_signals;
174 posix_spawn_file_actions_t actions;
175 bool actions_allocated;
176 posix_spawnattr_t attrs;
177 bool attrs_allocated;
178 int err;
179 pid_t child;
181 if (slave_process)
183 sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
184 block_fatal_signals ();
186 actions_allocated = false;
187 attrs_allocated = false;
188 if ((err = posix_spawn_file_actions_init (&actions)) != 0
189 || (actions_allocated = true,
190 (null_stdin
191 && (err = posix_spawn_file_actions_addopen (&actions,
192 STDIN_FILENO,
193 "/dev/null", O_RDONLY,
195 != 0)
196 || (null_stdout
197 && (err = posix_spawn_file_actions_addopen (&actions,
198 STDOUT_FILENO,
199 "/dev/null", O_RDWR,
201 != 0)
202 || (null_stderr
203 && (err = posix_spawn_file_actions_addopen (&actions,
204 STDERR_FILENO,
205 "/dev/null", O_RDWR,
207 != 0)
208 || (slave_process
209 && ((err = posix_spawnattr_init (&attrs)) != 0
210 || (attrs_allocated = true,
211 (err = posix_spawnattr_setsigmask (&attrs,
212 &blocked_signals))
213 != 0
214 || (err = posix_spawnattr_setflags (&attrs,
215 POSIX_SPAWN_SETSIGMASK))
216 != 0)))
217 || (err = posix_spawnp (&child, prog_path, &actions,
218 attrs_allocated ? &attrs : NULL, prog_argv,
219 environ))
220 != 0))
222 if (actions_allocated)
223 posix_spawn_file_actions_destroy (&actions);
224 if (attrs_allocated)
225 posix_spawnattr_destroy (&attrs);
226 if (slave_process)
227 unblock_fatal_signals ();
228 if (termsigp != NULL)
229 *termsigp = 0;
230 if (exit_on_error || !null_stderr)
231 error (exit_on_error ? EXIT_FAILURE : 0, err,
232 _("%s subprocess failed"), progname);
233 return 127;
235 posix_spawn_file_actions_destroy (&actions);
236 if (attrs_allocated)
237 posix_spawnattr_destroy (&attrs);
238 if (slave_process)
240 register_slave_subprocess (child);
241 unblock_fatal_signals ();
244 return wait_subprocess (child, progname, ignore_sigpipe, null_stderr,
245 slave_process, exit_on_error, termsigp);
247 #endif