[gnome-session] gnome-session: update activation environment from C code, instead of shell script



commit 52a3c15a1d756c559402cc9505926a9b9d6cf3a7
Author: Ray Strode <rstrode redhat com>
Date:   Fri Jan 13 10:23:35 2017 -0500

    gnome-session: update activation environment from C code, instead of shell script
    
    dbus-update-activation-environment excepts certain environment
    variables, that systemd won't.  We're going to want to eventually send
    the environment to systemd, too, so we shouldn't make sure the same set
    of variables get sent to both.
    
    This commit takes the dbus-update-activation-environment call out of the
    gnome-session shell script wrapper, and instead changes the C code to do
    the export explicitly.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=736660

 gnome-session/gnome-session.in |    2 -
 gnome-session/gsm-util.c       |   78 ++++++++++++++++++++++++++++++++++++++++
 gnome-session/gsm-util.h       |    2 +
 gnome-session/main.c           |    2 +
 4 files changed, 82 insertions(+), 2 deletions(-)
---
diff --git a/gnome-session/gnome-session.in b/gnome-session/gnome-session.in
index 530299d..fdf7163 100644
--- a/gnome-session/gnome-session.in
+++ b/gnome-session/gnome-session.in
@@ -12,6 +12,4 @@ if [ -n "$REGION" ]; then
   export LC_PAPER=$REGION
 fi
 
-dbus-update-activation-environment --all > /dev/null 2>&1 ||:
-
 exec @libexecdir@/gnome-session-binary "$@"
diff --git a/gnome-session/gsm-util.c b/gnome-session/gsm-util.c
index e30cf91..292fa65 100644
--- a/gnome-session/gsm-util.c
+++ b/gnome-session/gsm-util.c
@@ -492,6 +492,84 @@ gsm_util_update_activation_environment (const char  *variable,
         return environment_updated;
 }
 
+gboolean
+gsm_util_export_activation_environment (GError     **error)
+{
+
+        GDBusConnection *connection;
+        gboolean         environment_updated = FALSE;
+        char           **entry_names;
+        int              i = 0;
+        GVariantBuilder  builder;
+        GRegex          *name_regex, *value_regex;
+        GVariant        *reply;
+        GError          *bus_error = NULL;
+
+        connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
+
+        if (connection == NULL) {
+                return FALSE;
+        }
+
+        name_regex = g_regex_new ("^[a-zA-Z_][a-zA-Z0-9_]*$", G_REGEX_OPTIMIZE, 0, error);
+
+        if (name_regex == NULL) {
+                return FALSE;
+        }
+
+        value_regex = g_regex_new ("^([[:blank:]]|[^[:cntrl:]])*$", G_REGEX_OPTIMIZE, 0, error);
+
+        if (value_regex == NULL) {
+                return FALSE;
+        }
+
+        g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
+        for (entry_names = g_listenv (); entry_names[i] != NULL; i++) {
+                const char *entry_name = entry_names[i];
+                const char *entry_value = g_getenv (entry_name);
+
+                if (!g_utf8_validate (entry_name, -1, NULL))
+                    continue;
+
+                if (!g_regex_match (name_regex, entry_name, 0, NULL))
+                    continue;
+
+                if (!g_utf8_validate (entry_value, -1, NULL))
+                    continue;
+
+                if (!g_regex_match (value_regex, entry_value, 0, NULL))
+                    continue;
+
+                g_variant_builder_add (&builder, "{ss}", entry_name, entry_value);
+        }
+        g_regex_unref (name_regex);
+        g_regex_unref (value_regex);
+
+        g_strfreev (entry_names);
+
+        reply = g_dbus_connection_call_sync (connection,
+                                             "org.freedesktop.DBus",
+                                             "/org/freedesktop/DBus",
+                                             "org.freedesktop.DBus",
+                                             "UpdateActivationEnvironment",
+                                             g_variant_new ("(@a{ss})",
+                                                            g_variant_builder_end (&builder)),
+                                             NULL,
+                                             G_DBUS_CALL_FLAGS_NONE,
+                                             -1, NULL, &bus_error);
+
+        if (bus_error != NULL) {
+                g_propagate_error (error, bus_error);
+        } else {
+                environment_updated = TRUE;
+                g_variant_unref (reply);
+        }
+
+        g_clear_object (&connection);
+
+        return environment_updated;
+}
+
 void
 gsm_util_setenv (const char *variable,
                  const char *value)
diff --git a/gnome-session/gsm-util.h b/gnome-session/gsm-util.h
index 6b5e98a..b195d0b 100644
--- a/gnome-session/gsm-util.h
+++ b/gnome-session/gsm-util.h
@@ -51,6 +51,8 @@ void        gsm_util_setenv                         (const char *variable,
                                                      const char *value);
 const char * const * gsm_util_listenv               (void);
 
+gboolean    gsm_util_export_activation_environment  (GError     **error);
+
 void        gsm_quit                                (void);
 
 G_END_DECLS
diff --git a/gnome-session/main.c b/gnome-session/main.c
index 5d6b00f..29de33a 100644
--- a/gnome-session/main.c
+++ b/gnome-session/main.c
@@ -377,6 +377,8 @@ main (int argc, char **argv)
                 exit (1);
         }
 
+        gsm_util_export_activation_environment (NULL);
+
         {
                 gchar *ibus_path;
 


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