• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14815796853

02 May 2025 11:41AM UTC coverage: 72.24% (-0.003%) from 72.243%
14815796853

push

github

web-flow
Various changes to prepare for running IWYU on the repository (#37319)

These are various commits that were required to get things compiling
after running IWYU. I think all of them make sense on their own, hence
this split PR to merge them ahead of time.

81 of 96 new or added lines in 48 files covered. (84.38%)

209 existing lines in 39 files now uncovered.

297219 of 411432 relevant lines covered (72.24%)

693693.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.54
/src/test/test-cgroup-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "alloc-util.h"
4
#include "cgroup-util.h"
5
#include "dirent-util.h"
6
#include "errno-util.h"
7
#include "fd-util.h"
8
#include "format-util.h"
9
#include "parse-util.h"
10
#include "proc-cmdline.h"
11
#include "process-util.h"
12
#include "special.h"
13
#include "stat-util.h"
14
#include "string-util.h"
15
#include "strv.h"
16
#include "tests.h"
17
#include "user-util.h"
18

19
static void check_p_d_u(const char *path, int code, const char *result) {
9✔
20
        _cleanup_free_ char *unit = NULL;
18✔
21
        int r;
9✔
22

23
        r = cg_path_decode_unit(path, &unit);
9✔
24
        printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code);
13✔
25
        assert_se(r == code);
9✔
26
        ASSERT_STREQ(unit, result);
9✔
27
}
9✔
28

29
TEST(path_decode_unit) {
1✔
30
        check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
1✔
31
        check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
1✔
32
        check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service");
1✔
33
        check_p_d_u("getty@.service/", -ENXIO, NULL);
1✔
34
        check_p_d_u("getty@.service", -ENXIO, NULL);
1✔
35
        check_p_d_u("getty.service", 0, "getty.service");
1✔
36
        check_p_d_u("getty", -ENXIO, NULL);
1✔
37
        check_p_d_u("getty/waldo", -ENXIO, NULL);
1✔
38
        check_p_d_u("_cpu.service", 0, "cpu.service");
1✔
39
}
1✔
40

41
static void check_p_g_u(const char *path, int code, const char *result) {
11✔
42
        _cleanup_free_ char *unit = NULL;
22✔
43
        int r;
11✔
44

45
        r = cg_path_get_unit(path, &unit);
11✔
46
        printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code);
14✔
47
        assert_se(r == code);
11✔
48
        ASSERT_STREQ(unit, result);
11✔
49
}
11✔
50

51
TEST(path_get_unit) {
1✔
52
        check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
1✔
53
        check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
1✔
54
        check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
1✔
55
        check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
1✔
56
        check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
1✔
57
        check_p_g_u("sadfdsafsda", -ENXIO, NULL);
1✔
58
        check_p_g_u("/system.slice/getty####@tty6.service/xxx", -ENXIO, NULL);
1✔
59
        check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
1✔
60
        check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
1✔
61
        check_p_g_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "user@1000.service");
1✔
62
        check_p_g_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL);
1✔
63
}
1✔
64

65
static void check_p_g_u_p(const char *path, int code, const char *result) {
14✔
66
        _cleanup_free_ char *unit_path = NULL;
28✔
67
        int r;
14✔
68

69
        r = cg_path_get_unit_path(path, &unit_path);
14✔
70
        printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit_path, r, strnull(result), code);
17✔
71
        assert_se(r == code);
14✔
72
        ASSERT_STREQ(unit_path, result);
14✔
73
}
14✔
74

