richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
923a60
From d3706690bf2953cb8e0362253dc68d77989de3be Mon Sep 17 00:00:00 2001
923a60
From: Lukas Nykryn <lnykryn@redhat.com>
923a60
Date: Fri, 11 Sep 2015 13:37:59 +0200
923a60
Subject: [PATCH] loginctl: print nontrivial properties in logictl show-*
923a60
923a60
Cherry-picked from: 2a998c74028a109d6bd8888951abfa8e25a15fb1
923a60
Resolves: #1260465
923a60
---
923a60
 src/login/loginctl.c | 152 ++++++++++++++++++++++++++++++++++++++++++-
923a60
 src/shared/macro.h   |  13 ++++
923a60
 2 files changed, 162 insertions(+), 3 deletions(-)
923a60
923a60
diff --git a/src/login/loginctl.c b/src/login/loginctl.c
923a60
index b0eede9a34..6c8a59e7c9 100644
923a60
--- a/src/login/loginctl.c
923a60
+++ b/src/login/loginctl.c
923a60
@@ -657,19 +657,165 @@ finish:
923a60
         return r;
923a60
 }
923a60
 
923a60
+static int print_property(const char *name, sd_bus_message *m, const char *contents) {
923a60
+        int r;
923a60
+
923a60
+        assert(name);
923a60
+        assert(m);
923a60
+        assert(contents);
923a60
+
923a60
+        if (arg_property && !strv_find(arg_property, name))
923a60
+                /* skip what we didn't read */
923a60
+                return sd_bus_message_skip(m, contents);
923a60
+
923a60
+        switch (contents[0]) {
923a60
+
923a60
+        case SD_BUS_TYPE_STRUCT_BEGIN:
923a60
+
923a60
+                if (contents[1] == SD_BUS_TYPE_STRING && STR_IN_SET(name, "Display", "Seat", "ActiveSession")) {
923a60
+                        const char *s;
923a60
+
923a60
+                        r = sd_bus_message_read(m, "(so)", &s, NULL);
923a60
+                        if (r < 0)
923a60
+                                return bus_log_parse_error(r);
923a60
+
923a60
+                        if (arg_all || !isempty(s))
923a60
+                                printf("%s=%s\n", name, s);
923a60
+
923a60
+                        return 0;
923a60
+
923a60
+                } else if (contents[1] == SD_BUS_TYPE_UINT32 && streq(name, "User")) {
923a60
+                        uint32_t uid;
923a60
+
923a60
+                        r = sd_bus_message_read(m, "(uo)", &uid, NULL);
923a60
+                        if (r < 0)
923a60
+                                return bus_log_parse_error(r);
923a60
+
923a60
+                        if (UID_IS_INVALID(uid)) {
923a60
+                                log_error("Invalid user ID: " UID_FMT, uid);
923a60
+                                return -EINVAL;
923a60
+                        }
923a60
+
923a60
+                        printf("%s=" UID_FMT "\n", name, uid);
923a60
+
923a60
+                        return 0;
923a60
+                }
923a60
+
923a60
+                break;
923a60
+
923a60
+        case SD_BUS_TYPE_ARRAY:
923a60
+
923a60
+                if (contents[1] == SD_BUS_TYPE_STRUCT_BEGIN && streq(name, "Sessions")) {
923a60
+                        const char *s;
923a60
+                        bool space = false;
923a60
+
923a60
+                        r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "(so)");
923a60
+                        if (r < 0)
923a60
+                                return bus_log_parse_error(r);
923a60
+
923a60
+                        printf("%s=", name);
923a60
+
923a60
+                        while ((r = sd_bus_message_read(m, "(so)", &s, NULL)) > 0) {
923a60
+                                printf("%s%s", space ? " " : "", s);
923a60
+                                space = true;
923a60
+                        }
923a60
+
923a60
+                        printf("\n");
923a60
+
923a60
+                        if (r < 0)
923a60
+                                return bus_log_parse_error(r);
923a60
+
923a60
+                        r = sd_bus_message_exit_container(m);
923a60
+                        if (r < 0)
923a60
+                                return bus_log_parse_error(r);
923a60
+
923a60
+                        return 0;
923a60
+                }
923a60
+
923a60
+                break;
923a60
+        }
923a60
+
923a60
+        r = bus_print_property(name, m, arg_all);
923a60
+        if (r < 0)
923a60
+                return bus_log_parse_error(r);
923a60
+
923a60
+        if (r == 0) {
923a60
+                r = sd_bus_message_skip(m, contents);
923a60
+                if (r < 0)
923a60
+                        return bus_log_parse_error(r);
923a60
+
923a60
+                if (arg_all)
923a60
+                        printf("%s=[unprintable]\n", name);
923a60
+        }
923a60
+
923a60
+        return 0;
923a60
+}
923a60
+
923a60
 static int show_properties(sd_bus *bus, const char *path, bool *new_line) {
923a60
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
923a60
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
923a60
         int r;
923a60
 
923a60
+        assert(bus);
923a60
+        assert(path);
923a60
+        assert(new_line);
923a60
+
923a60
+        r = sd_bus_call_method(
923a60
+                        bus,
923a60
+                        "org.freedesktop.login1",
923a60
+                        path,
923a60
+                        "org.freedesktop.DBus.Properties",
923a60
+                        "GetAll",
923a60
+                        &error,
923a60
+                        &reply,
923a60
+                        "s", "");
923a60
+        if (r < 0)
923a60
+                return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
923a60
+
923a60
+        r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
923a60
+        if (r < 0)
923a60
+                return bus_log_parse_error(r);
923a60
+
923a60
         if (*new_line)
923a60
                 printf("\n");
923a60
 
923a60
         *new_line = true;
923a60
 
923a60
-        r = bus_print_all_properties(bus, "org.freedesktop.login1", path, arg_property, arg_all);
923a60
+        while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
923a60
+                const char *name, *contents;
923a60
+
923a60
+                r = sd_bus_message_read(reply, "s", &name);
923a60
+                if (r < 0)
923a60
+                        return bus_log_parse_error(r);
923a60
+
923a60
+                r = sd_bus_message_peek_type(reply, NULL, &contents);
923a60
+                if (r < 0)
923a60
+                        return bus_log_parse_error(r);
923a60
+
923a60
+                r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
923a60
+                if (r < 0)
923a60
+                        return bus_log_parse_error(r);
923a60
+
923a60
+                r = print_property(name, reply, contents);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
+
923a60
+                r = sd_bus_message_exit_container(reply);
923a60
+                if (r < 0)
923a60
+                        return bus_log_parse_error(r);
923a60
+
923a60
+                r = sd_bus_message_exit_container(reply);
923a60
+                if (r < 0)
923a60
+                        return bus_log_parse_error(r);
923a60
+        }
923a60
         if (r < 0)
