Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / posix / spawni.c
blob11a74b53b3659091feda7b91d70f83704c3794a1
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.
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 <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/resource.h>
27 #include "spawn_int.h"
28 #include <not-cancel.h>
29 #include <local-setxid.h>
30 #include <shlib-compat.h>
33 /* The Unix standard contains a long explanation of the way to signal
34 an error after the fork() was successful. Since no new wait status
35 was wanted there is no way to signal an error using one of the
36 available methods. The committee chose to signal an error by a
37 normal program exit with the exit code 127. */
38 #define SPAWN_ERROR 127
41 /* The file is accessible but it is not an executable file. Invoke
42 the shell to interpret it as a script. */
43 static void
44 internal_function
45 script_execute (const char *file, char *const argv[], char *const envp[])
47 /* Count the arguments. */
48 int argc = 0;
49 while (argv[argc++])
52 /* Construct an argument list for the shell. */
54 char *new_argv[argc + 1];
55 new_argv[0] = (char *) _PATH_BSHELL;
56 new_argv[1] = (char *) file;
57 while (argc > 1)
59 new_argv[argc] = argv[argc - 1];
60 --argc;
63 /* Execute the shell. */
64 __execve (new_argv[0], new_argv, envp);
68 static inline void
69 maybe_script_execute (const char *file, char *const argv[], char *const envp[],
70 int xflags)
72 if (SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_15)
73 && (xflags & SPAWN_XFLAGS_TRY_SHELL)
74 && errno == ENOEXEC)
75 script_execute (file, argv, envp);
78 /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
79 Before running the process perform the actions described in FILE-ACTIONS. */
80 int
81 __spawni (pid_t *pid, const char *file,
82 const posix_spawn_file_actions_t *file_actions,
83 const posix_spawnattr_t *attrp, char *const argv[],
84 char *const envp[], int xflags)
86 pid_t new_pid;
87 char *path, *p, *name;
88 size_t len;
89 size_t pathlen;
91 /* Do this once. */
92 short int flags = attrp == NULL ? 0 : attrp->__flags;
94 /* Generate the new process. */
95 if ((flags & POSIX_SPAWN_USEVFORK) != 0
96 /* If no major work is done, allow using vfork. Note that we
97 might perform the path searching. But this would be done by
98 a call to execvp(), too, and such a call must be OK according
99 to POSIX. */
100 || ((flags & (POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF
101 | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER
102 | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_RESETIDS)) == 0
103 && file_actions == NULL))
104 new_pid = __vfork ();
105 else
106 new_pid = __fork ();
108 if (new_pid != 0)
110 if (new_pid < 0)
111 return errno;
113 /* The call was successful. Store the PID if necessary. */
114 if (pid != NULL)
115 *pid = new_pid;
117 return 0;
120 /* Set signal mask. */
121 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0
122 && __sigprocmask (SIG_SETMASK, &attrp->__ss, NULL) != 0)
123 _exit (SPAWN_ERROR);
125 /* Set signal default action. */
126 if ((flags & POSIX_SPAWN_SETSIGDEF) != 0)
128 /* We have to iterate over all signals. This could possibly be
129 done better but it requires system specific solutions since
130 the sigset_t data type can be very different on different
131 architectures. */
132 int sig;
133 struct sigaction sa;
135 memset (&sa, '\0', sizeof (sa));
136 sa.sa_handler = SIG_DFL;
138 for (sig = 1; sig <= _NSIG; ++sig)
139 if (__sigismember (&attrp->__sd, sig) != 0
140 && __sigaction (sig, &sa, NULL) != 0)
141 _exit (SPAWN_ERROR);
145 #ifdef _POSIX_PRIORITY_SCHEDULING
146 /* Set the scheduling algorithm and parameters. */
147 if ((flags & (POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER))
148 == POSIX_SPAWN_SETSCHEDPARAM)
150 if (__sched_setparam (0, &attrp->__sp) == -1)
151 _exit (SPAWN_ERROR);
153 else if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0)
155 if (__sched_setscheduler (0, attrp->__policy, &attrp->__sp) == -1)
156 _exit (SPAWN_ERROR);
158 #endif
160 /* Set the process group ID. */
161 if ((flags & POSIX_SPAWN_SETPGROUP) != 0
162 && __setpgid (0, attrp->__pgrp) != 0)
163 _exit (SPAWN_ERROR);
165 /* Set the effective user and group IDs. */
166 if ((flags & POSIX_SPAWN_RESETIDS) != 0
167 && (local_seteuid (__getuid ()) != 0
168 || local_setegid (__getgid ()) != 0))
169 _exit (SPAWN_ERROR);
171 /* Execute the file actions. */
172 if (file_actions != NULL)
174 int cnt;
175 struct rlimit64 fdlimit;
176 bool have_fdlimit = false;
178 for (cnt = 0; cnt < file_actions->__used; ++cnt)
180 struct __spawn_action *action = &file_actions->__actions[cnt];
182 switch (action->tag)
184 case spawn_do_close:
185 if (close_not_cancel (action->action.close_action.fd) != 0)
187 if (! have_fdlimit)
189 getrlimit64 (RLIMIT_NOFILE, &fdlimit);
190 have_fdlimit = true;
193 /* Only signal errors for file descriptors out of range. */
194 if (action->action.close_action.fd < 0
195 || action->action.close_action.fd >= fdlimit.rlim_cur)
196 /* Signal the error. */
197 _exit (SPAWN_ERROR);
199 break;
201 case spawn_do_open:
203 int new_fd = open_not_cancel (action->action.open_action.path,
204 action->action.open_action.oflag
205 | O_LARGEFILE,
206 action->action.open_action.mode);
208 if (new_fd == -1)
209 /* The `open' call failed. */
210 _exit (SPAWN_ERROR);
212 /* Make sure the desired file descriptor is used. */
213 if (new_fd != action->action.open_action.fd)
215 if (__dup2 (new_fd, action->action.open_action.fd)
216 != action->action.open_action.fd)
217 /* The `dup2' call failed. */
218 _exit (SPAWN_ERROR);
220 if (close_not_cancel (new_fd) != 0)
221 /* The `close' call failed. */
222 _exit (SPAWN_ERROR);
225 break;
227 case spawn_do_dup2:
228 if (__dup2 (action->action.dup2_action.fd,
229 action->action.dup2_action.newfd)
230 != action->action.dup2_action.newfd)
231 /* The `dup2' call failed. */
232 _exit (SPAWN_ERROR);
233 break;
238 if ((xflags & SPAWN_XFLAGS_USE_PATH) == 0 || strchr (file, '/') != NULL)
240 /* The FILE parameter is actually a path. */
241 __execve (file, argv, envp);
243 maybe_script_execute (file, argv, envp, xflags);
245 /* Oh, oh. `execve' returns. This is bad. */
246 _exit (SPAWN_ERROR);
249 /* We have to search for FILE on the path. */
250 path = getenv ("PATH");
251 if (path == NULL)
253 /* There is no `PATH' in the environment.
254 The default search path is the current directory
255 followed by the path `confstr' returns for `_CS_PATH'. */
256 len = confstr (_CS_PATH, (char *) NULL, 0);
257 path = (char *) __alloca (1 + len);
258 path[0] = ':';
259 (void) confstr (_CS_PATH, path + 1, len);
262 len = strlen (file) + 1;
263 pathlen = strlen (path);
264 name = __alloca (pathlen + len + 1);
265 /* Copy the file name at the top. */
266 name = (char *) memcpy (name + pathlen + 1, file, len);
267 /* And add the slash. */
268 *--name = '/';
270 p = path;
273 char *startp;
275 path = p;
276 p = __strchrnul (path, ':');
278 if (p == path)
279 /* Two adjacent colons, or a colon at the beginning or the end
280 of `PATH' means to search the current directory. */
281 startp = name + 1;
282 else
283 startp = (char *) memcpy (name - (p - path), path, p - path);
285 /* Try to execute this name. If it works, execv will not return. */
286 __execve (startp, argv, envp);
288 maybe_script_execute (startp, argv, envp, xflags);
290 switch (errno)
292 case EACCES:
293 case ENOENT:
294 case ESTALE:
295 case ENOTDIR:
296 /* Those errors indicate the file is missing or not executable
297 by us, in which case we want to just try the next path
298 directory. */
299 break;
301 default:
302 /* Some other error means we found an executable file, but
303 something went wrong executing it; return the error to our
304 caller. */
305 _exit (SPAWN_ERROR);
308 while (*p++ != '\0');
310 /* Return with an error. */
311 _exit (SPAWN_ERROR);