75
TEST(path_get_unit_path) {
1✔
76
        check_p_g_u_p("/system.slice/foobar.service/sdfdsaf", 0, "/system.slice/foobar.service");
1✔
77
        check_p_g_u_p("/system.slice/getty@tty5.service", 0, "/system.slice/getty@tty5.service");
1✔
78
        check_p_g_u_p("/system.slice/getty@tty5.service/aaa/bbb", 0, "/system.slice/getty@tty5.service");
1✔
79
        check_p_g_u_p("/system.slice/getty@tty5.service/", 0, "/system.slice/getty@tty5.service");
1✔
80
        check_p_g_u_p("/system.slice/getty@tty6.service/tty5", 0, "/system.slice/getty@tty6.service");
1✔
81
        check_p_g_u_p("sadfdsafsda", -ENXIO, NULL);
1✔
82
        check_p_g_u_p("/system.slice/getty####@tty6.service/xxx", -ENXIO, NULL);
1✔
83
        check_p_g_u_p("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "/system.slice/system-waldo.slice/foobar.service");
1✔
84
        check_p_g_u_p("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "/system.slice/system-waldo.slice/_cpu.service");
1✔
85
        check_p_g_u_p("/system.slice/system-waldo.slice/_cpu.service", 0, "/system.slice/system-waldo.slice/_cpu.service");
1✔
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");
1✔
87
        check_p_g_u_p("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL);
1✔
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");
1✔
89
        check_p_g_u_p("/_session-2.scope/_foobar@pie.service/pa/po", 0, "/_session-2.scope");
1✔
90
}
1✔
91

92
static void check_p_g_u_u(const char *path, int code, const char *result) {
16✔
93
        _cleanup_free_ char *unit = NULL;
32✔
94
        int r;
16✔
95

96
        r = cg_path_get_user_unit(path, &unit);
16✔
97
        printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code);
20✔
98
        assert_se(r == code);
16✔
99
        ASSERT_STREQ(unit, result);
16✔
100
}
16✔
101

102
TEST(path_get_user_unit) {
1✔
103
        check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service");
1✔
104
        check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service");
1✔
105
        check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
1✔
106
        check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
1✔
107
        check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -ENXIO, NULL);
1✔
108
        check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
1✔
109
        check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
1✔
110
        check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
1✔
111
        check_p_g_u_u("/meh.service", -ENXIO, NULL);
1✔
112
        check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
1✔
113
        check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/server.service", 0, "server.service");
1✔
114
        check_p_g_u_u("/user.slice/user-1000.slice/user@1000.service/foobar.slice/foobar@pie.service", 0, "foobar@pie.service");
1✔
115
        check_p_g_u_u("/user.slice/user-1000.slice/user@.service/server.service", -ENXIO, NULL);
1✔
116
        check_p_g_u_u("/capsule.slice/capsule@test.service/app.slice/run-p9-i1.service", 0, "run-p9-i1.service");
1✔
117
        check_p_g_u_u("/capsule.slice/capsule@usr-joe.service/foo.slice/foo-bar.slice/run-p9-i1.service", 0, "run-p9-i1.service");
1✔
118
        check_p_g_u_u("/capsule.slice/capsule@#.service/foo.slice/foo-bar.slice/run-p9-i1.service", -ENXIO, NULL);
1✔
119
}
1✔
120

121
static void check_p_g_s(const char *path, int code, const char *result) {
4✔
122
        _cleanup_free_ char *s = NULL;
8✔
123

124
        assert_se(cg_path_get_session(path, &s) == code);
4✔
125
        ASSERT_STREQ(s, result);
4✔
126
}
4✔
127

128
TEST(path_get_session) {
1✔
129
        check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2");
1✔
130
        check_p_g_s("/session-3.scope", 0, "3");
1✔
131
        check_p_g_s("/session-.scope", -ENXIO, NULL);
1✔
132
        check_p_g_s("", -ENXIO, NULL);
1✔
133
}
1✔
134

135
static void check_p_g_o_u(const char *path, int code, uid_t result) {
3✔
136
        uid_t uid = 0;
3✔
137

138
        assert_se(cg_path_get_owner_uid(path, &uid) == code);
3✔
139
        assert_se(uid == result);
3✔
140
}
3✔
141

142
TEST(path_get_owner_uid) {
1✔
143
        check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000);
1✔
144
        check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006);
1✔
145
        check_p_g_o_u("", -ENXIO, 0);
1✔
146
}
1✔
147

148
static void check_p_g_slice(const char *path, int code, const char *result) {
8✔
149
        _cleanup_free_ char *s = NULL;
16✔
150

151
        assert_se(cg_path_get_slice(path, &s) == code);
8✔
152
        ASSERT_STREQ(s, result);
8✔
153
}
8✔
154

