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