mirror of
https://sourceware.org/git/glibc.git
synced 2025-08-07 06:43:00 +03:00
linux: spawni.c: simplify error reporting to parent
Using CLONE_VFORK already ensures that the parent does not run until the child has either exec'ed succesfully or called _exit. Hence we don't need to read from a CLOEXEC pipe to ensure proper synchronization - we just make explicit use of the fact the the child and parent run in the same VM, so the child can write an error code to a field of the posix_spawn_args struct instead of sending it through a pipe. To ensure that this mechanism really works, the parent initializes the field to -1 and the child writes 0 before execing. This eliminates some annoying bookkeeping that is necessary to avoid the file actions from clobbering the write end of the pipe, and getting rid of the pipe creation in the first place means fewer system calls (four in the parent, usually one in the child) and fewer chanches for the spawn to fail (e.g. if we're close to EMFILE). Checked on x86_64 and i686. * sysdeps/unix/sysv/linux/spawni.c (posix_spawn_args): Remove pipe field, add err field. (__spawni_child): Report error through err member instead of pipe. (__spawnix): Likewise.
This commit is contained in:
committed by
Adhemerval Zanella
parent
8d3bd94748
commit
4b4d4056bb
@@ -1,3 +1,10 @@
|
|||||||
|
2016-09-28 Rasmus Villemoes <rv@rasmusvillemoes.dk>
|
||||||
|
|
||||||
|
* sysdeps/unix/sysv/linux/spawni.c (posix_spawn_args): Remove pipe
|
||||||
|
field, add err field.
|
||||||
|
(__spawni_child): Report error through err member instead of pipe.
|
||||||
|
(__spawnix): Likewise.
|
||||||
|
|
||||||
2016-09-28 Zack Weinberg <zackw@panix.com>
|
2016-09-28 Zack Weinberg <zackw@panix.com>
|
||||||
|
|
||||||
* scripts/check-installed-headers.sh: Generalize treatment of
|
* scripts/check-installed-headers.sh: Generalize treatment of
|
||||||
|
@@ -17,6 +17,7 @@
|
|||||||
<http://www.gnu.org/licenses/>. */
|
<http://www.gnu.org/licenses/>. */
|
||||||
|
|
||||||
#include <spawn.h>
|
#include <spawn.h>
|
||||||
|
#include <assert.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <paths.h>
|
#include <paths.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -44,11 +45,12 @@
|
|||||||
3. Child must synchronize with parent to enforce 2. and to possible
|
3. Child must synchronize with parent to enforce 2. and to possible
|
||||||
return execv issues.
|
return execv issues.
|
||||||
|
|
||||||
The first issue is solved by blocking all signals in child, even the
|
The first issue is solved by blocking all signals in child, even
|
||||||
NPTL-internal ones (SIGCANCEL and SIGSETXID). The second and third issue
|
the NPTL-internal ones (SIGCANCEL and SIGSETXID). The second and
|
||||||
is done by a stack allocation in parent and a synchronization with using
|
third issue is done by a stack allocation in parent, and by using a
|
||||||
a pipe or waitpid (in case or error). The pipe has the advantage of
|
field in struct spawn_args where the child can write an error
|
||||||
allowing the child the communicate an exec error. */
|
code. CLONE_VFORK ensures that the parent does not run until the
|
||||||
|
child has either exec'ed successfully or exited. */
|
||||||
|
|
||||||
|
|
||||||
/* The Unix standard contains a long explanation of the way to signal
|
/* The Unix standard contains a long explanation of the way to signal
|
||||||
@@ -75,7 +77,6 @@
|
|||||||
|
|
||||||
struct posix_spawn_args
|
struct posix_spawn_args
|
||||||
{
|
{
|
||||||
int pipe[2];
|
|
||||||
sigset_t oldmask;
|
sigset_t oldmask;
|
||||||
const char *file;
|
const char *file;
|
||||||
int (*exec) (const char *, char *const *, char *const *);
|
int (*exec) (const char *, char *const *, char *const *);
|
||||||
@@ -85,6 +86,7 @@ struct posix_spawn_args
|
|||||||
ptrdiff_t argc;
|
ptrdiff_t argc;
|
||||||
char *const *envp;
|
char *const *envp;
|
||||||
int xflags;
|
int xflags;
|
||||||
|
int err;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Older version requires that shell script without shebang definition
|
/* Older version requires that shell script without shebang definition
|
||||||
@@ -121,11 +123,8 @@ __spawni_child (void *arguments)
|
|||||||
struct posix_spawn_args *args = arguments;
|
struct posix_spawn_args *args = arguments;
|
||||||
const posix_spawnattr_t *restrict attr = args->attr;
|
const posix_spawnattr_t *restrict attr = args->attr;
|
||||||
const posix_spawn_file_actions_t *file_actions = args->fa;
|
const posix_spawn_file_actions_t *file_actions = args->fa;
|
||||||
int p = args->pipe[1];
|
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
close_not_cancel (args->pipe[0]);
|
|
||||||
|
|
||||||
/* The child must ensure that no signal handler are enabled because it shared
|
/* The child must ensure that no signal handler are enabled because it shared
|
||||||
memory with parent, so the signal disposition must be either SIG_DFL or
|
memory with parent, so the signal disposition must be either SIG_DFL or
|
||||||
SIG_IGN. It does by iterating over all signals and although it could
|
SIG_IGN. It does by iterating over all signals and although it could
|
||||||
@@ -199,17 +198,6 @@ __spawni_child (void *arguments)
|
|||||||
{
|
{
|
||||||
struct __spawn_action *action = &file_actions->__actions[cnt];
|
struct __spawn_action *action = &file_actions->__actions[cnt];
|
||||||
|
|
||||||
/* Dup the pipe fd onto an unoccupied one to avoid any file
|
|
||||||
operation to clobber it. */
|
|
||||||
if ((action->action.close_action.fd == p)
|
|
||||||
|| (action->action.open_action.fd == p)
|
|
||||||
|| (action->action.dup2_action.fd == p))
|
|
||||||
{
|
|
||||||
if ((ret = __dup (p)) < 0)
|
|
||||||
goto fail;
|
|
||||||
p = ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (action->tag)
|
switch (action->tag)
|
||||||
{
|
{
|
||||||
case spawn_do_close:
|
case spawn_do_close:
|
||||||
@@ -269,6 +257,7 @@ __spawni_child (void *arguments)
|
|||||||
__sigprocmask (SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
|
__sigprocmask (SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
|
||||||
? &attr->__ss : &args->oldmask, 0);
|
? &attr->__ss : &args->oldmask, 0);
|
||||||
|
|
||||||
|
args->err = 0;
|
||||||
args->exec (args->file, args->argv, args->envp);
|
args->exec (args->file, args->argv, args->envp);
|
||||||
|
|
||||||
/* This is compatibility function required to enable posix_spawn run
|
/* This is compatibility function required to enable posix_spawn run
|
||||||
@@ -276,14 +265,13 @@ __spawni_child (void *arguments)
|
|||||||
(2.15). */
|
(2.15). */
|
||||||
maybe_script_execute (args);
|
maybe_script_execute (args);
|
||||||
|
|
||||||
ret = -errno;
|
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
/* Since sizeof errno < PIPE_BUF, the write is atomic. */
|
/* errno should have an appropriate non-zero value; otherwise,
|
||||||
ret = -ret;
|
there's a bug in glibc or the kernel. For lack of an error code
|
||||||
if (ret)
|
(EINTERNALBUG) describing that, use ECHILD. Another option would
|
||||||
while (write_not_cancel (p, &ret, sizeof ret) < 0)
|
be to set args->err to some negative sentinel and have the parent
|
||||||
continue;
|
abort(), but that seems needlessly harsh. */
|
||||||
|
args->err = errno ? : ECHILD;
|
||||||
_exit (SPAWN_ERROR);
|
_exit (SPAWN_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,9 +288,6 @@ __spawnix (pid_t * pid, const char *file,
|
|||||||
struct posix_spawn_args args;
|
struct posix_spawn_args args;
|
||||||
int ec;
|
int ec;
|
||||||
|
|
||||||
if (__pipe2 (args.pipe, O_CLOEXEC))
|
|
||||||
return errno;
|
|
||||||
|
|
||||||
/* To avoid imposing hard limits on posix_spawn{p} the total number of
|
/* To avoid imposing hard limits on posix_spawn{p} the total number of
|
||||||
arguments is first calculated to allocate a mmap to hold all possible
|
arguments is first calculated to allocate a mmap to hold all possible
|
||||||
values. */
|
values. */
|
||||||
@@ -329,17 +314,16 @@ __spawnix (pid_t * pid, const char *file,
|
|||||||
void *stack = __mmap (NULL, stack_size, prot,
|
void *stack = __mmap (NULL, stack_size, prot,
|
||||||
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
|
||||||
if (__glibc_unlikely (stack == MAP_FAILED))
|
if (__glibc_unlikely (stack == MAP_FAILED))
|
||||||
{
|
return errno;
|
||||||
close_not_cancel (args.pipe[0]);
|
|
||||||
close_not_cancel (args.pipe[1]);
|
|
||||||
return errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Disable asynchronous cancellation. */
|
/* Disable asynchronous cancellation. */
|
||||||
int state;
|
int state;
|
||||||
__libc_ptf_call (__pthread_setcancelstate,
|
__libc_ptf_call (__pthread_setcancelstate,
|
||||||
(PTHREAD_CANCEL_DISABLE, &state), 0);
|
(PTHREAD_CANCEL_DISABLE, &state), 0);
|
||||||
|
|
||||||
|
/* Child must set args.err to something non-negative - we rely on
|
||||||
|
the parent and child sharing VM. */
|
||||||
|
args.err = -1;
|
||||||
args.file = file;
|
args.file = file;
|
||||||
args.exec = exec;
|
args.exec = exec;
|
||||||
args.fa = file_actions;
|
args.fa = file_actions;
|
||||||
@@ -353,9 +337,8 @@ __spawnix (pid_t * pid, const char *file,
|
|||||||
|
|
||||||
/* The clone flags used will create a new child that will run in the same
|
/* The clone flags used will create a new child that will run in the same
|
||||||
memory space (CLONE_VM) and the execution of calling thread will be
|
memory space (CLONE_VM) and the execution of calling thread will be
|
||||||
suspend until the child calls execve or _exit. These condition as
|
suspend until the child calls execve or _exit.
|
||||||
signal below either by pipe write (_exit with SPAWN_ERROR) or
|
|
||||||
a successful execve.
|
|
||||||
Also since the calling thread execution will be suspend, there is not
|
Also since the calling thread execution will be suspend, there is not
|
||||||
need for CLONE_SETTLS. Although parent and child share the same TLS
|
need for CLONE_SETTLS. Although parent and child share the same TLS
|
||||||
namespace, there will be no concurrent access for TLS variables (errno
|
namespace, there will be no concurrent access for TLS variables (errno
|
||||||
@@ -363,22 +346,18 @@ __spawnix (pid_t * pid, const char *file,
|
|||||||
new_pid = CLONE (__spawni_child, STACK (stack, stack_size), stack_size,
|
new_pid = CLONE (__spawni_child, STACK (stack, stack_size), stack_size,
|
||||||
CLONE_VM | CLONE_VFORK | SIGCHLD, &args);
|
CLONE_VM | CLONE_VFORK | SIGCHLD, &args);
|
||||||
|
|
||||||
close_not_cancel (args.pipe[1]);
|
|
||||||
|
|
||||||
if (new_pid > 0)
|
if (new_pid > 0)
|
||||||
{
|
{
|
||||||
if (__read (args.pipe[0], &ec, sizeof ec) != sizeof ec)
|
ec = args.err;
|
||||||
ec = 0;
|
assert (ec >= 0);
|
||||||
else
|
if (ec != 0)
|
||||||
__waitpid (new_pid, NULL, 0);
|
__waitpid (new_pid, NULL, 0);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
ec = -new_pid;
|
ec = -new_pid;
|
||||||
|
|
||||||
__munmap (stack, stack_size);
|
__munmap (stack, stack_size);
|
||||||
|
|
||||||
close_not_cancel (args.pipe[0]);
|
|
||||||
|
|
||||||
if ((ec == 0) && (pid != NULL))
|
if ((ec == 0) && (pid != NULL))
|
||||||
*pid = new_pid;
|
*pid = new_pid;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user