155
TEST(path_get_slice) {
1✔
156
        check_p_g_slice("/user.slice", 0, "user.slice");
1✔
157
        check_p_g_slice("/foobar", 0, SPECIAL_ROOT_SLICE);
1✔
158
        check_p_g_slice("/user.slice/user-waldo.slice", 0, "user-waldo.slice");
1✔
159
        check_p_g_slice("", 0, SPECIAL_ROOT_SLICE);
1✔
160
        check_p_g_slice("foobar", 0, SPECIAL_ROOT_SLICE);
1✔
161
        check_p_g_slice("foobar.slice", 0, "foobar.slice");
1✔
162
        check_p_g_slice("foo.slice/foo-bar.slice/waldo.service", 0, "foo-bar.slice");
1✔
163
        check_p_g_slice("/capsule.slice/capsule@test.service/app.slice/run-p9-i1.service", 0, "capsule.slice");
1✔
164
}
1✔
165

166
static void check_p_g_u_slice(const char *path, int code, const char *result) {
16✔
167
        _cleanup_free_ char *s = NULL;
32✔
168

169
        assert_se(cg_path_get_user_slice(path, &s) == code);
16✔
170
        ASSERT_STREQ(s, result);
16✔
171
}
16✔
172

173
TEST(path_get_user_slice) {
1✔
174
        check_p_g_u_slice("/user.slice", -ENXIO, NULL);
1✔
175
        check_p_g_u_slice("/foobar", -ENXIO, NULL);
1✔
176
        check_p_g_u_slice("/user.slice/user-waldo.slice", -ENXIO, NULL);
1✔
177
        check_p_g_u_slice("", -ENXIO, NULL);
1✔
178
        check_p_g_u_slice("foobar", -ENXIO, NULL);
1✔
179
        check_p_g_u_slice("foobar.slice", -ENXIO, NULL);
1✔
180
        check_p_g_u_slice("foo.slice/foo-bar.slice/waldo.service", -ENXIO, NULL);
1✔
181

182
        check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service", 0, SPECIAL_ROOT_SLICE);
1✔
183
        check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/", 0, SPECIAL_ROOT_SLICE);
1✔
184
        check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service///", 0, SPECIAL_ROOT_SLICE);
1✔
185
        check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/waldo.service", 0, SPECIAL_ROOT_SLICE);
1✔
186
        check_p_g_u_slice("foo.slice/foo-bar.slice/user@1000.service/piep.slice/foo.service", 0, "piep.slice");
1✔
187
        check_p_g_u_slice("/foo.slice//foo-bar.slice/user@1000.service/piep.slice//piep-pap.slice//foo.service", 0, "piep-pap.slice");
1✔
188

189
        check_p_g_u_slice("/capsule.slice/capsule@test.service/app.slice/run-p9-i1.service", 0, "app.slice");
1✔
190
        check_p_g_u_slice("/capsule.slice/capsule@usr-joe.service/app.slice/run-p9-i1.service", 0, "app.slice");
1✔
191
        check_p_g_u_slice("/capsule.slice/capsule@usr-joe.service/foo.slice/foo-bar.slice/run-p9-i1.service", 0, "foo-bar.slice");
1✔
192
}
1✔
193

194
TEST(get_paths, .sd_booted = true) {
1✔
195
        _cleanup_free_ char *a = NULL;
2✔
196

197
        assert_se(cg_get_root_path(&a) >= 0);
1✔
198
        log_info("Root = %s", a);
1✔
199
}
1✔
200

201
static inline bool hidden_cgroup(const char *p) {
36✔
202
        assert(p);
36✔
203

204
        /* Consider top-level cgroup hidden from us */
205
        return p[0] == '/' && p[strspn(p, "/")] == '.';
36✔
206
}
207

