From 21bde9a7d2917d82fa3d15260879c80ff429dbb3 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Tue, 17 Nov 2020 18:08:15 +0000
Subject: [PATCH] PvPortalListener: Store original environment and working
 directory

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 pressure-vessel/launcher.c        | 14 +++-----------
 pressure-vessel/portal-listener.c | 10 +++++++++-
 pressure-vessel/portal-listener.h |  2 ++
 3 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/pressure-vessel/launcher.c b/pressure-vessel/launcher.c
index 146fb1300..3c2d02034 100644
--- a/pressure-vessel/launcher.c
+++ b/pressure-vessel/launcher.c
@@ -60,7 +60,6 @@ typedef GDBusServer AutoDBusServer;
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(AutoDBusServer, g_object_unref)
 
 static PvPortalListener *global_listener;
-static const char * const *global_original_environ = NULL;
 static FILE *original_stdout = NULL;
 static FILE *info_fh = NULL;
 static GHashTable *lock_env_hash = NULL;
@@ -69,7 +68,6 @@ static GHashTable *client_pid_data_hash = NULL;
 static guint name_owner_id = 0;
 static GMainLoop *main_loop;
 static PvLauncher1 *launcher;
-static gchar *original_cwd_l = NULL;
 
 /*
  * Close the --info-fd, and also close standard output (if different).
@@ -376,7 +374,7 @@ handle_launch (PvLauncher1           *object,
     }
   else
     {
-      env = g_strdupv ((gchar **) global_original_environ);
+      env = g_strdupv (global_listener->original_environ);
     }
 
   n_envs = g_variant_n_children (arg_envs);
@@ -425,7 +423,8 @@ handle_launch (PvLauncher1           *object,
     }
 
   if (arg_cwd_path == NULL)
-    env = g_environ_setenv (env, "PWD", original_cwd_l, TRUE);
+    env = g_environ_setenv (env, "PWD", global_listener->original_cwd_l,
+                            TRUE);
   else
     env = g_environ_setenv (env, "PWD", arg_cwd_path, TRUE);
 
@@ -1109,7 +1108,6 @@ int
 main (int argc,
       char *argv[])
 {
-  g_auto(GStrv) original_environ = NULL;
   g_autoptr(AutoDBusServer) server = NULL;
   g_autoptr(GOptionContext) context = NULL;
   guint signals_id = 0;
@@ -1122,9 +1120,6 @@ main (int argc,
   my_pid = getpid ();
 
   global_listener = pv_portal_listener_new ();
-  original_environ = g_get_environ ();
-  global_original_environ = (const char * const *) original_environ;
-  pv_get_current_dirs (NULL, &original_cwd_l);
 
   setlocale (LC_ALL, "");
 
@@ -1447,9 +1442,6 @@ out:
   g_clear_error (&local_error);
   close_info_fh ();
 
-  g_free (original_cwd_l);
-  global_original_environ = NULL;
-
   g_debug ("Exiting with status %d", ret);
   return ret;
 }
diff --git a/pressure-vessel/portal-listener.c b/pressure-vessel/portal-listener.c
index 3f9786fbe..3a17e1058 100644
--- a/pressure-vessel/portal-listener.c
+++ b/pressure-vessel/portal-listener.c
@@ -27,7 +27,12 @@
 #include "config.h"
 #include "subprojects/libglnx/config.h"
 
+#include "steam-runtime-tools/glib-backports-internal.h"
+#include "steam-runtime-tools/utils-internal.h"
+#include "libglnx/libglnx.h"
+
 #include "portal-listener.h"
+#include "utils.h"
 
 struct _PvPortalListenerClass
 {
@@ -39,6 +44,8 @@ G_DEFINE_TYPE (PvPortalListener, pv_portal_listener, G_TYPE_OBJECT)
 void
 pv_portal_listener_init (PvPortalListener *self)
 {
+  self->original_environ = g_get_environ ();
+  pv_get_current_dirs (NULL, &self->original_cwd_l);
 }
 
 static void
@@ -56,7 +63,8 @@ pv_portal_listener_finalize (GObject *object)
 {
   PvPortalListener *self = PV_PORTAL_LISTENER (object);
 
-  (void) self;
+  g_clear_pointer (&self->original_environ, g_strfreev);
+  g_clear_pointer (&self->original_cwd_l, g_free);
 
   G_OBJECT_CLASS (pv_portal_listener_parent_class)->finalize (object);
 }
diff --git a/pressure-vessel/portal-listener.h b/pressure-vessel/portal-listener.h
index 4be8bbe1c..4c435b0cd 100644
--- a/pressure-vessel/portal-listener.h
+++ b/pressure-vessel/portal-listener.h
@@ -50,6 +50,8 @@ GType pv_portal_listener_get_type (void);
 struct _PvPortalListener
 {
   GObject parent;
+  GStrv original_environ;
+  gchar *original_cwd_l;
 };
 
 PvPortalListener *pv_portal_listener_new (void);
-- 
GitLab