[gnome-screensaver] port gnome-screensaver to systemd-logind



commit 6bb98e9acb9391d1aec8f7dcb44ef7d16b3dcb91
Author: Lennart Poettering <lennart poettering net>
Date:   Fri Feb 10 05:52:51 2012 +0100

    port gnome-screensaver to systemd-logind
    
    On Fedora and other distributions ConsoleKit is being replaced by
    systemd-logind. Accordingly, all tools previously talking to CK need to
    talk to systemd-logind instead.
    
    This patch will dynamically fall back to CK if systemd is not found,
    thus being nice to Debian style distributions which want to support
    systemd and non-systemd systems with the same packages.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=669787

 configure.ac           |   38 +++++++++
 src/Makefile.am        |    2 +
 src/bus.h              |    8 ++
 src/gs-listener-dbus.c |  209 +++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 248 insertions(+), 9 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index a4f990f..29881a2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -574,6 +574,41 @@ if test "x$have_pam" = "xyes"; then
        )
 fi
 
+dnl ---------------------------------------------------------------------------
+dnl ConsoleKit
+dnl ---------------------------------------------------------------------------
+
+AC_ARG_WITH(console-kit,
+            AS_HELP_STRING([--with-console-kit],
+                           [Add ConsoleKit support]),,
+            with_console_kit=auto)
+
+use_console_kit=no
+if test "x$with_console_kit" != "xno" ; then
+        use_console_kit=yes
+        AC_DEFINE(WITH_CONSOLE_KIT, 1, [ConsoleKit support])
+fi
+AM_CONDITIONAL(WITH_CONSOLE_KIT, test x$use_console_kit = xyes)
+AC_SUBST(WITH_CONSOLE_KIT)
+
+dnl ---------------------------------------------------------------------------
+dnl systemd
+dnl ---------------------------------------------------------------------------
+
+AC_ARG_WITH(systemd,
+            AS_HELP_STRING([--with-systemd],
+                           [Add systemd support]),,
+            with_systemd=auto)
+
+use_systemd=no
+if test "x$with_systemd" != "xno" ; then
+        use_systemd=yes
+        AC_DEFINE(WITH_SYSTEMD, 1, [systemd support])
+
+        PKG_CHECK_MODULES(SYSTEMD, libsystemd-login libsystemd-daemon)
+fi
+AM_CONDITIONAL(WITH_SYSTEMD, test x$use_systemd = xyes)
+AC_SUBST(WITH_SYSTEMD)
 
 dnl ---------------------------------------------------------------------------
 dnl libgnomekbd
@@ -738,6 +773,9 @@ echo "
 
         Screen locking enabled:   ${enable_locking}
         Show keyboard indicator:  ${with_kbd_layout_indicator}
+
+        systemd:                  ${use_systemd}
+        ConsoleKit:               ${use_console_kit}
 "
 
 if test "x$have_pam" = "xyes" ; then
diff --git a/src/Makefile.am b/src/Makefile.am
index 0f07b36..1dd5d46 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -28,6 +28,7 @@ INCLUDES =							\
 	$(DBUS_CFLAGS)						\
 	$(LIBGNOMEKBDUI_CFLAGS)					\
 	$(LIBNOTIFY_CFLAGS)					\
+	$(SYSTEMD_CFLAGS)					\
 	$(NULL)
 
 bin_PROGRAMS = \
@@ -191,6 +192,7 @@ gnome_screensaver_SOURCES =	\
 gnome_screensaver_LDADD =		\
 	$(GNOME_SCREENSAVER_LIBS)	\
 	$(SAVER_LIBS)			\
+	$(SYSTEMD_LIBS)                 \
 	$(NULL)
 
 gnome_screensaver_LDFLAGS = -export-dynamic
diff --git a/src/bus.h b/src/bus.h
index d8554a9..44cf916 100644
--- a/src/bus.h
+++ b/src/bus.h
@@ -22,6 +22,14 @@
 #ifndef bus_h
 #define bus_h
 
+/* systemd logind */
+#define SYSTEMD_LOGIND_SERVICE          "org.freedesktop.login1"
+#define SYSTEMD_LOGIND_PATH             "/org/freedesktop/login1"
+#define SYSTEMD_LOGIND_INTERFACE        "org.freedesktop.login1.Manager"
+
+#define SYSTEMD_LOGIND_SESSION_INTERFACE "org.freedesktop.login1.Session"
+#define SYSTEMD_LOGIND_SESSION_PATH     "/org/freedesktop/login1/session"
+
 /* ConsoleKit */
 #define CK_SERVICE                      "org.freedesktop.ConsoleKit"
 #define CK_PATH                         "/org/freedesktop/ConsoleKit"