208
TEST(proc, .sd_booted = true) {
1✔
209
        _cleanup_closedir_ DIR *d = NULL;
2✔
210
        int r;
1✔
211

212
        ASSERT_OK(proc_dir_open(&d));
1✔
213

214
        for (;;) {
37✔
215
                _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL;
37✔
216
                _cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
1✔
217
                uid_t uid = UID_INVALID;
37✔
218

219
                ASSERT_OK(r = proc_dir_read_pidref(d, &pid));
37✔
220
                if (r == 0)
37✔
221
                        break;
222

223
                if (pidref_is_kernel_thread(&pid) != 0)
36✔
224
                        continue;
×
225

226
                r = cg_pidref_get_path(SYSTEMD_CGROUP_CONTROLLER, &pid, &path);
36✔
227
                if (r == -ESRCH)
36✔
228
                        continue;
×
229
                ASSERT_OK(r);
36✔
230

231
                /* Test may run in a container with supervising/monitor processes that don't belong to our
232
                 * cgroup tree (slices/leaves) */
233
                if (hidden_cgroup(path))
36✔
234
                        continue;
×
235

236
                r = cg_pid_get_path_shifted(pid.pid, NULL, &path_shifted);
36✔
237
                if (r != -ESRCH)
36✔
238
                        ASSERT_OK(r);
36✔
239
                r = cg_pidref_get_unit(&pid, &unit);
36✔
240
                if (r != -ESRCH)
36✔
241
                        ASSERT_OK(r);
36✔
242
                r = cg_pid_get_slice(pid.pid, &slice);
36✔
243
                if (r != -ESRCH)
36✔
244
                        ASSERT_OK(r);
36✔
245

246
                /* Not all processes belong to a specific user or a machine */
247
                r = cg_pidref_get_owner_uid(&pid, &uid);
36✔
248
                if (!IN_SET(r, -ESRCH, -ENXIO))
36✔
249
                        ASSERT_OK(r);
5✔
250
                r = cg_pidref_get_session(&pid, &session);
36✔
251
                if (!IN_SET(r, -ESRCH, -ENXIO))
36✔
UNCOV
252
                        ASSERT_OK(r);
×
253
                r = cg_pid_get_user_unit(pid.pid, &user_unit);
36✔
254
                if (!IN_SET(r, -ESRCH, -ENXIO))
36✔
255
                        ASSERT_OK(r);
5✔
256
                r = cg_pid_get_machine_name(pid.pid, &machine);
36✔
257
                if (!IN_SET(r, -ESRCH, -ENOENT))
36✔
258
                        ASSERT_OK(r);
×
259

260
                log_debug(PID_FMT": %s, %s, "UID_FMT", %s, %s, %s, %s, %s",
139✔
261
                          pid.pid,
262
                          path,
263
                          strna(path_shifted),
264
                          uid,
265
                          strna(session),
266
                          strna(unit),
267
                          strna(user_unit),
268
                          strna(machine),
269
                          strna(slice));
270
        }
271
}
1✔
272

273
static void test_escape_one(const char *s, const char *expected) {
9✔
274
        _cleanup_free_ char *b = NULL;
18✔
275

276
        assert_se(s);
9✔
277
        assert_se(expected);
9✔
278

279
        ASSERT_OK(cg_escape(s, &b));
9✔
280
        ASSERT_STREQ(b, expected);
9✔
281

282
        ASSERT_STREQ(cg_unescape(b), s);
9✔
283

284
        assert_se(filename_is_valid(b));
9✔
285
        assert_se(!cg_needs_escape(s) || b[0] == '_');
9✔
286
}
9✔
287

288
TEST(escape, .sd_booted = true) {
1✔
289
        test_escape_one("foobar", "foobar");
1✔
290
        test_escape_one(".foobar", "_.foobar");
1✔
291
        test_escape_one("foobar.service", "foobar.service");
1✔
292
        test_escape_one("cgroup.service", "_cgroup.service");
1✔
293
        test_escape_one("tasks", "_tasks");
1✔
294
        if (access("/sys/fs/cgroup/cpu", F_OK) == 0)
1✔
295
                test_escape_one("cpu.service", "_cpu.service");
×
296
        test_escape_one("_foobar", "__foobar");
1✔
297
        test_escape_one("", "_");
1✔
298
        test_escape_one("_", "__");
1✔
299
        test_escape_one(".", "_.");
1✔
300
}
1✔
301

302
TEST(controller_is_valid) {
1✔
303
        assert_se(cg_controller_is_valid("foobar"));
1✔
304
        assert_se(cg_controller_is_valid("foo_bar"));
1✔
305
        assert_se(cg_controller_is_valid("name=foo"));
1✔
306
        assert_se(!cg_controller_is_valid(""));
1✔
307
        assert_se(!cg_controller_is_valid("name="));
1✔
308
        assert_se(!cg_controller_is_valid("="));
1✔
309
        assert_se(!cg_controller_is_valid("cpu,cpuacct"));
1✔
310
        assert_se(!cg_controller_is_valid("_"));
1✔
311
        assert_se(!cg_controller_is_valid("_foobar"));
1✔
312
        assert_se(!cg_controller_is_valid("tatü"));
1✔
313
}
1✔
314

