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