chiark / gitweb /
logind: remove spurious include of <sys/capability.h>
[elogind.git] / src / login / logind-user-dbus.c
index fc71fcf..812a19f 100644 (file)
@@ -24,7 +24,6 @@
 
 #include "strv.h"
 #include "bus-util.h"
-
 #include "logind.h"
 #include "logind-user.h"
 
@@ -34,8 +33,8 @@ static int property_get_display(
                 const char *interface,
                 const char *property,
                 sd_bus_message *reply,
-                sd_bus_error *error,
-                void *userdata) {
+                void *userdata,
+                sd_bus_error *error) {
 
         _cleanup_free_ char *p = NULL;
         User *u = userdata;
@@ -57,8 +56,8 @@ static int property_get_state(
                 const char *interface,
                 const char *property,
                 sd_bus_message *reply,
-                sd_bus_error *error,
-                void *userdata) {
+                void *userdata,
+                sd_bus_error *error) {
 
         User *u = userdata;
 
@@ -75,8 +74,8 @@ static int property_get_sessions(
                 const char *interface,
                 const char *property,
                 sd_bus_message *reply,
-                sd_bus_error *error,
-                void *userdata) {
+                void *userdata,
+                sd_bus_error *error) {
 
         User *u = userdata;
         Session *session;
@@ -116,8 +115,8 @@ static int property_get_idle_hint(
                 const char *interface,
                 const char *property,
                 sd_bus_message *reply,
-                sd_bus_error *error,
-                void *userdata) {
+                void *userdata,
+                sd_bus_error *error) {
 
         User *u = userdata;
 
@@ -134,8 +133,8 @@ static int property_get_idle_since_hint(
                 const char *interface,
                 const char *property,
                 sd_bus_message *reply,
-                sd_bus_error *error,
-                void *userdata) {
+                void *userdata,
+                sd_bus_error *error) {
 
         User *u = userdata;
         dual_timestamp t;
@@ -151,7 +150,28 @@ static int property_get_idle_since_hint(
         return sd_bus_message_append(reply, "t", k);
 }
 
-static int method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata) {
+static int property_get_linger(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        User *u = userdata;
+        int r;
+
+        assert(bus);
+        assert(reply);
+        assert(u);
+
+        r = user_check_linger_file(u);
+
+        return sd_bus_message_append(reply, "b", r > 0);
+}
+
+static int method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
         User *u = userdata;
         int r;
 
@@ -159,14 +179,14 @@ static int method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata
         assert(message);
         assert(u);
 
-        r = user_stop(u);
+        r = user_stop(u, true);
         if (r < 0)
-                return sd_bus_reply_method_errno(bus, message, r, NULL);
+                return r;
 
-        return sd_bus_reply_method_return(bus, message, NULL);
+        return sd_bus_reply_method_return(message, NULL);
 }
 
-static int method_kill(sd_bus *bus, sd_bus_message *message, void *userdata) {
+static int method_kill(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
         User *u = userdata;
         int32_t signo;
         int r;
@@ -177,48 +197,44 @@ static int method_kill(sd_bus *bus, sd_bus_message *message, void *userdata) {
 
         r = sd_bus_message_read(message, "i", &signo);
         if (r < 0)
-                return sd_bus_reply_method_errno(bus, message, r, NULL);
+                return r;
 
         if (signo <= 0 || signo >= _NSIG)
-                return sd_bus_reply_method_errorf(bus, message, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
+                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
 
         r = user_kill(u, signo);
         if (r < 0)
-                return sd_bus_reply_method_errno(bus, message, r, NULL);
+                return r;
 
-        return sd_bus_reply_method_return(bus, message, NULL);
+        return sd_bus_reply_method_return(message, NULL);
 }
 
 const sd_bus_vtable user_vtable[] = {
         SD_BUS_VTABLE_START(0),
 
-        SD_BUS_PROPERTY("UID", "u", bus_property_get_uid, offsetof(User, uid), 0),
-        SD_BUS_PROPERTY("GID", "u", bus_property_get_gid, offsetof(User, gid), 0),
-        SD_BUS_PROPERTY("Name", "s", NULL, offsetof(User, name), 0),
-        SD_BUS_PROPERTY("Timestamp", "t", NULL, offsetof(User, timestamp.realtime), 0),
-        SD_BUS_PROPERTY("TimestampMonotonic", "t", NULL, offsetof(User, timestamp.monotonic), 0),
-        SD_BUS_PROPERTY("RuntimePath", "s", NULL, offsetof(User, runtime_path), 0),
-        SD_BUS_PROPERTY("Service", "s", NULL, offsetof(User, service), 0),
-        SD_BUS_PROPERTY("Slice", "s", NULL, offsetof(User, slice), 0),
-        SD_BUS_PROPERTY("Display", "(so)", property_get_display, 0, 0),
+        SD_BUS_PROPERTY("UID", "u", bus_property_get_uid, offsetof(User, uid), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("GID", "u", bus_property_get_gid, offsetof(User, gid), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("Name", "s", NULL, offsetof(User, name), SD_BUS_VTABLE_PROPERTY_CONST),
+        BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User, timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("RuntimePath", "s", NULL, offsetof(User, runtime_path), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("Service", "s", NULL, offsetof(User, service), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("Slice", "s", NULL, offsetof(User, slice), SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("Display", "(so)", property_get_display, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
         SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+        SD_BUS_PROPERTY("Linger", "b", property_get_linger, 0, 0),
 
-        SD_BUS_METHOD("Terminate", NULL, NULL, method_terminate, 0),
-        SD_BUS_METHOD("Kill", "i", NULL, method_kill, 0),
+        SD_BUS_METHOD("Terminate", NULL, NULL, method_terminate, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
+        SD_BUS_METHOD("Kill", "i", NULL, method_kill, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
 
         SD_BUS_VTABLE_END
 };
 
-int user_object_find(sd_bus *bus, const char *path, const char *interface, void **found, void *userdata) {
-
-        _cleanup_free_ char *e = NULL;
+int user_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
         Manager *m = userdata;
-        unsigned long lu;
-        const char *p;
         User *user;
         int r;
 
@@ -228,17 +244,42 @@ int user_object_find(sd_bus *bus, const char *path, const char *interface, void
         assert(found);
         assert(m);
 
-        p = startswith(path, "/org/freedesktop/login1/user/_");
-        if (!p)
-                return 0;
+        if (streq(path, "/org/freedesktop/login1/user/self")) {
+                _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
+                sd_bus_message *message;
+                pid_t pid;
 
-        r = safe_atolu(p, &lu);
-        if (r < 0)
-                return 0;
+                message = sd_bus_get_current_message(bus);
+                if (!message)
+                        return 0;
 
-        user = hashmap_get(m->users, ULONG_TO_PTR(lu));
-        if (!user)
-                return 0;
+                r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
+                if (r < 0)
+                        return r;
+
+                r = sd_bus_creds_get_pid(creds, &pid);
+                if (r < 0)
+                        return r;
+
+                r = manager_get_user_by_pid(m, pid, &user);
+                if (r <= 0)
+                        return 0;
+        } else {
+                unsigned long lu;
+                const char *p;
+
+                p = startswith(path, "/org/freedesktop/login1/user/_");
+                if (!p)
+                        return 0;
+
+                r = safe_atolu(p, &lu);
+                if (r < 0)
+                        return 0;
+
+                user = hashmap_get(m->users, ULONG_TO_PTR(lu));
+                if (!user)
+                        return 0;
+        }
 
         *found = user;
         return 1;
@@ -249,13 +290,13 @@ char *user_bus_path(User *u) {
 
         assert(u);
 
-        if (asprintf(&s, "/org/freedesktop/login1/user/_%llu", (unsigned long long) u->uid) < 0)
+        if (asprintf(&s, "/org/freedesktop/login1/user/_"UID_FMT, u->uid) < 0)
                 return NULL;
 
         return s;
 }
 
-int user_node_enumerator(sd_bus *bus, const char *path, char ***nodes, void *userdata) {
+int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
         _cleanup_strv_free_ char **l = NULL;
         Manager *m = userdata;
         User *user;
@@ -273,11 +314,9 @@ int user_node_enumerator(sd_bus *bus, const char *path, char ***nodes, void *use
                 if (!p)
                         return -ENOMEM;
 
-                r = strv_push(&l, p);
-                if (r < 0) {
-                        free(p);
+                r = strv_consume(&l, p);
+                if (r < 0)
                         return r;
-                }
         }
 
         *nodes = l;