From b3cb952c033ce53f25cf853796afc34102344f08 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 14 Jan 2024 21:52:27 +0800 Subject: [PATCH] logind-dbus: introduce ListSessionsEx() call As per https://github.com/systemd/systemd/pull/30884#discussion_r1448938737 --- man/org.freedesktop.login1.xml | 21 ++++++++-- src/login/logind-dbus.c | 59 +++++++++++++++++++++++++++ src/login/org.freedesktop.login1.conf | 4 ++ 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/man/org.freedesktop.login1.xml b/man/org.freedesktop.login1.xml index f647b1cb5b..9151926e64 100644 --- a/man/org.freedesktop.login1.xml +++ b/man/org.freedesktop.login1.xml @@ -53,6 +53,7 @@ node /org/freedesktop/login1 { GetSeat(in s seat_id, out o object_path); ListSessions(out a(susso) sessions); + ListSessionsEx(out a(sussussbto) sessions); ListUsers(out a(uso) users); ListSeats(out a(so) seats); ListInhibitors(out a(ssssuu) inhibitors); @@ -315,6 +316,8 @@ node /org/freedesktop/login1 { + + @@ -549,8 +552,17 @@ node /org/freedesktop/login1 { is any. ListSessions() returns an array of all current sessions. The structures in - the array consist of the following fields: session id, user id, user name, seat id, session object - path. If a session does not have a seat attached, the seat id field will be an empty string. + the array consist of the following fields: session id, user id, + user name, seat id, and session object path. + If a session does not have a seat attached, the seat id field will be an empty string. + + ListSessionsEx() returns an array of all current sessions with more metadata + than ListSessions(). The structures in the array consist of the following fields: + session id, user id, user name, + seat id, leader pid, session class, + tty name, idle hint, idle hint monotonic timestamp, + and session object path. tty and seat id fields + could be empty, if the session has no associated tty or session has no seat attached, respectively. ListUsers() returns an array of all currently logged in users. The structures in the array consist of the following fields: user id, user name, user object path. @@ -1559,8 +1571,9 @@ node /org/freedesktop/login1/session/1 { PrepareForShutdownWithMetadata and CreateSessionWithPIDFD() were added in version 255. Sleep(), - CanSleep(), and - SleepOperation were added in version 256. + CanSleep(), + SleepOperation, and + ListSessionsEx() were added in version 256. Session Objects diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 932eb42b4e..8221f76fbf 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -591,6 +591,60 @@ static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_ return sd_bus_send(NULL, reply, NULL); } +static int method_list_sessions_ex(sd_bus_message *message, void *userdata, sd_bus_error *error) { + Manager *m = ASSERT_PTR(userdata); + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + int r; + + assert(message); + + r = sd_bus_message_new_method_return(message, &reply); + if (r < 0) + return r; + + r = sd_bus_message_open_container(reply, 'a', "(sussussbto)"); + if (r < 0) + return r; + + Session *s; + HASHMAP_FOREACH(s, m->sessions) { + _cleanup_free_ char *path = NULL; + dual_timestamp idle_ts; + bool idle; + + assert(s->user); + + path = session_bus_path(s); + if (!path) + return -ENOMEM; + + r = session_get_idle_hint(s, &idle_ts); + if (r < 0) + return r; + idle = r > 0; + + r = sd_bus_message_append(reply, "(sussussbto)", + s->id, + (uint32_t) s->user->user_record->uid, + s->user->user_record->user_name, + s->seat ? s->seat->id : "", + (uint32_t) s->leader.pid, + session_class_to_string(s->class), + s->tty, + idle, + idle_ts.monotonic, + path); + if (r < 0) + return r; + } + + r = sd_bus_message_close_container(reply); + if (r < 0) + return r; + + return sd_bus_send(NULL, reply, NULL); +} + static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; Manager *m = ASSERT_PTR(userdata); @@ -3636,6 +3690,11 @@ static const sd_bus_vtable manager_vtable[] = { SD_BUS_RESULT("a(susso)", sessions), method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD_WITH_ARGS("ListSessionsEx", + SD_BUS_NO_ARGS, + SD_BUS_RESULT("a(sussussbto)", sessions), + method_list_sessions_ex, + SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD_WITH_ARGS("ListUsers", SD_BUS_NO_ARGS, SD_BUS_RESULT("a(uso)", users), diff --git a/src/login/org.freedesktop.login1.conf b/src/login/org.freedesktop.login1.conf index d33e89d5be..e49496fce8 100644 --- a/src/login/org.freedesktop.login1.conf +++ b/src/login/org.freedesktop.login1.conf @@ -62,6 +62,10 @@ send_interface="org.freedesktop.login1.Manager" send_member="ListSessions"/> + + -- 2.25.1