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. */
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. */
41 script_execute (const char *file
, char *const argv
[], char *const envp
[])
43 /* Count the arguments. */
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
;
55 new_argv
[argc
] = argv
[argc
- 1];
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. */
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
)
74 char *path
, *p
, *name
;
79 /* Generate the new process. */
86 /* The call was successful. Store the PID if necessary. */
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)
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
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)
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)
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)
138 /* Set the process group ID. */
139 if ((flags
& POSIX_SPAWN_SETPGROUP
) != 0
140 && __setpgid (0, attrp
->__pgrp
) != 0)
143 /* Set the effective user and group IDs. */
144 if ((flags
& POSIX_SPAWN_RESETIDS
) != 0
145 && (seteuid (__getuid ()) != 0 || setegid (__getgid ()) != 0))
148 /* Execute the file actions. */
149 if (file_actions
!= NULL
)
153 for (cnt
= 0; cnt
< file_actions
->__used
; ++cnt
)
155 struct __spawn_action
*action
= &file_actions
->__actions
[cnt
];
160 if (close (action
->action
.close_action
.fd
) != 0)
161 /* Signal the error. */
167 int new_fd
= open (action
->action
.open_action
.path
,
168 action
->action
.open_action
.oflag
,
169 action
->action
.open_action
.mode
);
172 /* The `open' call failed. */
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. */
183 if (close (new_fd
) != 0)
184 /* The `close' call failed. */
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. */
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. */
213 /* We have to search for FILE on the path. */
214 path
= getenv ("PATH");
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
);
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. */
240 p
= __strchrnul (path
, ':');
243 /* Two adjacent colons, or a colon at the beginning or the end
244 of `PATH' means to search the current directory. */
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
);
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
267 /* Some other error means we found an executable file, but
268 something went wrong executing it; return the error to our
273 while (*p
++ != '\0');
275 /* Return with an error. */