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