]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-cgroup-util.c
Merge pull request #32520 from YHNdnzj/sd-daemon-followup
[thirdparty/systemd.git] / src / test / test-cgroup-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
6c03089c 2
b5efdb8a 3#include "alloc-util.h"
96cde13a 4#include "cgroup-util.h"
a0956174 5#include "dirent-util.h"
3c14dc61 6#include "errno-util.h"
3ffd4af2 7#include "fd-util.h"
f97b34a6 8#include "format-util.h"
6bedfcbb 9#include "parse-util.h"
0a05dcc0 10#include "proc-cmdline.h"
0b452006 11#include "process-util.h"
e5af6e0e 12#include "special.h"
f0bef277 13#include "stat-util.h"
07630cea 14#include "string-util.h"
9177fa9f 15#include "strv.h"
6d7c4033 16#include "tests.h"
ee104e11 17#include "user-util.h"
96cde13a 18
7027ff61 19static void check_p_d_u(const char *path, int code, const char *result) {
6c03089c 20 _cleanup_free_ char *unit = NULL;
d4fffc4b 21 int r;
96cde13a 22
d4fffc4b 23 r = cg_path_decode_unit(path, &unit);
b1d666ac 24 printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code);
d4fffc4b 25 assert_se(r == code);
c79e88b3 26 ASSERT_STREQ(unit, result);
6c03089c 27}
96cde13a 28
26e555cb 29TEST(path_decode_unit) {
d7bd3de0
LP
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");
cfeaa44a
LP
33 check_p_d_u("getty@.service/", -ENXIO, NULL);
34 check_p_d_u("getty@.service", -ENXIO, NULL);
7027ff61 35 check_p_d_u("getty.service", 0, "getty.service");
cfeaa44a
LP
36 check_p_d_u("getty", -ENXIO, NULL);
37 check_p_d_u("getty/waldo", -ENXIO, NULL);
d7bd3de0 38 check_p_d_u("_cpu.service", 0, "cpu.service");
6c03089c
LP
39}
40
41static void check_p_g_u(const char *path, int code, const char *result) {
42 _cleanup_free_ char *unit = NULL;
d4fffc4b 43 int r;
6c03089c 44
d4fffc4b 45 r = cg_path_get_unit(path, &unit);
b1d666ac 46 printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code);
d4fffc4b 47 assert_se(r == code);
c79e88b3 48 ASSERT_STREQ(unit, result);
6c03089c
LP
49}
50
26e555cb 51TEST(path_get_unit) {
9444b1f2 52 check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
d7bd3de0
LP
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");
9444b1f2 56 check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
cfeaa44a
LP
57 check_p_g_u("sadfdsafsda", -ENXIO, NULL);
58 check_p_g_u("/system.slice/getty####@tty6.service/xxx", -ENXIO, NULL);
9444b1f2 59 check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
d7bd3de0 60 check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
d4fffc4b 61 check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service");
cfeaa44a 62 check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL);
9444b1f2
LP
63}
64
ee164216
QD
65static void check_p_g_u_p(const char *path, int code, const char *result) {
66 _cleanup_free_ char *unit_path = NULL;
67 int r;
68
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);
71 assert_se(r == code);
c79e88b3 72 ASSERT_STREQ(unit_path, result);
ee164216
QD
73}
74
75TEST(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");
90}
91
6c03089c
LP
92static void check_p_g_u_u(const char *path, int code, const char *result) {
93 _cleanup_free_ char *unit = NULL;
d4fffc4b 94 int r;
6c03089c 95
d4fffc4b 96 r = cg_path_get_user_unit(path, &unit);
b1d666ac 97 printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code);
d4fffc4b 98 assert_se(r == code);
c79e88b3 99 ASSERT_STREQ(unit, result);
6c03089c
LP
100}
101
26e555cb 102TEST(path_get_user_unit) {
374ec6ab
LP
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");
cfeaa44a 107 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -ENXIO, NULL);
d7bd3de0
LP
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");
cfeaa44a 111 check_p_g_u_u("/meh.service", -ENXIO, NULL);
d7bd3de0 112 check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
d4fffc4b 113 check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service");
32081481 114 check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "foobar@pie.service");
cfeaa44a 115 check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL);
7027ff61 116}
6c03089c 117
9444b1f2
LP
118static void check_p_g_s(const char *path, int code, const char *result) {
119 _cleanup_free_ char *s = NULL;
120
121 assert_se(cg_path_get_session(path, &s) == code);
c79e88b3 122 ASSERT_STREQ(s, result);
96cde13a
ZJS
123}
124
26e555cb 125TEST(path_get_session) {
374ec6ab
LP
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");
cfeaa44a
LP
128 check_p_g_s("/session-.scope", -ENXIO, NULL);
129 check_p_g_s("", -ENXIO, NULL);
9444b1f2 130}
7027ff61 131
9444b1f2
LP
132static void check_p_g_o_u(const char *path, int code, uid_t result) {
133 uid_t uid = 0;
7027ff61 134
9444b1f2
LP
135 assert_se(cg_path_get_owner_uid(path, &uid) == code);
136 assert_se(uid == result);
137}
138
26e555cb 139TEST(path_get_owner_uid) {
374ec6ab
LP
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);
cfeaa44a 142 check_p_g_o_u("", -ENXIO, 0);
9444b1f2 143}
7027ff61 144
8b0849e9
LP
145static void check_p_g_slice(const char *path, int code, const char *result) {
146 _cleanup_free_ char *s = NULL;
147
148 assert_se(cg_path_get_slice(path, &s) == code);
c79e88b3 149 ASSERT_STREQ(s, result);
8b0849e9
LP
150}
151
26e555cb 152TEST(path_get_slice) {
8b0849e9 153 check_p_g_slice("/user.slice", 0, "user.slice");
e5af6e0e 154 check_p_g_slice("/foobar", 0, SPECIAL_ROOT_SLICE);
8b0849e9 155 check_p_g_slice("/user.slice/user-waldo.slice", 0, "user-waldo.slice");
e5af6e0e
ZJS
156 check_p_g_slice("", 0, SPECIAL_ROOT_SLICE);
157 check_p_g_slice("foobar", 0, SPECIAL_ROOT_SLICE);
8b0849e9
LP
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");
160}
161
329ac4bc
LP
162static void check_p_g_u_slice(const char *path, int code, const char *result) {
163 _cleanup_free_ char *s = NULL;
164
165 assert_se(cg_path_get_user_slice(path, &s) == code);
c79e88b3 166 ASSERT_STREQ(s, result);
329ac4bc
LP
167}
168
26e555cb 169TEST(path_get_user_slice) {
329ac4bc
LP
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);
177
e5af6e0e
ZJS
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);
329ac4bc
LP
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");
184}
185
26e555cb 186TEST(get_paths, .sd_booted = true) {
9444b1f2
LP
187 _cleanup_free_ char *a = NULL;
188
189 assert_se(cg_get_root_path(&a) >= 0);
190 log_info("Root = %s", a);
7027ff61
LP
191}
192
26e555cb 193TEST(proc) {
aff38e74 194 _cleanup_closedir_ DIR *d = NULL;
aff38e74
LP
195 int r;
196
f9d273e6 197 ASSERT_OK(proc_dir_open(&d));
aff38e74 198
957f84e9 199 for (;;) {
e9174f29 200 _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL;
957f84e9 201 _cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
fed1e721 202 uid_t uid = UID_INVALID;
aff38e74 203
957f84e9
LP
204 r = proc_dir_read_pidref(d, &pid);
205 assert_se(r >= 0);
aff38e74 206
957f84e9
LP
207 if (r == 0)
208 break;
aff38e74 209
957f84e9 210 if (pidref_is_kernel_thread(&pid) != 0)
ae018d9b
LP
211 continue;
212
957f84e9
LP
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);
aff38e74 221
de0671ee 222 printf(PID_FMT"\t%s\t%s\t"UID_FMT"\t%s\t%s\t%s\t%s\t%s\n",
957f84e9 223 pid.pid,
aff38e74 224 path,
aff38e74 225 path_shifted,
de0671ee 226 uid,
aff38e74
LP
227 session,
228 unit,
229 user_unit,
1021b21b
LP
230 machine,
231 slice);
aff38e74
LP
232 }
233}
234
1a56b0c0
LP
235static void test_escape_one(const char *s, const char *expected) {
236 _cleanup_free_ char *b = NULL;
ae018d9b 237
1a56b0c0
LP
238 assert_se(s);
239 assert_se(expected);
240
f9d273e6 241 ASSERT_OK(cg_escape(s, &b));
c79e88b3 242 ASSERT_STREQ(b, expected);
ae018d9b 243
c79e88b3 244 ASSERT_STREQ(cg_unescape(b), s);
1a56b0c0
LP
245
246 assert_se(filename_is_valid(b));
247 assert_se(!cg_needs_escape(s) || b[0] == '_');
ae018d9b
LP
248}
249
26e555cb 250TEST(escape, .sd_booted = true) {
ae018d9b 251 test_escape_one("foobar", "foobar");
a0ab5665 252 test_escape_one(".foobar", "_.foobar");
ae018d9b
LP
253 test_escape_one("foobar.service", "foobar.service");
254 test_escape_one("cgroup.service", "_cgroup.service");
ae018d9b 255 test_escape_one("tasks", "_tasks");
e13bb5d2
KS
256 if (access("/sys/fs/cgroup/cpu", F_OK) == 0)
257 test_escape_one("cpu.service", "_cpu.service");
ae018d9b 258 test_escape_one("_foobar", "__foobar");
a0ab5665
LP
259 test_escape_one("", "_");
260 test_escape_one("_", "__");
261 test_escape_one(".", "_.");
ae018d9b
LP
262}
263
26e555cb 264TEST(controller_is_valid) {
185a0874
DJL
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ü"));
78edb35a
LP
275}
276
a016b922
LP
277static void test_slice_to_path_one(const char *unit, const char *path, int error) {
278 _cleanup_free_ char *ret = NULL;
0682ed5c
ZJS
279 int r;
280
281 log_info("unit: %s", unit);
a016b922 282
0682ed5c
ZJS
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);
c79e88b3 287 ASSERT_STREQ(ret, path);
a016b922
LP
288}
289
26e555cb 290TEST(slice_to_path) {
a016b922
LP
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);
e5af6e0e 294 test_slice_to_path_one(SPECIAL_ROOT_SLICE, "", 0);
c96cc582
LP
295 test_slice_to_path_one("--.slice", NULL, -EINVAL);
296 test_slice_to_path_one("-", NULL, -EINVAL);
a016b922
LP
297 test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
298 test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
c96cc582 299 test_slice_to_path_one("foo-.slice", NULL, -EINVAL);
e66e5b61 300 test_slice_to_path_one("foo--bar.slice", NULL, -EINVAL);
c96cc582 301 test_slice_to_path_one("foo.slice/foo--bar.slice", NULL, -EINVAL);
a016b922
LP
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);
0682ed5c
ZJS
304
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);
a016b922
LP
313}
314
751bc6ac
LP
315static void test_shift_path_one(const char *raw, const char *root, const char *shifted) {
316 const char *s = NULL;
317
f9d273e6 318 ASSERT_OK(cg_shift_path(raw, root, &s));
c79e88b3 319 ASSERT_STREQ(s, shifted);
751bc6ac
LP
320}
321
26e555cb 322TEST(shift_path) {
751bc6ac
LP
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");
a2917d3d 326 test_shift_path_one("/foobar/waldo", "/hogehoge", "/foobar/waldo");
751bc6ac
LP
327}
328
26e555cb 329TEST(mask_supported, .sd_booted = true) {
5f4c5fef 330 CGroupMask m;
5f4c5fef 331
f9d273e6 332 ASSERT_OK(cg_mask_supported(&m));
5f4c5fef 333
7e850da3
ZJS
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)));
5f4c5fef
LP
338}
339
26e555cb 340TEST(is_cgroup_fs, .sd_booted = true) {
f0bef277
EV
341 struct statfs sfs;
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));
346}
347
26e555cb 348TEST(fd_is_cgroup_fs, .sd_booted = true) {
f0bef277
EV
349 int fd;
350
351 fd = open("/sys/fs/cgroup", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
352 assert_se(fd >= 0);
353 if (fd_is_temporary_fs(fd)) {
354 fd = safe_close(fd);
355 fd = open("/sys/fs/cgroup/systemd", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
356 assert_se(fd >= 0);
357 }
358 assert_se(fd_is_cgroup_fs(fd));
359 fd = safe_close(fd);
360}
361
26e555cb 362TEST(cg_tests) {
05d69e02 363 int all, hybrid, systemd, r;
073e8f09 364
d4d99bc6 365 r = cg_unified();
a412a1b9 366 if (IN_SET(r, -ENOENT, -ENOMEDIUM)) {
b66789a9 367 log_tests_skipped("cgroup not mounted");
05d69e02
ZJS
368 return;
369 }
d4d99bc6 370 assert_se(r >= 0);
073e8f09
ZJS
371
372 all = cg_all_unified();
373 assert_se(IN_SET(all, 0, 1));
374
375 hybrid = cg_hybrid_unified();
376 assert_se(IN_SET(hybrid, 0, 1));
377
378 systemd = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER);
379 assert_se(IN_SET(systemd, 0, 1));
380
381 if (all) {
382 assert_se(systemd);
383 assert_se(!hybrid);
384
385 } else if (hybrid) {
386 assert_se(systemd);
387 assert_se(!all);
388
389 } else
390 assert_se(!systemd);
391}
392
26e555cb 393TEST(cg_get_keyed_attribute) {
9177fa9f
ZJS
394 _cleanup_free_ char *val = NULL;
395 char *vals3[3] = {}, *vals3a[3] = {};
7e850da3 396 int r;
9177fa9f 397
18ce247c 398 r = cg_get_keyed_attribute("cpu", "/init.scope", "no_such_file", STRV_MAKE("no_such_attr"), &val);
a412a1b9 399 if (IN_SET(r, -ENOMEDIUM, -ENOENT) || ERRNO_IS_PRIVILEGE(r)) {
18ce247c
ZJS
400 log_info_errno(r, "Skipping most of %s, /sys/fs/cgroup not accessible: %m", __func__);
401 return;
402 }
403
404 assert_se(r == -ENOENT);
5152b845 405 ASSERT_NULL(val);
9177fa9f
ZJS
406
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__);
409 return;
410 }
411
412 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val) == -ENXIO);
25a1f04c 413 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val) == 0);
5152b845 414 ASSERT_NULL(val);
9177fa9f
ZJS
415
416 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val) == 0);
111e71c4 417 val = mfree(val);
25a1f04c
MS
418
419 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val) == 1);
9177fa9f
ZJS
420 log_info("cpu /init.scope cpu.stat [usage_usec] → \"%s\"", val);
421
422 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3) == -ENXIO);
25a1f04c 423 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3) == 1);
f21b863e 424 assert_se(vals3[0] && !vals3[1]);
25a1f04c 425 free(vals3[0]);
9177fa9f
ZJS
426
427 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "usage_usec"), vals3) == -ENXIO);
25a1f04c 428 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "usage_usec"), vals3) == 1);
f21b863e 429 assert_se(vals3[0] && !vals3[1]);
25a1f04c 430 free(vals3[0]);
9177fa9f
ZJS
431
432 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat",
433 STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3) == 0);
7e850da3 434 for (size_t i = 0; i < 3; i++)
25a1f04c
MS
435 free(vals3[i]);
436
437 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat",
438 STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3) == 3);
9177fa9f
ZJS
439 log_info("cpu /init.scope cpu.stat [usage_usec user_usec system_usec] → \"%s\", \"%s\", \"%s\"",
440 vals3[0], vals3[1], vals3[2]);
441
442 assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat",
443 STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a) == 0);
7e850da3 444 for (size_t i = 0; i < 3; i++)
25a1f04c
MS
445 free(vals3a[i]);
446
447 assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat",
448 STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a) == 3);
9177fa9f
ZJS
449 log_info("cpu /init.scope cpu.stat [system_usec user_usec usage_usec] → \"%s\", \"%s\", \"%s\"",
450 vals3a[0], vals3a[1], vals3a[2]);
451
7e850da3 452 for (size_t i = 0; i < 3; i++) {
9177fa9f
ZJS
453 free(vals3[i]);
454 free(vals3a[i]);
455 }
456}
457
d2bd0bfa
MK
458TEST(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);
465}
466
26e555cb 467DEFINE_TEST_MAIN(LOG_DEBUG);