1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 /* When we include libgen.h because we need dirname() we immediately
12 * undefine basename() since libgen.h defines it as a macro to the POSIX
13 * version which is really broken. We prefer GNU basename(). */
17 #include "alloc-util.h"
18 #include "cgroup-setup.h"
19 #include "cgroup-util.h"
24 #include "path-util.h"
25 #include "random-util.h"
29 char* setup_fake_runtime_dir(void) {
30 char t
[] = "/tmp/fake-xdg-runtime-XXXXXX", *p
;
32 assert_se(mkdtemp(t
));
33 assert_se(setenv("XDG_RUNTIME_DIR", t
, 1) >= 0);
34 assert_se(p
= strdup(t
));
39 static void load_testdata_env(void) {
40 static bool called
= false;
41 _cleanup_free_
char *s
= NULL
;
42 _cleanup_free_
char *envpath
= NULL
;
43 _cleanup_strv_free_
char **pairs
= NULL
;
50 assert_se(readlink_and_make_absolute("/proc/self/exe", &s
) >= 0);
53 envpath
= path_join(s
, "systemd-runtest.env");
54 if (load_env_file_pairs(NULL
, envpath
, &pairs
) < 0)
57 STRV_FOREACH_PAIR(k
, v
, pairs
)
61 const char* get_testdata_dir(void) {
66 /* if the env var is set, use that */
67 env
= getenv("SYSTEMD_TEST_DATA");
69 env
= SYSTEMD_TEST_DATA
;
70 if (access(env
, F_OK
) < 0) {
71 fprintf(stderr
, "ERROR: $SYSTEMD_TEST_DATA directory [%s] does not exist\n", env
);
78 const char* get_catalog_dir(void) {
83 /* if the env var is set, use that */
84 env
= getenv("SYSTEMD_CATALOG_DIR");
86 env
= SYSTEMD_CATALOG_DIR
;
87 if (access(env
, F_OK
) < 0) {
88 fprintf(stderr
, "ERROR: $SYSTEMD_CATALOG_DIR directory [%s] does not exist\n", env
);
94 bool slow_tests_enabled(void) {
97 r
= getenv_bool("SYSTEMD_SLOW_TESTS");
102 log_warning_errno(r
, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring.");
103 return SYSTEMD_SLOW_TESTS_DEFAULT
;
106 void test_setup_logging(int level
) {
107 log_set_max_level(level
);
108 log_parse_environment();
112 int log_tests_skipped(const char *message
) {
113 log_notice("%s: %s, skipping tests.",
114 program_invocation_short_name
, message
);
115 return EXIT_TEST_SKIP
;
118 int log_tests_skipped_errno(int r
, const char *message
) {
119 log_notice_errno(r
, "%s: %s, skipping tests: %m",
120 program_invocation_short_name
, message
);
121 return EXIT_TEST_SKIP
;
124 bool have_namespaces(void) {
128 /* Checks whether namespaces are available. In some cases they aren't. We do this by calling unshare(), and we
129 * do so in a child process in order not to affect our own process. */
136 if (unshare(CLONE_NEWNS
) < 0)
139 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
) < 0)
145 assert_se(waitid(P_PID
, pid
, &si
, WEXITED
) >= 0);
146 assert_se(si
.si_code
== CLD_EXITED
);
148 if (si
.si_status
== EXIT_SUCCESS
)
151 if (si
.si_status
== EXIT_FAILURE
)
154 assert_not_reached("unexpected exit code");
157 bool can_memlock(void) {
158 /* Let's see if we can mlock() a larger blob of memory. BPF programs are charged against
159 * RLIMIT_MEMLOCK, hence let's first make sure we can lock memory at all, and skip the test if we
160 * cannot. Why not check RLIMIT_MEMLOCK explicitly? Because in container environments the
161 * RLIMIT_MEMLOCK value we see might not match the RLIMIT_MEMLOCK value actually in effect. */
163 void *p
= mmap(NULL
, CAN_MEMLOCK_SIZE
, PROT_READ
|PROT_WRITE
, MAP_ANONYMOUS
|MAP_SHARED
, -1, 0);
167 bool b
= mlock(p
, CAN_MEMLOCK_SIZE
) >= 0;
169 assert_se(munlock(p
, CAN_MEMLOCK_SIZE
) >= 0);
171 assert_se(munmap(p
, CAN_MEMLOCK_SIZE
) >= 0);
175 int enter_cgroup_subroot(char **ret_cgroup
) {
176 _cleanup_free_
char *cgroup_root
= NULL
, *cgroup_subroot
= NULL
;
177 CGroupMask supported
;
180 r
= cg_pid_get_path(NULL
, 0, &cgroup_root
);
182 return log_warning_errno(r
, "cg_pid_get_path(NULL, 0, ...) failed: %m");
185 assert_se(asprintf(&cgroup_subroot
, "%s/%" PRIx64
, cgroup_root
, random_u64()) >= 0);
186 assert_se(cg_mask_supported(&supported
) >= 0);
188 /* If this fails, then we don't mind as the later cgroup operations will fail too, and it's fine if
189 * we handle any errors at that point. */
191 r
= cg_create_everywhere(supported
, _CGROUP_MASK_ALL
, cgroup_subroot
);
195 r
= cg_attach_everywhere(supported
, cgroup_subroot
, 0, NULL
, NULL
);
200 *ret_cgroup
= TAKE_PTR(cgroup_subroot
);