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/>. */
32 #include "fatal-signal.h"
33 #include "wait-process.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"
47 # include "windows-spawn.h"
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. */
64 nonintr_close (int fd
)
70 while (retval
< 0 && errno
== EINTR
);
74 #undef close /* avoid warning related to gnulib module unistd */
75 #define close nonintr_close
78 nonintr_open (const char *pathname
, int oflag
, mode_t mode
)
83 retval
= open (pathname
, oflag
, mode
);
84 while (retval
< 0 && errno
== EINTR
);
88 #undef open /* avoid warning on VMS */
89 #define open nonintr_open
95 execute (const char *progname
,
96 const char *prog_path
, char **prog_argv
,
98 bool null_stdin
, bool null_stdout
, bool null_stderr
,
99 bool slave_process
, bool exit_on_error
,
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
);
111 /* Create standard file handles of child process. */
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. */
142 exitcode
= spawnpvech (P_WAIT
, prog_argv
[0], (const char **) prog_argv
,
143 (const char **) environ
, NULL
,
144 stdin_handle
, stdout_handle
, stderr_handle
);
152 if (termsigp
!= NULL
)
157 if (exit_on_error
|| !null_stderr
)
158 error (exit_on_error
? EXIT_FAILURE
: 0, errno
,
159 _("%s subprocess failed"), progname
);
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
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
;
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,
191 && (err
= posix_spawn_file_actions_addopen (&actions
,
193 "/dev/null", O_RDONLY
,
197 && (err
= posix_spawn_file_actions_addopen (&actions
,
203 && (err
= posix_spawn_file_actions_addopen (&actions
,
209 && ((err
= posix_spawnattr_init (&attrs
)) != 0
210 || (attrs_allocated
= true,
211 (err
= posix_spawnattr_setsigmask (&attrs
,
214 || (err
= posix_spawnattr_setflags (&attrs
,
215 POSIX_SPAWN_SETSIGMASK
))
217 || (err
= posix_spawnp (&child
, prog_path
, &actions
,
218 attrs_allocated
? &attrs
: NULL
, prog_argv
,
222 if (actions_allocated
)
223 posix_spawn_file_actions_destroy (&actions
);
225 posix_spawnattr_destroy (&attrs
);
227 unblock_fatal_signals ();
228 if (termsigp
!= NULL
)
230 if (exit_on_error
|| !null_stderr
)
231 error (exit_on_error
? EXIT_FAILURE
: 0, err
,
232 _("%s subprocess failed"), progname
);
235 posix_spawn_file_actions_destroy (&actions
);
237 posix_spawnattr_destroy (&attrs
);
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
);