]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
d2120590 | 2 | |
a4bc3c1d LP |
3 | #include <sched.h> |
4 | #include <signal.h> | |
d2120590 | 5 | #include <stdlib.h> |
77abd029 | 6 | #include <sys/mman.h> |
a4bc3c1d LP |
7 | #include <sys/mount.h> |
8 | #include <sys/wait.h> | |
d2120590 | 9 | |
b7b7ace4 LP |
10 | #include "sd-bus.h" |
11 | ||
e2d41370 | 12 | #include "alloc-util.h" |
b7b7ace4 LP |
13 | #include "bus-error.h" |
14 | #include "bus-locator.h" | |
15 | #include "bus-util.h" | |
16 | #include "bus-wait-for-jobs.h" | |
48e98ba5 ZJS |
17 | #include "cgroup-setup.h" |
18 | #include "cgroup-util.h" | |
686d13b9 | 19 | #include "env-file.h" |
0cf29baa | 20 | #include "env-util.h" |
367c47c8 | 21 | #include "fd-util.h" |
eca27ebb | 22 | #include "fs-util.h" |
317bb217 | 23 | #include "log.h" |
e2ec9c4d | 24 | #include "namespace-util.h" |
1f35a3b2 | 25 | #include "path-util.h" |
b7b7ace4 | 26 | #include "process-util.h" |
48e98ba5 | 27 | #include "random-util.h" |
e2d41370 FB |
28 | #include "strv.h" |
29 | #include "tests.h" | |
367c47c8 | 30 | #include "tmpfile-util.h" |
d2120590 LP |
31 | |
32 | char* setup_fake_runtime_dir(void) { | |
33 | char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p; | |
34 | ||
35 | assert_se(mkdtemp(t)); | |
36 | assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0); | |
37 | assert_se(p = strdup(t)); | |
38 | ||
39 | return p; | |
40 | } | |
f853c6ef | 41 | |
e2d41370 FB |
42 | static void load_testdata_env(void) { |
43 | static bool called = false; | |
8419213d | 44 | _cleanup_free_ char *s = NULL, *d = NULL, *envpath = NULL; |
e2d41370 | 45 | _cleanup_strv_free_ char **pairs = NULL; |
8419213d | 46 | int r; |
8cb10a4f | 47 | |
e2d41370 FB |
48 | if (called) |
49 | return; | |
50 | called = true; | |
8cb10a4f YW |
51 | |
52 | assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0); | |
8419213d LP |
53 | assert_se(path_extract_directory(s, &d) >= 0); |
54 | assert_se(envpath = path_join(d, "systemd-runtest.env")); | |
8cb10a4f | 55 | |
8419213d LP |
56 | r = load_env_file_pairs(NULL, envpath, &pairs); |
57 | if (r < 0) { | |
58 | log_debug_errno(r, "Reading %s failed: %m", envpath); | |
e2d41370 | 59 | return; |
8419213d | 60 | } |
8cb10a4f | 61 | |
e2d41370 | 62 | STRV_FOREACH_PAIR(k, v, pairs) |
8419213d | 63 | assert_se(setenv(*k, *v, 0) >= 0); |
e2d41370 FB |
64 | } |
65 | ||
7b432953 ZJS |
66 | int get_testdata_dir(const char *suffix, char **ret) { |
67 | const char *dir; | |
68 | char *p; | |
e2d41370 FB |
69 | |
70 | load_testdata_env(); | |
f853c6ef | 71 | |
c60b6dda | 72 | /* if the env var is set, use that */ |
7b432953 ZJS |
73 | dir = getenv("SYSTEMD_TEST_DATA"); |
74 | if (!dir) | |
75 | dir = SYSTEMD_TEST_DATA; | |
76 | if (access(dir, F_OK) < 0) | |
162392b7 | 77 | return log_error_errno(errno, "ERROR: $SYSTEMD_TEST_DATA directory [%s] not accessible: %m", dir); |
7b432953 ZJS |
78 | |
79 | p = path_join(dir, suffix); | |
80 | if (!p) | |
81 | return log_oom(); | |
82 | ||
83 | *ret = p; | |
84 | return 0; | |
f853c6ef | 85 | } |
49cdae63 FB |
86 | |
87 | const char* get_catalog_dir(void) { | |
88 | const char *env; | |
89 | ||
90 | load_testdata_env(); | |
91 | ||
92 | /* if the env var is set, use that */ | |
93 | env = getenv("SYSTEMD_CATALOG_DIR"); | |
94 | if (!env) | |
95 | env = SYSTEMD_CATALOG_DIR; | |
96 | if (access(env, F_OK) < 0) { | |
97 | fprintf(stderr, "ERROR: $SYSTEMD_CATALOG_DIR directory [%s] does not exist\n", env); | |
98 | exit(EXIT_FAILURE); | |
99 | } | |
100 | return env; | |
101 | } | |
0cf29baa ZJS |
102 | |
103 | bool slow_tests_enabled(void) { | |
104 | int r; | |
105 | ||
106 | r = getenv_bool("SYSTEMD_SLOW_TESTS"); | |
107 | if (r >= 0) | |
108 | return r; | |
109 | ||
110 | if (r != -ENXIO) | |
111 | log_warning_errno(r, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring."); | |
112 | return SYSTEMD_SLOW_TESTS_DEFAULT; | |
113 | } | |
317bb217 | 114 | |
6d7c4033 ZJS |
115 | void test_setup_logging(int level) { |
116 | log_set_max_level(level); | |
117 | log_parse_environment(); | |
118 | log_open(); | |
119 | } | |
120 | ||
367c47c8 | 121 | int write_tmpfile(char *pattern, const char *contents) { |
254d1313 | 122 | _cleanup_close_ int fd = -EBADF; |
367c47c8 ZJS |
123 | |
124 | assert(pattern); | |
125 | assert(contents); | |
126 | ||
127 | fd = mkostemp_safe(pattern); | |
128 | if (fd < 0) | |
129 | return fd; | |
130 | ||
131 | ssize_t l = strlen(contents); | |
132 | errno = 0; | |
133 | if (write(fd, contents, l) != l) | |
134 | return errno_or_else(EIO); | |
135 | return 0; | |
136 | } | |
137 | ||
a4bc3c1d LP |
138 | bool have_namespaces(void) { |
139 | siginfo_t si = {}; | |
140 | pid_t pid; | |
141 | ||
142 | /* Checks whether namespaces are available. In some cases they aren't. We do this by calling unshare(), and we | |
143 | * do so in a child process in order not to affect our own process. */ | |
144 | ||
145 | pid = fork(); | |
146 | assert_se(pid >= 0); | |
147 | ||
148 | if (pid == 0) { | |
149 | /* child */ | |
e2ec9c4d | 150 | if (detach_mount_namespace() < 0) |
a4bc3c1d LP |
151 | _exit(EXIT_FAILURE); |
152 | ||
153 | _exit(EXIT_SUCCESS); | |
154 | } | |
155 | ||
156 | assert_se(waitid(P_PID, pid, &si, WEXITED) >= 0); | |
157 | assert_se(si.si_code == CLD_EXITED); | |
158 | ||
159 | if (si.si_status == EXIT_SUCCESS) | |
160 | return true; | |
161 | ||
162 | if (si.si_status == EXIT_FAILURE) | |
163 | return false; | |
164 | ||
04499a70 | 165 | assert_not_reached(); |
a4bc3c1d | 166 | } |
77abd029 ZJS |
167 | |
168 | bool can_memlock(void) { | |
169 | /* Let's see if we can mlock() a larger blob of memory. BPF programs are charged against | |
170 | * RLIMIT_MEMLOCK, hence let's first make sure we can lock memory at all, and skip the test if we | |
171 | * cannot. Why not check RLIMIT_MEMLOCK explicitly? Because in container environments the | |
172 | * RLIMIT_MEMLOCK value we see might not match the RLIMIT_MEMLOCK value actually in effect. */ | |
173 | ||
174 | void *p = mmap(NULL, CAN_MEMLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0); | |
175 | if (p == MAP_FAILED) | |
176 | return false; | |
177 | ||
178 | bool b = mlock(p, CAN_MEMLOCK_SIZE) >= 0; | |
179 | if (b) | |
180 | assert_se(munlock(p, CAN_MEMLOCK_SIZE) >= 0); | |
181 | ||
182 | assert_se(munmap(p, CAN_MEMLOCK_SIZE) >= 0); | |
183 | return b; | |
184 | } | |
48e98ba5 | 185 | |
b7b7ace4 LP |
186 | static int allocate_scope(void) { |
187 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
188 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
189 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; | |
190 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
191 | _cleanup_free_ char *scope = NULL; | |
192 | const char *object; | |
193 | int r; | |
194 | ||
195 | /* Let's try to run this test in a scope of its own, with delegation turned on, so that PID 1 doesn't | |
196 | * interfere with our cgroup management. */ | |
197 | ||
198 | r = sd_bus_default_system(&bus); | |
199 | if (r < 0) | |
200 | return log_error_errno(r, "Failed to connect to system bus: %m"); | |
201 | ||
202 | r = bus_wait_for_jobs_new(bus, &w); | |
203 | if (r < 0) | |
f3f8bd6a | 204 | return log_error_errno(r, "Could not watch jobs: %m"); |
b7b7ace4 LP |
205 | |
206 | if (asprintf(&scope, "%s-%" PRIx64 ".scope", program_invocation_short_name, random_u64()) < 0) | |
207 | return log_oom(); | |
208 | ||
209 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit"); | |
210 | if (r < 0) | |
211 | return bus_log_create_error(r); | |
212 | ||
213 | /* Name and Mode */ | |
214 | r = sd_bus_message_append(m, "ss", scope, "fail"); | |
215 | if (r < 0) | |
216 | return bus_log_create_error(r); | |
217 | ||
218 | /* Properties */ | |
219 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
220 | if (r < 0) | |
221 | return bus_log_create_error(r); | |
222 | ||
223 | r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached()); | |
224 | if (r < 0) | |
225 | return bus_log_create_error(r); | |
226 | ||
227 | r = sd_bus_message_append(m, "(sv)", "Delegate", "b", 1); | |
228 | if (r < 0) | |
229 | return bus_log_create_error(r); | |
230 | ||
231 | r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed"); | |
232 | if (r < 0) | |
233 | return bus_log_create_error(r); | |
234 | ||
235 | r = sd_bus_message_close_container(m); | |
236 | if (r < 0) | |
237 | return bus_log_create_error(r); | |
238 | ||
239 | /* Auxiliary units */ | |
240 | r = sd_bus_message_append(m, "a(sa(sv))", 0); | |
241 | if (r < 0) | |
242 | return bus_log_create_error(r); | |
243 | ||
244 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
245 | if (r < 0) | |
246 | return log_error_errno(r, "Failed to start transient scope unit: %s", bus_error_message(&error, r)); | |
247 | ||
248 | r = sd_bus_message_read(reply, "o", &object); | |
249 | if (r < 0) | |
250 | return bus_log_parse_error(r); | |
251 | ||
e22ad53d | 252 | r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL); |
b7b7ace4 LP |
253 | if (r < 0) |
254 | return r; | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
61ff7397 | 259 | static int enter_cgroup(char **ret_cgroup, bool enter_subroot) { |
48e98ba5 ZJS |
260 | _cleanup_free_ char *cgroup_root = NULL, *cgroup_subroot = NULL; |
261 | CGroupMask supported; | |
262 | int r; | |
263 | ||
b7b7ace4 LP |
264 | r = allocate_scope(); |
265 | if (r < 0) | |
266 | log_warning_errno(r, "Couldn't allocate a scope unit for this test, proceeding without."); | |
267 | ||
48e98ba5 ZJS |
268 | r = cg_pid_get_path(NULL, 0, &cgroup_root); |
269 | if (r == -ENOMEDIUM) | |
270 | return log_warning_errno(r, "cg_pid_get_path(NULL, 0, ...) failed: %m"); | |
271 | assert(r >= 0); | |
272 | ||
61ff7397 AZ |
273 | if (enter_subroot) |
274 | assert_se(asprintf(&cgroup_subroot, "%s/%" PRIx64, cgroup_root, random_u64()) >= 0); | |
275 | else { | |
276 | cgroup_subroot = strdup(cgroup_root); | |
277 | assert_se(cgroup_subroot != NULL); | |
278 | } | |
279 | ||
48e98ba5 ZJS |
280 | assert_se(cg_mask_supported(&supported) >= 0); |
281 | ||
282 | /* If this fails, then we don't mind as the later cgroup operations will fail too, and it's fine if | |
283 | * we handle any errors at that point. */ | |
284 | ||
285 | r = cg_create_everywhere(supported, _CGROUP_MASK_ALL, cgroup_subroot); | |
286 | if (r < 0) | |
287 | return r; | |
288 | ||
64ad9e08 ZJS |
289 | r = cg_attach_everywhere(supported, cgroup_subroot, 0, NULL, NULL); |
290 | if (r < 0) | |
291 | return r; | |
292 | ||
293 | if (ret_cgroup) | |
294 | *ret_cgroup = TAKE_PTR(cgroup_subroot); | |
b7b7ace4 | 295 | |
64ad9e08 | 296 | return 0; |
48e98ba5 | 297 | } |
61ff7397 AZ |
298 | |
299 | int enter_cgroup_subroot(char **ret_cgroup) { | |
300 | return enter_cgroup(ret_cgroup, true); | |
301 | } | |
302 | ||
303 | int enter_cgroup_root(char **ret_cgroup) { | |
304 | return enter_cgroup(ret_cgroup, false); | |
305 | } | |
4eb0c875 ZJS |
306 | |
307 | const char *ci_environment(void) { | |
308 | /* We return a string because we might want to provide multiple bits of information later on: not | |
309 | * just the general CI environment type, but also whether we're sanitizing or not, etc. The caller is | |
310 | * expected to use strstr on the returned value. */ | |
311 | static const char *ans = POINTER_MAX; | |
4eb0c875 ZJS |
312 | int r; |
313 | ||
314 | if (ans != POINTER_MAX) | |
315 | return ans; | |
316 | ||
317 | /* We allow specifying the environment with $CITYPE. Nobody uses this so far, but we are ready. */ | |
5980d463 ZJS |
318 | const char *citype = getenv("CITYPE"); |
319 | if (!isempty(citype)) | |
320 | return (ans = citype); | |
4eb0c875 ZJS |
321 | |
322 | if (getenv_bool("TRAVIS") > 0) | |
323 | return (ans = "travis"); | |
324 | if (getenv_bool("SEMAPHORE") > 0) | |
325 | return (ans = "semaphore"); | |
326 | if (getenv_bool("GITHUB_ACTIONS") > 0) | |
327 | return (ans = "github-actions"); | |
328 | if (getenv("AUTOPKGTEST_ARTIFACTS") || getenv("AUTOPKGTEST_TMP")) | |
329 | return (ans = "autopkgtest"); | |
a0b0b670 LB |
330 | if (getenv("SALSA_CI_IMAGES")) |
331 | return (ans = "salsa-ci"); | |
4eb0c875 | 332 | |
5980d463 | 333 | FOREACH_STRING(var, "CI", "CONTINOUS_INTEGRATION") { |
4eb0c875 ZJS |
334 | /* Those vars are booleans according to Semaphore and Travis docs: |
335 | * https://docs.travis-ci.com/user/environment-variables/#default-environment-variables | |
336 | * https://docs.semaphoreci.com/ci-cd-environment/environment-variables/#ci | |
337 | */ | |
5980d463 | 338 | r = getenv_bool(var); |
4eb0c875 ZJS |
339 | if (r > 0) |
340 | return (ans = "unknown"); /* Some other unknown thing */ | |
341 | if (r == 0) | |
342 | return (ans = NULL); | |
343 | } | |
344 | ||
345 | return (ans = NULL); | |
346 | } |