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 "namespace-util.h"
25 #include "path-util.h"
26 #include "random-util.h"
30 char* setup_fake_runtime_dir(void) {
31 char t
[] = "/tmp/fake-xdg-runtime-XXXXXX", *p
;
33 assert_se(mkdtemp(t
));
34 assert_se(setenv("XDG_RUNTIME_DIR", t
, 1) >= 0);
35 assert_se(p
= strdup(t
));
40 static void load_testdata_env(void) {
41 static bool called
= false;
42 _cleanup_free_
char *s
= NULL
;
43 _cleanup_free_
char *envpath
= NULL
;
44 _cleanup_strv_free_
char **pairs
= NULL
;
51 assert_se(readlink_and_make_absolute("/proc/self/exe", &s
) >= 0);
54 envpath
= path_join(s
, "systemd-runtest.env");
55 if (load_env_file_pairs(NULL
, envpath
, &pairs
) < 0)
58 STRV_FOREACH_PAIR(k
, v
, pairs
)
62 int get_testdata_dir(const char *suffix
, char **ret
) {
68 /* if the env var is set, use that */
69 dir
= getenv("SYSTEMD_TEST_DATA");
71 dir
= SYSTEMD_TEST_DATA
;
72 if (access(dir
, F_OK
) < 0)
73 return log_error_errno(errno
, "ERROR: $SYSTEMD_TEST_DATA directory [%s] not accessible: %m", dir
);
75 p
= path_join(dir
, suffix
);
83 const char* get_catalog_dir(void) {
88 /* if the env var is set, use that */
89 env
= getenv("SYSTEMD_CATALOG_DIR");
91 env
= SYSTEMD_CATALOG_DIR
;
92 if (access(env
, F_OK
) < 0) {
93 fprintf(stderr
, "ERROR: $SYSTEMD_CATALOG_DIR directory [%s] does not exist\n", env
);
99 bool slow_tests_enabled(void) {
102 r
= getenv_bool("SYSTEMD_SLOW_TESTS");
107 log_warning_errno(r
, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring.");
108 return SYSTEMD_SLOW_TESTS_DEFAULT
;
111 void test_setup_logging(int level
) {
112 log_set_max_level(level
);
113 log_parse_environment();
117 int log_tests_skipped(const char *message
) {
118 log_notice("%s: %s, skipping tests.",
119 program_invocation_short_name
, message
);
120 return EXIT_TEST_SKIP
;
123 int log_tests_skipped_errno(int r
, const char *message
) {
124 log_notice_errno(r
, "%s: %s, skipping tests: %m",
125 program_invocation_short_name
, message
);
126 return EXIT_TEST_SKIP
;
129 bool have_namespaces(void) {
133 /* Checks whether namespaces are available. In some cases they aren't. We do this by calling unshare(), and we
134 * do so in a child process in order not to affect our own process. */
141 if (detach_mount_namespace() < 0)
147 assert_se(waitid(P_PID
, pid
, &si
, WEXITED
) >= 0);
148 assert_se(si
.si_code
== CLD_EXITED
);
150 if (si
.si_status
== EXIT_SUCCESS
)
153 if (si
.si_status
== EXIT_FAILURE
)
156 assert_not_reached("unexpected exit code");
159 bool can_memlock(void) {
160 /* Let's see if we can mlock() a larger blob of memory. BPF programs are charged against
161 * RLIMIT_MEMLOCK, hence let's first make sure we can lock memory at all, and skip the test if we
162 * cannot. Why not check RLIMIT_MEMLOCK explicitly? Because in container environments the
163 * RLIMIT_MEMLOCK value we see might not match the RLIMIT_MEMLOCK value actually in effect. */
165 void *p
= mmap(NULL
, CAN_MEMLOCK_SIZE
, PROT_READ
|PROT_WRITE
, MAP_ANONYMOUS
|MAP_SHARED
, -1, 0);
169 bool b
= mlock(p
, CAN_MEMLOCK_SIZE
) >= 0;
171 assert_se(munlock(p
, CAN_MEMLOCK_SIZE
) >= 0);
173 assert_se(munmap(p
, CAN_MEMLOCK_SIZE
) >= 0);
177 int enter_cgroup_subroot(char **ret_cgroup
) {
178 _cleanup_free_
char *cgroup_root
= NULL
, *cgroup_subroot
= NULL
;
179 CGroupMask supported
;
182 r
= cg_pid_get_path(NULL
, 0, &cgroup_root
);
184 return log_warning_errno(r
, "cg_pid_get_path(NULL, 0, ...) failed: %m");
187 assert_se(asprintf(&cgroup_subroot
, "%s/%" PRIx64
, cgroup_root
, random_u64()) >= 0);
188 assert_se(cg_mask_supported(&supported
) >= 0);
190 /* If this fails, then we don't mind as the later cgroup operations will fail too, and it's fine if
191 * we handle any errors at that point. */
193 r
= cg_create_everywhere(supported
, _CGROUP_MASK_ALL
, cgroup_subroot
);
197 r
= cg_attach_everywhere(supported
, cgroup_subroot
, 0, NULL
, NULL
);
202 *ret_cgroup
= TAKE_PTR(cgroup_subroot
);