From patchwork Thu Nov 14 14:47:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 179440 Delivered-To: patch@linaro.org Received: by 2002:a92:38d5:0:0:0:0:0 with SMTP id g82csp11156109ilf; Thu, 14 Nov 2019 06:48:07 -0800 (PST) X-Google-Smtp-Source: APXvYqyjXKOKO7UTyZxnNB8LyP7n/RWXBzjwkpkfn8P/nFYFKGGAj3MOShYxOLnEOPbmM2wQ2WqX X-Received: by 2002:a50:ef16:: with SMTP id m22mr1685853eds.154.1573742887283; Thu, 14 Nov 2019 06:48:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573742887; cv=none; d=google.com; s=arc-20160816; b=gw+m/sCSvwsKHAVeoAPRb/QbJfkVEjRAADoUkhfLi/lvyzOulreZk40xhxzHE4kvRq q/5wfrhoHQFnkh3c0zaKwysmE6bBaQ0UrxUmDn8/mdvwL39BjEe6yZs/K5XaB3i6zzxZ 2t7vPd89/gKm6pn21EO/ReVIONUEOVWYRjVvh95T+PYS/MnQllQff35hVrGoZ3lIzzIY bw6PzO6oxCgyr8QTKe2zs5tWT5llhFgHWIO4L13SM35g8FUc8eChWW9jsbTjX/pYpZpm 4w8FdOmTBff2WlDV0CvB1QC6+zIiBk6hzYt/H0HFN+jpbD6oRPldDMq6klL1kceZ38wF ZGdQ== 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:cc: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=sBN0cA4LyxPGOCHH5tUTr/M8ODXZLTgrNQdVi5Nk3SU=; b=FtQGkzU4URW4qwVG4nxRAUX/hifau4+xLejbXiNFcbh8hMxt/usF59D7jDyOy+to9N v9A8FxJJIvLuWzCD28PDt/xIfL7Uv2FE35NI4UARvL98LK/GE0Yk7d+OWcKfHk32s6K4 xgMUZBeQqMj14r5L/UxMCFOdrNoRlZHJCaN+1Tr7l4OH76J+gH24Qc/Hj6x3qYEzuXyG CpWJbug9sLIzXU26/WNM9xUbhdQgNvHe+2yh1jhcCTv4o3fTLWUvIiu62T9mNl8lN3X3 qeJJMiyt56lduXAx+pKsabERxGJckcDJusDCihwvco8lQ2tRg7GX8sbcV90+gWi3Ki0k 3Bzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="j/4dSo1/"; dkim=pass header.i=@linaro.org header.s=google header.b=dU7fmIAp; spf=pass (google.com: domain of libc-alpha-return-107084-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-107084-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 t8si4597489edd.373.2019.11.14.06.48.06 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Nov 2019 06:48:07 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-107084-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="j/4dSo1/"; dkim=pass header.i=@linaro.org header.s=google header.b=dU7fmIAp; spf=pass (google.com: domain of libc-alpha-return-107084-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-107084-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:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=lKECAxhMDChe8iJSOBikePu7WPHVPVo SZKVXngfZVmYx05T4gMYC5j9kmfx1C/+Do5GbQmPdgmWfZzs761/wSBCyKDndaMv 4CjG8u7OapW8vWt8cTaMDWyBUP8ALQL5xNUGXuAp5/9il/rpNxnGozv1zejRXGtQ ydDbGZ6NnPbw= 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:cc:subject:date:message-id:in-reply-to :references; s=default; bh=cJqLDwiZYe52udIDj+OqMpL0N/0=; b=j/4dS o1/4AUm/9HTa8kRGl9uEEFdPK8KwtXTlDDeJH9TcDj1km/IO4eI9ZxuJmvDvExSh ewMdy/Lp0FREkcwX/HegCc9WryONT9slK1m36l2asy54U7OeD1qUt0FuYB3LB7DE iUa+Tz6ozVTG14cES3hsap1eJlx9MS2sgK9QTg= Received: (qmail 30021 invoked by alias); 14 Nov 2019 14:47:22 -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 29961 invoked by uid 89); 14 Nov 2019 14:47:22 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.6 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.1 spammy= X-HELO: mail-qk1-f193.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=sBN0cA4LyxPGOCHH5tUTr/M8ODXZLTgrNQdVi5Nk3SU=; b=dU7fmIApcrzVVy86UdDRdBFEzONebVkhHDLQwGX3IWRiQc9sXL8IIcZEuuRJU6Jwvc lJGWyJ/MjHok2iRxLhMyEpM86pfQX+zpLfD2hwApuzze+mulNgyUG9K+9QSZ4RVHkSq5 tCj79sG5UGo+tAyaCdMSGnBhMb5eFK9AraxKTe8W9o9vKQUv2Ihs24M5LKxsbIwlSPc0 GSu29CjYVnUFDljsPvnGKievS6atoSIWla5nGPNR2W9uvRoBwjypkvAIR97fgwL1bPY5 RSTBjy/l+Z39q4ap6A/RgHnJ3N92A2wnuIVXFdR/zZaSX2/UZAAI4/gZ4VzB0YNvXu/2 xSBw== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Alistair Francis , alistair23@gmail.com Subject: [PATCH 5/7] linux: Use waitid on wait4 if __NR_wait4 is not defined Date: Thu, 14 Nov 2019 11:47:02 -0300 Message-Id: <20191114144704.19002-5-adhemerval.zanella@linaro.org> In-Reply-To: <20191114144704.19002-1-adhemerval.zanella@linaro.org> References: <20191114144704.19002-1-adhemerval.zanella@linaro.org> 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 -- 2.17.1 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 + 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)