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