[gdm] Move gdm_slave_run_script to common code



commit fc7d7305f45f8b6659903fb5760880ba69073d16
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Thu Mar 6 14:49:25 2014 -0500

    Move gdm_slave_run_script to common code
    
    We're going to move the code that runs Init to gdm-server.c and
    the code that runs PostLogin / PreSession to gdm-session-worker.c
    
    https://bugzilla.gnome.org/show_bug.cgi?id=726380

 common/Makefile.am               |    1 +
 common/gdm-common.c              |  175 ++++++++++++++++++++++++++++++++++++
 common/gdm-common.h              |   11 +++
 daemon/gdm-simple-slave.c        |   40 +++++++-
 daemon/gdm-slave.c               |  181 ++------------------------------------
 daemon/gdm-slave.h               |    3 -
 daemon/gdm-xdmcp-chooser-slave.c |    5 +-
 7 files changed, 232 insertions(+), 184 deletions(-)
---
diff --git a/common/Makefile.am b/common/Makefile.am
index ece167b..2e97090 100644
--- a/common/Makefile.am
+++ b/common/Makefile.am
@@ -20,6 +20,7 @@ AM_CPPFLAGS = \
        -DGDM_DEFAULTS_CONF=\"$(GDM_DEFAULTS_CONF)\"    \
        -DGDM_CUSTOM_CONF=\"$(GDM_CUSTOM_CONF)\"        \
        -DGDM_OLD_CONF=\"$(GDM_OLD_CONF)\"              \
+       -DGDM_SESSION_DEFAULT_PATH=\"$(GDM_SESSION_DEFAULT_PATH)\" \
        $(COMMON_CFLAGS)                                \
        $(NULL)
 
diff --git a/common/gdm-common.c b/common/gdm-common.c
index 30857a2..88f5419 100644
--- a/common/gdm-common.c
+++ b/common/gdm-common.c
@@ -1056,3 +1056,178 @@ gdm_goto_login_session (GError **error)
         return goto_login_session_for_ck (connection, error);
 #endif
 }
