]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-watch-pid: use pidref_safe_fork() with FORK_FREEZE
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 26 Mar 2025 19:02:26 +0000 (04:02 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 26 Mar 2025 19:13:54 +0000 (04:13 +0900)
Then, use pidref version of functions.

src/test/test-watch-pid.c

index 2769377c7f405ae6dd9a9655a8884a1c390f66ba..dd8ad11fa415b44526d630451cb1c4ac325363c2 100644 (file)
@@ -34,60 +34,53 @@ TEST(watch_pid) {
         ASSERT_TRUE(set_isempty(c->pids));
 
         /* Fork off a child so that we have a PID to watch */
-        _cleanup_(sigkill_waitp) pid_t pid = 0;
-        pid = fork();
-        if (pid == 0) {
-                /* Child */
-                pause();
-                _exit(EXIT_SUCCESS);
-        }
-
-        ASSERT_OK(pid);
+        _cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
+        ASSERT_OK_POSITIVE(pidref_safe_fork("(child)", FORK_FREEZE, &pidref));
 
         ASSERT_TRUE(hashmap_isempty(m->watch_pids));
-        ASSERT_NULL(manager_get_unit_by_pid(m, pid));
+        ASSERT_NULL(manager_get_unit_by_pidref(m, &pidref));
 
-        ASSERT_OK(unit_watch_pid(a, pid, false));
-        ASSERT_PTR_EQ(manager_get_unit_by_pid(m, pid), a);
+        ASSERT_OK(unit_watch_pidref(a, &pidref, false));
+        ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), a);
 
-        ASSERT_OK(unit_watch_pid(a, pid, false));
-        ASSERT_PTR_EQ(manager_get_unit_by_pid(m, pid), a);
+        ASSERT_OK(unit_watch_pidref(a, &pidref, false));
+        ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), a);
 
-        ASSERT_OK(unit_watch_pid(b, pid, false));
-        u = manager_get_unit_by_pid(m, pid);
+        ASSERT_OK(unit_watch_pidref(b, &pidref, false));
+        u = manager_get_unit_by_pidref(m, &pidref);
         ASSERT_TRUE(u == a || u == b);
 
-        ASSERT_OK(unit_watch_pid(b, pid, false));
-        u = manager_get_unit_by_pid(m, pid);
+        ASSERT_OK(unit_watch_pidref(b, &pidref, false));
+        u = manager_get_unit_by_pidref(m, &pidref);
         ASSERT_TRUE(u == a || u == b);
 
-        ASSERT_OK(unit_watch_pid(c, pid, false));
-        u = manager_get_unit_by_pid(m, pid);
+        ASSERT_OK(unit_watch_pidref(c, &pidref, false));
+        u = manager_get_unit_by_pidref(m, &pidref);
         ASSERT_TRUE(u == a || u == b || u == c);
 
-        ASSERT_OK(unit_watch_pid(c, pid, false));
-        u = manager_get_unit_by_pid(m, pid);
+        ASSERT_OK(unit_watch_pidref(c, &pidref, false));
+        u = manager_get_unit_by_pidref(m, &pidref);
         ASSERT_TRUE(u == a || u == b || u == c);
 
-        unit_unwatch_pid(b, pid);
-        u = manager_get_unit_by_pid(m, pid);
+        unit_unwatch_pidref(b, &pidref);
+        u = manager_get_unit_by_pidref(m, &pidref);
         ASSERT_TRUE(u == a || u == c);
 
-        unit_unwatch_pid(b, pid);
-        u = manager_get_unit_by_pid(m, pid);
+        unit_unwatch_pidref(b, &pidref);
+        u = manager_get_unit_by_pidref(m, &pidref);
         ASSERT_TRUE(u == a || u == c);
 
-        unit_unwatch_pid(a, pid);
-        ASSERT_PTR_EQ(manager_get_unit_by_pid(m, pid), c);
+        unit_unwatch_pidref(a, &pidref);
+        ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), c);
 
-        unit_unwatch_pid(a, pid);
-        ASSERT_PTR_EQ(manager_get_unit_by_pid(m, pid), c);
+        unit_unwatch_pidref(a, &pidref);
+        ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), c);
 
-        unit_unwatch_pid(c, pid);
-        ASSERT_NULL(manager_get_unit_by_pid(m, pid));
+        unit_unwatch_pidref(c, &pidref);
+        ASSERT_NULL(manager_get_unit_by_pidref(m, &pidref));
 
-        unit_unwatch_pid(c, pid);
-        ASSERT_NULL(manager_get_unit_by_pid(m, pid));
+        unit_unwatch_pidref(c, &pidref);
+        ASSERT_NULL(manager_get_unit_by_pidref(m, &pidref));
 }
 
 static int intro(void) {