Blame SOURCES/0007-common-dedupe-gdm_get_login_window_session_id.patch

400dab
From 2fc9a5f9db7c9d2ab828bcff4ee5dec9c3cf3d3c Mon Sep 17 00:00:00 2001
400dab
From: Ray Strode <rstrode@redhat.com>
400dab
Date: Wed, 1 Aug 2018 16:34:30 -0400
400dab
Subject: [PATCH 07/51] common: dedupe gdm_get_login_window_session_id
400dab
400dab
Right now there are two slightly different cut-and-pastes of
400dab
the function to get the session id of the login session in
400dab
the code.
400dab
400dab
This commit deduplicates them.
400dab
---
400dab
 common/gdm-common.c  | 47 ++++++++++++++++++++++++++++++++------------
400dab
 common/gdm-common.h  |  2 ++
400dab
 daemon/gdm-manager.c |  4 ++--
400dab
 3 files changed, 38 insertions(+), 15 deletions(-)
400dab
400dab
diff --git a/common/gdm-common.c b/common/gdm-common.c
400dab
index c44fa998d..00daf0df8 100644
400dab
--- a/common/gdm-common.c
400dab
+++ b/common/gdm-common.c
400dab
@@ -364,186 +364,207 @@ create_transient_display (GDBusConnection *connection,
400dab
 
400dab
 static gboolean
400dab
 activate_session_id (GDBusConnection *connection,
400dab
                      const char      *seat_id,
400dab
                      const char      *session_id)
400dab
 {
400dab
         GError *local_error = NULL;
400dab
         GVariant *reply;
400dab
 
400dab
         reply = g_dbus_connection_call_sync (connection,
400dab
                                              "org.freedesktop.login1",
400dab
                                              "/org/freedesktop/login1",
400dab
                                              "org.freedesktop.login1.Manager",
400dab
                                              "ActivateSessionOnSeat",
400dab
                                              g_variant_new ("(ss)", session_id, seat_id),
400dab
                                              NULL,
400dab
                                              G_DBUS_CALL_FLAGS_NONE,
400dab
                                              -1,
400dab
                                              NULL, &local_error);
400dab
         if (reply == NULL) {
400dab
                 g_warning ("Unable to activate session: %s", local_error->message);
400dab
                 g_error_free (local_error);
400dab
                 return FALSE;
400dab
         }
400dab
 
400dab
         g_variant_unref (reply);
400dab
 
400dab
         return TRUE;
400dab
 }
400dab
 
400dab
-static gboolean
400dab
-get_login_window_session_id (const char  *seat_id,
400dab
-                             char       **session_id)
400dab
+gboolean
400dab
+gdm_get_login_window_session_id (const char  *seat_id,
400dab
+		                 char       **session_id)
400dab
 {
400dab
         gboolean   ret;
400dab
         int        res, i;
400dab
         char     **sessions;
400dab
+        char      *service_id;
400dab
         char      *service_class;
400dab
         char      *state;
400dab
 
400dab
         res = sd_seat_get_sessions (seat_id, &sessions, NULL, NULL);
400dab
         if (res < 0) {
400dab
                 g_debug ("Failed to determine sessions: %s", strerror (-res));
400dab
                 return FALSE;
400dab
         }
400dab
 
400dab
         if (sessions == NULL || sessions[0] == NULL) {
400dab
                 *session_id = NULL;
400dab
-                ret = TRUE;
400dab
+                ret = FALSE;
400dab
                 goto out;
400dab
         }
400dab
 
400dab
         for (i = 0; sessions[i]; i ++) {
400dab
+
400dab
                 res = sd_session_get_class (sessions[i], &service_class);
400dab
                 if (res < 0) {
400dab
+                        if (res == -ENOENT) {
400dab
+                                free (service_class);
400dab
+                                continue;
400dab
+                        }
400dab
+
400dab
                         g_debug ("failed to determine class of session %s: %s", sessions[i], strerror (-res));
400dab
                         ret = FALSE;
400dab
                         goto out;
400dab
                 }
400dab
 
400dab
                 if (strcmp (service_class, "greeter") != 0) {
400dab
                         free (service_class);
400dab
                         continue;
400dab
                 }
400dab
 
400dab
                 free (service_class);
400dab
 
400dab
                 ret = sd_session_get_state (sessions[i], &state);
400dab
                 if (ret < 0) {
400dab
                         g_debug ("failed to determine state of session %s: %s", sessions[i], strerror (-res));
400dab
                         ret = FALSE;
400dab
                         goto out;
400dab
                 }
400dab
 
400dab
                 if (g_strcmp0 (state, "closing") == 0) {
400dab
                         free (state);
400dab
                         continue;
400dab
                 }
400dab
                 free (state);
400dab
 
400dab
-                *session_id = g_strdup (sessions[i]);
400dab
-                ret = TRUE;
400dab
-                break;
400dab
+                res = sd_session_get_service (sessions[i], &service_id);
400dab
+                if (res < 0) {
400dab
+                        g_debug ("failed to determine service of session %s: %s", sessions[i], strerror (-res));
400dab
+                        ret = FALSE;
400dab
+                        goto out;
400dab
+                }
400dab
 
400dab
+                if (strcmp (service_id, "gdm-launch-environment") == 0) {
400dab
+                        *session_id = g_strdup (sessions[i]);
400dab
+                        ret = TRUE;
400dab
+
400dab
+                        free (service_id);
400dab
+                        goto out;
400dab
+                }
400dab
+
400dab
+                free (service_id);
400dab
         }
400dab
 
400dab
         *session_id = NULL;
400dab
-        ret = TRUE;
400dab
+        ret = FALSE;
400dab
 
400dab
 out:
400dab
-        for (i = 0; sessions[i]; i ++) {
400dab
-                free (sessions[i]);
400dab
-        }
400dab
+        if (sessions) {
400dab
+                for (i = 0; sessions[i]; i ++) {
400dab
+                        free (sessions[i]);
400dab
+                }
400dab
 
400dab
-        free (sessions);
400dab
+                free (sessions);
400dab
+        }
400dab
 
400dab
         return ret;
400dab
 }
400dab
 
400dab
 static gboolean
400dab
 goto_login_session (GDBusConnection  *connection,
400dab
                     GError          **error)
400dab
 {
400dab
         gboolean        ret;
400dab
         int             res;
400dab
         char           *our_session;
400dab
         char           *session_id;
400dab
         char           *seat_id;
400dab
 
400dab
         ret = FALSE;
400dab
         session_id = NULL;
400dab
         seat_id = NULL;
400dab
 
400dab
         /* First look for any existing LoginWindow sessions on the seat.
400dab
            If none are found, create a new one. */
400dab
 
400dab
         /* Note that we mostly use free () here, instead of g_free ()
400dab
          * since the data allocated is from libsystemd-logind, which
400dab
          * does not use GLib's g_malloc (). */
400dab
 
400dab
         res = sd_pid_get_session (0, &our_session);
400dab
         if (res < 0) {
400dab
                 g_debug ("failed to determine own session: %s", strerror (-res));
400dab
                 g_set_error (error, GDM_COMMON_ERROR, 0, _("Could not identify the current session."));
400dab
 
400dab
                 return FALSE;
400dab
         }
400dab
 
400dab
         res = sd_session_get_seat (our_session, &seat_id);
400dab
         free (our_session);
400dab
         if (res < 0) {
400dab
                 g_debug ("failed to determine own seat: %s", strerror (-res));
400dab
                 g_set_error (error, GDM_COMMON_ERROR, 0, _("Could not identify the current seat."));
400dab
 
400dab
                 return FALSE;
400dab
         }
400dab
 
400dab
         res = sd_seat_can_multi_session (seat_id);
400dab
         if (res < 0) {
400dab
                 free (seat_id);
400dab
 
400dab
                 g_debug ("failed to determine whether seat can do multi session: %s", strerror (-res));
400dab
                 g_set_error (error, GDM_COMMON_ERROR, 0, _("The system is unable to determine whether to switch to an existing login screen or start up a new login screen."));
400dab
 
400dab
                 return FALSE;
400dab
         }
400dab
 
400dab
         if (res == 0) {
400dab
                 free (seat_id);
400dab
 
400dab
                 g_set_error (error, GDM_COMMON_ERROR, 0, _("The system is unable to start up a new login screen."));
400dab
 
400dab
                 return FALSE;
400dab
         }
400dab
 
400dab
-        res = get_login_window_session_id (seat_id, &session_id);
400dab
+        res = gdm_get_login_window_session_id (seat_id, &session_id);
400dab
         if (res && session_id != NULL) {
400dab
                 res = activate_session_id (connection, seat_id, session_id);
400dab
 
400dab
                 if (res) {
400dab
                         ret = TRUE;
400dab
                 }
400dab
         }
400dab
 
400dab
         if (! ret && g_strcmp0 (seat_id, "seat0") == 0) {
400dab
                 res = create_transient_display (connection, error);
400dab
                 if (res) {
400dab
                         ret = TRUE;
400dab
                 }
400dab
         }
400dab
 
400dab
         free (seat_id);
400dab
         g_free (session_id);
400dab
 
400dab
         return ret;
400dab
 }
400dab
 
400dab
 gboolean
400dab
 gdm_goto_login_session (GError **error)
400dab
 {
400dab
         GError *local_error;
400dab
         GDBusConnection *connection;
400dab
 
400dab
         local_error = NULL;
400dab
         connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &local_error);
400dab
         if (connection == NULL) {
400dab
diff --git a/common/gdm-common.h b/common/gdm-common.h
400dab
index 8d83a1246..c9cbd9c48 100644
400dab
--- a/common/gdm-common.h
400dab
+++ b/common/gdm-common.h
400dab
@@ -26,51 +26,53 @@
400dab
 #include <errno.h>
400dab
 
400dab
 #define        VE_IGNORE_EINTR(expr) \
400dab
         do {                         \
400dab
                 errno = 0;           \
400dab
                 expr;                \
400dab
         } while G_UNLIKELY (errno == EINTR);
400dab
 
400dab
 GQuark gdm_common_error_quark (void);
400dab
 #define GDM_COMMON_ERROR gdm_common_error_quark()
400dab
 
400dab
 typedef char * (*GdmExpandVarFunc) (const char *var,
400dab
                                     gpointer user_data);
400dab
 
400dab
 G_BEGIN_DECLS
400dab
 
400dab
 int            gdm_wait_on_pid           (int pid);
400dab
 int            gdm_wait_on_and_disown_pid (int pid,
400dab
                                            int timeout);
400dab
 int            gdm_signal_pid            (int pid,
400dab
                                           int signal);
400dab
 gboolean       gdm_get_pwent_for_name    (const char     *name,
400dab
                                           struct passwd **pwentp);
400dab
 
400dab
 gboolean       gdm_clear_close_on_exec_flag (int fd);
400dab
 
400dab
 const char *   gdm_make_temp_dir         (char    *template);
400dab
 
400dab
 char          *gdm_generate_random_bytes (gsize          size,
400dab
                                           GError       **error);
400dab
+gboolean       gdm_get_login_window_session_id (const char  *seat_id,
400dab
+                                                char       **session_id);
400dab
 gboolean       gdm_goto_login_session    (GError **error);
400dab
 
400dab
 GPtrArray     *gdm_get_script_environment (const char *username,
400dab
                                            const char *display_name,
400dab
                                            const char *display_hostname,
400dab
                                            const char *display_x11_authority_file);
400dab
 gboolean       gdm_run_script             (const char *dir,
400dab
                                            const char *username,
400dab
                                            const char *display_name,
400dab
                                            const char *display_hostname,
400dab
                                            const char *display_x11_authority_file);
400dab
 
400dab
 gboolean      gdm_shell_var_is_valid_char (char c,
400dab
                                            gboolean first);
400dab
 char *        gdm_shell_expand            (const char *str,
400dab
                                            GdmExpandVarFunc expand_func,
400dab
                                            gpointer user_data);
400dab
 
400dab
 G_END_DECLS
400dab
 
400dab
 #endif /* _GDM_COMMON_H */
400dab
diff --git a/daemon/gdm-manager.c b/daemon/gdm-manager.c
400dab
index 7a5554e9d..375ef6f80 100644
400dab
--- a/daemon/gdm-manager.c
400dab
+++ b/daemon/gdm-manager.c
400dab
@@ -1444,61 +1444,61 @@ get_login_window_session_id (const char  *seat_id,
400dab
                         ret = TRUE;
400dab
 
400dab
                         free (service_id);
400dab
                         goto out;
400dab
                 }
400dab
 
400dab
                 free (service_id);
400dab
         }
400dab
 
400dab
         *session_id = NULL;
400dab
         ret = FALSE;
400dab
 
400dab
 out:
400dab
         if (sessions) {
400dab
                 for (i = 0; sessions[i]; i ++) {
400dab
                         free (sessions[i]);
400dab
                 }
400dab
 
400dab
                 free (sessions);
400dab
         }
400dab
 
400dab
         return ret;
400dab
 }
