1 /* Guts of POSIX spawn interface. Generic POSIX.1 version.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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
27 #include "spawn_int.h"
28 #include <not-cancel.h>
31 /* The Unix standard contains a long explanation of the way to signal
32 an error after the fork() was successful. Since no new wait status
33 was wanted there is no way to signal an error using one of the
34 available methods. The committee chose to signal an error by a
35 normal program exit with the exit code 127. */
36 #define SPAWN_ERROR 127
39 /* The file is accessible but it is not an executable file. Invoke
40 the shell to interpret it as a script. */
43 script_execute (const char *file
, char *const argv
[], char *const envp
[])
45 /* Count the arguments. */
50 /* Construct an argument list for the shell. */
52 char *new_argv
[argc
+ 1];
53 new_argv
[0] = (char *) _PATH_BSHELL
;
54 new_argv
[1] = (char *) file
;
57 new_argv
[argc
] = argv
[argc
- 1];
61 /* Execute the shell. */
62 __execve (new_argv
[0], new_argv
, envp
);
67 /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
68 Before running the process perform the actions described in FILE-ACTIONS. */
70 __spawni (pid_t
*pid
, const char *file
,
71 const posix_spawn_file_actions_t
*file_actions
,
72 const posix_spawnattr_t
*attrp
, char *const argv
[],
73 char *const envp
[], int use_path
)
76 char *path
, *p
, *name
;
81 short int flags
= attrp
== NULL
? 0 : attrp
->__flags
;
83 /* Generate the new process. */
84 if (flags
& POSIX_SPAWN_USEVFORK
)
94 /* The call was successful. Store the PID if necessary. */
101 /* Set signal mask. */
102 if ((flags
& POSIX_SPAWN_SETSIGMASK
) != 0
103 && __sigprocmask (SIG_SETMASK
, &attrp
->__ss
, NULL
) != 0)
106 /* Set signal default action. */
107 if ((flags
& POSIX_SPAWN_SETSIGDEF
) != 0)
109 /* We have to iterate over all signals. This could possibly be
110 done better but it requires system specific solutions since
111 the sigset_t data type can be very different on different
116 memset (&sa
, '\0', sizeof (sa
));
117 sa
.sa_handler
= SIG_DFL
;
119 for (sig
= 1; sig
<= _NSIG
; ++sig
)
120 if (__sigismember (&attrp
->__sd
, sig
) != 0
121 && __sigaction (sig
, &sa
, NULL
) != 0)
126 #ifdef _POSIX_PRIORITY_SCHEDULING
127 /* Set the scheduling algorithm and parameters. */
128 if ((flags
& (POSIX_SPAWN_SETSCHEDPARAM
| POSIX_SPAWN_SETSCHEDULER
))
129 == POSIX_SPAWN_SETSCHEDPARAM
)
131 if (__sched_setparam (0, &attrp
->__sp
) == -1)
134 else if ((flags
& POSIX_SPAWN_SETSCHEDULER
) != 0)
136 if (__sched_setscheduler (0, attrp
->__policy
,
137 (flags
& POSIX_SPAWN_SETSCHEDPARAM
) != 0
138 ? &attrp
->__sp
: NULL
) == -1)
143 /* Set the process group ID. */
144 if ((flags
& POSIX_SPAWN_SETPGROUP
) != 0
145 && __setpgid (0, attrp
->__pgrp
) != 0)
148 /* Set the effective user and group IDs. */
149 if ((flags
& POSIX_SPAWN_RESETIDS
) != 0
150 && (seteuid (__getuid ()) != 0 || setegid (__getgid ()) != 0))
153 /* Execute the file actions. */
154 if (file_actions
!= NULL
)
158 for (cnt
= 0; cnt
< file_actions
->__used
; ++cnt
)
160 struct __spawn_action
*action
= &file_actions
->__actions
[cnt
];
165 if (close_not_cancel (action
->action
.close_action
.fd
) != 0)
166 /* Signal the error. */
172 int new_fd
= __open64 (action
->action
.open_action
.path
,
173 action
->action
.open_action
.oflag
,
174 action
->action
.open_action
.mode
);
177 /* The `open' call failed. */
180 /* Make sure the desired file descriptor is used. */
181 if (new_fd
!= action
->action
.open_action
.fd
)
183 if (__dup2 (new_fd
, action
->action
.open_action
.fd
)
184 != action
->action
.open_action
.fd
)
185 /* The `dup2' call failed. */
188 if (__close (new_fd
) != 0)
189 /* The `close' call failed. */
196 if (__dup2 (action
->action
.dup2_action
.fd
,
197 action
->action
.dup2_action
.newfd
)
198 != action
->action
.dup2_action
.newfd
)
199 /* The `dup2' call failed. */
206 if (! use_path
|| strchr (file
, '/') != NULL
)
208 /* The FILE parameter is actually a path. */
209 __execve (file
, argv
, envp
);
211 if (errno
== ENOEXEC
)
212 script_execute (file
, argv
, envp
);
214 /* Oh, oh. `execve' returns. This is bad. */
218 /* We have to search for FILE on the path. */
219 path
= getenv ("PATH");
222 /* There is no `PATH' in the environment.
223 The default search path is the current directory
224 followed by the path `confstr' returns for `_CS_PATH'. */
225 len
= confstr (_CS_PATH
, (char *) NULL
, 0);
226 path
= (char *) __alloca (1 + len
);
228 (void) confstr (_CS_PATH
, path
+ 1, len
);
231 len
= strlen (file
) + 1;
232 pathlen
= strlen (path
);
233 name
= __alloca (pathlen
+ len
+ 1);
234 /* Copy the file name at the top. */
235 name
= (char *) memcpy (name
+ pathlen
+ 1, file
, len
);
236 /* And add the slash. */
245 p
= __strchrnul (path
, ':');
248 /* Two adjacent colons, or a colon at the beginning or the end
249 of `PATH' means to search the current directory. */
252 startp
= (char *) memcpy (name
- (p
- path
), path
, p
- path
);
254 /* Try to execute this name. If it works, execv will not return. */
255 __execve (startp
, argv
, envp
);
257 if (errno
== ENOEXEC
)
258 script_execute (startp
, argv
, envp
);
266 /* Those errors indicate the file is missing or not executable
267 by us, in which case we want to just try the next path
272 /* Some other error means we found an executable file, but
273 something went wrong executing it; return the error to our
278 while (*p
++ != '\0');
280 /* Return with an error. */