Debian Codegraph @

src/test/test-watch-pid.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "manager.h"
6 #include "process-util.h"
7 #include "rm-rf.h"
8 #include "service.h"
9 #include "set.h"
10 #include "tests.h"
11
12 TEST(watch_pid) {
13 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
14 _cleanup_(manager_freep) Manager *m = NULL;
15 Unit *a, *b, *c, *u;
16
17 _cleanup_free_ char *unit_dir = NULL;
18 ASSERT_OK(get_testdata_dir("units/", &unit_dir));
19 ASSERT_OK(setenv_unit_path(unit_dir));
20
21 ASSERT_NOT_NULL(runtime_dir = setup_fake_runtime_dir());
22
23 ASSERT_OK(manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m));
24 ASSERT_OK(manager_startup(m, NULL, NULL, NULL, NULL));
25
26 ASSERT_NOT_NULL(a = unit_new(m, sizeof(Service)));
27 ASSERT_OK(unit_add_name(a, "a.service"));
28 ASSERT_TRUE(set_isempty(a->pids));
29
30 ASSERT_NOT_NULL(b = unit_new(m, sizeof(Service)));
31 ASSERT_OK(unit_add_name(b, "b.service"));
32 ASSERT_TRUE(set_isempty(b->pids));
33
34 ASSERT_NOT_NULL(c = unit_new(m, sizeof(Service)));
35 ASSERT_OK(unit_add_name(c, "c.service"));
36 ASSERT_TRUE(set_isempty(c->pids));
37
38 /* Fork off a child so that we have a PID to watch */
39 _cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
40 ASSERT_OK_POSITIVE(pidref_safe_fork("(child)", FORK_FREEZE, &pidref));
41
42 ASSERT_TRUE(hashmap_isempty(m->watch_pids));
43 ASSERT_NULL(manager_get_unit_by_pidref(m, &pidref));
44
45 ASSERT_OK(unit_watch_pidref(a, &pidref, false));
46 ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), a);
47
48 ASSERT_OK(unit_watch_pidref(a, &pidref, false));
49 ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), a);
50
51 ASSERT_OK(unit_watch_pidref(b, &pidref, false));
52 u = manager_get_unit_by_pidref(m, &pidref);
53 ASSERT_TRUE(u == a || u == b);
54
55 ASSERT_OK(unit_watch_pidref(b, &pidref, false));
56 u = manager_get_unit_by_pidref(m, &pidref);
57 ASSERT_TRUE(u == a || u == b);
58
59 ASSERT_OK(unit_watch_pidref(c, &pidref, false));
60 u = manager_get_unit_by_pidref(m, &pidref);
61 ASSERT_TRUE(u == a || u == b || u == c);
62
63 ASSERT_OK(unit_watch_pidref(c, &pidref, false));
64 u = manager_get_unit_by_pidref(m, &pidref);
65 ASSERT_TRUE(u == a || u == b || u == c);
66
67 unit_unwatch_pidref(b, &pidref);
68 u = manager_get_unit_by_pidref(m, &pidref);
69 ASSERT_TRUE(u == a || u == c);
70
71 unit_unwatch_pidref(b, &pidref);
72 u = manager_get_unit_by_pidref(m, &pidref);
73 ASSERT_TRUE(u == a || u == c);
74
75 unit_unwatch_pidref(a, &pidref);
76 ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), c);
77
78 unit_unwatch_pidref(a, &pidref);
79 ASSERT_PTR_EQ(manager_get_unit_by_pidref(m, &pidref), c);
80
81 unit_unwatch_pidref(c, &pidref);
82 ASSERT_NULL(manager_get_unit_by_pidref(m, &pidref));
83
84 unit_unwatch_pidref(c, &pidref);
85 ASSERT_NULL(manager_get_unit_by_pidref(m, &pidref));
86 }
87
88 static int intro(void) {
89 int r;
90
91 if (getuid() != 0)
92 return log_tests_skipped("not root");
93
94 r = enter_cgroup_subroot(NULL);
95 if (r < 0)
96 return log_tests_skipped_errno(r, "cgroupfs not available");
97
98 return EXIT_SUCCESS;
99 }
100
101 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);