Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include "log.h"
4 : #include "manager.h"
5 : #include "rm-rf.h"
6 : #include "service.h"
7 : #include "test-helper.h"
8 : #include "tests.h"
9 :
10 1 : int main(int argc, char *argv[]) {
11 1 : _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
12 1 : _cleanup_(manager_freep) Manager *m = NULL;
13 : Unit *a, *b, *c, *u;
14 : int r;
15 :
16 1 : test_setup_logging(LOG_DEBUG);
17 :
18 1 : if (getuid() != 0)
19 1 : return log_tests_skipped("not root");
20 0 : r = enter_cgroup_subroot();
21 0 : if (r == -ENOMEDIUM)
22 0 : return log_tests_skipped("cgroupfs not available");
23 :
24 0 : assert_se(set_unit_path(get_testdata_dir()) >= 0);
25 0 : assert_se(runtime_dir = setup_fake_runtime_dir());
26 :
27 0 : assert_se(manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &m) >= 0);
28 0 : assert_se(manager_startup(m, NULL, NULL) >= 0);
29 :
30 0 : assert_se(a = unit_new(m, sizeof(Service)));
31 0 : assert_se(unit_add_name(a, "a.service") >= 0);
32 0 : assert_se(set_isempty(a->pids));
33 :
34 0 : assert_se(b = unit_new(m, sizeof(Service)));
35 0 : assert_se(unit_add_name(b, "b.service") >= 0);
36 0 : assert_se(set_isempty(b->pids));
37 :
38 0 : assert_se(c = unit_new(m, sizeof(Service)));
39 0 : assert_se(unit_add_name(c, "c.service") >= 0);
40 0 : assert_se(set_isempty(c->pids));
41 :
42 0 : assert_se(hashmap_isempty(m->watch_pids));
43 0 : assert_se(manager_get_unit_by_pid(m, 4711) == NULL);
44 :
45 0 : assert_se(unit_watch_pid(a, 4711, false) >= 0);
46 0 : assert_se(manager_get_unit_by_pid(m, 4711) == a);
47 :
48 0 : assert_se(unit_watch_pid(a, 4711, false) >= 0);
49 0 : assert_se(manager_get_unit_by_pid(m, 4711) == a);
50 :
51 0 : assert_se(unit_watch_pid(b, 4711, false) >= 0);
52 0 : u = manager_get_unit_by_pid(m, 4711);
53 0 : assert_se(u == a || u == b);
54 :
55 0 : assert_se(unit_watch_pid(b, 4711, false) >= 0);
56 0 : u = manager_get_unit_by_pid(m, 4711);
57 0 : assert_se(u == a || u == b);
58 :
59 0 : assert_se(unit_watch_pid(c, 4711, false) >= 0);
60 0 : u = manager_get_unit_by_pid(m, 4711);
61 0 : assert_se(u == a || u == b || u == c);
62 :
63 0 : assert_se(unit_watch_pid(c, 4711, false) >= 0);
64 0 : u = manager_get_unit_by_pid(m, 4711);
65 0 : assert_se(u == a || u == b || u == c);
66 :
67 0 : unit_unwatch_pid(b, 4711);
68 0 : u = manager_get_unit_by_pid(m, 4711);
69 0 : assert_se(u == a || u == c);
70 :
71 0 : unit_unwatch_pid(b, 4711);
72 0 : u = manager_get_unit_by_pid(m, 4711);
73 0 : assert_se(u == a || u == c);
74 :
75 0 : unit_unwatch_pid(a, 4711);
76 0 : assert_se(manager_get_unit_by_pid(m, 4711) == c);
77 :
78 0 : unit_unwatch_pid(a, 4711);
79 0 : assert_se(manager_get_unit_by_pid(m, 4711) == c);
80 :
81 0 : unit_unwatch_pid(c, 4711);
82 0 : assert_se(manager_get_unit_by_pid(m, 4711) == NULL);
83 :
84 0 : unit_unwatch_pid(c, 4711);
85 0 : assert_se(manager_get_unit_by_pid(m, 4711) == NULL);
86 :
87 0 : return 0;
88 : }
|