From 5d2f49451c5e697d6e3548cfc9087ef3c16aa969 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Fri, 19 May 2023 13:33:58 +0200 Subject: [PATCH] loginctl: use bus_map_all_properties (cherry picked from commit 5b7d1536d0c2ccf0b7688490f31c92c1e766ea44) Related: #2156786 --- src/login/loginctl.c | 78 ++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 079c0e2b17..9e4c710062 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -47,6 +47,27 @@ static bool arg_ask_password = true; static unsigned arg_lines = 10; static OutputMode arg_output = OUTPUT_SHORT; +typedef struct SessionStatusInfo { + const char *id; + uid_t uid; + const char *name; + struct dual_timestamp timestamp; + unsigned int vtnr; + const char *seat; + const char *tty; + const char *display; + bool remote; + const char *remote_host; + const char *remote_user; + const char *service; + pid_t leader; + const char *type; + const char *class; + const char *state; + const char *scope; + const char *desktop; +} SessionStatusInfo; + static OutputFlags get_output_flags(void) { return @@ -112,6 +133,12 @@ static int show_table(Table *table, const char *word) { } static int list_sessions(int argc, char *argv[], void *userdata) { + + static const struct bus_properties_map map[] = { + { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) }, + {}, + }; + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; _cleanup_(table_unrefp) Table *table = NULL; @@ -148,9 +175,10 @@ static int list_sessions(int argc, char *argv[], void *userdata) { for (;;) { _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; - _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_tty = NULL; - const char *id, *user, *seat, *object, *tty = NULL; + const char *id, *user, *seat, *object; uint32_t uid; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; + SessionStatusInfo i = {}; r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object); if (r < 0) @@ -158,21 +186,14 @@ static int list_sessions(int argc, char *argv[], void *userdata) { if (r == 0) break; - r = sd_bus_get_property( - bus, - "org.freedesktop.login1", - object, - "org.freedesktop.login1.Session", - "TTY", - &e, - &reply_tty, - "s"); - if (r < 0) - log_warning_errno(r, "Failed to get TTY for session %s: %s", id, bus_error_message(&e, r)); - else { - r = sd_bus_message_read(reply_tty, "s", &tty); - if (r < 0) - return bus_log_parse_error(r); + r = bus_map_all_properties(bus, "org.freedesktop.login1", object, map, BUS_MAP_BOOLEAN_AS_BOOL, &e, &m, &i); + if (r < 0) { + if (sd_bus_error_has_name(&e, SD_BUS_ERROR_UNKNOWN_OBJECT)) + /* The session is already closed when we're querying the property */ + continue; + + log_warning_errno(r, "Failed to get properties of session %s, ignoring: %s", + id, bus_error_message(&e, r)); } r = table_add_many(table, @@ -180,7 +201,7 @@ static int list_sessions(int argc, char *argv[], void *userdata) { TABLE_UINT32, uid, TABLE_STRING, user, TABLE_STRING, seat, - TABLE_STRING, strna(tty)); + TABLE_STRING, strna(i.tty)); if (r < 0) return log_error_errno(r, "Failed to add row to table: %m"); } @@ -341,27 +362,6 @@ static int show_unit_cgroup(sd_bus *bus, const char *interface, const char *unit return 0; } -typedef struct SessionStatusInfo { - const char *id; - uid_t uid; - const char *name; - struct dual_timestamp timestamp; - unsigned int vtnr; - const char *seat; - const char *tty; - const char *display; - bool remote; - const char *remote_host; - const char *remote_user; - const char *service; - pid_t leader; - const char *type; - const char *class; - const char *state; - const char *scope; - const char *desktop; -} SessionStatusInfo; - typedef struct UserStatusInfo { uid_t uid; bool linger;