powerpc: fix sysconf support for cache geometries
[glibc.git] / sysdeps / posix / spawni.c
blob9cad25ca4ed9d2ba67144e9942069aa9c2815dd5
1 /* Guts of POSIX spawn interface. Generic POSIX.1 version.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <paths.h>
22 #include <spawn.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <sys/resource.h>
29 #include "spawn_int.h"
30 #include <not-cancel.h>
31 #include <local-setxid.h>
32 #include <shlib-compat.h>
35 /* The Unix standard contains a long explanation of the way to signal
36 an error after the fork() was successful. Since no new wait status
37 was wanted there is no way to signal an error using one of the
38 available methods. The committee chose to signal an error by a
39 normal program exit with the exit code 127. */
40 #define SPAWN_ERROR 127
43 /* The file is accessible but it is not an executable file. Invoke
44 the shell to interpret it as a script. */
45 static void
46 internal_function
47 script_execute (const char *file, char *const argv[], char *const envp[])
49 /* Count the arguments. */
50 int argc = 0;
51 while (argv[argc++])
54 /* Construct an argument list for the shell. */
56 char *new_argv[argc + 1];
57 new_argv[0] = (char *) _PATH_BSHELL;
58 new_argv[1] = (char *) file;
59 while (argc > 1)
61 new_argv[argc] = argv[argc - 1];
62 --argc;
65 /* Execute the shell. */
66 __execve (new_argv[0], new_argv, envp);
70 static inline void
71 maybe_script_execute (const char *file, char *const argv[], char *const envp[],
72 int xflags)
74 if (SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_15)
75 && (xflags & SPAWN_XFLAGS_TRY_SHELL)
76 && errno == ENOEXEC)
77 script_execute (file, argv, envp);
80 /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
81 Before running the process perform the actions described in FILE-ACTIONS. */
82 int
83 __spawni (pid_t *pid, const char *file,
84 const posix_spawn_file_actions_t *file_actions,
85 const posix_spawnattr_t *attrp, char *const argv[],
86 char *const envp[], int xflags)
88 pid_t new_pid;
89 char *path, *p, *name;
90 size_t len;
91 size_t pathlen;
93 /* Do this once. */
94 short int flags = attrp == NULL ? 0 : attrp->__flags;
96 /* Generate the new process. */
97 if ((flags & POSIX_SPAWN_USEVFORK) != 0
98 /* If no major work is done, allow using vfork. Note that we
99 might perform the path searching. But this would be done by
100 a call to execvp(), too, and such a call must be OK according
101 to POSIX. */
102 || ((flags & (POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF
103 | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER
104 | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_RESETIDS
105 | POSIX_SPAWN_SETSID)) == 0
106 && file_actions == NULL))
107 new_pid = __vfork ();
108 else
109 new_pid = __fork ();
111 if (new_pid != 0)
113 if (new_pid < 0)
114 return errno;
116 /* The call was successful. Store the PID if necessary. */
117 if (pid != NULL)
118 *pid = new_pid;
120 return 0;
123 /* Set signal mask. */
124 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0
125 && __sigprocmask (SIG_SETMASK, &attrp->__ss, NULL) != 0)
126 _exit (SPAWN_ERROR);
128 /* Set signal default action. */
129 if ((flags & POSIX_SPAWN_SETSIGDEF) != 0)
131 /* We have to iterate over all signals. This could possibly be
132 done better but it requires system specific solutions since
133 the sigset_t data type can be very different on different
134 architectures. */
135 int sig;
136 struct sigaction sa;
138 memset (&sa, '\0', sizeof (sa));
139 sa.sa_handler = SIG_DFL;
141 for (sig = 1; sig <= _NSIG; ++sig)
142 if (__sigismember (&attrp->__sd, sig) != 0
143 && __sigaction (sig, &sa, NULL) != 0)
144 _exit (SPAWN_ERROR);
148 #ifdef _POSIX_PRIORITY_SCHEDULING
149 /* Set the scheduling algorithm and parameters. */
150 if ((flags & (POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER))
151 == POSIX_SPAWN_SETSCHEDPARAM)
153 if (__sched_setparam (0, &attrp->__sp) == -1)
154 _exit (SPAWN_ERROR);
156 else if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0)
158 if (__sched_setscheduler (0, attrp->__policy, &attrp->__sp) == -1)
159 _exit (SPAWN_ERROR);
161 #endif
163 if ((flags & POSIX_SPAWN_SETSID) != 0
164 && __setsid () < 0)
165 _exit (SPAWN_ERROR);
167 /* Set the process group ID. */
168 if ((flags & POSIX_SPAWN_SETPGROUP) != 0
169 && __setpgid (0, attrp->__pgrp) != 0)
170 _exit (SPAWN_ERROR);
172 /* Set the effective user and group IDs. */
173 if ((flags & POSIX_SPAWN_RESETIDS) != 0
174 && (local_seteuid (__getuid ()) != 0
175 || local_setegid (__getgid ()) != 0))
176 _exit (SPAWN_ERROR);
178 /* Execute the file actions. */
179 if (file_actions != NULL)
181 int cnt;
182 struct rlimit64 fdlimit;
183 bool have_fdlimit = false;
185 for (cnt = 0; cnt < file_actions->__used; ++cnt)
187 struct __spawn_action *action = &file_actions->__actions[cnt];
189 switch (action->tag)
191 case spawn_do_close:
192 if (close_not_cancel (action->action.close_action.fd) != 0)
194 if (! have_fdlimit)
196 __getrlimit64 (RLIMIT_NOFILE, &fdlimit);
197 have_fdlimit = true;
200 /* Only signal errors for file descriptors out of range. */
201 if (action->action.close_action.fd < 0
202 || action->action.close_action.fd >= fdlimit.rlim_cur)
203 /* Signal the error. */
204 _exit (SPAWN_ERROR);
206 break;
208 case spawn_do_open:
210 int new_fd = open_not_cancel (action->action.open_action.path,
211 action->action.open_action.oflag
212 | O_LARGEFILE,
213 action->action.open_action.mode);
215 if (new_fd == -1)
216 /* The `open' call failed. */
217 _exit (SPAWN_ERROR);
219 /* Make sure the desired file descriptor is used. */
220 if (new_fd != action->action.open_action.fd)
222 if (__dup2 (new_fd, action->action.open_action.fd)
223 != action->action.open_action.fd)
224 /* The `dup2' call failed. */
225 _exit (SPAWN_ERROR);
227 if (close_not_cancel (new_fd) != 0)
228 /* The `close' call failed. */
229 _exit (SPAWN_ERROR);
232 break;
234 case spawn_do_dup2:
235 if (__dup2 (action->action.dup2_action.fd,
236 action->action.dup2_action.newfd)
237 != action->action.dup2_action.newfd)
238 /* The `dup2' call failed. */
239 _exit (SPAWN_ERROR);
240 break;
245 if ((xflags & SPAWN_XFLAGS_USE_PATH) == 0 || strchr (file, '/') != NULL)
247 /* The FILE parameter is actually a path. */
248 __execve (file, argv, envp);
250 maybe_script_execute (file, argv, envp, xflags);
252 /* Oh, oh. `execve' returns. This is bad. */
253 _exit (SPAWN_ERROR);
256 /* We have to search for FILE on the path. */
257 path = getenv ("PATH");
258 if (path == NULL)
260 /* There is no `PATH' in the environment.
261 The default search path is the current directory
262 followed by the path `confstr' returns for `_CS_PATH'. */
263 len = confstr (_CS_PATH, (char *) NULL, 0);
264 path = (char *) __alloca (1 + len);
265 path[0] = ':';
266 (void) confstr (_CS_PATH, path + 1, len);
269 len = strlen (file) + 1;
270 pathlen = strlen (path);
271 name = __alloca (pathlen + len + 1);
272 /* Copy the file name at the top. */
273 name = (char *) memcpy (name + pathlen + 1, file, len);
274 /* And add the slash. */
275 *--name = '/';
277 p = path;
280 char *startp;
282 path = p;
283 p = __strchrnul (path, ':');
285 if (p == path)
286 /* Two adjacent colons, or a colon at the beginning or the end
287 of `PATH' means to search the current directory. */
288 startp = name + 1;
289 else
290 startp = (char *) memcpy (name - (p - path), path, p - path);
292 /* Try to execute this name. If it works, execv will not return. */
293 __execve (startp, argv, envp);
295 maybe_script_execute (startp, argv, envp, xflags);
297 switch (errno)
299 case EACCES:
300 case ENOENT:
301 case ESTALE:
302 case ENOTDIR:
303 /* Those errors indicate the file is missing or not executable
304 by us, in which case we want to just try the next path
305 directory. */
306 break;
308 default:
309 /* Some other error means we found an executable file, but
310 something went wrong executing it; return the error to our
311 caller. */
312 _exit (SPAWN_ERROR);
315 while (*p++ != '\0');
317 /* Return with an error. */
318 _exit (SPAWN_ERROR);