]>
Commit | Line | Data |
---|---|---|
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" |
3ffd4af2 | 18 | #include "util.h" |
47350c5f | 19 | #include "version.h" |
96cde13a | 20 | |
7027ff61 | 21 | static void check_p_d_u(const char *path, int code, const char *result) { |
6c03089c | 22 | _cleanup_free_ char *unit = NULL; |
d4fffc4b | 23 | int r; |
96cde13a | 24 | |
d4fffc4b | 25 | r = cg_path_decode_unit(path, &unit); |
b1d666ac | 26 | printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code); |
d4fffc4b | 27 | assert_se(r == code); |
6c03089c LP |
28 | assert_se(streq_ptr(unit, result)); |
29 | } | |
96cde13a | 30 | |
7027ff61 | 31 | static void test_path_decode_unit(void) { |
d7bd3de0 LP |
32 | check_p_d_u("getty@tty2.service", 0, "getty@tty2.service"); |
33 | check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service"); | |
34 | check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service"); | |
cfeaa44a LP |
35 | check_p_d_u("getty@.service/", -ENXIO, NULL); |
36 | check_p_d_u("getty@.service", -ENXIO, NULL); | |
7027ff61 | 37 | check_p_d_u("getty.service", 0, "getty.service"); |
cfeaa44a LP |
38 | check_p_d_u("getty", -ENXIO, NULL); |
39 | check_p_d_u("getty/waldo", -ENXIO, NULL); | |
d7bd3de0 | 40 | check_p_d_u("_cpu.service", 0, "cpu.service"); |
6c03089c LP |
41 | } |
42 | ||
43 | static void check_p_g_u(const char *path, int code, const char *result) { | |
44 | _cleanup_free_ char *unit = NULL; | |
d4fffc4b | 45 | int r; |
6c03089c | 46 | |
d4fffc4b | 47 | r = cg_path_get_unit(path, &unit); |
b1d666ac | 48 | printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code); |
d4fffc4b | 49 | assert_se(r == code); |
6c03089c LP |
50 | assert_se(streq_ptr(unit, result)); |
51 | } | |
52 | ||
9444b1f2 LP |
53 | static void test_path_get_unit(void) { |
54 | check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service"); | |
d7bd3de0 LP |
55 | check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service"); |
56 | check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service"); | |
57 | check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service"); | |
9444b1f2 | 58 | check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service"); |
cfeaa44a LP |
59 | check_p_g_u("sadfdsafsda", -ENXIO, NULL); |
60 | check_p_g_u("/system.slice/getty####@tty6.service/xxx", -ENXIO, NULL); | |
9444b1f2 | 61 | check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service"); |
d7bd3de0 | 62 | check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service"); |
d4fffc4b | 63 | check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service"); |
cfeaa44a | 64 | check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL); |
9444b1f2 LP |
65 | } |
66 | ||
6c03089c LP |
67 | static void check_p_g_u_u(const char *path, int code, const char *result) { |
68 | _cleanup_free_ char *unit = NULL; | |
d4fffc4b | 69 | int r; |
6c03089c | 70 | |
d4fffc4b | 71 | r = cg_path_get_user_unit(path, &unit); |
b1d666ac | 72 | printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code); |
d4fffc4b | 73 | assert_se(r == code); |
6c03089c LP |
74 | assert_se(streq_ptr(unit, result)); |
75 | } | |
76 | ||
9444b1f2 | 77 | static void test_path_get_user_unit(void) { |
374ec6ab LP |
78 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service"); |
79 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service"); | |
80 | check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service"); | |
81 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service"); | |
cfeaa44a | 82 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -ENXIO, NULL); |
d7bd3de0 LP |
83 | check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service"); |
84 | check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service"); | |
85 | check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service"); | |
cfeaa44a | 86 | check_p_g_u_u("/meh.service", -ENXIO, NULL); |
d7bd3de0 | 87 | check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service"); |
d4fffc4b | 88 | check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service"); |
32081481 | 89 | check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "foobar@pie.service"); |
cfeaa44a | 90 | check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL); |
7027ff61 | 91 | } |
6c03089c | 92 | |
9444b1f2 LP |
93 | static void check_p_g_s(const char *path, int code, const char *result) { |
94 | _cleanup_free_ char *s = NULL; | |
95 | ||
96 | assert_se(cg_path_get_session(path, &s) == code); | |
97 | assert_se(streq_ptr(s, result)); | |
96cde13a ZJS |
98 | } |
99 | ||
9444b1f2 | 100 | static void test_path_get_session(void) { |
374ec6ab LP |
101 | check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2"); |
102 | check_p_g_s("/session-3.scope", 0, "3"); | |
cfeaa44a LP |
103 | check_p_g_s("/session-.scope", -ENXIO, NULL); |
104 | check_p_g_s("", -ENXIO, NULL); | |
9444b1f2 | 105 | } |
7027ff61 | 106 | |
9444b1f2 LP |
107 | static void check_p_g_o_u(const char *path, int code, uid_t result) { |
108 | uid_t uid = 0; | |
7027ff61 | 109 | |
9444b1f2 LP |
110 | assert_se(cg_path_get_owner_uid(path, &uid) == code); |
111 | assert_se(uid == result); | |
112 | } | |
113 | ||
114 | static void test_path_get_owner_uid(void) { | |
374ec6ab LP |
115 | check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000); |
116 | check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006); | |
cfeaa44a | 117 | check_p_g_o_u("", -ENXIO, 0); |
9444b1f2 | 118 | } |
7027ff61 | 119 | |
8b0849e9 LP |
120 | static void check_p_g_slice(const char *path, int code, const char *result) { |
121 | _cleanup_free_ char *s = NULL; | |
122 | ||
123 | assert_se(cg_path_get_slice(path, &s) == code); | |
124 | assert_se(streq_ptr(s, result)); | |
125 | } | |
126 | ||
127 | static void test_path_get_slice(void) { | |
128 | check_p_g_slice("/user.slice", 0, "user.slice"); | |
e5af6e0e | 129 | check_p_g_slice("/foobar", 0, SPECIAL_ROOT_SLICE); |
8b0849e9 | 130 | check_p_g_slice("/user.slice/user-waldo.slice", 0, "user-waldo.slice"); |
e5af6e0e ZJS |
131 | check_p_g_slice("", 0, SPECIAL_ROOT_SLICE); |
132 | check_p_g_slice("foobar", 0, SPECIAL_ROOT_SLICE); | |
8b0849e9 LP |
133 | check_p_g_slice("foobar.slice", 0, "foobar.slice"); |
134 | check_p_g_slice("foo.slice/foo-bar.slice/waldo.service", 0, "foo-bar.slice"); | |
135 | } | |
136 | ||
329ac4bc LP |
137 | static void check_p_g_u_slice(const char *path, int code, const char *result) { |
138 | _cleanup_free_ char *s = NULL; | |
139 | ||
140 | assert_se(cg_path_get_user_slice(path, &s) == code); | |
141 | assert_se(streq_ptr(s, result)); | |
142 | } | |
143 | ||
144 | static void test_path_get_user_slice(void) { | |
145 | check_p_g_u_slice("/user.slice", -ENXIO, NULL); | |
146 | check_p_g_u_slice("/foobar", -ENXIO, NULL); | |
147 | check_p_g_u_slice("/user.slice/user-waldo.slice", -ENXIO, NULL); | |
148 | check_p_g_u_slice("", -ENXIO, NULL); | |
149 | check_p_g_u_slice("foobar", -ENXIO, NULL); | |
150 | check_p_g_u_slice("foobar.slice", -ENXIO, NULL); | |
151 | check_p_g_u_slice("foo.slice/foo-bar.slice/waldo.service", -ENXIO, NULL); | |
152 | ||
e5af6e0e ZJS |
153 | check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service", 0, SPECIAL_ROOT_SLICE); |
154 | check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/", 0, SPECIAL_ROOT_SLICE); | |
155 | check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service///", 0, SPECIAL_ROOT_SLICE); | |
156 | check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/waldo.service", 0, SPECIAL_ROOT_SLICE); | |
329ac4bc LP |
157 | check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/piep.slice/foo.service", 0, "piep.slice"); |
158 | check_p_g_u_slice("/foo.slice//foo-bar.slice/user@1000.service/piep.slice//piep-pap.slice//foo.service", 0, "piep-pap.slice"); | |
159 | } | |
160 | ||
9444b1f2 LP |
161 | static void test_get_paths(void) { |
162 | _cleanup_free_ char *a = NULL; | |
163 | ||
164 | assert_se(cg_get_root_path(&a) >= 0); | |
165 | log_info("Root = %s", a); | |
7027ff61 LP |
166 | } |
167 | ||
aff38e74 LP |
168 | static void test_proc(void) { |
169 | _cleanup_closedir_ DIR *d = NULL; | |
170 | struct dirent *de; | |
171 | int r; | |
172 | ||
173 | d = opendir("/proc"); | |
174 | assert_se(d); | |
175 | ||
176 | FOREACH_DIRENT(de, d, break) { | |
e9174f29 | 177 | _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL; |
aff38e74 | 178 | pid_t pid; |
fed1e721 | 179 | uid_t uid = UID_INVALID; |
aff38e74 | 180 | |
ec2ce0c5 | 181 | if (!IN_SET(de->d_type, DT_DIR, DT_UNKNOWN)) |
aff38e74 LP |
182 | continue; |
183 | ||
184 | r = parse_pid(de->d_name, &pid); | |
185 | if (r < 0) | |
186 | continue; | |
187 | ||
ae018d9b LP |
188 | if (is_kernel_thread(pid)) |
189 | continue; | |
190 | ||
aff38e74 | 191 | cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path); |
e9174f29 | 192 | cg_pid_get_path_shifted(pid, NULL, &path_shifted); |
ae018d9b | 193 | cg_pid_get_owner_uid(pid, &uid); |
aff38e74 LP |
194 | cg_pid_get_session(pid, &session); |
195 | cg_pid_get_unit(pid, &unit); | |
196 | cg_pid_get_user_unit(pid, &user_unit); | |
197 | cg_pid_get_machine_name(pid, &machine); | |
1021b21b | 198 | cg_pid_get_slice(pid, &slice); |
aff38e74 | 199 | |
de0671ee ZJS |
200 | printf(PID_FMT"\t%s\t%s\t"UID_FMT"\t%s\t%s\t%s\t%s\t%s\n", |
201 | pid, | |
aff38e74 | 202 | path, |
aff38e74 | 203 | path_shifted, |
de0671ee | 204 | uid, |
aff38e74 LP |
205 | session, |
206 | unit, | |
207 | user_unit, | |
1021b21b LP |
208 | machine, |
209 | slice); | |
aff38e74 LP |
210 | } |
211 | } | |
212 | ||
ae018d9b LP |
213 | static void test_escape_one(const char *s, const char *r) { |
214 | _cleanup_free_ char *b; | |
215 | ||
216 | b = cg_escape(s); | |
217 | assert_se(b); | |
218 | assert_se(streq(b, r)); | |
219 | ||
220 | assert_se(streq(cg_unescape(b), s)); | |
221 | } | |
222 | ||
223 | static void test_escape(void) { | |
224 | test_escape_one("foobar", "foobar"); | |
a0ab5665 | 225 | test_escape_one(".foobar", "_.foobar"); |
ae018d9b LP |
226 | test_escape_one("foobar.service", "foobar.service"); |
227 | test_escape_one("cgroup.service", "_cgroup.service"); | |
ae018d9b | 228 | test_escape_one("tasks", "_tasks"); |
e13bb5d2 KS |
229 | if (access("/sys/fs/cgroup/cpu", F_OK) == 0) |
230 | test_escape_one("cpu.service", "_cpu.service"); | |
ae018d9b | 231 | test_escape_one("_foobar", "__foobar"); |
a0ab5665 LP |
232 | test_escape_one("", "_"); |
233 | test_escape_one("_", "__"); | |
234 | test_escape_one(".", "_."); | |
ae018d9b LP |
235 | } |
236 | ||
78edb35a | 237 | static void test_controller_is_valid(void) { |
185a0874 DJL |
238 | assert_se(cg_controller_is_valid("foobar")); |
239 | assert_se(cg_controller_is_valid("foo_bar")); | |
240 | assert_se(cg_controller_is_valid("name=foo")); | |
241 | assert_se(!cg_controller_is_valid("")); | |
242 | assert_se(!cg_controller_is_valid("name=")); | |
243 | assert_se(!cg_controller_is_valid("=")); | |
244 | assert_se(!cg_controller_is_valid("cpu,cpuacct")); | |
245 | assert_se(!cg_controller_is_valid("_")); | |
246 | assert_se(!cg_controller_is_valid("_foobar")); | |
247 | assert_se(!cg_controller_is_valid("tatü")); | |
78edb35a LP |
248 | } |
249 | ||
a016b922 LP |
250 | static void test_slice_to_path_one(const char *unit, const char *path, int error) { |
251 | _cleanup_free_ char *ret = NULL; | |
0682ed5c ZJS |
252 | int r; |
253 | ||
254 | log_info("unit: %s", unit); | |
a016b922 | 255 | |
0682ed5c ZJS |
256 | r = cg_slice_to_path(unit, &ret); |
257 | log_info("actual: %s / %d", strnull(ret), r); | |
258 | log_info("expect: %s / %d", strnull(path), error); | |
259 | assert_se(r == error); | |
a016b922 LP |
260 | assert_se(streq_ptr(ret, path)); |
261 | } | |
262 | ||
263 | static void test_slice_to_path(void) { | |
a016b922 LP |
264 | test_slice_to_path_one("foobar.slice", "foobar.slice", 0); |
265 | test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0); | |
266 | test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL); | |
e5af6e0e | 267 | test_slice_to_path_one(SPECIAL_ROOT_SLICE, "", 0); |
c96cc582 LP |
268 | test_slice_to_path_one("--.slice", NULL, -EINVAL); |
269 | test_slice_to_path_one("-", NULL, -EINVAL); | |
a016b922 LP |
270 | test_slice_to_path_one("-foo-.slice", NULL, -EINVAL); |
271 | test_slice_to_path_one("-foo.slice", NULL, -EINVAL); | |
c96cc582 | 272 | test_slice_to_path_one("foo-.slice", NULL, -EINVAL); |
e66e5b61 | 273 | test_slice_to_path_one("foo--bar.slice", NULL, -EINVAL); |
c96cc582 | 274 | test_slice_to_path_one("foo.slice/foo--bar.slice", NULL, -EINVAL); |
a016b922 LP |
275 | test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0); |
276 | 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 |
277 | |
278 | test_slice_to_path_one("foobar@.slice", NULL, -EINVAL); | |
279 | test_slice_to_path_one("foobar@waldo.slice", NULL, -EINVAL); | |
280 | test_slice_to_path_one("foobar@waldo.service", NULL, -EINVAL); | |
281 | test_slice_to_path_one("-foo@-.slice", NULL, -EINVAL); | |
282 | test_slice_to_path_one("-foo@.slice", NULL, -EINVAL); | |
283 | test_slice_to_path_one("foo@-.slice", NULL, -EINVAL); | |
284 | test_slice_to_path_one("foo@@bar.slice", NULL, -EINVAL); | |
285 | test_slice_to_path_one("foo.slice/foo@@bar.slice", NULL, -EINVAL); | |
a016b922 LP |
286 | } |
287 | ||
751bc6ac LP |
288 | static 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 | ||
295 | static 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"); | |
a2917d3d | 300 | test_shift_path_one("/foobar/waldo", "/hogehoge", "/foobar/waldo"); |
751bc6ac LP |
301 | } |
302 | ||
5f4c5fef LP |
303 | static 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 |
314 | static 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 | ||
322 | static 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 | ||
073e8f09 | 336 | static void test_cg_tests(void) { |
05d69e02 | 337 | int all, hybrid, systemd, r; |
073e8f09 | 338 | |
d4d99bc6 | 339 | r = cg_unified(); |
05d69e02 | 340 | if (r == -ENOMEDIUM) { |
b66789a9 | 341 | log_tests_skipped("cgroup not mounted"); |
05d69e02 ZJS |
342 | return; |
343 | } | |
d4d99bc6 | 344 | assert_se(r >= 0); |
073e8f09 ZJS |
345 | |
346 | all = cg_all_unified(); | |
347 | assert_se(IN_SET(all, 0, 1)); | |
348 | ||
349 | hybrid = cg_hybrid_unified(); | |
350 | assert_se(IN_SET(hybrid, 0, 1)); | |
351 | ||
352 | systemd = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER); | |
353 | assert_se(IN_SET(systemd, 0, 1)); | |
354 | ||
355 | if (all) { | |
356 | assert_se(systemd); | |
357 | assert_se(!hybrid); | |
358 | ||
359 | } else if (hybrid) { | |
360 | assert_se(systemd); | |
361 | assert_se(!all); | |
362 | ||
363 | } else | |
364 | assert_se(!systemd); | |
365 | } | |
366 | ||
9177fa9f ZJS |
367 | static void test_cg_get_keyed_attribute(void) { |
368 | _cleanup_free_ char *val = NULL; | |
369 | char *vals3[3] = {}, *vals3a[3] = {}; | |
18ce247c | 370 | int i, r; |
9177fa9f | 371 | |
18ce247c | 372 | r = cg_get_keyed_attribute("cpu", "/init.scope", "no_such_file", STRV_MAKE("no_such_attr"), &val); |
3c14dc61 | 373 | if (r == -ENOMEDIUM || ERRNO_IS_PRIVILEGE(r)) { |
18ce247c ZJS |
374 | log_info_errno(r, "Skipping most of %s, /sys/fs/cgroup not accessible: %m", __func__); |
375 | return; | |
376 | } | |
377 | ||
378 | assert_se(r == -ENOENT); | |
9177fa9f ZJS |
379 | assert_se(val == NULL); |
380 | ||
381 | if (access("/sys/fs/cgroup/init.scope/cpu.stat", R_OK) < 0) { | |
382 | log_info_errno(errno, "Skipping most of %s, /init.scope/cpu.stat not accessible: %m", __func__); | |
383 | return; | |
384 | } | |
385 | ||
386 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val) == -ENXIO); | |
25a1f04c | 387 | assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val) == 0); |
9177fa9f ZJS |
388 | assert_se(val == NULL); |
389 | ||
390 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val) == 0); | |
111e71c4 | 391 | val = mfree(val); |
25a1f04c MS |
392 | |
393 | assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val) == 1); | |
9177fa9f ZJS |
394 | log_info("cpu /init.scope cpu.stat [usage_usec] → \"%s\"", val); |
395 | ||
396 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3) == -ENXIO); | |
25a1f04c | 397 | assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3) == 1); |
f21b863e | 398 | assert_se(vals3[0] && !vals3[1]); |
25a1f04c | 399 | free(vals3[0]); |
9177fa9f ZJS |
400 | |
401 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "usage_usec"), vals3) == -ENXIO); | |
25a1f04c | 402 | assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "usage_usec"), vals3) == 1); |
f21b863e | 403 | assert_se(vals3[0] && !vals3[1]); |
25a1f04c | 404 | free(vals3[0]); |
9177fa9f ZJS |
405 | |
406 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", | |
407 | STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3) == 0); | |
25a1f04c MS |
408 | for (i = 0; i < 3; i++) |
409 | free(vals3[i]); | |
410 | ||
411 | assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", | |
412 | STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3) == 3); | |
9177fa9f ZJS |
413 | log_info("cpu /init.scope cpu.stat [usage_usec user_usec system_usec] → \"%s\", \"%s\", \"%s\"", |
414 | vals3[0], vals3[1], vals3[2]); | |
415 | ||
416 | assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", | |
417 | STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a) == 0); | |
25a1f04c MS |
418 | for (i = 0; i < 3; i++) |
419 | free(vals3a[i]); | |
420 | ||
421 | assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", | |
422 | STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a) == 3); | |
9177fa9f ZJS |
423 | log_info("cpu /init.scope cpu.stat [system_usec user_usec usage_usec] → \"%s\", \"%s\", \"%s\"", |
424 | vals3a[0], vals3a[1], vals3a[2]); | |
425 | ||
426 | for (i = 0; i < 3; i++) { | |
427 | free(vals3[i]); | |
428 | free(vals3a[i]); | |
429 | } | |
430 | } | |
431 | ||
96cde13a | 432 | int main(void) { |
6d7c4033 | 433 | test_setup_logging(LOG_DEBUG); |
0a05dcc0 | 434 | |
7027ff61 | 435 | test_path_decode_unit(); |
6c03089c | 436 | test_path_get_unit(); |
7027ff61 | 437 | test_path_get_user_unit(); |
9444b1f2 LP |
438 | test_path_get_session(); |
439 | test_path_get_owner_uid(); | |
8b0849e9 | 440 | test_path_get_slice(); |
329ac4bc | 441 | test_path_get_user_slice(); |
143bfdaf | 442 | TEST_REQ_RUNNING_SYSTEMD(test_get_paths()); |
aff38e74 | 443 | test_proc(); |
143bfdaf | 444 | TEST_REQ_RUNNING_SYSTEMD(test_escape()); |
78edb35a | 445 | test_controller_is_valid(); |
a016b922 | 446 | test_slice_to_path(); |
751bc6ac | 447 | test_shift_path(); |
b3a7ba89 | 448 | TEST_REQ_RUNNING_SYSTEMD(test_mask_supported()); |
f0bef277 EV |
449 | TEST_REQ_RUNNING_SYSTEMD(test_is_cgroup_fs()); |
450 | TEST_REQ_RUNNING_SYSTEMD(test_fd_is_cgroup_fs()); | |
073e8f09 | 451 | test_cg_tests(); |
9177fa9f | 452 | test_cg_get_keyed_attribute(); |
96cde13a ZJS |
453 | |
454 | return 0; | |
455 | } |