]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/tests.c
ethtool: add several new link modes
[thirdparty/systemd.git] / src / shared / tests.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 #include <util.h>
10
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(). */
14 #include <libgen.h>
15 #undef basename
16
17 #include "sd-bus.h"
18
19 #include "alloc-util.h"
20 #include "bus-error.h"
21 #include "bus-locator.h"
22 #include "bus-util.h"
23 #include "bus-wait-for-jobs.h"
24 #include "cgroup-setup.h"
25 #include "cgroup-util.h"
26 #include "env-file.h"
27 #include "env-util.h"
28 #include "fs-util.h"
29 #include "log.h"
30 #include "namespace-util.h"
31 #include "path-util.h"
32 #include "process-util.h"
33 #include "random-util.h"
34 #include "strv.h"
35 #include "tests.h"
36
37 char* setup_fake_runtime_dir(void) {
38 char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
39
40 assert_se(mkdtemp(t));
41 assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0);
42 assert_se(p = strdup(t));
43
44 return p;
45 }
46
47 static void load_testdata_env(void) {
48 static bool called = false;
49 _cleanup_free_ char *s = NULL;
50 _cleanup_free_ char *envpath = NULL;
51 _cleanup_strv_free_ char **pairs = NULL;
52 char **k, **v;
53
54 if (called)
55 return;
56 called = true;
57
58 assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0);
59 dirname(s);
60
61 envpath = path_join(s, "systemd-runtest.env");
62 if (load_env_file_pairs(NULL, envpath, &pairs) < 0)
63 return;
64
65 STRV_FOREACH_PAIR(k, v, pairs)
66 setenv(*k, *v, 0);
67 }
68
69 int get_testdata_dir(const char *suffix, char **ret) {
70 const char *dir;
71 char *p;
72
73 load_testdata_env();
74
75 /* if the env var is set, use that */
76 dir = getenv("SYSTEMD_TEST_DATA");
77 if (!dir)
78 dir = SYSTEMD_TEST_DATA;
79 if (access(dir, F_OK) < 0)
80 return log_error_errno(errno, "ERROR: $SYSTEMD_TEST_DATA directory [%s] not accessible: %m", dir);
81
82 p = path_join(dir, suffix);
83 if (!p)
84 return log_oom();
85
86 *ret = p;
87 return 0;
88 }
89
90 const char* get_catalog_dir(void) {
91 const char *env;
92
93 load_testdata_env();
94
95 /* if the env var is set, use that */
96 env = getenv("SYSTEMD_CATALOG_DIR");
97 if (!env)
98 env = SYSTEMD_CATALOG_DIR;
99 if (access(env, F_OK) < 0) {
100 fprintf(stderr, "ERROR: $SYSTEMD_CATALOG_DIR directory [%s] does not exist\n", env);
101 exit(EXIT_FAILURE);
102 }
103 return env;
104 }
105
106 bool slow_tests_enabled(void) {
107 int r;
108
109 r = getenv_bool("SYSTEMD_SLOW_TESTS");
110 if (r >= 0)
111 return r;
112
113 if (r != -ENXIO)
114 log_warning_errno(r, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring.");
115 return SYSTEMD_SLOW_TESTS_DEFAULT;
116 }
117
118 void test_setup_logging(int level) {
119 log_set_max_level(level);
120 log_parse_environment();
121 log_open();
122 }
123
124 int log_tests_skipped(const char *message) {
125 log_notice("%s: %s, skipping tests.",
126 program_invocation_short_name, message);
127 return EXIT_TEST_SKIP;
128 }
129
130 int log_tests_skipped_errno(int r, const char *message) {
131 log_notice_errno(r, "%s: %s, skipping tests: %m",
132 program_invocation_short_name, message);
133 return EXIT_TEST_SKIP;
134 }
135
136 bool have_namespaces(void) {
137 siginfo_t si = {};
138 pid_t pid;
139
140 /* Checks whether namespaces are available. In some cases they aren't. We do this by calling unshare(), and we
141 * do so in a child process in order not to affect our own process. */
142
143 pid = fork();
144 assert_se(pid >= 0);
145
146 if (pid == 0) {
147 /* child */
148 if (detach_mount_namespace() < 0)
149 _exit(EXIT_FAILURE);
150
151 _exit(EXIT_SUCCESS);
152 }
153
154 assert_se(waitid(P_PID, pid, &si, WEXITED) >= 0);
155 assert_se(si.si_code == CLD_EXITED);
156
157 if (si.si_status == EXIT_SUCCESS)
158 return true;
159
160 if (si.si_status == EXIT_FAILURE)
161 return false;
162
163 assert_not_reached("unexpected exit code");
164 }
165
166 bool can_memlock(void) {
167 /* Let's see if we can mlock() a larger blob of memory. BPF programs are charged against
168 * RLIMIT_MEMLOCK, hence let's first make sure we can lock memory at all, and skip the test if we
169 * cannot. Why not check RLIMIT_MEMLOCK explicitly? Because in container environments the
170 * RLIMIT_MEMLOCK value we see might not match the RLIMIT_MEMLOCK value actually in effect. */
171
172 void *p = mmap(NULL, CAN_MEMLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
173 if (p == MAP_FAILED)
174 return false;
175
176 bool b = mlock(p, CAN_MEMLOCK_SIZE) >= 0;
177 if (b)
178 assert_se(munlock(p, CAN_MEMLOCK_SIZE) >= 0);
179
180 assert_se(munmap(p, CAN_MEMLOCK_SIZE) >= 0);
181 return b;
182 }
183
184 static int allocate_scope(void) {
185 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
186 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
187 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
188 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
189 _cleanup_free_ char *scope = NULL;
190 const char *object;
191 int r;
192
193 /* Let's try to run this test in a scope of its own, with delegation turned on, so that PID 1 doesn't
194 * interfere with our cgroup management. */
195
196 r = sd_bus_default_system(&bus);
197 if (r < 0)
198 return log_error_errno(r, "Failed to connect to system bus: %m");
199
200 r = bus_wait_for_jobs_new(bus, &w);
201 if (r < 0)
202 return log_oom();
203
204 if (asprintf(&scope, "%s-%" PRIx64 ".scope", program_invocation_short_name, random_u64()) < 0)
205 return log_oom();
206
207 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
208 if (r < 0)
209 return bus_log_create_error(r);
210
211 /* Name and Mode */
212 r = sd_bus_message_append(m, "ss", scope, "fail");
213 if (r < 0)
214 return bus_log_create_error(r);
215
216 /* Properties */
217 r = sd_bus_message_open_container(m, 'a', "(sv)");
218 if (r < 0)
219 return bus_log_create_error(r);
220
221 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
222 if (r < 0)
223 return bus_log_create_error(r);
224
225 r = sd_bus_message_append(m, "(sv)", "Delegate", "b", 1);
226 if (r < 0)
227 return bus_log_create_error(r);
228
229 r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed");
230 if (r < 0)
231 return bus_log_create_error(r);
232
233 r = sd_bus_message_close_container(m);
234 if (r < 0)
235 return bus_log_create_error(r);
236
237 /* Auxiliary units */
238 r = sd_bus_message_append(m, "a(sa(sv))", 0);
239 if (r < 0)
240 return bus_log_create_error(r);
241
242 r = sd_bus_call(bus, m, 0, &error, &reply);
243 if (r < 0)
244 return log_error_errno(r, "Failed to start transient scope unit: %s", bus_error_message(&error, r));
245
246 r = sd_bus_message_read(reply, "o", &object);
247 if (r < 0)
248 return bus_log_parse_error(r);
249
250 r = bus_wait_for_jobs_one(w, object, false);
251 if (r < 0)
252 return r;
253
254 return 0;
255 }
256
257 int enter_cgroup_subroot(char **ret_cgroup) {
258 _cleanup_free_ char *cgroup_root = NULL, *cgroup_subroot = NULL;
259 CGroupMask supported;
260 int r;
261
262 r = allocate_scope();
263 if (r < 0)
264 log_warning_errno(r, "Couldn't allocate a scope unit for this test, proceeding without.");
265
266 r = cg_pid_get_path(NULL, 0, &cgroup_root);
267 if (r == -ENOMEDIUM)
268 return log_warning_errno(r, "cg_pid_get_path(NULL, 0, ...) failed: %m");
269 assert(r >= 0);
270
271 assert_se(asprintf(&cgroup_subroot, "%s/%" PRIx64, cgroup_root, random_u64()) >= 0);
272 assert_se(cg_mask_supported(&supported) >= 0);
273
274 /* If this fails, then we don't mind as the later cgroup operations will fail too, and it's fine if
275 * we handle any errors at that point. */
276
277 r = cg_create_everywhere(supported, _CGROUP_MASK_ALL, cgroup_subroot);
278 if (r < 0)
279 return r;
280
281 r = cg_attach_everywhere(supported, cgroup_subroot, 0, NULL, NULL);
282 if (r < 0)
283 return r;
284
285 if (ret_cgroup)
286 *ret_cgroup = TAKE_PTR(cgroup_subroot);
287
288 return 0;
289 }