]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
loginctl: show session idle status in list-sessions 27740/head
authorDavid Tardon <dtardon@redhat.com>
Fri, 19 May 2023 12:03:09 +0000 (14:03 +0200)
committerDavid Tardon <dtardon@redhat.com>
Wed, 24 May 2023 08:04:45 +0000 (10:04 +0200)
src/login/loginctl.c
test/units/testsuite-35.sh

index cf560aafdde558d1bc7f4e8457eaf76427892e58..28f4231ac1fa5a1d2fcb36c32a10ca6f9dabd1c8 100644 (file)
@@ -73,6 +73,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) {
@@ -138,8 +140,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)                            },
                 {},
         };
 
@@ -161,7 +165,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();
 
@@ -184,12 +188,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,
@@ -198,7 +201,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);
         }
index cae25499ad26cafc2c1178f8eb1d4df06cb037dc..659882ed7ae36f2699a71aaa7029ebd3c760acea 100755 (executable)
@@ -543,6 +543,8 @@ testcase_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?}"