4f94566
From ba421b96a473a4e608c2b1d01d3bb3dfe36c23a6 Mon Sep 17 00:00:00 2001
4f94566
From: Lennart Poettering <lennart@poettering.net>
4f94566
Date: Thu, 6 Mar 2014 02:19:42 +0100
4f94566
Subject: [PATCH] core: correctly unregister PIDs from PID hashtables
4f94566
4f94566
Conflicts:
4f94566
	src/core/unit.c
4f94566
---
4f94566
 src/core/unit.c | 41 ++++++++++++++++++-----------------------
4f94566
 1 file changed, 18 insertions(+), 23 deletions(-)
4f94566
4f94566
diff --git a/src/core/unit.c b/src/core/unit.c
4f94566
index dc88862..9a7720d 100644
4f94566
--- a/src/core/unit.c
4f94566
+++ b/src/core/unit.c
4f94566
@@ -1666,11 +1666,11 @@ int unit_watch_pid(Unit *u, pid_t pid) {
4f94566
         /* Watch a specific PID. We only support one or two units
4f94566
          * watching each PID for now, not more. */
4f94566
 
4f94566
-        r = hashmap_ensure_allocated(&u->manager->watch_pids1, trivial_hash_func, trivial_compare_func);
4f94566
+        r = set_ensure_allocated(&u->pids, trivial_hash_func, trivial_compare_func);
4f94566
         if (r < 0)
4f94566
                 return r;
4f94566
 
4f94566
-        r = set_ensure_allocated(&u->pids, trivial_hash_func, trivial_compare_func);
4f94566
+        r = hashmap_ensure_allocated(&u->manager->watch_pids1, trivial_hash_func, trivial_compare_func);
4f94566
         if (r < 0)
4f94566
                 return r;
4f94566
 
4f94566
@@ -1699,7 +1699,17 @@ void unit_unwatch_pid(Unit *u, pid_t pid) {
4f94566
         set_remove(u->pids, LONG_TO_PTR(pid));
4f94566
 }
4f94566
 
4f94566
-static int watch_pids_in_path(Unit *u, const char *path) {
4f94566
+void unit_unwatch_all_pids(Unit *u) {
4f94566
+        assert(u);
4f94566
+
4f94566
+        while (!set_isempty(u->pids))
4f94566
+                unit_unwatch_pid(u, PTR_TO_LONG(set_first(u->pids)));
4f94566
+
4f94566
+        set_free(u->pids);
4f94566
+        u->pids = NULL;
4f94566
+}
4f94566
+
4f94566
+static int unit_watch_pids_in_path(Unit *u, const char *path) {
4f94566
         _cleanup_closedir_ DIR *d = NULL;
4f94566
         _cleanup_fclose_ FILE *f = NULL;
4f94566
         int ret = 0, r;
4f94566
@@ -1737,7 +1747,7 @@ static int watch_pids_in_path(Unit *u, const char *path) {
4f94566
                         if (!p)
4f94566
                                 return -ENOMEM;
4f94566
 
4f94566
-                        r = watch_pids_in_path(u, p);
4f94566
+                        r = unit_watch_pids_in_path(u, p);
4f94566
                         if (r < 0 && ret >= 0)
4f94566
                                 ret = r;
4f94566
                 }
4f94566
@@ -1754,27 +1764,12 @@ static int watch_pids_in_path(Unit *u, const char *path) {
4f94566
 int unit_watch_all_pids(Unit *u) {
4f94566
         assert(u);
4f94566
 
4f94566
-        if (!u->cgroup_path)
4f94566
-                return -ENOENT;
4f94566
-
4f94566
         /* Adds all PIDs from our cgroup to the set of PIDs we watch */
4f94566
 
4f94566
-        return watch_pids_in_path(u, u->cgroup_path);
4f94566
-}
4f94566
-
4f94566
-void unit_unwatch_all_pids(Unit *u) {
4f94566
-        Iterator i;
4f94566
-        void *e;
4f94566
-
4f94566
-        assert(u);
4f94566
-
4f94566
-        SET_FOREACH(e, u->pids, i) {
4f94566
-                hashmap_remove_value(u->manager->watch_pids1, e, u);
4f94566
-                hashmap_remove_value(u->manager->watch_pids2, e, u);
4f94566
-        }
4f94566
+        if (!u->cgroup_path)
4f94566
+                return -ENOENT;
4f94566
 
4f94566
-        set_free(u->pids);
4f94566
-        u->pids = NULL;
4f94566
+        return unit_watch_pids_in_path(u, u->cgroup_path);
4f94566
 }
4f94566
 
4f94566
 void unit_tidy_watch_pids(Unit *u, pid_t except1, pid_t except2) {
4f94566
@@ -1792,7 +1787,7 @@ void unit_tidy_watch_pids(Unit *u, pid_t except1, pid_t except2) {
4f94566
                         continue;
4f94566
 
4f94566
                 if (kill(pid, 0) < 0 && errno == ESRCH)
4f94566
-                        set_remove(u->pids, e);
4f94566
+                        unit_unwatch_pid(u, pid);
4f94566
         }
4f94566
 }
4f94566