1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
12 #include "alloc-util.h"
13 #include "bus-error.h"
14 #include "bus-locator.h"
16 #include "bus-wait-for-jobs.h"
17 #include "cgroup-setup.h"
18 #include "cgroup-util.h"
24 #include "mountpoint-util.h"
25 #include "namespace-util.h"
26 #include "path-util.h"
27 #include "process-util.h"
28 #include "random-util.h"
31 #include "tmpfile-util.h"
33 char* setup_fake_runtime_dir(void) {
34 char t
[] = "/tmp/fake-xdg-runtime-XXXXXX", *p
;
36 assert_se(mkdtemp(t
));
37 assert_se(setenv("XDG_RUNTIME_DIR", t
, 1) >= 0);
38 assert_se(p
= strdup(t
));
43 static void load_testdata_env(void) {
44 static bool called
= false;
45 _cleanup_free_
char *s
= NULL
, *d
= NULL
, *envpath
= NULL
;
46 _cleanup_strv_free_
char **pairs
= NULL
;
53 assert_se(readlink_and_make_absolute("/proc/self/exe", &s
) >= 0);
54 assert_se(path_extract_directory(s
, &d
) >= 0);
55 assert_se(envpath
= path_join(d
, "systemd-runtest.env"));
57 r
= load_env_file_pairs(NULL
, envpath
, &pairs
);
59 log_debug_errno(r
, "Reading %s failed: %m", envpath
);
63 STRV_FOREACH_PAIR(k
, v
, pairs
)
64 assert_se(setenv(*k
, *v
, 0) >= 0);
67 int get_testdata_dir(const char *suffix
, char **ret
) {
73 /* if the env var is set, use that */
74 dir
= getenv("SYSTEMD_TEST_DATA");
76 dir
= SYSTEMD_TEST_DATA
;
77 if (access(dir
, F_OK
) < 0)
78 return log_error_errno(errno
, "ERROR: $SYSTEMD_TEST_DATA directory [%s] not accessible: %m", dir
);
80 p
= path_join(dir
, suffix
);
88 const char* get_catalog_dir(void) {
93 /* if the env var is set, use that */
94 env
= getenv("SYSTEMD_CATALOG_DIR");
96 env
= SYSTEMD_CATALOG_DIR
;
97 if (access(env
, F_OK
) < 0) {
98 fprintf(stderr
, "ERROR: $SYSTEMD_CATALOG_DIR directory [%s] does not exist\n", env
);
104 bool slow_tests_enabled(void) {
107 r
= getenv_bool("SYSTEMD_SLOW_TESTS");
112 log_warning_errno(r
, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring.");
113 return SYSTEMD_SLOW_TESTS_DEFAULT
;
116 void test_setup_logging(int level
) {
117 log_set_max_level(level
);
118 log_parse_environment();
122 int write_tmpfile(char *pattern
, const char *contents
) {
123 _cleanup_close_
int fd
= -EBADF
;
128 fd
= mkostemp_safe(pattern
);
132 ssize_t l
= strlen(contents
);
134 if (write(fd
, contents
, l
) != l
)
135 return errno_or_else(EIO
);
139 bool have_namespaces(void) {
143 /* Checks whether namespaces are available. In some cases they aren't. We do this by calling unshare(), and we
144 * do so in a child process in order not to affect our own process. */
151 if (detach_mount_namespace() < 0)
157 assert_se(waitid(P_PID
, pid
, &si
, WEXITED
) >= 0);
158 assert_se(si
.si_code
== CLD_EXITED
);
160 if (si
.si_status
== EXIT_SUCCESS
)
163 if (si
.si_status
== EXIT_FAILURE
)
166 assert_not_reached();
169 bool can_memlock(void) {
170 /* Let's see if we can mlock() a larger blob of memory. BPF programs are charged against
171 * RLIMIT_MEMLOCK, hence let's first make sure we can lock memory at all, and skip the test if we
172 * cannot. Why not check RLIMIT_MEMLOCK explicitly? Because in container environments the
173 * RLIMIT_MEMLOCK value we see might not match the RLIMIT_MEMLOCK value actually in effect. */
175 void *p
= mmap(NULL
, CAN_MEMLOCK_SIZE
, PROT_READ
|PROT_WRITE
, MAP_ANONYMOUS
|MAP_SHARED
, -1, 0);
179 bool b
= mlock(p
, CAN_MEMLOCK_SIZE
) >= 0;
181 assert_se(munlock(p
, CAN_MEMLOCK_SIZE
) >= 0);
183 assert_se(munmap(p
, CAN_MEMLOCK_SIZE
) >= 0);
187 static int allocate_scope(void) {
188 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
189 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
190 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
191 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
192 _cleanup_free_
char *scope
= NULL
;
196 /* Let's try to run this test in a scope of its own, with delegation turned on, so that PID 1 doesn't
197 * interfere with our cgroup management. */
199 r
= sd_bus_default_system(&bus
);
201 return log_error_errno(r
, "Failed to connect to system bus: %m");
203 r
= bus_wait_for_jobs_new(bus
, &w
);
205 return log_error_errno(r
, "Could not watch jobs: %m");
207 if (asprintf(&scope
, "%s-%" PRIx64
".scope", program_invocation_short_name
, random_u64()) < 0)
210 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
212 return bus_log_create_error(r
);
215 r
= sd_bus_message_append(m
, "ss", scope
, "fail");
217 return bus_log_create_error(r
);
220 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
222 return bus_log_create_error(r
);
224 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
226 return bus_log_create_error(r
);
228 r
= sd_bus_message_append(m
, "(sv)", "Delegate", "b", 1);
230 return bus_log_create_error(r
);
232 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
234 return bus_log_create_error(r
);
236 r
= sd_bus_message_close_container(m
);
238 return bus_log_create_error(r
);
240 /* Auxiliary units */
241 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
243 return bus_log_create_error(r
);
245 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
247 return log_error_errno(r
, "Failed to start transient scope unit: %s", bus_error_message(&error
, r
));
249 r
= sd_bus_message_read(reply
, "o", &object
);
251 return bus_log_parse_error(r
);
253 r
= bus_wait_for_jobs_one(w
, object
, BUS_WAIT_JOBS_LOG_ERROR
, NULL
);
260 static int enter_cgroup(char **ret_cgroup
, bool enter_subroot
) {
261 _cleanup_free_
char *cgroup_root
= NULL
, *cgroup_subroot
= NULL
;
262 CGroupMask supported
;
265 r
= allocate_scope();
267 log_warning_errno(r
, "Couldn't allocate a scope unit for this test, proceeding without.");
269 r
= cg_pid_get_path(NULL
, 0, &cgroup_root
);
270 if (IN_SET(r
, -ENOMEDIUM
, -ENOENT
))
271 return log_warning_errno(r
, "cg_pid_get_path(NULL, 0, ...) failed: %m");
275 assert_se(asprintf(&cgroup_subroot
, "%s/%" PRIx64
, cgroup_root
, random_u64()) >= 0);
277 cgroup_subroot
= strdup(cgroup_root
);
278 assert_se(cgroup_subroot
!= NULL
);
281 assert_se(cg_mask_supported(&supported
) >= 0);
283 /* If this fails, then we don't mind as the later cgroup operations will fail too, and it's fine if
284 * we handle any errors at that point. */
286 r
= cg_create_everywhere(supported
, _CGROUP_MASK_ALL
, cgroup_subroot
);
290 r
= cg_attach_everywhere(supported
, cgroup_subroot
, 0, NULL
, NULL
);
295 *ret_cgroup
= TAKE_PTR(cgroup_subroot
);
300 int enter_cgroup_subroot(char **ret_cgroup
) {
301 return enter_cgroup(ret_cgroup
, true);
304 int enter_cgroup_root(char **ret_cgroup
) {
305 return enter_cgroup(ret_cgroup
, false);
308 const char *ci_environment(void) {
309 /* We return a string because we might want to provide multiple bits of information later on: not
310 * just the general CI environment type, but also whether we're sanitizing or not, etc. The caller is
311 * expected to use strstr on the returned value. */
312 static const char *ans
= POINTER_MAX
;
315 if (ans
!= POINTER_MAX
)
318 /* We allow specifying the environment with $CITYPE. Nobody uses this so far, but we are ready. */
319 const char *citype
= getenv("CITYPE");
320 if (!isempty(citype
))
321 return (ans
= citype
);
323 if (getenv_bool("TRAVIS") > 0)
324 return (ans
= "travis");
325 if (getenv_bool("SEMAPHORE") > 0)
326 return (ans
= "semaphore");
327 if (getenv_bool("GITHUB_ACTIONS") > 0)
328 return (ans
= "github-actions");
329 if (getenv("AUTOPKGTEST_ARTIFACTS") || getenv("AUTOPKGTEST_TMP"))
330 return (ans
= "autopkgtest");
331 if (getenv("SALSA_CI_IMAGES"))
332 return (ans
= "salsa-ci");
334 FOREACH_STRING(var
, "CI", "CONTINOUS_INTEGRATION") {
335 /* Those vars are booleans according to Semaphore and Travis docs:
336 * https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
337 * https://docs.semaphoreci.com/ci-cd-environment/environment-variables/#ci
339 r
= getenv_bool(var
);
341 return (ans
= "unknown"); /* Some other unknown thing */