Blame SOURCES/0027-systemd-login-check-for-LockedHint-property.patch

a547b4
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
a547b4
From: Victor Toso <victortoso@redhat.com>
a547b4
Date: Fri, 27 May 2016 11:42:29 +0200
a547b4
Subject: [PATCH] systemd-login: check for LockedHint property
a547b4
a547b4
Property introduced in v230 of systemd.
a547b4
a547b4
Systems that don't have up to date systemd-login will get the
a547b4
following log message:
a547b4
a547b4
"Properties.Get failed (locked-hint) due Unknown property or interface."
a547b4
a547b4
Resolves: rhbz#1323623
a547b4
Acked-by: Pavel Grunt <pgrunt@redhat.com>
a547b4
(cherry picked from commit ec843a21b29d7fa21ba3393b84368bc2e39d3ce7)
a547b4
---
a547b4
 src/systemd-login.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++-----
a547b4
 1 file changed, 98 insertions(+), 10 deletions(-)
a547b4
a547b4
diff --git a/src/systemd-login.c b/src/systemd-login.c
a547b4
index ff9a3be..1b0b6f1 100644
a547b4
--- a/src/systemd-login.c
a547b4
+++ b/src/systemd-login.c
a547b4
@@ -36,14 +36,21 @@ struct session_info {
a547b4
         char *match_session_signals;
a547b4
     } dbus;
a547b4
     gboolean session_is_locked;
a547b4
+    gboolean session_locked_hint;
a547b4
 };
a547b4
 
a547b4
+#define LOGIND_INTERFACE            "org.freedesktop.login1"
a547b4
+
a547b4
 #define LOGIND_SESSION_INTERFACE    "org.freedesktop.login1.Session"
a547b4
-#define LOGIND_SESSION_OBJ_TEMPLATE "'/org/freedesktop/login1/session/_3%s'"
a547b4
+#define LOGIND_SESSION_OBJ_TEMPLATE "/org/freedesktop/login1/session/_3%s"
a547b4
+
a547b4
+#define DBUS_PROPERTIES_INTERFACE   "org.freedesktop.DBus.Properties"
a547b4
 
a547b4
 #define SESSION_SIGNAL_LOCK         "Lock"
a547b4
 #define SESSION_SIGNAL_UNLOCK       "Unlock"
a547b4
 
a547b4
+#define SESSION_PROP_LOCKED_HINT    "LockedHint"
a547b4
+
a547b4
 /* dbus related */
a547b4
 static DBusConnection *si_dbus_get_system_bus(void)
