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