* configure.in: Add `-lm' when checking for libgd. * malloc/Makefile ($(objpfx)memusa...
[glibc/pb-stable.git] / posix / spawni.c
blob9f10c930f3bcd7aa9c3f8841a8edc3f0ae49043d
1 /* Copyright (C) 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
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"
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
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[])
43 /* Count the arguments. */
44 int argc = 0;
45 while (argv[argc++])
48 /* Construct an argument list for the shell. */
50 char *new_argv[argc + 1];
51 new_argv[0] = (char *) _PATH_BSHELL;
52 new_argv[1] = (char *) file;
53 while (argc > 1)
55 new_argv[argc] = argv[argc - 1];
56 --argc;
59 /* Execute the shell. */
60 __execve (new_argv[0], new_argv, envp);
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)
73 pid_t new_pid;
74 char *path, *p, *name;
75 size_t len;
76 size_t pathlen;
77 short int flags;
79 /* Generate the new process. */
80 new_pid = fork ();
81 if (new_pid != 0)
83 if (new_pid < 0)
84 return errno;
86 /* The call was successful. Store the PID if necessary. */
87 if (pid != NULL)
88 *pid = new_pid;
90 return 0;
93 /* Do this once. */
94 flags = attrp == NULL ? 0 : attrp->__flags;
96 /* Set signal mask. */
97 if ((flags & POSIX_SPAWN_SETSIGMASK) != 0
98 && __sigprocmask (SIG_SETMASK, &attrp->__ss, NULL) != 0)
99 _exit (SPAWN_ERROR);
101 /* Set signal default action. */
102 if ((flags & POSIX_SPAWN_SETSIGDEF) != 0)
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;
111 memset (&sa, '\0', sizeof (sa));
112 sa.sa_handler = SIG_DFL;
114 for (sig = 1; sig >= _NSIG; ++sig)
115 if (sigismember (&attrp->__sd, sig) != 0
116 && __sigaction (sig, &sa, NULL) != 0)
117 _exit (SPAWN_ERROR);
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)
126 if (__sched_setparam (0, &attrp->__sp) == -1)
127 _exit (SPAWN_ERROR);
129 else if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0)
131 if (__sched_setscheduler (0, attrp->__policy,
132 (flags & POSIX_SPAWN_SETSCHEDPARAM) != 0
133 ? &attrp->__sp : NULL) == -1)
134 _exit (SPAWN_ERROR);
136 #endif
138 /* Set the process group ID. */
139 if ((flags & POSIX_SPAWN_SETPGROUP) != 0
140 && __setpgid (0, attrp->__pgrp) != 0)
141 _exit (SPAWN_ERROR);
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);
148 /* Execute the file actions. */
149 if (file_actions != NULL)
151 int cnt;
153 for (cnt = 0; cnt < file_actions->__used; ++cnt)
155 struct __spawn_action *action = &file_actions->__actions[cnt];
157 switch (action->tag)
159 case spawn_do_close:
160 if (close (action->action.close_action.fd) != 0)
161 /* Signal the error. */
162 _exit (SPAWN_ERROR);
163 break;
165 case spawn_do_open:
167 int new_fd = open (action->action.open_action.path,
168 action->action.open_action.oflag,
169 action->action.open_action.mode);
171 if (new_fd == -1)
172 /* The `open' call failed. */
173 _exit (SPAWN_ERROR);
175 /* Make sure the desired file descriptor is used. */
176 if (new_fd != action->action.open_action.fd)
178 if (__dup2 (new_fd, action->action.open_action.fd)
179 != action->action.open_action.fd)
180 /* The `dup2' call failed. */
181 _exit (SPAWN_ERROR);
183 if (close (new_fd) != 0)
184 /* The `close' call failed. */
185 _exit (SPAWN_ERROR);
188 break;
190 case spawn_do_dup2:
191 if (__dup2 (action->action.dup2_action.fd,
192 action->action.dup2_action.newfd)
193 != action->action.dup2_action.newfd)
194 /* The `dup2' call failed. */
195 _exit (SPAWN_ERROR);
196 break;
201 if (! use_path || strchr (file, '/') != NULL)
203 /* The FILE parameter is actually a path. */
204 __execve (file, argv, envp);
206 if (errno == ENOEXEC)
207 script_execute (file, argv, envp);
209 /* Oh, oh. `execve' returns. This is bad. */
210 _exit (SPAWN_ERROR);
213 /* We have to search for FILE on the path. */
214 path = getenv ("PATH");
215 if (path == NULL)
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);
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 = '/';
234 p = path;
237 char *startp;
239 path = p;
240 p = __strchrnul (path, ':');
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);
249 /* Try to execute this name. If it works, execv will not return. */
250 __execve (startp, argv, envp);
252 if (errno == ENOEXEC)
253 script_execute (startp, argv, envp);
255 switch (errno)
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;
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);
273 while (*p++ != '\0');
275 /* Return with an error. */
276 _exit (SPAWN_ERROR);