Blame SOURCES/polkit-0.112-spawning-zombie-processes.patch

f322fc
From a028743f5c88dd7c27c102c34535f25b42ea2c5f Mon Sep 17 00:00:00 2001
f322fc
From: Kyle Walker <kwalker@redhat.com>
f322fc
Date: Mon, 23 Apr 2018 13:07:37 -0400
f322fc
Subject: [PATCH] Backport of:
f322fc
 https://bugs.freedesktop.org/attachment.cgi?id=138819
f322fc
f322fc
Signed-off-by: Kyle Walker <kwalker@redhat.com>
f322fc
---
f322fc
 src/polkitbackend/polkitbackendjsauthority.c | 76 +++++++++++++++++++++++++++-
f322fc
 1 file changed, 74 insertions(+), 2 deletions(-)
f322fc
f322fc
diff --git a/src/polkitbackend/polkitbackendjsauthority.c b/src/polkitbackend/polkitbackendjsauthority.c
f322fc
index 39ed718..fd1dbfd 100644
f322fc
--- a/src/polkitbackend/polkitbackendjsauthority.c
f322fc
+++ b/src/polkitbackend/polkitbackendjsauthority.c
f322fc
@@ -83,6 +83,13 @@ struct _PolkitBackendJsAuthorityPrivate
f322fc
   GMutex rkt_timeout_pending_mutex;
f322fc
   gboolean rkt_timeout_pending;
f322fc
 
f322fc
+  /* avoid zombies by reap child in a new thread */
f322fc
+  GThread *child_reaper_thread;
f322fc
+  GMutex crt_init_mutex;
f322fc
+  GCond crt_init_cond;
f322fc
+  GMainContext *crt_context;
f322fc
+  GMainLoop *crt_loop;
f322fc
+
f322fc
   /* A list of JSObject instances */
f322fc
   GList *scripts;
f322fc
 };
f322fc
@@ -124,6 +131,7 @@ enum
f322fc
 /* ---------------------------------------------------------------------------------------------------- */
f322fc
 
f322fc
 static gpointer runaway_killer_thread_func (gpointer user_data);
f322fc
+static gpointer child_reaper_thread_func (gpointer user_data);
f322fc
 
