From patchwork Tue May 23 13:27:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 69891 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6C7BA3856DFB for ; Tue, 23 May 2023 13:28:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6C7BA3856DFB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684848509; bh=0Y9Bl+/UE5oCCEZv9RD8POa4bbSTL86mvFgDEpiUgpk=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=X1u8b+aSLUaGoy5FRCVeN+Bjswr8QNDl+HqylLDHAoSrgTyoOayrOhaK6T0N6GdZX N+3Ohj9PlHESC6IjAd34Jew8yg2rRlyO6WraWJ43WHSqlgK2cZQC/vyvbs9PZc33yn KIQUzAE8C7daHX8NGIjTtarJG80FZXaWUsR4zi7g= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id C15CC385841D for ; Tue, 23 May 2023 13:28:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C15CC385841D Received: by mail-ot1-x331.google.com with SMTP id 46e09a7af769-6af754a2f2cso2032336a34.1 for ; Tue, 23 May 2023 06:28:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684848481; x=1687440481; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0Y9Bl+/UE5oCCEZv9RD8POa4bbSTL86mvFgDEpiUgpk=; b=Vk1J+8WJak7y7SthbpVsTUvM0PDLMHsNQGBLl9NUilWK8IFc/sEJmw4aGTrMZKoB0o MbDwwD9dtas+/FNUIJRnxsJQsdhYnVjTcUNXnAf6gPEFzunZl1wp6MNn21N+2i0HVt8x WwkU40ZkxvF6TmWvvr2E2ZnYOIFcMjbu6cs7xz5Po/Qad8iZI7zAuLg+74Lj4PopZCn2 JTldTMFzPSnFDq46QjTvJDMEWtOo+El5SyMM7nWaSmExFHAs0Bvyvg6c/TuwVa+t9dKD AT7mIKrTisklYsk+e9kfMeNejWMK6J6vPHG7FlJDV3O3/M/2ZGiCAGOz/0n8dq3z7V1/ RhZQ== X-Gm-Message-State: AC+VfDyuO0gVfxvih8zB7ye8B+lC4wKaSYiUtLZ1HIgpfZ9aAdfEV0Uq Erb6Fi9j5Hig/i78IZOs9gwZSr50h9OOwZCfi+YN1w== X-Google-Smtp-Source: ACHHUZ5Eg4YPSMM9lNw7ERVrsSuIlMELW8PMWcWXOA9+rWUs5AsY2EZY+YljTPDdI9OZHF3BqItqcw== X-Received: by 2002:a05:6830:1394:b0:6af:78cc:e489 with SMTP id d20-20020a056830139400b006af78cce489mr3507871otq.26.1684848481378; Tue, 23 May 2023 06:28:01 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:2e05:abff:50e6:a79d:43f4]) by smtp.gmail.com with ESMTPSA id t3-20020a9d5903000000b006a43519523fsm3397047oth.1.2023.05.23.06.27.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 May 2023 06:28:00 -0700 (PDT) To: libc-alpha@sourceware.org, Luca Boccassi , Florian Weimer , Philip Withnall Subject: [PATCH v5 3/4] posix: Add PIDFDFORK_NOSIGCHLD for pidfd_fork Date: Tue, 23 May 2023 10:27:48 -0300 Message-Id: <20230523132749.1646124-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230523132749.1646124-1-adhemerval.zanella@linaro.org> References: <20230523132749.1646124-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" It clones the process without setting SIGCHLD as the termination signal. When using this flag, the parent process must specify the __WALL or __WCLONE Linux specific options when waiting for the child with wait or waitid. Checked on x86_64-linux-gnu and i686-linux-gnu. --- manual/process.texi | 6 ++++ sysdeps/nptl/_Fork.c | 2 +- sysdeps/unix/sysv/linux/arch-fork.h | 2 +- sysdeps/unix/sysv/linux/pidfd_fork.c | 13 ++++--- sysdeps/unix/sysv/linux/sys/pidfd.h | 2 ++ sysdeps/unix/sysv/linux/tst-pidfd_fork.c | 45 ++++++++++++++++++++++-- 6 files changed, 61 insertions(+), 9 deletions(-) diff --git a/manual/process.texi b/manual/process.texi index 9675cdc486..80fdc808f0 100644 --- a/manual/process.texi +++ b/manual/process.texi @@ -386,6 +386,12 @@ following flags: Acts as @code{_Fork}, where it does not invoke any callbacks registered with @code{pthread_atfork}, nor does it reset internal state or locks (such as the @code{malloc} locks). + +@item PIDFDFORK_NOSIGCHLD +Do not send a @code{SIGCHLD} termination signal when child terminates. +@strong{NB:} When using this flag, the parent process must specify the +@code{__WALL} or @code{__WCLONE} options when waiting for the child with +@code{wait} or @code{waitid}. @end table @end deftypefun diff --git a/sysdeps/nptl/_Fork.c b/sysdeps/nptl/_Fork.c index aa99e05b5b..397f059fb0 100644 --- a/sysdeps/nptl/_Fork.c +++ b/sysdeps/nptl/_Fork.c @@ -22,7 +22,7 @@ pid_t _Fork (void) { - pid_t pid = arch_fork (0, NULL, &THREAD_SELF->tid); + pid_t pid = arch_fork (SIGCHLD, NULL, &THREAD_SELF->tid); if (pid == 0) { struct pthread *self = THREAD_SELF; diff --git a/sysdeps/unix/sysv/linux/arch-fork.h b/sysdeps/unix/sysv/linux/arch-fork.h index 9e8a449e2c..f978d4c4f4 100644 --- a/sysdeps/unix/sysv/linux/arch-fork.h +++ b/sysdeps/unix/sysv/linux/arch-fork.h @@ -35,7 +35,7 @@ static inline pid_t arch_fork (int flags, void *ptid, void *ctid) { long int ret; - flags |= CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD; + flags |= CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID; #ifdef __ASSUME_CLONE_BACKWARDS # ifdef INLINE_CLONE_SYSCALL ret = INLINE_CLONE_SYSCALL (flags, 0, ptid, 0, ctid); diff --git a/sysdeps/unix/sysv/linux/pidfd_fork.c b/sysdeps/unix/sysv/linux/pidfd_fork.c index 16d55faea1..341b42fd4b 100644 --- a/sysdeps/unix/sysv/linux/pidfd_fork.c +++ b/sysdeps/unix/sysv/linux/pidfd_fork.c @@ -23,9 +23,10 @@ #include static pid_t -forkfd (int *pidfd) +forkfd (int *pidfd, bool nosigchld) { - int flags = pidfd == NULL ? 0 : CLONE_PIDFD; + int flags = (pidfd == NULL ? 0 : CLONE_PIDFD) + | (nosigchld ? 0 : SIGCHLD); pid_t pid = arch_fork (flags, pidfd, &THREAD_SELF->tid); if (pid == 0) { @@ -49,9 +50,11 @@ pidfd_fork (int *pidfd, unsigned int flags) if (!__clone_pidfd_supported ()) return INLINE_SYSCALL_ERROR_RETURN_VALUE (ENOSYS); - if (flags & ~(PIDFDFORK_ASYNCSAFE)) + if (flags & ~(PIDFDFORK_ASYNCSAFE | PIDFDFORK_NOSIGCHLD)) return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL); + bool nosigchld = flags & PIDFDFORK_NOSIGCHLD; + pid_t pid; if (!(flags & PIDFDFORK_ASYNCSAFE)) { @@ -62,7 +65,7 @@ pidfd_fork (int *pidfd, unsigned int flags) struct nss_database_data nss_database_data; state.lastrun = __fork_pre (multiple_threads, &nss_database_data); - state.pid = forkfd (pidfd); + state.pid = forkfd (pidfd, nosigchld); /* It follow the usual fork semantic, where a positive or negative value is returned to parent, and 0 for the child. */ __fork_post (&state, &nss_database_data); @@ -70,7 +73,7 @@ pidfd_fork (int *pidfd, unsigned int flags) pid = state.pid; } else - pid = forkfd (pidfd); + pid = forkfd (pidfd, nosigchld); return pid; } diff --git a/sysdeps/unix/sysv/linux/sys/pidfd.h b/sysdeps/unix/sysv/linux/sys/pidfd.h index 94d06bad76..451718aebe 100644 --- a/sysdeps/unix/sysv/linux/sys/pidfd.h +++ b/sysdeps/unix/sysv/linux/sys/pidfd.h @@ -49,6 +49,8 @@ extern int pidfd_send_signal (int __pidfd, int __sig, siginfo_t *__info, /* Do not issue the pthread_atfork on pidfd_fork. */ #define PIDFDFORK_ASYNCSAFE (1U << 1) +/* Do not send a SIGCHLD termination signal. */ +#define PIDFDFORK_NOSIGCHLD (1U << 2) /* Clone the calling process, creating an exact copy and return a file descriptor that can be used along other pidfd functions. diff --git a/sysdeps/unix/sysv/linux/tst-pidfd_fork.c b/sysdeps/unix/sysv/linux/tst-pidfd_fork.c index 329750f506..dfd75afe7c 100644 --- a/sysdeps/unix/sysv/linux/tst-pidfd_fork.c +++ b/sysdeps/unix/sysv/linux/tst-pidfd_fork.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -33,6 +34,14 @@ static bool atfork_prepare_var; static bool atfork_parent_var; static bool atfork_child_var; +static sig_atomic_t sigchld_called; + +static void +sigchld_handler (int sig) +{ + sigchld_called = 1; +} + static void atfork_prepare (void) { @@ -69,6 +78,9 @@ singlethread_test (unsigned int flags, bool wait_with_pid) off_t off = xlseek (tempfd, 0, SEEK_CUR); TEST_COMPARE (off, testdatalen1); + bool check_nosigchld = flags & PIDFDFORK_NOSIGCHLD; + sigchld_called = 0; + int pidfd; pid_t pid = pidfd_fork (&pidfd, flags); TEST_VERIFY_EXIT (pid != -1); @@ -100,13 +112,18 @@ singlethread_test (unsigned int flags, bool wait_with_pid) { siginfo_t sinfo; + int options = WEXITED | (check_nosigchld ? __WCLONE : 0); if (wait_with_pid) - TEST_COMPARE (waitid (P_PID, pid, &sinfo, WEXITED), 0); + TEST_COMPARE (waitid (P_PID, pid, &sinfo, options), 0); else - TEST_COMPARE (waitid (P_PIDFD, pidfd, &sinfo, WEXITED), 0); + TEST_COMPARE (waitid (P_PIDFD, pidfd, &sinfo, options), 0); TEST_COMPARE (sinfo.si_signo, SIGCHLD); TEST_COMPARE (sinfo.si_code, CLD_EXITED); TEST_COMPARE (sinfo.si_status, 0); + + /* If PIDFDFORK_NOSIGCHLD is specified no SIGCHLD should be sent by the + kernel. */ + TEST_COMPARE (sigchld_called, check_nosigchld ? 0 : 1); } TEST_COMPARE (xlseek (tempfd, 0, SEEK_CUR), testdatalen2); @@ -140,6 +157,14 @@ do_test (void) TEST_COMPARE (sinfo.si_status, 0); } + { + struct sigaction sa; + sa.sa_handler = sigchld_handler; + sa.sa_flags = 0; + sigemptyset (&sa.sa_mask); + xsigaction (SIGCHLD, &sa, NULL); + } + pthread_atfork (atfork_prepare, atfork_parent, atfork_child); /* With default flags, pidfd_fork acts as fork and run the pthread_atfork @@ -161,6 +186,14 @@ do_test (void) TEST_VERIFY (!atfork_child_var); } + { + atfork_prepare_var = atfork_parent_var = atfork_child_var = false; + singlethread_test (PIDFDFORK_NOSIGCHLD, false); + TEST_VERIFY (atfork_prepare_var); + TEST_VERIFY (atfork_parent_var); + TEST_VERIFY (!atfork_child_var); + } + /* With PIDFDFORK_ASYNCSAFE, pidfd_fork acts as _Fork. */ { atfork_prepare_var = atfork_parent_var = atfork_child_var = false; @@ -180,6 +213,14 @@ do_test (void) TEST_VERIFY (!atfork_child_var); } + { + atfork_prepare_var = atfork_parent_var = atfork_child_var = false; + singlethread_test (PIDFDFORK_NOSIGCHLD | PIDFDFORK_ASYNCSAFE, true); + TEST_VERIFY (!atfork_prepare_var); + TEST_VERIFY (!atfork_parent_var); + TEST_VERIFY (!atfork_child_var); + } + return 0; }