400dab
 
400dab
 static void
400dab
 activate_login_window_session_on_seat (GdmManager *self,
400dab
                                        const char *seat_id)
400dab
 {
400dab
         char *session_id;
400dab
 
400dab
-        if (!get_login_window_session_id (seat_id, &session_id)) {
400dab
+        if (!gdm_get_login_window_session_id (seat_id, &session_id)) {
400dab
                 return;
400dab
         }
400dab
 
400dab
         if (session_id) {
400dab
                 activate_session_id (self, seat_id, session_id);
400dab
                 g_free (session_id);
400dab
         }
400dab
 }
400dab
 
400dab
 static void
400dab
 maybe_activate_other_session (GdmManager *self,
400dab
                               GdmDisplay *old_display)
400dab
 {
400dab
         char *seat_id = NULL;
400dab
         char *session_id;
400dab
         int ret;
400dab
 
400dab
         g_object_get (G_OBJECT (old_display),
400dab
                       "seat-id", &seat_id,
400dab
                       NULL);
400dab
 
400dab
         ret = sd_seat_get_active (seat_id, &session_id, NULL);
400dab
 
400dab
         if (ret == 0) {
400dab
                 GdmDisplay *display;
400dab
 
400dab
                 display = gdm_display_store_find (self->priv->display_store,
400dab
                                                   lookup_by_session_id,
400dab
                                                   (gpointer) session_id);
400dab
 
400dab
@@ -2082,61 +2082,61 @@ on_user_session_exited (GdmSession *session,
400dab
 
400dab
 static void
400dab
 on_user_session_died (GdmSession *session,
400dab
                       int         signal_number,
400dab
                       GdmManager *manager)
400dab
 {
400dab
         g_debug ("GdmManager: session died with signal %s", strsignal (signal_number));
400dab
         remove_user_session (manager, session);
400dab
 }
400dab
 
400dab
 static char *
400dab
 get_display_device (GdmManager *manager,
400dab
                     GdmDisplay *display)
400dab
 {
400dab
         /* systemd finds the display device out on its own based on the display */
400dab
         return NULL;
400dab
 }
400dab
 
400dab
 static void
400dab
 on_session_reauthenticated (GdmSession *session,
400dab
                             const char *service_name,
400dab
                             GdmManager *manager)
400dab
 {
400dab
         gboolean fail_if_already_switched = FALSE;
400dab
 
400dab
         if (gdm_session_get_display_mode (session) == GDM_SESSION_DISPLAY_MODE_REUSE_VT) {
400dab
                 const char *seat_id;
400dab
                 char *session_id;
400dab
 
400dab
                 seat_id = gdm_session_get_display_seat_id (session);
400dab
-                if (get_login_window_session_id (seat_id, &session_id)) {
400dab
+                if (gdm_get_login_window_session_id (seat_id, &session_id)) {
400dab
                         GdmDisplay *display = gdm_display_store_find (manager->priv->display_store,
400dab
                                                                       lookup_by_session_id,
400dab
                                                                       (gpointer) session_id);
400dab
 
400dab
                         if (display != NULL) {
400dab
                                 gdm_display_stop_greeter_session (display);
400dab
                                 gdm_display_unmanage (display);
400dab
                                 gdm_display_finish (display);
400dab
                         }
400dab
                         g_free (session_id);
400dab
                 }
400dab
         }
400dab
 
400dab
         /* There should already be a session running, so jump to its
400dab
          * VT. In the event we're already on the right VT, (i.e. user
400dab
          * used an unlock screen instead of a user switched login screen),
400dab
          * then silently succeed and unlock the session.
400dab
          */
400dab
         switch_to_compatible_user_session (manager, session, fail_if_already_switched);
400dab
 }
400dab
 
400dab
 static void
400dab
 on_session_client_ready_for_session_to_start (GdmSession      *session,
400dab
                                               const char      *service_name,
400dab
                                               gboolean         client_is_ready,
400dab
                                               GdmManager      *manager)
400dab
 {
400dab
         gboolean waiting_to_start_user_session;
400dab
 
400dab
         if (client_is_ready) {
400dab
-- 
400dab
2.27.0
400dab