1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
4 #include "cgroup-util.h"
5 #include "dirent-util.h"
6 #include "errno-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"
17 #include "user-util.h"
19 static void check_p_d_u(const char *path
, int code
, const char *result
) {
20 _cleanup_free_
char *unit
= NULL
;
23 r
= cg_path_decode_unit(path
, &unit
);
24 printf("%s: %s → %s %d expected %s %d\n", __func__
, path
, unit
, r
, strnull(result
), code
);
26 ASSERT_STREQ(unit
, result
);
29 TEST(path_decode_unit
) {
30 check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
31 check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
32 check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service");
33 check_p_d_u("getty@.service/", -ENXIO
, NULL
);
34 check_p_d_u("getty@.service", -ENXIO
, NULL
);
35 check_p_d_u("getty.service", 0, "getty.service");
36 check_p_d_u("getty", -ENXIO
, NULL
);
37 check_p_d_u("getty/waldo", -ENXIO
, NULL
);
38 check_p_d_u("_cpu.service", 0, "cpu.service");
41 static void check_p_g_u(const char *path
, int code
, const char *result
) {
42 _cleanup_free_
char *unit
= NULL
;
45 r
= cg_path_get_unit(path
, &unit
);
46 printf("%s: %s → %s %d expected %s %d\n", __func__
, path
, unit
, r
, strnull(result
), code
);
48 ASSERT_STREQ(unit
, result
);
52 check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
53 check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
54 check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
55 check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
56 check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
57 check_p_g_u("sadfdsafsda", -ENXIO
, NULL
);
58 check_p_g_u("/system.slice/getty####@tty6.service/xxx", -ENXIO
, NULL
);
59 check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
60 check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
61 check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service");
62 check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO
, NULL
);
65 static void check_p_g_u_p(const char *path
, int code
, const char *result
) {
66 _cleanup_free_
char *unit_path
= NULL
;
69 r
= cg_path_get_unit_path(path
, &unit_path
);
70 printf("%s: %s → %s %d expected %s %d\n", __func__
, path
, unit_path
, r
, strnull(result
), code
);
72 ASSERT_STREQ(unit_path
, result
);
75 TEST(path_get_unit_path
) {
76 check_p_g_u_p("/system.slice/foobar.service/sdfdsaf", 0, "/system.slice/foobar.service");
77 check_p_g_u_p("/system.slice/getty@tty5.service", 0, "/system.slice/getty@tty5.service");
78 check_p_g_u_p("/system.slice/getty@tty5.service/aaa/bbb", 0, "/system.slice/getty@tty5.service");
79 check_p_g_u_p("/system.slice/getty@tty5.service/", 0, "/system.slice/getty@tty5.service");
80 check_p_g_u_p("/system.slice/getty@tty6.service/tty5", 0, "/system.slice/getty@tty6.service");
81 check_p_g_u_p("sadfdsafsda", -ENXIO
, NULL
);
82 check_p_g_u_p("/system.slice/getty####@tty6.service/xxx", -ENXIO
, NULL
);
83 check_p_g_u_p("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "/system.slice/system-waldo.slice/foobar.service");
84 check_p_g_u_p("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "/system.slice/system-waldo.slice/_cpu.service");
85 check_p_g_u_p("/system.slice/system-waldo.slice/_cpu.service", 0, "/system.slice/system-waldo.slice/_cpu.service");
86 check_p_g_u_p("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "/user.slice/user-1000.slice/user@1000.service");
87 check_p_g_u_p("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO
, NULL
);
88 check_p_g_u_p("/user.slice/_user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "/user.slice/_user-1000.slice/user@1000.service");
89 check_p_g_u_p("/_session-2.scope/_foobar@pie.service/pa/po", 0, "/_session-2.scope");
92 static void check_p_g_u_u(const char *path
, int code
, const char *result
) {
93 _cleanup_free_
char *unit
= NULL
;
96 r
= cg_path_get_user_unit(path
, &unit
);
97 printf("%s: %s → %s %d expected %s %d\n", __func__
, path
, unit
, r
, strnull(result
), code
);
99 ASSERT_STREQ(unit
, result
);
102 TEST(path_get_user_unit
) {
103 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service");
104 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service");
105 check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
106 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
107 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -ENXIO
, NULL
);
108 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
109 check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
110 check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
111 check_p_g_u_u("/meh.service", -ENXIO
, NULL
);
112 check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
113 check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service");
114 check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "foobar@pie.service");
115 check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO
, NULL
);
118 static void check_p_g_s(const char *path
, int code
, const char *result
) {
119 _cleanup_free_
char *s
= NULL
;
121 assert_se(cg_path_get_session(path
, &s
) == code
);
122 ASSERT_STREQ(s
, result
);
125 TEST(path_get_session
) {
126 check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2");
127 check_p_g_s("/session-3.scope", 0, "3");
128 check_p_g_s("/session-.scope", -ENXIO
, NULL
);
129 check_p_g_s("", -ENXIO
, NULL
);
132 static void check_p_g_o_u(const char *path
, int code
, uid_t result
) {
135 assert_se(cg_path_get_owner_uid(path
, &uid
) == code
);
136 assert_se(uid
== result
);
139 TEST(path_get_owner_uid
) {
140 check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000);
141 check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006);
142 check_p_g_o_u("", -ENXIO
, 0);
145 static void check_p_g_slice(const char *path
, int code
, const char *result
) {
146 _cleanup_free_
char *s
= NULL
;
148 assert_se(cg_path_get_slice(path
, &s
) == code
);
149 ASSERT_STREQ(s
, result
);
152 TEST(path_get_slice
) {
153 check_p_g_slice("/user.slice", 0, "user.slice");
154 check_p_g_slice("/foobar", 0, SPECIAL_ROOT_SLICE
);
155 check_p_g_slice("/user.slice/user-waldo.slice", 0, "user-waldo.slice");
156 check_p_g_slice("", 0, SPECIAL_ROOT_SLICE
);
157 check_p_g_slice("foobar", 0, SPECIAL_ROOT_SLICE
);
158 check_p_g_slice("foobar.slice", 0, "foobar.slice");
159 check_p_g_slice("foo.slice/foo-bar.slice/waldo.service", 0, "foo-bar.slice");
162 static void check_p_g_u_slice(const char *path
, int code
, const char *result
) {
163 _cleanup_free_
char *s
= NULL
;
165 assert_se(cg_path_get_user_slice(path
, &s
) == code
);
166 ASSERT_STREQ(s
, result
);
169 TEST(path_get_user_slice
) {
170 check_p_g_u_slice("/user.slice", -ENXIO
, NULL
);
171 check_p_g_u_slice("/foobar", -ENXIO
, NULL
);
172 check_p_g_u_slice("/user.slice/user-waldo.slice", -ENXIO
, NULL
);
173 check_p_g_u_slice("", -ENXIO
, NULL
);
174 check_p_g_u_slice("foobar", -ENXIO
, NULL
);
175 check_p_g_u_slice("foobar.slice", -ENXIO
, NULL
);
176 check_p_g_u_slice("foo.slice/foo-bar.slice/waldo.service", -ENXIO
, NULL
);
178 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service", 0, SPECIAL_ROOT_SLICE
);
179 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/", 0, SPECIAL_ROOT_SLICE
);
180 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service///", 0, SPECIAL_ROOT_SLICE
);
181 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/waldo.service", 0, SPECIAL_ROOT_SLICE
);
182 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/piep.slice/foo.service", 0, "piep.slice");
183 check_p_g_u_slice("/foo.slice//foo-bar.slice/user@1000.service/piep.slice//piep-pap.slice//foo.service", 0, "piep-pap.slice");
186 TEST(get_paths
, .sd_booted
= true) {
187 _cleanup_free_
char *a
= NULL
;
189 assert_se(cg_get_root_path(&a
) >= 0);
190 log_info("Root = %s", a
);
194 _cleanup_closedir_
DIR *d
= NULL
;
197 ASSERT_OK(proc_dir_open(&d
));
200 _cleanup_free_
char *path
= NULL
, *path_shifted
= NULL
, *session
= NULL
, *unit
= NULL
, *user_unit
= NULL
, *machine
= NULL
, *slice
= NULL
;
201 _cleanup_(pidref_done
) PidRef pid
= PIDREF_NULL
;
202 uid_t uid
= UID_INVALID
;
204 r
= proc_dir_read_pidref(d
, &pid
);
210 if (pidref_is_kernel_thread(&pid
) != 0)
213 cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
.pid
, &path
);
214 cg_pid_get_path_shifted(pid
.pid
, NULL
, &path_shifted
);
215 cg_pid_get_owner_uid(pid
.pid
, &uid
);
216 cg_pid_get_session(pid
.pid
, &session
);
217 cg_pid_get_unit(pid
.pid
, &unit
);
218 cg_pid_get_user_unit(pid
.pid
, &user_unit
);
219 cg_pid_get_machine_name(pid
.pid
, &machine
);
220 cg_pid_get_slice(pid
.pid
, &slice
);
222 printf(PID_FMT
"\t%s\t%s\t"UID_FMT
"\t%s\t%s\t%s\t%s\t%s\n",
235 static void test_escape_one(const char *s
, const char *expected
) {
236 _cleanup_free_
char *b
= NULL
;
241 ASSERT_OK(cg_escape(s
, &b
));
242 ASSERT_STREQ(b
, expected
);
244 ASSERT_STREQ(cg_unescape(b
), s
);
246 assert_se(filename_is_valid(b
));
247 assert_se(!cg_needs_escape(s
) || b
[0] == '_');
250 TEST(escape
, .sd_booted
= true) {
251 test_escape_one("foobar", "foobar");
252 test_escape_one(".foobar", "_.foobar");
253 test_escape_one("foobar.service", "foobar.service");
254 test_escape_one("cgroup.service", "_cgroup.service");
255 test_escape_one("tasks", "_tasks");
256 if (access("/sys/fs/cgroup/cpu", F_OK
) == 0)
257 test_escape_one("cpu.service", "_cpu.service");
258 test_escape_one("_foobar", "__foobar");
259 test_escape_one("", "_");
260 test_escape_one("_", "__");
261 test_escape_one(".", "_.");
264 TEST(controller_is_valid
) {
265 assert_se(cg_controller_is_valid("foobar"));
266 assert_se(cg_controller_is_valid("foo_bar"));
267 assert_se(cg_controller_is_valid("name=foo"));
268 assert_se(!cg_controller_is_valid(""));
269 assert_se(!cg_controller_is_valid("name="));
270 assert_se(!cg_controller_is_valid("="));
271 assert_se(!cg_controller_is_valid("cpu,cpuacct"));
272 assert_se(!cg_controller_is_valid("_"));
273 assert_se(!cg_controller_is_valid("_foobar"));
274 assert_se(!cg_controller_is_valid("tatü"));
277 static void test_slice_to_path_one(const char *unit
, const char *path
, int error
) {
278 _cleanup_free_
char *ret
= NULL
;
281 log_info("unit: %s", unit
);
283 r
= cg_slice_to_path(unit
, &ret
);
284 log_info("actual: %s / %d", strnull(ret
), r
);
285 log_info("expect: %s / %d", strnull(path
), error
);
286 assert_se(r
== error
);
287 ASSERT_STREQ(ret
, path
);
290 TEST(slice_to_path
) {
291 test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
292 test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
293 test_slice_to_path_one("foobar-waldo.service", NULL
, -EINVAL
);
294 test_slice_to_path_one(SPECIAL_ROOT_SLICE
, "", 0);
295 test_slice_to_path_one("--.slice", NULL
, -EINVAL
);
296 test_slice_to_path_one("-", NULL
, -EINVAL
);
297 test_slice_to_path_one("-foo-.slice", NULL
, -EINVAL
);
298 test_slice_to_path_one("-foo.slice", NULL
, -EINVAL
);
299 test_slice_to_path_one("foo-.slice", NULL
, -EINVAL
);
300 test_slice_to_path_one("foo--bar.slice", NULL
, -EINVAL
);
301 test_slice_to_path_one("foo.slice/foo--bar.slice", NULL
, -EINVAL
);
302 test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
303 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);
305 test_slice_to_path_one("foobar@.slice", NULL
, -EINVAL
);
306 test_slice_to_path_one("foobar@waldo.slice", NULL
, -EINVAL
);
307 test_slice_to_path_one("foobar@waldo.service", NULL
, -EINVAL
);
308 test_slice_to_path_one("-foo@-.slice", NULL
, -EINVAL
);
309 test_slice_to_path_one("-foo@.slice", NULL
, -EINVAL
);
310 test_slice_to_path_one("foo@-.slice", NULL
, -EINVAL
);
311 test_slice_to_path_one("foo@@bar.slice", NULL
, -EINVAL
);
312 test_slice_to_path_one("foo.slice/foo@@bar.slice", NULL
, -EINVAL
);
315 static void test_shift_path_one(const char *raw
, const char *root
, const char *shifted
) {
316 const char *s
= NULL
;
318 ASSERT_OK(cg_shift_path(raw
, root
, &s
));
319 ASSERT_STREQ(s
, shifted
);
323 test_shift_path_one("/foobar/waldo", "/", "/foobar/waldo");
324 test_shift_path_one("/foobar/waldo", "", "/foobar/waldo");
325 test_shift_path_one("/foobar/waldo", "/foobar", "/waldo");
326 test_shift_path_one("/foobar/waldo", "/hogehoge", "/foobar/waldo");
329 TEST(mask_supported
, .sd_booted
= true) {
332 ASSERT_OK(cg_mask_supported(&m
));
334 for (CGroupController c
= 0; c
< _CGROUP_CONTROLLER_MAX
; c
++)
335 printf("'%s' is supported: %s\n",
336 cgroup_controller_to_string(c
),
337 yes_no(m
& CGROUP_CONTROLLER_TO_MASK(c
)));
340 TEST(is_cgroup_fs
, .sd_booted
= true) {
342 assert_se(statfs("/sys/fs/cgroup", &sfs
) == 0);
343 if (is_temporary_fs(&sfs
))
344 assert_se(statfs("/sys/fs/cgroup/systemd", &sfs
) == 0);
345 assert_se(is_cgroup_fs(&sfs
));
348 TEST(fd_is_cgroup_fs
, .sd_booted
= true) {
351 fd
= open("/sys/fs/cgroup", O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
353 if (fd_is_temporary_fs(fd
)) {
355 fd
= open("/sys/fs/cgroup/systemd", O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
358 assert_se(fd_is_cgroup_fs(fd
));
363 int all
, hybrid
, systemd
, r
;
366 if (IN_SET(r
, -ENOENT
, -ENOMEDIUM
)) {
367 log_tests_skipped("cgroup not mounted");
372 all
= cg_all_unified();
373 assert_se(IN_SET(all
, 0, 1));
375 hybrid
= cg_hybrid_unified();
376 assert_se(IN_SET(hybrid
, 0, 1));
378 systemd
= cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER
);
379 assert_se(IN_SET(systemd
, 0, 1));
393 TEST(cg_get_keyed_attribute
) {
394 _cleanup_free_
char *val
= NULL
;
395 char *vals3
[3] = {}, *vals3a
[3] = {};
398 r
= cg_get_keyed_attribute("cpu", "/init.scope", "no_such_file", STRV_MAKE("no_such_attr"), &val
);
399 if (IN_SET(r
, -ENOMEDIUM
, -ENOENT
) || ERRNO_IS_PRIVILEGE(r
)) {
400 log_info_errno(r
, "Skipping most of %s, /sys/fs/cgroup not accessible: %m", __func__
);
404 assert_se(r
== -ENOENT
);
407 if (access("/sys/fs/cgroup/init.scope/cpu.stat", R_OK
) < 0) {
408 log_info_errno(errno
, "Skipping most of %s, /init.scope/cpu.stat not accessible: %m", __func__
);
412 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val
) == -ENXIO
);
413 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val
) == 0);
416 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val
) == 0);
419 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val
) == 1);
420 log_info("cpu /init.scope cpu.stat [usage_usec] → \"%s\"", val
);
422 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3
) == -ENXIO
);
423 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3
) == 1);
424 assert_se(vals3
[0] && !vals3
[1]);
427 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "usage_usec"), vals3
) == -ENXIO
);
428 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "usage_usec"), vals3
) == 1);
429 assert_se(vals3
[0] && !vals3
[1]);
432 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat",
433 STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3
) == 0);
434 for (size_t i
= 0; i
< 3; i
++)
437 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat",
438 STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3
) == 3);
439 log_info("cpu /init.scope cpu.stat [usage_usec user_usec system_usec] → \"%s\", \"%s\", \"%s\"",
440 vals3
[0], vals3
[1], vals3
[2]);
442 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat",
443 STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a
) == 0);
444 for (size_t i
= 0; i
< 3; i
++)
447 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat",
448 STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a
) == 3);
449 log_info("cpu /init.scope cpu.stat [system_usec user_usec usage_usec] → \"%s\", \"%s\", \"%s\"",
450 vals3a
[0], vals3a
[1], vals3a
[2]);
452 for (size_t i
= 0; i
< 3; i
++) {
458 TEST(bfq_weight_conversion
) {
459 assert_se(BFQ_WEIGHT(1) == 1);
460 assert_se(BFQ_WEIGHT(50) == 50);
461 assert_se(BFQ_WEIGHT(100) == 100);
462 assert_se(BFQ_WEIGHT(500) == 136);
463 assert_se(BFQ_WEIGHT(5000) == 545);
464 assert_se(BFQ_WEIGHT(10000) == 1000);
467 DEFINE_TEST_MAIN(LOG_DEBUG
);