1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
5 #include "cgroup-util.h"
6 #include "dirent-util.h"
8 #include "format-util.h"
9 #include "parse-util.h"
10 #include "proc-cmdline.h"
11 #include "process-util.h"
13 #include "stat-util.h"
14 #include "string-util.h"
16 #include "test-helper.h"
18 #include "user-util.h"
21 static void check_p_d_u(const char *path
, int code
, const char *result
) {
22 _cleanup_free_
char *unit
= NULL
;
25 r
= cg_path_decode_unit(path
, &unit
);
26 printf("%s: %s → %s %d expected %s %d\n", __func__
, path
, unit
, r
, result
, code
);
28 assert_se(streq_ptr(unit
, result
));
31 static void test_path_decode_unit(void) {
32 check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
33 check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
34 check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service");
35 check_p_d_u("getty@.service/", -ENXIO
, NULL
);
36 check_p_d_u("getty@.service", -ENXIO
, NULL
);
37 check_p_d_u("getty.service", 0, "getty.service");
38 check_p_d_u("getty", -ENXIO
, NULL
);
39 check_p_d_u("getty/waldo", -ENXIO
, NULL
);
40 check_p_d_u("_cpu.service", 0, "cpu.service");
43 static void check_p_g_u(const char *path
, int code
, const char *result
) {
44 _cleanup_free_
char *unit
= NULL
;
47 r
= cg_path_get_unit(path
, &unit
);
48 printf("%s: %s → %s %d expected %s %d\n", __func__
, path
, unit
, r
, result
, code
);
50 assert_se(streq_ptr(unit
, result
));
53 static void test_path_get_unit(void) {
54 check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
55 check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
56 check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
57 check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
58 check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
59 check_p_g_u("sadfdsafsda", -ENXIO
, NULL
);
60 check_p_g_u("/system.slice/getty####@tty6.service/xxx", -ENXIO
, NULL
);
61 check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
62 check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
63 check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service");
64 check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO
, NULL
);
67 static void check_p_g_u_u(const char *path
, int code
, const char *result
) {
68 _cleanup_free_
char *unit
= NULL
;
71 r
= cg_path_get_user_unit(path
, &unit
);
72 printf("%s: %s → %s %d expected %s %d\n", __func__
, path
, unit
, r
, result
, code
);
74 assert_se(streq_ptr(unit
, result
));
77 static void test_path_get_user_unit(void) {
78 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service");
79 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service");
80 check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
81 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
82 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -ENXIO
, NULL
);
83 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
84 check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
85 check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
86 check_p_g_u_u("/meh.service", -ENXIO
, NULL
);
87 check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
88 check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service");
89 check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "foobar@pie.service");
90 check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO
, NULL
);
93 static void check_p_g_s(const char *path
, int code
, const char *result
) {
94 _cleanup_free_
char *s
= NULL
;
96 assert_se(cg_path_get_session(path
, &s
) == code
);
97 assert_se(streq_ptr(s
, result
));
100 static void test_path_get_session(void) {
101 check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2");
102 check_p_g_s("/session-3.scope", 0, "3");
103 check_p_g_s("/session-.scope", -ENXIO
, NULL
);
104 check_p_g_s("", -ENXIO
, NULL
);
107 static void check_p_g_o_u(const char *path
, int code
, uid_t result
) {
110 assert_se(cg_path_get_owner_uid(path
, &uid
) == code
);
111 assert_se(uid
== result
);
114 static void test_path_get_owner_uid(void) {
115 check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000);
116 check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006);
117 check_p_g_o_u("", -ENXIO
, 0);
120 static void check_p_g_slice(const char *path
, int code
, const char *result
) {
121 _cleanup_free_
char *s
= NULL
;
123 assert_se(cg_path_get_slice(path
, &s
) == code
);
124 assert_se(streq_ptr(s
, result
));
127 static void test_path_get_slice(void) {
128 check_p_g_slice("/user.slice", 0, "user.slice");
129 check_p_g_slice("/foobar", 0, SPECIAL_ROOT_SLICE
);
130 check_p_g_slice("/user.slice/user-waldo.slice", 0, "user-waldo.slice");
131 check_p_g_slice("", 0, SPECIAL_ROOT_SLICE
);
132 check_p_g_slice("foobar", 0, SPECIAL_ROOT_SLICE
);
133 check_p_g_slice("foobar.slice", 0, "foobar.slice");
134 check_p_g_slice("foo.slice/foo-bar.slice/waldo.service", 0, "foo-bar.slice");
137 static void check_p_g_u_slice(const char *path
, int code
, const char *result
) {
138 _cleanup_free_
char *s
= NULL
;
140 assert_se(cg_path_get_user_slice(path
, &s
) == code
);
141 assert_se(streq_ptr(s
, result
));
144 static void test_path_get_user_slice(void) {
145 check_p_g_u_slice("/user.slice", -ENXIO
, NULL
);
146 check_p_g_u_slice("/foobar", -ENXIO
, NULL
);
147 check_p_g_u_slice("/user.slice/user-waldo.slice", -ENXIO
, NULL
);
148 check_p_g_u_slice("", -ENXIO
, NULL
);
149 check_p_g_u_slice("foobar", -ENXIO
, NULL
);
150 check_p_g_u_slice("foobar.slice", -ENXIO
, NULL
);
151 check_p_g_u_slice("foo.slice/foo-bar.slice/waldo.service", -ENXIO
, NULL
);
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///", 0, SPECIAL_ROOT_SLICE
);
156 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/waldo.service", 0, SPECIAL_ROOT_SLICE
);
157 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/piep.slice/foo.service", 0, "piep.slice");
158 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 static void test_get_paths(void) {
162 _cleanup_free_
char *a
= NULL
;
164 assert_se(cg_get_root_path(&a
) >= 0);
165 log_info("Root = %s", a
);
168 static void test_proc(void) {
169 _cleanup_closedir_
DIR *d
= NULL
;
173 d
= opendir("/proc");
176 FOREACH_DIRENT(de
, d
, break) {
177 _cleanup_free_
char *path
= NULL
, *path_shifted
= NULL
, *session
= NULL
, *unit
= NULL
, *user_unit
= NULL
, *machine
= NULL
, *slice
= NULL
;
179 uid_t uid
= UID_INVALID
;
181 if (!IN_SET(de
->d_type
, DT_DIR
, DT_UNKNOWN
))
184 r
= parse_pid(de
->d_name
, &pid
);
188 if (is_kernel_thread(pid
))
191 cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &path
);
192 cg_pid_get_path_shifted(pid
, NULL
, &path_shifted
);
193 cg_pid_get_owner_uid(pid
, &uid
);
194 cg_pid_get_session(pid
, &session
);
195 cg_pid_get_unit(pid
, &unit
);
196 cg_pid_get_user_unit(pid
, &user_unit
);
197 cg_pid_get_machine_name(pid
, &machine
);
198 cg_pid_get_slice(pid
, &slice
);
200 printf(PID_FMT
"\t%s\t%s\t"UID_FMT
"\t%s\t%s\t%s\t%s\t%s\n",
213 static void test_escape_one(const char *s
, const char *r
) {
214 _cleanup_free_
char *b
;
218 assert_se(streq(b
, r
));
220 assert_se(streq(cg_unescape(b
), s
));
223 static void test_escape(void) {
224 test_escape_one("foobar", "foobar");
225 test_escape_one(".foobar", "_.foobar");
226 test_escape_one("foobar.service", "foobar.service");
227 test_escape_one("cgroup.service", "_cgroup.service");
228 test_escape_one("tasks", "_tasks");
229 if (access("/sys/fs/cgroup/cpu", F_OK
) == 0)
230 test_escape_one("cpu.service", "_cpu.service");
231 test_escape_one("_foobar", "__foobar");
232 test_escape_one("", "_");
233 test_escape_one("_", "__");
234 test_escape_one(".", "_.");
237 static void test_controller_is_valid(void) {
238 assert_se(cg_controller_is_valid("foobar"));
239 assert_se(cg_controller_is_valid("foo_bar"));
240 assert_se(cg_controller_is_valid("name=foo"));
241 assert_se(!cg_controller_is_valid(""));
242 assert_se(!cg_controller_is_valid("name="));
243 assert_se(!cg_controller_is_valid("="));
244 assert_se(!cg_controller_is_valid("cpu,cpuacct"));
245 assert_se(!cg_controller_is_valid("_"));
246 assert_se(!cg_controller_is_valid("_foobar"));
247 assert_se(!cg_controller_is_valid("tatü"));
250 static void test_slice_to_path_one(const char *unit
, const char *path
, int error
) {
251 _cleanup_free_
char *ret
= NULL
;
254 log_info("unit: %s", unit
);
256 r
= cg_slice_to_path(unit
, &ret
);
257 log_info("actual: %s / %d", strnull(ret
), r
);
258 log_info("expect: %s / %d", strnull(path
), error
);
259 assert_se(r
== error
);
260 assert_se(streq_ptr(ret
, path
));
263 static void test_slice_to_path(void) {
264 test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
265 test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
266 test_slice_to_path_one("foobar-waldo.service", NULL
, -EINVAL
);
267 test_slice_to_path_one(SPECIAL_ROOT_SLICE
, "", 0);
268 test_slice_to_path_one("--.slice", NULL
, -EINVAL
);
269 test_slice_to_path_one("-", 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-.slice", NULL
, -EINVAL
);
273 test_slice_to_path_one("foo--bar.slice", NULL
, -EINVAL
);
274 test_slice_to_path_one("foo.slice/foo--bar.slice", NULL
, -EINVAL
);
275 test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
276 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);
278 test_slice_to_path_one("foobar@.slice", NULL
, -EINVAL
);
279 test_slice_to_path_one("foobar@waldo.slice", NULL
, -EINVAL
);
280 test_slice_to_path_one("foobar@waldo.service", 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@-.slice", NULL
, -EINVAL
);
284 test_slice_to_path_one("foo@@bar.slice", NULL
, -EINVAL
);
285 test_slice_to_path_one("foo.slice/foo@@bar.slice", NULL
, -EINVAL
);
288 static void test_shift_path_one(const char *raw
, const char *root
, const char *shifted
) {
289 const char *s
= NULL
;
291 assert_se(cg_shift_path(raw
, root
, &s
) >= 0);
292 assert_se(streq(s
, shifted
));
295 static void test_shift_path(void) {
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", "/foobar", "/waldo");
300 test_shift_path_one("/foobar/waldo", "/hogehoge", "/foobar/waldo");
303 static void test_mask_supported(void) {
308 assert_se(cg_mask_supported(&m
) >= 0);
310 for (c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++)
311 printf("'%s' is supported: %s\n", cgroup_controller_to_string(c
), yes_no(m
& CGROUP_CONTROLLER_TO_MASK(c
)));
314 static void test_is_cgroup_fs(void) {
316 assert_se(statfs("/sys/fs/cgroup", &sfs
) == 0);
317 if (is_temporary_fs(&sfs
))
318 assert_se(statfs("/sys/fs/cgroup/systemd", &sfs
) == 0);
319 assert_se(is_cgroup_fs(&sfs
));
322 static void test_fd_is_cgroup_fs(void) {
325 fd
= open("/sys/fs/cgroup", O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
327 if (fd_is_temporary_fs(fd
)) {
329 fd
= open("/sys/fs/cgroup/systemd", O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
332 assert_se(fd_is_cgroup_fs(fd
));
336 static void test_is_wanted_print(bool header
) {
337 _cleanup_free_
char *cmdline
= NULL
;
339 log_info("-- %s --", __func__
);
340 assert_se(proc_cmdline(&cmdline
) >= 0);
341 log_info("cmdline: %s", cmdline
);
344 log_info(_CGROUP_HIEARCHY_
);
345 (void) system("findmnt -n /sys/fs/cgroup");
348 log_info("is_unified_wanted() → %s", yes_no(cg_is_unified_wanted()));
349 log_info("is_hybrid_wanted() → %s", yes_no(cg_is_hybrid_wanted()));
350 log_info("is_legacy_wanted() → %s", yes_no(cg_is_legacy_wanted()));
354 static void test_is_wanted(void) {
355 assert_se(setenv("SYSTEMD_PROC_CMDLINE",
356 "systemd.unified_cgroup_hierarchy", 1) >= 0);
357 test_is_wanted_print(false);
359 assert_se(setenv("SYSTEMD_PROC_CMDLINE",
360 "systemd.unified_cgroup_hierarchy=0", 1) >= 0);
361 test_is_wanted_print(false);
363 assert_se(setenv("SYSTEMD_PROC_CMDLINE",
364 "systemd.unified_cgroup_hierarchy=0 "
365 "systemd.legacy_systemd_cgroup_controller", 1) >= 0);
366 test_is_wanted_print(false);
368 assert_se(setenv("SYSTEMD_PROC_CMDLINE",
369 "systemd.unified_cgroup_hierarchy=0 "
370 "systemd.legacy_systemd_cgroup_controller=0", 1) >= 0);
371 test_is_wanted_print(false);
373 /* cgroup_no_v1=all implies unified cgroup hierarchy, unless otherwise
374 * explicitly specified. */
375 assert_se(setenv("SYSTEMD_PROC_CMDLINE",
376 "cgroup_no_v1=all", 1) >= 0);
377 test_is_wanted_print(false);
379 assert_se(setenv("SYSTEMD_PROC_CMDLINE",
381 "systemd.unified_cgroup_hierarchy=0", 1) >= 0);
382 test_is_wanted_print(false);
385 static void test_cg_tests(void) {
386 int all
, hybrid
, systemd
, r
;
388 r
= cg_unified_flush();
389 if (r
== -ENOMEDIUM
) {
390 log_notice_errno(r
, "Skipping cg hierarchy tests: %m");
395 all
= cg_all_unified();
396 assert_se(IN_SET(all
, 0, 1));
398 hybrid
= cg_hybrid_unified();
399 assert_se(IN_SET(hybrid
, 0, 1));
401 systemd
= cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER
);
402 assert_se(IN_SET(systemd
, 0, 1));
416 static void test_cg_get_keyed_attribute(void) {
417 _cleanup_free_
char *val
= NULL
;
418 char *vals3
[3] = {}, *vals3a
[3] = {};
421 r
= cg_get_keyed_attribute("cpu", "/init.scope", "no_such_file", STRV_MAKE("no_such_attr"), &val
);
422 if (r
== -ENOMEDIUM
) {
423 log_info_errno(r
, "Skipping most of %s, /sys/fs/cgroup not accessible: %m", __func__
);
427 assert_se(r
== -ENOENT
);
428 assert_se(val
== NULL
);
430 if (access("/sys/fs/cgroup/init.scope/cpu.stat", R_OK
) < 0) {
431 log_info_errno(errno
, "Skipping most of %s, /init.scope/cpu.stat not accessible: %m", __func__
);
435 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val
) == -ENXIO
);
436 assert_se(val
== NULL
);
438 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val
) == 0);
439 log_info("cpu /init.scope cpu.stat [usage_usec] → \"%s\"", val
);
441 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3
) == -ENXIO
);
443 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "usage_usec"), vals3
) == -ENXIO
);
445 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat",
446 STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3
) == 0);
447 log_info("cpu /init.scope cpu.stat [usage_usec user_usec system_usec] → \"%s\", \"%s\", \"%s\"",
448 vals3
[0], vals3
[1], vals3
[2]);
450 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat",
451 STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a
) == 0);
452 log_info("cpu /init.scope cpu.stat [system_usec user_usec usage_usec] → \"%s\", \"%s\", \"%s\"",
453 vals3a
[0], vals3a
[1], vals3a
[2]);
455 for (i
= 0; i
< 3; i
++) {
462 test_setup_logging(LOG_DEBUG
);
464 test_path_decode_unit();
465 test_path_get_unit();
466 test_path_get_user_unit();
467 test_path_get_session();
468 test_path_get_owner_uid();
469 test_path_get_slice();
470 test_path_get_user_slice();
471 TEST_REQ_RUNNING_SYSTEMD(test_get_paths());
473 TEST_REQ_RUNNING_SYSTEMD(test_escape());
474 test_controller_is_valid();
475 test_slice_to_path();
477 TEST_REQ_RUNNING_SYSTEMD(test_mask_supported());
478 TEST_REQ_RUNNING_SYSTEMD(test_is_cgroup_fs());
479 TEST_REQ_RUNNING_SYSTEMD(test_fd_is_cgroup_fs());
480 test_is_wanted_print(true);
481 test_is_wanted_print(false); /* run twice to test caching */
484 test_cg_get_keyed_attribute();