1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-12 13:18:14 +03:00

loginctl: list-users: also show state

This commit is contained in:
Mike Yuan 2023-05-11 13:05:39 +08:00
parent f44744ff51
commit bae05711b5
No known key found for this signature in database
GPG Key ID: 417471C0A40F58B3

View 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);
}