[gnome-builder] core: add _ide_host_environ()



commit 1e8b72927ef4f1d2eef10a589f73943535239d4f
Author: Christian Hergert <chergert redhat com>
Date:   Wed Mar 24 12:21:48 2021 -0700

    core: add _ide_host_environ()
    
    This private API attempts to extract the environment from the host so that
    we can proxy necessary variables into new containers. In particular, this
    is useful to get the correct XAUTHORITY, DISPLAY, and other things which
    might be different in the Flatpak runtime.
    
    Generally, you wouldn't do this, but since we are a developer tool that is
    mostly just using Flatpak for packaging, it helps with the integration
    tooling for podman, toolbox, etc.

 src/libide/core/ide-global.c  | 67 +++++++++++++++++++++++++++++++++++++++++++
 src/libide/core/ide-private.h | 11 +++----
 2 files changed, 73 insertions(+), 5 deletions(-)
---
diff --git a/src/libide/core/ide-global.c b/src/libide/core/ide-global.c
index d32b351fb..5b2e3db6d 100644
--- a/src/libide/core/ide-global.c
+++ b/src/libide/core/ide-global.c
@@ -289,3 +289,70 @@ _ide_trace_log (GLogLevelFlags  log_level,
   if (trace_vtable.log)
     trace_vtable.log (log_level, domain, message);
 }
+
+static gchar **
+get_environ_from_stdout (GSubprocess *subprocess)
+{
+  g_autofree gchar *stdout_buf = NULL;
+
+  if (g_subprocess_communicate_utf8 (subprocess, NULL, NULL, &stdout_buf, NULL, NULL))
+    {
+      g_auto(GStrv) lines = g_strsplit (stdout_buf, "\n", 0);
+      g_autoptr(GPtrArray) env = g_ptr_array_new_with_free_func (g_free);
+
+      for (guint i = 0; lines[i]; i++)
+        {
+          const char *line = lines[i];
+
+          if (!g_ascii_isalpha (*line) && *line != '_')
+            continue;
+
+          for (const char *iter = line; *iter; iter = g_utf8_next_char (iter))
+            {
+              if (*iter == '=')
+                {
+                  g_ptr_array_add (env, g_strdup (line));
+                  break;
+                }
+
+              if (!g_ascii_isalnum (*iter) && *iter != '_')
+                break;
+            }
+        }
+
+      if (env->len > 0)
+        {
+          g_ptr_array_add (env, NULL);
+          return (gchar **)g_ptr_array_free (g_steal_pointer (&env), FALSE);
+        }
+    }
+
+  return NULL;
+}
+
+const gchar * const *
+_ide_host_environ (void)
+{
+  static gchar **host_environ;
+
+  if (host_environ == NULL)
+    {
+      if (ide_is_flatpak ())
+        {
+          g_autoptr(GSubprocessLauncher) launcher = NULL;
+          g_autoptr(GSubprocess) subprocess = NULL;
+          g_autoptr(GError) error = NULL;
+
+          launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE);
+          subprocess = g_subprocess_launcher_spawn (launcher, &error,
+                                                    "flatpak-spawn", "--host", "printenv", NULL);
+          if (subprocess != NULL)
+            host_environ = get_environ_from_stdout (subprocess);
+        }
+
+      if (host_environ == NULL)
+        host_environ = g_get_environ ();
+    }
+
+  return (const char * const *)host_environ;
+}
diff --git a/src/libide/core/ide-private.h b/src/libide/core/ide-private.h
index 888b650d1..d366ded4c 100644
--- a/src/libide/core/ide-private.h
+++ b/src/libide/core/ide-private.h
@@ -36,10 +36,11 @@ typedef struct
                     const gchar    *message);
 } IdeTraceVTable;
 
-void _ide_trace_init     (IdeTraceVTable *vtable);
-void _ide_trace_log      (GLogLevelFlags  log_level,
-                          const gchar    *domain,
-                          const gchar    *message);
-void _ide_trace_shutdown (void);
+void                 _ide_trace_init     (IdeTraceVTable *vtable);
+void                 _ide_trace_log      (GLogLevelFlags  log_level,
+                                          const gchar    *domain,
+                                          const gchar    *message);
+void                 _ide_trace_shutdown (void);
+const gchar * const *_ide_host_environ   (void);
 
 G_END_DECLS


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