]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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 LP |
9 | #include <util.h> |
10 | ||
795919ef LP |
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 | ||
e2d41370 | 17 | #include "alloc-util.h" |
48e98ba5 ZJS |
18 | #include "cgroup-setup.h" |
19 | #include "cgroup-util.h" | |
686d13b9 | 20 | #include "env-file.h" |
0cf29baa | 21 | #include "env-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" |
48e98ba5 | 26 | #include "random-util.h" |
e2d41370 FB |
27 | #include "strv.h" |
28 | #include "tests.h" | |
d2120590 LP |
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 | } | |
f853c6ef | 39 | |
e2d41370 FB |
40 | static void load_testdata_env(void) { |
41 | static bool called = false; | |
8cb10a4f | 42 | _cleanup_free_ char *s = NULL; |
e2d41370 FB |
43 | _cleanup_free_ char *envpath = NULL; |
44 | _cleanup_strv_free_ char **pairs = NULL; | |
45 | char **k, **v; | |
8cb10a4f | 46 | |
e2d41370 FB |
47 | if (called) |
48 | return; | |
49 | called = true; | |
8cb10a4f YW |
50 | |
51 | assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0); | |
e2d41370 | 52 | dirname(s); |
8cb10a4f | 53 | |
62a85ee0 | 54 | envpath = path_join(s, "systemd-runtest.env"); |
aa8fbc74 | 55 | if (load_env_file_pairs(NULL, envpath, &pairs) < 0) |
e2d41370 | 56 | return; |
8cb10a4f | 57 | |
e2d41370 FB |
58 | STRV_FOREACH_PAIR(k, v, pairs) |
59 | setenv(*k, *v, 0); | |
60 | } | |
61 | ||
7b432953 ZJS |
62 | int get_testdata_dir(const char *suffix, char **ret) { |
63 | const char *dir; | |
64 | char *p; | |
e2d41370 FB |
65 | |
66 | load_testdata_env(); | |
f853c6ef | 67 | |
c60b6dda | 68 | /* if the env var is set, use that */ |
7b432953 ZJS |
69 | dir = getenv("SYSTEMD_TEST_DATA"); |
70 | if (!dir) | |
71 | dir = SYSTEMD_TEST_DATA; | |
72 | if (access(dir, F_OK) < 0) | |
162392b7 | 73 | return log_error_errno(errno, "ERROR: $SYSTEMD_TEST_DATA directory [%s] not accessible: %m", dir); |
7b432953 ZJS |
74 | |
75 | p = path_join(dir, suffix); | |
76 | if (!p) | |
77 | return log_oom(); | |
78 | ||
79 | *ret = p; | |
80 | return 0; | |
f853c6ef | 81 | } |
49cdae63 FB |
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 | } | |
0cf29baa ZJS |
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 | } | |
317bb217 | 110 | |
6d7c4033 ZJS |
111 | void test_setup_logging(int level) { |
112 | log_set_max_level(level); | |
113 | log_parse_environment(); | |
114 | log_open(); | |
115 | } | |
116 | ||
317bb217 ZJS |
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 | } | |
730d989a ZJS |
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 | } | |
a4bc3c1d LP |
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 */ | |
e2ec9c4d | 141 | if (detach_mount_namespace() < 0) |
a4bc3c1d LP |
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 | } | |
77abd029 ZJS |
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 | } | |
48e98ba5 | 176 | |
64ad9e08 | 177 | int enter_cgroup_subroot(char **ret_cgroup) { |
48e98ba5 ZJS |
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 | ||
64ad9e08 ZJS |
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; | |
48e98ba5 | 204 | } |