9fc0f6
From 8581dc72de05227a236b6ff3751c40f1e0be1b2f Mon Sep 17 00:00:00 2001
9fc0f6
From: Lennart Poettering <lennart@poettering.net>
9fc0f6
Date: Thu, 6 Mar 2014 02:19:42 +0100
9fc0f6
Subject: [PATCH] core: correctly unregister PIDs from PID hashtables
9fc0f6
9fc0f6
Conflicts:
9fc0f6
	src/core/unit.c
9fc0f6
---
9fc0f6
 src/core/unit.c | 41 ++++++++++++++++++-----------------------
9fc0f6
 1 file changed, 18 insertions(+), 23 deletions(-)
9fc0f6
9fc0f6
diff --git a/src/core/unit.c b/src/core/unit.c
9fc0f6
index a510eb2..6ee34ec 100644
9fc0f6
--- a/src/core/unit.c
9fc0f6
+++ b/src/core/unit.c
9fc0f6
@@ -1668,11 +1668,11 @@ int unit_watch_pid(Unit *u, pid_t pid) {
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
+        r = set_ensure_allocated(&u->pids, 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
+        r = hashmap_ensure_allocated(&u->manager->watch_pids1, trivial_hash_func, trivial_compare_func);
9fc0f6
         if (r < 0)
9fc0f6
                 return r;
9fc0f6
 
9fc0f6
@@ -1701,7 +1701,17 @@ void unit_unwatch_pid(Unit *u, pid_t pid) {
9fc0f6
         set_remove(u->pids, LONG_TO_PTR(pid));
9fc0f6
 }
9fc0f6
 
9fc0f6
-static int watch_pids_in_path(Unit *u, const char *path) {
9fc0f6
+void unit_unwatch_all_pids(Unit *u) {
9fc0f6
+        assert(u);
9fc0f6
+
9fc0f6
+        while (!set_isempty(u->pids))
9fc0f6
+                unit_unwatch_pid(u, PTR_TO_LONG(set_first(u->pids)));
9fc0f6
+
9fc0f6
+        set_free(u->pids);
9fc0f6
+        u->pids = NULL;
9fc0f6
+}
9fc0f6
+
9fc0f6
+static int unit_watch_pids_in_path(Unit *u, const char *path) {
9fc0f6
         _cleanup_closedir_ DIR *d = NULL;
9fc0f6
         _cleanup_fclose_ FILE *f = NULL;
9fc0f6
         int ret = 0, r;
9fc0f6
@@ -1739,7 +1749,7 @@ static int watch_pids_in_path(Unit *u, const char *path) {
9fc0f6
                         if (!p)
9fc0f6
                                 return -ENOMEM;
9fc0f6
 
9fc0f6
-                        r = watch_pids_in_path(u, p);
9fc0f6
+                        r = unit_watch_pids_in_path(u, p);
9fc0f6
                         if (r < 0 && ret >= 0)
9fc0f6
                                 ret = r;
9fc0f6
                 }
9fc0f6
@@ -1756,27 +1766,12 @@ static int watch_pids_in_path(Unit *u, const char *path) {
9fc0f6
 int unit_watch_all_pids(Unit *u) {
9fc0f6
         assert(u);
9fc0f6
 
9fc0f6
-        if (!u->cgroup_path)
9fc0f6
-                return -ENOENT;
9fc0f6
-
9fc0f6
         /* Adds all PIDs from our cgroup to the set of PIDs we watch */
9fc0f6
 
9fc0f6
-        return watch_pids_in_path(u, u->cgroup_path);
9fc0f6
-}
9fc0f6
-
9fc0f6
-void unit_unwatch_all_pids(Unit *u) {
9fc0f6
-        Iterator i;
9fc0f6
-        void *e;
9fc0f6
-
9fc0f6
-        assert(u);
9fc0f6
-
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
+        if (!u->cgroup_path)
9fc0f6
+                return -ENOENT;
9fc0f6
 
9fc0f6
-        set_free(u->pids);
9fc0f6
-        u->pids = NULL;
9fc0f6
+        return unit_watch_pids_in_path(u, u->cgroup_path);
9fc0f6
 }
9fc0f6
 
9fc0f6
 void unit_tidy_watch_pids(Unit *u, pid_t except1, pid_t except2) {
9fc0f6
@@ -1794,7 +1789,7 @@ void unit_tidy_watch_pids(Unit *u, pid_t except1, pid_t except2) {
9fc0f6
                         continue;
9fc0f6
 
9fc0f6
                 if (kill(pid, 0) < 0 && errno == ESRCH)
9fc0f6
-                        set_remove(u->pids, e);
9fc0f6
+                        unit_unwatch_pid(u, pid);
9fc0f6
         }
9fc0f6
 }
9fc0f6