From b4e164914c66eb8aeb71170c1d7615589783f467 Mon Sep 17 00:00:00 2001 From: David Tardon Date: Fri, 19 May 2023 14:03:09 +0200 Subject: [PATCH] loginctl: show session idle status in list-sessions (cherry picked from commit 556723e738b96a5c2b2d45a96b87b7b80e0c5664) Resolves: #2209912 --- src/login/loginctl.c | 31 +++++++++++++++++++++---------- test/units/testsuite-35.sh | 2 ++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 9a09bfc82c..967ae230ab 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -72,6 +72,8 @@ typedef struct SessionStatusInfo { const char *state; const char *scope; const char *desktop; + bool idle_hint; + dual_timestamp idle_hint_timestamp; } SessionStatusInfo; static OutputFlags get_output_flags(void) { @@ -137,8 +139,10 @@ 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[] = { - { "State", "s", NULL, offsetof(SessionStatusInfo, state) }, - { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) }, + { "IdleHint", "b", NULL, offsetof(SessionStatusInfo, idle_hint) }, + { "IdleSinceHintMonotonic", "t", NULL, offsetof(SessionStatusInfo, idle_hint_timestamp.monotonic) }, + { "State", "s", NULL, offsetof(SessionStatusInfo, state) }, + { "TTY", "s", NULL, offsetof(SessionStatusInfo, tty) }, {}, }; @@ -160,7 +164,7 @@ static int list_sessions(int argc, char *argv[], void *userdata) { if (r < 0) return bus_log_parse_error(r); - table = table_new("session", "uid", "user", "seat", "tty", "state"); + table = table_new("session", "uid", "user", "seat", "tty", "state", "idle", "since"); if (!table) return log_oom(); @@ -183,12 +187,11 @@ static int list_sessions(int argc, char *argv[], void *userdata) { 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)); + log_full_errno(sd_bus_error_has_name(&e, SD_BUS_ERROR_UNKNOWN_OBJECT) ? LOG_DEBUG : LOG_WARNING, + r, + "Failed to get properties of session %s, ignoring: %s", + id, bus_error_message(&e, r)); + continue; } r = table_add_many(table, @@ -197,7 +200,15 @@ static int list_sessions(int argc, char *argv[], void *userdata) { TABLE_STRING, user, TABLE_STRING, seat, TABLE_STRING, strna(i.tty), - TABLE_STRING, i.state); + TABLE_STRING, i.state, + TABLE_BOOLEAN, i.idle_hint); + if (r < 0) + return table_log_add_error(r); + + if (i.idle_hint) + r = table_add_cell(table, NULL, TABLE_TIMESTAMP_RELATIVE, &i.idle_hint_timestamp.monotonic); + else + r = table_add_cell(table, NULL, TABLE_EMPTY, NULL); if (r < 0) return table_log_add_error(r); } diff --git a/test/units/testsuite-35.sh b/test/units/testsuite-35.sh index c817bc82bb..1863c535a4 100755 --- a/test/units/testsuite-35.sh +++ b/test/units/testsuite-35.sh @@ -541,6 +541,8 @@ test_list_users_sessions_seats() { seat=$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $4 }') assert_eq "$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $5 }')" tty2 assert_eq "$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $6 }')" active + assert_eq "$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $7 }')" no + assert_eq "$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $8 }')" '' loginctl list-seats --no-legend | grep -Fwq "${seat?}"