summaryrefslogtreecommitdiffstats
path: root/src/test/test-watch-pid.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:49:52 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:49:52 +0000
commit55944e5e40b1be2afc4855d8d2baf4b73d1876b5 (patch)
tree33f869f55a1b149e9b7c2b7e201867ca5dd52992 /src/test/test-watch-pid.c
parentInitial commit. (diff)
downloadsystemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.tar.xz
systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.zip
Adding upstream version 255.4.upstream/255.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/test-watch-pid.c')
-rw-r--r--src/test/test-watch-pid.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/test/test-watch-pid.c b/src/test/test-watch-pid.c
new file mode 100644
index 0000000..b0c2c06
--- /dev/null
+++ b/src/test/test-watch-pid.c
@@ -0,0 +1,102 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "log.h"
+#include "manager.h"
+#include "process-util.h"
+#include "rm-rf.h"
+#include "service.h"
+#include "tests.h"
+
+int main(int argc, char *argv[]) {
+ _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
+ _cleanup_(manager_freep) Manager *m = NULL;
+ Unit *a, *b, *c, *u;
+ int r;
+
+ test_setup_logging(LOG_DEBUG);
+
+ if (getuid() != 0)
+ return log_tests_skipped("not root");
+ r = enter_cgroup_subroot(NULL);
+ if (r == -ENOMEDIUM)
+ return log_tests_skipped("cgroupfs not available");
+
+ _cleanup_free_ char *unit_dir = NULL;
+ assert_se(get_testdata_dir("units/", &unit_dir) >= 0);
+ assert_se(set_unit_path(unit_dir) >= 0);
+
+ assert_se(runtime_dir = setup_fake_runtime_dir());
+
+ assert_se(manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m) >= 0);
+ assert_se(manager_startup(m, NULL, NULL, NULL) >= 0);
+
+ assert_se(a = unit_new(m, sizeof(Service)));
+ assert_se(unit_add_name(a, "a.service") >= 0);
+ assert_se(set_isempty(a->pids));
+
+ assert_se(b = unit_new(m, sizeof(Service)));
+ assert_se(unit_add_name(b, "b.service") >= 0);
+ assert_se(set_isempty(b->pids));
+
+ assert_se(c = unit_new(m, sizeof(Service)));
+ assert_se(unit_add_name(c, "c.service") >= 0);
+ assert_se(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_se(pid >= 0);
+
+ assert_se(hashmap_isempty(m->watch_pids));
+ assert_se(manager_get_unit_by_pid(m, pid) == NULL);
+
+ assert_se(unit_watch_pid(a, pid, false) >= 0);
+ assert_se(manager_get_unit_by_pid(m, pid) == a);
+
+ assert_se(unit_watch_pid(a, pid, false) >= 0);
+ assert_se(manager_get_unit_by_pid(m, pid) == a);
+
+ assert_se(unit_watch_pid(b, pid, false) >= 0);
+ u = manager_get_unit_by_pid(m, pid);
+ assert_se(u == a || u == b);
+
+ assert_se(unit_watch_pid(b, pid, false) >= 0);
+ u = manager_get_unit_by_pid(m, pid);
+ assert_se(u == a || u == b);
+
+ assert_se(unit_watch_pid(c, pid, false) >= 0);
+ u = manager_get_unit_by_pid(m, pid);
+ assert_se(u == a || u == b || u == c);
+
+ assert_se(unit_watch_pid(c, pid, false) >= 0);
+ u = manager_get_unit_by_pid(m, pid);
+ assert_se(u == a || u == b || u == c);
+
+ unit_unwatch_pid(b, pid);
+ u = manager_get_unit_by_pid(m, pid);
+ assert_se(u == a || u == c);
+
+ unit_unwatch_pid(b, pid);
+ u = manager_get_unit_by_pid(m, pid);
+ assert_se(u == a || u == c);
+
+ unit_unwatch_pid(a, pid);
+ assert_se(manager_get_unit_by_pid(m, pid) == c);
+
+ unit_unwatch_pid(a, pid);
+ assert_se(manager_get_unit_by_pid(m, pid) == c);
+
+ unit_unwatch_pid(c, pid);
+ assert_se(manager_get_unit_by_pid(m, pid) == NULL);
+
+ unit_unwatch_pid(c, pid);
+ assert_se(manager_get_unit_by_pid(m, pid) == NULL);
+
+ return 0;
+}