]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
6c03089c | 2 | /*** |
96b2fb93 | 3 | Copyright © 2013 Zbigniew Jędrzejewski-Szmek |
6c03089c LP |
4 | ***/ |
5 | ||
b5efdb8a | 6 | #include "alloc-util.h" |
0a05dcc0 | 7 | #include "build.h" |
96cde13a | 8 | #include "cgroup-util.h" |
a0956174 | 9 | #include "dirent-util.h" |
3ffd4af2 | 10 | #include "fd-util.h" |
f97b34a6 | 11 | #include "format-util.h" |
6bedfcbb | 12 | #include "parse-util.h" |
0a05dcc0 | 13 | #include "proc-cmdline.h" |
0b452006 | 14 | #include "process-util.h" |
e5af6e0e | 15 | #include "special.h" |
f0bef277 | 16 | #include "stat-util.h" |
07630cea | 17 | #include "string-util.h" |
9177fa9f | 18 | #include "strv.h" |
3ffd4af2 | 19 | #include "test-helper.h" |
ee104e11 | 20 | #include "user-util.h" |
3ffd4af2 | 21 | #include "util.h" |
96cde13a | 22 | |
7027ff61 | 23 | static void check_p_d_u(const char *path, int code, const char *result) { |
6c03089c | 24 | _cleanup_free_ char *unit = NULL; |
d4fffc4b | 25 | int r; |
96cde13a | 26 | |
d4fffc4b ZJS |
27 | r = cg_path_decode_unit(path, &unit); |
28 | printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code); | |
29 | assert_se(r == code); | |
6c03089c LP |
30 | assert_se(streq_ptr(unit, result)); |
31 | } | |
96cde13a | 32 | |
7027ff61 | 33 | static void test_path_decode_unit(void) { |
d7bd3de0 LP |
34 | check_p_d_u("getty@tty2.service", 0, "getty@tty2.service"); |
35 | check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service"); | |
36 | check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service"); | |
cfeaa44a LP |
37 | check_p_d_u("getty@.service/", -ENXIO, NULL); |
38 | check_p_d_u("getty@.service", -ENXIO, NULL); | |
7027ff61 | 39 | check_p_d_u("getty.service", 0, "getty.service"); |
cfeaa44a LP |
40 | check_p_d_u("getty", -ENXIO, NULL); |
41 | check_p_d_u("getty/waldo", -ENXIO, NULL); | |
d7bd3de0 | 42 | check_p_d_u("_cpu.service", 0, "cpu.service"); |
6c03089c LP |
43 | } |
44 | ||
45 | static void check_p_g_u(const char *path, int code, const char *result) { | |
46 | _cleanup_free_ char *unit = NULL; | |
d4fffc4b | 47 | int r; |
6c03089c | 48 | |
d4fffc4b ZJS |
49 | r = cg_path_get_unit(path, &unit); |
50 | printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code); | |
51 | assert_se(r == code); | |
6c03089c LP |
52 | assert_se(streq_ptr(unit, result)); |
53 | } | |
54 | ||
9444b1f2 LP |
55 | static void test_path_get_unit(void) { |
56 | check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service"); | |
d7bd3de0 LP |
57 | check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service"); |
58 | check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service"); | |
59 | check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service"); | |
9444b1f2 | 60 | check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service"); |
cfeaa44a LP |
61 | check_p_g_u("sadfdsafsda", -ENXIO, NULL); |
62 | check_p_g_u("/system.slice/getty####@tty6.service/xxx", -ENXIO, NULL); | |
9444b1f2 | 63 | check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service"); |
d7bd3de0 | 64 | check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service"); |
d4fffc4b | 65 | check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service"); |
cfeaa44a | 66 | check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL); |
9444b1f2 LP |
67 | } |
68 | ||
6c03089c LP |
69 | static void check_p_g_u_u(const char *path, int code, const char *result) { |
70 | _cleanup_free_ char *unit = NULL; | |
d4fffc4b | 71 | int r; |
6c03089c | 72 | |
d4fffc4b ZJS |
73 | r = cg_path_get_user_unit(path, &unit); |
74 | printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code); | |
75 | assert_se(r == code); | |
6c03089c LP |
76 | assert_se(streq_ptr(unit, result)); |
77 | } | |
78 | ||
9444b1f2 | 79 | static void test_path_get_user_unit(void) { |
374ec6ab LP |
80 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service"); |
81 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service"); | |
82 | check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service"); | |
83 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service"); | |
cfeaa44a | 84 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -ENXIO, NULL); |
d7bd3de0 LP |
85 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service"); |
86 | check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service"); | |
87 | check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service"); | |
cfeaa44a | 88 | check_p_g_u_u("/meh.service", -ENXIO, NULL); |
d7bd3de0 | 89 | check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service"); |
d4fffc4b | 90 | check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service"); |
32081481 | 91 | check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "foobar@pie.service"); |
cfeaa44a | 92 | check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL); |
7027ff61 | 93 | } |
6c03089c | 94 | |
9444b1f2 LP |
95 | static void check_p_g_s(const char *path, int code, const char *result) { |
96 | _cleanup_free_ char *s = NULL; | |
97 | ||
98 | assert_se(cg_path_get_session(path, &s) == code); | |
99 | assert_se(streq_ptr(s, result)); | |
96cde13a ZJS |
100 | } |
101 | ||
9444b1f2 | 102 | static void test_path_get_session(void) { |
374ec6ab LP |
103 | check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2"); |
104 | check_p_g_s("/session-3.scope", 0, "3"); | |
cfeaa44a LP |
105 | check_p_g_s("/session-.scope", -ENXIO, NULL); |
106 | check_p_g_s("", -ENXIO, NULL); | |
9444b1f2 | 107 | } |
7027ff61 | 108 | |
9444b1f2 LP |
109 | static void check_p_g_o_u(const char *path, int code, uid_t result) { |
110 | uid_t uid = 0; | |
7027ff61 | 111 | |
9444b1f2 LP |
112 | assert_se(cg_path_get_owner_uid(path, &uid) == code); |
113 | assert_se(uid == result); | |
114 | } | |
115 | ||
116 | static void test_path_get_owner_uid(void) { | |
374ec6ab LP |
117 | check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000); |
118 | check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006); | |
cfeaa44a | 119 | check_p_g_o_u("", -ENXIO, 0); |
9444b1f2 | 120 | } |
7027ff61 | 121 | |
8b0849e9 LP |
122 | static void check_p_g_slice(const char *path, int code, const char *result) { |
123 | _cleanup_free_ char *s = NULL; | |
124 | ||
125 | assert_se(cg_path_get_slice(path, &s) == code); | |
126 | assert_se(streq_ptr(s, result)); | |
127 | } | |
128 | ||
129 | static void test_path_get_slice(void) { | |
130 | check_p_g_slice("/user.slice", 0, "user.slice"); | |
e5af6e0e | 131 | check_p_g_slice("/foobar", 0, SPECIAL_ROOT_SLICE); |
8b0849e9 | 132 | check_p_g_slice("/user.slice/user-waldo.slice", 0, "user-waldo.slice"); |
e5af6e0e ZJS |
133 | check_p_g_slice("", 0, SPECIAL_ROOT_SLICE); |
134 | check_p_g_slice("foobar", 0, SPECIAL_ROOT_SLICE); | |
8b0849e9 LP |
135 | check_p_g_slice("foobar.slice", 0, "foobar.slice"); |
136 | check_p_g_slice("foo.slice/foo-bar.slice/waldo.service", 0, "foo-bar.slice"); | |
137 | } | |
138 | ||
329ac4bc LP |
139 | static void check_p_g_u_slice(const char *path, int code, const char *result) { |
140 | _cleanup_free_ char *s = NULL; | |
141 | ||
142 | assert_se(cg_path_get_user_slice(path, &s) == code); | |
143 | assert_se(streq_ptr(s, result)); | |
144 | } | |
145 | ||
146 | static void test_path_get_user_slice(void) { | |
147 | check_p_g_u_slice("/user.slice", -ENXIO, NULL); | |
148 | check_p_g_u_slice("/foobar", -ENXIO, NULL); | |
149 | check_p_g_u_slice("/user.slice/user-waldo.slice", -ENXIO, NULL); | |
150 | check_p_g_u_slice("", -ENXIO, NULL); | |
151 | check_p_g_u_slice("foobar", -ENXIO, NULL); | |
152 | check_p_g_u_slice("foobar.slice", -ENXIO, NULL); | |
153 | check_p_g_u_slice("foo.slice/foo-bar.slice/waldo.service", -ENXIO, NULL); | |
154 | ||
e5af6e0e ZJS |
155 | check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service", 0, SPECIAL_ROOT_SLICE); |
156 | check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/", 0, SPECIAL_ROOT_SLICE); | |
157 | check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service///", 0, SPECIAL_ROOT_SLICE); | |
158 | check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/waldo.service", 0, SPECIAL_ROOT_SLICE); | |
329ac4bc LP |
159 | check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/piep.slice/foo.service", 0, "piep.slice"); |
160 | check_p_g_u_slice("/foo.slice//foo-bar.slice/user@1000.service/piep.slice//piep-pap.slice//foo.service", 0, "piep-pap.slice"); | |
161 | } | |
162 | ||
9444b1f2 LP |
163 | static void test_get_paths(void) { |
164 | _cleanup_free_ char *a = NULL; | |
165 | ||
166 | assert_se(cg_get_root_path(&a) >= 0); | |
167 | log_info("Root = %s", a); | |
7027ff61 LP |
168 | } |
169 | ||
aff38e74 LP |
170 | static void test_proc(void) { |
171 | _cleanup_closedir_ DIR *d = NULL; | |
172 | struct dirent *de; | |
173 | int r; | |
174 | ||
175 | d = opendir("/proc"); | |
176 | assert_se(d); | |
177 | ||
178 | FOREACH_DIRENT(de, d, break) { | |
e9174f29 | 179 | _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL; |
aff38e74 | 180 | pid_t pid; |
fed1e721 | 181 | uid_t uid = UID_INVALID; |
aff38e74 | 182 | |
ec2ce0c5 | 183 | if (!IN_SET(de->d_type, DT_DIR, DT_UNKNOWN)) |
aff38e74 LP |
184 | continue; |
185 | ||
186 | r = parse_pid(de->d_name, &pid); | |
187 | if (r < 0) | |
188 | continue; | |
189 | ||
ae018d9b LP |
190 | if (is_kernel_thread(pid)) |
191 | continue; | |
192 | ||
aff38e74 | 193 | cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path); |
e9174f29 | 194 | cg_pid_get_path_shifted(pid, NULL, &path_shifted); |
ae018d9b | 195 | cg_pid_get_owner_uid(pid, &uid); |
aff38e74 LP |
196 | cg_pid_get_session(pid, &session); |
197 | cg_pid_get_unit(pid, &unit); | |
198 | cg_pid_get_user_unit(pid, &user_unit); | |
199 | cg_pid_get_machine_name(pid, &machine); | |
1021b21b | 200 | cg_pid_get_slice(pid, &slice); |
aff38e74 | 201 | |
de0671ee ZJS |
202 | printf(PID_FMT"\t%s\t%s\t"UID_FMT"\t%s\t%s\t%s\t%s\t%s\n", |
203 | pid, | |
aff38e74 | 204 | path, |
aff38e74 | 205 | path_shifted, |
de0671ee | 206 | uid, |
aff38e74 LP |
207 | session, |
208 | unit, | |
209 | user_unit, | |
1021b21b LP |
210 | machine, |
211 | slice); | |
aff38e74 LP |
212 | } |
213 | } | |
214 | ||
ae018d9b LP |
215 | static void test_escape_one(const char *s, const char *r) { |
216 | _cleanup_free_ char *b; | |
217 | ||
218 | b = cg_escape(s); | |
219 | assert_se(b); | |
220 | assert_se(streq(b, r)); | |
221 | ||
222 | assert_se(streq(cg_unescape(b), s)); | |
223 | } | |
224 | ||
225 | static void test_escape(void) { | |
226 | test_escape_one("foobar", "foobar"); | |
a0ab5665 | 227 | test_escape_one(".foobar", "_.foobar"); |
ae018d9b LP |
228 | test_escape_one("foobar.service", "foobar.service"); |
229 | test_escape_one("cgroup.service", "_cgroup.service"); | |
ae018d9b | 230 | test_escape_one("tasks", "_tasks"); |
e13bb5d2 KS |
231 | if (access("/sys/fs/cgroup/cpu", F_OK) == 0) |
232 | test_escape_one("cpu.service", "_cpu.service"); | |
ae018d9b | 233 | test_escape_one("_foobar", "__foobar"); |
a0ab5665 LP |
234 | test_escape_one("", "_"); |
235 | test_escape_one("_", "__"); | |
236 | test_escape_one(".", "_."); | |
ae018d9b LP |
237 | } |
238 | ||
78edb35a | 239 | static void test_controller_is_valid(void) { |
185a0874 DJL |
240 | assert_se(cg_controller_is_valid("foobar")); |
241 | assert_se(cg_controller_is_valid("foo_bar")); | |
242 | assert_se(cg_controller_is_valid("name=foo")); | |
243 | assert_se(!cg_controller_is_valid("")); | |
244 | assert_se(!cg_controller_is_valid("name=")); | |
245 | assert_se(!cg_controller_is_valid("=")); | |
246 | assert_se(!cg_controller_is_valid("cpu,cpuacct")); | |
247 | assert_se(!cg_controller_is_valid("_")); | |
248 | assert_se(!cg_controller_is_valid("_foobar")); | |
249 | assert_se(!cg_controller_is_valid("tatü")); | |
78edb35a LP |
250 | } |
251 | ||
a016b922 LP |
252 | static void test_slice_to_path_one(const char *unit, const char *path, int error) { |
253 | _cleanup_free_ char *ret = NULL; | |
0682ed5c ZJS |
254 | int r; |
255 | ||
256 | log_info("unit: %s", unit); | |
a016b922 | 257 | |
0682ed5c ZJS |
258 | r = cg_slice_to_path(unit, &ret); |
259 | log_info("actual: %s / %d", strnull(ret), r); | |
260 | log_info("expect: %s / %d", strnull(path), error); | |
261 | assert_se(r == error); | |
a016b922 LP |
262 | assert_se(streq_ptr(ret, path)); |
263 | } | |
264 | ||
265 | static void test_slice_to_path(void) { | |
a016b922 LP |
266 | test_slice_to_path_one("foobar.slice", "foobar.slice", 0); |
267 | test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0); | |
268 | test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL); | |
e5af6e0e | 269 | test_slice_to_path_one(SPECIAL_ROOT_SLICE, "", 0); |
c96cc582 LP |
270 | test_slice_to_path_one("--.slice", NULL, -EINVAL); |
271 | test_slice_to_path_one("-", NULL, -EINVAL); | |
a016b922 LP |
272 | test_slice_to_path_one("-foo-.slice", NULL, -EINVAL); |
273 | test_slice_to_path_one("-foo.slice", NULL, -EINVAL); | |
c96cc582 | 274 | test_slice_to_path_one("foo-.slice", NULL, -EINVAL); |
e66e5b61 | 275 | test_slice_to_path_one("foo--bar.slice", NULL, -EINVAL); |
c96cc582 | 276 | test_slice_to_path_one("foo.slice/foo--bar.slice", NULL, -EINVAL); |
a016b922 LP |
277 | test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0); |
278 | test_slice_to_path_one("a-b-c-d-e.slice", "a.slice/a-b.slice/a-b-c.slice/a-b-c-d.slice/a-b-c-d-e.slice", 0); | |
0682ed5c ZJS |
279 | |
280 | test_slice_to_path_one("foobar@.slice", NULL, -EINVAL); | |
281 | test_slice_to_path_one("foobar@waldo.slice", NULL, -EINVAL); | |
282 | test_slice_to_path_one("foobar@waldo.service", NULL, -EINVAL); | |
283 | test_slice_to_path_one("-foo@-.slice", NULL, -EINVAL); | |
284 | test_slice_to_path_one("-foo@.slice", NULL, -EINVAL); | |
285 | test_slice_to_path_one("foo@-.slice", NULL, -EINVAL); | |
286 | test_slice_to_path_one("foo@@bar.slice", NULL, -EINVAL); | |
287 | test_slice_to_path_one("foo.slice/foo@@bar.slice", NULL, -EINVAL); | |
a016b922 LP |
288 | } |
289 | ||
751bc6ac LP |
290 | static void test_shift_path_one(const char *raw, const char *root, const char *shifted) { |
291 | const char *s = NULL; | |
292 | ||
293 | assert_se(cg_shift_path(raw, root, &s) >= 0); | |
294 | assert_se(streq(s, shifted)); | |
295 | } | |
296 | ||
297 | static void test_shift_path(void) { | |
298 | ||
299 | test_shift_path_one("/foobar/waldo", "/", "/foobar/waldo"); | |
300 | test_shift_path_one("/foobar/waldo", "", "/foobar/waldo"); | |
301 | test_shift_path_one("/foobar/waldo", "/foobar", "/waldo"); | |
302 | test_shift_path_one("/foobar/waldo", "/fuckfuck", "/foobar/waldo"); | |
303 | } | |
304 | ||
5f4c5fef LP |
305 | static void test_mask_supported(void) { |
306 | ||
307 | CGroupMask m; | |
308 | CGroupController c; | |
309 | ||
310 | assert_se(cg_mask_supported(&m) >= 0); | |
311 | ||
312 | for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++) | |
313 | printf("'%s' is supported: %s\n", cgroup_controller_to_string(c), yes_no(m & CGROUP_CONTROLLER_TO_MASK(c))); | |
314 | } | |
315 | ||
f0bef277 EV |
316 | static void test_is_cgroup_fs(void) { |
317 | struct statfs sfs; | |
318 | assert_se(statfs("/sys/fs/cgroup", &sfs) == 0); | |
319 | if (is_temporary_fs(&sfs)) | |
320 | assert_se(statfs("/sys/fs/cgroup/systemd", &sfs) == 0); | |
321 | assert_se(is_cgroup_fs(&sfs)); | |
322 | } | |
323 | ||
324 | static void test_fd_is_cgroup_fs(void) { | |
325 | int fd; | |
326 | ||
327 | fd = open("/sys/fs/cgroup", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW); | |
328 | assert_se(fd >= 0); | |
329 | if (fd_is_temporary_fs(fd)) { | |
330 | fd = safe_close(fd); | |
331 | fd = open("/sys/fs/cgroup/systemd", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW); | |
332 | assert_se(fd >= 0); | |
333 | } | |
334 | assert_se(fd_is_cgroup_fs(fd)); | |
335 | fd = safe_close(fd); | |
336 | } | |
337 | ||
0a05dcc0 ZJS |
338 | static void test_is_wanted_print(bool header) { |
339 | _cleanup_free_ char *cmdline = NULL; | |
340 | ||
341 | log_info("-- %s --", __func__); | |
342 | assert_se(proc_cmdline(&cmdline) >= 0); | |
343 | log_info("cmdline: %s", cmdline); | |
344 | if (header) { | |
345 | ||
346 | log_info(_CGROUP_HIEARCHY_); | |
347 | (void) system("findmnt -n /sys/fs/cgroup"); | |
348 | } | |
349 | ||
350 | log_info("is_unified_wanted() → %s", yes_no(cg_is_unified_wanted())); | |
351 | log_info("is_hybrid_wanted() → %s", yes_no(cg_is_hybrid_wanted())); | |
352 | log_info("is_legacy_wanted() → %s", yes_no(cg_is_legacy_wanted())); | |
353 | log_info(" "); | |
354 | } | |
355 | ||
356 | static void test_is_wanted(void) { | |
357 | assert_se(setenv("SYSTEMD_PROC_CMDLINE", | |
358 | "systemd.unified_cgroup_hierarchy", 1) >= 0); | |
359 | test_is_wanted_print(false); | |
360 | ||
361 | assert_se(setenv("SYSTEMD_PROC_CMDLINE", | |
362 | "systemd.unified_cgroup_hierarchy=0", 1) >= 0); | |
363 | test_is_wanted_print(false); | |
364 | ||
365 | assert_se(setenv("SYSTEMD_PROC_CMDLINE", | |
366 | "systemd.unified_cgroup_hierarchy=0 " | |
367 | "systemd.legacy_systemd_cgroup_controller", 1) >= 0); | |
368 | test_is_wanted_print(false); | |
369 | ||
370 | assert_se(setenv("SYSTEMD_PROC_CMDLINE", | |
371 | "systemd.unified_cgroup_hierarchy=0 " | |
372 | "systemd.legacy_systemd_cgroup_controller=0", 1) >= 0); | |
373 | test_is_wanted_print(false); | |
374 | } | |
375 | ||
073e8f09 | 376 | static void test_cg_tests(void) { |
05d69e02 | 377 | int all, hybrid, systemd, r; |
073e8f09 | 378 | |
05d69e02 ZJS |
379 | r = cg_unified_flush(); |
380 | if (r == -ENOMEDIUM) { | |
381 | log_notice_errno(r, "Skipping cg hierarchy tests: %m"); | |
382 | return; | |
383 | } | |
384 | assert_se(r == 0); | |
073e8f09 ZJS |
385 | |
386 | all = cg_all_unified(); | |
387 | assert_se(IN_SET(all, 0, 1)); | |
388 | ||
389 | hybrid = cg_hybrid_unified(); | |
390 | assert_se(IN_SET(hybrid, 0, 1)); | |
391 | ||
392 | systemd = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER); | |
393 | assert_se(IN_SET(systemd, 0, 1)); | |
394 | ||
395 | if (all) { | |
396 | assert_se(systemd); | |
397 | assert_se(!hybrid); | |
398 | ||
399 | } else if (hybrid) { | |
400 | assert_se(systemd); | |
401 | assert_se(!all); | |
402 | ||
403 | } else | |
404 | assert_se(!systemd); | |
405 | } | |
406 | ||
9177fa9f ZJS |
407 | static void test_cg_get_keyed_attribute(void) { |
408 | _cleanup_free_ char *val = NULL; | |
409 | char *vals3[3] = {}, *vals3a[3] = {}; | |
18ce247c | 410 | int i, r; |
9177fa9f | 411 | |
18ce247c ZJS |
412 | r = cg_get_keyed_attribute("cpu", "/init.scope", "no_such_file", STRV_MAKE("no_such_attr"), &val); |
413 | if (r == -ENOMEDIUM) { | |
414 | log_info_errno(r, "Skipping most of %s, /sys/fs/cgroup not accessible: %m", __func__); | |
415 | return; | |
416 | } | |
417 | ||
418 | assert_se(r == -ENOENT); | |
9177fa9f ZJS |
419 | assert_se(val == NULL); |
420 | ||
421 | if (access("/sys/fs/cgroup/init.scope/cpu.stat", R_OK) < 0) { | |
422 | log_info_errno(errno, "Skipping most of %s, /init.scope/cpu.stat not accessible: %m", __func__); | |
423 | return; | |
424 | } | |
425 | ||
426 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val) == -ENXIO); | |
427 | assert_se(val == NULL); | |
428 | ||
429 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val) == 0); | |
430 | log_info("cpu /init.scope cpu.stat [usage_usec] → \"%s\"", val); | |
431 | ||
432 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3) == -ENXIO); | |
433 | ||
434 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "usage_usec"), vals3) == -ENXIO); | |
435 | ||
436 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", | |
437 | STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3) == 0); | |
438 | log_info("cpu /init.scope cpu.stat [usage_usec user_usec system_usec] → \"%s\", \"%s\", \"%s\"", | |
439 | vals3[0], vals3[1], vals3[2]); | |
440 | ||
441 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", | |
442 | STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a) == 0); | |
443 | log_info("cpu /init.scope cpu.stat [system_usec user_usec usage_usec] → \"%s\", \"%s\", \"%s\"", | |
444 | vals3a[0], vals3a[1], vals3a[2]); | |
445 | ||
446 | for (i = 0; i < 3; i++) { | |
447 | free(vals3[i]); | |
448 | free(vals3a[i]); | |
449 | } | |
450 | } | |
451 | ||
96cde13a | 452 | int main(void) { |
0a05dcc0 ZJS |
453 | log_set_max_level(LOG_DEBUG); |
454 | log_parse_environment(); | |
455 | log_open(); | |
456 | ||
7027ff61 | 457 | test_path_decode_unit(); |
6c03089c | 458 | test_path_get_unit(); |
7027ff61 | 459 | test_path_get_user_unit(); |
9444b1f2 LP |
460 | test_path_get_session(); |
461 | test_path_get_owner_uid(); | |
8b0849e9 | 462 | test_path_get_slice(); |
329ac4bc | 463 | test_path_get_user_slice(); |
143bfdaf | 464 | TEST_REQ_RUNNING_SYSTEMD(test_get_paths()); |
aff38e74 | 465 | test_proc(); |
143bfdaf | 466 | TEST_REQ_RUNNING_SYSTEMD(test_escape()); |
78edb35a | 467 | test_controller_is_valid(); |
a016b922 | 468 | test_slice_to_path(); |
751bc6ac | 469 | test_shift_path(); |
b3a7ba89 | 470 | TEST_REQ_RUNNING_SYSTEMD(test_mask_supported()); |
f0bef277 EV |
471 | TEST_REQ_RUNNING_SYSTEMD(test_is_cgroup_fs()); |
472 | TEST_REQ_RUNNING_SYSTEMD(test_fd_is_cgroup_fs()); | |
0a05dcc0 ZJS |
473 | test_is_wanted_print(true); |
474 | test_is_wanted_print(false); /* run twice to test caching */ | |
475 | test_is_wanted(); | |
073e8f09 | 476 | test_cg_tests(); |
9177fa9f | 477 | test_cg_get_keyed_attribute(); |
96cde13a ZJS |
478 | |
479 | return 0; | |
480 | } |