unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alistair Francis <alistair.francis@wdc.com>
To: libc-alpha@sourceware.org
Cc: arnd@arndb.de, adhemerval.zanella@linaro.org, fweimer@redhat.com,
	palmer@sifive.com, macro@wdc.com, zongbox@gmail.com,
	zong@andestech.com, alistair.francis@wdc.com,
	alistair23@gmail.com
Subject: [RFC v2 08/20] sysdeps/wait: Use waitid if avaliable
Date: Mon, 24 Jun 2019 17:09:07 -0700	[thread overview]
Message-ID: <2df9d3878359585ac1cc46243fb6664f7a50b3b3.1561421042.git.alistair.francis@wdc.com> (raw)
In-Reply-To: <cover.1561421042.git.alistair.francis@wdc.com>

If the waitid syscall is avaliable let's use that as waitpid
and wait4 aren't always avaliable (they aren't avaliable on RV32).

Unfortunately waitid is substantially differnt to waitpid and wait4, so
the conversion ends up being complex.

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
---
 ChangeLog                                  |  3 ++
 sysdeps/unix/sysv/linux/wait.c             | 21 ++++++++-
 sysdeps/unix/sysv/linux/waitpid.c          | 54 ++++++++++++++++++++++
 sysdeps/unix/sysv/linux/waitpid_nocancel.c | 53 +++++++++++++++++++++
 4 files changed, 129 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index f1c7acb6ab..9ed9bea8b1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,9 @@
 	* sysdeps/unix/sysv/linux/nanosleep_nocancel.c: Likewise.
 	* sysdeps/unix/sysv/linux/lowlevellock-futex.h: Use __NR_futex_time64 if we don't have __NR_futex.
 	* sysdeps/unix/sysv/linux/gettimeofday.c: Use clock_gettime64 syscall for gettimeofday.
+	* sysdeps/unix/sysv/linux/wait.c: Use __NR_waitid if avaliable.
+	* sysdeps/unix/sysv/linux/waitpid.c: Likewise.
+	* sysdeps/unix/sysv/linux/waitpid_nocancel.c: Likewise.
 
 2019-06-20  Dmitry V. Levin  <ldv@altlinux.org>
 	    Florian Weimer  <fweimer@redhat.com>
diff --git a/sysdeps/unix/sysv/linux/wait.c b/sysdeps/unix/sysv/linux/wait.c
index 498bd1c095..67b94776a4 100644
--- a/sysdeps/unix/sysv/linux/wait.c
+++ b/sysdeps/unix/sysv/linux/wait.c
@@ -26,8 +26,25 @@
 pid_t
 __libc_wait (int *stat_loc)
 {
-  pid_t result = SYSCALL_CANCEL (wait4, WAIT_ANY, stat_loc, 0,
-				 (struct rusage *) NULL);
+  pid_t result;
+
+#ifdef __NR_waitid
+  siginfo_t infop;
+
+  result = SYSCALL_CANCEL (waitid, P_ALL, 0, &infop, WEXITED);
+
+  if (stat_loc) {
+    *stat_loc = infop.si_status;
+  }
+
+  if (result == 0) {
+    result = infop.si_pid;
+  }
+#else
+  result = SYSCALL_CANCEL (wait4, WAIT_ANY, stat_loc, 0,
+                           (struct rusage *) NULL);
+#endif
+
   return result;
 }
 
diff --git a/sysdeps/unix/sysv/linux/waitpid.c b/sysdeps/unix/sysv/linux/waitpid.c
index f0897574c0..8c0e2f882a 100644
--- a/sysdeps/unix/sysv/linux/waitpid.c
+++ b/sysdeps/unix/sysv/linux/waitpid.c
@@ -20,12 +20,66 @@
 #include <sysdep-cancel.h>
 #include <stdlib.h>
 #include <sys/wait.h>
