| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "cgroup-util.h" |
| 7 |
#include "errno-util.h" |
| 8 |
#include "fd-util.h" |
| 9 |
#include "format-util.h" |
| 10 |
#include "mountpoint-util.h" |
| 11 |
#include "path-util.h" |
| 12 |
#include "pidref.h" |
| 13 |
#include "process-util.h" |
| 14 |
#include "special.h" |
| 15 |
#include "stat-util.h" |
| 16 |
#include "string-util.h" |
| 17 |
#include "tests.h" |
| 18 |
|
| 19 |
static void check_p_d_u(const char *path, int code, const char *result) { |
| 20 |
_cleanup_free_ char *unit = NULL; |
| 21 |
int r; |
| 22 |
|
| 23 |
r = cg_path_decode_unit(path, &unit); |
| 24 |
printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code); |
| 25 |
assert_se(r == code); |
| 26 |
ASSERT_STREQ(unit, result); |
| 27 |
} |
| 28 |
|
| 29 |
TEST(path_decode_unit) { |
| 30 |
check_p_d_u("[email protected]", 0, "[email protected]"); |
| 31 |
check_p_d_u("[email protected]/", 0, "[email protected]"); |
| 32 |
check_p_d_u("[email protected]/xxx", 0, "[email protected]"); |
| 33 |
check_p_d_u("[email protected]/", -ENXIO, NULL); |
| 34 |
check_p_d_u("[email protected]", -ENXIO, NULL); |
| 35 |
check_p_d_u("getty.service", 0, "getty.service"); |
| 36 |
check_p_d_u("getty", -ENXIO, NULL); |
| 37 |
check_p_d_u("getty/waldo", -ENXIO, NULL); |
| 38 |
check_p_d_u("_cpu.service", 0, "cpu.service"); |
| 39 |
} |
| 40 |
|
| 41 |
static void check_p_g_u(const char *path, int code, const char *result) { |
| 42 |
_cleanup_free_ char *unit = NULL; |
| 43 |
int r; |
| 44 |
|
| 45 |
r = cg_path_get_unit(path, &unit); |
| 46 |
printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code); |
| 47 |
assert_se(r == code); |
| 48 |
ASSERT_STREQ(unit, result); |
| 49 |
} |
| 50 |
|
| 51 |
TEST(path_get_unit) { |
| 52 |
check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service"); |
| 53 |
check_p_g_u("/system.slice/[email protected]", 0, "[email protected]"); |
| 54 |
check_p_g_u("/system.slice/[email protected]/aaa/bbb", 0, "[email protected]"); |
| 55 |
check_p_g_u("/system.slice/[email protected]/", 0, "[email protected]"); |
| 56 |
check_p_g_u("/system.slice/[email protected]/tty5", 0, "[email protected]"); |
| 57 |
check_p_g_u("sadfdsafsda", -ENXIO, NULL); |
| 58 |
check_p_g_u("/system.slice/getty####@tty6.service/xxx", -ENXIO, NULL); |
| 59 |
check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service"); |
| 60 |
check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service"); |
| 61 |
check_p_g_u("/user.slice/user-1000.slice/[email protected]/server.service", 0, "[email protected]"); |
| 62 |
check_p_g_u("/user.slice/user-1000.slice/[email protected]/server.service", -ENXIO, NULL); |
| 63 |
} |
| 64 |
|
| 65 |
static void check_p_g_u_f(const char *path, int expected_code, const char *expected_unit, const char *expected_subgroup) { |
| 66 |
_cleanup_free_ char *unit = NULL, *subgroup = NULL; |
| 67 |
int r; |
| 68 |
|
| 69 |
r = cg_path_get_unit_full(path, &unit, &subgroup); |
| 70 |
printf("%s: %s → %s %s %d expected %s %s %d\n", __func__, path, unit, subgroup, r, strnull(expected_unit), strnull(expected_subgroup), expected_code); |
| 71 |
ASSERT_EQ(r, expected_code); |
| 72 |
ASSERT_STREQ(unit, expected_unit); |
| 73 |
ASSERT_STREQ(subgroup, expected_subgroup); |
| 74 |
} |
| 75 |
|
| 76 |
TEST(path_get_unit_full) { |
| 77 |
check_p_g_u_f("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service", "sdfdsaf"); |
| 78 |
check_p_g_u_f("/system.slice/foobar.service//sdfdsaf", 0, "foobar.service", "sdfdsaf"); |
| 79 |
check_p_g_u_f("/system.slice/foobar.service/sdfdsaf/", 0, "foobar.service", "sdfdsaf"); |
| 80 |
check_p_g_u_f("/system.slice/foobar.service//sdfdsaf/", 0, "foobar.service", "sdfdsaf"); |
| 81 |
check_p_g_u_f("/system.slice/foobar.service//sdfdsaf//", 0, "foobar.service", "sdfdsaf"); |
| 82 |
check_p_g_u_f("/system.slice/foobar.service/sdfdsaf/urks", 0, "foobar.service", "sdfdsaf/urks"); |
| 83 |
check_p_g_u_f("/system.slice/foobar.service//sdfdsaf//urks", 0, "foobar.service", "sdfdsaf/urks"); |
| 84 |
check_p_g_u_f("/system.slice/foobar.service/sdfdsaf/urks/", 0, "foobar.service", "sdfdsaf/urks"); |
| 85 |
check_p_g_u_f("/system.slice/foobar.service//sdfdsaf//urks//", 0, "foobar.service", "sdfdsaf/urks"); |
| 86 |
check_p_g_u_f("/system.slice/foobar.service", 0, "foobar.service", NULL); |
| 87 |
check_p_g_u_f("/system.slice/foobar.service/", 0, "foobar.service", NULL); |
| 88 |
check_p_g_u_f("/system.slice/foobar.service//", 0, "foobar.service", NULL); |
| 89 |
check_p_g_u_f("/system.slice/", -ENXIO, NULL, NULL); |
| 90 |
check_p_g_u_f("/system.slice/piff", -ENXIO, NULL, NULL); |
| 91 |
check_p_g_u_f("/system.service/piff", 0, "system.service", "piff"); |
| 92 |
check_p_g_u_f("//system.service//piff", 0, "system.service", "piff"); |
| 93 |
} |
| 94 |
|
| 95 |
static void check_p_g_u_p(const char *path, int code, const char *result) { |
| 96 |
_cleanup_free_ char *unit_path = NULL; |
| 97 |
int r; |
| 98 |
|
| 99 |
r = cg_path_get_unit_path(path, &unit_path); |
| 100 |
printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit_path, r, strnull(result), code); |
| 101 |
assert_se(r == code); |
| 102 |
ASSERT_STREQ(unit_path, result); |
| 103 |
} |
| 104 |
|
| 105 |
TEST(path_get_unit_path) { |
| 106 |
check_p_g_u_p("/system.slice/foobar.service/sdfdsaf", 0, "/system.slice/foobar.service"); |
| 107 |
check_p_g_u_p("/system.slice/[email protected]", 0, "/system.slice/[email protected]"); |
| 108 |
check_p_g_u_p("/system.slice/[email protected]/aaa/bbb", 0, "/system.slice/[email protected]"); |
| 109 |
check_p_g_u_p("/system.slice/[email protected]/", 0, "/system.slice/[email protected]"); |
| 110 |
check_p_g_u_p("/system.slice/[email protected]/tty5", 0, "/system.slice/[email protected]"); |
| 111 |
check_p_g_u_p("sadfdsafsda", -ENXIO, NULL); |
| 112 |
check_p_g_u_p("/system.slice/getty####@tty6.service/xxx", -ENXIO, NULL); |
| 113 |
check_p_g_u_p("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "/system.slice/system-waldo.slice/foobar.service"); |
| 114 |
check_p_g_u_p("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "/system.slice/system-waldo.slice/_cpu.service"); |
| 115 |
check_p_g_u_p("/system.slice/system-waldo.slice/_cpu.service", 0, "/system.slice/system-waldo.slice/_cpu.service"); |
| 116 |
check_p_g_u_p("/user.slice/user-1000.slice/[email protected]/server.service", 0, "/user.slice/user-1000.slice/[email protected]"); |
| 117 |
check_p_g_u_p("/user.slice/user-1000.slice/[email protected]/server.service", -ENXIO, NULL); |
| 118 |
check_p_g_u_p("/user.slice/_user-1000.slice/[email protected]/foobar.slice/[email protected]", 0, "/user.slice/_user-1000.slice/[email protected]"); |
| 119 |
check_p_g_u_p("/_session-2.scope/[email protected]/pa/po", 0, "/_session-2.scope"); |
| 120 |
} |
| 121 |
|
| 122 |
static void check_p_g_u_u(const char *path, int code, const char *result) { |
| 123 |
_cleanup_free_ char *unit = NULL; |
| 124 |
int r; |
| 125 |
|
| 126 |
r = cg_path_get_user_unit(path, &unit); |
| 127 |
printf("%s: %s → %s %d expected %s %d\n", __func__, path, unit, r, strnull(result), code); |
| 128 |
assert_se(r == code); |
| 129 |
ASSERT_STREQ(unit, result); |
| 130 |
} |
| 131 |
|
| 132 |
TEST(path_get_user_unit) { |
| 133 |
check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "foobar.service"); |
| 134 |
check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo.slice/foobar.service", 0, "foobar.service"); |
| 135 |
check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service"); |
| 136 |
check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service"); |
| 137 |
check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -ENXIO, NULL); |
| 138 |
check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/[email protected]/pa/po", 0, "[email protected]"); |
| 139 |
check_p_g_u_u("/session-2.scope/[email protected]/pa/po", 0, "[email protected]"); |
| 140 |
check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/[email protected]/pa/po", 0, "[email protected]"); |
| 141 |
check_p_g_u_u("/meh.service", -ENXIO, NULL); |
| 142 |
check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service"); |
| 143 |
check_p_g_u_u("/user.slice/user-1000.slice/[email protected]/server.service", 0, "server.service"); |
| 144 |
check_p_g_u_u("/user.slice/user-1000.slice/[email protected]/foobar.slice/[email protected]", 0, "[email protected]"); |
| 145 |
check_p_g_u_u("/user.slice/user-1000.slice/[email protected]/server.service", -ENXIO, NULL); |
| 146 |
check_p_g_u_u("/capsule.slice/[email protected]/app.slice/run-p9-i1.service", 0, "run-p9-i1.service"); |
| 147 |
check_p_g_u_u("/capsule.slice/[email protected]/foo.slice/foo-bar.slice/run-p9-i1.service", 0, "run-p9-i1.service"); |
| 148 |
check_p_g_u_u("/capsule.slice/capsule@#.service/foo.slice/foo-bar.slice/run-p9-i1.service", -ENXIO, NULL); |
| 149 |
} |
| 150 |
|
| 151 |
static void check_p_g_s(const char *path, int code, const char *result) { |
| 152 |
_cleanup_free_ char *s = NULL; |
| 153 |
|
| 154 |
assert_se(cg_path_get_session(path, &s) == code); |
| 155 |
ASSERT_STREQ(s, result); |
| 156 |
} |
| 157 |
|
| 158 |
TEST(path_get_session) { |
| 159 |
check_p_g_s("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, "2"); |
| 160 |
check_p_g_s("/session-3.scope", 0, "3"); |
| 161 |
check_p_g_s("/session-.scope", -ENXIO, NULL); |
| 162 |
check_p_g_s("", -ENXIO, NULL); |
| 163 |
} |
| 164 |
|
| 165 |
static void check_p_g_o_u(const char *path, int code, uid_t result) { |
| 166 |
uid_t uid = 0; |
| 167 |
|
| 168 |
assert_se(cg_path_get_owner_uid(path, &uid) == code); |
| 169 |
assert_se(uid == result); |
| 170 |
} |
| 171 |
|
| 172 |
TEST(path_get_owner_uid) { |
| 173 |
check_p_g_o_u("/user.slice/user-1000.slice/session-2.scope/foobar.service", 0, 1000); |
| 174 |
check_p_g_o_u("/user.slice/user-1006.slice", 0, 1006); |
| 175 |
check_p_g_o_u("", -ENXIO, 0); |
| 176 |
} |
| 177 |
|
| 178 |
static void check_p_g_slice(const char *path, int code, const char *result) { |
| 179 |
_cleanup_free_ char *s = NULL; |
| 180 |
|
| 181 |
assert_se(cg_path_get_slice(path, &s) == code); |
| 182 |
ASSERT_STREQ(s, result); |
| 183 |
} |
| 184 |
|
| 185 |
TEST(path_get_slice) { |
| 186 |
check_p_g_slice("/user.slice", 0, "user.slice"); |
| 187 |
check_p_g_slice("/foobar", 0, SPECIAL_ROOT_SLICE); |
| 188 |
check_p_g_slice("/user.slice/user-waldo.slice", 0, "user-waldo.slice"); |
| 189 |
check_p_g_slice("", 0, SPECIAL_ROOT_SLICE); |
| 190 |
check_p_g_slice("foobar", 0, SPECIAL_ROOT_SLICE); |
| 191 |
check_p_g_slice("foobar.slice", 0, "foobar.slice"); |
| 192 |
check_p_g_slice("foo.slice/foo-bar.slice/waldo.service", 0, "foo-bar.slice"); |
| 193 |
check_p_g_slice("/capsule.slice/[email protected]/app.slice/run-p9-i1.service", 0, "capsule.slice"); |
| 194 |
} |
| 195 |
|
| 196 |
static void check_p_g_u_slice(const char *path, int code, const char *result) { |
| 197 |
_cleanup_free_ char *s = NULL; |
| 198 |
|
| 199 |
assert_se(cg_path_get_user_slice(path, &s) == code); |
| 200 |
ASSERT_STREQ(s, result); |
| 201 |
} |
| 202 |
|
| 203 |
TEST(path_get_user_slice) { |
| 204 |
check_p_g_u_slice("/user.slice", -ENXIO, NULL); |
| 205 |
check_p_g_u_slice("/foobar", -ENXIO, NULL); |
| 206 |
check_p_g_u_slice("/user.slice/user-waldo.slice", -ENXIO, NULL); |
| 207 |
check_p_g_u_slice("", -ENXIO, NULL); |
| 208 |
check_p_g_u_slice("foobar", -ENXIO, NULL); |
| 209 |
check_p_g_u_slice("foobar.slice", -ENXIO, NULL); |
| 210 |
check_p_g_u_slice("foo.slice/foo-bar.slice/waldo.service", -ENXIO, NULL); |
| 211 |
|
| 212 |
check_p_g_u_slice("foo.slice/foo-bar.slice/[email protected]", 0, SPECIAL_ROOT_SLICE); |
| 213 |
check_p_g_u_slice("foo.slice/foo-bar.slice/[email protected]/", 0, SPECIAL_ROOT_SLICE); |
| 214 |
check_p_g_u_slice("foo.slice/foo-bar.slice/[email protected]///", 0, SPECIAL_ROOT_SLICE); |
| 215 |
check_p_g_u_slice("foo.slice/foo-bar.slice/[email protected]/waldo.service", 0, SPECIAL_ROOT_SLICE); |
| 216 |
check_p_g_u_slice("foo.slice/foo-bar.slice/[email protected]/piep.slice/foo.service", 0, "piep.slice"); |
| 217 |
check_p_g_u_slice("/foo.slice//foo-bar.slice/[email protected]/piep.slice//piep-pap.slice//foo.service", 0, "piep-pap.slice"); |
| 218 |
|
| 219 |
check_p_g_u_slice("/capsule.slice/[email protected]/app.slice/run-p9-i1.service", 0, "app.slice"); |
| 220 |
check_p_g_u_slice("/capsule.slice/[email protected]/app.slice/run-p9-i1.service", 0, "app.slice"); |
| 221 |
check_p_g_u_slice("/capsule.slice/[email protected]/foo.slice/foo-bar.slice/run-p9-i1.service", 0, "foo-bar.slice"); |
| 222 |
} |
| 223 |
|
| 224 |
TEST(get_paths, .sd_booted = true) { |
| 225 |
_cleanup_free_ char *a = NULL; |
| 226 |
|
| 227 |
assert_se(cg_get_root_path(&a) >= 0); |
| 228 |
log_info("Root = %s", a); |
| 229 |
} |
| 230 |
|
| 231 |
static inline bool hidden_cgroup(const char *p) { |
| 232 |
assert(p); |
| 233 |
|
| 234 |
/* Consider top-level cgroup hidden from us */ |
| 235 |
return p[0] == '/' && p[strspn(p, "/")] == '.'; |
| 236 |
} |
| 237 |
|
| 238 |
TEST(proc, .sd_booted = true) { |
| 239 |
_cleanup_closedir_ DIR *d = NULL; |
| 240 |
int r; |
| 241 |
|
| 242 |
ASSERT_OK(proc_dir_open(&d)); |
| 243 |
|
| 244 |
for (;;) { |
| 245 |
_cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL; |
| 246 |
_cleanup_(pidref_done) PidRef pid = PIDREF_NULL; |
| 247 |
uid_t uid = UID_INVALID; |
| 248 |
|
| 249 |
r = proc_dir_read_pidref(d, &pid); |
| 250 |
if (r == -EINVAL) /* Can happen if we pick a non-thread-group leader */ |
| 251 |
continue; |
| 252 |
ASSERT_OK(r); |
| 253 |
if (r == 0) |
| 254 |
break; |
| 255 |
|
| 256 |
if (pidref_is_kernel_thread(&pid) != 0) |
| 257 |
continue; |
| 258 |
|
| 259 |
r = cg_pidref_get_path(&pid, &path); |
| 260 |
if (r == -ESRCH) |
| 261 |
continue; |
| 262 |
ASSERT_OK(r); |
| 263 |
|
| 264 |
/* Test may run in a container with supervising/monitor processes that don't belong to our |
| 265 |
* cgroup tree (slices/leaves) */ |
| 266 |
if (hidden_cgroup(path)) |
| 267 |
continue; |
| 268 |
|
| 269 |
int r1 = cg_pid_get_path_shifted(pid.pid, NULL, &path_shifted); |
| 270 |
int r2 = cg_pidref_get_unit(&pid, &unit); |
| 271 |
int r3 = cg_pid_get_slice(pid.pid, &slice); |
| 272 |
|
| 273 |
/* Not all processes belong to a specific user or a machine */ |
| 274 |
int r4 = cg_pidref_get_owner_uid(&pid, &uid); |
| 275 |
int r5 = cg_pidref_get_session(&pid, &session); |
| 276 |
int r6 = cg_pid_get_user_unit(pid.pid, &user_unit); |
| 277 |
int r7 = cg_pid_get_machine_name(pid.pid, &machine); |
| 278 |
|
| 279 |
log_debug(PID_FMT": %s, %s, "UID_FMT", %s, %s, %s, %s, %s", |
| 280 |
pid.pid, |
| 281 |
path, |
| 282 |
strna(path_shifted), |
| 283 |
uid, |
| 284 |
strna(session), |
| 285 |
strna(unit), |
| 286 |
strna(user_unit), |
| 287 |
strna(machine), |
| 288 |
strna(slice)); |
| 289 |
|
| 290 |
ASSERT_OK_OR(r1, -ESRCH); |
| 291 |
ASSERT_OK_OR(r2, -ESRCH, -ENXIO); |
| 292 |
ASSERT_OK_OR(r3, -ESRCH, -ENXIO); |
| 293 |
ASSERT_OK_OR(r4, -ESRCH, -ENXIO); |
| 294 |
ASSERT_OK_OR(r5, -ESRCH, -ENXIO); |
| 295 |
ASSERT_OK_OR(r6, -ESRCH, -ENXIO); |
| 296 |
ASSERT_OK_OR(r7, -ESRCH, -ENXIO, -ENOENT); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
static void test_escape_one(const char *s, const char *expected) { |
| 301 |
_cleanup_free_ char *b = NULL; |
| 302 |
|
| 303 |
assert_se(s); |
| 304 |
assert_se(expected); |
| 305 |
|
| 306 |
ASSERT_OK(cg_escape(s, &b)); |
| 307 |
ASSERT_STREQ(b, expected); |
| 308 |
|
| 309 |
ASSERT_STREQ(cg_unescape(b), s); |
| 310 |
|
| 311 |
assert_se(filename_is_valid(b)); |
| 312 |
assert_se(!cg_needs_escape(s) || b[0] == '_'); |
| 313 |
} |
| 314 |
|
| 315 |
TEST(escape, .sd_booted = true) { |
| 316 |
test_escape_one("foobar", "foobar"); |
| 317 |
test_escape_one(".foobar", "_.foobar"); |
| 318 |
test_escape_one("foobar.service", "foobar.service"); |
| 319 |
test_escape_one("cgroup.service", "_cgroup.service"); |
| 320 |
test_escape_one("tasks", "_tasks"); |
| 321 |
if (access("/sys/fs/cgroup/cpu", F_OK) == 0) |
| 322 |
test_escape_one("cpu.service", "_cpu.service"); |
| 323 |
test_escape_one("_foobar", "__foobar"); |
| 324 |
test_escape_one("", "_"); |
| 325 |
test_escape_one("_", "__"); |
| 326 |
test_escape_one(".", "_."); |
| 327 |
} |
| 328 |
|
| 329 |
TEST(cg_split_spec) { |
| 330 |
char *c, *p; |
| 331 |
|
| 332 |
ASSERT_OK(cg_split_spec("foobar:/", &c, &p)); |
| 333 |
ASSERT_STREQ(c, "foobar"); |
| 334 |
ASSERT_STREQ(p, "/"); |
| 335 |
c = mfree(c); |
| 336 |
p = mfree(p); |
| 337 |
|
| 338 |
ASSERT_OK(cg_split_spec("foobar:", &c, &p)); |
| 339 |
ASSERT_STREQ(c, "foobar"); |
| 340 |
ASSERT_NULL(p); |
| 341 |
c = mfree(c); |
| 342 |
|
| 343 |
ASSERT_OK(cg_split_spec("foobar", &c, &p)); |
| 344 |
ASSERT_STREQ(c, "foobar"); |
| 345 |
ASSERT_NULL(p); |
| 346 |
c = mfree(c); |
| 347 |
|
| 348 |
ASSERT_OK(cg_split_spec(":///", &c, &p)); |
| 349 |
ASSERT_STREQ(c, ""); |
| 350 |
ASSERT_STREQ(p, "/"); |
| 351 |
c = mfree(c); |
| 352 |
p = mfree(p); |
| 353 |
|
| 354 |
ASSERT_OK(cg_split_spec(":", &c, &p)); |
| 355 |
ASSERT_STREQ(c, ""); |
| 356 |
ASSERT_NULL(p); |
| 357 |
c = mfree(c); |
| 358 |
|
| 359 |
ASSERT_OK(cg_split_spec("", &c, &p)); |
| 360 |
ASSERT_NULL(c); |
| 361 |
ASSERT_NULL(p); |
| 362 |
|
| 363 |
ASSERT_OK(cg_split_spec("fo/obar:/", &c, &p)); |
| 364 |
ASSERT_STREQ(c, "fo/obar"); |
| 365 |
ASSERT_STREQ(p, "/"); |
| 366 |
c = mfree(c); |
| 367 |
p = mfree(p); |
| 368 |
|
| 369 |
ASSERT_ERROR(cg_split_spec("foobar:asdfd", &c, &p), EINVAL); |
| 370 |
|
| 371 |
ASSERT_OK(cg_split_spec("/", &c, &p)); |
| 372 |
ASSERT_NULL(c); |
| 373 |
ASSERT_STREQ(p, "/"); |
| 374 |
p = mfree(p); |
| 375 |
} |
| 376 |
|
| 377 |
static void test_slice_to_path_one(const char *unit, const char *path, int error) { |
| 378 |
_cleanup_free_ char *ret = NULL; |
| 379 |
int r; |
| 380 |
|
| 381 |
log_info("unit: %s", unit); |
| 382 |
|
| 383 |
r = cg_slice_to_path(unit, &ret); |
| 384 |
log_info("actual: %s / %d", strnull(ret), r); |
| 385 |
log_info("expect: %s / %d", strnull(path), error); |
| 386 |
assert_se(r == error); |
| 387 |
ASSERT_STREQ(ret, path); |
| 388 |
} |
| 389 |
|
| 390 |
TEST(slice_to_path) { |
| 391 |
test_slice_to_path_one("foobar.slice", "foobar.slice", 0); |
| 392 |
test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0); |
| 393 |
test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL); |
| 394 |
test_slice_to_path_one(SPECIAL_ROOT_SLICE, "", 0); |
| 395 |
test_slice_to_path_one("--.slice", NULL, -EINVAL); |
| 396 |
test_slice_to_path_one("-", NULL, -EINVAL); |
| 397 |
test_slice_to_path_one("-foo-.slice", NULL, -EINVAL); |
| 398 |
test_slice_to_path_one("-foo.slice", NULL, -EINVAL); |
| 399 |
test_slice_to_path_one("foo-.slice", NULL, -EINVAL); |
| 400 |
test_slice_to_path_one("foo--bar.slice", NULL, -EINVAL); |
| 401 |
test_slice_to_path_one("foo.slice/foo--bar.slice", NULL, -EINVAL); |
| 402 |
test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0); |
| 403 |
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); |
| 404 |
|
| 405 |
test_slice_to_path_one("[email protected]", NULL, -EINVAL); |
| 406 |
test_slice_to_path_one("[email protected]", NULL, -EINVAL); |
| 407 |
test_slice_to_path_one("[email protected]", NULL, -EINVAL); |
| 408 |
test_slice_to_path_one("[email protected]", NULL, -EINVAL); |
| 409 |
test_slice_to_path_one("[email protected]", NULL, -EINVAL); |
| 410 |
test_slice_to_path_one("[email protected]", NULL, -EINVAL); |
| 411 |
test_slice_to_path_one("foo@@bar.slice", NULL, -EINVAL); |
| 412 |
test_slice_to_path_one("foo.slice/foo@@bar.slice", NULL, -EINVAL); |
| 413 |
} |
| 414 |
|
| 415 |
static void test_shift_path_one(const char *raw, const char *root, const char *shifted) { |
| 416 |
const char *s = NULL; |
| 417 |
|
| 418 |
ASSERT_OK(cg_shift_path(raw, root, &s)); |
| 419 |
ASSERT_STREQ(s, shifted); |
| 420 |
} |
| 421 |
|
| 422 |
TEST(shift_path) { |
| 423 |
test_shift_path_one("/foobar/waldo", "/", "/foobar/waldo"); |
| 424 |
test_shift_path_one("/foobar/waldo", "", "/foobar/waldo"); |
| 425 |
test_shift_path_one("/foobar/waldo", "/foobar", "/waldo"); |
| 426 |
test_shift_path_one("/foobar/waldo", "/hogehoge", "/foobar/waldo"); |
| 427 |
} |
| 428 |
|
| 429 |
TEST(mask_supported, .sd_booted = true) { |
| 430 |
CGroupMask m; |
| 431 |
|
| 432 |
ASSERT_OK(cg_mask_supported(&m)); |
| 433 |
|
| 434 |
for (CGroupController c = 0; c < _CGROUP_CONTROLLER_MAX; c++) |
| 435 |
printf("'%s' is supported: %s\n", |
| 436 |
cgroup_controller_to_string(c), |
| 437 |
yes_no(m & CGROUP_CONTROLLER_TO_MASK(c))); |
| 438 |
} |
| 439 |
|
| 440 |
TEST(cg_get_keyed_attribute) { |
| 441 |
_cleanup_free_ char *val = NULL; |
| 442 |
char *vals3[3] = {}, *vals3a[3] = {}; |
| 443 |
int r; |
| 444 |
|
| 445 |
if (cg_is_available() <= 0) |
| 446 |
return (void) log_tests_skipped("cgroupfs v2 is not mounted"); |
| 447 |
|
| 448 |
r = cg_get_keyed_attribute("/init.scope", "no_such_file", STRV_MAKE("no_such_attr"), &val); |
| 449 |
if (ERRNO_IS_PRIVILEGE(r)) |
| 450 |
return (void) log_tests_skipped_errno(r, "/sys/fs/cgroup not accessible"); |
| 451 |
|
| 452 |
assert_se(r == -ENOENT); |
| 453 |
ASSERT_NULL(val); |
| 454 |
|
| 455 |
if (access("/sys/fs/cgroup/init.scope/cpu.stat", R_OK) < 0) |
| 456 |
return (void) log_tests_skipped_errno(errno, "/init.scope/cpu.stat not accessible"); |
| 457 |
|
| 458 |
assert_se(cg_get_keyed_attribute("/init.scope", "cpu.stat", STRV_MAKE("no_such_attr"), &val) == -ENXIO); |
| 459 |
ASSERT_NULL(val); |
| 460 |
|
| 461 |
assert_se(cg_get_keyed_attribute("/init.scope", "cpu.stat", STRV_MAKE("usage_usec"), &val) == 0); |
| 462 |
val = mfree(val); |
| 463 |
|
| 464 |
assert_se(cg_get_keyed_attribute("/init.scope", "cpu.stat", STRV_MAKE("usage_usec", "no_such_attr"), vals3) == -ENXIO); |
| 465 |
|
| 466 |
assert_se(cg_get_keyed_attribute("/init.scope", "cpu.stat", |
| 467 |
STRV_MAKE("usage_usec", "user_usec", "system_usec"), vals3) == 0); |
| 468 |
for (size_t i = 0; i < 3; i++) |
| 469 |
free(vals3[i]); |
| 470 |
|
| 471 |
assert_se(cg_get_keyed_attribute("/init.scope", "cpu.stat", |
| 472 |
STRV_MAKE("system_usec", "user_usec", "usage_usec"), vals3a) == 0); |
| 473 |
for (size_t i = 0; i < 3; i++) |
| 474 |
free(vals3a[i]); |
| 475 |
} |
| 476 |
|
| 477 |
TEST(bfq_weight_conversion) { |
| 478 |
assert_se(BFQ_WEIGHT(1) == 1); |
| 479 |
assert_se(BFQ_WEIGHT(50) == 50); |
| 480 |
assert_se(BFQ_WEIGHT(100) == 100); |
| 481 |
assert_se(BFQ_WEIGHT(500) == 136); |
| 482 |
assert_se(BFQ_WEIGHT(5000) == 545); |
| 483 |
assert_se(BFQ_WEIGHT(10000) == 1000); |
| 484 |
} |
| 485 |
|
| 486 |
TEST(cgroupid) { |
| 487 |
_cleanup_free_ char *p = NULL, *p2 = NULL; |
| 488 |
_cleanup_close_ int fd = -EBADF, fd2 = -EBADF; |
| 489 |
uint64_t id, id2; |
| 490 |
|
| 491 |
if (cg_is_available() <= 0) |
| 492 |
return (void) log_tests_skipped("cgroupfs v2 is not mounted"); |
| 493 |
|
| 494 |
fd = cg_path_open("/"); |
| 495 |
ASSERT_OK(fd); |
| 496 |
|
| 497 |
ASSERT_OK(fd_get_path(fd, &p)); |
| 498 |
ASSERT_TRUE(path_equal(p, "/sys/fs/cgroup")); |
| 499 |
|
| 500 |
ASSERT_OK(fd_to_handle_u64(fd, &id)); |
| 501 |
|
| 502 |
fd2 = cg_cgroupid_open(fd, id); |
| 503 |
|
| 504 |
/* The kernel converts a bunch of errors to ESTALE in the open_by_handle_at() codepath so we treat |
| 505 |
* it as missing privs but it could be absolutely anything really. */ |
| 506 |
if (ERRNO_IS_NEG_PRIVILEGE(fd2) || fd2 == -ESTALE) |
| 507 |
log_notice("Skipping open-by-cgroup-id test because lacking privs."); |
| 508 |
else if (ERRNO_IS_NEG_NOT_SUPPORTED(fd2)) |
| 509 |
log_notice("Skipping open-by-cgroup-id test because syscall is missing or blocked."); |
| 510 |
else { |
| 511 |
ASSERT_OK(fd2); |
| 512 |
|
| 513 |
ASSERT_OK(fd_get_path(fd2, &p2)); |
| 514 |
ASSERT_TRUE(path_equal(p2, "/sys/fs/cgroup")); |
| 515 |
|
| 516 |
ASSERT_OK(fd_to_handle_u64(fd2, &id2)); |
| 517 |
|
| 518 |
ASSERT_EQ(id, id2); |
| 519 |
|
| 520 |
ASSERT_OK_EQ(inode_same_at(fd, NULL, fd2, NULL, AT_EMPTY_PATH), true); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
DEFINE_TEST_MAIN(LOG_DEBUG); |