a547b4
 {
a547b4
@@ -91,8 +98,8 @@ static void si_dbus_match_rule_update(struct session_info *si)
a547b4
     si_dbus_match_remove(si);
a547b4
 
a547b4
     si->dbus.match_session_signals =
a547b4
-        g_strdup_printf ("type='signal',interface='%s',path="
a547b4
-                         LOGIND_SESSION_OBJ_TEMPLATE,
a547b4
+        g_strdup_printf ("type='signal',interface='%s',path='"
a547b4
+                         LOGIND_SESSION_OBJ_TEMPLATE"'",
a547b4
                          LOGIND_SESSION_INTERFACE,
a547b4
                          si->session);
a547b4
     if (si->verbose)
a547b4
@@ -112,6 +119,84 @@ static void si_dbus_match_rule_update(struct session_info *si)
a547b4
 }
a547b4
 
a547b4
 static void
a547b4
+si_dbus_read_properties(struct session_info *si)
a547b4
+{
a547b4
+    dbus_bool_t locked_hint, ret;
a547b4
+    DBusMessageIter iter, iter_variant;
a547b4
+    gint type;
a547b4
+    DBusError error;
a547b4
+    DBusMessage *message = NULL;
a547b4
+    DBusMessage *reply = NULL;
a547b4
+    gchar *session_object;
a547b4
+    const gchar *interface, *property;
a547b4
+
a547b4
+    if (si->session == NULL)
a547b4
+        return;
a547b4
+
a547b4
+    session_object = g_strdup_printf(LOGIND_SESSION_OBJ_TEMPLATE, si->session);
a547b4
+    message = dbus_message_new_method_call(LOGIND_INTERFACE,
a547b4
+                                           session_object,
a547b4
+                                           DBUS_PROPERTIES_INTERFACE,
a547b4
+                                           "Get");
a547b4
+    g_free (session_object);
a547b4
+    if (message == NULL) {
a547b4
+        syslog(LOG_ERR, "Unable to create dbus message");
a547b4
+        goto exit;
a547b4
+    }
a547b4
+
a547b4
+    interface = LOGIND_SESSION_INTERFACE;
a547b4
+    property = SESSION_PROP_LOCKED_HINT;
a547b4
+    ret = dbus_message_append_args(message,
a547b4
+                                   DBUS_TYPE_STRING, &interface,
a547b4
+                                   DBUS_TYPE_STRING, &property,
a547b4
+                                   DBUS_TYPE_INVALID);
a547b4
+    if (!ret) {
a547b4
+        syslog(LOG_ERR, "Unable to request locked-hint");
a547b4
+        goto exit;
a547b4
+    }
a547b4
+
a547b4
+    dbus_error_init(&error);
a547b4
+    reply = dbus_connection_send_with_reply_and_block(si->dbus.system_connection,
a547b4
+                                                      message,
a547b4
+                                                      -1,
a547b4
+                                                      &error);
a547b4
+    if (reply == NULL) {
a547b4
+        if (dbus_error_is_set(&error)) {
a547b4
+            syslog(LOG_ERR, "Properties.Get failed (locked-hint) due %s", error.message);
a547b4
+            dbus_error_free(&error);
a547b4
+        } else {
a547b4
+            syslog(LOG_ERR, "Properties.Get failed (locked-hint)");
a547b4
+        }
a547b4
+        goto exit;
a547b4
+    }
a547b4
+
a547b4
+    dbus_message_iter_init(reply, &iter);
a547b4
+    type = dbus_message_iter_get_arg_type(&iter);
a547b4
+    if (type != DBUS_TYPE_VARIANT) {
a547b4
+        syslog(LOG_ERR, "expected a variant, got a '%c' instead", type);
a547b4
+        goto exit;
a547b4
+    }
a547b4
+
a547b4
+    dbus_message_iter_recurse(&iter, &iter_variant);
a547b4
+    type = dbus_message_iter_get_arg_type(&iter_variant);
a547b4
+    if (type != DBUS_TYPE_BOOLEAN) {
a547b4
+        syslog(LOG_ERR, "expected a boolean, got a '%c' instead", type);
a547b4
+        goto exit;
a547b4
+    }
a547b4
+    dbus_message_iter_get_basic(&iter_variant, &locked_hint);
a547b4
+
a547b4
+    si->session_locked_hint = (locked_hint) ? TRUE : FALSE;
a547b4
+exit:
a547b4
+    if (reply != NULL) {
a547b4
+        dbus_message_unref(reply);
a547b4
+    }
a547b4
+
a547b4
+    if (message != NULL) {
a547b4
+        dbus_message_unref(message);
a547b4
+    }
a547b4
+}
a547b4
+
a547b4
+static void
a547b4
 si_dbus_read_signals(struct session_info *si)
a547b4
 {
a547b4
     DBusMessage *message = NULL;
a547b4
@@ -220,16 +305,19 @@ char *session_info_session_for_pid(struct session_info *si, uint32_t pid)
a547b4
 
a547b4
 gboolean session_info_session_is_locked(struct session_info *si)
a547b4
 {
a547b4
-    g_return_val_if_fail (si != NULL, FALSE);
a547b4
+    gboolean locked;
a547b4
 
a547b4
-    /* We could also rely on IdleHint property from Session which seems to work
a547b4
-     * well in rhel7 but it wasn't working well in my own system (F23). I'm
a547b4
-     * convinced for now that Lock/Unlock signals should be enough but that
a547b4
-     * means Lock/Unlock being done by logind. That might take a while.
a547b4
-     * Check: https://bugzilla.gnome.org/show_bug.cgi?id=764773 */
a547b4
+    g_return_val_if_fail (si != NULL, FALSE);
a547b4
 
a547b4
     si_dbus_read_signals(si);
a547b4
-    return si->session_is_locked;
a547b4
+    si_dbus_read_properties(si);
a547b4
+
a547b4
+    locked = (si->session_is_locked || si->session_locked_hint);
a547b4
+    if (si->verbose) {
a547b4
+        syslog(LOG_DEBUG, "(systemd-login) session is locked: %s",
a547b4
+               locked ? "yes" : "no");
a547b4
+    }
a547b4
+    return locked;
a547b4
 }
a547b4
 
a547b4
 /* This function should only be called after session_info_get_active_session