9fc0f6
From 49bfaaa65107a9d79fbb1276e44fd4e3c98b0e9c Mon Sep 17 00:00:00 2001
9fc0f6
From: Lennart Poettering <lennart@poettering.net>
9fc0f6
Date: Fri, 7 Feb 2014 11:58:25 +0100
9fc0f6
Subject: [PATCH] core: allow PIDs to be watched by two units at the same time
9fc0f6
9fc0f6
In some cases it is interesting to map a PID to two units at the same
9fc0f6
time. For example, when a user logs in via a getty, which is reexeced to
9fc0f6
/sbin/login that binary will be explicitly referenced as main pid of the
9fc0f6
getty service, as well as implicitly referenced as part of the session
9fc0f6
scope.
9fc0f6
9fc0f6
Conflicts:
9fc0f6
	src/core/manager.c
9fc0f6
	src/core/manager.h
9fc0f6
---
9fc0f6
 src/core/manager.c | 201 ++++++++++++++++++++++++++++++-----------------------
9fc0f6
 src/core/manager.h |   9 ++-
9fc0f6
 src/core/unit.c    |  28 ++++++--
9fc0f6
 3 files changed, 142 insertions(+), 96 deletions(-)
9fc0f6
9fc0f6
diff --git a/src/core/manager.c b/src/core/manager.c
9fc0f6
index db5094f..2829c95 100644
9fc0f6
--- a/src/core/manager.c
9fc0f6
+++ b/src/core/manager.c
9fc0f6
@@ -525,7 +525,10 @@ int manager_new(SystemdRunningAs running_as, bool reexecuting, Manager **_m) {
9fc0f6
         if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
9fc0f6
                 goto fail;
9fc0f6
 
9fc0f6
-        if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
9fc0f6
+        if (!(m->watch_pids1 = hashmap_new(trivial_hash_func, trivial_compare_func)))
9fc0f6
+                goto fail;
9fc0f6
+
9fc0f6
+        if (!(m->watch_pids2 = hashmap_new(trivial_hash_func, trivial_compare_func)))
9fc0f6
                 goto fail;
9fc0f6
 
9fc0f6
         m->cgroup_unit = hashmap_new(string_hash_func, string_compare_func);
9fc0f6
@@ -740,7 +743,8 @@ void manager_free(Manager *m) {
9fc0f6
 
9fc0f6
         hashmap_free(m->units);
9fc0f6
         hashmap_free(m->jobs);
9fc0f6
-        hashmap_free(m->watch_pids);
9fc0f6
+        hashmap_free(m->watch_pids1);
9fc0f6
+        hashmap_free(m->watch_pids2);
9fc0f6
         hashmap_free(m->watch_bus);
9fc0f6
 
9fc0f6
         if (m->epoll_fd >= 0)
9fc0f6
@@ -1247,6 +1251,26 @@ unsigned manager_dispatch_dbus_queue(Manager *m) {
9fc0f6
         return n;
9fc0f6
 }
9fc0f6
 
9fc0f6
+static void manager_invoke_notify_message(Manager *m, Unit *u, pid_t pid, char *buf, size_t n) {
9fc0f6
+        _cleanup_strv_free_ char **tags = NULL;
9fc0f6
+
9fc0f6
+        assert(m);
9fc0f6
+        assert(u);
9fc0f6
+        assert(buf);
9fc0f6
+        assert(n > 0);
9fc0f6
+
9fc0f6
+        tags = strv_split(buf, "\n\r");
9fc0f6
+        if (!tags) {
9fc0f6
+                log_oom();
9fc0f6
+                return;
9fc0f6
+        }
9fc0f6
+
9fc0f6
+        log_debug_unit(u->id, "Got notification message for unit %s", u->id);
9fc0f6
+
9fc0f6
+        if (UNIT_VTABLE(u)->notify_message)
9fc0f6
+                UNIT_VTABLE(u)->notify_message(u, pid, tags);
9fc0f6
+}
9fc0f6
+
9fc0f6
 static int manager_process_notify_fd(Manager *m) {
9fc0f6
         ssize_t n;
9fc0f6
 
9fc0f6
@@ -1259,6 +1283,8 @@ static int manager_process_notify_fd(Manager *m) {
9fc0f6
                         .iov_len = sizeof(buf)-1,
9fc0f6
                 };
9fc0f6
 
9fc0f6
+                bool found = false;
9fc0f6
+
9fc0f6
                 union {
9fc0f6
                         struct cmsghdr cmsghdr;
9fc0f6
                         uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
9fc0f6
@@ -1272,7 +1298,6 @@ static int manager_process_notify_fd(Manager *m) {
9fc0f6
                 };
9fc0f6
                 struct ucred *ucred;
9fc0f6
                 Unit *u;
9fc0f6
-                _cleanup_strv_free_ char **tags = NULL;
9fc0f6
 
9fc0f6
                 n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT);
9fc0f6
                 if (n <= 0) {
9fc0f6
@@ -1295,105 +1320,105 @@ static int manager_process_notify_fd(Manager *m) {
9fc0f6
 
9fc0f6
                 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
9fc0f6
 
9fc0f6
-                u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid));
9fc0f6
-                if (!u) {
9fc0f6
-                        u = manager_get_unit_by_pid(m, ucred->pid);
9fc0f6
-                        if (!u) {
9fc0f6
-                                log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
9fc0f6
-                                continue;
9fc0f6
-                        }
9fc0f6
-                }
9fc0f6
-
9fc0f6
                 assert((size_t) n < sizeof(buf));
9fc0f6
                 buf[n] = 0;
9fc0f6
-                tags = strv_split(buf, "\n\r");
9fc0f6
-                if (!tags)
9fc0f6
-                        return log_oom();
9fc0f6
-
9fc0f6
-                log_debug_unit(u->id, "Got notification message for unit %s", u->id);
9fc0f6
-
9fc0f6
-                if (UNIT_VTABLE(u)->notify_message)
9fc0f6
-                        UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
9fc0f6
-        }
9fc0f6
-
9fc0f6
-        return 0;
9fc0f6
-}
9fc0f6
-
9fc0f6
-static int manager_dispatch_sigchld(Manager *m) {
9fc0f6
-        assert(m);
9fc0f6
-
9fc0f6
-        for (;;) {
9fc0f6
-                siginfo_t si = {};
9fc0f6
-                Unit *u;
9fc0f6
-                int r;
9fc0f6
-
9fc0f6
-                /* First we call waitd() for a PID and do not reap the
9fc0f6
-                 * zombie. That way we can still access /proc/$PID for
9fc0f6
-                 * it while it is a zombie. */
9fc0f6
-                if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
9fc0f6
-
9fc0f6
-                        if (errno == ECHILD)
9fc0f6
-                                break;
9fc0f6
 
9fc0f6
-                        if (errno == EINTR)
9fc0f6
-                                continue;
9fc0f6
-
9fc0f6
-                        return -errno;
9fc0f6
+                u = manager_get_unit_by_pid(m, ucred->pid);
9fc0f6
+                if (u) {
9fc0f6
+                        manager_invoke_notify_message(m, u, ucred->pid, buf, n);
9fc0f6
+                        found = true;
9fc0f6
                 }
9fc0f6
 
9fc0f6
-                if (si.si_pid <= 0)
9fc0f6
-                        break;
9fc0f6
-
9fc0f6
-                if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
9fc0f6
-                        _cleanup_free_ char *name = NULL;
9fc0f6
-
9fc0f6
-                        get_process_comm(si.si_pid, &name);
9fc0f6
-                        log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
9fc0f6
+                u = hashmap_get(m->watch_pids1, LONG_TO_PTR(ucred->pid));
9fc0f6
+                if (u) {
9fc0f6
+                        manager_invoke_notify_message(m, u, ucred->pid, buf, n);
9fc0f6
+                        found = true;
9fc0f6
                 }
9fc0f6
 
9fc0f6
-                /* Let's flush any message the dying child might still
9fc0f6
-                 * have queued for us. This ensures that the process
9fc0f6
-                 * still exists in /proc so that we can figure out
9fc0f6
-                 * which cgroup and hence unit it belongs to. */
9fc0f6
-                r = manager_process_notify_fd(m);
9fc0f6
-                if (r < 0)
9fc0f6
-                        return r;
9fc0f6
-
9fc0f6
-                /* And now figure out the unit this belongs to */
9fc0f6
-                u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid));
9fc0f6
-                if (!u)
9fc0f6
-                        u = manager_get_unit_by_pid(m, si.si_pid);
9fc0f6
-
9fc0f6
-                /* And now, we actually reap the zombie. */
9fc0f6
-                if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
9fc0f6
-                        if (errno == EINTR)
9fc0f6
-                                continue;
9fc0f6
-
9fc0f6
-                        return -errno;
9fc0f6
+                u = hashmap_get(m->watch_pids2, LONG_TO_PTR(ucred->pid));
9fc0f6
+                if (u) {
9fc0f6
+                        manager_invoke_notify_message(m, u, ucred->pid, buf, n);
9fc0f6
+                        found = true;
9fc0f6
                 }
9fc0f6
 
9fc0f6
-                if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
9fc0f6
-                        continue;
9fc0f6
+                if (!found)
9fc0f6
+                        log_warning("Cannot find unit for notify message of PID %lu.",(long unsigned) ucred->pid);
9fc0f6
+        }
9fc0f6
 
