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

Merge pull request #27606 from YHNdnzj/loginctl-list-show-state

loginctl: list-{users,sessions}: add a column for showing state
This commit is contained in:
Yu Watanabe 2023-05-17 01:41:13 +09:00 committed by GitHub
commit 871a41f0ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 29 deletions

View 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();
@ -142,9 +142,9 @@ static int list_sessions(int argc, char *argv[], void *userdata) {
(void) table_set_align_percent(table, TABLE_HEADER_CELL(1), 100);
for (;;) {
_cleanup_(sd_bus_error_free) sd_bus_error error_tty = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_tty = NULL;
const char *id, *user, *seat, *object, *tty = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error_property = SD_BUS_ERROR_NULL;
_cleanup_free_ char *tty = NULL, *state = NULL;
const char *id, *user, *seat, *object;
uint32_t uid;
r = sd_bus_message_read(reply, "(susso)", &id, &uid, &user, &seat, &object);
@ -153,21 +153,38 @@ static int list_sessions(int argc, char *argv[], void *userdata) {
if (r == 0)
break;
r = sd_bus_get_property(
bus,
"org.freedesktop.login1",
object,
"org.freedesktop.login1.Session",
"TTY",
&error_tty,
&reply_tty,
"s");
if (r < 0)
log_warning_errno(r, "Failed to get TTY for session %s: %s", id, bus_error_message(&error_tty, r));
else {
r = sd_bus_message_read(reply_tty, "s", &tty);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_get_property_string(bus,
"org.freedesktop.login1",
object,
"org.freedesktop.login1.Session",
"TTY",
&error_property,
&tty);
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;
log_warning_errno(r, "Failed to get TTY for session %s, ignoring: %s",
id, bus_error_message(&error_property, r));
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,
@ -175,7 +192,8 @@ static int list_sessions(int argc, char *argv[], void *userdata) {
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);
}
@ -206,13 +224,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 +248,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);
}

View File

@ -520,7 +520,7 @@ test_session_properties() {
/usr/lib/systemd/tests/unit-tests/manual/test-session-properties "/org/freedesktop/login1/session/_3${s?}"
}
test_list_users() {
test_list_users_sessions() {
if [[ ! -c /dev/tty2 ]]; then
echo "/dev/tty2 does not exist, skipping test ${FUNCNAME[0]}."
return
@ -531,12 +531,22 @@ test_list_users() {
assert_eq "$(loginctl list-users --no-legend | awk '$2 == "logind-test-user" { print $1 }')" "$(id -ru logind-test-user)"
assert_eq "$(loginctl list-users --no-legend | awk '$2 == "logind-test-user" { print $3 }')" no
assert_eq "$(loginctl list-users --no-legend | awk '$2 == "logind-test-user" { print $4 }')" active
assert_eq "$(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $6 }')" active
loginctl enable-linger logind-test-user
assert_eq "$(loginctl list-users --no-legend | awk '$2 == "logind-test-user" { print $3 }')" yes
}
for s in $(loginctl list-sessions --no-legend | awk '$3 == "logind-test-user" { print $1 }'); do
loginctl terminate-session "$s"
done
if ! timeout 30 bash -c "while loginctl --no-legend | grep -q logind-test-user; do sleep 1; done"; then
echo "WARNING: session for logind-test-user still active, ignoring."
return
fi
assert_eq "$(loginctl list-users --no-legend | awk '$2 == "logind-test-user" { print $4 }')" lingering
}
teardown_stop_idle_session() (
set +eux
@ -637,7 +647,7 @@ test_sanity_check
test_session
test_lock_idle_action
test_session_properties
test_list_users
test_list_users_sessions
test_stop_idle_session
test_ambient_caps