+
+static void
+listify_hash (const char *key,
+              const char *value,
+              GPtrArray  *env)
+{
+        char *str;
+        str = g_strdup_printf ("%s=%s", key, value);
+        g_debug ("GdmSlave: script environment: %s", str);
+        g_ptr_array_add (env, str);
+}
+
+GPtrArray *
+gdm_get_script_environment (const char *username,
+                            const char *display_name,
+                            const char *display_hostname,
+                            const char *display_x11_authority_file)
+{
+        GPtrArray     *env;
+        GHashTable    *hash;
+        struct passwd *pwent;
+
+        env = g_ptr_array_new ();
+
+        /* create a hash table of current environment, then update keys has necessary */
+        hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+
+        /* modify environment here */
+        g_hash_table_insert (hash, g_strdup ("HOME"), g_strdup ("/"));
+        g_hash_table_insert (hash, g_strdup ("PWD"), g_strdup ("/"));
+        g_hash_table_insert (hash, g_strdup ("SHELL"), g_strdup ("/bin/sh"));
+
+        if (username != NULL) {
+                g_hash_table_insert (hash, g_strdup ("LOGNAME"),
+                                     g_strdup (username));
+                g_hash_table_insert (hash, g_strdup ("USER"),
+                                     g_strdup (username));
+                g_hash_table_insert (hash, g_strdup ("USERNAME"),
+                                     g_strdup (username));
+
+                gdm_get_pwent_for_name (username, &pwent);
+                if (pwent != NULL) {
+                        if (pwent->pw_dir != NULL && pwent->pw_dir[0] != '\0') {
+                                g_hash_table_insert (hash, g_strdup ("HOME"),
+                                                     g_strdup (pwent->pw_dir));
+                                g_hash_table_insert (hash, g_strdup ("PWD"),
+                                                     g_strdup (pwent->pw_dir));
+                        }
+
+                        g_hash_table_insert (hash, g_strdup ("SHELL"),
+                                             g_strdup (pwent->pw_shell));
+                }
+        }
+
+        if (display_hostname) {
+                g_hash_table_insert (hash, g_strdup ("REMOTE_HOST"), g_strdup (display_hostname));
+        }
+
+        /* Runs as root */
+        g_hash_table_insert (hash, g_strdup ("XAUTHORITY"), g_strdup (display_x11_authority_file));
+        g_hash_table_insert (hash, g_strdup ("DISPLAY"), g_strdup (display_name));
+        g_hash_table_insert (hash, g_strdup ("PATH"), g_strdup (GDM_SESSION_DEFAULT_PATH));
+        g_hash_table_insert (hash, g_strdup ("RUNNING_UNDER_GDM"), g_strdup ("true"));
+
+        g_hash_table_remove (hash, "MAIL");
+
+        g_hash_table_foreach (hash, (GHFunc)listify_hash, env);
+        g_hash_table_destroy (hash);
+
+        g_ptr_array_add (env, NULL);
+
+        return env;
+}
+
+gboolean
+gdm_run_script (const char *dir,
+                const char *username,
+                const char *display_name,
+                const char *display_hostname,
+                const char *display_x11_authority_file)
+{
+        char      *script;
+        char     **argv;
+        gint       status;
+        GError    *error;
+        GPtrArray *env;
+        gboolean   res;
+        gboolean   ret;
+
+        ret = FALSE;
+
+        g_assert (dir != NULL);
+        g_assert (username != NULL);
+
+        script = g_build_filename (dir, display_name, NULL);
+        g_debug ("Trying script %s", script);
+        if (! (g_file_test (script, G_FILE_TEST_IS_REGULAR)
+               && g_file_test (script, G_FILE_TEST_IS_EXECUTABLE))) {
+                g_debug ("script %s not found; skipping", script);
+                g_free (script);
+                script = NULL;
+        }
+
+        if (script == NULL
+            && display_hostname != NULL
+            && display_hostname[0] != '\0') {
+                script = g_build_filename (dir, display_hostname, NULL);
+                g_debug ("Trying script %s", script);
+                if (! (g_file_test (script, G_FILE_TEST_IS_REGULAR)
+                       && g_file_test (script, G_FILE_TEST_IS_EXECUTABLE))) {
+                        g_debug ("script %s not found; skipping", script);
+                        g_free (script);
+                        script = NULL;
+                }
+        }
+
+        if (script == NULL) {
+                script = g_build_filename (dir, "Default", NULL);
+                g_debug ("Trying script %s", script);
+                if (! (g_file_test (script, G_FILE_TEST_IS_REGULAR)
+                       && g_file_test (script, G_FILE_TEST_IS_EXECUTABLE))) {
+                        g_debug ("script %s not found; skipping", script);
+                        g_free (script);
+                        script = NULL;
+                }
+        }
+
+        if (script == NULL) {
+                g_debug ("no script found");
+                return TRUE;
+        }
+
+        g_debug ("Running process: %s", script);
+        error = NULL;
+        if (! g_shell_parse_argv (script, NULL, &argv, &error)) {
+                g_warning ("Could not parse command: %s", error->message);
+                g_error_free (error);
+                goto out;
+        }
+
+        env = gdm_get_script_environment (username,
+                                          display_name,
+                                          display_hostname,
+                                          display_x11_authority_file);
+
+        res = g_spawn_sync (NULL,
+                            argv,
+                            (char **)env->pdata,
+                            G_SPAWN_SEARCH_PATH,
+                            NULL,
+                            NULL,
+                            NULL,
+                            NULL,
+                            &status,
+                            &error);
+
+        g_ptr_array_foreach (env, (GFunc)g_free, NULL);
+        g_ptr_array_free (env, TRUE);
+        g_strfreev (argv);
+
+        if (! res) {
+                g_warning ("Unable to run script: %s", error->message);
+                g_error_free (error);
+        }
+
+        if (WIFEXITED (status)) {
+                g_debug ("Process exit status: %d", WEXITSTATUS (status));
+                ret = WEXITSTATUS (status) == 0;
+        }
+
+ out:
+        g_free (script);
+
+        return ret;
+}
diff --git a/common/gdm-common.h b/common/gdm-common.h
index c17e603..50bf153 100644
--- a/common/gdm-common.h
+++ b/common/gdm-common.h
@@ -65,6 +65,17 @@ gboolean       gdm_string_hex_decode     (const GString *source,
 char          *gdm_generate_random_bytes (gsize          size,
                                           GError       **error);
 gboolean       gdm_goto_login_session    (GError **error);
+
+GPtrArray     *gdm_get_script_environment (const char *username,
+                                           const char *display_name,
+                                           const char *display_hostname,
+                                           const char *display_x11_authority_file);
+gboolean       gdm_run_script             (const char *dir,
+                                           const char *username,
+                                           const char *display_name,
+                                           const char *display_hostname,
+                                           const char *display_x11_authority_file);
+
 G_END_DECLS
 
 #endif /* _GDM_COMMON_H */
diff --git a/daemon/gdm-simple-slave.c b/daemon/gdm-simple-slave.c
index ca96113..214e1a4 100644
--- a/daemon/gdm-simple-slave.c
+++ b/daemon/gdm-simple-slave.c
@@ -246,6 +246,36 @@ out:
         g_free (gis_dir_path);
 }
 
