2 This file is part of systemd.
4 Copyright 2014 Ronny Chevalier
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.
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.
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/>.
23 #include <sys/prctl.h>
24 #include <sys/types.h>
31 #include "path-util.h"
34 #include "seccomp-util.h"
36 #include "stat-util.h"
37 #include "test-helper.h"
43 typedef void (*test_function_t
)(Manager
*m
);
45 static void check(Manager
*m
, Unit
*unit
, int status_expected
, int code_expected
) {
46 Service
*service
= NULL
;
48 usec_t timeout
= 2 * USEC_PER_SEC
;
53 service
= SERVICE(unit
);
54 printf("%s\n", unit
->id
);
55 exec_context_dump(&service
->exec_context
, stdout
, "\t");
56 ts
= now(CLOCK_MONOTONIC
);
57 while (service
->state
!= SERVICE_DEAD
&& service
->state
!= SERVICE_FAILED
) {
61 r
= sd_event_run(m
->event
, 100 * USEC_PER_MSEC
);
64 n
= now(CLOCK_MONOTONIC
);
65 if (ts
+ timeout
< n
) {
66 log_error("Test timeout when testing %s", unit
->id
);
70 exec_status_dump(&service
->main_exec_status
, stdout
, "\t");
71 assert_se(service
->main_exec_status
.status
== status_expected
);
72 assert_se(service
->main_exec_status
.code
== code_expected
);
75 static bool is_inaccessible_available(void) {
79 "/run/systemd/inaccessible/reg",
80 "/run/systemd/inaccessible/dir",
81 "/run/systemd/inaccessible/chr",
82 "/run/systemd/inaccessible/blk",
83 "/run/systemd/inaccessible/fifo",
84 "/run/systemd/inaccessible/sock"
86 if (access(p
, F_OK
) < 0)
93 static void test(Manager
*m
, const char *unit_name
, int status_expected
, int code_expected
) {
98 assert_se(manager_load_unit(m
, unit_name
, NULL
, NULL
, &unit
) >= 0);
99 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
100 check(m
, unit
, status_expected
, code_expected
);
103 static void test_exec_workingdirectory(Manager
*m
) {
104 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
106 test(m
, "exec-workingdirectory.service", 0, CLD_EXITED
);
108 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT
|REMOVE_PHYSICAL
);
111 static void test_exec_personality(Manager
*m
) {
112 #if defined(__x86_64__)
113 test(m
, "exec-personality-x86-64.service", 0, CLD_EXITED
);
115 #elif defined(__s390__)
116 test(m
, "exec-personality-s390.service", 0, CLD_EXITED
);
118 #elif defined(__powerpc64__)
119 # if __BYTE_ORDER == __BIG_ENDIAN
120 test(m
, "exec-personality-ppc64.service", 0, CLD_EXITED
);
122 test(m
, "exec-personality-ppc64le.service", 0, CLD_EXITED
);
125 #elif defined(__aarch64__)
126 test(m
, "exec-personality-aarch64.service", 0, CLD_EXITED
);
128 #elif defined(__i386__)
129 test(m
, "exec-personality-x86.service", 0, CLD_EXITED
);
133 static void test_exec_ignoresigpipe(Manager
*m
) {
134 test(m
, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED
);
135 test(m
, "exec-ignoresigpipe-no.service", SIGPIPE
, CLD_KILLED
);
138 static void test_exec_privatetmp(Manager
*m
) {
139 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
141 test(m
, "exec-privatetmp-yes.service", 0, CLD_EXITED
);
142 test(m
, "exec-privatetmp-no.service", 0, CLD_EXITED
);
144 unlink("/tmp/test-exec_privatetmp");
147 static void test_exec_privatedevices(Manager
*m
) {
148 if (detect_container() > 0) {
149 log_notice("testing in container, skipping %s", __func__
);
152 if (!is_inaccessible_available()) {
153 log_notice("testing without inaccessible, skipping %s", __func__
);
157 test(m
, "exec-privatedevices-yes.service", 0, CLD_EXITED
);
158 test(m
, "exec-privatedevices-no.service", 0, CLD_EXITED
);
161 static void test_exec_privatedevices_capabilities(Manager
*m
) {
164 if (detect_container() > 0) {
165 log_notice("testing in container, skipping %s", __func__
);
168 if (!is_inaccessible_available()) {
169 log_notice("testing without inaccessible, skipping %s", __func__
);
173 /* We use capsh to test if the capabilities are
174 * properly set, so be sure that it exists */
175 r
= find_binary("capsh", NULL
);
177 log_error_errno(r
, "Skipping %s, could not find capsh binary: %m", __func__
);
181 test(m
, "exec-privatedevices-yes-capability-mknod.service", 0, CLD_EXITED
);
182 test(m
, "exec-privatedevices-no-capability-mknod.service", 0, CLD_EXITED
);
183 test(m
, "exec-privatedevices-yes-capability-sys-rawio.service", 0, CLD_EXITED
);
184 test(m
, "exec-privatedevices-no-capability-sys-rawio.service", 0, CLD_EXITED
);
187 static void test_exec_protectkernelmodules(Manager
*m
) {
190 if (detect_container() > 0) {
191 log_notice("testing in container, skipping %s", __func__
);
194 if (!is_inaccessible_available()) {
195 log_notice("testing without inaccessible, skipping %s", __func__
);
199 r
= find_binary("capsh", NULL
);
201 log_error_errno(r
, "Skipping %s, could not find capsh binary: %m", __func__
);
206 test(m
, "exec-protectkernelmodules-no-capabilities.service", 0, CLD_EXITED
);
207 test(m
, "exec-protectkernelmodules-yes-capabilities.service", 0, CLD_EXITED
);
208 test(m
, "exec-protectkernelmodules-yes-mount-propagation.service", 0, CLD_EXITED
);
211 static void test_exec_readonlypaths(Manager
*m
) {
213 if (path_is_read_only_fs("/var") > 0)
216 test(m
, "exec-readonlypaths.service", 0, CLD_EXITED
);
217 test(m
, "exec-readonlypaths-mount-propagation.service", 0, CLD_EXITED
);
220 static void test_exec_readwritepaths(Manager
*m
) {
222 if (path_is_read_only_fs("/") > 0)
225 test(m
, "exec-readwritepaths-mount-propagation.service", 0, CLD_EXITED
);
228 static void test_exec_inaccessiblepaths(Manager
*m
) {
230 if (path_is_read_only_fs("/") > 0)
233 test(m
, "exec-inaccessiblepaths-mount-propagation.service", 0, CLD_EXITED
);
236 static void test_exec_inaccessiblepaths_proc(Manager
*m
) {
237 if (!is_inaccessible_available()) {
238 log_notice("testing without inaccessible, skipping %s", __func__
);
242 test(m
, "exec-inaccessiblepaths-proc.service", 0, CLD_EXITED
);
245 static void test_exec_systemcallfilter(Manager
*m
) {
247 if (!is_seccomp_available())
249 test(m
, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED
);
250 test(m
, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED
);
251 test(m
, "exec-systemcallfilter-failing.service", SIGSYS
, CLD_KILLED
);
252 test(m
, "exec-systemcallfilter-failing2.service", SIGSYS
, CLD_KILLED
);
257 static void test_exec_systemcallerrornumber(Manager
*m
) {
259 if (is_seccomp_available())
260 test(m
, "exec-systemcallerrornumber.service", 1, CLD_EXITED
);
264 static void test_exec_restrict_namespaces(Manager
*m
) {
266 if (!is_seccomp_available())
269 test(m
, "exec-restrict-namespaces-no.service", 0, CLD_EXITED
);
270 test(m
, "exec-restrict-namespaces-yes.service", 1, CLD_EXITED
);
271 test(m
, "exec-restrict-namespaces-mnt.service", 0, CLD_EXITED
);
272 test(m
, "exec-restrict-namespaces-mnt-blacklist.service", 1, CLD_EXITED
);
276 static void test_exec_systemcall_system_mode_with_user(Manager
*m
) {
278 if (!is_seccomp_available())
280 if (getpwnam("nobody"))
281 test(m
, "exec-systemcallfilter-system-user.service", 0, CLD_EXITED
);
282 else if (getpwnam("nfsnobody"))
283 test(m
, "exec-systemcallfilter-system-user-nfsnobody.service", 0, CLD_EXITED
);
285 log_error_errno(errno
, "Skipping %s, could not find nobody/nfsnobody user: %m", __func__
);
289 static void test_exec_user(Manager
*m
) {
290 if (getpwnam("nobody"))
291 test(m
, "exec-user.service", 0, CLD_EXITED
);
292 else if (getpwnam("nfsnobody"))
293 test(m
, "exec-user-nfsnobody.service", 0, CLD_EXITED
);
295 log_error_errno(errno
, "Skipping %s, could not find nobody/nfsnobody user: %m", __func__
);
298 static void test_exec_group(Manager
*m
) {
299 if (getgrnam("nobody"))
300 test(m
, "exec-group.service", 0, CLD_EXITED
);
301 else if (getgrnam("nfsnobody"))
302 test(m
, "exec-group-nfsnobody.service", 0, CLD_EXITED
);
304 log_error_errno(errno
, "Skipping %s, could not find nobody/nfsnobody group: %m", __func__
);
307 static void test_exec_supplementary_groups(Manager
*m
) {
308 test(m
, "exec-supplementarygroups.service", 0, CLD_EXITED
);
309 test(m
, "exec-supplementarygroups-single-group.service", 0, CLD_EXITED
);
310 test(m
, "exec-supplementarygroups-single-group-user.service", 0, CLD_EXITED
);
311 test(m
, "exec-supplementarygroups-multiple-groups-default-group-user.service", 0, CLD_EXITED
);
312 test(m
, "exec-supplementarygroups-multiple-groups-withgid.service", 0, CLD_EXITED
);
313 test(m
, "exec-supplementarygroups-multiple-groups-withuid.service", 0, CLD_EXITED
);
316 static void test_exec_dynamic_user(Manager
*m
) {
317 test(m
, "exec-dynamicuser-fixeduser.service", 0, CLD_EXITED
);
318 test(m
, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", 0, CLD_EXITED
);
319 test(m
, "exec-dynamicuser-supplementarygroups.service", 0, CLD_EXITED
);
322 static void test_exec_environment(Manager
*m
) {
323 test(m
, "exec-environment.service", 0, CLD_EXITED
);
324 test(m
, "exec-environment-multiple.service", 0, CLD_EXITED
);
325 test(m
, "exec-environment-empty.service", 0, CLD_EXITED
);
328 static void test_exec_environmentfile(Manager
*m
) {
329 static const char e
[] =
330 "VAR1='word1 word2'\n"
336 "line without an equal\n"
337 "VAR3='$word 5 6'\n";
340 r
= write_string_file("/tmp/test-exec_environmentfile.conf", e
, WRITE_STRING_FILE_CREATE
);
343 test(m
, "exec-environmentfile.service", 0, CLD_EXITED
);
345 unlink("/tmp/test-exec_environmentfile.conf");
348 static void test_exec_passenvironment(Manager
*m
) {
349 /* test-execute runs under MANAGER_USER which, by default, forwards all
350 * variables present in the environment, but only those that are
351 * present _at the time it is created_!
353 * So these PassEnvironment checks are still expected to work, since we
354 * are ensuring the variables are not present at manager creation (they
355 * are unset explicitly in main) and are only set here.
357 * This is still a good approximation of how a test for MANAGER_SYSTEM
360 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
361 assert_se(setenv("VAR2", "word3", 1) == 0);
362 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
363 test(m
, "exec-passenvironment.service", 0, CLD_EXITED
);
364 test(m
, "exec-passenvironment-repeated.service", 0, CLD_EXITED
);
365 test(m
, "exec-passenvironment-empty.service", 0, CLD_EXITED
);
366 assert_se(unsetenv("VAR1") == 0);
367 assert_se(unsetenv("VAR2") == 0);
368 assert_se(unsetenv("VAR3") == 0);
369 test(m
, "exec-passenvironment-absent.service", 0, CLD_EXITED
);
372 static void test_exec_umask(Manager
*m
) {
373 test(m
, "exec-umask-default.service", 0, CLD_EXITED
);
374 test(m
, "exec-umask-0177.service", 0, CLD_EXITED
);
377 static void test_exec_runtimedirectory(Manager
*m
) {
378 test(m
, "exec-runtimedirectory.service", 0, CLD_EXITED
);
379 test(m
, "exec-runtimedirectory-mode.service", 0, CLD_EXITED
);
380 if (getgrnam("nobody"))
381 test(m
, "exec-runtimedirectory-owner.service", 0, CLD_EXITED
);
382 else if (getgrnam("nfsnobody"))
383 test(m
, "exec-runtimedirectory-owner-nfsnobody.service", 0, CLD_EXITED
);
385 log_error_errno(errno
, "Skipping %s, could not find nobody/nfsnobody group: %m", __func__
);
388 static void test_exec_capabilityboundingset(Manager
*m
) {
391 r
= find_binary("capsh", NULL
);
393 log_error_errno(r
, "Skipping %s, could not find capsh binary: %m", __func__
);
397 test(m
, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED
);
398 test(m
, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED
);
399 test(m
, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED
);
400 test(m
, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED
);
403 static void test_exec_capabilityambientset(Manager
*m
) {
406 /* Check if the kernel has support for ambient capabilities. Run
407 * the tests only if that's the case. Clearing all ambient
408 * capabilities is fine, since we are expecting them to be unset
409 * in the first place for the tests. */
410 r
= prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_CLEAR_ALL
, 0, 0, 0);
411 if (r
>= 0 || errno
!= EINVAL
) {
412 if (getpwnam("nobody")) {
413 test(m
, "exec-capabilityambientset.service", 0, CLD_EXITED
);
414 test(m
, "exec-capabilityambientset-merge.service", 0, CLD_EXITED
);
415 } else if (getpwnam("nfsnobody")) {
416 test(m
, "exec-capabilityambientset-nfsnobody.service", 0, CLD_EXITED
);
417 test(m
, "exec-capabilityambientset-merge-nfsnobody.service", 0, CLD_EXITED
);
419 log_error_errno(errno
, "Skipping %s, could not find nobody/nfsnobody user: %m", __func__
);
421 log_error_errno(errno
, "Skipping %s, the kernel does not support ambient capabilities: %m", __func__
);
424 static void test_exec_privatenetwork(Manager
*m
) {
427 r
= find_binary("ip", NULL
);
429 log_error_errno(r
, "Skipping %s, could not find ip binary: %m", __func__
);
433 test(m
, "exec-privatenetwork-yes.service", 0, CLD_EXITED
);
436 static void test_exec_oomscoreadjust(Manager
*m
) {
437 test(m
, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED
);
438 test(m
, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED
);
441 static void test_exec_ioschedulingclass(Manager
*m
) {
442 test(m
, "exec-ioschedulingclass-none.service", 0, CLD_EXITED
);
443 test(m
, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED
);
444 test(m
, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED
);
445 test(m
, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED
);
448 static void test_exec_spec_interpolation(Manager
*m
) {
449 test(m
, "exec-spec-interpolation.service", 0, CLD_EXITED
);
452 static void test_exec_read_only_path_suceed(Manager
*m
) {
453 test(m
, "exec-read-only-path-succeed.service", 0, CLD_EXITED
);
456 static void test_exec_unset_environment(Manager
*m
) {
457 test(m
, "exec-unset-environment.service", 0, CLD_EXITED
);
460 static int run_tests(UnitFileScope scope
, const test_function_t
*tests
) {
461 const test_function_t
*test
= NULL
;
467 r
= manager_new(scope
, true, &m
);
468 if (MANAGER_SKIP_TEST(r
)) {
469 log_notice_errno(r
, "Skipping test: manager_new: %m");
470 return EXIT_TEST_SKIP
;
473 assert_se(manager_startup(m
, NULL
, NULL
) >= 0);
475 for (test
= tests
; test
&& *test
; test
++)
483 int main(int argc
, char *argv
[]) {
484 static const test_function_t user_tests
[] = {
485 test_exec_workingdirectory
,
486 test_exec_personality
,
487 test_exec_ignoresigpipe
,
488 test_exec_privatetmp
,
489 test_exec_privatedevices
,
490 test_exec_privatedevices_capabilities
,
491 test_exec_protectkernelmodules
,
492 test_exec_readonlypaths
,
493 test_exec_readwritepaths
,
494 test_exec_inaccessiblepaths
,
495 test_exec_inaccessiblepaths_proc
,
496 test_exec_privatenetwork
,
497 test_exec_systemcallfilter
,
498 test_exec_systemcallerrornumber
,
499 test_exec_restrict_namespaces
,
502 test_exec_supplementary_groups
,
503 test_exec_dynamic_user
,
504 test_exec_environment
,
505 test_exec_environmentfile
,
506 test_exec_passenvironment
,
508 test_exec_runtimedirectory
,
509 test_exec_capabilityboundingset
,
510 test_exec_capabilityambientset
,
511 test_exec_oomscoreadjust
,
512 test_exec_ioschedulingclass
,
513 test_exec_spec_interpolation
,
514 test_exec_read_only_path_suceed
,
515 test_exec_unset_environment
,
518 static const test_function_t system_tests
[] = {
519 test_exec_systemcall_system_mode_with_user
,
524 log_set_max_level(LOG_DEBUG
);
525 log_parse_environment();
528 /* It is needed otherwise cgroup creation fails */
530 printf("Skipping test: not root\n");
531 return EXIT_TEST_SKIP
;
534 enter_cgroup_subroot();
536 assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
537 assert_se(set_unit_path(get_testdata_dir("/test-execute")) >= 0);
539 /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
540 * cases, otherwise (and if they are present in the environment),
541 * `manager_default_environment` will copy them into the default
542 * environment which is passed to each created job, which will make the
543 * tests that expect those not to be present to fail.
545 assert_se(unsetenv("VAR1") == 0);
546 assert_se(unsetenv("VAR2") == 0);
547 assert_se(unsetenv("VAR3") == 0);
549 r
= run_tests(UNIT_FILE_USER
, user_tests
);
553 return run_tests(UNIT_FILE_SYSTEM
, system_tests
);