+#include <unistd.h>
 
 __pid_t
 __waitpid (__pid_t pid, int *stat_loc, int options)
 {
 #ifdef __NR_waitpid
   return SYSCALL_CANCEL (waitpid, pid, stat_loc, options);
+#elif defined(__NR_waitid)
+  int ret, waitid_options = 0;
+  idtype_t idtype = P_PID;
+  siginfo_t infop;
+
+  /* Set this to zero so we can test if WNOHANG was specified in options
+   * and there were no children in a waitable state. This is required to match
+   * waitid() behaviour.
+   */
+  infop.si_pid = 0;
+
+  if (pid < -1) {
+    idtype = P_PGID;
+    pid *= -1;
+  } else if (pid == -1) {
+    idtype = P_ALL;
+  } else if (pid == 0) {
+    idtype = P_PGID;
+    pid = getpgrp();
+  }
+
+  /* Convert the older WNOHANG, WUNTRACED and WCONTINUED options to the newer
+   * ones uses for waitid().
+   */
+  if (options | WNOHANG) {
+    waitid_options |= WNOHANG;
+  }
+  if (options | WUNTRACED) {
+    waitid_options |= WSTOPPED;
+  }
+  if (options | WCONTINUED) {
+    waitid_options |= WCONTINUED;
+  }
+
+  /* waitid() requires at least WEXITED, WSTOPPED or WCONTINUED to be
+   * specified. If none were specified default to WEXITED as that is what
+   * waitpid() and wait4() do.
+   */
+  if (options == 0 || waitid_options == WNOHANG) {
+    waitid_options |= WEXITED;
+  }
+
+  ret = SYSCALL_CANCEL (waitid, idtype, pid, &infop, waitid_options);
+
+  if (stat_loc) {
+    *stat_loc = infop.si_status;
+  }
+
+  if (ret == 0) {
+    return infop.si_pid;
+  }
+
+  return ret;
 #else
   return SYSCALL_CANCEL (wait4, pid, stat_loc, options, NULL);
 #endif
diff --git a/sysdeps/unix/sysv/linux/waitpid_nocancel.c b/sysdeps/unix/sysv/linux/waitpid_nocancel.c
index 89e36a5c0b..d14b7bf260 100644
--- a/sysdeps/unix/sysv/linux/waitpid_nocancel.c
+++ b/sysdeps/unix/sysv/linux/waitpid_nocancel.c
@@ -27,6 +27,59 @@ __waitpid_nocancel (__pid_t pid, int *stat_loc, int options)
 {
 #ifdef __NR_waitpid
   return INLINE_SYSCALL_CALL (waitpid, pid, stat_loc, options);
+#elif defined(__NR_waitid)
+  int ret, waitid_options = 0;
+  idtype_t idtype = P_PID;
+  siginfo_t infop;
+
+  /* Set this to zero so we can test if WNOHANG was specified in options
+   * and there were no children in a waitable state. This is required to match
+   * waitid() behaviour.
+   */
+  infop.si_pid = 0;
+
+  if (pid < -1) {
+    idtype = P_PGID;
+    pid *= -1;
+  } else if (pid == -1) {
+    idtype = P_ALL;
+  } else if (pid == 0) {
+    idtype = P_PGID;
+    pid = getpgrp();
+  }
+
+  /* Convert the older WNOHANG, WUNTRACED and WCONTINUED options to the newer
+   * ones uses for waitid().
+   */
+  if (options | WNOHANG) {
+    waitid_options |= WNOHANG;
+  }
+  if (options | WUNTRACED) {
+    waitid_options |= WSTOPPED;
+  }
+  if (options | WCONTINUED) {
+    waitid_options |= WCONTINUED;
+  }
+
+  /* waitid() requires at least WEXITED, WSTOPPED or WCONTINUED to be
+   * specified. If none were specified default to WEXITED as that is what
+   * waitpid() and wait4() do.
+   */
+  if (options == 0 || waitid_options == WNOHANG) {
+    waitid_options |= WEXITED;
+  }
+
+  ret = INLINE_SYSCALL_CALL (waitid, idtype, pid, &infop, waitid_options);
+
+  if (stat_loc) {
+    *stat_loc = infop.si_status;
+  }
+
+  if (ret == 0) {
+    return infop.si_pid;
+  }
+
+  return ret;
 #else
   return INLINE_SYSCALL_CALL (wait4, pid, stat_loc, options, NULL);
 #endif
-- 
2.22.0


  parent reply	other threads:[~2019-06-25  0:12 UTC|newest]

Thread overview: 107+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-25  0:08 [RFC v2 00/20] RISC-V glibc port for the 32-bit Alistair Francis
2019-06-25  0:08 ` [RFC v2 01/20] y2038: Introduce internal for glibc struct __timespec64 Alistair Francis
2019-06-25  0:08 ` [RFC v2 02/20] y2038: Provide conversion helpers for " Alistair Francis
2019-06-25  0:08 ` [RFC v2 03/20] y2038: linux: Provide __clock_settime64 implementation Alistair Francis
2019-06-25 11:05   ` Arnd Bergmann
2019-06-25 15:51     ` Lukasz Majewski
2019-06-25 16:39       ` Arnd Bergmann
2019-06-26  9:07         ` Lukasz Majewski
2019-06-26 12:36           ` Arnd Bergmann
2019-06-26 15:03             ` Lukasz Majewski
2019-06-26 15:11               ` Florian Weimer
2019-06-26 22:49                 ` Lukasz Majewski
2019-06-27  7:14                   ` Florian Weimer
2019-06-27  7:36                     ` Lukasz Majewski
2019-06-26 18:58               ` Arnd Bergmann
2019-06-26 22:55                 ` Lukasz Majewski
2019-06-27  7:45                   ` Arnd Bergmann
2019-06-27 10:35                     ` Lukasz Majewski
2019-06-27 13:32                       ` Arnd Bergmann
2019-06-27 14:07                         ` Lukasz Majewski
2019-06-27 14:57                           ` Arnd Bergmann
2019-06-27 15:23                             ` Lukasz Majewski
2019-06-27 15:45                               ` Arnd Bergmann
2019-06-27 16:16                                 ` Lukasz Majewski
2019-06-27 21:25                                   ` Arnd Bergmann
2019-06-27 22:08                                     ` Lukasz Majewski
2019-07-04  0:04                                       ` Alistair Francis
2019-07-04  8:13                                         ` Lukasz Majewski
2019-07-08  9:32                                           ` Lukasz Majewski
2019-06-27 20:08                           ` Alistair Francis
2019-06-27 21:02                             ` Lukasz Majewski
2019-07-08 10:49         ` Joseph Myers
2019-06-25  0:08 ` [RFC v2 04/20] include/time.h: Fix conflicting timespec types on 32-bit Alistair Francis
2019-06-25 11:17   ` Arnd Bergmann
2019-06-25 22:20     ` Alistair Francis
2019-06-25  0:08 ` [RFC v2 05/20] sysdeps/nanosleep: Use clock_nanosleep_time64 if avaliable Alistair Francis
2019-06-25  8:24   ` Andreas Schwab
2019-06-25  8:59   ` Arnd Bergmann
2019-06-26 18:20     ` Alistair Francis
2019-06-25  0:09 ` [RFC v2 06/20] sysdeps/futex: Use futex_time64 " Alistair Francis
2019-06-25 11:14   ` Florian Weimer
2019-06-25 11:26     ` Andreas Schwab
2019-06-25 11:41       ` Arnd Bergmann
2019-06-25 12:06         ` Florian Weimer
2019-06-25 13:32           ` Arnd Bergmann
2019-06-25  0:09 ` [RFC v2 07/20] sysdeps/gettimeofday: Use clock_gettime64 " Alistair Francis
2019-06-27 13:14   ` Adhemerval Zanella
2019-07-03 23:49     ` Alistair Francis
2019-07-24 20:22   ` Joseph Myers
2019-07-24 23:00     ` Alistair Francis
2019-07-25 12:57       ` Arnd Bergmann
2019-07-25 17:03         ` Paul Eggert
2019-07-25 17:21           ` Zack Weinberg
2019-07-25 18:53             ` Arnd Bergmann
2019-07-26 13:01               ` Florian Weimer
2019-07-26 13:08                 ` Zack Weinberg
2019-07-25 21:23       ` Joseph Myers
2019-06-25  0:09 ` Alistair Francis [this message]
2019-06-25  8:16   ` [RFC v2 08/20] sysdeps/wait: Use waitid " Andreas Schwab
2019-06-25 10:55   ` Zack Weinberg
2019-06-25 11:06     ` Florian Weimer
2019-06-25 12:00       ` Arnd Bergmann
2019-06-25 12:10         ` Florian Weimer
2019-06-25 13:29           ` Arnd Bergmann
2019-06-25 13:39             ` Arnd Bergmann
2019-06-25 13:47               ` Florian Weimer
2019-06-25 14:04                 ` Arnd Bergmann
2019-06-25 14:08                   ` Florian Weimer
2019-06-25 14:21                     ` Arnd Bergmann
2019-06-25 14:29                       ` Florian Weimer
2019-06-26 14:37                         ` Arnd Bergmann
2019-06-26 15:48                           ` Florian Weimer
2019-06-26 16:28                             ` Andreas Schwab
2019-07-08 12:09                               ` Florian Weimer
2019-07-08 12:34                                 ` Andreas Schwab
2019-07-08 12:36                                   ` Florian Weimer
2019-07-09 22:57                                     ` Alistair Francis
2019-07-10  7:33                                       ` Andreas Schwab
2019-07-10 17:47                                         ` Alistair Francis
2019-07-25 15:49                                     ` Joseph Myers
2019-06-26 21:08                             ` Arnd Bergmann
2019-06-27  7:33                               ` Florian Weimer
2019-06-27  8:25                                 ` Andreas Schwab
2019-06-27 10:21                                 ` Arnd Bergmann
2019-06-27 11:12                                   ` Florian Weimer
2019-06-27 15:24                                     ` Arnd Bergmann
2019-07-03 23:50                                       ` Alistair Francis
2019-06-25 23:51         ` Alistair Francis
2019-06-25  0:09 ` [RFC v2 09/20] sysdeps/getrlimit: Use prlimit64 " Alistair Francis
2019-06-25 11:11   ` Florian Weimer
2019-06-25 20:45     ` Alistair Francis
2019-06-25 21:10       ` Florian Weimer
2019-06-25 23:38         ` Alistair Francis
2019-06-25  0:09 ` [RFC v2 10/20] Documentation for the RISC-V 32-bit port Alistair Francis
2019-06-25  0:09 ` [RFC v2 11/20] RISC-V: Use 64-bit time_t and off_t for RV32 and RV64 Alistair Francis
2019-06-25  0:09 ` [RFC v2 12/20] RISC-V: Support dynamic loader for the 32-bit Alistair Francis
2019-06-25  0:09 ` [RFC v2 13/20] RISC-V: Add path of library directories " Alistair Francis
2019-06-25  0:09 ` [RFC v2 14/20] RISC-V: The ABI implementation " Alistair Francis
2019-06-25  0:09 ` [RFC v2 15/20] RISC-V: Hard float support for the 32 bit Alistair Francis
2019-06-25  0:09 ` [RFC v2 16/20] RISC-V: Regenerate ULPs of RISC-V Alistair Francis
2019-06-25  0:09 ` [RFC v2 17/20] RISC-V: Add ABI lists Alistair Francis
2019-06-25  0:09 ` [RFC v2 18/20] RISC-V: Build Infastructure for the 32-bit Alistair Francis
2019-06-25  0:09 ` [RFC v2 19/20] RISC-V: Fix llrint and llround missing exceptions on RV32 Alistair Francis
2019-06-25  0:09 ` [RFC v2 20/20] Add RISC-V 32-bit target to build-many-glibcs.py Alistair Francis
2019-06-25 11:15 ` [RFC v2 00/20] RISC-V glibc port for the 32-bit Florian Weimer
2019-06-25 12:07   ` Arnd Bergmann
2019-07-04  8:47 ` Jim Wilson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2df9d3878359585ac1cc46243fb6664f7a50b3b3.1561421042.git.alistair.francis@wdc.com \
    --to=alistair.francis@wdc.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=alistair23@gmail.com \
    --cc=arnd@arndb.de \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=macro@wdc.com \
    --cc=palmer@sifive.com \
    --cc=zong@andestech.com \
    --cc=zongbox@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).