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

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