923a60
-                log_error_errno(r, "Could not get properties: %m");
923a60
+                return bus_log_parse_error(r);
923a60
 
923a60
-        return r;
923a60
+        r = sd_bus_message_exit_container(reply);
923a60
+        if (r < 0)
923a60
+                return bus_log_parse_error(r);
923a60
+
923a60
+        return 0;
923a60
 }
923a60
 
923a60
 static int show_session(int argc, char *argv[], void *userdata) {
923a60
diff --git a/src/shared/macro.h b/src/shared/macro.h
923a60
index 7f89951d62..9d857dc8d7 100644
923a60
--- a/src/shared/macro.h
923a60
+++ b/src/shared/macro.h
923a60
@@ -26,6 +26,7 @@
923a60
 #include <sys/types.h>
923a60
 #include <sys/uio.h>
923a60
 #include <inttypes.h>
923a60
+#include <stdbool.h>
923a60
 
923a60
 #define _printf_(a,b) __attribute__ ((format (printf, a, b)))
923a60
 #define _alloc_(...) __attribute__ ((alloc_size(__VA_ARGS__)))
923a60
@@ -451,6 +452,18 @@ do {                                                                    \
923a60
 #define GID_INVALID ((gid_t) -1)
923a60
 #define MODE_INVALID ((mode_t) -1)
923a60
 
923a60
+static inline bool UID_IS_INVALID(uid_t uid) {
923a60
+        /* We consider both the old 16bit -1 user and the newer 32bit
923a60
+         * -1 user invalid, since they are or used to be incompatible
923a60
+         * with syscalls such as setresuid() or chown(). */
923a60
+
923a60
+        return uid == (uid_t) ((uint32_t) -1) || uid == (uid_t) ((uint16_t) -1);
923a60
+}
923a60
+
923a60
+static inline bool GID_IS_INVALID(gid_t gid) {
923a60
+        return gid == (gid_t) ((uint32_t) -1) || gid == (gid_t) ((uint16_t) -1);
923a60
+}
923a60
+
923a60
 #define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func)                 \
923a60
         static inline void func##p(type *p) {                   \
923a60
                 if (*p)                                         \