2 This file is part of systemd.
4 Copyright 2013 Zbigniew Jędrzejewski-Szmek
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.
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.
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/>.
20 #include "alloc-util.h"
22 #include "cgroup-util.h"
23 #include "dirent-util.h"
25 #include "format-util.h"
26 #include "parse-util.h"
27 #include "proc-cmdline.h"
28 #include "process-util.h"
29 #include "stat-util.h"
30 #include "string-util.h"
31 #include "test-helper.h"
32 #include "user-util.h"
35 static void check_p_d_u(const char *path
, int code
, const char *result
) {
36 _cleanup_free_
char *unit
= NULL
;
39 r
= cg_path_decode_unit(path
, &unit
);
40 printf("%s: %s → %s %d expected %s %d\n", __func__
, path
, unit
, r
, result
, code
);
42 assert_se(streq_ptr(unit
, result
));
45 static void test_path_decode_unit(void) {
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");
49 check_p_d_u("getty@.service/", -ENXIO
, NULL
);
50 check_p_d_u("getty@.service", -ENXIO
, NULL
);
51 check_p_d_u("getty.service", 0, "getty.service");
52 check_p_d_u("getty", -ENXIO
, NULL
);
53 check_p_d_u("getty/waldo", -ENXIO
, NULL
);
54 check_p_d_u("_cpu.service", 0, "cpu.service");
57 static void check_p_g_u(const char *path
, int code
, const char *result
) {
58 _cleanup_free_
char *unit
= NULL
;
61 r
= cg_path_get_unit(path
, &unit
);
62 printf("%s: %s → %s %d expected %s %d\n", __func__
, path
, unit
, r
, result
, code
);
64 assert_se(streq_ptr(unit
, result
));
67 static void test_path_get_unit(void) {
68 check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
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");
72 check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
73 check_p_g_u("sadfdsafsda", -ENXIO
, NULL
);
74 check_p_g_u("/system.slice/getty####@tty6.service/xxx", -ENXIO
, NULL
);
75 check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
76 check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
77 check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service");
78 check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO
, NULL
);
81 static void check_p_g_u_u(const char *path
, int code
, const char *result
) {
82 _cleanup_free_
char *unit
= NULL
;
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
);
88 assert_se(streq_ptr(unit
, result
));
91 static void test_path_get_user_unit(void) {
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");
96 check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -ENXIO
, NULL
);
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");
100 check_p_g_u_u("/meh.service", -ENXIO
, NULL
);
101 check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
102 check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service");
103 check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "foobar@pie.service");
104 check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO
, NULL
);
107 static void check_p_g_s(const char *path
, int code
, const char *result
) {
108 _cleanup_free_
char *s
= NULL
;
110 assert_se(cg_path_get_session(path
, &s
) == code
);
111 assert_se(streq_ptr(s
, result
));
114 static void test_path_get_session(void) {
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");
117 check_p_g_s("/session-.scope", -ENXIO
, NULL
);
118 check_p_g_s("", -ENXIO
, NULL
);
121 static void check_p_g_o_u(const char *path
, int code
, uid_t result
) {
124 assert_se(cg_path_get_owner_uid(path
, &uid
) == code
);
125 assert_se(uid
== result
);
128 static void test_path_get_owner_uid(void) {
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);
131 check_p_g_o_u("", -ENXIO
, 0);
134 static void check_p_g_slice(const char *path
, int code
, const char *result
) {
135 _cleanup_free_
char *s
= NULL
;
137 assert_se(cg_path_get_slice(path
, &s
) == code
);
138 assert_se(streq_ptr(s
, result
));
141 static 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");
151 static void check_p_g_u_slice(const char *path
, int code
, const char *result
) {
152 _cleanup_free_
char *s
= NULL
;
154 assert_se(cg_path_get_user_slice(path
, &s
) == code
);
155 assert_se(streq_ptr(s
, result
));
158 static 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
);
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");
175 static void test_get_paths(void) {
176 _cleanup_free_
char *a
= NULL
;
178 assert_se(cg_get_root_path(&a
) >= 0);
179 log_info("Root = %s", a
);
182 static void test_proc(void) {
183 _cleanup_closedir_
DIR *d
= NULL
;
187 d
= opendir("/proc");
190 FOREACH_DIRENT(de
, d
, break) {
191 _cleanup_free_
char *path
= NULL
, *path_shifted
= NULL
, *session
= NULL
, *unit
= NULL
, *user_unit
= NULL
, *machine
= NULL
, *slice
= NULL
;
193 uid_t uid
= UID_INVALID
;
195 if (!IN_SET(de
->d_type
, DT_DIR
, DT_UNKNOWN
))
198 r
= parse_pid(de
->d_name
, &pid
);
202 if (is_kernel_thread(pid
))
205 cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, pid
, &path
);
206 cg_pid_get_path_shifted(pid
, NULL
, &path_shifted
);
207 cg_pid_get_owner_uid(pid
, &uid
);
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
);
212 cg_pid_get_slice(pid
, &slice
);
214 printf(PID_FMT
"\t%s\t%s\t"UID_FMT
"\t%s\t%s\t%s\t%s\t%s\n",
227 static void test_escape_one(const char *s
, const char *r
) {
228 _cleanup_free_
char *b
;
232 assert_se(streq(b
, r
));
234 assert_se(streq(cg_unescape(b
), s
));
237 static void test_escape(void) {
238 test_escape_one("foobar", "foobar");
239 test_escape_one(".foobar", "_.foobar");
240 test_escape_one("foobar.service", "foobar.service");
241 test_escape_one("cgroup.service", "_cgroup.service");
242 test_escape_one("tasks", "_tasks");
243 if (access("/sys/fs/cgroup/cpu", F_OK
) == 0)
244 test_escape_one("cpu.service", "_cpu.service");
245 test_escape_one("_foobar", "__foobar");
246 test_escape_one("", "_");
247 test_escape_one("_", "__");
248 test_escape_one(".", "_.");
251 static void test_controller_is_valid(void) {
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ü"));
264 static void test_slice_to_path_one(const char *unit
, const char *path
, int error
) {
265 _cleanup_free_
char *ret
= NULL
;
267 assert_se(cg_slice_to_path(unit
, &ret
) == error
);
268 assert_se(streq_ptr(ret
, path
));
271 static void test_slice_to_path(void) {
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
);
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
);
279 test_slice_to_path_one("-foo-.slice", NULL
, -EINVAL
);
280 test_slice_to_path_one("-foo.slice", NULL
, -EINVAL
);
281 test_slice_to_path_one("foo-.slice", NULL
, -EINVAL
);
282 test_slice_to_path_one("foo--bar.slice", NULL
, -EINVAL
);
283 test_slice_to_path_one("foo.slice/foo--bar.slice", NULL
, -EINVAL
);
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);
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", "/fuckfuck", "/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);
375 log_set_max_level(LOG_DEBUG
);
376 log_parse_environment();
379 test_path_decode_unit();
380 test_path_get_unit();
381 test_path_get_user_unit();
382 test_path_get_session();
383 test_path_get_owner_uid();
384 test_path_get_slice();
385 test_path_get_user_slice();
386 TEST_REQ_RUNNING_SYSTEMD(test_get_paths());
388 TEST_REQ_RUNNING_SYSTEMD(test_escape());
389 test_controller_is_valid();
390 test_slice_to_path();
392 TEST_REQ_RUNNING_SYSTEMD(test_mask_supported());
393 TEST_REQ_RUNNING_SYSTEMD(test_is_cgroup_fs());
394 TEST_REQ_RUNNING_SYSTEMD(test_fd_is_cgroup_fs());
395 test_is_wanted_print(true);
396 test_is_wanted_print(false); /* run twice to test caching */