diff --git a/src/gs-listener-dbus.c b/src/gs-listener-dbus.c
index d4be392..237c11c 100644
--- a/src/gs-listener-dbus.c
+++ b/src/gs-listener-dbus.c
@@ -32,6 +32,11 @@
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
 
+#ifdef WITH_SYSTEMD
+#include <systemd/sd-daemon.h>
+#include <systemd/sd-login.h>
+#endif
+
 #include "gs-listener-dbus.h"
 #include "gs-marshal.h"
 #include "gs-debug.h"
@@ -70,6 +75,10 @@ struct GSListenerPrivate
         time_t          active_start;
         time_t          session_idle_start;
         char           *session_id;
+
+#ifdef WITH_SYSTEMD
+        gboolean        have_systemd;
+#endif
 };
 
 enum {
@@ -731,19 +740,100 @@ _listener_message_path_is_our_session (GSListener  *listener,
                                        DBusMessage *message)
 {
         const char *ssid;
-        gboolean    ours;
-
-        ours = FALSE;
 
         ssid = dbus_message_get_path (message);
-        if (ssid != NULL
-            && listener->priv->session_id != NULL
-            && strcmp (ssid, listener->priv->session_id) == 0) {
-                ours = TRUE;
+        if (ssid == NULL)
+                return FALSE;
+
+        if (listener->priv->session_id == NULL)
+                return FALSE;
+
+#ifdef WITH_SYSTEMD
+        /* The bus object path is simply the actual session ID
+         * prefixed to make it a bus path */
+        if (listener->priv->have_systemd)
+                return g_str_has_prefix (ssid, SYSTEMD_LOGIND_SESSION_PATH "/")
+                        && strcmp (ssid + sizeof (SYSTEMD_LOGIND_SESSION_PATH),
+                                   listener->priv->session_id) == 0;
+#endif
+
+#ifdef WITH_CONSOLE_KIT
+        if (strcmp (ssid, listener->priv->session_id) == 0)
+                return TRUE;
+#endif
+
+        return FALSE;
+}
+
+#ifdef WITH_SYSTEMD
+static gboolean
+properties_changed_match (DBusMessage *message,
+                          const char  *property)
+{
+        DBusMessageIter iter, sub, sub2;
+
+        /* Checks whether a certain property is listed in the
+         * specified PropertiesChanged message */
+
+        if (!dbus_message_iter_init (message, &iter))
+                goto failure;
+
+        /* Jump over interface name */
+        if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
+                goto failure;
+
+        dbus_message_iter_next (&iter);
+
+        /* First, iterate through the changed properties array */
+        if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY ||
+            dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_DICT_ENTRY)
+                goto failure;
+
+        dbus_message_iter_recurse (&iter, &sub);
+        while (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_INVALID) {
+                const char *name;
+
+                if (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_DICT_ENTRY)
+                        goto failure;
+
+                dbus_message_iter_recurse (&sub, &sub2);
+                dbus_message_iter_get_basic (&sub2, &name);
+
+                if (strcmp (name, property) == 0)
+                        return TRUE;
+
+                dbus_message_iter_next (&sub);
         }
 
-        return ours;
+        dbus_message_iter_next (&iter);
+
+        /* Second, iterate through the invalidated properties array */
+        if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY ||
+            dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING)
+                goto failure;
+
+        dbus_message_iter_recurse (&iter, &sub);
+        while (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_INVALID) {
+                const char *name;
+
+                if (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_STRING)
+                        goto failure;
+
+                dbus_message_iter_get_basic (&sub, &name);
+
+                if (strcmp (name, property) == 0)
+                        return TRUE;
+
+                dbus_message_iter_next (&sub);
+        }
+
+        return FALSE;
+
+failure:
+        gs_debug ("Failed to decode PropertiesChanged message.");
+        return FALSE;
 }
