You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
systemd/SOURCES/0297-loginctl-list-sessions...

66 lines
2.9 KiB

From a6b868dd39b60199862e306a272b54d2ef617317 Mon Sep 17 00:00:00 2001
From: Mike Yuan <me@yhndnzj.com>
Date: Thu, 11 May 2023 13:21:37 +0800
Subject: [PATCH] loginctl: list-sessions: also show state
(cherry picked from commit 8b6c039a1ac73da006bfe9d5735515bba12ef3c4)
Related: #2209912
---
src/login/loginctl.c | 23 ++++++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/src/login/loginctl.c b/src/login/loginctl.c
index 9c21b97bb5..0bea31796d 100644
--- a/src/login/loginctl.c
+++ b/src/login/loginctl.c
@@ -132,7 +132,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,7 +142,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;
@@ -170,12 +170,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);
}