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