+#endif
 
 static DBusHandlerResult
 listener_dbus_handle_system_message (DBusConnection *connection,
@@ -764,6 +854,52 @@ listener_dbus_handle_system_message (DBusConnection *connection,
                   dbus_message_get_destination (message));
 #endif
 
+#ifdef WITH_SYSTEMD
+
+        if (listener->priv->have_systemd) {
+
+                if (dbus_message_is_signal (message, SYSTEMD_LOGIND_SESSION_INTERFACE, "Unlock")) {
+                        if (_listener_message_path_is_our_session (listener, message)) {
+                                gs_debug ("Console kit requested session unlock");
+                                gs_listener_set_active (listener, FALSE);
+                        }
+
+                        return DBUS_HANDLER_RESULT_HANDLED;
+                } else if (dbus_message_is_signal (message, SYSTEMD_LOGIND_SESSION_INTERFACE, "Lock")) {
+                        if (_listener_message_path_is_our_session (listener, message)) {
+                                gs_debug ("ConsoleKit requested session lock");
+                                g_signal_emit (listener, signals [LOCK], 0);
+                        }
+
+                        return DBUS_HANDLER_RESULT_HANDLED;
+                } else if (dbus_message_is_signal (message, DBUS_INTERFACE_PROPERTIES, "PropertiesChanged")) {
+
+                        if (_listener_message_path_is_our_session (listener, message)) {
+
+                                if (properties_changed_match (message, "Active")) {
+                                        gboolean new_active;
+
+                                        /* Instead of going via the
+                                         * bus to read the new
+                                         * property state, let's
+                                         * shortcut this and ask
+                                         * directly the low-level
+                                         * information */
+
+                                        new_active = sd_session_is_active (listener->priv->session_id) != 0;
+                                        if (new_active)
+                                                g_signal_emit (listener, signals [SIMULATE_USER_ACTIVITY], 0);
+                                }
+                        }
+
+                        return DBUS_HANDLER_RESULT_HANDLED;
+                }
+
+                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+        }
+#endif
+
+#ifdef WITH_CONSOLE_KIT
         if (dbus_message_is_signal (message, CK_SESSION_INTERFACE, "Unlock")) {
                 if (_listener_message_path_is_our_session (listener, message)) {
                         gs_debug ("Console kit requested session unlock");
@@ -810,8 +946,9 @@ listener_dbus_handle_system_message (DBusConnection *connection,
 
                 return DBUS_HANDLER_RESULT_HANDLED;
         }
+#endif
 
-        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
 static DBusHandlerResult
@@ -1214,7 +1351,32 @@ gs_listener_acquire (GSListener *listener,
                                             listener_dbus_system_filter_function,
                                             listener,
                                             NULL);
+#ifdef WITH_SYSTEMD
+                if (listener->priv->have_systemd) {
+                        dbus_bus_add_match (listener->priv->system_connection,
+                                            "type='signal'"
+                                            ",sender='"SYSTEMD_LOGIND_SERVICE"'"
+                                            ",interface='"SYSTEMD_LOGIND_SESSION_INTERFACE"'"
+                                            ",member='Unlock'",
+                                            NULL);
+                        dbus_bus_add_match (listener->priv->system_connection,
+                                            "type='signal'"
+                                            ",sender='"SYSTEMD_LOGIND_SERVICE"'"
+                                            ",interface='"SYSTEMD_LOGIND_SESSION_INTERFACE"'"
+                                            ",member='Lock'",
+                                            NULL);
+                        dbus_bus_add_match (listener->priv->system_connection,
+                                            "type='signal'"
+                                            ",sender='"SYSTEMD_LOGIND_SERVICE"'"
+                                            ",interface='"DBUS_INTERFACE_PROPERTIES"'"
+                                            ",member='PropertiesChanged'",
+                                            NULL);
 
+                        goto finish;
+                }
+#endif
+
+#ifdef WITH_CONSOLE_KIT
                 dbus_bus_add_match (listener->priv->system_connection,
                                     "type='signal'"
                                     ",interface='"CK_SESSION_INTERFACE"'"
@@ -1230,8 +1392,10 @@ gs_listener_acquire (GSListener *listener,
                                     ",interface='"CK_SESSION_INTERFACE"'"
                                     ",member='ActiveChanged'",
                                     NULL);
+#endif
         }
 
+finish:
         return (res != -1);
 }
 
@@ -1253,6 +1417,26 @@ query_session_id (GSListener *listener)
 
         dbus_error_init (&error);
 
+#ifdef WITH_SYSTEMD
+        if (listener->priv->have_systemd) {
+                char *t;
+                int r;
+
+                r = sd_pid_get_session (0, &t);
+                if (r < 0) {
+                        gs_debug ("Couldn't determine our own session id: %s", strerror (-r));
+                        return NULL;
+                }
+
+                /* t is allocated with malloc(), we need it with g_malloc() */
+                ssid = g_strdup(t);
+                free (t);
+
+                return ssid;
+        }
+#endif
+
+#ifdef WITH_CONSOLE_KIT
         message = dbus_message_new_method_call (CK_SERVICE, CK_MANAGER_PATH, CK_MANAGER_INTERFACE, "GetCurrentSession");
         if (message == NULL) {
                 gs_debug ("Couldn't allocate the dbus message");
@@ -1277,6 +1461,9 @@ query_session_id (GSListener *listener)
         dbus_message_unref (reply);
 
         return g_strdup (ssid);
+#else
+        return NULL;
+#endif
 }
 
 static void
@@ -1292,6 +1479,10 @@ gs_listener_init (GSListener *listener)
 {
         listener->priv = GS_LISTENER_GET_PRIVATE (listener);
 
+#ifdef WITH_SYSTEMD
+        listener->priv->have_systemd = sd_booted () > 0;
+#endif
+
         gs_listener_dbus_init (listener);
 
         init_session_id (listener);



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