]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
loginctl: list-sessions: also show state
authorMike Yuan <me@yhndnzj.com>
Thu, 11 May 2023 05:21:37 +0000 (13:21 +0800)
committerMike Yuan <me@yhndnzj.com>
Tue, 16 May 2023 10:09:15 +0000 (18:09 +0800)
src/login/loginctl.c

index 8f1a5a06decbcf4812ba9ea5433ac78a0ad6e03d..dfc738c6d02ee207e4dbcd31eba5bf72e661342b 100644 (file)
@@ -133,7 +133,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");
+        table = table_new("session", "uid", "user", "seat", "tty", "state");
         if (!table)
                 return log_oom();
 
@@ -143,7 +143,7 @@ static int list_sessions(int argc, char *argv[], void *userdata) {
 
         for (;;) {
                 _cleanup_(sd_bus_error_free) sd_bus_error error_property = SD_BUS_ERROR_NULL;
-                _cleanup_free_ char *tty = NULL;
+                _cleanup_free_ char *tty = NULL, *state = NULL;
                 const char *id, *user, *seat, *object;
                 uint32_t uid;
 
@@ -171,12 +171,29 @@ static int list_sessions(int argc, char *argv[], void *userdata) {
                         sd_bus_error_free(&error_property);
                 }
 
+                r = sd_bus_get_property_string(bus,
+                                               "org.freedesktop.login1",
+                                               object,
+                                               "org.freedesktop.login1.Session",
+                                               "State",
+                                               &error_property,
+                                               &state);
+                if (r < 0) {
+                        if (sd_bus_error_has_name(&error_property, SD_BUS_ERROR_UNKNOWN_OBJECT))
+                                /* The session is already closed when we're querying the property */
+                                continue;
+
+                        return log_error_errno(r, "Failed to get state for session %s: %s",
+                                               id, bus_error_message(&error_property, r));
+                }
+
                 r = table_add_many(table,
                                    TABLE_STRING, id,
                                    TABLE_UID, (uid_t) uid,
                                    TABLE_STRING, user,
                                    TABLE_STRING, seat,
-                                   TABLE_STRING, strna(tty));
+                                   TABLE_STRING, strna(tty),
+                                   TABLE_STRING, state);
                 if (r < 0)
                         return table_log_add_error(r);
         }