]>
Commit | Line | Data |
---|---|---|
986ad61e | 1 | /* Guts of POSIX spawn interface. Generic POSIX.1 version. |
bfff8b1b | 2 | Copyright (C) 2000-2017 Free Software Foundation, Inc. |
a5a6f926 UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
a5a6f926 UD |
9 | |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
a5a6f926 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
a5a6f926 UD |
18 | |
19 | #include <errno.h> | |
20 | #include <fcntl.h> | |
21 | #include <paths.h> | |
22 | #include <spawn.h> | |
bea9b193 | 23 | #include <stdbool.h> |
a5a6f926 UD |
24 | #include <stdlib.h> |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
12e5c3b9 | 27 | #include <signal.h> |
cfa28e56 | 28 | #include <sys/resource.h> |
a5a6f926 | 29 | #include "spawn_int.h" |
73299943 | 30 | #include <not-cancel.h> |
1b8373f4 | 31 | #include <local-setxid.h> |
d96de963 | 32 | #include <shlib-compat.h> |
a5a6f926 UD |
33 | |
34 | ||
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 | |
41 | ||
42 | ||
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[]) | |
48 | { | |
49 | /* Count the arguments. */ | |
50 | int argc = 0; | |
51 | while (argv[argc++]) | |
52 | ; | |
53 | ||
54 | /* Construct an argument list for the shell. */ | |
55 | { | |
56 | char *new_argv[argc + 1]; | |
57 | new_argv[0] = (char *) _PATH_BSHELL; | |
58 | new_argv[1] = (char *) file; | |
59 | while (argc > 1) | |
60 | { | |
61 | new_argv[argc] = argv[argc - 1]; | |
62 | --argc; | |
63 | } | |
64 | ||
65 | /* Execute the shell. */ | |
66 | __execve (new_argv[0], new_argv, envp); | |
67 | } | |
68 | } | |
69 | ||
ecb1482f RM |
70 | static inline void |
71 | maybe_script_execute (const char *file, char *const argv[], char *const envp[], | |
72 | int xflags) | |
73 | { | |
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); | |
78 | } | |
a5a6f926 UD |
79 | |
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[], | |
d96de963 | 86 | char *const envp[], int xflags) |
a5a6f926 UD |
87 | { |
88 | pid_t new_pid; | |
89 | char *path, *p, *name; | |
90 | size_t len; | |
91 | size_t pathlen; | |
9ad68422 UD |
92 | |
93 | /* Do this once. */ | |
94 | short int flags = attrp == NULL ? 0 : attrp->__flags; | |
a5a6f926 UD |
95 | |
96 | /* Generate the new process. */ | |
a9f43ef4 UD |
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)) == 0 | |
105 | && file_actions == NULL)) | |
9ad68422 UD |
106 | new_pid = __vfork (); |
107 | else | |
108 | new_pid = __fork (); | |
109 | ||
a5a6f926 UD |
110 | if (new_pid != 0) |
111 | { | |
112 | if (new_pid < 0) | |
113 | return errno; | |
114 | ||
115 | /* The call was successful. Store the PID if necessary. */ | |
116 | if (pid != NULL) | |
117 | *pid = new_pid; | |
118 | ||
119 | return 0; | |
120 | } | |
121 | ||
a5a6f926 UD |
122 | /* Set signal mask. */ |
123 | if ((flags & POSIX_SPAWN_SETSIGMASK) != 0 | |
124 | && __sigprocmask (SIG_SETMASK, &attrp->__ss, NULL) != 0) | |
125 | _exit (SPAWN_ERROR); | |
126 | ||
127 | /* Set signal default action. */ | |
128 | if ((flags & POSIX_SPAWN_SETSIGDEF) != 0) | |
129 | { | |
130 | /* We have to iterate over all signals. This could possibly be | |
131 | done better but it requires system specific solutions since | |
132 | the sigset_t data type can be very different on different | |
133 | architectures. */ | |
134 | int sig; | |
135 | struct sigaction sa; | |
136 | ||
137 | memset (&sa, '\0', sizeof (sa)); | |
138 | sa.sa_handler = SIG_DFL; | |
139 | ||
ba3752d5 | 140 | for (sig = 1; sig <= _NSIG; ++sig) |
284128f6 | 141 | if (__sigismember (&attrp->__sd, sig) != 0 |
a5a6f926 UD |
142 | && __sigaction (sig, &sa, NULL) != 0) |
143 | _exit (SPAWN_ERROR); | |
144 | ||
145 | } | |
146 | ||
147 | #ifdef _POSIX_PRIORITY_SCHEDULING | |
148 | /* Set the scheduling algorithm and parameters. */ | |
149 | if ((flags & (POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER)) | |
150 | == POSIX_SPAWN_SETSCHEDPARAM) | |
151 | { | |
152 | if (__sched_setparam (0, &attrp->__sp) == -1) | |
153 | _exit (SPAWN_ERROR); | |
154 | } | |
155 | else if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0) | |
156 | { | |
f574184a | 157 | if (__sched_setscheduler (0, attrp->__policy, &attrp->__sp) == -1) |
a5a6f926 UD |
158 | _exit (SPAWN_ERROR); |
159 | } | |
160 | #endif | |
161 | ||
162 | /* Set the process group ID. */ | |
163 | if ((flags & POSIX_SPAWN_SETPGROUP) != 0 | |
164 | && __setpgid (0, attrp->__pgrp) != 0) | |
165 | _exit (SPAWN_ERROR); | |
166 | ||
167 | /* Set the effective user and group IDs. */ | |
168 | if ((flags & POSIX_SPAWN_RESETIDS) != 0 | |
1b8373f4 UD |
169 | && (local_seteuid (__getuid ()) != 0 |
170 | || local_setegid (__getgid ()) != 0)) | |
a5a6f926 UD |
171 | _exit (SPAWN_ERROR); |
172 | ||
173 | /* Execute the file actions. */ | |
174 | if (file_actions != NULL) | |
175 | { | |
176 | int cnt; | |
cfa28e56 UD |
177 | struct rlimit64 fdlimit; |
178 | bool have_fdlimit = false; | |
a5a6f926 UD |
179 | |
180 | for (cnt = 0; cnt < file_actions->__used; ++cnt) | |
181 | { | |
182 | struct __spawn_action *action = &file_actions->__actions[cnt]; | |
183 | ||
184 | switch (action->tag) | |
185 | { | |
186 | case spawn_do_close: | |
73299943 | 187 | if (close_not_cancel (action->action.close_action.fd) != 0) |
cfa28e56 UD |
188 | { |
189 | if (! have_fdlimit) | |
190 | { | |
1a2325c0 | 191 | __getrlimit64 (RLIMIT_NOFILE, &fdlimit); |
cfa28e56 UD |
192 | have_fdlimit = true; |
193 | } | |
194 | ||
195 | /* Only signal errors for file descriptors out of range. */ | |
196 | if (action->action.close_action.fd < 0 | |
197 | || action->action.close_action.fd >= fdlimit.rlim_cur) | |
198 | /* Signal the error. */ | |
199 | _exit (SPAWN_ERROR); | |
200 | } | |
a5a6f926 UD |
201 | break; |
202 | ||
203 | case spawn_do_open: | |
204 | { | |
7231452e UD |
205 | int new_fd = open_not_cancel (action->action.open_action.path, |
206 | action->action.open_action.oflag | |
207 | | O_LARGEFILE, | |
208 | action->action.open_action.mode); | |
a5a6f926 UD |
209 | |
210 | if (new_fd == -1) | |
211 | /* The `open' call failed. */ | |
212 | _exit (SPAWN_ERROR); | |
213 | ||
214 | /* Make sure the desired file descriptor is used. */ | |
215 | if (new_fd != action->action.open_action.fd) | |
216 | { | |
08c7f6b0 UD |
217 | if (__dup2 (new_fd, action->action.open_action.fd) |
218 | != action->action.open_action.fd) | |
a5a6f926 UD |
219 | /* The `dup2' call failed. */ |
220 | _exit (SPAWN_ERROR); | |
221 | ||
7231452e | 222 | if (close_not_cancel (new_fd) != 0) |
a5a6f926 UD |
223 | /* The `close' call failed. */ |
224 | _exit (SPAWN_ERROR); | |
225 | } | |
226 | } | |
227 | break; | |
228 | ||
229 | case spawn_do_dup2: | |
230 | if (__dup2 (action->action.dup2_action.fd, | |
08c7f6b0 UD |
231 | action->action.dup2_action.newfd) |
232 | != action->action.dup2_action.newfd) | |
a5a6f926 UD |
233 | /* The `dup2' call failed. */ |
234 | _exit (SPAWN_ERROR); | |
235 | break; | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
d96de963 | 240 | if ((xflags & SPAWN_XFLAGS_USE_PATH) == 0 || strchr (file, '/') != NULL) |
a5a6f926 UD |
241 | { |
242 | /* The FILE parameter is actually a path. */ | |
243 | __execve (file, argv, envp); | |
244 | ||
ecb1482f | 245 | maybe_script_execute (file, argv, envp, xflags); |
a5a6f926 UD |
246 | |
247 | /* Oh, oh. `execve' returns. This is bad. */ | |
248 | _exit (SPAWN_ERROR); | |
249 | } | |
250 | ||
251 | /* We have to search for FILE on the path. */ | |
252 | path = getenv ("PATH"); | |
253 | if (path == NULL) | |
254 | { | |
255 | /* There is no `PATH' in the environment. | |
256 | The default search path is the current directory | |
257 | followed by the path `confstr' returns for `_CS_PATH'. */ | |
258 | len = confstr (_CS_PATH, (char *) NULL, 0); | |
259 | path = (char *) __alloca (1 + len); | |
260 | path[0] = ':'; | |
261 | (void) confstr (_CS_PATH, path + 1, len); | |
262 | } | |
263 | ||
264 | len = strlen (file) + 1; | |
265 | pathlen = strlen (path); | |
266 | name = __alloca (pathlen + len + 1); | |
267 | /* Copy the file name at the top. */ | |
268 | name = (char *) memcpy (name + pathlen + 1, file, len); | |
269 | /* And add the slash. */ | |
270 | *--name = '/'; | |
271 | ||
272 | p = path; | |
273 | do | |
274 | { | |
275 | char *startp; | |
276 | ||
277 | path = p; | |
278 | p = __strchrnul (path, ':'); | |
279 | ||
280 | if (p == path) | |
281 | /* Two adjacent colons, or a colon at the beginning or the end | |
282 | of `PATH' means to search the current directory. */ | |
283 | startp = name + 1; | |
284 | else | |
285 | startp = (char *) memcpy (name - (p - path), path, p - path); | |
286 | ||
287 | /* Try to execute this name. If it works, execv will not return. */ | |
288 | __execve (startp, argv, envp); | |
289 | ||
ecb1482f | 290 | maybe_script_execute (startp, argv, envp, xflags); |
a5a6f926 UD |
291 | |
292 | switch (errno) | |
293 | { | |
294 | case EACCES: | |
295 | case ENOENT: | |
296 | case ESTALE: | |
297 | case ENOTDIR: | |
298 | /* Those errors indicate the file is missing or not executable | |
299 | by us, in which case we want to just try the next path | |
300 | directory. */ | |
301 | break; | |
302 | ||
303 | default: | |
304 | /* Some other error means we found an executable file, but | |
305 | something went wrong executing it; return the error to our | |
306 | caller. */ | |
307 | _exit (SPAWN_ERROR); | |
308 | } | |
309 | } | |
310 | while (*p++ != '\0'); | |
311 | ||
312 | /* Return with an error. */ | |
313 | _exit (SPAWN_ERROR); | |
314 | } |