]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
logind: Don't match non-leader processes for utmp TTY determination
authorMyrrh Periwinkle <myrrhperiwinkle@qtmlabs.xyz>
Wed, 2 Jul 2025 05:53:15 +0000 (12:53 +0700)
committerMyrrh Periwinkle <myrrhperiwinkle@qtmlabs.xyz>
Wed, 2 Jul 2025 05:53:15 +0000 (12:53 +0700)
This ensures we don't erroneously assign pseudoterminals created by
terminal emulators that use utempter to register themselves in utmp when
run under a GUI session that doesn't have a TTY assigned.

src/login/logind-core.c
src/login/logind-utmp.c
src/login/logind.h

index 3c51bc4a40ae26dc815176c87e300c62eb1e4212..2f8c7824d2ada6fa1b7d17cde8e6c09232ec273a 100644 (file)
@@ -393,6 +393,28 @@ int manager_get_session_by_pidref(Manager *m, const PidRef *pid, Session **ret)
         return !!s;
 }
 
+int manager_get_session_by_leader(Manager *m, const PidRef *pid, Session **ret) {
+        Session *s;
+        int r;
+
+        assert(m);
+
+        if (!pidref_is_set(pid))
+                return -EINVAL;
+
+        s = hashmap_get(m->sessions_by_leader, pid);
+        if (s) {
+                r = pidref_verify(pid);
+                if (r < 0)
+                        return r;
+        }
+
+        if (ret)
+                *ret = s;
+
+        return !!s;
+}
+
 int manager_get_user_by_pid(Manager *m, pid_t pid, User **ret) {
         _cleanup_free_ char *unit = NULL;
         User *u = NULL;
index 8305ae743fa83f0ab578c1a02e55ce887d6b91cc..2adfc082573ef0ff49e65500af7bc2100bff3b82 100644 (file)
@@ -56,7 +56,7 @@ int manager_read_utmp(Manager *m) {
                 if (isempty(t))
                         continue;
 
-                if (manager_get_session_by_pidref(m, &PIDREF_MAKE_FROM_PID(u->ut_pid), &s) <= 0)
+                if (manager_get_session_by_leader(m, &PIDREF_MAKE_FROM_PID(u->ut_pid), &s) <= 0)
                         continue;
 
                 if (s->tty_validity == TTY_FROM_UTMP && !streq_ptr(s->tty, t)) {
index 6a46374e8ace3000c9c114e524671fc10eb03b41..bc9ea97d80169b2a3d7ccb1db2ec68092e44eb13 100644 (file)
@@ -163,6 +163,7 @@ int manager_get_idle_hint(Manager *m, dual_timestamp *t);
 
 int manager_get_user_by_pid(Manager *m, pid_t pid, User **user);
 int manager_get_session_by_pidref(Manager *m, const PidRef *pid, Session **ret);
+int manager_get_session_by_leader(Manager *m, const PidRef *pid, Session **ret);
 
 bool manager_is_lid_closed(Manager *m);
 bool manager_is_docked_or_external_displays(Manager *m);