923a60
From 75131b469fa9e1e2e3cb623fa1f3d36cba36af78 Mon Sep 17 00:00:00 2001
923a60
From: Victor Toso <me@victortoso.com>
923a60
Date: Wed, 11 May 2016 19:34:13 +0200
923a60
Subject: [PATCH] logind: introduce LockedHint and SetLockedHint (#3238)
923a60
923a60
Desktop environments can keep this property up to date to allow
923a60
applications to easily track session's Lock status.
923a60
923a60
Cherry-picked from: 42d35e1301928d08dd32ec51f0205252ae658ba5
923a60
Resolves: #1335499
923a60
---
923a60
 src/login/logind-session-dbus.c       | 50 +++++++++++++++++++++++++++
923a60
 src/login/logind-session.c            | 17 +++++++++
923a60
 src/login/logind-session.h            |  4 +++
923a60
 src/login/org.freedesktop.login1.conf |  4 +++
923a60
 4 files changed, 75 insertions(+)
923a60
923a60
diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c
923a60
index 4e7edef52d..75b7186e8f 100644
923a60
--- a/src/login/logind-session-dbus.c
923a60
+++ b/src/login/logind-session-dbus.c
923a60
@@ -180,6 +180,24 @@ static int property_get_idle_since_hint(
923a60
         return sd_bus_message_append(reply, "t", u);
923a60
 }
923a60
 
923a60
+static int property_get_locked_hint(
923a60
+                sd_bus *bus,
923a60
+                const char *path,
923a60
+                const char *interface,
923a60
+                const char *property,
923a60
+                sd_bus_message *reply,
923a60
+                void *userdata,
923a60
+                sd_bus_error *error) {
923a60
+
923a60
+        Session *s = userdata;
923a60
+
923a60
+        assert(bus);
923a60
+        assert(reply);
923a60
+        assert(s);
923a60
+
923a60
+        return sd_bus_message_append(reply, "b", session_get_locked_hint(s) > 0);
923a60
+}
923a60
+
923a60
 static int method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
923a60
         Session *s = userdata;
923a60
         int r;
923a60
@@ -255,6 +273,36 @@ static int method_set_idle_hint(sd_bus *bus, sd_bus_message *message, void *user
923a60
         return sd_bus_reply_method_return(message, NULL);
923a60
 }
923a60
 
923a60
+static int method_set_locked_hint(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
923a60
+        _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
923a60
+        Session *s = userdata;
923a60
+        uid_t uid;
923a60
+        int r, b;
923a60
+
923a60
+        assert(bus);
923a60
+        assert(message);
923a60
+        assert(s);
923a60
+
923a60
+        r = sd_bus_message_read(message, "b", &b);
923a60
+        if (r < 0)
923a60
+                return r;
923a60
+
923a60
+        r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
923a60
+        if (r < 0)
923a60
+                return r;
923a60
+
923a60
+        r = sd_bus_creds_get_euid(creds, &uid);
923a60
+        if (r < 0)
923a60
+                return r;
923a60
+
923a60
+        if (uid != 0 && uid != s->user->uid)
923a60
+                return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may set locked hint");
923a60
+
923a60
+        session_set_locked_hint(s, b);
923a60
+
923a60
+        return sd_bus_reply_method_return(message, NULL);
923a60
+}
923a60
+
923a60
 static int method_kill(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
923a60
         Session *s = userdata;
923a60
         const char *swho;
923a60
@@ -455,6 +503,7 @@ const sd_bus_vtable session_vtable[] = {
923a60
         SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
923a60
         SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
923a60
         SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
923a60
+        SD_BUS_PROPERTY("LockedHint", "b", property_get_locked_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
923a60
 
923a60
         SD_BUS_METHOD("Terminate", NULL, NULL, method_terminate, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
923a60
         SD_BUS_METHOD("Activate", NULL, NULL, method_activate, SD_BUS_VTABLE_UNPRIVILEGED),
923a60
@@ -462,6 +511,7 @@ const sd_bus_vtable session_vtable[] = {
923a60
         SD_BUS_METHOD("Unlock", NULL, NULL, method_lock, 0),
923a60
         SD_BUS_METHOD("SetIdleHint", "b", NULL, method_set_idle_hint, SD_BUS_VTABLE_UNPRIVILEGED),
923a60
         SD_BUS_METHOD("Kill", "si", NULL, method_kill, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
923a60
+        SD_BUS_METHOD("SetLockedHint", "b", NULL, method_set_locked_hint, SD_BUS_VTABLE_UNPRIVILEGED),
923a60
         SD_BUS_METHOD("TakeControl", "b", NULL, method_take_control, SD_BUS_VTABLE_UNPRIVILEGED),
923a60
         SD_BUS_METHOD("ReleaseControl", NULL, NULL, method_release_control, SD_BUS_VTABLE_UNPRIVILEGED),
923a60
         SD_BUS_METHOD("TakeDevice", "uu", "hb", method_take_device, SD_BUS_VTABLE_UNPRIVILEGED),
923a60
diff --git a/src/login/logind-session.c b/src/login/logind-session.c
923a60
index d2e7b40124..dc24539f12 100644
923a60
--- a/src/login/logind-session.c
923a60
+++ b/src/login/logind-session.c
923a60
@@ -843,6 +843,23 @@ void session_set_idle_hint(Session *s, bool b) {
923a60
         manager_send_changed(s->manager, "IdleHint", "IdleSinceHint", "IdleSinceHintMonotonic", NULL);
923a60
 }
923a60
 
923a60
+int session_get_locked_hint(Session *s) {
923a60
+        assert(s);
923a60
+
923a60
+        return s->locked_hint;
923a60
+}
923a60
+
923a60
+void session_set_locked_hint(Session *s, bool b) {
923a60
+        assert(s);
923a60
+
923a60
+        if (s->locked_hint == b)
923a60
+                return;
923a60
+
923a60
+        s->locked_hint = b;
923a60
+
923a60
+        session_send_changed(s, "LockedHint", NULL);
923a60
+}
923a60
+
923a60
 static int session_dispatch_fifo(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
923a60
         Session *s = userdata;
923a60
 
923a60
diff --git a/src/login/logind-session.h b/src/login/logind-session.h
923a60
index a007fb5e84..5002b68689 100644
923a60
--- a/src/login/logind-session.h
923a60
+++ b/src/login/logind-session.h
923a60
@@ -111,6 +111,8 @@ struct Session {
923a60
         bool idle_hint;
923a60
         dual_timestamp idle_hint_timestamp;
923a60
 
923a60
+        bool locked_hint;
923a60
+
923a60
         bool in_gc_queue:1;
923a60
         bool started:1;
923a60
         bool stopping:1;
923a60
@@ -137,6 +139,8 @@ int session_activate(Session *s);
923a60
 bool session_is_active(Session *s);
923a60
 int session_get_idle_hint(Session *s, dual_timestamp *t);
923a60
 void session_set_idle_hint(Session *s, bool b);
923a60
+int session_get_locked_hint(Session *s);
923a60
+void session_set_locked_hint(Session *s, bool b);
923a60
 int session_create_fifo(Session *s);
923a60
 int session_start(Session *s);
923a60
 int session_stop(Session *s, bool force);
923a60
diff --git a/src/login/org.freedesktop.login1.conf b/src/login/org.freedesktop.login1.conf
923a60
index 1318328aa0..dc7e0bec20 100644
923a60
--- a/src/login/org.freedesktop.login1.conf
923a60
+++ b/src/login/org.freedesktop.login1.conf
923a60
@@ -160,6 +160,10 @@
923a60
                        send_interface="org.freedesktop.login1.Session"
923a60
                        send_member="SetIdleHint"/>
923a60
 
923a60
+                
923a60
+                       send_interface="org.freedesktop.login1.Session"
923a60
+                       send_member="SetLockedHint"/>
923a60
+
923a60
                 
923a60
                        send_interface="org.freedesktop.login1.Session"
923a60
                        send_member="TakeControl"/>