* resolv/res_send.c (res_queriesmatch): Fix typo in comment.
[glibc.git] / sysdeps / posix / spawni.c
blob27699f4df8243796c387313eb68b8e224dfb583a
1 /* Guts of POSIX spawn interface. Generic POSIX.1 version.
2 Copyright (C) 2000,2001,2002,2003,2004,2005 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
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 "spawn_int.h"
28 #include <not-cancel.h>
31 /* The Unix standard contains a long explanation of the way to signal
32 an error after the fork() was successful. Since no new wait status
33 was wanted there is no way to signal an error using one of the
34 available methods. The committee chose to signal an error by a
35 normal program exit with the exit code 127. */
36 #define SPAWN_ERROR 127
39 /* The file is accessible but it is not an executable file. Invoke
40 the shell to interpret it as a script. */
41 static void
42 internal_function
43 script_execute (const char *file, char *const argv[], char *const envp[])
45 /* Count the arguments. */
46 int argc = 0;
47 while (argv[argc++])
50 /* Construct an argument list for the shell. */
52 char *new_argv[argc + 1];
53 new_argv[0] = (char *) _PATH_BSHELL;
54 new_argv[1] = (char *) file;
55 while (argc > 1)
57 new_argv[argc] = argv[argc - 1];
58 --argc;
61 /* Execute the shell. */
62 __execve (new_argv[0], new_argv, envp);
67 /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
68 Before running the process perform the actions described in FILE-ACTIONS. */
69 int
70 __spawni (pid_t *pid, const char *file,
71 const posix_spawn_file_actions_t *file_actions,
72 const posix_spawnattr_t *attrp, char *const argv[],
73 char *const envp[], int use_path)
75 pid_t new_pid;
76 char *path, *p, *name;
77 size_t len;
78 size_t pathlen;
80 /* Do this once. */
81 short int flags = attrp == NULL ? 0 : attrp->__flags;
83 /* Generate the new process. */
84 if ((flags & POSIX_SPAWN_USEVFORK) != 0
85 /* If no major work is done, allow using vfork. Note that we
86 might perform the path searching. But this would be done by
87 a call to execvp(), too, and such a call must be OK according
88 to POSIX. */
89 || ((flags & (POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF
90 | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER
91 | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_RESETIDS)) == 0
92 && file_actions == NULL))
93 new_pid = __vfork ();
94 else
95 new_pid = __fork ();
97 if (new_pid != 0)
99 if (new_pid < 0)
100 return errno;
102 /* The call was successful. Store the PID if necessary. */
103 if (pid != NULL)
104 *pid = new_pid;
106 return 0;
109 /* Set signal mask. */
110 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0
111 && __sigprocmask (SIG_SETMASK, &attrp->__ss, NULL) != 0)
112 _exit (SPAWN_ERROR);
114 /* Set signal default action. */
115 if ((flags & POSIX_SPAWN_SETSIGDEF) != 0)
117 /* We have to iterate over all signals. This could possibly be
118 done better but it requires system specific solutions since
119 the sigset_t data type can be very different on different
120 architectures. */
121 int sig;
122 struct sigaction sa;
124 memset (&sa, '\0', sizeof (sa));
125 sa.sa_handler = SIG_DFL;
127 for (sig = 1; sig <= _NSIG; ++sig)
128 if (__sigismember (&attrp->__sd, sig) != 0
129 && __sigaction (sig, &sa, NULL) != 0)
130 _exit (SPAWN_ERROR);
134 #ifdef _POSIX_PRIORITY_SCHEDULING
135 /* Set the scheduling algorithm and parameters. */
136 if ((flags & (POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER))
137 == POSIX_SPAWN_SETSCHEDPARAM)
139 if (__sched_setparam (0, &attrp->__sp) == -1)
140 _exit (SPAWN_ERROR);
142 else if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0)
144 if (__sched_setscheduler (0, attrp->__policy,
145 (flags & POSIX_SPAWN_SETSCHEDPARAM) != 0
146 ? &attrp->__sp : NULL) == -1)
147 _exit (SPAWN_ERROR);
149 #endif
151 /* Set the process group ID. */
152 if ((flags & POSIX_SPAWN_SETPGROUP) != 0
153 && __setpgid (0, attrp->__pgrp) != 0)
154 _exit (SPAWN_ERROR);
156 /* Set the effective user and group IDs. */
157 if ((flags & POSIX_SPAWN_RESETIDS) != 0
158 && (seteuid (__getuid ()) != 0 || setegid (__getgid ()) != 0))
159 _exit (SPAWN_ERROR);
161 /* Execute the file actions. */
162 if (file_actions != NULL)
164 int cnt;
166 for (cnt = 0; cnt < file_actions->__used; ++cnt)
168 struct __spawn_action *action = &file_actions->__actions[cnt];
170 switch (action->tag)
172 case spawn_do_close:
173 if (close_not_cancel (action->action.close_action.fd) != 0)
174 /* Signal the error. */
175 _exit (SPAWN_ERROR);
176 break;
178 case spawn_do_open:
180 int new_fd = __open64 (action->action.open_action.path,
181 action->action.open_action.oflag,
182 action->action.open_action.mode);
184 if (new_fd == -1)
185 /* The `open' call failed. */
186 _exit (SPAWN_ERROR);
188 /* Make sure the desired file descriptor is used. */
189 if (new_fd != action->action.open_action.fd)
191 if (__dup2 (new_fd, action->action.open_action.fd)
192 != action->action.open_action.fd)
193 /* The `dup2' call failed. */
194 _exit (SPAWN_ERROR);
196 if (__close (new_fd) != 0)
197 /* The `close' call failed. */
198 _exit (SPAWN_ERROR);
201 break;
203 case spawn_do_dup2:
204 if (__dup2 (action->action.dup2_action.fd,
205 action->action.dup2_action.newfd)
206 != action->action.dup2_action.newfd)
207 /* The `dup2' call failed. */
208 _exit (SPAWN_ERROR);
209 break;
214 if (! use_path || strchr (file, '/') != NULL)
216 /* The FILE parameter is actually a path. */
217 __execve (file, argv, envp);
219 if (errno == ENOEXEC)
220 script_execute (file, argv, envp);
222 /* Oh, oh. `execve' returns. This is bad. */
223 _exit (SPAWN_ERROR);
226 /* We have to search for FILE on the path. */
227 path = getenv ("PATH");
228 if (path == NULL)
230 /* There is no `PATH' in the environment.
231 The default search path is the current directory
232 followed by the path `confstr' returns for `_CS_PATH'. */
233 len = confstr (_CS_PATH, (char *) NULL, 0);
234 path = (char *) __alloca (1 + len);
235 path[0] = ':';
236 (void) confstr (_CS_PATH, path + 1, len);
239 len = strlen (file) + 1;
240 pathlen = strlen (path);
241 name = __alloca (pathlen + len + 1);
242 /* Copy the file name at the top. */
243 name = (char *) memcpy (name + pathlen + 1, file, len);
244 /* And add the slash. */
245 *--name = '/';
247 p = path;
250 char *startp;
252 path = p;
253 p = __strchrnul (path, ':');
255 if (p == path)
256 /* Two adjacent colons, or a colon at the beginning or the end
257 of `PATH' means to search the current directory. */
258 startp = name + 1;
259 else
260 startp = (char *) memcpy (name - (p - path), path, p - path);
262 /* Try to execute this name. If it works, execv will not return. */
263 __execve (startp, argv, envp);
265 if (errno == ENOEXEC)
266 script_execute (startp, argv, envp);
268 switch (errno)
270 case EACCES:
271 case ENOENT:
272 case ESTALE:
273 case ENOTDIR:
274 /* Those errors indicate the file is missing or not executable
275 by us, in which case we want to just try the next path
276 directory. */
277 break;
279 default:
280 /* Some other error means we found an executable file, but
281 something went wrong executing it; return the error to our
282 caller. */
283 _exit (SPAWN_ERROR);
286 while (*p++ != '\0');
288 /* Return with an error. */
289 _exit (SPAWN_ERROR);