[gdm/wip/try-harder-to-get-a-login-screen: 3/5] common: dedupe gdm_get_login_window_session_id



commit 0e9f09da4897bdc4069298c8a4d189cd7f217f81
Author: Ray Strode <rstrode redhat com>
Date:   Wed Aug 1 16:34:30 2018 -0400

    common: dedupe gdm_get_login_window_session_id
    
    Right now there are two slightly different cut-and-pastes of
    the function to get the session id of the login session in
    the code.
    
    This commit deduplicates them.

 common/gdm-common.c  | 47 ++++++++++++++++++-------
 common/gdm-common.h  |  2 ++
 daemon/gdm-manager.c | 99 ++--------------------------------------------------
 3 files changed, 38 insertions(+), 110 deletions(-)
---
diff --git a/common/gdm-common.c b/common/gdm-common.c
index a5b59242..613511c4 100644
--- a/common/gdm-common.c
+++ b/common/gdm-common.c
@@ -381,13 +381,14 @@ activate_session_id (GDBusConnection *connection,
         return TRUE;
 }
 
-static gboolean
-get_login_window_session_id (const char  *seat_id,
-                             char       **session_id)
+gboolean
+gdm_get_login_window_session_id (const char  *seat_id,
+                                char       **session_id)
 {
         gboolean   ret;
         int        res, i;
         char     **sessions;
+        char      *service_id;
         char      *service_class;
         char      *state;
 
@@ -399,13 +400,19 @@ get_login_window_session_id (const char  *seat_id,
 
         if (sessions == NULL || sessions[0] == NULL) {
                 *session_id = NULL;
-                ret = TRUE;
+                ret = FALSE;
                 goto out;
         }
 
         for (i = 0; sessions[i]; i ++) {
+
                 res = sd_session_get_class (sessions[i], &service_class);
                 if (res < 0) {
+                        if (res == -ENOENT) {
+                                free (service_class);
+                                continue;
+                        }
+
                         g_debug ("failed to determine class of session %s: %s", sessions[i], strerror 
(-res));
                         ret = FALSE;
                         goto out;
@@ -431,21 +438,35 @@ get_login_window_session_id (const char  *seat_id,
                 }
                 free (state);
 
-                *session_id = g_strdup (sessions[i]);
-                ret = TRUE;
-                break;
+                res = sd_session_get_service (sessions[i], &service_id);
+                if (res < 0) {
+                        g_debug ("failed to determine service of session %s: %s", sessions[i], strerror 
(-res));
+                        ret = FALSE;
+                        goto out;
+                }
 
+                if (strcmp (service_id, "gdm-launch-environment") == 0) {
+                        *session_id = g_strdup (sessions[i]);
+                        ret = TRUE;
+
+                        free (service_id);
+                        goto out;
+                }
+
+                free (service_id);
         }
 
         *session_id = NULL;
-        ret = TRUE;
+        ret = FALSE;
 
 out:
-        for (i = 0; sessions[i]; i ++) {
-                free (sessions[i]);
-        }
+        if (sessions) {
+                for (i = 0; sessions[i]; i ++) {
+                        free (sessions[i]);
+                }
 
-        free (sessions);
+                free (sessions);
+        }
 
         return ret;
 }
@@ -506,7 +527,7 @@ goto_login_session (GDBusConnection  *connection,
                 return FALSE;
         }
 
-        res = get_login_window_session_id (seat_id, &session_id);
+        res = gdm_get_login_window_session_id (seat_id, &session_id);
         if (res && session_id != NULL) {
                 res = activate_session_id (connection, seat_id, session_id);
 
diff --git a/common/gdm-common.h b/common/gdm-common.h
index e13c3a3d..bd8ac705 100644
--- a/common/gdm-common.h
+++ b/common/gdm-common.h
@@ -51,6 +51,8 @@ gboolean       gdm_clear_close_on_exec_flag (int fd);
 
 char          *gdm_generate_random_bytes (gsize          size,
                                           GError       **error);
+gboolean       gdm_get_login_window_session_id (const char  *seat_id,
+                                                char       **session_id);
 gboolean       gdm_goto_login_session    (GError **error);
 
 GPtrArray     *gdm_get_script_environment (const char *username,
diff --git a/daemon/gdm-manager.c b/daemon/gdm-manager.c
index 2ec8d652..ac30aee9 100644
--- a/daemon/gdm-manager.c
+++ b/daemon/gdm-manager.c
@@ -1318,108 +1318,13 @@ maybe_start_pending_initial_login (GdmManager *manager,
         g_free (user_session_seat_id);
 }
 
-static gboolean
-get_login_window_session_id (const char  *seat_id,
-                             char       **session_id)
-{
-        gboolean   ret;
-        int        res, i;
-        char     **sessions;
-        char      *service_id;
-        char      *service_class;
-        char      *state;
-
-        res = sd_seat_get_sessions (seat_id, &sessions, NULL, NULL);
-        if (res < 0) {
-                g_debug ("Failed to determine sessions: %s", strerror (-res));
-                return FALSE;
-        }
-
-        if (sessions == NULL || sessions[0] == NULL) {
-                *session_id = NULL;
-                ret = FALSE;
-                goto out;
-        }
-
-        for (i = 0; sessions[i]; i ++) {
-
-                res = sd_session_get_class (sessions[i], &service_class);
-                if (res < 0) {
-                        if (res == -ENOENT) {
-                                free (service_class);
-                                continue;
-                        }
-
-                        g_debug ("failed to determine class of session %s: %s", sessions[i], strerror 
(-res));
-                        ret = FALSE;
-                        goto out;
-                }
-
-                if (strcmp (service_class, "greeter") != 0) {
-                        free (service_class);
-                        continue;
-                }
-
-                free (service_class);
-
-                ret = sd_session_get_state (sessions[i], &state);
-                if (ret < 0) {
-                        if (res == -ENOENT)
-                                continue;
-
-                        g_debug ("failed to determine state of session %s: %s", sessions[i], strerror 
(-res));
-                        ret = FALSE;
-                        goto out;
-                }
-
-                if (g_strcmp0 (state, "closing") == 0) {
-                        free (state);
-                        continue;
-                }
-                free (state);
-
-                res = sd_session_get_service (sessions[i], &service_id);
-                if (res < 0) {
-                        if (res == -ENOENT)
-                                continue;
-                        g_debug ("failed to determine service of session %s: %s", sessions[i], strerror 
(-res));
-                        ret = FALSE;
-                        goto out;
-                }
-
-                if (strcmp (service_id, "gdm-launch-environment") == 0) {
-                        *session_id = g_strdup (sessions[i]);
-                        ret = TRUE;
-
-                        free (service_id);
-                        goto out;
-                }
-
-                free (service_id);
-        }
-
-        *session_id = NULL;
-        ret = FALSE;
-
-out:
-        if (sessions) {
-                for (i = 0; sessions[i]; i ++) {
-                        free (sessions[i]);
-                }
-
-                free (sessions);
-        }
-
-        return ret;
-}
-
 static void
 activate_login_window_session_on_seat (GdmManager *self,
                                        const char *seat_id)
 {
         char *session_id;
 
-        if (!get_login_window_session_id (seat_id, &session_id)) {
+        if (!gdm_get_login_window_session_id (seat_id, &session_id)) {
                 return;
         }
 
@@ -2059,7 +1964,7 @@ on_session_reauthenticated (GdmSession *session,
                 char *session_id;
 
                 seat_id = gdm_session_get_display_seat_id (session);
-                if (get_login_window_session_id (seat_id, &session_id)) {
+                if (gdm_get_login_window_session_id (seat_id, &session_id)) {
                         GdmDisplay *display = gdm_display_store_find (manager->priv->display_store,
                                                                       lookup_by_session_id,
                                                                       (gpointer) session_id);


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