]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-execute.c
test: make unit tests relocatable
[thirdparty/systemd.git] / src / test / test-execute.c
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
20 #include <grp.h>
21 #include <pwd.h>
22 #include <stdio.h>
23 #include <sys/prctl.h>
24 #include <sys/types.h>
25
26 #include "fileio.h"
27 #include "fs-util.h"
28 #include "macro.h"
29 #include "manager.h"
30 #include "mkdir.h"
31 #include "path-util.h"
32 #include "rm-rf.h"
33 #ifdef HAVE_SECCOMP
34 #include "seccomp-util.h"
35 #endif
36 #include "stat-util.h"
37 #include "test-helper.h"
38 #include "unit.h"
39 #include "util.h"
40 #include "virt.h"
41
42 typedef void (*test_function_t)(Manager *m);
43
44 static void check(Manager *m, Unit *unit, int status_expected, int code_expected) {
45 Service *service = NULL;
46 usec_t ts;
47 usec_t timeout = 2 * USEC_PER_SEC;
48
49 assert_se(m);
50 assert_se(unit);
51
52 service = SERVICE(unit);
53 printf("%s\n", unit->id);
54 exec_context_dump(&service->exec_context, stdout, "\t");
55 ts = now(CLOCK_MONOTONIC);
56 while (service->state != SERVICE_DEAD && service->state != SERVICE_FAILED) {
57 int r;
58 usec_t n;
59
60 r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
61 assert_se(r >= 0);
62
63 n = now(CLOCK_MONOTONIC);
64 if (ts + timeout < n) {
65 log_error("Test timeout when testing %s", unit->id);
66 exit(EXIT_FAILURE);
67 }
68 }
69 exec_status_dump(&service->main_exec_status, stdout, "\t");
70 assert_se(service->main_exec_status.status == status_expected);
71 assert_se(service->main_exec_status.code == code_expected);
72 }
73
74 static bool is_inaccessible_available(void) {
75 char *p;
76
77 FOREACH_STRING(p,
78 "/run/systemd/inaccessible/reg",
79 "/run/systemd/inaccessible/dir",
80 "/run/systemd/inaccessible/chr",
81 "/run/systemd/inaccessible/blk",
82 "/run/systemd/inaccessible/fifo",
83 "/run/systemd/inaccessible/sock"
84 ) {
85 if (access(p, F_OK) < 0)
86 return false;
87 }
88
89 return true;
90 }
91
92 static void test(Manager *m, const char *unit_name, int status_expected, int code_expected) {
93 Unit *unit;
94
95 assert_se(unit_name);
96
97 assert_se(manager_load_unit(m, unit_name, NULL, NULL, &unit) >= 0);
98 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
99 check(m, unit, status_expected, code_expected);
100 }
101
102 static void test_exec_workingdirectory(Manager *m) {
103 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
104
105 test(m, "exec-workingdirectory.service", 0, CLD_EXITED);
106
107 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
108 }
109
110 static void test_exec_personality(Manager *m) {
111 #if defined(__x86_64__)
112 test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
113
114 #elif defined(__s390__)
115 test(m, "exec-personality-s390.service", 0, CLD_EXITED);
116
117 #elif defined(__powerpc64__)
118 # if __BYTE_ORDER == __BIG_ENDIAN
119 test(m, "exec-personality-ppc64.service", 0, CLD_EXITED);
120 # else
121 test(m, "exec-personality-ppc64le.service", 0, CLD_EXITED);
122 # endif
123
124 #elif defined(__aarch64__)
125 test(m, "exec-personality-aarch64.service", 0, CLD_EXITED);
126
127 #elif defined(__i386__)
128 test(m, "exec-personality-x86.service", 0, CLD_EXITED);
129 #endif
130 }
131
132 static void test_exec_ignoresigpipe(Manager *m) {
133 test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
134 test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
135 }
136
137 static void test_exec_privatetmp(Manager *m) {
138 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
139
140 test(m, "exec-privatetmp-yes.service", 0, CLD_EXITED);
141 test(m, "exec-privatetmp-no.service", 0, CLD_EXITED);
142
143 unlink("/tmp/test-exec_privatetmp");
144 }
145
146 static void test_exec_privatedevices(Manager *m) {
147 if (detect_container() > 0) {
148 log_notice("testing in container, skipping %s", __func__);
149 return;
150 }
151 if (!is_inaccessible_available()) {
152 log_notice("testing without inaccessible, skipping %s", __func__);
153 return;
154 }
155
156 test(m, "exec-privatedevices-yes.service", 0, CLD_EXITED);
157 test(m, "exec-privatedevices-no.service", 0, CLD_EXITED);
158 }
159
160 static void test_exec_privatedevices_capabilities(Manager *m) {
161 int r;
162
163 if (detect_container() > 0) {
164 log_notice("testing in container, skipping %s", __func__);
165 return;
166 }
167 if (!is_inaccessible_available()) {
168 log_notice("testing without inaccessible, skipping %s", __func__);
169 return;
170 }
171
172 /* We use capsh to test if the capabilities are
173 * properly set, so be sure that it exists */
174 r = find_binary("capsh", NULL);
175 if (r < 0) {
176 log_error_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
177 return;
178 }
179
180 test(m, "exec-privatedevices-yes-capability-mknod.service", 0, CLD_EXITED);
181 test(m, "exec-privatedevices-no-capability-mknod.service", 0, CLD_EXITED);
182 test(m, "exec-privatedevices-yes-capability-sys-rawio.service", 0, CLD_EXITED);
183 test(m, "exec-privatedevices-no-capability-sys-rawio.service", 0, CLD_EXITED);
184 }
185
186 static void test_exec_protectkernelmodules(Manager *m) {
187 int r;
188
189 if (detect_container() > 0) {
190 log_notice("testing in container, skipping %s", __func__);
191 return;
192 }
193 if (!is_inaccessible_available()) {
194 log_notice("testing without inaccessible, skipping %s", __func__);
195 return;
196 }
197
198 r = find_binary("capsh", NULL);
199 if (r < 0) {
200 log_error_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
201 return;
202 }
203
204
205 test(m, "exec-protectkernelmodules-no-capabilities.service", 0, CLD_EXITED);
206 test(m, "exec-protectkernelmodules-yes-capabilities.service", 0, CLD_EXITED);
207 test(m, "exec-protectkernelmodules-yes-mount-propagation.service", 0, CLD_EXITED);
208 }
209
210 static void test_exec_readonlypaths(Manager *m) {
211
212 if (path_is_read_only_fs("/var") > 0)
213 return;
214
215 test(m, "exec-readonlypaths.service", 0, CLD_EXITED);
216 test(m, "exec-readonlypaths-mount-propagation.service", 0, CLD_EXITED);
217 }
218
219 static void test_exec_readwritepaths(Manager *m) {
220
221 if (path_is_read_only_fs("/") > 0)
222 return;
223
224 test(m, "exec-readwritepaths-mount-propagation.service", 0, CLD_EXITED);
225 }
226
227 static void test_exec_inaccessiblepaths(Manager *m) {
228
229 if (path_is_read_only_fs("/") > 0)
230 return;
231
232 test(m, "exec-inaccessiblepaths-mount-propagation.service", 0, CLD_EXITED);
233 }
234
235 static void test_exec_systemcallfilter(Manager *m) {
236 #ifdef HAVE_SECCOMP
237 if (!is_seccomp_available())
238 return;
239 test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
240 test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
241 test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
242 test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
243
244 #endif
245 }
246
247 static void test_exec_systemcallerrornumber(Manager *m) {
248 #ifdef HAVE_SECCOMP
249 if (is_seccomp_available())
250 test(m, "exec-systemcallerrornumber.service", 1, CLD_EXITED);
251 #endif
252 }
253
254 static void test_exec_restrict_namespaces(Manager *m) {
255 #ifdef HAVE_SECCOMP
256 if (!is_seccomp_available())
257 return;
258
259 test(m, "exec-restrict-namespaces-no.service", 0, CLD_EXITED);
260 test(m, "exec-restrict-namespaces-yes.service", 1, CLD_EXITED);
261 test(m, "exec-restrict-namespaces-mnt.service", 0, CLD_EXITED);
262 test(m, "exec-restrict-namespaces-mnt-blacklist.service", 1, CLD_EXITED);
263 #endif
264 }
265
266 static void test_exec_systemcall_system_mode_with_user(Manager *m) {
267 #ifdef HAVE_SECCOMP
268 if (!is_seccomp_available())
269 return;
270 if (getpwnam("nobody"))
271 test(m, "exec-systemcallfilter-system-user.service", 0, CLD_EXITED);
272 else if (getpwnam("nfsnobody"))
273 test(m, "exec-systemcallfilter-system-user-nfsnobody.service", 0, CLD_EXITED);
274 else
275 log_error_errno(errno, "Skipping %s, could not find nobody/nfsnobody user: %m", __func__);
276 #endif
277 }
278
279 static void test_exec_user(Manager *m) {
280 if (getpwnam("nobody"))
281 test(m, "exec-user.service", 0, CLD_EXITED);
282 else if (getpwnam("nfsnobody"))
283 test(m, "exec-user-nfsnobody.service", 0, CLD_EXITED);
284 else
285 log_error_errno(errno, "Skipping %s, could not find nobody/nfsnobody user: %m", __func__);
286 }
287
288 static void test_exec_group(Manager *m) {
289 if (getgrnam("nobody"))
290 test(m, "exec-group.service", 0, CLD_EXITED);
291 else if (getgrnam("nfsnobody"))
292 test(m, "exec-group-nfsnobody.service", 0, CLD_EXITED);
293 else
294 log_error_errno(errno, "Skipping %s, could not find nobody/nfsnobody group: %m", __func__);
295 }
296
297 static void test_exec_supplementary_groups(Manager *m) {
298 test(m, "exec-supplementarygroups.service", 0, CLD_EXITED);
299 test(m, "exec-supplementarygroups-single-group.service", 0, CLD_EXITED);
300 test(m, "exec-supplementarygroups-single-group-user.service", 0, CLD_EXITED);
301 test(m, "exec-supplementarygroups-multiple-groups-default-group-user.service", 0, CLD_EXITED);
302 test(m, "exec-supplementarygroups-multiple-groups-withgid.service", 0, CLD_EXITED);
303 test(m, "exec-supplementarygroups-multiple-groups-withuid.service", 0, CLD_EXITED);
304 }
305
306 static void test_exec_dynamic_user(Manager *m) {
307 test(m, "exec-dynamicuser-fixeduser.service", 0, CLD_EXITED);
308 test(m, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", 0, CLD_EXITED);
309 test(m, "exec-dynamicuser-supplementarygroups.service", 0, CLD_EXITED);
310 }
311
312 static void test_exec_environment(Manager *m) {
313 test(m, "exec-environment.service", 0, CLD_EXITED);
314 test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
315 test(m, "exec-environment-empty.service", 0, CLD_EXITED);
316 }
317
318 static void test_exec_environmentfile(Manager *m) {
319 static const char e[] =
320 "VAR1='word1 word2'\n"
321 "VAR2=word3 \n"
322 "# comment1\n"
323 "\n"
324 "; comment2\n"
325 " ; # comment3\n"
326 "line without an equal\n"
327 "VAR3='$word 5 6'\n";
328 int r;
329
330 r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
331 assert_se(r == 0);
332
333 test(m, "exec-environmentfile.service", 0, CLD_EXITED);
334
335 unlink("/tmp/test-exec_environmentfile.conf");
336 }
337
338 static void test_exec_passenvironment(Manager *m) {
339 /* test-execute runs under MANAGER_USER which, by default, forwards all
340 * variables present in the environment, but only those that are
341 * present _at the time it is created_!
342 *
343 * So these PassEnvironment checks are still expected to work, since we
344 * are ensuring the variables are not present at manager creation (they
345 * are unset explicitly in main) and are only set here.
346 *
347 * This is still a good approximation of how a test for MANAGER_SYSTEM
348 * would work.
349 */
350 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
351 assert_se(setenv("VAR2", "word3", 1) == 0);
352 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
353 test(m, "exec-passenvironment.service", 0, CLD_EXITED);
354 test(m, "exec-passenvironment-repeated.service", 0, CLD_EXITED);
355 test(m, "exec-passenvironment-empty.service", 0, CLD_EXITED);
356 assert_se(unsetenv("VAR1") == 0);
357 assert_se(unsetenv("VAR2") == 0);
358 assert_se(unsetenv("VAR3") == 0);
359 test(m, "exec-passenvironment-absent.service", 0, CLD_EXITED);
360 }
361
362 static void test_exec_umask(Manager *m) {
363 test(m, "exec-umask-default.service", 0, CLD_EXITED);
364 test(m, "exec-umask-0177.service", 0, CLD_EXITED);
365 }
366
367 static void test_exec_runtimedirectory(Manager *m) {
368 test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
369 test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
370 if (getgrnam("nobody"))
371 test(m, "exec-runtimedirectory-owner.service", 0, CLD_EXITED);
372 else if (getgrnam("nfsnobody"))
373 test(m, "exec-runtimedirectory-owner-nfsnobody.service", 0, CLD_EXITED);
374 else
375 log_error_errno(errno, "Skipping %s, could not find nobody/nfsnobody group: %m", __func__);
376 }
377
378 static void test_exec_capabilityboundingset(Manager *m) {
379 int r;
380
381 r = find_binary("capsh", NULL);
382 if (r < 0) {
383 log_error_errno(r, "Skipping %s, could not find capsh binary: %m", __func__);
384 return;
385 }
386
387 test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
388 test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
389 test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
390 test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
391 }
392
393 static void test_exec_capabilityambientset(Manager *m) {
394 int r;
395
396 /* Check if the kernel has support for ambient capabilities. Run
397 * the tests only if that's the case. Clearing all ambient
398 * capabilities is fine, since we are expecting them to be unset
399 * in the first place for the tests. */
400 r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
401 if (r >= 0 || errno != EINVAL) {
402 if (getpwnam("nobody")) {
403 test(m, "exec-capabilityambientset.service", 0, CLD_EXITED);
404 test(m, "exec-capabilityambientset-merge.service", 0, CLD_EXITED);
405 } else if (getpwnam("nfsnobody")) {
406 test(m, "exec-capabilityambientset-nfsnobody.service", 0, CLD_EXITED);
407 test(m, "exec-capabilityambientset-merge-nfsnobody.service", 0, CLD_EXITED);
408 } else
409 log_error_errno(errno, "Skipping %s, could not find nobody/nfsnobody user: %m", __func__);
410 } else
411 log_error_errno(errno, "Skipping %s, the kernel does not support ambient capabilities: %m", __func__);
412 }
413
414 static void test_exec_privatenetwork(Manager *m) {
415 int r;
416
417 r = find_binary("ip", NULL);
418 if (r < 0) {
419 log_error_errno(r, "Skipping %s, could not find ip binary: %m", __func__);
420 return;
421 }
422
423 test(m, "exec-privatenetwork-yes.service", 0, CLD_EXITED);
424 }
425
426 static void test_exec_oomscoreadjust(Manager *m) {
427 test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
428 test(m, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED);
429 }
430
431 static void test_exec_ioschedulingclass(Manager *m) {
432 test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
433 test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
434 test(m, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED);
435 test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
436 }
437
438 static void test_exec_spec_interpolation(Manager *m) {
439 test(m, "exec-spec-interpolation.service", 0, CLD_EXITED);
440 }
441
442 static void test_exec_read_only_path_suceed(Manager *m) {
443 test(m, "exec-read-only-path-succeed.service", 0, CLD_EXITED);
444 }
445
446 static int run_tests(UnitFileScope scope, const test_function_t *tests) {
447 const test_function_t *test = NULL;
448 Manager *m = NULL;
449 int r;
450
451 assert_se(tests);
452
453 r = manager_new(scope, true, &m);
454 if (MANAGER_SKIP_TEST(r)) {
455 log_notice_errno(r, "Skipping test: manager_new: %m");
456 return EXIT_TEST_SKIP;
457 }
458 assert_se(r >= 0);
459 assert_se(manager_startup(m, NULL, NULL) >= 0);
460
461 for (test = tests; test && *test; test++)
462 (*test)(m);
463
464 manager_free(m);
465
466 return 0;
467 }
468
469 int main(int argc, char *argv[]) {
470 static const test_function_t user_tests[] = {
471 test_exec_workingdirectory,
472 test_exec_personality,
473 test_exec_ignoresigpipe,
474 test_exec_privatetmp,
475 test_exec_privatedevices,
476 test_exec_privatedevices_capabilities,
477 test_exec_protectkernelmodules,
478 test_exec_readonlypaths,
479 test_exec_readwritepaths,
480 test_exec_inaccessiblepaths,
481 test_exec_privatenetwork,
482 test_exec_systemcallfilter,
483 test_exec_systemcallerrornumber,
484 test_exec_restrict_namespaces,
485 test_exec_user,
486 test_exec_group,
487 test_exec_supplementary_groups,
488 test_exec_dynamic_user,
489 test_exec_environment,
490 test_exec_environmentfile,
491 test_exec_passenvironment,
492 test_exec_umask,
493 test_exec_runtimedirectory,
494 test_exec_capabilityboundingset,
495 test_exec_capabilityambientset,
496 test_exec_oomscoreadjust,
497 test_exec_ioschedulingclass,
498 test_exec_spec_interpolation,
499 test_exec_read_only_path_suceed,
500 NULL,
501 };
502 static const test_function_t system_tests[] = {
503 test_exec_systemcall_system_mode_with_user,
504 NULL,
505 };
506 int r;
507
508 log_set_max_level(LOG_DEBUG);
509 log_parse_environment();
510 log_open();
511
512 /* It is needed otherwise cgroup creation fails */
513 if (getuid() != 0) {
514 printf("Skipping test: not root\n");
515 return EXIT_TEST_SKIP;
516 }
517
518 assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
519 assert_se(set_unit_path(TEST_DATA_DIR("/test-execute/")) >= 0);
520
521 /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
522 * cases, otherwise (and if they are present in the environment),
523 * `manager_default_environment` will copy them into the default
524 * environment which is passed to each created job, which will make the
525 * tests that expect those not to be present to fail.
526 */
527 assert_se(unsetenv("VAR1") == 0);
528 assert_se(unsetenv("VAR2") == 0);
529 assert_se(unsetenv("VAR3") == 0);
530
531 r = run_tests(UNIT_FILE_USER, user_tests);
532 if (r != 0)
533 return r;
534
535 return run_tests(UNIT_FILE_SYSTEM, system_tests);
536 }