[gnome-builder] threading: set PDEATHSIG when possible



commit 1a2c5a1c6e0f080a498440ef7c583645ff03ffa9
Author: Christian Hergert <chergert redhat com>
Date:   Thu Apr 18 21:34:02 2019 -0700

    threading: set PDEATHSIG when possible
    
    If we are launching from the main thread, then we can opportunistically
    use prctl() to ensure the child dies with the parent.

 src/libide/threading/ide-subprocess-launcher.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
---
diff --git a/src/libide/threading/ide-subprocess-launcher.c b/src/libide/threading/ide-subprocess-launcher.c
index cdb22b3bd..ac04f395b 100644
--- a/src/libide/threading/ide-subprocess-launcher.c
+++ b/src/libide/threading/ide-subprocess-launcher.c
@@ -29,6 +29,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/ioctl.h>
+#ifdef __linux__
+# include <sys/prctl.h>
+#endif
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -92,6 +95,16 @@ child_setup_func (gpointer data)
   setsid ();
   setpgid (0, 0);
 
+#ifdef __linux__
+  /*
+   * If we were spawned from the main thread, then we can setup the
+   * PR_SET_PDEATHSIG and know that when this thread exits that the
+   * child will get a kill sig.
+   */
+  if (data != NULL)
+    prctl (PR_SET_PDEATHSIG, SIGKILL);
+#endif
+
   if (isatty (STDIN_FILENO))
     {
       if (ioctl (STDIN_FILENO, TIOCSCTTY, 0) != 0)
@@ -266,11 +279,15 @@ ide_subprocess_launcher_spawn_worker (GTask        *task,
   g_autoptr(GSubprocess) real = NULL;
   g_autoptr(IdeSubprocess) wrapped = NULL;
   g_autoptr(GError) error = NULL;
+  gpointer child_data = NULL;
 
   IDE_ENTRY;
 
   g_return_if_fail (IDE_IS_SUBPROCESS_LAUNCHER (self));
 
+  if (IDE_IS_MAIN_THREAD ())
+    child_data = GUINT_TO_POINTER (TRUE);
+
   {
     g_autofree gchar *str = NULL;
     g_autofree gchar *env = NULL;
@@ -283,7 +300,7 @@ ide_subprocess_launcher_spawn_worker (GTask        *task,
   }
 
   launcher = g_subprocess_launcher_new (priv->flags);
-  g_subprocess_launcher_set_child_setup (launcher, child_setup_func, NULL, NULL);
+  g_subprocess_launcher_set_child_setup (launcher, child_setup_func, child_data, NULL);
   g_subprocess_launcher_set_cwd (launcher, priv->cwd);
 
   if (priv->stdout_file_path != NULL)


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