]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: add pidref_is_unwaited() and make pid_is_unwaited() return errors
authorLennart Poettering <lennart@poettering.net>
Tue, 17 Oct 2023 10:32:00 +0000 (12:32 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 18 Oct 2023 12:49:40 +0000 (14:49 +0200)
src/basic/process-util.c
src/basic/process-util.h
src/core/mount.c
src/core/service.c
src/core/socket.c
src/core/swap.c
src/core/unit.c
src/journal/journald-context.c
src/libsystemd/sd-bus/bus-creds.c
src/test/test-process-util.c

index c15460d8c0c0392f0c39a875aae7cd3644e7ba2b..75bbebd60fb70da560c40e26c18faaffc81f6913 100644 (file)
@@ -1045,11 +1045,11 @@ int pidref_is_my_child(const PidRef *pid) {
         return result;
 }
 
-bool pid_is_unwaited(pid_t pid) {
+int pid_is_unwaited(pid_t pid) {
         /* Checks whether a PID is still valid at all, including a zombie */
 
         if (pid < 0)
-                return false;
+                return -ESRCH;
 
         if (pid <= 1) /* If we or PID 1 would be dead and have been waited for, this code would not be running */
                 return true;
@@ -1063,6 +1063,24 @@ bool pid_is_unwaited(pid_t pid) {
         return errno != ESRCH;
 }
 
+int pidref_is_unwaited(const PidRef *pid) {
+        int r;
+
+        if (!pidref_is_set(pid))
+                return -ESRCH;
+
+        if (pid->pid == 1 || pidref_is_self(pid))
+                return true;
+
+        r = pidref_kill(pid, 0);
+        if (r == -ESRCH)
+                return false;
+        if (r < 0)
+                return r;
+
+        return true;
+}
+
 int pid_is_alive(pid_t pid) {
         int r;
 
index 437a0ccd3062347b6e77eca3ebf8957b65a76bd5..7c05a69ca0971e85676b0a44bbcfc7f8910f1d60 100644 (file)
@@ -88,7 +88,8 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value);
 
 int pid_is_alive(pid_t pid);
 int pidref_is_alive(const PidRef *pidref);
-bool pid_is_unwaited(pid_t pid);
+int pid_is_unwaited(pid_t pid);
+int pidref_is_unwaited(const PidRef *pidref);
 int pid_is_my_child(pid_t pid);
 int pidref_is_my_child(const PidRef *pidref);
 int pid_from_same_root_fs(pid_t pid);
index b6b76c560254789327ce8b503099f717551a2355..123c87ea1af6b15a00b98809dd5d56d5a14b9f84 100644 (file)
@@ -796,7 +796,7 @@ static int mount_coldplug(Unit *u) {
                 return 0;
 
         if (pidref_is_set(&m->control_pid) &&
-            pid_is_unwaited(m->control_pid.pid) &&
+            pidref_is_unwaited(&m->control_pid) > 0 &&
             MOUNT_STATE_WITH_PROCESS(m->deserialized_state)) {
 
                 r = unit_watch_pidref(UNIT(m), &m->control_pid, /* exclusive= */ false);
index fbd09844598d4658fa99ceaf3615ad4847149afa..e2b8401560a399e788fcbbfbb081e4b64ed02c31 100644 (file)
@@ -1326,7 +1326,7 @@ static int service_coldplug(Unit *u) {
                 return r;
 
         if (pidref_is_set(&s->main_pid) &&
-            pid_is_unwaited(s->main_pid.pid) &&
+            pidref_is_unwaited(&s->main_pid) > 0 &&
             (IN_SET(s->deserialized_state,
                     SERVICE_START, SERVICE_START_POST,
                     SERVICE_RUNNING,
@@ -1339,7 +1339,7 @@ static int service_coldplug(Unit *u) {
         }
 
         if (pidref_is_set(&s->control_pid) &&
-            pid_is_unwaited(s->control_pid.pid) &&
+            pidref_is_unwaited(&s->control_pid) > 0 &&
             IN_SET(s->deserialized_state,
                    SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
                    SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY,
index dff4bc42d79a00eed258ef78e29b12fddc9be078..62ce29076ee1c69643cec6f9d3c54a7eb7344c2d 100644 (file)
@@ -1852,7 +1852,7 @@ static int socket_coldplug(Unit *u) {
                 return 0;
 
         if (pidref_is_set(&s->control_pid) &&
-            pid_is_unwaited(s->control_pid.pid) &&
+            pidref_is_unwaited(&s->control_pid) > 0 &&
             IN_SET(s->deserialized_state,
                    SOCKET_START_PRE,
                    SOCKET_START_CHOWN,
index d0bce8c44bd7fafe5dface80eff48cda9af5d093..d0aae9a5343a3625dbf2a9e38e875cf6d4293278 100644 (file)
@@ -557,7 +557,7 @@ static int swap_coldplug(Unit *u) {
                 return 0;
 
         if (pidref_is_set(&s->control_pid) &&
-            pid_is_unwaited(s->control_pid.pid) &&
+            pidref_is_unwaited(&s->control_pid) > 0 &&
             SWAP_STATE_WITH_PROCESS(new_state)) {
 
                 r = unit_watch_pidref(UNIT(s), &s->control_pid, /* exclusive= */ false);
index da71e09f15685d2ea1fafd61e20088d5709e8e0b..a60656709adaa478ee01d74ad1787371aa0389e9 100644 (file)
@@ -2982,7 +2982,7 @@ static void unit_tidy_watch_pids(Unit *u) {
                 if (pidref_equal(except1, e) || pidref_equal(except2, e))
                         continue;
 
-                if (!pid_is_unwaited(e->pid))
+                if (pidref_is_unwaited(e) <= 0)
                         unit_unwatch_pidref(u, e);
         }
 }
index 1f653801ad74c032fbb014d079733fc3d1f771a9..cc5b4bc6add28edd135c3ff716432bc12e93245d 100644 (file)
@@ -609,7 +609,7 @@ static void client_context_try_shrink_to(Server *s, size_t limit) {
 
                         assert(c->n_ref == 0);
 
-                        if (!pid_is_unwaited(c->pid))
+                        if (pid_is_unwaited(c->pid) == 0)
                                 client_context_free(s, c);
                         else
                                 idx ++;
index 64af18de9e59605c064f1a7aca7b14453fa80576..c6d8caaf6d71b4a189bf0101cac0cd180f5d745a 100644 (file)
@@ -1097,7 +1097,7 @@ int bus_creds_add_more(sd_bus_creds *c, uint64_t mask, pid_t pid, pid_t tid) {
         if (r == 0)
                 return -ESRCH;
 
-        if (tid > 0 && tid != pid && !pid_is_unwaited(tid))
+        if (tid > 0 && tid != pid && pid_is_unwaited(tid) == 0)
                 return -ESRCH;
 
         c->augmented = missing & c->mask;
index e6dea2f05f09e3cdd1cea4e2b8d7073e50c76d8f..2ff9f1fad5a1c1d10ae05a703937cb2116556935 100644 (file)
@@ -207,11 +207,11 @@ TEST(pid_is_unwaited) {
         } else {
                 int status;
 
-                waitpid(pid, &status, 0);
-                assert_se(!pid_is_unwaited(pid));
+                assert_se(waitpid(pid, &status, 0) == pid);
+                assert_se(pid_is_unwaited(pid) == 0);
         }
-        assert_se(pid_is_unwaited(getpid_cached()));
-        assert_se(!pid_is_unwaited(-1));
+        assert_se(pid_is_unwaited(getpid_cached()) > 0);
+        assert_se(pid_is_unwaited(-1) < 0);
 }
 
 TEST(pid_is_alive) {