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