]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/tests.c
Merge pull request #14592 from keszybz/simplifications
[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 "alloc-util.h"
18 #include "cgroup-setup.h"
19 #include "cgroup-util.h"
20 #include "env-file.h"
21 #include "env-util.h"
22 #include "fs-util.h"
23 #include "log.h"
24 #include "path-util.h"
25 #include "random-util.h"
26 #include "strv.h"
27 #include "tests.h"
28
29 char* setup_fake_runtime_dir(void) {
30 char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
31
32 assert_se(mkdtemp(t));
33 assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0);
34 assert_se(p = strdup(t));
35
36 return p;
37 }
38
39 static void load_testdata_env(void) {
40 static bool called = false;
41 _cleanup_free_ char *s = NULL;
42 _cleanup_free_ char *envpath = NULL;
43 _cleanup_strv_free_ char **pairs = NULL;
44 char **k, **v;
45
46 if (called)
47 return;
48 called = true;
49
50 assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0);
51 dirname(s);
52
53 envpath = path_join(s, "systemd-runtest.env");
54 if (load_env_file_pairs(NULL, envpath, &pairs) < 0)
55 return;
56
57 STRV_FOREACH_PAIR(k, v, pairs)
58 setenv(*k, *v, 0);
59 }
60
61 const char* get_testdata_dir(void) {
62 const char *env;
63
64 load_testdata_env();
65
66 /* if the env var is set, use that */
67 env = getenv("SYSTEMD_TEST_DATA");
68 if (!env)
69 env = SYSTEMD_TEST_DATA;
70 if (access(env, F_OK) < 0) {
71 fprintf(stderr, "ERROR: $SYSTEMD_TEST_DATA directory [%s] does not exist\n", env);
72 exit(EXIT_FAILURE);
73 }
74
75 return env;
76 }
77
78 const char* get_catalog_dir(void) {
79 const char *env;
80
81 load_testdata_env();
82
83 /* if the env var is set, use that */
84 env = getenv("SYSTEMD_CATALOG_DIR");
85 if (!env)
86 env = SYSTEMD_CATALOG_DIR;
87 if (access(env, F_OK) < 0) {
88 fprintf(stderr, "ERROR: $SYSTEMD_CATALOG_DIR directory [%s] does not exist\n", env);
89 exit(EXIT_FAILURE);
90 }
91 return env;
92 }
93
94 bool slow_tests_enabled(void) {
95 int r;
96
97 r = getenv_bool("SYSTEMD_SLOW_TESTS");
98 if (r >= 0)
99 return r;
100
101 if (r != -ENXIO)
102 log_warning_errno(r, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring.");
103 return SYSTEMD_SLOW_TESTS_DEFAULT;
104 }
105
106 void test_setup_logging(int level) {
107 log_set_max_level(level);
108 log_parse_environment();
109 log_open();
110 }
111
112 int log_tests_skipped(const char *message) {
113 log_notice("%s: %s, skipping tests.",
114 program_invocation_short_name, message);
115 return EXIT_TEST_SKIP;
116 }
117
118 int log_tests_skipped_errno(int r, const char *message) {
119 log_notice_errno(r, "%s: %s, skipping tests: %m",
120 program_invocation_short_name, message);
121 return EXIT_TEST_SKIP;
122 }
123
124 bool have_namespaces(void) {
125 siginfo_t si = {};
126 pid_t pid;
127
128 /* Checks whether namespaces are available. In some cases they aren't. We do this by calling unshare(), and we
129 * do so in a child process in order not to affect our own process. */
130
131 pid = fork();
132 assert_se(pid >= 0);
133
134 if (pid == 0) {
135 /* child */
136 if (unshare(CLONE_NEWNS) < 0)
137 _exit(EXIT_FAILURE);
138
139 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
140 _exit(EXIT_FAILURE);
141
142 _exit(EXIT_SUCCESS);
143 }
144
145 assert_se(waitid(P_PID, pid, &si, WEXITED) >= 0);
146 assert_se(si.si_code == CLD_EXITED);
147
148 if (si.si_status == EXIT_SUCCESS)
149 return true;
150
151 if (si.si_status == EXIT_FAILURE)
152 return false;
153
154 assert_not_reached("unexpected exit code");
155 }
156
157 bool can_memlock(void) {
158 /* Let's see if we can mlock() a larger blob of memory. BPF programs are charged against
159 * RLIMIT_MEMLOCK, hence let's first make sure we can lock memory at all, and skip the test if we
160 * cannot. Why not check RLIMIT_MEMLOCK explicitly? Because in container environments the
161 * RLIMIT_MEMLOCK value we see might not match the RLIMIT_MEMLOCK value actually in effect. */
162
163 void *p = mmap(NULL, CAN_MEMLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
164 if (p == MAP_FAILED)
165 return false;
166
167 bool b = mlock(p, CAN_MEMLOCK_SIZE) >= 0;
168 if (b)
169 assert_se(munlock(p, CAN_MEMLOCK_SIZE) >= 0);
170
171 assert_se(munmap(p, CAN_MEMLOCK_SIZE) >= 0);
172 return b;
173 }
174
175 int enter_cgroup_subroot(char **ret_cgroup) {
176 _cleanup_free_ char *cgroup_root = NULL, *cgroup_subroot = NULL;
177 CGroupMask supported;
178 int r;
179
180 r = cg_pid_get_path(NULL, 0, &cgroup_root);
181 if (r == -ENOMEDIUM)
182 return log_warning_errno(r, "cg_pid_get_path(NULL, 0, ...) failed: %m");
183 assert(r >= 0);
184
185 assert_se(asprintf(&cgroup_subroot, "%s/%" PRIx64, cgroup_root, random_u64()) >= 0);
186 assert_se(cg_mask_supported(&supported) >= 0);
187
188 /* If this fails, then we don't mind as the later cgroup operations will fail too, and it's fine if
189 * we handle any errors at that point. */
190
191 r = cg_create_everywhere(supported, _CGROUP_MASK_ALL, cgroup_subroot);
192 if (r < 0)
193 return r;
194
195 r = cg_attach_everywhere(supported, cgroup_subroot, 0, NULL, NULL);
196 if (r < 0)
197 return r;
198
199 if (ret_cgroup)
200 *ret_cgroup = TAKE_PTR(cgroup_subroot);
201 return 0;
202 }