core: split up manager_get_unit_by_pid()
authorLennart Poettering <lennart@poettering.net>
Thu, 3 Sep 2015 12:57:44 +0000 (14:57 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 4 Sep 2015 07:07:31 +0000 (09:07 +0200)
Let's move the actual cgroup part of it into a new separate function
manager_get_unit_by_pid_cgroup(), and then make
manager_get_unit_by_pid() just a wrapper that also checks the two pid
hashmaps.

Then, let's make sure the various calls that want to deliver events to
the owners of a PID check both hashmaps and the cgroup and deliver the
event to *each* of them. OTOH make sure bus calls like GetUnitByPID()
continue to check the PID hashmaps first and the cgroup only as
fallback.

src/core/cgroup.c
src/core/cgroup.h
src/core/manager.c

index 9f06b28..0a5a08a 100644 (file)
@@ -1378,9 +1378,8 @@ Unit* manager_get_unit_by_cgroup(Manager *m, const char *cgroup) {
         }
 }
 
-Unit *manager_get_unit_by_pid(Manager *m, pid_t pid) {
+Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid) {
         _cleanup_free_ char *cgroup = NULL;
-        Unit *u;
         int r;
 
         assert(m);
@@ -1388,6 +1387,21 @@ Unit *manager_get_unit_by_pid(Manager *m, pid_t pid) {
         if (pid <= 0)
                 return NULL;
 
+        r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup);
+        if (r < 0)
+                return NULL;
+
+        return manager_get_unit_by_cgroup(m, cgroup);
+}
+
+Unit *manager_get_unit_by_pid(Manager *m, pid_t pid) {
+        Unit *u;
+
+        assert(m);
+
+        if (pid <= 0)
+                return NULL;
+
         if (pid == 1)
                 return hashmap_get(m->units, SPECIAL_INIT_SCOPE);
 
@@ -1399,11 +1413,7 @@ Unit *manager_get_unit_by_pid(Manager *m, pid_t pid) {
         if (u)
                 return u;
 
-        r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cgroup);
-        if (r < 0)
-                return NULL;
-
-        return manager_get_unit_by_cgroup(m, cgroup);
+        return manager_get_unit_by_pid_cgroup(m, pid);
 }
 
 int manager_notify_cgroup_empty(Manager *m, const char *cgroup) {
index 1ce21f4..438f5bf 100644 (file)
@@ -130,6 +130,7 @@ void manager_shutdown_cgroup(Manager *m, bool delete);
 unsigned manager_dispatch_cgroup_queue(Manager *m);
 
 Unit *manager_get_unit_by_cgroup(Manager *m, const char *cgroup);
+Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid);
 Unit* manager_get_unit_by_pid(Manager *m, pid_t pid);
 
 int unit_search_main_pid(Unit *u, pid_t *ret);
index c2d262a..fc10ddb 100644 (file)
@@ -1585,7 +1585,7 @@ static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t
 
                 /* Notify every unit that might be interested, but try
                  * to avoid notifying the same one multiple times. */
-                u1 = manager_get_unit_by_pid(m, ucred->pid);
+                u1 = manager_get_unit_by_pid_cgroup(m, ucred->pid);
                 if (u1) {
                         manager_invoke_notify_message(m, u1, ucred->pid, buf, n, fds);
                         found = true;
@@ -1663,7 +1663,7 @@ static int manager_dispatch_sigchld(Manager *m) {
 
                         /* And now figure out the unit this belongs
                          * to, it might be multiple... */
-                        u1 = manager_get_unit_by_pid(m, si.si_pid);
+                        u1 = manager_get_unit_by_pid_cgroup(m, si.si_pid);
                         if (u1)
                                 invoke_sigchld_event(m, u1, &si);
                         u2 = hashmap_get(m->watch_pids1, PID_TO_PTR(si.si_pid));