+static gboolean
+run_script (GdmSimpleSlave *slave,
+            const char     *dir,
+            const char     *username)
+{
+        char *display_name;
+        char *display_hostname;
+        char *display_x11_authority_file;
+        gboolean display_is_local;
+        gboolean ret;
+
+        g_object_get (slave,
+                      "display-name", &display_name,
+                      "display-hostname", &display_hostname,
+                      "display-is-local", &display_is_local,
+                      "display-x11-authority-file", &display_x11_authority_file,
+                      NULL);
+
+        ret = gdm_run_script (dir, username,
+                              display_name,
+                              display_is_local? NULL : display_hostname,
+                              display_x11_authority_file);
+
+        g_free (display_name);
+        g_free (display_hostname);
+        g_free (display_x11_authority_file);
+
+        return ret;
+}
+
 static void
 on_session_started (GdmSession       *session,
                     const char       *service_name,
@@ -265,7 +295,7 @@ on_session_started (GdmSession       *session,
         /* Run the PreSession script. gdmslave suspends until script has terminated */
         username = gdm_session_get_username (slave->priv->session);
         if (username != NULL) {
-                gdm_slave_run_script (GDM_SLAVE (slave), GDMCONFDIR "/PreSession", username);
+                run_script (slave, GDMCONFDIR "/PreSession", username);
         }
 
         /* FIXME: should we do something here?
@@ -464,7 +494,7 @@ stop_greeter (GdmSimpleSlave *slave)
         }
 
         if (username != NULL) {
-                script_successful = gdm_slave_run_script (GDM_SLAVE (slave), GDMCONFDIR "/PostLogin", 
username);
+                script_successful = run_script (slave, GDMCONFDIR "/PostLogin", username);
         } else {
                 script_successful = TRUE;
         }
@@ -1220,7 +1250,7 @@ start_launch_environment (GdmSimpleSlave *slave,
         }
 
         /* Run the init script. gdmslave suspends until script has terminated */
-        gdm_slave_run_script (GDM_SLAVE (slave), GDMCONFDIR "/Init", GDM_USERNAME);
+        run_script (slave, GDMCONFDIR "/Init", GDM_USERNAME);
 
         g_debug ("GdmSimpleSlave: Creating greeter on %s %s %s", display_name, display_device, 
display_hostname);
         slave->priv->greeter_environment = create_environment (session_id,
@@ -1331,7 +1361,7 @@ setup_session (GdmSimpleSlave *slave)
                 start_initial_setup (slave);
         } else if (wants_autologin (slave)) {
                 /* Run the init script. gdmslave suspends until script has terminated */
-                gdm_slave_run_script (GDM_SLAVE (slave), GDMCONFDIR "/Init", GDM_USERNAME);
+                run_script (slave, GDMCONFDIR "/Init", GDM_USERNAME);
         } else {
                 start_greeter (slave);
         }
@@ -1691,7 +1721,7 @@ gdm_simple_slave_stop (GdmSlave *slave)
                  */
                 username = gdm_session_get_username (self->priv->session);
                 if (username != NULL) {
-                        gdm_slave_run_script (slave, GDMCONFDIR "/PostSession", username);
+                        run_script (self, GDMCONFDIR "/PostSession", username);
                 }
 
 #ifdef  HAVE_LOGINDEVPERM
diff --git a/daemon/gdm-slave.c b/daemon/gdm-slave.c
index 31dff56..b382451 100644
--- a/daemon/gdm-slave.c
+++ b/daemon/gdm-slave.c
@@ -138,175 +138,6 @@ gdm_slave_error_quark (void)
         return ret;
 }
 
-static void
-listify_hash (const char *key,
-              const char *value,
-              GPtrArray  *env)
-{
-        char *str;
-        str = g_strdup_printf ("%s=%s", key, value);
-        g_debug ("GdmSlave: script environment: %s", str);
-        g_ptr_array_add (env, str);
-}
-
-static GPtrArray *
-get_script_environment (GdmSlave   *slave,
-                        const char *username)
-{
-        GPtrArray     *env;
-        GHashTable    *hash;
-        struct passwd *pwent;
-
-        env = g_ptr_array_new ();
-
-        /* create a hash table of current environment, then update keys has necessary */
-        hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
-
-        /* modify environment here */
-        g_hash_table_insert (hash, g_strdup ("HOME"), g_strdup ("/"));
-        g_hash_table_insert (hash, g_strdup ("PWD"), g_strdup ("/"));
-        g_hash_table_insert (hash, g_strdup ("SHELL"), g_strdup ("/bin/sh"));
-
-        if (username != NULL) {
-                g_hash_table_insert (hash, g_strdup ("LOGNAME"),
-                                     g_strdup (username));
-                g_hash_table_insert (hash, g_strdup ("USER"),
-                                     g_strdup (username));
-                g_hash_table_insert (hash, g_strdup ("USERNAME"),
-                                     g_strdup (username));
-
-                gdm_get_pwent_for_name (username, &pwent);
-                if (pwent != NULL) {
-                        if (pwent->pw_dir != NULL && pwent->pw_dir[0] != '\0') {
-                                g_hash_table_insert (hash, g_strdup ("HOME"),
-                                                     g_strdup (pwent->pw_dir));
-                                g_hash_table_insert (hash, g_strdup ("PWD"),
-                                                     g_strdup (pwent->pw_dir));
-                        }
-
-                        g_hash_table_insert (hash, g_strdup ("SHELL"),
-                                             g_strdup (pwent->pw_shell));
-                }
-        }
-
-        if (! slave->priv->display_is_local) {
-                g_hash_table_insert (hash, g_strdup ("REMOTE_HOST"), g_strdup 
(slave->priv->display_hostname));
-        }
-
-        /* Runs as root */
-        g_hash_table_insert (hash, g_strdup ("XAUTHORITY"), g_strdup 
(slave->priv->display_x11_authority_file));
-        g_hash_table_insert (hash, g_strdup ("DISPLAY"), g_strdup (slave->priv->display_name));
-        g_hash_table_insert (hash, g_strdup ("PATH"), g_strdup (GDM_SESSION_DEFAULT_PATH));
-        g_hash_table_insert (hash, g_strdup ("RUNNING_UNDER_GDM"), g_strdup ("true"));
-
-        g_hash_table_remove (hash, "MAIL");
-
-
-        g_hash_table_foreach (hash, (GHFunc)listify_hash, env);
-        g_hash_table_destroy (hash);
-
-        g_ptr_array_add (env, NULL);
-
-        return env;
-}
-
-gboolean
-gdm_slave_run_script (GdmSlave   *slave,
-                      const char *dir,
-                      const char *login)
-{
-        char      *script;
-        char     **argv;
-        gint       status;
-        GError    *error;
-        GPtrArray *env;
-        gboolean   res;
-        gboolean   ret;
-
-        ret = FALSE;
-
-        g_assert (dir != NULL);
-        g_assert (login != NULL);
-
-        script = g_build_filename (dir, slave->priv->display_name, NULL);
-        g_debug ("GdmSlave: Trying script %s", script);
-        if (! (g_file_test (script, G_FILE_TEST_IS_REGULAR)
-               && g_file_test (script, G_FILE_TEST_IS_EXECUTABLE))) {
-                g_debug ("GdmSlave: script %s not found; skipping", script);
-                g_free (script);
-                script = NULL;
-        }
-
-        if (script == NULL
-            && slave->priv->display_hostname != NULL
-            && slave->priv->display_hostname[0] != '\0') {
-                script = g_build_filename (dir, slave->priv->display_hostname, NULL);
-                g_debug ("GdmSlave: Trying script %s", script);
-                if (! (g_file_test (script, G_FILE_TEST_IS_REGULAR)
-                       && g_file_test (script, G_FILE_TEST_IS_EXECUTABLE))) {
-                        g_debug ("GdmSlave: script %s not found; skipping", script);
-                        g_free (script);
-                        script = NULL;
-                }
-        }
-
-        if (script == NULL) {
-                script = g_build_filename (dir, "Default", NULL);
-                g_debug ("GdmSlave: Trying script %s", script);
-                if (! (g_file_test (script, G_FILE_TEST_IS_REGULAR)
-                       && g_file_test (script, G_FILE_TEST_IS_EXECUTABLE))) {
-                        g_debug ("GdmSlave: script %s not found; skipping", script);
-                        g_free (script);
-                        script = NULL;
-                }
-        }
-
-        if (script == NULL) {
-                g_debug ("GdmSlave: no script found");
-                return TRUE;
-        }
-
-        g_debug ("GdmSlave: Running process: %s", script);
-        error = NULL;
-        if (! g_shell_parse_argv (script, NULL, &argv, &error)) {
-                g_warning ("Could not parse command: %s", error->message);
-                g_error_free (error);
-                goto out;
-        }
-
-        env = get_script_environment (slave, login);
-
-        res = g_spawn_sync (NULL,
-                            argv,
-                            (char **)env->pdata,
-                            G_SPAWN_SEARCH_PATH,
-                            NULL,
-                            NULL,
-                            NULL,
-                            NULL,
-                            &status,
-                            &error);
-
-        g_ptr_array_foreach (env, (GFunc)g_free, NULL);
-        g_ptr_array_free (env, TRUE);
-        g_strfreev (argv);
-
-        if (! res) {
-                g_warning ("GdmSlave: Unable to run script: %s", error->message);
-                g_error_free (error);
-        }
-
-        if (WIFEXITED (status)) {
-                g_debug ("GdmSlave: Process exit status: %d", WEXITSTATUS (status));
-                ret = WEXITSTATUS (status) == 0;
-        }
-
- out:
-        g_free (script);
-
-        return ret;
-}
-
 static XRRScreenResources *
 get_screen_resources (Display *dpy)
 {
@@ -773,8 +604,7 @@ gdm_slave_add_user_authorization (GdmSlave   *slave,
 
 static char *
 gdm_slave_parse_enriched_login (GdmSlave   *slave,
-                                const char *username,
-                                const char *display_name)
+                                const char *username)
 {
         char     **argv;
         int        username_len;
@@ -817,7 +647,10 @@ gdm_slave_parse_enriched_login (GdmSlave   *slave,
         g_debug ("GdmSlave: running '%s' to acquire auto/timed username", command);
         g_free (command);
 
-        env = get_script_environment (slave, NULL);
+        env = gdm_get_script_environment (username,
+                                          slave->priv->display_name,
+                                          slave->priv->display_hostname,
+                                          slave->priv->display_x11_authority_file);
 
         error = NULL;
         std_output = NULL;
@@ -892,9 +725,7 @@ gdm_slave_get_timed_login_details (GdmSlave   *slave,
         }
 
         if (usernamep != NULL) {
-                *usernamep = gdm_slave_parse_enriched_login (slave,
-                                                             username,
-                                                             slave->priv->display_name);
+                *usernamep = gdm_slave_parse_enriched_login (slave, username);
         } else {
                 g_free (username);
 
diff --git a/daemon/gdm-slave.h b/daemon/gdm-slave.h
index c442806..57cf2be 100644
--- a/daemon/gdm-slave.h
+++ b/daemon/gdm-slave.h
@@ -107,9 +107,6 @@ gboolean            gdm_slave_connect_to_x11_display (GdmSlave   *slave);
 
 void                gdm_slave_set_initial_cursor_position (GdmSlave *slave);
 
-gboolean            gdm_slave_run_script             (GdmSlave   *slave,
-                                                      const char *dir,
-                                                      const char *username);
 void                gdm_slave_export_interface       (GdmSlave               *slave,
                                                       GDBusInterfaceSkeleton *interface);
 G_END_DECLS
diff --git a/daemon/gdm-xdmcp-chooser-slave.c b/daemon/gdm-xdmcp-chooser-slave.c
index a21898c..666f2a8 100644
--- a/daemon/gdm-xdmcp-chooser-slave.c
+++ b/daemon/gdm-xdmcp-chooser-slave.c
@@ -218,7 +218,10 @@ run_chooser (GdmXdmcpChooserSlave *slave)
         }
 
         /* Run the init script. gdmslave suspends until script has terminated */
-        gdm_slave_run_script (GDM_SLAVE (slave), GDMCONFDIR "/Init", GDM_USERNAME);
+        gdm_run_script (GDMCONFDIR "/Init", GDM_USERNAME,
+                        display_name,
+                        display_hostname,
+                        auth_file);
 
         g_debug ("GdmXdmcpChooserSlave: Creating chooser on %s %s %s", display_name, display_device, 
display_hostname);
         slave->priv->chooser_environment = create_chooser_session (display_name,


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