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