[glib/wip/pwithnall/2216-pidfd-sigchld: 2/2] gmain: Use waitid() on pidfds rather than a global SIGCHLD handler




commit ff243b787fff0dd244fec780c3da704d975961b6
Author: Philip Withnall <pwithnall endlessos org>
Date:   Thu Dec 23 17:45:51 2021 +0000

    gmain: Use waitid() on pidfds rather than a global SIGCHLD handler
    
    When the system supports it (as all Linux kernels ≥ 5.3 should), it’s
    preferable to use `pidfd_open()` and `waitid()` to be notified of
    child processes exiting or being signalled, rather than installing a
    default `SIGCHLD` handler.
    
    A default `SIGCHLD` handler is global, and can never interact well with
    other code (from the application or other libraries) which also wants to
    install a `SIGCHLD` handler.
    
    This use of `pidfd_open()` is racy (the PID may be reused between
    `g_child_watch_source_new()` being called and `pidfd_open()` being
    called), so it doesn’t improve behaviour there. For that, we’d need
    continuous use of pidfds throughout GLib, from fork/spawn time until
    here. See #1866 for that.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #1866
    Fixes: #2216

 glib/gmain.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 89 insertions(+), 6 deletions(-)
---
diff --git a/glib/gmain.c b/glib/gmain.c
index cb94a2551..67ca23b22 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -55,7 +55,10 @@
 #endif
 
 #include <signal.h>
+#include <sys/syscall.h>
 #include <sys/types.h>
+#include <sys/wait.h>
+#include <linux/wait.h>  /* P_PIDFD */
 #include <time.h>
 #include <stdlib.h>
 #ifdef HAVE_SYS_TIME_H
@@ -341,10 +344,11 @@ struct _GChildWatchSource
   /* On Unix this is a wait status, which is the thing you pass to WEXITSTATUS()
    * to get the status returned from the process’ main() or passed to exit(): */
   gint        child_status;
-#ifdef G_OS_WIN32
+  /* @poll is always used on Windows, and used on Unix iff @using_pidfd is set: */
   GPollFD     poll;
-#else /* G_OS_WIN32 */
-  gboolean    child_exited; /* (atomic) */
+#ifndef G_OS_WIN32
+  gboolean    child_exited; /* (atomic); not used iff @using_pidfd is set */
+  gboolean    using_pidfd;
 #endif /* G_OS_WIN32 */
 };
 
@@ -5408,7 +5412,8 @@ dispatch_unix_signals_unlocked (void)
         {
           GChildWatchSource *source = node->data;
 
-          if (!g_atomic_int_get (&source->child_exited))
+          if (!source->using_pidfd &&
+              !g_atomic_int_get (&source->child_exited))
             {
               pid_t pid;
               do
@@ -5467,6 +5472,26 @@ g_child_watch_prepare (GSource *source,
   return g_atomic_int_get (&child_watch_source->child_exited);
 }
 
+static int
+siginfo_t_to_wait_status (const siginfo_t *info)
+{
+  switch (info->si_code)
+    {
+    case CLD_EXITED:
+      return W_EXITCODE (info->si_status, 0);
+    case CLD_KILLED:
+      return W_EXITCODE (0, info->si_status);
+    case CLD_DUMPED:
+      return W_EXITCODE (0, info->si_status | WCOREFLAG);
+    case CLD_CONTINUED:
+      return __W_CONTINUED;
+    case CLD_STOPPED:
+    case CLD_TRAPPED:
+    default:
+      return W_STOPCODE (info->si_status);
+    }
+}
+
 static gboolean
 g_child_watch_check (GSource *source)
 {
@@ -5474,6 +5499,26 @@ g_child_watch_check (GSource *source)
 
   child_watch_source = (GChildWatchSource *) source;
 
+  if (child_watch_source->using_pidfd)
+    {
+      gboolean child_exited = child_watch_source->poll.revents & G_IO_IN;
+
+      if (child_exited)
+        {
+          siginfo_t child_info = { 0, };
+
+          /* Get the exit status */
+          if (waitid (P_PIDFD, child_watch_source->poll.fd, &child_info, WEXITED | WSTOPPED | WCONTINUED | 
WNOHANG) >= 0 &&
+              child_info.si_pid != 0)
+            {
+              child_watch_source->child_status = siginfo_t_to_wait_status (&child_info);
+              child_watch_source->child_exited = TRUE;
+            }
+        }
+
+      return child_exited;
+    }
+
   return g_atomic_int_get (&child_watch_source->child_exited);
 }
 
@@ -5658,6 +5703,11 @@ g_unix_signal_watch_finalize (GSource    *source)
 static void
 g_child_watch_finalize (GSource *source)
 {
+  GChildWatchSource *child_watch_source = (GChildWatchSource *) source;
+
+  if (child_watch_source->using_pidfd)
+    return;
+
   G_LOCK (unix_signal_lock);
   unix_child_watches = g_slist_remove (unix_child_watches, source);
   unref_unix_signal_handler_unlocked (SIGCHLD);
@@ -5759,6 +5809,9 @@ g_child_watch_source_new (GPid pid)
 {
   GSource *source;
   GChildWatchSource *child_watch_source;
+#if !defined(G_OS_WIN32) && defined(SYS_pidfd_open)
+  int errsv;
+#endif
 
 #ifndef G_OS_WIN32
   g_return_val_if_fail (pid > 0, NULL);
@@ -5777,14 +5830,44 @@ g_child_watch_source_new (GPid pid)
   child_watch_source->poll.events = G_IO_IN;
 
   g_source_add_poll (source, &child_watch_source->poll);
-#else /* G_OS_WIN32 */
+#else /* !G_OS_WIN32 */
+
+#if defined(SYS_pidfd_open)
+  /* Use a pidfd, if possible, to avoid having to install a global SIGCHLD
+   * handler and potentially competing with any other library/code which wants
+   * to install one.
+   *
+   * Unfortunately this use of pidfd isn’t race-free (the PID could be recycled
+   * between the caller calling g_child_watch_source_new() and here), but it’s
+   * better than SIGCHLD.
+   */
+  child_watch_source->poll.fd = (int) syscall (SYS_pidfd_open, pid, 0);
+  errsv = errno;
+
+  if (child_watch_source->poll.fd >= 0)
+    {
+      child_watch_source->using_pidfd = TRUE;
+      child_watch_source->poll.events = G_IO_IN;
+      g_source_add_poll (source, &child_watch_source->poll);
+
+      return source;
+    }
+  else
+    {
+      g_debug ("pidfd_open(%" G_PID_FORMAT ") failed with error: %s",
+               pid, g_strerror (errsv));
+      /* Fall through; likely the kernel isn’t new enough to support pidfd_open() */
+    }
+
+#endif  /* defined(SYS_pidfd_open) */
+
   G_LOCK (unix_signal_lock);
   ref_unix_signal_handler_unlocked (SIGCHLD);
   unix_child_watches = g_slist_prepend (unix_child_watches, child_watch_source);
   if (waitpid (pid, &child_watch_source->child_status, WNOHANG) > 0)
     child_watch_source->child_exited = TRUE;
   G_UNLOCK (unix_signal_lock);
-#endif /* G_OS_WIN32 */
+#endif /* !G_OS_WIN32 */
 
   return source;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]