]>
git.proxmox.com Git - systemd.git/blob - src/test/test-watch-pid.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "test-helper.h"
10 int main(int argc
, char *argv
[]) {
11 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
12 _cleanup_(manager_freep
) Manager
*m
= NULL
;
16 test_setup_logging(LOG_DEBUG
);
19 return log_tests_skipped("not root");
20 r
= enter_cgroup_subroot();
22 return log_tests_skipped("cgroupfs not available");
24 assert_se(set_unit_path(get_testdata_dir()) >= 0);
25 assert_se(runtime_dir
= setup_fake_runtime_dir());
27 assert_se(manager_new(UNIT_FILE_USER
, MANAGER_TEST_RUN_BASIC
, &m
) >= 0);
28 assert_se(manager_startup(m
, NULL
, NULL
) >= 0);
30 assert_se(a
= unit_new(m
, sizeof(Service
)));
31 assert_se(unit_add_name(a
, "a.service") >= 0);
32 assert_se(set_isempty(a
->pids
));
34 assert_se(b
= unit_new(m
, sizeof(Service
)));
35 assert_se(unit_add_name(b
, "b.service") >= 0);
36 assert_se(set_isempty(b
->pids
));
38 assert_se(c
= unit_new(m
, sizeof(Service
)));
39 assert_se(unit_add_name(c
, "c.service") >= 0);
40 assert_se(set_isempty(c
->pids
));
42 assert_se(hashmap_isempty(m
->watch_pids
));
43 assert_se(manager_get_unit_by_pid(m
, 4711) == NULL
);
45 assert_se(unit_watch_pid(a
, 4711, false) >= 0);
46 assert_se(manager_get_unit_by_pid(m
, 4711) == a
);
48 assert_se(unit_watch_pid(a
, 4711, false) >= 0);
49 assert_se(manager_get_unit_by_pid(m
, 4711) == a
);
51 assert_se(unit_watch_pid(b
, 4711, false) >= 0);
52 u
= manager_get_unit_by_pid(m
, 4711);
53 assert_se(u
== a
|| u
== b
);
55 assert_se(unit_watch_pid(b
, 4711, false) >= 0);
56 u
= manager_get_unit_by_pid(m
, 4711);
57 assert_se(u
== a
|| u
== b
);
59 assert_se(unit_watch_pid(c
, 4711, false) >= 0);
60 u
= manager_get_unit_by_pid(m
, 4711);
61 assert_se(u
== a
|| u
== b
|| u
== c
);
63 assert_se(unit_watch_pid(c
, 4711, false) >= 0);
64 u
= manager_get_unit_by_pid(m
, 4711);
65 assert_se(u
== a
|| u
== b
|| u
== c
);
67 unit_unwatch_pid(b
, 4711);
68 u
= manager_get_unit_by_pid(m
, 4711);
69 assert_se(u
== a
|| u
== c
);
71 unit_unwatch_pid(b
, 4711);
72 u
= manager_get_unit_by_pid(m
, 4711);
73 assert_se(u
== a
|| u
== c
);
75 unit_unwatch_pid(a
, 4711);
76 assert_se(manager_get_unit_by_pid(m
, 4711) == c
);
78 unit_unwatch_pid(a
, 4711);
79 assert_se(manager_get_unit_by_pid(m
, 4711) == c
);
81 unit_unwatch_pid(c
, 4711);
82 assert_se(manager_get_unit_by_pid(m
, 4711) == NULL
);
84 unit_unwatch_pid(c
, 4711);
85 assert_se(manager_get_unit_by_pid(m
, 4711) == NULL
);