1 /* Guts of POSIX spawn interface. Generic POSIX.1 version.
2 Copyright (C) 2000-2019 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, see
17 <http://www.gnu.org/licenses/>. */
24 #include <sys/resource.h>
26 #include <sys/param.h>
28 #include <not-cancel.h>
29 #include <local-setxid.h>
30 #include <shlib-compat.h>
31 #include <nptl/pthreadP.h>
32 #include <dl-sysdep.h>
33 #include <libc-pointer-arith.h>
35 #include "spawn_int.h"
38 /* The Unix standard contains a long explanation of the way to signal
39 an error after the fork() was successful. Since no new wait status
40 was wanted there is no way to signal an error using one of the
41 available methods. The committee chose to signal an error by a
42 normal program exit with the exit code 127. */
43 #define SPAWN_ERROR 127
45 struct posix_spawn_args
49 int (*exec
) (const char *, char *const *, char *const *);
50 const posix_spawn_file_actions_t
*fa
;
51 const posix_spawnattr_t
*restrict attr
;
59 /* Older version requires that shell script without shebang definition
60 to be called explicitly using /bin/sh (_PATH_BSHELL). */
62 maybe_script_execute (struct posix_spawn_args
*args
)
64 if (SHLIB_COMPAT (libc
, GLIBC_2_2
, GLIBC_2_15
)
65 && (args
->xflags
& SPAWN_XFLAGS_TRY_SHELL
) && errno
== ENOEXEC
)
67 char *const *argv
= args
->argv
;
68 ptrdiff_t argc
= args
->argc
;
70 /* Construct an argument list for the shell. */
71 char *new_argv
[argc
+ 2];
72 new_argv
[0] = (char *) _PATH_BSHELL
;
73 new_argv
[1] = (char *) args
->file
;
75 memcpy (new_argv
+ 2, argv
+ 1, argc
* sizeof (char *));
79 /* Execute the shell. */
80 args
->exec (new_argv
[0], new_argv
, args
->envp
);
84 /* Function used in the clone call to setup the signals mask, posix_spawn
85 attributes, and file actions. */
87 __spawni_child (void *arguments
)
89 struct posix_spawn_args
*args
= arguments
;
90 const posix_spawnattr_t
*restrict attr
= args
->attr
;
91 const posix_spawn_file_actions_t
*file_actions
= args
->fa
;
94 __close (args
->pipe
[0]);
96 /* Set signal default action. */
97 if ((attr
->__flags
& POSIX_SPAWN_SETSIGDEF
) != 0)
99 /* We have to iterate over all signals. This could possibly be
100 done better but it requires system specific solutions since
101 the sigset_t data type can be very different on different
106 memset (&sa
, '\0', sizeof (sa
));
107 sa
.sa_handler
= SIG_DFL
;
109 for (sig
= 1; sig
<= _NSIG
; ++sig
)
110 if (__sigismember (&attr
->__sd
, sig
) != 0
111 && __sigaction (sig
, &sa
, NULL
) != 0)
115 #ifdef _POSIX_PRIORITY_SCHEDULING
116 /* Set the scheduling algorithm and parameters. */
117 if ((attr
->__flags
& (POSIX_SPAWN_SETSCHEDPARAM
| POSIX_SPAWN_SETSCHEDULER
))
118 == POSIX_SPAWN_SETSCHEDPARAM
)
120 if (__sched_setparam (0, &attr
->__sp
) == -1)
123 else if ((attr
->__flags
& POSIX_SPAWN_SETSCHEDULER
) != 0)
125 if (__sched_setscheduler (0, attr
->__policy
, &attr
->__sp
) == -1)
130 /* Set the process session ID. */
131 if ((attr
->__flags
& POSIX_SPAWN_SETSID
) != 0
135 /* Set the process group ID. */
136 if ((attr
->__flags
& POSIX_SPAWN_SETPGROUP
) != 0
137 && __setpgid (0, attr
->__pgrp
) != 0)
140 /* Set the effective user and group IDs. */
141 if ((attr
->__flags
& POSIX_SPAWN_RESETIDS
) != 0
142 && (local_seteuid (__getuid ()) != 0
143 || local_setegid (__getgid ())) != 0)
146 /* Execute the file actions. */
147 if (file_actions
!= NULL
)
150 struct rlimit64 fdlimit
;
151 bool have_fdlimit
= false;
153 for (cnt
= 0; cnt
< file_actions
->__used
; ++cnt
)
155 struct __spawn_action
*action
= &file_actions
->__actions
[cnt
];
160 if (__close_nocancel (action
->action
.close_action
.fd
) != 0)
162 if (have_fdlimit
== 0)
164 __getrlimit64 (RLIMIT_NOFILE
, &fdlimit
);
168 /* Only signal errors for file descriptors out of range. */
169 if (action
->action
.close_action
.fd
< 0
170 || action
->action
.close_action
.fd
>= fdlimit
.rlim_cur
)
177 /* POSIX states that if fildes was already an open file descriptor,
178 it shall be closed before the new file is opened. This avoid
179 pontential issues when posix_spawn plus addopen action is called
180 with the process already at maximum number of file descriptor
181 opened and also for multiple actions on single-open special
182 paths (like /dev/watchdog). */
183 __close_nocancel (action
->action
.open_action
.fd
);
185 int new_fd
= __open_nocancel (action
->action
.open_action
.path
,
186 action
->action
.open_action
.oflag
188 action
->action
.open_action
.mode
);
193 /* Make sure the desired file descriptor is used. */
194 if (new_fd
!= action
->action
.open_action
.fd
)
196 if (__dup2 (new_fd
, action
->action
.open_action
.fd
)
197 != action
->action
.open_action
.fd
)
200 if (__close_nocancel (new_fd
) != 0)
207 /* Austin Group issue #411 requires adddup2 action with source
208 and destination being equal to remove close-on-exec flag. */
209 if (action
->action
.dup2_action
.fd
210 == action
->action
.dup2_action
.newfd
)
212 int fd
= action
->action
.dup2_action
.newfd
;
213 int flags
= __fcntl (fd
, F_GETFD
, 0);
216 if (__fcntl (fd
, F_SETFD
, flags
& ~FD_CLOEXEC
) == -1)
219 else if (__dup2 (action
->action
.dup2_action
.fd
,
220 action
->action
.dup2_action
.newfd
)
221 != action
->action
.dup2_action
.newfd
)
226 if (__chdir (action
->action
.chdir_action
.path
) != 0)
230 case spawn_do_fchdir
:
231 if (__fchdir (action
->action
.fchdir_action
.fd
) != 0)
238 /* Set the initial signal mask of the child if POSIX_SPAWN_SETSIGMASK
239 is set, otherwise restore the previous one. */
240 __sigprocmask (SIG_SETMASK
, (attr
->__flags
& POSIX_SPAWN_SETSIGMASK
)
241 ? &attr
->__ss
: &args
->oldmask
, 0);
243 args
->exec (args
->file
, args
->argv
, args
->envp
);
245 /* This is compatibility function required to enable posix_spawn run
246 script without shebang definition for older posix_spawn versions
248 maybe_script_execute (args
);
251 /* errno should have an appropriate non-zero value; otherwise,
252 there's a bug in glibc or the kernel. For lack of an error code
253 (EINTERNALBUG) describing that, use ECHILD. Another option would
254 be to set args->err to some negative sentinel and have the parent
255 abort(), but that seems needlessly harsh. */
256 ret
= errno
? : ECHILD
;
258 /* Since sizeof errno < PIPE_BUF, the write is atomic. */
259 while (__write_nocancel (args
->pipe
[1], &ret
, sizeof (ret
)) < 0);
264 /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
265 Before running the process perform the actions described in FILE-ACTIONS. */
267 __spawnix (pid_t
*pid
, const char *file
,
268 const posix_spawn_file_actions_t
*file_actions
,
269 const posix_spawnattr_t
*attrp
, char *const argv
[],
270 char *const envp
[], int xflags
,
271 int (*exec
) (const char *, char *const *, char *const *))
273 struct posix_spawn_args args
;
276 if (__pipe2 (args
.pipe
, O_CLOEXEC
))
279 /* Disable asynchronous cancellation. */
281 __libc_ptf_call (__pthread_setcancelstate
,
282 (PTHREAD_CANCEL_DISABLE
, &state
), 0);
285 ptrdiff_t limit
= INT_MAX
- 1;
286 while (argv
[argc
++] != NULL
)
295 args
.fa
= file_actions
;
296 args
.attr
= attrp
? attrp
: &(const posix_spawnattr_t
) { 0 };
300 args
.xflags
= xflags
;
302 /* Generate the new process. */
303 pid_t new_pid
= __fork ();
306 __spawni_child (&args
);
307 else if (new_pid
> 0)
309 __close (args
.pipe
[1]);
311 if (__read (args
.pipe
[0], &ec
, sizeof ec
) != sizeof ec
)
314 __waitpid (new_pid
, &(int) { 0 }, 0);
319 __close (args
.pipe
[0]);
321 if ((ec
== 0) && (pid
!= NULL
))
324 __libc_ptf_call (__pthread_setcancelstate
, (state
, NULL
), 0);
330 __spawni (pid_t
* pid
, const char *file
,
331 const posix_spawn_file_actions_t
* acts
,
332 const posix_spawnattr_t
* attrp
, char *const argv
[],
333 char *const envp
[], int xflags
)
335 /* It uses __execvpex to avoid run ENOEXEC in non compatibility mode (it
336 will be handled by maybe_script_execute). */
337 return __spawnix (pid
, file
, acts
, attrp
, argv
, envp
, xflags
,
338 xflags
& SPAWN_XFLAGS_USE_PATH
? __execvpex
: __execve
);