9fc0f6
-                log_debug("Child %lu died (code=%s, status=%i/%s)",
9fc0f6
-                          (long unsigned) si.si_pid,
9fc0f6
-                          sigchld_code_to_string(si.si_code),
9fc0f6
-                          si.si_status,
9fc0f6
-                          strna(si.si_code == CLD_EXITED
9fc0f6
-                                ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
9fc0f6
-                                : signal_to_string(si.si_status)));
9fc0f6
+        return 0;
9fc0f6
+}
9fc0f6
 
9fc0f6
-                if (!u)
9fc0f6
-                        continue;
9fc0f6
+static void invoke_sigchld_event(Manager *m, Unit *u, siginfo_t *si) {
9fc0f6
+        assert(m);
9fc0f6
+        assert(u);
9fc0f6
+        assert(si);
9fc0f6
 
9fc0f6
-                log_debug_unit(u->id,
9fc0f6
-                               "Child %lu belongs to %s", (long unsigned) si.si_pid, u->id);
9fc0f6
+        log_debug_unit(u->id, "Child %lu belongs to %s",(long unsigned) si->si_pid, u->id);
9fc0f6
 
9fc0f6
-                unit_unwatch_pid(u, si.si_pid);
9fc0f6
-                UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
9fc0f6
-        }
9fc0f6
+        unit_unwatch_pid(u, si->si_pid);
9fc0f6
+        UNIT_VTABLE(u)->sigchld_event(u, si->si_pid, si->si_code, si->si_status);
9fc0f6
+}
9fc0f6
 
