From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS31976 209.132.180.0/23 X-Spam-Status: No, score=-2.9 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_GMAIL_RCVD, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 4327B1F4B5 for ; Fri, 15 Nov 2019 18:27:45 +0000 (UTC) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-type; q=dns; s=default; b=Zk1I bQgY9uf+WjVgCSHc79ElV5/qsl/ZxpXDfoFo5zbwvcaJvBJ/1AM+IRNj8bvfGQXq 8V03olKwYVfM7iybtrbYE62daO90QyZO/HBE2N+7eMtFtyIMfD87E+NhKkGuH1PP hTWyX7LVp7JNd9FX9qJ+/x2l5xnyrW8f1jWDE6o= 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:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-type; s=default; bh=SG4UA75HrG spCsqj3U4VBdp2jOg=; b=JK+YLb1KtM5xAkIAytIuD0kG9Oe9wCVQq4np5ynu7R HCQyh3v89YvNZuZazeQw+/5R9RU0HH1yB9LfJXSLUiErYRz3mO+Xb+JRs36dDrCp +KPQ0JtzMPZPJ8uzb/LNBqdTu8sEywYvR8UKdg1oIa6tPG3Ei4Bj5OCKQpbUdfzq k= Received: (qmail 60510 invoked by alias); 15 Nov 2019 18:27:42 -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 Received: (qmail 60495 invoked by uid 89); 15 Nov 2019 18:27:42 -0000 Authentication-Results: sourceware.org; auth=none X-HELO: mail-lf1-f67.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=J1bvi4/UxNUE5hzE2LH5jUXxdJC4t6AJRn0i+3KRQKU=; b=asDBDxJ03on+ebQOqYadQWv2KIAh+LJtHaylrvRJy42RDr/L+VOYq+G5HigAdzrhpj r6QCIvysliz1lj6H4JBR9WHr+sJrELMsnwbdg8vDSwxgj8qMCBHK89H199gW9PIeaCeS hXdWxJmiceMbBg3cebPJZR9BoOMJqI7RYfOL9R1Hn2id/8+Lm3UGr96ZToHy1Ka2btUj NQqWKGyxJUhF298aHYb7uAbfzoqSJPqkghmTH5bDXBbppAQRJCWm7+mSRQ7U20xs+4zR I0ygic6rcfCAjR6au3zriKjlumZViGD2ns6aQNrHn5j0pbxeV68Usdpo+uJOgLaPzsj0 gxtA== MIME-Version: 1.0 References: <20191114144704.19002-1-adhemerval.zanella@linaro.org> <20191114144704.19002-5-adhemerval.zanella@linaro.org> In-Reply-To: <20191114144704.19002-5-adhemerval.zanella@linaro.org> From: Alistair Francis Date: Fri, 15 Nov 2019 13:27:12 -0500 Message-ID: Subject: Re: [PATCH 5/7] linux: Use waitid on wait4 if __NR_wait4 is not defined To: Adhemerval Zanella Cc: GNU C Library , Alistair Francis Content-Type: text/plain; charset="UTF-8" On Thu, Nov 14, 2019 at 9:47 AM Adhemerval Zanella wrote: > > If the wait4 syscall is not avaliable (such as y2038 safe 32-bit > systems) waitid should be used instead. However prior Linux 5.4 > waitid is not a fully superset of other wait instruction, since it > does not include support for waiting for the current process group. > > It would be possible to emulate to issue an extra syscall to get > the current process group, but it is inherent racy: after the current > process group is received and before it is passed to waitid a signal > could arrive causing the current process group to change > > So waitid is used if wait4 is not defined iff the build is > enabled with a minimum kernel if 5.4+. The new assume > __ASSUME_WAITID_PID0_P_PGID is added and an error is issued if waitid > can not be implemented by either __NR_wait4 or > __NR_waitid && __ASSUME_WAITID_PID0_P_PGID. > > Checked on x86_64-linux-gnu and i686-linux-gnu. > > Co-authored-by: Alistair Francis > --- > sysdeps/unix/sysv/linux/kernel-features.h | 6 ++ > sysdeps/unix/sysv/linux/syscalls.list | 1 - > sysdeps/unix/sysv/linux/wait4.c | 88 +++++++++++++++++++++++ > 3 files changed, 94 insertions(+), 1 deletion(-) > create mode 100644 sysdeps/unix/sysv/linux/wait4.c > > diff --git a/sysdeps/unix/sysv/linux/kernel-features.h b/sysdeps/unix/sysv/linux/kernel-features.h > index e6be76ff46..43faaa3f9d 100644 > --- a/sysdeps/unix/sysv/linux/kernel-features.h > +++ b/sysdeps/unix/sysv/linux/kernel-features.h > @@ -208,4 +208,10 @@ > # define __ASSUME_TIME64_SYSCALLS 1 > #endif > > +/* Linux waitid prior kernel 5.4 does not support waiting for the current > + process group. */ > +#if __LINUX_KERNEL_VERSION >= 0x050400 > +# define __ASSUME_WAITID_PID0_P_PGID > +#endif > + > #endif /* kernel-features.h */ > diff --git a/sysdeps/unix/sysv/linux/syscalls.list b/sysdeps/unix/sysv/linux/syscalls.list > index 603e517ca6..5f1352ad43 100644 > --- a/sysdeps/unix/sysv/linux/syscalls.list > +++ b/sysdeps/unix/sysv/linux/syscalls.list > @@ -67,7 +67,6 @@ swapoff - swapoff i:s __swapoff swapoff > unshare EXTRA unshare i:i unshare > uselib EXTRA uselib i:s __compat_uselib uselib@GLIBC_2.0:GLIBC_2.23 > utime - utime i:sP utime > -wait4 - wait4 i:iWiP __wait4 wait4 > > chown - chown i:sii __libc_chown __chown chown > > diff --git a/sysdeps/unix/sysv/linux/wait4.c b/sysdeps/unix/sysv/linux/wait4.c > new file mode 100644 > index 0000000000..d09917c7ff > --- /dev/null > +++ b/sysdeps/unix/sysv/linux/wait4.c > @@ -0,0 +1,88 @@ > +/* Wait for process to change state. Linux version. > + Copyright (C) 2019 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 > + > +pid_t > +__wait4 (pid_t pid, int *stat_loc, int options, struct rusage *usage) > +{ > +#if __NR_wait4 This needs to be #ifdef as __NR_wait4 doesn't exist on RV32. Alistair > + return SYSCALL_CANCEL (wait4, pid, stat_loc, options, usage); > +#elif defined (__ASSUME_WAITID_PID0_P_PGID) > + idtype_t idtype = P_PID; > + > + if (pid < -1) > + { > + idtype = P_PGID; > + pid *= -1; > + } > + else if (pid == -1) > + { > + idtype = P_ALL; > + } > + else if (pid == 0) > + { > + idtype = P_PGID; > + } > + > + options |= WEXITED; > + > + siginfo_t infop; > + if (SYSCALL_CANCEL (waitid, idtype, pid, &infop, options, usage) < 0) > + return -1; > + > + if (stat_loc) > + { > + switch (infop.si_code) > + { > + case CLD_EXITED: > + *stat_loc = W_EXITCODE (infop.si_status, 0); > + break; > + case CLD_DUMPED: > + *stat_loc = WCOREFLAG | infop.si_status; > + break; > + case CLD_KILLED: > + *stat_loc = infop.si_status; > + break; > + case CLD_TRAPPED: > + case CLD_STOPPED: > + *stat_loc = W_STOPCODE (infop.si_status); > + break; > + case CLD_CONTINUED: > + *stat_loc = __W_CONTINUED; > + break; > + default: > + *stat_loc = 0; > + break; > + } > + } > + > + return infop.si_pid; > +# else > +/* Linux waitid prior kernel 5.4 does not support waiting for the current > + process. It would be possible to emulate it by calling getpgid for pid 0, > + however, it would require an additional syscall and it is inherent racy: > + after the current process group is received and before it is passed > + to waitid a signal could arrive causing the current process group to > + change. */ > +# error "The kernel ABI does not provide a way to implement wait4" > +#endif > +} > +weak_alias (__wait4, wait4) > -- > 2.17.1 >