315
static void test_slice_to_path_one(const char *unit, const char *path, int error) {
21✔
316
        _cleanup_free_ char *ret = NULL;
42✔
317
        int r;
21✔
318

319
        log_info("unit: %s", unit);
21✔
320

321
        r = cg_slice_to_path(unit, &ret);
21✔
322
        log_info("actual: %s / %d", strnull(ret), r);
37✔
323
        log_info("expect: %s / %d", strnull(path), error);
42✔
324
        assert_se(r == error);
21✔
325
        ASSERT_STREQ(ret, path);
21✔
326
}
21✔
327

328
TEST(slice_to_path) {
1✔
329
        test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
1✔
330
        test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
1✔
331
        test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL);
1✔
332
        test_slice_to_path_one(SPECIAL_ROOT_SLICE, "", 0);
1✔
333
        test_slice_to_path_one("--.slice", NULL, -EINVAL);
1✔
334
        test_slice_to_path_one("-", NULL, -EINVAL);
1✔
335
        test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
1✔
336
        test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
1✔
337
        test_slice_to_path_one("foo-.slice", NULL, -EINVAL);
1✔
338
        test_slice_to_path_one("foo--bar.slice", NULL, -EINVAL);
1✔
339
        test_slice_to_path_one("foo.slice/foo--bar.slice", NULL, -EINVAL);
1✔
340
        test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
1✔
341
        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);
1✔
342

343
        test_slice_to_path_one("foobar@.slice", NULL, -EINVAL);
1✔
344
        test_slice_to_path_one("foobar@waldo.slice", NULL, -EINVAL);
1✔
345
        test_slice_to_path_one("foobar@waldo.service", NULL, -EINVAL);
1✔
346
        test_slice_to_path_one("-foo@-.slice", NULL, -EINVAL);
1✔
347
        test_slice_to_path_one("-foo@.slice", NULL, -EINVAL);
1✔
348
        test_slice_to_path_one("foo@-.slice", NULL, -EINVAL);
1✔
349
        test_slice_to_path_one("foo@@bar.slice", NULL, -EINVAL);
1✔
350
        test_slice_to_path_one("foo.slice/foo@@bar.slice", NULL, -EINVAL);
1✔
351
}
1✔
352

353
static void test_shift_path_one(const char *raw, const char *root, const char *shifted) {
4✔
354
        const char *s = NULL;
4✔
355

356
        ASSERT_OK(cg_shift_path(raw, root, &s));
4✔
357
        ASSERT_STREQ(s, shifted);
4✔
358
}
4✔
359

360
TEST(shift_path) {
1✔
361
        test_shift_path_one("/foobar/waldo", "/", "/foobar/waldo");
1✔
362
        test_shift_path_one("/foobar/waldo", "", "/foobar/waldo");
1✔
363
        test_shift_path_one("/foobar/waldo", "/foobar", "/waldo");
1✔
364
        test_shift_path_one("/foobar/waldo", "/hogehoge", "/foobar/waldo");
1✔
365
}
1✔
366

367
TEST(mask_supported, .sd_booted = true) {
1✔
368
        CGroupMask m;
1✔
369

370
        ASSERT_OK(cg_mask_supported(&m));
1✔
371

372
        for (CGroupController c = 0; c < _CGROUP_CONTROLLER_MAX; c++)
14✔
373
                printf("'%s' is supported: %s\n",
13✔
374
                       cgroup_controller_to_string(c),
375
                       yes_no(m & CGROUP_CONTROLLER_TO_MASK(c)));
13✔
376
}
1✔
377

378
TEST(cg_tests) {
1✔
379
        int all, hybrid, systemd, r;
1✔
380

381
        r = cg_unified();
1✔
382
        if (IN_SET(r, -ENOENT, -ENOMEDIUM)) {
1✔
383
                log_tests_skipped("cgroup not mounted");
×
384
                return;
×
385
        }
386
        assert_se(r >= 0);
1✔
387

388
        all = cg_all_unified();
1✔
389
        assert_se(IN_SET(all, 0, 1));
1✔
390

391
        hybrid = cg_hybrid_unified();
1✔
392
        assert_se(IN_SET(hybrid, 0, 1));
1✔
393

394
        systemd = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER);