9fc0f6
-        return 0;
9fc0f6
+static int manager_dispatch_sigchld(Manager *m) {
9fc0f6
+    assert(m);
9fc0f6
+
9fc0f6
+    for (;;) {
9fc0f6
+            siginfo_t si = {};
9fc0f6
+
9fc0f6
+            /* First we call waitd() for a PID and do not reap the
9fc0f6
+             * zombie. That way we can still access /proc/$PID for
9fc0f6
+             * it while it is a zombie. */
9fc0f6
+            if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
9fc0f6
+
9fc0f6
+                    if (errno == ECHILD)
9fc0f6
+                            break;
9fc0f6
+
9fc0f6
+                    if (errno == EINTR)
9fc0f6
+                            continue;
9fc0f6
+
9fc0f6
+                    return -errno;
9fc0f6
+            }
9fc0f6
+
9fc0f6
+            if (si.si_pid <= 0)
9fc0f6
+                    break;
9fc0f6
+
9fc0f6
+            if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
9fc0f6
+                    _cleanup_free_ char *name = NULL;
9fc0f6
+                    Unit *u;
9fc0f6
+
9fc0f6
+                    get_process_comm(si.si_pid, &name);
9fc0f6
+
9fc0f6
+                    log_debug("Child %lu (%s) died (code=%s, status=%i/%s)",
9fc0f6
+                              (long unsigned) si.si_pid, strna(name),
9fc0f6
+                              sigchld_code_to_string(si.si_code),
9fc0f6
+                              si.si_status,
9fc0f6
+                              strna(si.si_code == CLD_EXITED
9fc0f6
+                                    ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
9fc0f6
+                                    : signal_to_string(si.si_status)));
9fc0f6
+
9fc0f6
+                    /* And now figure out the unit this belongs
9fc0f6
+                     * to, it might be multiple... */
9fc0f6
+                    u = manager_get_unit_by_pid(m, si.si_pid);
9fc0f6
+                    if (u)
9fc0f6
+                            invoke_sigchld_event(m, u, &si);
9fc0f6
+                    u = hashmap_get(m->watch_pids1, LONG_TO_PTR(si.si_pid));
9fc0f6
+                    if (u)
9fc0f6
+                            invoke_sigchld_event(m, u, &si);
9fc0f6
+                    u = hashmap_get(m->watch_pids2, LONG_TO_PTR(si.si_pid));
9fc0f6
+                    if (u)
9fc0f6
+                            invoke_sigchld_event(m, u, &si);
9fc0f6
+            }
9fc0f6
+
9fc0f6
+            /* And now, we actually reap the zombie. */
9fc0f6
+            if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
9fc0f6
+                    if (errno == EINTR)
9fc0f6
+                            continue;
9fc0f6
+
9fc0f6
+                    return -errno;
9fc0f6
+            }
9fc0f6
+    }
9fc0f6
+
9fc0f6
+    return 0;
9fc0f6
 }
9fc0f6
 