f322fc
 static GList *polkit_backend_js_authority_get_admin_auth_identities (PolkitBackendInteractiveAuthority *authority,
f322fc
                                                                      PolkitSubject                     *caller,
f322fc
@@ -461,6 +469,18 @@ polkit_backend_js_authority_constructed (GObject *object)
f322fc
   PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (object);
f322fc
   gboolean entered_request = FALSE;
f322fc
 
f322fc
+  g_mutex_init (&authority->priv->crt_init_mutex);
f322fc
+  g_cond_init (&authority->priv->crt_init_cond);
f322fc
+
f322fc
+  authority->priv->child_reaper_thread = g_thread_new ("reap-child-thread",
f322fc
+                                                       child_reaper_thread_func,
f322fc
+                                                       authority);
f322fc
+  /* wait for child_reaper_thread to set up its GMainContext */
f322fc
+  g_mutex_lock (&authority->priv->crt_init_mutex);
f322fc
+  while (authority->priv->crt_context == NULL)
f322fc
+    g_cond_wait (&authority->priv->crt_init_cond, &authority->priv->crt_init_mutex);
f322fc
+  g_mutex_unlock (&authority->priv->crt_init_mutex);
f322fc
+
f322fc
   authority->priv->rt = JS_NewRuntime (8L * 1024L * 1024L);
f322fc
   if (authority->priv->rt == NULL)
f322fc
     goto fail;
f322fc
@@ -585,6 +605,15 @@ polkit_backend_js_authority_finalize (GObject *object)
f322fc
   g_free (authority->priv->dir_monitors);
f322fc
   g_strfreev (authority->priv->rules_dirs);
f322fc
 
f322fc
+  g_mutex_clear (&authority->priv->crt_init_mutex);
f322fc
+  g_cond_clear (&authority->priv->crt_init_cond);
f322fc
+
f322fc
+  /* shut down the child reaper thread */
f322fc
+  g_assert (authority->priv->crt_loop != NULL);
f322fc
+  g_main_loop_quit (authority->priv->crt_loop);
f322fc
+  g_thread_join (authority->priv->child_reaper_thread);
f322fc
+  g_assert (authority->priv->crt_loop == NULL);
f322fc
+
f322fc
   JS_BeginRequest (authority->priv->cx);
f322fc
   JS_RemoveObjectRoot (authority->priv->cx, &authority->priv->js_polkit);
f322fc
   JS_RemoveObjectRoot (authority->priv->cx, &authority->priv->js_global);
f322fc
@@ -1360,6 +1389,7 @@ get_signal_name (gint signal_number)
f322fc
 
f322fc
 typedef struct
f322fc
 {
f322fc
+  PolkitBackendJsAuthority *authority;
f322fc
   GMainLoop *loop;
f322fc
   GAsyncResult *res;
f322fc
 } SpawnData;
f322fc
@@ -1379,7 +1409,7 @@ js_polkit_spawn (JSContext  *cx,
f322fc
                  unsigned    js_argc,
f322fc
                  jsval      *vp)
f322fc
 {
f322fc
-  /* PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (JS_GetContextPrivate (cx)); */
f322fc
+  PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (JS_GetContextPrivate (cx));
f322fc
   JSBool ret = JS_FALSE;
f322fc
   JSObject *array_object;
f322fc
   gchar *standard_output = NULL;
f322fc
@@ -1424,6 +1454,8 @@ js_polkit_spawn (JSContext  *cx,
f322fc
       JS_free (cx, s);
f322fc
     }
f322fc
 
f322fc
+  data.authority = authority;
f322fc
+
f322fc
   context = g_main_context_new ();
f322fc
   loop = g_main_loop_new (context, FALSE);
f322fc
 
f322fc
@@ -1540,6 +1572,8 @@ js_polkit_user_is_in_netgroup (JSContext  *cx,
f322fc
 
f322fc
 typedef struct
f322fc
 {
f322fc
+  PolkitBackendJsAuthority *authority;
f322fc
+
f322fc
   GSimpleAsyncResult *simple; /* borrowed reference */
f322fc
   GMainContext *main_context; /* may be NULL */
f322fc
 
f322fc
@@ -1572,11 +1606,43 @@ utils_child_watch_from_release_cb (GPid     pid,
f322fc
                                    gint     status,
f322fc
                                    gpointer user_data)
f322fc
 {
f322fc
+  g_print("Child(pid: %d) has been reaped!\n", pid);
f322fc
+}
f322fc
+
f322fc
+/* ---------------------------------------------------------------------------------------------------- */
f322fc
+
f322fc
+static gpointer
f322fc
+child_reaper_thread_func (gpointer user_data)
f322fc
+{
f322fc
+  PolkitBackendJsAuthority *authority = POLKIT_BACKEND_JS_AUTHORITY (user_data);
f322fc
+
f322fc
+  g_mutex_lock (&authority->priv->crt_init_mutex);
f322fc
+
f322fc
+  authority->priv->crt_context = g_main_context_new ();
f322fc
+  authority->priv->crt_loop = g_main_loop_new (authority->priv->crt_context, FALSE);
f322fc
+  g_main_context_push_thread_default (authority->priv->crt_context);
f322fc
+
f322fc
+  /* Signal the main thread that we're done constructing */
f322fc
+  g_cond_signal (&authority->priv->crt_init_cond);
f322fc
+  g_mutex_unlock (&authority->priv->crt_init_mutex);
f322fc
+
f322fc
+  g_main_loop_run (authority->priv->crt_loop);
f322fc
+
f322fc
+  g_main_context_pop_thread_default (authority->priv->crt_context);
f322fc
+
f322fc
+  g_main_loop_unref (authority->priv->crt_loop);
f322fc
+  authority->priv->crt_loop = NULL;
f322fc
+  g_main_context_unref (authority->priv->crt_context);
f322fc
+  authority->priv->crt_context = NULL;
f322fc
+
f322fc
+  return NULL;
f322fc
 }
f322fc
 
f322fc
+/* ---------------------------------------------------------------------------------------------------- */
f322fc
 static void
f322fc
 utils_spawn_data_free (UtilsSpawnData *data)
f322fc
 {
f322fc
+  PolkitBackendJsAuthority *authority = data->authority;
f322fc
   if (data->timeout_source != NULL)
f322fc
     {
f322fc
       g_source_destroy (data->timeout_source);
f322fc
@@ -1604,12 +1670,17 @@ utils_spawn_data_free (UtilsSpawnData *data)
f322fc
        * Avoid taking a references to ourselves. but note that we need
f322fc
        * to pass the GSource so we can nuke it once handled.
f322fc
        */
f322fc
+
f322fc
+      /* avoid zombies by reaping child in a new thread
f322fc
+       * add source to reap thread context
f322fc
+       */
f322fc
+      GMainContext *reap_context = authority->priv->crt_context;
f322fc
       source = g_child_watch_source_new (data->child_pid);
f322fc
       g_source_set_callback (source,
f322fc
                              (GSourceFunc) utils_child_watch_from_release_cb,
f322fc
                              source,
f322fc
                              (GDestroyNotify) g_source_destroy);
f322fc
-      g_source_attach (source, data->main_context);
f322fc
+      g_source_attach (source, reap_context);
f322fc
       g_source_unref (source);
f322fc
       data->child_pid = 0;
f322fc
     }
f322fc
@@ -1776,6 +1847,7 @@ utils_spawn (const gchar *const  *argv,
f322fc
   GError *error;
f322fc
 
f322fc
   data = g_slice_new0 (UtilsSpawnData);
f322fc
+  data->authority = ((SpawnData *)user_data)->authority;
f322fc
   data->timeout_seconds = timeout_seconds;
f322fc
   data->simple = g_simple_async_result_new (NULL,
f322fc
                                             callback,
f322fc
-- 
f322fc
2.14.3
f322fc