86b31d
From fbc394252588325b6e7ecd1ab65ad40b51763c58 Mon Sep 17 00:00:00 2001
86b31d
From: Lennart Poettering <lennart@poettering.net>
86b31d
Date: Tue, 7 Aug 2018 12:08:24 +0200
86b31d
Subject: [PATCH] logind: add hashtable for finding session by leader PID
86b31d
86b31d
This is useful later on, when we quickly want to find the session for a
86b31d
leader PID.
86b31d
86b31d
(cherry picked from commit 238794b15082e6f61d0ce2943d39205289fff7f0)
86b31d
86b31d
Related: #2122288
86b31d
---
86b31d
 src/login/logind-core.c    | 15 ++++++++------
86b31d
 src/login/logind-dbus.c    |  3 +--
86b31d
 src/login/logind-session.c | 41 +++++++++++++++++++++++++++++++++++---
86b31d
 src/login/logind-session.h |  1 +
86b31d
 src/login/logind.c         |  4 +++-
86b31d
 src/login/logind.h         |  1 +
86b31d
 6 files changed, 53 insertions(+), 12 deletions(-)
86b31d
86b31d
diff --git a/src/login/logind-core.c b/src/login/logind-core.c
86b31d
index 7e33f8e6aa..a1943b6f9d 100644
86b31d
--- a/src/login/logind-core.c
86b31d
+++ b/src/login/logind-core.c
86b31d
@@ -339,13 +339,16 @@ int manager_get_session_by_pid(Manager *m, pid_t pid, Session **ret) {
86b31d
         if (!pid_is_valid(pid))
86b31d
                 return -EINVAL;
86b31d
 
86b31d
-        r = cg_pid_get_unit(pid, &unit);
86b31d
-        if (r < 0)
86b31d
-                goto not_found;
86b31d
+        s = hashmap_get(m->sessions_by_leader, PID_TO_PTR(pid));
86b31d
+        if (!s) {
86b31d
+                r = cg_pid_get_unit(pid, &unit);
86b31d
+                if (r < 0)
86b31d
+                        goto not_found;
86b31d
 
86b31d
-        s = hashmap_get(m->session_units, unit);
86b31d
-        if (!s)
86b31d
-                goto not_found;
86b31d
+                s = hashmap_get(m->session_units, unit);
86b31d
+                if (!s)
86b31d
+                        goto not_found;
86b31d
+        }
86b31d
 
86b31d
         if (ret)
86b31d
                 *ret = s;
86b31d
diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c
86b31d
index 0248042308..01bfef4ff7 100644
86b31d
--- a/src/login/logind-dbus.c
86b31d
+++ b/src/login/logind-dbus.c
86b31d
@@ -784,9 +784,8 @@ static int method_create_session(sd_bus_message *message, void *userdata, sd_bus
86b31d
                 goto fail;
86b31d
 
86b31d
         session_set_user(session, user);
86b31d
+        session_set_leader(session, leader);
86b31d
 
86b31d
-        session->leader = leader;
86b31d
-        session->audit_id = audit_id;
86b31d
         session->type = t;
86b31d
         session->class = c;
86b31d
         session->remote = remote;
86b31d
diff --git a/src/login/logind-session.c b/src/login/logind-session.c
86b31d
index d666f86d3f..cc838ca383 100644
86b31d
--- a/src/login/logind-session.c
86b31d
+++ b/src/login/logind-session.c
86b31d
@@ -121,6 +121,9 @@ Session* session_free(Session *s) {
86b31d
                 free(s->scope);
86b31d
         }
86b31d
 
86b31d
+        if (pid_is_valid(s->leader))
86b31d
+                (void) hashmap_remove_value(s->manager->sessions_by_leader, PID_TO_PTR(s->leader), s);
86b31d
+
86b31d
         free(s->scope_job);
86b31d
 
86b31d
         sd_bus_message_unref(s->create_message);
86b31d
@@ -149,6 +152,30 @@ void session_set_user(Session *s, User *u) {
86b31d
         user_update_last_session_timer(u);
86b31d
 }
86b31d
 
86b31d
+int session_set_leader(Session *s, pid_t pid) {
86b31d
+        int r;
86b31d
+
86b31d
+        assert(s);
86b31d
+
86b31d
+        if (!pid_is_valid(pid))
86b31d
+                return -EINVAL;
86b31d
+
86b31d
+        if (s->leader == pid)
86b31d
+                return 0;
86b31d
+
86b31d
+        r = hashmap_put(s->manager->sessions_by_leader, PID_TO_PTR(pid), s);
86b31d
+        if (r < 0)
86b31d
+                return r;
86b31d
+
86b31d
+        if (pid_is_valid(s->leader))
86b31d
+                (void) hashmap_remove_value(s->manager->sessions_by_leader, PID_TO_PTR(s->leader), s);
86b31d
+
86b31d
+        s->leader = pid;
86b31d
+        (void) audit_session_from_pid(pid, &s->audit_id);
86b31d
+
86b31d
+        return 1;
86b31d
+}
86b31d
+
86b31d
 static void session_save_devices(Session *s, FILE *f) {
86b31d
         SessionDevice *sd;
86b31d
         Iterator i;
86b31d
@@ -473,8 +500,16 @@ int session_load(Session *s) {
86b31d
         }
86b31d
 
86b31d
         if (leader) {
86b31d
-                if (parse_pid(leader, &s->leader) >= 0)
86b31d
-                        (void) audit_session_from_pid(s->leader, &s->audit_id);
86b31d
+                pid_t pid;
86b31d
+
86b31d
+                r = parse_pid(leader, &pid;;
86b31d
+                if (r < 0)
86b31d
+                        log_debug_errno(r, "Failed to parse leader PID of session: %s", leader);
86b31d
+                else {
86b31d
+                        r = session_set_leader(s, pid);
86b31d
+                        if (r < 0)
86b31d
+                                log_warning_errno(r, "Failed to set session leader PID, ignoring: %m");
86b31d
+                }
86b31d
         }
86b31d
 
86b31d
         if (type) {
86b31d
@@ -910,7 +945,7 @@ int session_get_idle_hint(Session *s, dual_timestamp *t) {
86b31d
 
86b31d
         /* For sessions with a leader but no explicitly configured
86b31d
          * tty, let's check the controlling tty of the leader */
86b31d
-        if (s->leader > 0) {
86b31d
+        if (pid_is_valid(s->leader)) {
86b31d
                 r = get_process_ctty_atime(s->leader, &atime);
86b31d
                 if (r >= 0)
86b31d
                         goto found_atime;
86b31d
diff --git a/src/login/logind-session.h b/src/login/logind-session.h
86b31d
index 7da845cea3..8c7d0301f2 100644
86b31d
--- a/src/login/logind-session.h
86b31d
+++ b/src/login/logind-session.h
86b31d
@@ -124,6 +124,7 @@ Session* session_free(Session *s);
86b31d
 DEFINE_TRIVIAL_CLEANUP_FUNC(Session *, session_free);
86b31d
 
86b31d
 void session_set_user(Session *s, User *u);
86b31d
+int session_set_leader(Session *s, pid_t pid);
86b31d
 bool session_may_gc(Session *s, bool drop_not_started);
86b31d
 void session_add_to_gc_queue(Session *s);
86b31d
 int session_activate(Session *s);
86b31d
diff --git a/src/login/logind.c b/src/login/logind.c
86b31d
index 25de9a6ab2..6b576dad0d 100644
86b31d
--- a/src/login/logind.c
86b31d
+++ b/src/login/logind.c
86b31d
@@ -46,6 +46,7 @@ static int manager_new(Manager **ret) {
86b31d
         m->devices = hashmap_new(&string_hash_ops);
86b31d
         m->seats = hashmap_new(&string_hash_ops);
86b31d
         m->sessions = hashmap_new(&string_hash_ops);
86b31d
+        m->sessions_by_leader = hashmap_new(NULL);
86b31d
         m->users = hashmap_new(NULL);
86b31d
         m->inhibitors = hashmap_new(&string_hash_ops);
86b31d
         m->buttons = hashmap_new(&string_hash_ops);
86b31d
@@ -53,7 +54,7 @@ static int manager_new(Manager **ret) {
86b31d
         m->user_units = hashmap_new(&string_hash_ops);
86b31d
         m->session_units = hashmap_new(&string_hash_ops);
86b31d
 
86b31d
-        if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units)
86b31d
+        if (!m->devices || !m->seats || !m->sessions || !m->sessions_by_leader || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units)
86b31d
                 return -ENOMEM;
86b31d
 
86b31d
         m->udev = udev_new();
86b31d
@@ -112,6 +113,7 @@ static Manager* manager_unref(Manager *m) {
86b31d
         hashmap_free(m->devices);
86b31d
         hashmap_free(m->seats);
86b31d
         hashmap_free(m->sessions);
86b31d
+        hashmap_free(m->sessions_by_leader);
86b31d
         hashmap_free(m->users);
86b31d
         hashmap_free(m->inhibitors);
86b31d
         hashmap_free(m->buttons);
86b31d
diff --git a/src/login/logind.h b/src/login/logind.h
86b31d
index bb127bf4a5..7f94dea2f6 100644
86b31d
--- a/src/login/logind.h
86b31d
+++ b/src/login/logind.h
86b31d
@@ -26,6 +26,7 @@ struct Manager {
86b31d
         Hashmap *devices;
86b31d
         Hashmap *seats;
86b31d
         Hashmap *sessions;
86b31d
+        Hashmap *sessions_by_leader;
86b31d
         Hashmap *users;
86b31d
         Hashmap *inhibitors;
86b31d
         Hashmap *buttons;