9fc0f6
 static int manager_start_target(Manager *m, const char *name, JobMode mode) {
9fc0f6
diff --git a/src/core/manager.h b/src/core/manager.h
9fc0f6
index ee42c5e..0133ea5 100644
9fc0f6
--- a/src/core/manager.h
9fc0f6
+++ b/src/core/manager.h
9fc0f6
@@ -125,7 +125,14 @@ struct Manager {
9fc0f6
         /* Units that should be realized */
9fc0f6
         LIST_HEAD(Unit, cgroup_queue);
9fc0f6
 
9fc0f6
-        Hashmap *watch_pids;  /* pid => Unit object n:1 */
9fc0f6
+        /* We use two hash tables here, since the same PID might be
9fc0f6
+         * watched by two different units: once the unit that forked
9fc0f6
+         * it off, and possibly a different unit to which it was
9fc0f6
+         * joined as cgroup member. Since we know that it is either
9fc0f6
+         * one or two units for each PID we just use to hashmaps
9fc0f6
+         * here. */
9fc0f6
+        Hashmap *watch_pids1;  /* pid => Unit object n:1 */
9fc0f6
+        Hashmap *watch_pids2;  /* pid => Unit object n:1 */
9fc0f6
 
9fc0f6
         char *notify_socket;
9fc0f6
 
9fc0f6
diff --git a/src/core/unit.c b/src/core/unit.c
9fc0f6
index 0332094..a510eb2 100644
9fc0f6
--- a/src/core/unit.c
9fc0f6
+++ b/src/core/unit.c
9fc0f6
@@ -1665,16 +1665,27 @@ int unit_watch_pid(Unit *u, pid_t pid) {
9fc0f6
         assert(u);
9fc0f6
         assert(pid >= 1);
9fc0f6
 
9fc0f6
+        /* Watch a specific PID. We only support one or two units
9fc0f6
+         * watching each PID for now, not more. */
9fc0f6
+
9fc0f6
+        r = hashmap_ensure_allocated(&u->manager->watch_pids1, trivial_hash_func, trivial_compare_func);
9fc0f6
+        if (r < 0)
9fc0f6
+                return r;
9fc0f6
+
9fc0f6
         r = set_ensure_allocated(&u->pids, trivial_hash_func, trivial_compare_func);
9fc0f6
         if (r < 0)
9fc0f6
                 return r;
9fc0f6
 
9fc0f6
-        /* Watch a specific PID. We only support one unit watching
9fc0f6
-         * each PID for now. */
9fc0f6
+        r = hashmap_put(u->manager->watch_pids1, LONG_TO_PTR(pid), u);
9fc0f6
+        if (r == -EEXIST) {
9fc0f6
+                r = hashmap_ensure_allocated(&u->manager->watch_pids2, trivial_hash_func, trivial_compare_func);
9fc0f6
+                if (r < 0)
9fc0f6
+                        return r;
9fc0f6
 
9fc0f6
-        r = set_put(u->pids, LONG_TO_PTR(pid));
9fc0f6
+                r = hashmap_put(u->manager->watch_pids2, LONG_TO_PTR(pid), u);
9fc0f6
+        }
9fc0f6
 
9fc0f6
-        q = hashmap_put(u->manager->watch_pids, LONG_TO_PTR(pid), u);
9fc0f6
+        q = set_put(u->pids, LONG_TO_PTR(pid));
9fc0f6
         if (q < 0)
9fc0f6
                 return q;
9fc0f6
 
9fc0f6
@@ -1685,7 +1696,8 @@ void unit_unwatch_pid(Unit *u, pid_t pid) {
9fc0f6
         assert(u);
9fc0f6
         assert(pid >= 1);
9fc0f6
 
9fc0f6
-        hashmap_remove_value(u->manager->watch_pids, LONG_TO_PTR(pid), u);
9fc0f6
+        hashmap_remove_value(u->manager->watch_pids1, LONG_TO_PTR(pid), u);
9fc0f6
+        hashmap_remove_value(u->manager->watch_pids2, LONG_TO_PTR(pid), u);
9fc0f6
         set_remove(u->pids, LONG_TO_PTR(pid));
9fc0f6
 }
9fc0f6
 
9fc0f6
@@ -1758,8 +1770,10 @@ void unit_unwatch_all_pids(Unit *u) {
9fc0f6
 
9fc0f6
         assert(u);
9fc0f6
 
9fc0f6
-        SET_FOREACH(e, u->pids, i)
9fc0f6
-                hashmap_remove_value(u->manager->watch_pids, e, u);
9fc0f6
+        SET_FOREACH(e, u->pids, i) {
9fc0f6
+                hashmap_remove_value(u->manager->watch_pids1, e, u);
9fc0f6
+                hashmap_remove_value(u->manager->watch_pids2, e, u);
9fc0f6
+        }
9fc0f6
 
9fc0f6
         set_free(u->pids);
9fc0f6
         u->pids = NULL;