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