]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
logind-dbus: introduce ListSessionsEx() call
authorMike Yuan <me@yhndnzj.com>
Sun, 14 Jan 2024 13:52:27 +0000 (21:52 +0800)
committerMike Yuan <me@yhndnzj.com>
Wed, 17 Jan 2024 03:25:48 +0000 (11:25 +0800)
As per https://github.com/systemd/systemd/pull/30884#discussion_r1448938737

man/org.freedesktop.login1.xml
src/login/logind-dbus.c
src/login/org.freedesktop.login1.conf

index f647b1cb5b08babbc8b88dc9b6010c8e7a52ffdd..9151926e64d5c1efb8300ec1912d42222f008ea8 100644 (file)
@@ -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 {
 
     <variablelist class="dbus-method" generated="True" extra-ref="ListSessions()"/>
 
+    <variablelist class="dbus-method" generated="True" extra-ref="ListSessionsEx()"/>
+
     <variablelist class="dbus-method" generated="True" extra-ref="ListUsers()"/>
 
     <variablelist class="dbus-method" generated="True" extra-ref="ListSeats()"/>
@@ -549,8 +552,17 @@ node /org/freedesktop/login1 {
       is any.</para>
 
       <para><function>ListSessions()</function> 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.</para>
+      the array consist of the following fields: <varname>session id</varname>, <varname>user id</varname>,
+      <varname>user name</varname>, <varname>seat id</varname>, and <varname>session object path</varname>.
+      If a session does not have a seat attached, the seat id field will be an empty string.</para>
+
+      <para><function>ListSessionsEx()</function> returns an array of all current sessions with more metadata
+      than <function>ListSessions()</function>. The structures in the array consist of the following fields:
+      <varname>session id</varname>, <varname>user id</varname>, <varname>user name</varname>,
+      <varname>seat id</varname>, <varname>leader pid</varname>, <varname>session class</varname>,
+      <varname>tty name</varname>, <varname>idle hint</varname>, <varname>idle hint monotonic timestamp</varname>,
+      and <varname>session object path</varname>. <varname>tty</varname> and <varname>seat id</varname> fields
+      could be empty, if the session has no associated tty or session has no seat attached, respectively.</para>
 
       <para><function>ListUsers()</function> 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.</para>
@@ -1559,8 +1571,9 @@ node /org/freedesktop/login1/session/1 {
       <para><function>PrepareForShutdownWithMetadata</function> and
       <function>CreateSessionWithPIDFD()</function> were added in version 255.</para>
       <para><function>Sleep()</function>,
-      <function>CanSleep()</function>, and
-      <varname>SleepOperation</varname> were added in version 256.</para>
+      <function>CanSleep()</function>,
+      <varname>SleepOperation</varname>, and
+      <function>ListSessionsEx()</function> were added in version 256.</para>
     </refsect2>
     <refsect2>
       <title>Session Objects</title>
index 932eb42b4e8d5fe89277166d0b34e84183028d43..8221f76fbf267f9d3f671d130080a555abae8099 100644 (file)
@@ -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),
index d33e89d5be8a51514f507baa1013c99c1f256ed9..e49496fce840be9deb22f9dbf3942b510b6d7ca8 100644 (file)
                        send_interface="org.freedesktop.login1.Manager"
                        send_member="ListSessions"/>
 
+                <allow send_destination="org.freedesktop.login1"
+                       send_interface="org.freedesktop.login1.Manager"
+                       send_member="ListSessionsEx"/>
+
                 <allow send_destination="org.freedesktop.login1"
                        send_interface="org.freedesktop.login1.Manager"
                        send_member="ListUsers"/>