]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/tests.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / src / shared / tests.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sched.h>
4 #include <signal.h>
5 #include <stdlib.h>
6 #include <sys/mman.h>
7 #include <sys/mount.h>
8 #include <sys/wait.h>
9
10 #include "sd-bus.h"
11
12 #include "alloc-util.h"
13 #include "bus-error.h"
14 #include "bus-locator.h"
15 #include "bus-util.h"
16 #include "bus-wait-for-jobs.h"
17 #include "cgroup-setup.h"
18 #include "cgroup-util.h"
19 #include "env-file.h"
20 #include "env-util.h"
21 #include "fd-util.h"
22 #include "fs-util.h"
23 #include "log.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"
29 #include "strv.h"
30 #include "tests.h"
31 #include "tmpfile-util.h"
32
33 char* setup_fake_runtime_dir(void) {
34 char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
35
36 assert_se(mkdtemp(t));
37 assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0);
38 assert_se(p = strdup(t));
39
40 return p;
41 }
42
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;
47 int r;
48
49 if (called)
50 return;
51 called = true;
52
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"));
56
57 r = load_env_file_pairs(NULL, envpath, &pairs);
58 if (r < 0) {
59 log_debug_errno(r, "Reading %s failed: %m", envpath);
60 return;
61 }
62
63 STRV_FOREACH_PAIR(k, v, pairs)
64 assert_se(setenv(*k, *v, 0) >= 0);
65 }
66
67 int get_testdata_dir(const char *suffix, char **ret) {
68 const char *dir;
69 char *p;
70
71 load_testdata_env();
72
73 /* if the env var is set, use that */
74 dir = getenv("SYSTEMD_TEST_DATA");
75 if (!dir)
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);
79
80 p = path_join(dir, suffix);
81 if (!p)
82 return log_oom();
83
84 *ret = p;
85 return 0;
86 }
87
88 const char* get_catalog_dir(void) {
89 const char *env;
90
91 load_testdata_env();
92
93 /* if the env var is set, use that */
94 env = getenv("SYSTEMD_CATALOG_DIR");
95 if (!env)
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);
99 exit(EXIT_FAILURE);
100 }
101 return env;
102 }
103
104 bool slow_tests_enabled(void) {
105 int r;
106
107 r = getenv_bool("SYSTEMD_SLOW_TESTS");
108 if (r >= 0)
109 return r;
110
111 if (r != -ENXIO)
112 log_warning_errno(r, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring.");
113 return SYSTEMD_SLOW_TESTS_DEFAULT;
114 }
115
116 void test_setup_logging(int level) {
117 log_set_max_level(level);
118 log_parse_environment();
119 log_open();
120 }
121
122 int write_tmpfile(char *pattern, const char *contents) {
123 _cleanup_close_ int fd = -EBADF;
124
125 assert(pattern);
126 assert(contents);
127
128 fd = mkostemp_safe(pattern);
129 if (fd < 0)
130 return fd;
131
132 ssize_t l = strlen(contents);
133 errno = 0;
134 if (write(fd, contents, l) != l)
135 return errno_or_else(EIO);
136 return 0;
137 }
138
139 bool have_namespaces(void) {
140 siginfo_t si = {};
141 pid_t pid;
142
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. */
145
146 pid = fork();
147 assert_se(pid >= 0);
148
149 if (pid == 0) {
150 /* child */
151 if (detach_mount_namespace() < 0)
152 _exit(EXIT_FAILURE);
153
154 _exit(EXIT_SUCCESS);
155 }
156
157 assert_se(waitid(P_PID, pid, &si, WEXITED) >= 0);
158 assert_se(si.si_code == CLD_EXITED);
159
160 if (si.si_status == EXIT_SUCCESS)
161 return true;
162
163 if (si.si_status == EXIT_FAILURE)
164 return false;
165
166 assert_not_reached();
167 }
168
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. */
174
175 void *p = mmap(NULL, CAN_MEMLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
176 if (p == MAP_FAILED)
177 return false;
178
179 bool b = mlock(p, CAN_MEMLOCK_SIZE) >= 0;
180 if (b)
181 assert_se(munlock(p, CAN_MEMLOCK_SIZE) >= 0);
182
183 assert_se(munmap(p, CAN_MEMLOCK_SIZE) >= 0);
184 return b;
185 }
186
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;
193 const char *object;
194 int r;
195
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. */
198
199 r = sd_bus_default_system(&bus);
200 if (r < 0)
201 return log_error_errno(r, "Failed to connect to system bus: %m");
202
203 r = bus_wait_for_jobs_new(bus, &w);
204 if (r < 0)
205 return log_error_errno(r, "Could not watch jobs: %m");
206
207 if (asprintf(&scope, "%s-%" PRIx64 ".scope", program_invocation_short_name, random_u64()) < 0)
208 return log_oom();
209
210 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
211 if (r < 0)
212 return bus_log_create_error(r);
213
214 /* Name and Mode */
215 r = sd_bus_message_append(m, "ss", scope, "fail");
216 if (r < 0)
217 return bus_log_create_error(r);
218
219 /* Properties */
220 r = sd_bus_message_open_container(m, 'a', "(sv)");
221 if (r < 0)
222 return bus_log_create_error(r);
223
224 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
225 if (r < 0)
226 return bus_log_create_error(r);
227
228 r = sd_bus_message_append(m, "(sv)", "Delegate", "b", 1);
229 if (r < 0)
230 return bus_log_create_error(r);
231
232 r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed");
233 if (r < 0)
234 return bus_log_create_error(r);
235
236 r = sd_bus_message_close_container(m);
237 if (r < 0)
238 return bus_log_create_error(r);
239
240 /* Auxiliary units */
241 r = sd_bus_message_append(m, "a(sa(sv))", 0);
242 if (r < 0)
243 return bus_log_create_error(r);
244
245 r = sd_bus_call(bus, m, 0, &error, &reply);
246 if (r < 0)
247 return log_error_errno(r, "Failed to start transient scope unit: %s", bus_error_message(&error, r));
248
249 r = sd_bus_message_read(reply, "o", &object);
250 if (r < 0)
251 return bus_log_parse_error(r);
252
253 r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL);
254 if (r < 0)
255 return r;
256
257 return 0;
258 }
259
260 static int enter_cgroup(char **ret_cgroup, bool enter_subroot) {
261 _cleanup_free_ char *cgroup_root = NULL, *cgroup_subroot = NULL;
262 CGroupMask supported;
263 int r;
264
265 r = allocate_scope();
266 if (r < 0)
267 log_warning_errno(r, "Couldn't allocate a scope unit for this test, proceeding without.");
268
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");
272 assert(r >= 0);
273
274 if (enter_subroot)
275 assert_se(asprintf(&cgroup_subroot, "%s/%" PRIx64, cgroup_root, random_u64()) >= 0);
276 else {
277 cgroup_subroot = strdup(cgroup_root);
278 assert_se(cgroup_subroot != NULL);
279 }
280
281 assert_se(cg_mask_supported(&supported) >= 0);
282
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. */
285
286 r = cg_create_everywhere(supported, _CGROUP_MASK_ALL, cgroup_subroot);
287 if (r < 0)
288 return r;
289
290 r = cg_attach_everywhere(supported, cgroup_subroot, 0, NULL, NULL);
291 if (r < 0)
292 return r;
293
294 if (ret_cgroup)
295 *ret_cgroup = TAKE_PTR(cgroup_subroot);
296
297 return 0;
298 }
299
300 int enter_cgroup_subroot(char **ret_cgroup) {
301 return enter_cgroup(ret_cgroup, true);
302 }
303
304 int enter_cgroup_root(char **ret_cgroup) {
305 return enter_cgroup(ret_cgroup, false);
306 }
307
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;
313 int r;
314
315 if (ans != POINTER_MAX)
316 return ans;
317
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);
322
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");
333
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
338 */
339 r = getenv_bool(var);
340 if (r > 0)
341 return (ans = "unknown"); /* Some other unknown thing */
342 if (r == 0)
343 return (ans = NULL);
344 }
345
346 return (ans = NULL);
347 }