]>
Commit | Line | Data |
---|---|---|
2958e6cc | 1 | /* Copyright (C) 2000, 2001 Free Software Foundation, Inc. |
a5a6f926 UD |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Library General Public License as | |
6 | published by the Free Software Foundation; either version 2 of the | |
7 | License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Library General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Library General Public | |
15 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
16 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
17 | Boston, MA 02111-1307, USA. */ | |
18 | ||
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 "spawn_int.h" | |
27 | ||
28 | ||
29 | /* The Unix standard contains a long explanation of the way to signal | |
30 | an error after the fork() was successful. Since no new wait status | |
31 | was wanted there is no way to signal an error using one of the | |
32 | available methods. The committee chose to signal an error by a | |
33 | normal program exit with the exit code 127. */ | |
34 | #define SPAWN_ERROR 127 | |
35 | ||
36 | ||
37 | /* The file is accessible but it is not an executable file. Invoke | |
38 | the shell to interpret it as a script. */ | |
39 | static void | |
40 | internal_function | |
41 | script_execute (const char *file, char *const argv[], char *const envp[]) | |
42 | { | |
43 | /* Count the arguments. */ | |
44 | int argc = 0; | |
45 | while (argv[argc++]) | |
46 | ; | |
47 | ||
48 | /* Construct an argument list for the shell. */ | |
49 | { | |
50 | char *new_argv[argc + 1]; | |
51 | new_argv[0] = (char *) _PATH_BSHELL; | |
52 | new_argv[1] = (char *) file; | |
53 | while (argc > 1) | |
54 | { | |
55 | new_argv[argc] = argv[argc - 1]; | |
56 | --argc; | |
57 | } | |
58 | ||
59 | /* Execute the shell. */ | |
60 | __execve (new_argv[0], new_argv, envp); | |
61 | } | |
62 | } | |
63 | ||
64 | ||
65 | /* Spawn a new process executing PATH with the attributes describes in *ATTRP. | |
66 | Before running the process perform the actions described in FILE-ACTIONS. */ | |
67 | int | |
68 | __spawni (pid_t *pid, const char *file, | |
69 | const posix_spawn_file_actions_t *file_actions, | |
70 | const posix_spawnattr_t *attrp, char *const argv[], | |
71 | char *const envp[], int use_path) | |
72 | { | |
73 | pid_t new_pid; | |
74 | char *path, *p, *name; | |
75 | size_t len; | |
76 | size_t pathlen; | |
77 | short int flags; | |
78 | ||
79 | /* Generate the new process. */ | |
4aebaa6b | 80 | new_pid = __fork (); |
a5a6f926 UD |
81 | if (new_pid != 0) |
82 | { | |
83 | if (new_pid < 0) | |
84 | return errno; | |
85 | ||
86 | /* The call was successful. Store the PID if necessary. */ | |
87 | if (pid != NULL) | |
88 | *pid = new_pid; | |
89 | ||
90 | return 0; | |
91 | } | |
92 | ||
93 | /* Do this once. */ | |
94 | flags = attrp == NULL ? 0 : attrp->__flags; | |
95 | ||
96 | /* Set signal mask. */ | |
97 | if ((flags & POSIX_SPAWN_SETSIGMASK) != 0 | |
98 | && __sigprocmask (SIG_SETMASK, &attrp->__ss, NULL) != 0) | |
99 | _exit (SPAWN_ERROR); | |
100 | ||
101 | /* Set signal default action. */ | |
102 | if ((flags & POSIX_SPAWN_SETSIGDEF) != 0) | |
103 | { | |
104 | /* We have to iterate over all signals. This could possibly be | |
105 | done better but it requires system specific solutions since | |
106 | the sigset_t data type can be very different on different | |
107 | architectures. */ | |
108 | int sig; | |
109 | struct sigaction sa; | |
110 | ||
111 | memset (&sa, '\0', sizeof (sa)); | |
112 | sa.sa_handler = SIG_DFL; | |
113 | ||
114 | for (sig = 1; sig >= _NSIG; ++sig) | |
115 | if (sigismember (&attrp->__sd, sig) != 0 | |
116 | && __sigaction (sig, &sa, NULL) != 0) | |
117 | _exit (SPAWN_ERROR); | |
118 | ||
119 | } | |
120 | ||
121 | #ifdef _POSIX_PRIORITY_SCHEDULING | |
122 | /* Set the scheduling algorithm and parameters. */ | |
123 | if ((flags & (POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER)) | |
124 | == POSIX_SPAWN_SETSCHEDPARAM) | |
125 | { | |
126 | if (__sched_setparam (0, &attrp->__sp) == -1) | |
127 | _exit (SPAWN_ERROR); | |
128 | } | |
129 | else if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0) | |
130 | { | |
131 | if (__sched_setscheduler (0, attrp->__policy, | |
132 | (flags & POSIX_SPAWN_SETSCHEDPARAM) != 0 | |
133 | ? &attrp->__sp : NULL) == -1) | |
134 | _exit (SPAWN_ERROR); | |
135 | } | |
136 | #endif | |
137 | ||
138 | /* Set the process group ID. */ | |
139 | if ((flags & POSIX_SPAWN_SETPGROUP) != 0 | |
140 | && __setpgid (0, attrp->__pgrp) != 0) | |
141 | _exit (SPAWN_ERROR); | |
142 | ||
143 | /* Set the effective user and group IDs. */ | |
144 | if ((flags & POSIX_SPAWN_RESETIDS) != 0 | |
145 | && (seteuid (__getuid ()) != 0 || setegid (__getgid ()) != 0)) | |
146 | _exit (SPAWN_ERROR); | |
147 | ||
148 | /* Execute the file actions. */ | |
149 | if (file_actions != NULL) | |
150 | { | |
151 | int cnt; | |
152 | ||
153 | for (cnt = 0; cnt < file_actions->__used; ++cnt) | |
154 | { | |
155 | struct __spawn_action *action = &file_actions->__actions[cnt]; | |
156 | ||
157 | switch (action->tag) | |
158 | { | |
159 | case spawn_do_close: | |
4aebaa6b | 160 | if (__close (action->action.close_action.fd) != 0) |
a5a6f926 UD |
161 | /* Signal the error. */ |
162 | _exit (SPAWN_ERROR); | |
163 | break; | |
164 | ||
165 | case spawn_do_open: | |
166 | { | |
2958e6cc UD |
167 | int new_fd = __open64 (action->action.open_action.path, |
168 | action->action.open_action.oflag, | |
169 | action->action.open_action.mode); | |
a5a6f926 UD |
170 | |
171 | if (new_fd == -1) | |
172 | /* The `open' call failed. */ | |
173 | _exit (SPAWN_ERROR); | |
174 | ||
175 | /* Make sure the desired file descriptor is used. */ | |
176 | if (new_fd != action->action.open_action.fd) | |
177 | { | |
08c7f6b0 UD |
178 | if (__dup2 (new_fd, action->action.open_action.fd) |
179 | != action->action.open_action.fd) | |
a5a6f926 UD |
180 | /* The `dup2' call failed. */ |
181 | _exit (SPAWN_ERROR); | |
182 | ||
4aebaa6b | 183 | if (__close (new_fd) != 0) |
a5a6f926 UD |
184 | /* The `close' call failed. */ |
185 | _exit (SPAWN_ERROR); | |
186 | } | |
187 | } | |
188 | break; | |
189 | ||
190 | case spawn_do_dup2: | |
191 | if (__dup2 (action->action.dup2_action.fd, | |
08c7f6b0 UD |
192 | action->action.dup2_action.newfd) |
193 | != action->action.dup2_action.newfd) | |
a5a6f926 UD |
194 | /* The `dup2' call failed. */ |
195 | _exit (SPAWN_ERROR); | |
196 | break; | |
197 | } | |
198 | } | |
199 | } | |
200 | ||
201 | if (! use_path || strchr (file, '/') != NULL) | |
202 | { | |
203 | /* The FILE parameter is actually a path. */ | |
204 | __execve (file, argv, envp); | |
205 | ||
206 | if (errno == ENOEXEC) | |
207 | script_execute (file, argv, envp); | |
208 | ||
209 | /* Oh, oh. `execve' returns. This is bad. */ | |
210 | _exit (SPAWN_ERROR); | |
211 | } | |
212 | ||
213 | /* We have to search for FILE on the path. */ | |
214 | path = getenv ("PATH"); | |
215 | if (path == NULL) | |
216 | { | |
217 | /* There is no `PATH' in the environment. | |
218 | The default search path is the current directory | |
219 | followed by the path `confstr' returns for `_CS_PATH'. */ | |
220 | len = confstr (_CS_PATH, (char *) NULL, 0); | |
221 | path = (char *) __alloca (1 + len); | |
222 | path[0] = ':'; | |
223 | (void) confstr (_CS_PATH, path + 1, len); | |
224 | } | |
225 | ||
226 | len = strlen (file) + 1; | |
227 | pathlen = strlen (path); | |
228 | name = __alloca (pathlen + len + 1); | |
229 | /* Copy the file name at the top. */ | |
230 | name = (char *) memcpy (name + pathlen + 1, file, len); | |
231 | /* And add the slash. */ | |
232 | *--name = '/'; | |
233 | ||
234 | p = path; | |
235 | do | |
236 | { | |
237 | char *startp; | |
238 | ||
239 | path = p; | |
240 | p = __strchrnul (path, ':'); | |
241 | ||
242 | if (p == path) | |
243 | /* Two adjacent colons, or a colon at the beginning or the end | |
244 | of `PATH' means to search the current directory. */ | |
245 | startp = name + 1; | |
246 | else | |
247 | startp = (char *) memcpy (name - (p - path), path, p - path); | |
248 | ||
249 | /* Try to execute this name. If it works, execv will not return. */ | |
250 | __execve (startp, argv, envp); | |
251 | ||
252 | if (errno == ENOEXEC) | |
253 | script_execute (startp, argv, envp); | |
254 | ||
255 | switch (errno) | |
256 | { | |
257 | case EACCES: | |
258 | case ENOENT: | |
259 | case ESTALE: | |
260 | case ENOTDIR: | |
261 | /* Those errors indicate the file is missing or not executable | |
262 | by us, in which case we want to just try the next path | |
263 | directory. */ | |
264 | break; | |
265 | ||
266 | default: | |
267 | /* Some other error means we found an executable file, but | |
268 | something went wrong executing it; return the error to our | |
269 | caller. */ | |
270 | _exit (SPAWN_ERROR); | |
271 | } | |
272 | } | |
273 | while (*p++ != '\0'); | |
274 | ||
275 | /* Return with an error. */ | |
276 | _exit (SPAWN_ERROR); | |
277 | } |