1✔
395
        assert_se(IN_SET(systemd, 0, 1));
1✔
396

397
        if (all) {
1✔
398
                assert_se(systemd);
1✔
399
                assert_se(!hybrid);
1✔
400

401
        } else if (hybrid) {
×
402
                assert_se(systemd);
×
403
                assert_se(!all);
404

405
        } else
406
                assert_se(!systemd);
×
407
}
408

409
TEST(cg_get_keyed_attribute) {
1✔
410
        _cleanup_free_ char *val = NULL;
1✔
411
        char *vals3[3] = {}, *vals3a[3] = {};
1✔
412
        int r;
1✔
413

414
        r = cg_get_keyed_attribute("cpu", "/init.scope", "no_such_file", STRV_MAKE("no_such_attr"), &val);
1✔
415
        if (IN_SET(r, -ENOMEDIUM, -ENOENT) || ERRNO_IS_PRIVILEGE(r)) {
1✔
416
                log_info_errno(r, "Skipping most of %s, /sys/fs/cgroup not accessible: %m", __func__);
1✔
417
                return;
1✔
418
        }
419

420
        assert_se(r == -ENOENT);
×
421
        ASSERT_NULL(val);
×
422

423
        if (access("/sys/fs/cgroup/init.scope/cpu.stat", R_OK) < 0) {
×
424
                log_info_errno(errno, "Skipping most of %s, /init.scope/cpu.stat not accessible: %m", __func__);
×
425
                return;
×
426
        }
427

428
        assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val) == -ENXIO);
×
429
        assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val) == 0);
×
430
        ASSERT_NULL(val);
×
431

432
        assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val) == 0);
×
433
        val = mfree(val);
×
434

435
        assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val) == 1);
×
436
        log_info("cpu /init.scope cpu.stat [usage_usec] → \"%s\"", val);
×
437

438
        assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3) == -ENXIO);
×
439
        assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3) == 1);
×
440
        assert_se(vals3[0] && !vals3[1]);
×
441
        free(vals3[0]);
×
442

443
        assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "usage_usec"), vals3) == -ENXIO);
×
444
        assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "usage_usec"), vals3) == 1);
×
445
        assert_se(vals3[0] && !vals3[1]);
×
446
        free(vals3[0]);
×
447

448
        assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat",
×
449
                                         STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3) == 0);
450
        for (size_t i = 0; i < 3; i++)
×
451
                free(vals3[i]);
×
452

453
        assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat",
×
454
                                         STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3) == 3);
455
        log_info("cpu /init.scope cpu.stat [usage_usec user_usec system_usec] → \"%s\", \"%s\", \"%s\"",
×
456
                 vals3[0], vals3[1], vals3[2]);
457

458
        assert_se(cg_get_keyed_attribute("cpu", "/init.scope", "cpu.stat",
×
459
                                         STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a) == 0);
460
        for (size_t i = 0; i < 3; i++)
×
461
                free(vals3a[i]);
×
462

463
        assert_se(cg_get_keyed_attribute_graceful("cpu", "/init.scope", "cpu.stat",
×
464
                                         STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a) == 3);
465
        log_info("cpu /init.scope cpu.stat [system_usec user_usec usage_usec] → \"%s\", \"%s\", \"%s\"",
×
466
                 vals3a[0], vals3a[1], vals3a[2]);
467

468
        for (size_t i = 0; i < 3; i++) {
×
469
                free(vals3[i]);
×
470
                free(vals3a[i]);
×
471
        }
472
}
473

474
TEST(bfq_weight_conversion) {
1✔
475
        assert_se(BFQ_WEIGHT(1) == 1);
1✔
476
        assert_se(BFQ_WEIGHT(50) == 50);
1✔
477
        assert_se(BFQ_WEIGHT(100) == 100);
1✔
478
        assert_se(BFQ_WEIGHT(500) == 136);
1✔
479
        assert_se(BFQ_WEIGHT(5000) == 545);
1✔
480
        assert_se(BFQ_WEIGHT(10000) == 1000);
1✔
481
}
1✔
482

483
DEFINE_TEST_MAIN(LOG_DEBUG);
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc