]>
Commit | Line | Data |
---|---|---|
281e05b6 RC |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 Ronny Chevalier | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
cc7fa4fb RC |
20 | #include <grp.h> |
21 | #include <pwd.h> | |
ff4ca461 | 22 | #include <stdio.h> |
70d7aea5 | 23 | #include <sys/prctl.h> |
ff4ca461 | 24 | #include <sys/types.h> |
281e05b6 | 25 | |
03bd70dd | 26 | #include "fileio.h" |
f4f15635 | 27 | #include "fs-util.h" |
281e05b6 | 28 | #include "macro.h" |
f4f15635 | 29 | #include "manager.h" |
281e05b6 | 30 | #include "mkdir.h" |
ff4ca461 | 31 | #include "path-util.h" |
c6878637 | 32 | #include "rm-rf.h" |
8b3aa503 | 33 | #include "test-helper.h" |
f4f15635 LP |
34 | #include "unit.h" |
35 | #include "util.h" | |
281e05b6 RC |
36 | |
37 | typedef void (*test_function_t)(Manager *m); | |
38 | ||
39 | static void check(Manager *m, Unit *unit, int status_expected, int code_expected) { | |
40 | Service *service = NULL; | |
41 | usec_t ts; | |
42 | usec_t timeout = 2 * USEC_PER_SEC; | |
43 | ||
44 | assert_se(m); | |
45 | assert_se(unit); | |
46 | ||
47 | service = SERVICE(unit); | |
48 | printf("%s\n", unit->id); | |
49 | exec_context_dump(&service->exec_context, stdout, "\t"); | |
50 | ts = now(CLOCK_MONOTONIC); | |
51 | while (service->state != SERVICE_DEAD && service->state != SERVICE_FAILED) { | |
52 | int r; | |
53 | usec_t n; | |
54 | ||
55 | r = sd_event_run(m->event, 100 * USEC_PER_MSEC); | |
56 | assert_se(r >= 0); | |
57 | ||
58 | n = now(CLOCK_MONOTONIC); | |
59 | if (ts + timeout < n) { | |
60 | log_error("Test timeout when testing %s", unit->id); | |
61 | exit(EXIT_FAILURE); | |
62 | } | |
63 | } | |
64 | exec_status_dump(&service->main_exec_status, stdout, "\t"); | |
65 | assert_se(service->main_exec_status.status == status_expected); | |
66 | assert_se(service->main_exec_status.code == code_expected); | |
67 | } | |
68 | ||
69 | static void test(Manager *m, const char *unit_name, int status_expected, int code_expected) { | |
70 | Unit *unit; | |
71 | ||
72 | assert_se(unit_name); | |
73 | ||
74 | assert_se(manager_load_unit(m, unit_name, NULL, NULL, &unit) >= 0); | |
75 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); | |
76 | check(m, unit, status_expected, code_expected); | |
77 | } | |
78 | ||
79 | static void test_exec_workingdirectory(Manager *m) { | |
80 | assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0); | |
81 | ||
82 | test(m, "exec-workingdirectory.service", 0, CLD_EXITED); | |
83 | ||
c6878637 | 84 | (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL); |
281e05b6 RC |
85 | } |
86 | ||
87 | static void test_exec_personality(Manager *m) { | |
281e05b6 RC |
88 | #if defined(__x86_64__) |
89 | test(m, "exec-personality-x86-64.service", 0, CLD_EXITED); | |
7517f51e HB |
90 | |
91 | #elif defined(__s390__) | |
92 | test(m, "exec-personality-s390.service", 0, CLD_EXITED); | |
93 | ||
5798eb4c | 94 | #elif defined(__i386__) |
7517f51e | 95 | test(m, "exec-personality-x86.service", 0, CLD_EXITED); |
281e05b6 RC |
96 | #endif |
97 | } | |
98 | ||
99 | static void test_exec_ignoresigpipe(Manager *m) { | |
100 | test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED); | |
101 | test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED); | |
102 | } | |
103 | ||
104 | static void test_exec_privatetmp(Manager *m) { | |
105 | assert_se(touch("/tmp/test-exec_privatetmp") >= 0); | |
106 | ||
107 | test(m, "exec-privatetmp-yes.service", 0, CLD_EXITED); | |
108 | test(m, "exec-privatetmp-no.service", 0, CLD_EXITED); | |
109 | ||
110 | unlink("/tmp/test-exec_privatetmp"); | |
111 | } | |
112 | ||
113 | static void test_exec_privatedevices(Manager *m) { | |
114 | test(m, "exec-privatedevices-yes.service", 0, CLD_EXITED); | |
115 | test(m, "exec-privatedevices-no.service", 0, CLD_EXITED); | |
116 | } | |
117 | ||
118 | static void test_exec_systemcallfilter(Manager *m) { | |
119 | #ifdef HAVE_SECCOMP | |
120 | test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED); | |
121 | test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED); | |
122 | test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED); | |
123 | test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED); | |
124 | #endif | |
125 | } | |
126 | ||
127 | static void test_exec_systemcallerrornumber(Manager *m) { | |
128 | #ifdef HAVE_SECCOMP | |
129 | test(m, "exec-systemcallerrornumber.service", 1, CLD_EXITED); | |
130 | #endif | |
131 | } | |
132 | ||
19c0b0b9 RC |
133 | static void test_exec_systemcall_system_mode_with_user(Manager *m) { |
134 | #ifdef HAVE_SECCOMP | |
135 | if (getpwnam("nobody")) | |
136 | test(m, "exec-systemcallfilter-system-user.service", 0, CLD_EXITED); | |
137 | else | |
138 | log_error_errno(errno, "Skipping test_exec_systemcall_system_mode_with_user, could not find nobody user: %m"); | |
139 | #endif | |
140 | } | |
141 | ||
281e05b6 | 142 | static void test_exec_user(Manager *m) { |
cc7fa4fb RC |
143 | if (getpwnam("nobody")) |
144 | test(m, "exec-user.service", 0, CLD_EXITED); | |
ff4ca461 RC |
145 | else |
146 | log_error_errno(errno, "Skipping test_exec_user, could not find nobody user: %m"); | |
281e05b6 RC |
147 | } |
148 | ||
149 | static void test_exec_group(Manager *m) { | |
cc7fa4fb RC |
150 | if (getgrnam("nobody")) |
151 | test(m, "exec-group.service", 0, CLD_EXITED); | |
ff4ca461 RC |
152 | else |
153 | log_error_errno(errno, "Skipping test_exec_group, could not find nobody group: %m"); | |
281e05b6 RC |
154 | } |
155 | ||
156 | static void test_exec_environment(Manager *m) { | |
157 | test(m, "exec-environment.service", 0, CLD_EXITED); | |
158 | test(m, "exec-environment-multiple.service", 0, CLD_EXITED); | |
159 | test(m, "exec-environment-empty.service", 0, CLD_EXITED); | |
160 | } | |
161 | ||
03bd70dd RC |
162 | static void test_exec_environmentfile(Manager *m) { |
163 | static const char e[] = | |
164 | "VAR1='word1 word2'\n" | |
165 | "VAR2=word3 \n" | |
166 | "# comment1\n" | |
167 | "\n" | |
168 | "; comment2\n" | |
169 | " ; # comment3\n" | |
170 | "line without an equal\n" | |
171 | "VAR3='$word 5 6'\n"; | |
172 | int r; | |
173 | ||
174 | r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE); | |
175 | assert_se(r == 0); | |
176 | ||
177 | test(m, "exec-environmentfile.service", 0, CLD_EXITED); | |
178 | ||
179 | unlink("/tmp/test-exec_environmentfile.conf"); | |
180 | } | |
181 | ||
4c80d201 | 182 | static void test_exec_passenvironment(Manager *m) { |
e1abca2e FB |
183 | /* test-execute runs under MANAGER_USER which, by default, forwards all |
184 | * variables present in the environment, but only those that are | |
185 | * present _at the time it is created_! | |
186 | * | |
187 | * So these PassEnvironment checks are still expected to work, since we | |
188 | * are ensuring the variables are not present at manager creation (they | |
189 | * are unset explicitly in main) and are only set here. | |
190 | * | |
191 | * This is still a good approximation of how a test for MANAGER_SYSTEM | |
192 | * would work. | |
193 | */ | |
4c80d201 FB |
194 | assert_se(setenv("VAR1", "word1 word2", 1) == 0); |
195 | assert_se(setenv("VAR2", "word3", 1) == 0); | |
196 | assert_se(setenv("VAR3", "$word 5 6", 1) == 0); | |
197 | test(m, "exec-passenvironment.service", 0, CLD_EXITED); | |
198 | test(m, "exec-passenvironment-repeated.service", 0, CLD_EXITED); | |
199 | test(m, "exec-passenvironment-empty.service", 0, CLD_EXITED); | |
200 | assert_se(unsetenv("VAR1") == 0); | |
201 | assert_se(unsetenv("VAR2") == 0); | |
202 | assert_se(unsetenv("VAR3") == 0); | |
203 | test(m, "exec-passenvironment-absent.service", 0, CLD_EXITED); | |
204 | } | |
205 | ||
27c5347c RC |
206 | static void test_exec_umask(Manager *m) { |
207 | test(m, "exec-umask-default.service", 0, CLD_EXITED); | |
208 | test(m, "exec-umask-0177.service", 0, CLD_EXITED); | |
209 | } | |
210 | ||
cc3ddc85 RC |
211 | static void test_exec_runtimedirectory(Manager *m) { |
212 | test(m, "exec-runtimedirectory.service", 0, CLD_EXITED); | |
213 | test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED); | |
cc7fa4fb RC |
214 | if (getgrnam("nobody")) |
215 | test(m, "exec-runtimedirectory-owner.service", 0, CLD_EXITED); | |
ff4ca461 RC |
216 | else |
217 | log_error_errno(errno, "Skipping test_exec_runtimedirectory-owner, could not find nobody group: %m"); | |
218 | } | |
219 | ||
220 | static void test_exec_capabilityboundingset(Manager *m) { | |
221 | int r; | |
222 | ||
223 | /* We use capsh to test if the capabilities are | |
224 | * properly set, so be sure that it exists */ | |
225 | r = find_binary("capsh", NULL); | |
226 | if (r < 0) { | |
227 | log_error_errno(r, "Skipping test_exec_capabilityboundingset, could not find capsh binary: %m"); | |
228 | return; | |
229 | } | |
230 | ||
231 | test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED); | |
232 | test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED); | |
233 | test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED); | |
234 | test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED); | |
cc3ddc85 RC |
235 | } |
236 | ||
70d7aea5 IP |
237 | static void test_exec_capabilityambientset(Manager *m) { |
238 | int r; | |
239 | ||
240 | /* Check if the kernel has support for ambient capabilities. Run | |
241 | * the tests only if that's the case. Clearing all ambient | |
242 | * capabilities is fine, since we are expecting them to be unset | |
243 | * in the first place for the tests. */ | |
244 | r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0); | |
245 | if (r >= 0 || errno != EINVAL) { | |
246 | test(m, "exec-capabilityambientset.service", 0, CLD_EXITED); | |
247 | test(m, "exec-capabilityambientset-merge.service", 0, CLD_EXITED); | |
248 | } | |
249 | } | |
250 | ||
63447f11 RC |
251 | static void test_exec_privatenetwork(Manager *m) { |
252 | int r; | |
253 | ||
254 | r = find_binary("ip", NULL); | |
255 | if (r < 0) { | |
256 | log_error_errno(r, "Skipping test_exec_privatenetwork, could not find ip binary: %m"); | |
257 | return; | |
258 | } | |
259 | ||
260 | test(m, "exec-privatenetwork-yes.service", 0, CLD_EXITED); | |
261 | } | |
262 | ||
c388dfea RC |
263 | static void test_exec_oomscoreadjust(Manager *m) { |
264 | test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED); | |
265 | test(m, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED); | |
266 | } | |
267 | ||
a6226758 RC |
268 | static void test_exec_ioschedulingclass(Manager *m) { |
269 | test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED); | |
270 | test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED); | |
271 | test(m, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED); | |
272 | test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED); | |
273 | } | |
274 | ||
25254999 EV |
275 | static void test_exec_spec_interpolation(Manager *m) { |
276 | test(m, "exec-spec-interpolation.service", 0, CLD_EXITED); | |
277 | } | |
278 | ||
19c0b0b9 RC |
279 | static int run_tests(ManagerRunningAs running_as, test_function_t *tests) { |
280 | test_function_t *test = NULL; | |
281 | Manager *m = NULL; | |
282 | int r; | |
283 | ||
284 | assert_se(tests); | |
285 | ||
286 | r = manager_new(running_as, true, &m); | |
287 | if (MANAGER_SKIP_TEST(r)) { | |
288 | printf("Skipping test: manager_new: %s\n", strerror(-r)); | |
289 | return EXIT_TEST_SKIP; | |
290 | } | |
291 | assert_se(r >= 0); | |
292 | assert_se(manager_startup(m, NULL, NULL) >= 0); | |
293 | ||
294 | for (test = tests; test && *test; test++) | |
295 | (*test)(m); | |
296 | ||
297 | manager_free(m); | |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
281e05b6 | 302 | int main(int argc, char *argv[]) { |
19c0b0b9 | 303 | test_function_t user_tests[] = { |
281e05b6 RC |
304 | test_exec_workingdirectory, |
305 | test_exec_personality, | |
306 | test_exec_ignoresigpipe, | |
307 | test_exec_privatetmp, | |
308 | test_exec_privatedevices, | |
63447f11 | 309 | test_exec_privatenetwork, |
281e05b6 RC |
310 | test_exec_systemcallfilter, |
311 | test_exec_systemcallerrornumber, | |
312 | test_exec_user, | |
313 | test_exec_group, | |
314 | test_exec_environment, | |
03bd70dd | 315 | test_exec_environmentfile, |
4c80d201 | 316 | test_exec_passenvironment, |
27c5347c | 317 | test_exec_umask, |
cc3ddc85 | 318 | test_exec_runtimedirectory, |
ff4ca461 | 319 | test_exec_capabilityboundingset, |
70d7aea5 | 320 | test_exec_capabilityambientset, |
c388dfea | 321 | test_exec_oomscoreadjust, |
a6226758 | 322 | test_exec_ioschedulingclass, |
25254999 | 323 | test_exec_spec_interpolation, |
281e05b6 RC |
324 | NULL, |
325 | }; | |
19c0b0b9 RC |
326 | test_function_t system_tests[] = { |
327 | test_exec_systemcall_system_mode_with_user, | |
328 | NULL, | |
329 | }; | |
281e05b6 RC |
330 | int r; |
331 | ||
332 | log_parse_environment(); | |
333 | log_open(); | |
334 | ||
607ff5f9 DH |
335 | /* It is needed otherwise cgroup creation fails */ |
336 | if (getuid() != 0) { | |
337 | printf("Skipping test: not root\n"); | |
338 | return EXIT_TEST_SKIP; | |
339 | } | |
340 | ||
cc3ddc85 | 341 | assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0); |
ac400816 | 342 | assert_se(set_unit_path(TEST_DIR "/test-execute/") >= 0); |
281e05b6 | 343 | |
e1abca2e FB |
344 | /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test |
345 | * cases, otherwise (and if they are present in the environment), | |
346 | * `manager_default_environment` will copy them into the default | |
347 | * environment which is passed to each created job, which will make the | |
348 | * tests that expect those not to be present to fail. | |
349 | */ | |
350 | assert_se(unsetenv("VAR1") == 0); | |
351 | assert_se(unsetenv("VAR2") == 0); | |
352 | assert_se(unsetenv("VAR3") == 0); | |
353 | ||
19c0b0b9 RC |
354 | r = run_tests(MANAGER_USER, user_tests); |
355 | if (r != 0) | |
356 | return r; | |
281e05b6 | 357 | |
19c0b0b9 | 358 | return run_tests(MANAGER_SYSTEM, system_tests); |
281e05b6 | 359 | } |