From patchwork Fri Oct 19 23:26:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 149357 Delivered-To: patch@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp3894543lji; Fri, 19 Oct 2018 16:26:47 -0700 (PDT) X-Google-Smtp-Source: ACcGV62FaTmoSJhU5cksMS3sfkHm9YNZ9MpEUAtAJOf7UhjeqJWMN0I+91uaJnRTuB0S2ogjbNrN X-Received: by 2002:a63:4745:: with SMTP id w5-v6mr35226324pgk.377.1539991607770; Fri, 19 Oct 2018 16:26:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539991607; cv=none; d=google.com; s=arc-20160816; b=h8okh5IQAbK/J7uELSk0CsPIJa2fkkV4DyKCtJcQfndff9gtg3o59APhb1/LoRQqUE LuetSrIubxASEK9SkKKcs1Cwzi5CHotXynznfKmnt4SkwOWkCpXHGrh6HjFKb9REO+/L Wz4a/tkcF8DI/teVpFZWmVrDaY/o/rNA+1OBtun/NULfl4knDDPP1HVOFbmVsToWjRVe o6c7INPMtCltURH8w4od1Vqy60YOfD+Zqqmd6RYYtMm0b1Tx2o+vct/hBDwWpzs8ZnPH 5EeKmKZ+wYiwKxykAOxdinH3TgVXQOTkleovA1GiHcwZ8t3OViVwdcssn2IQvScQxpl1 WpAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from :dkim-signature:delivered-to:sender:list-help:list-post:list-archive :list-subscribe:list-unsubscribe:list-id:precedence:mailing-list :dkim-signature:domainkey-signature; bh=6m1NQbD+uOOdGUdjtN/vSmJ0LUNFcEzgwrE5Q5wwGfY=; b=PzXaYrh+ebyWybc6JvH/0x4pLLvCxC1lQqhK9XK+2w9k4vOPOXpu39xIc5WI+lnqSw SZO+ACOx99yq/hjV6IMWKq4lqGXAddUUvO22YeSju7P3AMWEYJRNQ3J9hRhL1RZF5a6h 4Yn2G1j7ZqHcfT4V0nht/+biz8FdE5QCMW4Bs1AjptLGaXR+fJ7Atgmmx6vQGS4IV0e+ oQqw4CeK8MfidSWj613+Od8RyJm9hc0ytkhsHKtih79MlFrKDq2K08fpZrsPgPvJm7Ls Nlh8NSCge75cijoBVMdg9J2uB3pgFCiWaXs66lOujJXleWywLb8xF5HwqDibt4eilGqD 1KcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=OYXJ+lqW; dkim=pass header.i=@linaro.org header.s=google header.b=MlbDqjjm; spf=pass (google.com: domain of libc-alpha-return-96545-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-96545-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id 8-v6si26597319pfx.185.2018.10.19.16.26.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Oct 2018 16:26:47 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-96545-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=OYXJ+lqW; dkim=pass header.i=@linaro.org header.s=google header.b=MlbDqjjm; spf=pass (google.com: domain of libc-alpha-return-96545-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-96545-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=Vs/EoCcYxEY6fLYRYKHrF3/vQfbrFCg H56RoLat6dNyEMR/qllq/XZ0MDQMzlrvt2nke2lv/vly4mvraoBq6EZcpo1TY20S DntROplGHPN/uAWJZj1tdzXGboZUcKtXbGvgIrCisWoaGaSI0gIJy3Ic3bshgOX/ 5BN0RB4rdcOU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=vryvcprDjPorN5C+1Rc89mbA2aQ=; b=OYXJ+ lqWrOsNcDnc0DFOof8CnljecXltcxRm7NnUdNFTCLCvjlNa9kfWOeMYPmhsm72rA uXSAa0dBKxFF8Xs52ENGGbwyILVPD6mJKr4JbQNKItISBpVHrVOiBWaWiZYkVoiY SAYmeIkaaN7YvBcYsv8jIBFh1LreY7Un/mayUM= Received: (qmail 44583 invoked by alias); 19 Oct 2018 23:26:25 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 44474 invoked by uid 89); 19 Oct 2018 23:26:24 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=HX-Google-Smtp-Source:ACcGV60, 0x3, aims X-HELO: mail-qt1-f193.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references; bh=6m1NQbD+uOOdGUdjtN/vSmJ0LUNFcEzgwrE5Q5wwGfY=; b=MlbDqjjmiZBR1DosoVbUE4gnpIKTRvT/0PUsucBJ2tT0/uJjPami5tIUqchvS+TxQ3 T7/zDz9TmpaG8Xd7ci8UEFORviRvz7K7P/2fejkP8olNalSjpExvdVFKOstVk/xJGxnc iPjyewu16ovcG22ZiLVGR6J3DfmLu2wfVarvU= Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 3/3] posix: Use posix_spawn on system Date: Fri, 19 Oct 2018 20:26:09 -0300 Message-Id: <20181019232609.25531-3-adhemerval.zanella@linaro.org> In-Reply-To: <20181019232609.25531-1-adhemerval.zanella@linaro.org> References: <20181019232609.25531-1-adhemerval.zanella@linaro.org> Changed from previous version: - Call __libc_signal_block_app for SPAWN_XFLAGS_ENABLE_CANCEL. --- This patch uses posix_spawn on system implementation. On Linux this has the advantage of much lower memory consumption (usually 32 Kb minimum for the mmap stack area). Although POSIX does not require, glibc system implementation aims to be thread and cancellation safe. While reentracy handling does not require any direct change of current strategy, cancellation requires a posix_spawn to be cancellable. This is done by adding an internal __posix_spawn_cancellable which does not disable cancellation neither change process signal mask. The cancellation code is also moved to generic implementation and enabled only if SIGCANCEL is defined (similar on how the cancellation handler is enabled on nptl-init.c). Checked on x86_64-linux-gnu and i686-linux-gnu. * include/spawn.h (__posix_spawn_cancellable): New prototype. * posix/spawn.c (__posix_spawn_cancellable): New function. * posix/spawn_int.h (SPAWN_XFLAGS_ENABLE_CANCEL): New define. * sysdeps/generic/not-errno.h (__kill_noerrno): New prototype. * sysdeps/unix/sysv/linux/not-errno.h (__kill_noerrno): New implementation. * sysdeps/posix/spawni.c (__spawnix): Disable cancellation for !SPAWN_XFLAGS_ENABLE_CANCEL. * sysdeps/unix/sysv/linux/spawni.c (__spawnix): Likewise. * sysdeps/posix/system.c [_LIBC_REENTRANT && SIGCANCEL] (cancel_handler): New function. (CLEANUP_HANDLER, CLEANUP_RESET): Likewise. (do_system): Use posix_spawn instead of fork and execl. * sysdeps/unix/sysv/linux/ia64/system.c: Remove file. * sysdeps/unix/sysv/linux/s390/system.c: Likewise. * sysdeps/unix/sysv/linux/sparc/system.c: Likewise. * sysdeps/unix/sysv/linux/system.c: Likewise. --- ChangeLog | 18 ++++ include/spawn.h | 2 + posix/spawn.c | 14 +++ posix/spawn_int.h | 1 + sysdeps/generic/not-errno.h | 2 + sysdeps/posix/spawni.c | 8 +- sysdeps/posix/system.c | 132 ++++++++++++++----------- sysdeps/unix/sysv/linux/ia64/system.c | 30 ------ sysdeps/unix/sysv/linux/not-errno.h | 13 +++ sysdeps/unix/sysv/linux/s390/system.c | 29 ------ sysdeps/unix/sysv/linux/sparc/system.c | 29 ------ sysdeps/unix/sysv/linux/spawni.c | 22 +++-- sysdeps/unix/sysv/linux/system.c | 76 -------------- 13 files changed, 145 insertions(+), 231 deletions(-) delete mode 100644 sysdeps/unix/sysv/linux/ia64/system.c delete mode 100644 sysdeps/unix/sysv/linux/s390/system.c delete mode 100644 sysdeps/unix/sysv/linux/sparc/system.c delete mode 100644 sysdeps/unix/sysv/linux/system.c -- 2.17.1 diff --git a/include/spawn.h b/include/spawn.h index 7fdd965bd7..995ab9a8f4 100644 --- a/include/spawn.h +++ b/include/spawn.h @@ -5,6 +5,8 @@ __typeof (posix_spawn) __posix_spawn; libc_hidden_proto (__posix_spawn) +__typeof (posix_spawn) __posix_spawn_cancellable attribute_hidden; + __typeof (posix_spawn_file_actions_addclose) __posix_spawn_file_actions_addclose attribute_hidden; diff --git a/posix/spawn.c b/posix/spawn.c index a82f1c84e2..24a416f53c 100644 --- a/posix/spawn.c +++ b/posix/spawn.c @@ -33,6 +33,20 @@ versioned_symbol (libc, __posix_spawn, posix_spawn, GLIBC_2_15); libc_hidden_def (__posix_spawn) +/* Same as __posix_spawn but allows asynchronous cancellation. It is used + internally on SYSTEM implementation to avoid leak a process ID through + a cancellation handler. */ +int +__posix_spawn_cancellable (pid_t *pid, const char *path, + const posix_spawn_file_actions_t *file_actions, + const posix_spawnattr_t *attrp, char *const argv[], + char *const envp[]) +{ + return __spawni (pid, path, file_actions, attrp, argv, envp, + SPAWN_XFLAGS_ENABLE_CANCEL); +} + + #if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_15) int attribute_compat_text_section diff --git a/posix/spawn_int.h b/posix/spawn_int.h index 171f67c649..5cb8ce49b3 100644 --- a/posix/spawn_int.h +++ b/posix/spawn_int.h @@ -55,6 +55,7 @@ struct __spawn_action #define SPAWN_XFLAGS_USE_PATH 0x1 #define SPAWN_XFLAGS_TRY_SHELL 0x2 +#define SPAWN_XFLAGS_ENABLE_CANCEL 0x3 extern int __posix_spawn_file_actions_realloc (posix_spawn_file_actions_t * file_actions) diff --git a/sysdeps/generic/not-errno.h b/sysdeps/generic/not-errno.h index 93617a3266..0fd66b5c5e 100644 --- a/sysdeps/generic/not-errno.h +++ b/sysdeps/generic/not-errno.h @@ -17,3 +17,5 @@ . */ extern __typeof (__access) __access_noerrno attribute_hidden; + +extern __typeof (__kill) __kill_noerrno attribute_hidden; diff --git a/sysdeps/posix/spawni.c b/sysdeps/posix/spawni.c index b138ab4393..e1a391659b 100644 --- a/sysdeps/posix/spawni.c +++ b/sysdeps/posix/spawni.c @@ -256,8 +256,9 @@ __spawnix (pid_t *pid, const char *file, /* Disable asynchronous cancellation. */ int state; - __libc_ptf_call (__pthread_setcancelstate, - (PTHREAD_CANCEL_DISABLE, &state), 0); + if ((xflags & SPAWN_XFLAGS_ENABLE_CANCEL) == 0) + __libc_ptf_call (__pthread_setcancelstate, + (PTHREAD_CANCEL_DISABLE, &state), 0); ptrdiff_t argc = 0; ptrdiff_t limit = INT_MAX - 1; @@ -299,7 +300,8 @@ __spawnix (pid_t *pid, const char *file, if ((ec == 0) && (pid != NULL)) *pid = new_pid; - __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0); + if ((xflags & SPAWN_XFLAGS_ENABLE_CANCEL) == 0) + __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0); return ec; } diff --git a/sysdeps/posix/system.c b/sysdeps/posix/system.c index d7594436ed..38d5e28ca2 100644 --- a/sysdeps/posix/system.c +++ b/sysdeps/posix/system.c @@ -17,20 +17,22 @@ #include #include -#include #include #include +#include +#include +#include #include #include -#include -#include -#include +#include +#include +#include +#include #define SHELL_PATH "/bin/sh" /* Path of the shell. */ #define SHELL_NAME "sh" /* Name to give it. */ - #ifdef _LIBC_REENTRANT static struct sigaction intr, quit; static int sa_refcntr; @@ -38,17 +40,53 @@ __libc_lock_define_initialized (static, lock); # define DO_LOCK() __libc_lock_lock (lock) # define DO_UNLOCK() __libc_lock_unlock (lock) -# define INIT_LOCK() ({ __libc_lock_init (lock); sa_refcntr = 0; }) # define ADD_REF() sa_refcntr++ # define SUB_REF() --sa_refcntr + #else # define DO_LOCK() # define DO_UNLOCK() -# define INIT_LOCK() # define ADD_REF() 0 # define SUB_REF() 0 #endif +/* We have to and actually can handle cancelable system(). The big + problem: we have to kill the child process if necessary. To do + this a cleanup handler has to be registered and is has to be able + to find the PID of the child. The main problem is to reliable have + the PID when needed. It is not necessary for the parent thread to + return. It might still be in the kernel when the cancellation + request comes. Therefore we have to use the clone() calls ability + to have the kernel write the PID into the user-level variable. */ + +#if defined(_LIBC_REENTRANT) && defined(SIGCANCEL) +static void cancel_handler (void *arg) +{ + pid_t child = *(pid_t *) arg; + + __kill_noerrno (child, SIGKILL); + + TEMP_FAILURE_RETRY (__waitpid (child, NULL, 0)); + + DO_LOCK (); + + if (SUB_REF () == 0) + { + __sigaction (SIGQUIT, &quit, NULL); + __sigaction (SIGINT, &intr, NULL); + } + + DO_UNLOCK (); +} +# define CLEANUP_HANDLER() \ + __libc_cleanup_region_start (1, cancel_handler, &pid) +# define CLEANUP_RESET() \ + __libc_cleanup_region_end (0) +#else +# define CLEANUP_HANDLER() +# define CLEANUP_RESET() +#endif + /* Execute LINE as a shell command, returning its status. */ static int @@ -60,7 +98,9 @@ do_system (const char *line) #ifndef _LIBC_REENTRANT struct sigaction intr, quit; #endif + struct sigaction oldintr, oldquit; sigset_t omask; + sigset_t reset; sa.sa_handler = SIG_IGN; sa.sa_flags = 0; @@ -98,9 +138,9 @@ do_system (const char *line) if (SUB_REF () == 0) { save = errno; - (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL); + __sigaction (SIGQUIT, &quit, &oldquit); out_restore_sigint: - (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL); + __sigaction (SIGINT, &intr, &oldintr); __set_errno (save); } out: @@ -109,39 +149,30 @@ do_system (const char *line) } } -#ifdef CLEANUP_HANDLER - CLEANUP_HANDLER; -#endif - -#ifdef FORK - pid = FORK (); -#else - pid = __fork (); -#endif - if (pid == (pid_t) 0) - { - /* Child side. */ - const char *new_argv[4]; - new_argv[0] = SHELL_NAME; - new_argv[1] = "-c"; - new_argv[2] = line; - new_argv[3] = NULL; - - /* Restore the signals. */ - (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL); - (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL); - (void) __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL); - INIT_LOCK (); - - /* Exec the shell. */ - (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ); - _exit (127); - } - else if (pid < (pid_t) 0) - /* The fork failed. */ - status = -1; - else - /* Parent side. */ + __sigemptyset (&reset); + if (oldintr.sa_handler != SIG_IGN) + __sigaddset(&reset, SIGINT); + if (oldquit.sa_handler != SIG_IGN) + __sigaddset(&reset, SIGQUIT); + + posix_spawnattr_t spawn_attr; + /* None of the posix_spawnattr_* function returns, including + posix_spawnattr_setflags for the follow specific usage (using valid + flags). */ + __posix_spawnattr_init (&spawn_attr); + __posix_spawnattr_setsigmask (&spawn_attr, &omask); + __posix_spawnattr_setsigdefault (&spawn_attr, &reset); + __posix_spawnattr_setflags (&spawn_attr, + POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK); + + CLEANUP_HANDLER (); + status = __posix_spawn_cancellable (&pid, SHELL_PATH, 0, &spawn_attr, + (char *const[]){ (char*) SHELL_NAME, + (char*) "-c", + (char *) line, NULL }, + __environ); + __posix_spawnattr_destroy (&spawn_attr); + if (status == 0) { /* Note the system() is a cancellation point. But since we call waitpid() which itself is a cancellation point we do not @@ -149,25 +180,16 @@ do_system (const char *line) if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) != pid) status = -1; } - -#ifdef CLEANUP_HANDLER - CLEANUP_RESET; -#endif + CLEANUP_RESET (); save = errno; DO_LOCK (); if ((SUB_REF () == 0 - && (__sigaction (SIGINT, &intr, (struct sigaction *) NULL) - | __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL)) != 0) + && (__sigaction (SIGINT, &intr, NULL) + | __sigaction (SIGQUIT, &quit, NULL)) != 0) || __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL) != 0) { -#ifndef _LIBC - /* glibc cannot be used on systems without waitpid. */ - if (errno == ENOSYS) - __set_errno (save); - else -#endif - status = -1; + status = -1; } DO_UNLOCK (); diff --git a/sysdeps/unix/sysv/linux/ia64/system.c b/sysdeps/unix/sysv/linux/ia64/system.c deleted file mode 100644 index d09fefefe6..0000000000 --- a/sysdeps/unix/sysv/linux/ia64/system.c +++ /dev/null @@ -1,30 +0,0 @@ -/* Copyright (C) 2002-2018 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -/* We have to and actually can handle cancelable system(). The big - problem: we have to kill the child process if necessary. To do - this a cleanup handler has to be registered and is has to be able - to find the PID of the child. The main problem is to reliable have - the PID when needed. It is not necessary for the parent thread to - return. It might still be in the kernel when the cancellation - request comes. Therefore we have to use the clone() calls ability - to have the kernel write the PID into the user-level variable. */ -#define FORK() \ - INLINE_SYSCALL (clone2, 6, CLONE_PARENT_SETTID | SIGCHLD, NULL, 0, \ - &pid, NULL, NULL) - -#include diff --git a/sysdeps/unix/sysv/linux/not-errno.h b/sysdeps/unix/sysv/linux/not-errno.h index 106ba5c72e..4c5ae1a4c2 100644 --- a/sysdeps/unix/sysv/linux/not-errno.h +++ b/sysdeps/unix/sysv/linux/not-errno.h @@ -16,6 +16,8 @@ License along with the GNU C Library; if not, see . */ +#include + /* This function is used on maybe_enable_malloc_check (elf/dl-tunables.c) and to avoid having to build/use multiple versions if stack protection in enabled it is defined as inline. */ @@ -33,3 +35,14 @@ __access_noerrno (const char *pathname, int mode) return INTERNAL_SYSCALL_ERRNO (res, err); return 0; } + +static inline int +__kill_noerrno (pid_t pid, int sig) +{ + int res; + INTERNAL_SYSCALL_DECL (err); + res = INTERNAL_SYSCALL_CALL (kill, err, pid, sig); + if (INTERNAL_SYSCALL_ERROR_P (res, err)) + return INTERNAL_SYSCALL_ERRNO (res, err); + return 0; +} diff --git a/sysdeps/unix/sysv/linux/s390/system.c b/sysdeps/unix/sysv/linux/s390/system.c deleted file mode 100644 index d8ef461334..0000000000 --- a/sysdeps/unix/sysv/linux/s390/system.c +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright (C) 2003-2018 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -/* We have to and actually can handle cancelable system(). The big - problem: we have to kill the child process if necessary. To do - this a cleanup handler has to be registered and is has to be able - to find the PID of the child. The main problem is to reliable have - the PID when needed. It is not necessary for the parent thread to - return. It might still be in the kernel when the cancellation - request comes. Therefore we have to use the clone() calls ability - to have the kernel write the PID into the user-level variable. */ -#define FORK() \ - INLINE_SYSCALL (clone, 3, 0, CLONE_PARENT_SETTID | SIGCHLD, &pid) - -#include "../system.c" diff --git a/sysdeps/unix/sysv/linux/sparc/system.c b/sysdeps/unix/sysv/linux/sparc/system.c deleted file mode 100644 index 1f65c83399..0000000000 --- a/sysdeps/unix/sysv/linux/sparc/system.c +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright (C) 2003-2018 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -/* We have to and actually can handle cancelable system(). The big - problem: we have to kill the child process if necessary. To do - this a cleanup handler has to be registered and is has to be able - to find the PID of the child. The main problem is to reliable have - the PID when needed. It is not necessary for the parent thread to - return. It might still be in the kernel when the cancellation - request comes. Therefore we have to use the clone() calls ability - to have the kernel write the PID into the user-level variable. */ -#define FORK() \ - INLINE_CLONE_SYSCALL (CLONE_PARENT_SETTID | SIGCHLD, 0, &pid, NULL, NULL) - -#include "../system.c" diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c index 85239cedbf..7f17fb2f89 100644 --- a/sysdeps/unix/sysv/linux/spawni.c +++ b/sysdeps/unix/sysv/linux/spawni.c @@ -138,11 +138,11 @@ __spawni_child (void *arguments) for (int sig = 1; sig < _NSIG; ++sig) { if ((attr->__flags & POSIX_SPAWN_SETSIGDEF) - && sigismember (&attr->__sd, sig)) + && __sigismember (&attr->__sd, sig)) { sa.sa_handler = SIG_DFL; } - else if (sigismember (&hset, sig)) + else if (__sigismember (&hset, sig)) { if (__is_internal_signal (sig)) sa.sa_handler = SIG_IGN; @@ -330,10 +330,16 @@ __spawnix (pid_t * pid, const char *file, if (__glibc_unlikely (stack == MAP_FAILED)) return errno; - /* Disable asynchronous cancellation. */ int state; - __libc_ptf_call (__pthread_setcancelstate, - (PTHREAD_CANCEL_DISABLE, &state), 0); + if (xflags & SPAWN_XFLAGS_ENABLE_CANCEL) + __libc_signal_block_app (&args.oldmask); + else + { + /* Disable asynchronous cancellation. */ + __libc_ptf_call (__pthread_setcancelstate, + (PTHREAD_CANCEL_DISABLE, &state), 0); + __libc_signal_block_all (&args.oldmask); + } /* Child must set args.err to something non-negative - we rely on the parent and child sharing VM. */ @@ -347,8 +353,6 @@ __spawnix (pid_t * pid, const char *file, args.envp = envp; args.xflags = xflags; - __libc_signal_block_all (&args.oldmask); - /* 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 suspend until the child calls execve or _exit. @@ -390,8 +394,8 @@ __spawnix (pid_t * pid, const char *file, *pid = new_pid; __libc_signal_restore_set (&args.oldmask); - - __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0); + if ((xflags & SPAWN_XFLAGS_ENABLE_CANCEL) == 0) + __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0); return ec; } diff --git a/sysdeps/unix/sysv/linux/system.c b/sysdeps/unix/sysv/linux/system.c deleted file mode 100644 index 7cc68a1528..0000000000 --- a/sysdeps/unix/sysv/linux/system.c +++ /dev/null @@ -1,76 +0,0 @@ -/* Copyright (C) 2002-2018 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include -#include /* For the real memset prototype. */ -#include -#include -#include -#include - -/* We have to and actually can handle cancelable system(). The big - problem: we have to kill the child process if necessary. To do - this a cleanup handler has to be registered and is has to be able - to find the PID of the child. The main problem is to reliable have - the PID when needed. It is not necessary for the parent thread to - return. It might still be in the kernel when the cancellation - request comes. Therefore we have to use the clone() calls ability - to have the kernel write the PID into the user-level variable. */ -#ifndef FORK -# define FORK() \ - INLINE_SYSCALL (clone, 3, CLONE_PARENT_SETTID | SIGCHLD, 0, &pid) -#endif - -#ifdef _LIBC_REENTRANT -static void cancel_handler (void *arg); - -# define CLEANUP_HANDLER \ - __libc_cleanup_region_start (1, cancel_handler, &pid) - -# define CLEANUP_RESET \ - __libc_cleanup_region_end (0) -#endif - - -/* Linux has waitpid(), so override the generic unix version. */ -#include - - -#ifdef _LIBC_REENTRANT -/* The cancellation handler. */ -static void -cancel_handler (void *arg) -{ - pid_t child = *(pid_t *) arg; - - INTERNAL_SYSCALL_DECL (err); - INTERNAL_SYSCALL (kill, err, 2, child, SIGKILL); - - TEMP_FAILURE_RETRY (__waitpid (child, NULL, 0)); - - DO_LOCK (); - - if (SUB_REF () == 0) - { - (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL); - (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL); - } - - DO_UNLOCK (); -} -#endif