posix_spawn[p]: Fix compilation error on Windows (regr. 2020-12-14).
[gnulib.git] / lib / spawni.c
blob06d98b473d721ec6f403c0c2d60896acb4126d91
1 /* Guts of POSIX spawn interface. Generic POSIX.1 version.
2 Copyright (C) 2000-2006, 2008-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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/>. */
18 #include <config.h>
20 /* Specification. */
21 #include <spawn.h>
22 #include "spawn_int.h"
24 #include <alloca.h>
25 #include <errno.h>
27 #include <fcntl.h>
28 #ifndef O_LARGEFILE
29 # define O_LARGEFILE 0
30 #endif
32 #if _LIBC || HAVE_PATHS_H
33 # include <paths.h>
34 #else
35 # define _PATH_BSHELL BOURNE_SHELL
36 #endif
38 #include <signal.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #if _LIBC
44 # include <not-cancel.h>
45 #else
46 # define close_not_cancel close
47 # define open_not_cancel open
48 #endif
50 #if _LIBC
51 # include <local-setxid.h>
52 #else
53 # if !HAVE_SETEUID
54 # define seteuid(id) setresuid (-1, id, -1)
55 # endif
56 # if !HAVE_SETEGID
57 # define setegid(id) setresgid (-1, id, -1)
58 # endif
59 # define local_seteuid(id) seteuid (id)
60 # define local_setegid(id) setegid (id)
61 #endif
63 #if _LIBC
64 # define alloca __alloca
65 # define execve __execve
66 # define dup2 __dup2
67 # define fork __fork
68 # define getgid __getgid
69 # define getuid __getuid
70 # define sched_setparam __sched_setparam
71 # define sched_setscheduler __sched_setscheduler
72 # define setpgid __setpgid
73 # define sigaction __sigaction
74 # define sigismember __sigismember
75 # define sigprocmask __sigprocmask
76 # define strchrnul __strchrnul
77 # define vfork __vfork
78 #else
79 # undef internal_function
80 # define internal_function /* empty */
81 #endif
84 /* The Unix standard contains a long explanation of the way to signal
85 an error after the fork() was successful. Since no new wait status
86 was wanted there is no way to signal an error using one of the
87 available methods. The committee chose to signal an error by a
88 normal program exit with the exit code 127. */
89 #define SPAWN_ERROR 127
92 #if defined _WIN32 && ! defined __CYGWIN__
94 /* Native Windows API. */
95 int
96 __spawni (pid_t *pid, const char *file,
97 const posix_spawn_file_actions_t *file_actions,
98 const posix_spawnattr_t *attrp, const char *const argv[],
99 const char *const envp[], int use_path)
101 /* Not yet implemented. */
102 return ENOSYS;
105 #else
108 /* The file is accessible but it is not an executable file. Invoke
109 the shell to interpret it as a script. */
110 static void
111 internal_function
112 script_execute (const char *file, const char *const argv[],
113 const char *const envp[])
115 /* Count the arguments. */
116 int argc = 0;
117 while (argv[argc++])
120 /* Construct an argument list for the shell. */
122 const char **new_argv =
123 (const char **) alloca ((argc + 1) * sizeof (const char *));
124 new_argv[0] = _PATH_BSHELL;
125 new_argv[1] = file;
126 while (argc > 1)
128 new_argv[argc] = argv[argc - 1];
129 --argc;
132 /* Execute the shell. */
133 execve (new_argv[0], (char * const *) new_argv, (char * const *) envp);
138 /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
139 Before running the process perform the actions described in FILE-ACTIONS. */
141 __spawni (pid_t *pid, const char *file,
142 const posix_spawn_file_actions_t *file_actions,
143 const posix_spawnattr_t *attrp, const char *const argv[],
144 const char *const envp[], int use_path)
146 pid_t new_pid;
147 char *path, *p, *name;
148 size_t len;
149 size_t pathlen;
151 /* Do this once. */
152 short int flags = attrp == NULL ? 0 : attrp->_flags;
154 /* Avoid gcc warning
155 "variable 'flags' might be clobbered by 'longjmp' or 'vfork'" */
156 (void) &flags;
158 /* Generate the new process. */
159 #if HAVE_VFORK
160 if ((flags & POSIX_SPAWN_USEVFORK) != 0
161 /* If no major work is done, allow using vfork. Note that we
162 might perform the path searching. But this would be done by
163 a call to execvp(), too, and such a call must be OK according
164 to POSIX. */
165 || ((flags & (POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF
166 | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER
167 | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_RESETIDS)) == 0
168 && file_actions == NULL))
169 new_pid = vfork ();
170 else
171 #endif
172 new_pid = fork ();
174 if (new_pid != 0)
176 if (new_pid < 0)
177 return errno;
179 /* The call was successful. Store the PID if necessary. */
180 if (pid != NULL)
181 *pid = new_pid;
183 return 0;
186 /* Set signal mask. */
187 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0
188 && sigprocmask (SIG_SETMASK, &attrp->_ss, NULL) != 0)
189 _exit (SPAWN_ERROR);
191 /* Set signal default action. */
192 if ((flags & POSIX_SPAWN_SETSIGDEF) != 0)
194 /* We have to iterate over all signals. This could possibly be
195 done better but it requires system specific solutions since
196 the sigset_t data type can be very different on different
197 architectures. */
198 int sig;
199 struct sigaction sa;
201 memset (&sa, '\0', sizeof (sa));
202 sa.sa_handler = SIG_DFL;
204 for (sig = 1; sig <= NSIG; ++sig)
205 if (sigismember (&attrp->_sd, sig) != 0
206 && sigaction (sig, &sa, NULL) != 0)
207 _exit (SPAWN_ERROR);
211 #if (_LIBC ? defined _POSIX_PRIORITY_SCHEDULING : HAVE_SCHED_SETPARAM && HAVE_SCHED_SETSCHEDULER)
212 /* Set the scheduling algorithm and parameters. */
213 if ((flags & (POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER))
214 == POSIX_SPAWN_SETSCHEDPARAM)
216 if (sched_setparam (0, &attrp->_sp) == -1)
217 _exit (SPAWN_ERROR);
219 else if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0)
221 if (sched_setscheduler (0, attrp->_policy,
222 (flags & POSIX_SPAWN_SETSCHEDPARAM) != 0
223 ? &attrp->_sp : NULL) == -1)
224 _exit (SPAWN_ERROR);
226 #endif
228 /* Set the process group ID. */
229 if ((flags & POSIX_SPAWN_SETPGROUP) != 0
230 && setpgid (0, attrp->_pgrp) != 0)
231 _exit (SPAWN_ERROR);
233 /* Set the effective user and group IDs. */
234 if ((flags & POSIX_SPAWN_RESETIDS) != 0
235 && (local_seteuid (getuid ()) != 0
236 || local_setegid (getgid ()) != 0))
237 _exit (SPAWN_ERROR);
239 /* Execute the file actions. */
240 if (file_actions != NULL)
242 int cnt;
244 for (cnt = 0; cnt < file_actions->_used; ++cnt)
246 struct __spawn_action *action = &file_actions->_actions[cnt];
248 switch (action->tag)
250 case spawn_do_close:
251 if (close_not_cancel (action->action.close_action.fd) != 0)
252 /* Signal the error. */
253 _exit (SPAWN_ERROR);
254 break;
256 case spawn_do_open:
258 int new_fd = open_not_cancel (action->action.open_action.path,
259 action->action.open_action.oflag
260 | O_LARGEFILE,
261 action->action.open_action.mode);
263 if (new_fd == -1)
264 /* The 'open' call failed. */
265 _exit (SPAWN_ERROR);
267 /* Make sure the desired file descriptor is used. */
268 if (new_fd != action->action.open_action.fd)
270 if (dup2 (new_fd, action->action.open_action.fd)
271 != action->action.open_action.fd)
272 /* The 'dup2' call failed. */
273 _exit (SPAWN_ERROR);
275 if (close_not_cancel (new_fd) != 0)
276 /* The 'close' call failed. */
277 _exit (SPAWN_ERROR);
280 break;
282 case spawn_do_dup2:
283 if (dup2 (action->action.dup2_action.fd,
284 action->action.dup2_action.newfd)
285 != action->action.dup2_action.newfd)
286 /* The 'dup2' call failed. */
287 _exit (SPAWN_ERROR);
288 break;
290 case spawn_do_chdir:
291 if (chdir (action->action.chdir_action.path) < 0)
292 /* The 'chdir' call failed. */
293 _exit (SPAWN_ERROR);
294 break;
296 case spawn_do_fchdir:
297 if (fchdir (action->action.fchdir_action.fd) < 0)
298 /* The 'fchdir' call failed. */
299 _exit (SPAWN_ERROR);
300 break;
305 if (! use_path || strchr (file, '/') != NULL)
307 /* The FILE parameter is actually a path. */
308 execve (file, (char * const *) argv, (char * const *) envp);
310 if (errno == ENOEXEC)
311 script_execute (file, argv, envp);
313 /* Oh, oh. 'execve' returns. This is bad. */
314 _exit (SPAWN_ERROR);
317 /* We have to search for FILE on the path. */
318 path = getenv ("PATH");
319 if (path == NULL)
321 #if HAVE_CONFSTR
322 /* There is no 'PATH' in the environment.
323 The default search path is the current directory
324 followed by the path 'confstr' returns for '_CS_PATH'. */
325 len = confstr (_CS_PATH, (char *) NULL, 0);
326 path = (char *) alloca (1 + len);
327 path[0] = ':';
328 (void) confstr (_CS_PATH, path + 1, len);
329 #else
330 /* Pretend that the PATH contains only the current directory. */
331 path = "";
332 #endif
335 len = strlen (file) + 1;
336 pathlen = strlen (path);
337 name = alloca (pathlen + len + 1);
338 /* Copy the file name at the top. */
339 name = (char *) memcpy (name + pathlen + 1, file, len);
340 /* And add the slash. */
341 *--name = '/';
343 p = path;
346 char *startp;
348 path = p;
349 p = strchrnul (path, ':');
351 if (p == path)
352 /* Two adjacent colons, or a colon at the beginning or the end
353 of 'PATH' means to search the current directory. */
354 startp = name + 1;
355 else
356 startp = (char *) memcpy (name - (p - path), path, p - path);
358 /* Try to execute this name. If it works, execv will not return. */
359 execve (startp, (char * const *) argv, (char * const *) envp);
361 if (errno == ENOEXEC)
362 script_execute (startp, argv, envp);
364 switch (errno)
366 case EACCES:
367 case ENOENT:
368 case ESTALE:
369 case ENOTDIR:
370 /* Those errors indicate the file is missing or not executable
371 by us, in which case we want to just try the next path
372 directory. */
373 break;
375 default:
376 /* Some other error means we found an executable file, but
377 something went wrong executing it; return the error to our
378 caller. */
379 _exit (SPAWN_ERROR);
382 while (*p++ != '\0');
384 /* Return with an error. */
385 _exit (SPAWN_ERROR);
388 #endif