]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/posix/spawni.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / posix / spawni.c
CommitLineData
986ad61e 1/* Guts of POSIX spawn interface. Generic POSIX.1 version.
04277e02 2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
a5a6f926
UD
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
41bdb6e2
AJ
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.
a5a6f926
UD
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
41bdb6e2 13 Lesser General Public License for more details.
a5a6f926 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
a5a6f926 18
ccfb2964
AZ
19#include <spawn.h>
20#include <assert.h>
a5a6f926
UD
21#include <fcntl.h>
22#include <paths.h>
a5a6f926 23#include <string.h>
cfa28e56 24#include <sys/resource.h>
ccfb2964
AZ
25#include <sys/wait.h>
26#include <sys/param.h>
27#include <sys/mman.h>
73299943 28#include <not-cancel.h>
1b8373f4 29#include <local-setxid.h>
d96de963 30#include <shlib-compat.h>
ccfb2964
AZ
31#include <nptl/pthreadP.h>
32#include <dl-sysdep.h>
33#include <libc-pointer-arith.h>
34#include <ldsodefs.h>
35#include "spawn_int.h"
a5a6f926
UD
36
37
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
44
ccfb2964 45struct posix_spawn_args
a5a6f926 46{
ccfb2964
AZ
47 sigset_t oldmask;
48 const char *file;
49 int (*exec) (const char *, char *const *, char *const *);
50 const posix_spawn_file_actions_t *fa;
51 const posix_spawnattr_t *restrict attr;
52 char *const *argv;
53 ptrdiff_t argc;
54 char *const *envp;
55 int xflags;
56 int pipe[2];
57};
58
59/* Older version requires that shell script without shebang definition
60 to be called explicitly using /bin/sh (_PATH_BSHELL). */
61static void
62maybe_script_execute (struct posix_spawn_args *args)
ecb1482f
RM
63{
64 if (SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_15)
ccfb2964 65 && (args->xflags & SPAWN_XFLAGS_TRY_SHELL) && errno == ENOEXEC)
a5a6f926 66 {
ccfb2964
AZ
67 char *const *argv = args->argv;
68 ptrdiff_t argc = args->argc;
69
70 /* Construct an argument list for the shell. */
f5ec0ea9 71 char *new_argv[argc + 2];
ccfb2964
AZ
72 new_argv[0] = (char *) _PATH_BSHELL;
73 new_argv[1] = (char *) args->file;
74 if (argc > 1)
75 memcpy (new_argv + 2, argv + 1, argc * sizeof(char *));
76 else
77 new_argv[2] = NULL;
a5a6f926 78
ccfb2964
AZ
79 /* Execute the shell. */
80 args->exec (new_argv[0], new_argv, args->envp);
a5a6f926 81 }
ccfb2964
AZ
82}
83
84/* Function used in the clone call to setup the signals mask, posix_spawn
85 attributes, and file actions. */
86static int
87__spawni_child (void *arguments)
88{
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;
92 int ret;
a5a6f926 93
ccfb2964 94 __close (args->pipe[0]);
a5a6f926
UD
95
96 /* Set signal default action. */
ccfb2964 97 if ((attr->__flags & POSIX_SPAWN_SETSIGDEF) != 0)
a5a6f926
UD
98 {
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
102 architectures. */
103 int sig;
104 struct sigaction sa;
105
106 memset (&sa, '\0', sizeof (sa));
107 sa.sa_handler = SIG_DFL;
108
ba3752d5 109 for (sig = 1; sig <= _NSIG; ++sig)
ccfb2964 110 if (__sigismember (&attr->__sd, sig) != 0
a5a6f926 111 && __sigaction (sig, &sa, NULL) != 0)
ccfb2964 112 goto fail;
a5a6f926
UD
113 }
114
115#ifdef _POSIX_PRIORITY_SCHEDULING
116 /* Set the scheduling algorithm and parameters. */
ccfb2964 117 if ((attr->__flags & (POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER))
a5a6f926
UD
118 == POSIX_SPAWN_SETSCHEDPARAM)
119 {
db6b2f25 120 if (__sched_setparam (0, &attr->__sp) == -1)
ccfb2964 121 goto fail;
a5a6f926 122 }
ccfb2964 123 else if ((attr->__flags & POSIX_SPAWN_SETSCHEDULER) != 0)
a5a6f926 124 {
db6b2f25 125 if (__sched_setscheduler (0, attr->__policy, &attr->__sp) == -1)
ccfb2964 126 goto fail;
a5a6f926
UD
127 }
128#endif
129
ccfb2964
AZ
130 /* Set the process session ID. */
131 if ((attr->__flags & POSIX_SPAWN_SETSID) != 0
db6b2f25 132 && __setsid () < 0)
ccfb2964 133 goto fail;
daeb1fa2 134
a5a6f926 135 /* Set the process group ID. */
ccfb2964 136 if ((attr->__flags & POSIX_SPAWN_SETPGROUP) != 0
db6b2f25 137 && __setpgid (0, attr->__pgrp) != 0)
ccfb2964 138 goto fail;
a5a6f926
UD
139
140 /* Set the effective user and group IDs. */
ccfb2964 141 if ((attr->__flags & POSIX_SPAWN_RESETIDS) != 0
db6b2f25
AZ
142 && (local_seteuid (__getuid ()) != 0
143 || local_setegid (__getgid ())) != 0)
ccfb2964 144 goto fail;
a5a6f926
UD
145
146 /* Execute the file actions. */
147 if (file_actions != NULL)
148 {
149 int cnt;
cfa28e56
UD
150 struct rlimit64 fdlimit;
151 bool have_fdlimit = false;
a5a6f926
UD
152
153 for (cnt = 0; cnt < file_actions->__used; ++cnt)
154 {
155 struct __spawn_action *action = &file_actions->__actions[cnt];
156
157 switch (action->tag)
158 {
159 case spawn_do_close:
c181840c 160 if (__close_nocancel (action->action.close_action.fd) != 0)
cfa28e56 161 {
ccfb2964 162 if (have_fdlimit == 0)
cfa28e56 163 {
1a2325c0 164 __getrlimit64 (RLIMIT_NOFILE, &fdlimit);
cfa28e56
UD
165 have_fdlimit = true;
166 }
167
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)
db6b2f25 171 goto fail;
cfa28e56 172 }
a5a6f926
UD
173 break;
174
175 case spawn_do_open:
176 {
ccfb2964
AZ
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). */
c181840c 183 __close_nocancel (action->action.open_action.fd);
ccfb2964 184
c2284574 185 int new_fd = __open_nocancel (action->action.open_action.path,
7231452e
UD
186 action->action.open_action.oflag
187 | O_LARGEFILE,
188 action->action.open_action.mode);
a5a6f926 189
db6b2f25 190 if (new_fd == -1)
ccfb2964 191 goto fail;
a5a6f926
UD
192
193 /* Make sure the desired file descriptor is used. */
194 if (new_fd != action->action.open_action.fd)
195 {
db6b2f25 196 if (__dup2 (new_fd, action->action.open_action.fd)
08c7f6b0 197 != action->action.open_action.fd)
ccfb2964 198 goto fail;
a5a6f926 199
c181840c 200 if (__close_nocancel (new_fd) != 0)
ccfb2964 201 goto fail;
a5a6f926
UD
202 }
203 }
204 break;
205
206 case spawn_do_dup2:
db6b2f25
AZ
207 if (__dup2 (action->action.dup2_action.fd,
208 action->action.dup2_action.newfd)
08c7f6b0 209 != action->action.dup2_action.newfd)
ccfb2964 210 goto fail;
a5a6f926 211 break;
4a938cb2
FW
212
213 case spawn_do_chdir:
214 if (__chdir (action->action.chdir_action.path) != 0)
215 goto fail;
216 break;
3a3fb755
FW
217
218 case spawn_do_fchdir:
219 if (__fchdir (action->action.fchdir_action.fd) != 0)
220 goto fail;
221 break;
a5a6f926
UD
222 }
223 }
224 }
225
ccfb2964
AZ
226 /* Set the initial signal mask of the child if POSIX_SPAWN_SETSIGMASK
227 is set, otherwise restore the previous one. */
228 __sigprocmask (SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
229 ? &attr->__ss : &args->oldmask, 0);
a5a6f926 230
ccfb2964 231 args->exec (args->file, args->argv, args->envp);
a5a6f926 232
ccfb2964
AZ
233 /* This is compatibility function required to enable posix_spawn run
234 script without shebang definition for older posix_spawn versions
235 (2.15). */
236 maybe_script_execute (args);
a5a6f926 237
ccfb2964 238fail:
db6b2f25
AZ
239 /* errno should have an appropriate non-zero value; otherwise,
240 there's a bug in glibc or the kernel. For lack of an error code
241 (EINTERNALBUG) describing that, use ECHILD. Another option would
242 be to set args->err to some negative sentinel and have the parent
243 abort(), but that seems needlessly harsh. */
244 ret = errno ? : ECHILD;
ccfb2964 245 if (ret)
db6b2f25 246 /* Since sizeof errno < PIPE_BUF, the write is atomic. */
c647fb88 247 while (__write_nocancel (args->pipe[1], &ret, sizeof (ret)) < 0);
a5a6f926 248
ccfb2964
AZ
249 _exit (SPAWN_ERROR);
250}
a5a6f926 251
ccfb2964
AZ
252/* Spawn a new process executing PATH with the attributes describes in *ATTRP.
253 Before running the process perform the actions described in FILE-ACTIONS. */
254int
255__spawnix (pid_t *pid, const char *file,
256 const posix_spawn_file_actions_t *file_actions,
257 const posix_spawnattr_t *attrp, char *const argv[],
258 char *const envp[], int xflags,
259 int (*exec) (const char *, char *const *, char *const *))
260{
261 struct posix_spawn_args args;
262 int ec;
a5a6f926 263
ccfb2964
AZ
264 if (__pipe2 (args.pipe, O_CLOEXEC))
265 return errno;
a5a6f926 266
ccfb2964
AZ
267 /* Disable asynchronous cancellation. */
268 int state;
269 __libc_ptf_call (__pthread_setcancelstate,
270 (PTHREAD_CANCEL_DISABLE, &state), 0);
a5a6f926 271
ccfb2964
AZ
272 ptrdiff_t argc = 0;
273 ptrdiff_t limit = INT_MAX - 1;
274 while (argv[argc++] != NULL)
275 if (argc == limit)
276 {
277 errno = E2BIG;
278 return errno;
279 }
a5a6f926 280
ccfb2964
AZ
281 args.file = file;
282 args.exec = exec;
283 args.fa = file_actions;
284 args.attr = attrp ? attrp : &(const posix_spawnattr_t) { 0 };
285 args.argv = argv;
286 args.argc = argc;
287 args.envp = envp;
288 args.xflags = xflags;
289
290 /* Generate the new process. */
291 pid_t new_pid = __fork ();
292
293 if (new_pid == 0)
294 __spawni_child (&args);
295 else if (new_pid > 0)
296 {
297 __close (args.pipe[1]);
298
299 if (__read (args.pipe[0], &ec, sizeof ec) != sizeof ec)
300 ec = 0;
301 else
302 __waitpid (new_pid, &(int) { 0 }, 0);
a5a6f926 303 }
ccfb2964 304 else
db6b2f25 305 ec = errno;
a5a6f926 306
ccfb2964
AZ
307 __close (args.pipe[0]);
308
309 if ((ec == 0) && (pid != NULL))
310 *pid = new_pid;
311
312 __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0);
313
314 return ec;
315}
316
317int
318__spawni (pid_t * pid, const char *file,
319 const posix_spawn_file_actions_t * acts,
320 const posix_spawnattr_t * attrp, char *const argv[],
321 char *const envp[], int xflags)
322{
283d9851
AZ
323 /* It uses __execvpex to avoid run ENOEXEC in non compatibility mode (it
324 will be handled by maybe_script_execute). */
ccfb2964 325 return __spawnix (pid, file, acts, attrp, argv, envp, xflags,
283d9851 326 xflags & SPAWN_XFLAGS_USE_PATH ? __execvpex : __execve);
a5a6f926 327}