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

index a2d03a9e64f06e06bce73256a164890dcc823665..4559b83e506622c0dba15258dcca952e00ea3ecc 100644 (file)
@@ -206,13 +206,15 @@ static int list_users(int argc, char *argv[], void *userdata) {
         if (r < 0)
                 return bus_log_parse_error(r);
 
-        table = table_new("uid", "user", "linger");
+        table = table_new("uid", "user", "linger", "state");
         if (!table)
                 return log_oom();
 
         (void) table_set_align_percent(table, TABLE_HEADER_CELL(0), 100);
 
         for (;;) {
+                _cleanup_(sd_bus_error_free) sd_bus_error error_property = SD_BUS_ERROR_NULL;
+                _cleanup_free_ char *state = NULL;
                 const char *user, *object;
                 uint32_t uid;
                 int linger;
@@ -228,16 +230,39 @@ static int list_users(int argc, char *argv[], void *userdata) {
                                                 object,
                                                 "org.freedesktop.login1.User",
                                                 "Linger",
-                                                &error,
+                                                &error_property,
                                                 'b',
                                                 &linger);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to get linger status: %s", bus_error_message(&error, r));
+                if (r < 0) {
+                        if (sd_bus_error_has_name(&error_property, SD_BUS_ERROR_UNKNOWN_OBJECT))
+                                /* The user logged out when we're querying the property */
+                                continue;
+
+                        return log_error_errno(r, "Failed to get linger status for user %s: %s",
+                                               user, bus_error_message(&error_property, r));
+                }
+
+                r = sd_bus_get_property_string(bus,
+                                               "org.freedesktop.login1",
+                                               object,
+                                               "org.freedesktop.login1.User",
+                                               "State",
+                                               &error_property,
+                                               &state);
+                if (r < 0) {
+                        if (sd_bus_error_has_name(&error_property, SD_BUS_ERROR_UNKNOWN_OBJECT))
+                                /* The user logged out when we're querying the property */
+                                continue;
+
+                        return log_error_errno(r, "Failed to get state for user %s: %s",
+                                               user, bus_error_message(&error_property, r));
+                }
 
                 r = table_add_many(table,
                                    TABLE_UID, (uid_t) uid,
                                    TABLE_STRING, user,
-                                   TABLE_BOOLEAN, linger);
+                                   TABLE_BOOLEAN, linger,
+                                   TABLE_STRING, state);
                 if (r < 0)
                         return table_log_add_error(r);
         }