]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-cgroup-util.c
tree-wide: use IN_SET where possible
[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
LP
194
195 if (de->d_type != DT_DIR &&
196 de->d_type != DT_UNKNOWN)
197 continue;
198
199 r = parse_pid(de->d_name, &pid);
200 if (r < 0)
201 continue;
202
ae018d9b
LP
203 if (is_kernel_thread(pid))
204 continue;
205
aff38e74 206 cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path);
e9174f29 207 cg_pid_get_path_shifted(pid, NULL, &path_shifted);
ae018d9b 208 cg_pid_get_owner_uid(pid, &uid);
aff38e74
LP
209 cg_pid_get_session(pid, &session);
210 cg_pid_get_unit(pid, &unit);
211 cg_pid_get_user_unit(pid, &user_unit);
212 cg_pid_get_machine_name(pid, &machine);
1021b21b 213 cg_pid_get_slice(pid, &slice);
aff38e74 214
de0671ee
ZJS
215 printf(PID_FMT"\t%s\t%s\t"UID_FMT"\t%s\t%s\t%s\t%s\t%s\n",
216 pid,
aff38e74 217 path,
aff38e74 218 path_shifted,
de0671ee 219 uid,
aff38e74
LP
220 session,
221 unit,
222 user_unit,
1021b21b
LP
223 machine,
224 slice);
aff38e74
LP
225 }
226}
227
ae018d9b
LP
228static void test_escape_one(const char *s, const char *r) {
229 _cleanup_free_ char *b;
230
231 b = cg_escape(s);
232 assert_se(b);
233 assert_se(streq(b, r));
234
235 assert_se(streq(cg_unescape(b), s));
236}
237
238static void test_escape(void) {
239 test_escape_one("foobar", "foobar");
a0ab5665 240 test_escape_one(".foobar", "_.foobar");
ae018d9b
LP
241 test_escape_one("foobar.service", "foobar.service");
242 test_escape_one("cgroup.service", "_cgroup.service");
ae018d9b 243 test_escape_one("tasks", "_tasks");
e13bb5d2
KS
244 if (access("/sys/fs/cgroup/cpu", F_OK) == 0)
245 test_escape_one("cpu.service", "_cpu.service");
ae018d9b 246 test_escape_one("_foobar", "__foobar");
a0ab5665
LP
247 test_escape_one("", "_");
248 test_escape_one("_", "__");
249 test_escape_one(".", "_.");
ae018d9b
LP
250}
251
78edb35a 252static void test_controller_is_valid(void) {
185a0874
DJL
253 assert_se(cg_controller_is_valid("foobar"));
254 assert_se(cg_controller_is_valid("foo_bar"));
255 assert_se(cg_controller_is_valid("name=foo"));
256 assert_se(!cg_controller_is_valid(""));
257 assert_se(!cg_controller_is_valid("name="));
258 assert_se(!cg_controller_is_valid("="));
259 assert_se(!cg_controller_is_valid("cpu,cpuacct"));
260 assert_se(!cg_controller_is_valid("_"));
261 assert_se(!cg_controller_is_valid("_foobar"));
262 assert_se(!cg_controller_is_valid("tatü"));
78edb35a
LP
263}
264
a016b922
LP
265static void test_slice_to_path_one(const char *unit, const char *path, int error) {
266 _cleanup_free_ char *ret = NULL;
267
268 assert_se(cg_slice_to_path(unit, &ret) == error);
269 assert_se(streq_ptr(ret, path));
270}
271
272static void test_slice_to_path(void) {
273
274 test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
275 test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
276 test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL);
c96cc582
LP
277 test_slice_to_path_one("-.slice", "", 0);
278 test_slice_to_path_one("--.slice", NULL, -EINVAL);
279 test_slice_to_path_one("-", NULL, -EINVAL);
a016b922
LP
280 test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
281 test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
c96cc582 282 test_slice_to_path_one("foo-.slice", NULL, -EINVAL);
e66e5b61 283 test_slice_to_path_one("foo--bar.slice", NULL, -EINVAL);
c96cc582 284 test_slice_to_path_one("foo.slice/foo--bar.slice", NULL, -EINVAL);
a016b922
LP
285 test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
286 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);
287}
288
751bc6ac
LP
289static void test_shift_path_one(const char *raw, const char *root, const char *shifted) {
290 const char *s = NULL;
291
292 assert_se(cg_shift_path(raw, root, &s) >= 0);
293 assert_se(streq(s, shifted));
294}
295
296static void test_shift_path(void) {
297
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", "/foobar", "/waldo");
301 test_shift_path_one("/foobar/waldo", "/fuckfuck", "/foobar/waldo");
302}
303
5f4c5fef
LP
304static void test_mask_supported(void) {
305
306 CGroupMask m;
307 CGroupController c;
308
309 assert_se(cg_mask_supported(&m) >= 0);
310
311 for (c = 0; c < _CGROUP_CONTROLLER_MAX; c++)
312 printf("'%s' is supported: %s\n", cgroup_controller_to_string(c), yes_no(m & CGROUP_CONTROLLER_TO_MASK(c)));
313}
314
f0bef277
EV
315static void test_is_cgroup_fs(void) {
316 struct statfs sfs;
317 assert_se(statfs("/sys/fs/cgroup", &sfs) == 0);
318 if (is_temporary_fs(&sfs))
319 assert_se(statfs("/sys/fs/cgroup/systemd", &sfs) == 0);
320 assert_se(is_cgroup_fs(&sfs));
321}
322
323static void test_fd_is_cgroup_fs(void) {
324 int fd;
325
326 fd = open("/sys/fs/cgroup", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
327 assert_se(fd >= 0);
328 if (fd_is_temporary_fs(fd)) {
329 fd = safe_close(fd);
330 fd = open("/sys/fs/cgroup/systemd", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
331 assert_se(fd >= 0);
332 }
333 assert_se(fd_is_cgroup_fs(fd));
334 fd = safe_close(fd);
335}
336
0a05dcc0
ZJS
337static void test_is_wanted_print(bool header) {
338 _cleanup_free_ char *cmdline = NULL;
339
340 log_info("-- %s --", __func__);
341 assert_se(proc_cmdline(&cmdline) >= 0);
342 log_info("cmdline: %s", cmdline);
343 if (header) {
344
345 log_info(_CGROUP_HIEARCHY_);
346 (void) system("findmnt -n /sys/fs/cgroup");
347 }
348
349 log_info("is_unified_wanted() → %s", yes_no(cg_is_unified_wanted()));
350 log_info("is_hybrid_wanted() → %s", yes_no(cg_is_hybrid_wanted()));
351 log_info("is_legacy_wanted() → %s", yes_no(cg_is_legacy_wanted()));
352 log_info(" ");
353}
354
355static void test_is_wanted(void) {
356 assert_se(setenv("SYSTEMD_PROC_CMDLINE",
357 "systemd.unified_cgroup_hierarchy", 1) >= 0);
358 test_is_wanted_print(false);
359
360 assert_se(setenv("SYSTEMD_PROC_CMDLINE",
361 "systemd.unified_cgroup_hierarchy=0", 1) >= 0);
362 test_is_wanted_print(false);
363
364 assert_se(setenv("SYSTEMD_PROC_CMDLINE",
365 "systemd.unified_cgroup_hierarchy=0 "
366 "systemd.legacy_systemd_cgroup_controller", 1) >= 0);
367 test_is_wanted_print(false);
368
369 assert_se(setenv("SYSTEMD_PROC_CMDLINE",
370 "systemd.unified_cgroup_hierarchy=0 "
371 "systemd.legacy_systemd_cgroup_controller=0", 1) >= 0);
372 test_is_wanted_print(false);
373}
374
96cde13a 375int main(void) {
0a05dcc0
ZJS
376 log_set_max_level(LOG_DEBUG);
377 log_parse_environment();
378 log_open();
379
7027ff61 380 test_path_decode_unit();
6c03089c 381 test_path_get_unit();
7027ff61 382 test_path_get_user_unit();
9444b1f2
LP
383 test_path_get_session();
384 test_path_get_owner_uid();
8b0849e9 385 test_path_get_slice();
329ac4bc 386 test_path_get_user_slice();
143bfdaf 387 TEST_REQ_RUNNING_SYSTEMD(test_get_paths());
aff38e74 388 test_proc();
143bfdaf 389 TEST_REQ_RUNNING_SYSTEMD(test_escape());
78edb35a 390 test_controller_is_valid();
a016b922 391 test_slice_to_path();
751bc6ac 392 test_shift_path();
b3a7ba89 393 TEST_REQ_RUNNING_SYSTEMD(test_mask_supported());
f0bef277
EV
394 TEST_REQ_RUNNING_SYSTEMD(test_is_cgroup_fs());
395 TEST_REQ_RUNNING_SYSTEMD(test_fd_is_cgroup_fs());
0a05dcc0
ZJS
396 test_is_wanted_print(true);
397 test_is_wanted_print(false); /* run twice to test caching */
398 test_is_wanted();
96cde13a
ZJS
399
400 return 0;
401}