]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-cgroup-util.c
nspawn: correctly document the relationship of --keep-unit and --register=no (#7364)
[thirdparty/systemd.git] / src / test / test-cgroup-util.c
CommitLineData
6c03089c
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
b5efdb8a 20#include "alloc-util.h"
0a05dcc0 21#include "build.h"
96cde13a 22#include "cgroup-util.h"
a0956174 23#include "dirent-util.h"
3ffd4af2 24#include "fd-util.h"
f97b34a6 25#include "format-util.h"
6bedfcbb 26#include "parse-util.h"
0a05dcc0 27#include "proc-cmdline.h"
0b452006 28#include "process-util.h"
f0bef277 29#include "stat-util.h"
07630cea 30#include "string-util.h"
3ffd4af2 31#include "test-helper.h"
ee104e11 32#include "user-util.h"
3ffd4af2 33#include "util.h"
96cde13a 34
7027ff61 35static void check_p_d_u(const char *path, int code, const char *result) {
6c03089c 36 _cleanup_free_ char *unit = NULL;
d4fffc4b 37 int r;
96cde13a 38
d4fffc4b
ZJS
39 r = cg_path_decode_unit(path, &unit);
40 printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
41 assert_se(r == code);
6c03089c
LP
42 assert_se(streq_ptr(unit, result));
43}
96cde13a 44
7027ff61 45static void test_path_decode_unit(void) {
d7bd3de0
LP
46 check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
47 check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
48 check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service");
cfeaa44a
LP
49 check_p_d_u("getty@.service/", -ENXIO, NULL);
50 check_p_d_u("getty@.service", -ENXIO, NULL);
7027ff61 51 check_p_d_u("getty.service", 0, "getty.service");
cfeaa44a
LP
52 check_p_d_u("getty", -ENXIO, NULL);
53 check_p_d_u("getty/waldo", -ENXIO, NULL);
d7bd3de0 54 check_p_d_u("_cpu.service", 0, "cpu.service");
6c03089c
LP
55}
56
57static void check_p_g_u(const char *path, int code, const char *result) {
58 _cleanup_free_ char *unit = NULL;
d4fffc4b 59 int r;
6c03089c 60
d4fffc4b
ZJS
61 r = cg_path_get_unit(path, &unit);
62 printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
63 assert_se(r == code);
6c03089c
LP
64 assert_se(streq_ptr(unit, result));
65}
66
9444b1f2
LP
67static void test_path_get_unit(void) {
68 check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
d7bd3de0
LP
69 check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
70 check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
71 check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
9444b1f2 72 check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
cfeaa44a
LP
73 check_p_g_u("sadfdsafsda", -ENXIO, NULL);
74 check_p_g_u("/system.slice/getty####@tty6.service/xxx", -ENXIO, NULL);
9444b1f2 75 check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
d7bd3de0 76 check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
d4fffc4b 77 check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service");
cfeaa44a 78 check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL);
9444b1f2
LP
79}
80
6c03089c
LP
81static void check_p_g_u_u(const char *path, int code, const char *result) {
82 _cleanup_free_ char *unit = NULL;
d4fffc4b 83 int r;
6c03089c 84
d4fffc4b
ZJS
85 r = cg_path_get_user_unit(path, &unit);
86 printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, result, code);
87 assert_se(r == code);
6c03089c
LP
88 assert_se(streq_ptr(unit, result));
89}
90
9444b1f2 91static void test_path_get_user_unit(void) {
374ec6ab
LP
92 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service");
93 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service");
94 check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
95 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
cfeaa44a 96 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -ENXIO, NULL);
d7bd3de0
LP
97 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
98 check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
99 check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
cfeaa44a 100 check_p_g_u_u("/meh.service", -ENXIO, NULL);
d7bd3de0 101 check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
d4fffc4b 102 check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service");
32081481 103 check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "foobar@pie.service");
cfeaa44a 104 check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL);
7027ff61 105}
6c03089c 106
9444b1f2
LP
107static void check_p_g_s(const char *path, int code, const char *result) {
108 _cleanup_free_ char *s = NULL;
109
110 assert_se(cg_path_get_session(path, &s) == code);
111 assert_se(streq_ptr(s, result));
96cde13a
ZJS
112}
113
9444b1f2 114static void test_path_get_session(void) {
374ec6ab
LP
115 check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2");
116 check_p_g_s("/session-3.scope", 0, "3");
cfeaa44a
LP
117 check_p_g_s("/session-.scope", -ENXIO, NULL);
118 check_p_g_s("", -ENXIO, NULL);
9444b1f2 119}
7027ff61 120
9444b1f2
LP
121static void check_p_g_o_u(const char *path, int code, uid_t result) {
122 uid_t uid = 0;
7027ff61 123
9444b1f2
LP
124 assert_se(cg_path_get_owner_uid(path, &uid) == code);
125 assert_se(uid == result);
126}
127
128static void test_path_get_owner_uid(void) {
374ec6ab
LP
129 check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000);
130 check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006);
cfeaa44a 131 check_p_g_o_u("", -ENXIO, 0);
9444b1f2 132}
7027ff61 133
8b0849e9
LP
134static void check_p_g_slice(const char *path, int code, const char *result) {
135 _cleanup_free_ char *s = NULL;
136
137 assert_se(cg_path_get_slice(path, &s) == code);
138 assert_se(streq_ptr(s, result));
139}
140
141static void test_path_get_slice(void) {
142 check_p_g_slice("/user.slice", 0, "user.slice");
143 check_p_g_slice("/foobar", 0, "-.slice");
144 check_p_g_slice("/user.slice/user-waldo.slice", 0, "user-waldo.slice");
145 check_p_g_slice("", 0, "-.slice");
146 check_p_g_slice("foobar", 0, "-.slice");
147 check_p_g_slice("foobar.slice", 0, "foobar.slice");
148 check_p_g_slice("foo.slice/foo-bar.slice/waldo.service", 0, "foo-bar.slice");
149}
150
329ac4bc
LP
151static void check_p_g_u_slice(const char *path, int code, const char *result) {
152 _cleanup_free_ char *s = NULL;
153
154 assert_se(cg_path_get_user_slice(path, &s) == code);
155 assert_se(streq_ptr(s, result));
156}
157
158static void test_path_get_user_slice(void) {
159 check_p_g_u_slice("/user.slice", -ENXIO, NULL);
160 check_p_g_u_slice("/foobar", -ENXIO, NULL);
161 check_p_g_u_slice("/user.slice/user-waldo.slice", -ENXIO, NULL);
162 check_p_g_u_slice("", -ENXIO, NULL);
163 check_p_g_u_slice("foobar", -ENXIO, NULL);
164 check_p_g_u_slice("foobar.slice", -ENXIO, NULL);
165 check_p_g_u_slice("foo.slice/foo-bar.slice/waldo.service", -ENXIO, NULL);
166
167 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service", 0, "-.slice");
168 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/", 0, "-.slice");
169 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service///", 0, "-.slice");
170 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/waldo.service", 0, "-.slice");
171 check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/piep.slice/foo.service", 0, "piep.slice");
172 check_p_g_u_slice("/foo.slice//foo-bar.slice/user@1000.service/piep.slice//piep-pap.slice//foo.service", 0, "piep-pap.slice");
173}
174
9444b1f2
LP
175static void test_get_paths(void) {
176 _cleanup_free_ char *a = NULL;
177
178 assert_se(cg_get_root_path(&a) >= 0);
179 log_info("Root = %s", a);
7027ff61
LP
180}
181
aff38e74
LP
182static void test_proc(void) {
183 _cleanup_closedir_ DIR *d = NULL;
184 struct dirent *de;
185 int r;
186
187 d = opendir("/proc");
188 assert_se(d);
189
190 FOREACH_DIRENT(de, d, break) {
e9174f29 191 _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL;
aff38e74 192 pid_t pid;
fed1e721 193 uid_t uid = UID_INVALID;
aff38e74 194
ec2ce0c5 195 if (!IN_SET(de->d_type, DT_DIR, DT_UNKNOWN))
aff38e74
LP
196 continue;
197
198 r = parse_pid(de->d_name, &pid);
199 if (r < 0)
200 continue;
201
ae018d9b
LP
202 if (is_kernel_thread(pid))
203 continue;
204
aff38e74 205 cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
e9174f29 206 cg_pid_get_path_shifted(pid, NULL, &path_shifted);
ae018d9b 207 cg_pid_get_owner_uid(pid, &uid);
aff38e74
LP
208 cg_pid_get_session(pid, &session);
209 cg_pid_get_unit(pid, &unit);
210 cg_pid_get_user_unit(pid, &user_unit);
211 cg_pid_get_machine_name(pid, &machine);
1021b21b 212 cg_pid_get_slice(pid, &slice);
aff38e74 213
de0671ee
ZJS
214 printf(PID_FMT"\t%s\t%s\t"UID_FMT"\t%s\t%s\t%s\t%s\t%s\n",
215 pid,
aff38e74 216 path,
aff38e74 217 path_shifted,
de0671ee 218 uid,
aff38e74
LP
219 session,
220 unit,
221 user_unit,
1021b21b
LP
222 machine,
223 slice);
aff38e74
LP
224 }
225}
226
ae018d9b
LP
227static void test_escape_one(const char *s, const char *r) {
228 _cleanup_free_ char *b;
229
230 b = cg_escape(s);
231 assert_se(b);
232 assert_se(streq(b, r));
233
234 assert_se(streq(cg_unescape(b), s));
235}
236
237static void test_escape(void) {
238 test_escape_one("foobar", "foobar");
a0ab5665 239 test_escape_one(".foobar", "_.foobar");
ae018d9b
LP
240 test_escape_one("foobar.service", "foobar.service");
241 test_escape_one("cgroup.service", "_cgroup.service");
ae018d9b 242 test_escape_one("tasks", "_tasks");
e13bb5d2
KS
243 if (access("/sys/fs/cgroup/cpu", F_OK) == 0)
244 test_escape_one("cpu.service", "_cpu.service");
ae018d9b 245 test_escape_one("_foobar", "__foobar");
a0ab5665
LP
246 test_escape_one("", "_");
247 test_escape_one("_", "__");
248 test_escape_one(".", "_.");
ae018d9b
LP
249}
250
78edb35a 251static void test_controller_is_valid(void) {
185a0874
DJL
252 assert_se(cg_controller_is_valid("foobar"));
253 assert_se(cg_controller_is_valid("foo_bar"));
254 assert_se(cg_controller_is_valid("name=foo"));
255 assert_se(!cg_controller_is_valid(""));
256 assert_se(!cg_controller_is_valid("name="));
257 assert_se(!cg_controller_is_valid("="));
258 assert_se(!cg_controller_is_valid("cpu,cpuacct"));
259 assert_se(!cg_controller_is_valid("_"));
260 assert_se(!cg_controller_is_valid("_foobar"));
261 assert_se(!cg_controller_is_valid("tatü"));
78edb35a
LP
262}
263
a016b922
LP
264static void test_slice_to_path_one(const char *unit, const char *path, int error) {
265 _cleanup_free_ char *ret = NULL;
266
267 assert_se(cg_slice_to_path(unit, &ret) == error);
268 assert_se(streq_ptr(ret, path));
269}
270
271static void test_slice_to_path(void) {
272
273 test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
274 test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
275 test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL);
c96cc582
LP
276 test_slice_to_path_one("-.slice", "", 0);
277 test_slice_to_path_one("--.slice", NULL, -EINVAL);
278 test_slice_to_path_one("-", NULL, -EINVAL);
a016b922
LP
279 test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
280 test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
c96cc582 281 test_slice_to_path_one("foo-.slice", NULL, -EINVAL);
e66e5b61 282 test_slice_to_path_one("foo--bar.slice", NULL, -EINVAL);
c96cc582 283 test_slice_to_path_one("foo.slice/foo--bar.slice", NULL, -EINVAL);
a016b922
LP
284 test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
285 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);
286}
287
751bc6ac
LP
288static void test_shift_path_one(const char *raw, const char *root, const char *shifted) {
289 const char *s = NULL;
290
291 assert_se(cg_shift_path(raw, root, &s) >= 0);
292 assert_se(streq(s, shifted));
293}
294
295static void test_shift_path(void) {
296
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", "/fuckfuck", "/foobar/waldo");
301}
302
5f4c5fef
LP
303static void test_mask_supported(void) {
304
305 CGroupMask m;
306 CGroupController c;
307
308 assert_se(cg_mask_supported(&m) >= 0);
309
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)));
312}
313
f0bef277
EV
314static void test_is_cgroup_fs(void) {
315 struct statfs sfs;
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));
320}
321
322static void test_fd_is_cgroup_fs(void) {
323 int fd;
324
325 fd = open("/sys/fs/cgroup", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
326 assert_se(fd >= 0);
327 if (fd_is_temporary_fs(fd)) {
328 fd = safe_close(fd);
329 fd = open("/sys/fs/cgroup/systemd", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
330 assert_se(fd >= 0);
331 }
332 assert_se(fd_is_cgroup_fs(fd));
333 fd = safe_close(fd);
334}
335
0a05dcc0
ZJS
336static void test_is_wanted_print(bool header) {
337 _cleanup_free_ char *cmdline = NULL;
338
339 log_info("-- %s --", __func__);
340 assert_se(proc_cmdline(&cmdline) >= 0);
341 log_info("cmdline: %s", cmdline);
342 if (header) {
343
344 log_info(_CGROUP_HIEARCHY_);
345 (void) system("findmnt -n /sys/fs/cgroup");
346 }
347
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()));
351 log_info(" ");
352}
353
354static 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);
358
359 assert_se(setenv("SYSTEMD_PROC_CMDLINE",
360 "systemd.unified_cgroup_hierarchy=0", 1) >= 0);
361 test_is_wanted_print(false);
362
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);
367
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);
372}
373
073e8f09
ZJS
374static void test_cg_tests(void) {
375 int all, hybrid, systemd;
376
377 assert_se(cg_unified_flush() == 0);
378
379 all = cg_all_unified();
380 assert_se(IN_SET(all, 0, 1));
381
382 hybrid = cg_hybrid_unified();
383 assert_se(IN_SET(hybrid, 0, 1));
384
385 systemd = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER);
386 assert_se(IN_SET(systemd, 0, 1));
387
388 if (all) {
389 assert_se(systemd);
390 assert_se(!hybrid);
391
392 } else if (hybrid) {
393 assert_se(systemd);
394 assert_se(!all);
395
396 } else
397 assert_se(!systemd);
398}
399
96cde13a 400int main(void) {
0a05dcc0
ZJS
401 log_set_max_level(LOG_DEBUG);
402 log_parse_environment();
403 log_open();
404
7027ff61 405 test_path_decode_unit();
6c03089c 406 test_path_get_unit();
7027ff61 407 test_path_get_user_unit();
9444b1f2
LP
408 test_path_get_session();
409 test_path_get_owner_uid();
8b0849e9 410 test_path_get_slice();
329ac4bc 411 test_path_get_user_slice();
143bfdaf 412 TEST_REQ_RUNNING_SYSTEMD(test_get_paths());
aff38e74 413 test_proc();
143bfdaf 414 TEST_REQ_RUNNING_SYSTEMD(test_escape());
78edb35a 415 test_controller_is_valid();
a016b922 416 test_slice_to_path();
751bc6ac 417 test_shift_path();
b3a7ba89 418 TEST_REQ_RUNNING_SYSTEMD(test_mask_supported());
f0bef277
EV
419 TEST_REQ_RUNNING_SYSTEMD(test_is_cgroup_fs());
420 TEST_REQ_RUNNING_SYSTEMD(test_fd_is_cgroup_fs());
0a05dcc0
ZJS
421 test_is_wanted_print(true);
422 test_is_wanted_print(false); /* run twice to test caching */
423 test_is_wanted();
073e8f09 424 test_cg_tests();
96cde13a
ZJS
425
426 return 0;
427}