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"
42 typedef void (*test_function_t
)(Manager
*m
);
44 static void check(Manager
*m
, Unit
*unit
, int status_expected
, int code_expected
) {
45 Service
*service
= NULL
;
47 usec_t timeout
= 2 * USEC_PER_SEC
;
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
) {
60 r
= sd_event_run(m
->event
, 100 * USEC_PER_MSEC
);
63 n
= now(CLOCK_MONOTONIC
);
64 if (ts
+ timeout
< n
) {
65 log_error("Test timeout when testing %s", unit
->id
);
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
);
74 static bool is_inaccessible_available(void) {
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"
85 if (access(p
, F_OK
) < 0)
92 static void test(Manager
*m
, const char *unit_name
, int status_expected
, int code_expected
) {
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
);
102 static void test_exec_workingdirectory(Manager
*m
) {
103 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
105 test(m
, "exec-workingdirectory.service", 0, CLD_EXITED
);
107 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT
|REMOVE_PHYSICAL
);
110 static void test_exec_personality(Manager
*m
) {
111 #if defined(__x86_64__)
112 test(m
, "exec-personality-x86-64.service", 0, CLD_EXITED
);
114 #elif defined(__s390__)
115 test(m
, "exec-personality-s390.service", 0, CLD_EXITED
);
117 #elif defined(__powerpc64__)
118 # if __BYTE_ORDER == __BIG_ENDIAN
119 test(m
, "exec-personality-ppc64.service", 0, CLD_EXITED
);
121 test(m
, "exec-personality-ppc64le.service", 0, CLD_EXITED
);
124 #elif defined(__aarch64__)
125 test(m
, "exec-personality-aarch64.service", 0, CLD_EXITED
);
127 #elif defined(__i386__)
128 test(m
, "exec-personality-x86.service", 0, CLD_EXITED
);
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
);
137 static void test_exec_privatetmp(Manager
*m
) {
138 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
140 test(m
, "exec-privatetmp-yes.service", 0, CLD_EXITED
);
141 test(m
, "exec-privatetmp-no.service", 0, CLD_EXITED
);
143 unlink("/tmp/test-exec_privatetmp");
146 static void test_exec_privatedevices(Manager
*m
) {
147 if (detect_container() > 0) {
148 log_notice("testing in container, skipping private device tests");
151 if (!is_inaccessible_available()) {
152 log_notice("testing without inaccessible, skipping private device tests");
156 test(m
, "exec-privatedevices-yes.service", 0, CLD_EXITED
);
157 test(m
, "exec-privatedevices-no.service", 0, CLD_EXITED
);
160 static void test_exec_privatedevices_capabilities(Manager
*m
) {
161 if (detect_container() > 0) {
162 log_notice("testing in container, skipping private device tests");
165 if (!is_inaccessible_available()) {
166 log_notice("testing without inaccessible, skipping private device tests");
170 test(m
, "exec-privatedevices-yes-capability-mknod.service", 0, CLD_EXITED
);
171 test(m
, "exec-privatedevices-no-capability-mknod.service", 0, CLD_EXITED
);
172 test(m
, "exec-privatedevices-yes-capability-sys-rawio.service", 0, CLD_EXITED
);
173 test(m
, "exec-privatedevices-no-capability-sys-rawio.service", 0, CLD_EXITED
);
176 static void test_exec_protectkernelmodules(Manager
*m
) {
177 if (detect_container() > 0) {
178 log_notice("testing in container, skipping protectkernelmodules tests");
181 if (!is_inaccessible_available()) {
182 log_notice("testing without inaccessible, skipping protectkernelmodules tests");
186 test(m
, "exec-protectkernelmodules-no-capabilities.service", 0, CLD_EXITED
);
187 test(m
, "exec-protectkernelmodules-yes-capabilities.service", 0, CLD_EXITED
);
188 test(m
, "exec-protectkernelmodules-yes-mount-propagation.service", 0, CLD_EXITED
);
191 static void test_exec_readonlypaths(Manager
*m
) {
193 if (path_is_read_only_fs("/var") > 0)
196 test(m
, "exec-readonlypaths.service", 0, CLD_EXITED
);
197 test(m
, "exec-readonlypaths-mount-propagation.service", 0, CLD_EXITED
);
200 static void test_exec_readwritepaths(Manager
*m
) {
202 if (path_is_read_only_fs("/") > 0)
205 test(m
, "exec-readwritepaths-mount-propagation.service", 0, CLD_EXITED
);
208 static void test_exec_inaccessiblepaths(Manager
*m
) {
210 if (path_is_read_only_fs("/") > 0)
213 test(m
, "exec-inaccessiblepaths-mount-propagation.service", 0, CLD_EXITED
);
216 static void test_exec_systemcallfilter(Manager
*m
) {
218 if (!is_seccomp_available())
220 test(m
, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED
);
221 test(m
, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED
);
222 test(m
, "exec-systemcallfilter-failing.service", SIGSYS
, CLD_KILLED
);
223 test(m
, "exec-systemcallfilter-failing2.service", SIGSYS
, CLD_KILLED
);
228 static void test_exec_systemcallerrornumber(Manager
*m
) {
230 if (is_seccomp_available())
231 test(m
, "exec-systemcallerrornumber.service", 1, CLD_EXITED
);
235 static void test_exec_restrict_namespaces(Manager
*m
) {
237 if (!is_seccomp_available())
240 test(m
, "exec-restrict-namespaces-no.service", 0, CLD_EXITED
);
241 test(m
, "exec-restrict-namespaces-yes.service", 1, CLD_EXITED
);
242 test(m
, "exec-restrict-namespaces-mnt.service", 0, CLD_EXITED
);
243 test(m
, "exec-restrict-namespaces-mnt-blacklist.service", 1, CLD_EXITED
);
247 static void test_exec_systemcall_system_mode_with_user(Manager
*m
) {
249 if (!is_seccomp_available())
251 if (getpwnam("nobody"))
252 test(m
, "exec-systemcallfilter-system-user.service", 0, CLD_EXITED
);
253 else if (getpwnam("nfsnobody"))
254 test(m
, "exec-systemcallfilter-system-user-nfsnobody.service", 0, CLD_EXITED
);
256 log_error_errno(errno
, "Skipping test_exec_systemcall_system_mode_with_user, could not find nobody/nfsnobody user: %m");
260 static void test_exec_user(Manager
*m
) {
261 if (getpwnam("nobody"))
262 test(m
, "exec-user.service", 0, CLD_EXITED
);
263 else if (getpwnam("nfsnobody"))
264 test(m
, "exec-user-nfsnobody.service", 0, CLD_EXITED
);
266 log_error_errno(errno
, "Skipping test_exec_user, could not find nobody/nfsnobody user: %m");
269 static void test_exec_group(Manager
*m
) {
270 if (getgrnam("nobody"))
271 test(m
, "exec-group.service", 0, CLD_EXITED
);
272 else if (getgrnam("nfsnobody"))
273 test(m
, "exec-group-nfsnobody.service", 0, CLD_EXITED
);
275 log_error_errno(errno
, "Skipping test_exec_group, could not find nobody/nfsnobody group: %m");
278 static void test_exec_supplementary_groups(Manager
*m
) {
279 test(m
, "exec-supplementarygroups.service", 0, CLD_EXITED
);
280 test(m
, "exec-supplementarygroups-single-group.service", 0, CLD_EXITED
);
281 test(m
, "exec-supplementarygroups-single-group-user.service", 0, CLD_EXITED
);
282 test(m
, "exec-supplementarygroups-multiple-groups-default-group-user.service", 0, CLD_EXITED
);
283 test(m
, "exec-supplementarygroups-multiple-groups-withgid.service", 0, CLD_EXITED
);
284 test(m
, "exec-supplementarygroups-multiple-groups-withuid.service", 0, CLD_EXITED
);
287 static void test_exec_dynamic_user(Manager
*m
) {
288 test(m
, "exec-dynamicuser-fixeduser.service", 0, CLD_EXITED
);
289 test(m
, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", 0, CLD_EXITED
);
290 test(m
, "exec-dynamicuser-supplementarygroups.service", 0, CLD_EXITED
);
293 static void test_exec_environment(Manager
*m
) {
294 test(m
, "exec-environment.service", 0, CLD_EXITED
);
295 test(m
, "exec-environment-multiple.service", 0, CLD_EXITED
);
296 test(m
, "exec-environment-empty.service", 0, CLD_EXITED
);
299 static void test_exec_environmentfile(Manager
*m
) {
300 static const char e
[] =
301 "VAR1='word1 word2'\n"
307 "line without an equal\n"
308 "VAR3='$word 5 6'\n";
311 r
= write_string_file("/tmp/test-exec_environmentfile.conf", e
, WRITE_STRING_FILE_CREATE
);
314 test(m
, "exec-environmentfile.service", 0, CLD_EXITED
);
316 unlink("/tmp/test-exec_environmentfile.conf");
319 static void test_exec_passenvironment(Manager
*m
) {
320 /* test-execute runs under MANAGER_USER which, by default, forwards all
321 * variables present in the environment, but only those that are
322 * present _at the time it is created_!
324 * So these PassEnvironment checks are still expected to work, since we
325 * are ensuring the variables are not present at manager creation (they
326 * are unset explicitly in main) and are only set here.
328 * This is still a good approximation of how a test for MANAGER_SYSTEM
331 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
332 assert_se(setenv("VAR2", "word3", 1) == 0);
333 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
334 test(m
, "exec-passenvironment.service", 0, CLD_EXITED
);
335 test(m
, "exec-passenvironment-repeated.service", 0, CLD_EXITED
);
336 test(m
, "exec-passenvironment-empty.service", 0, CLD_EXITED
);
337 assert_se(unsetenv("VAR1") == 0);
338 assert_se(unsetenv("VAR2") == 0);
339 assert_se(unsetenv("VAR3") == 0);
340 test(m
, "exec-passenvironment-absent.service", 0, CLD_EXITED
);
343 static void test_exec_umask(Manager
*m
) {
344 test(m
, "exec-umask-default.service", 0, CLD_EXITED
);
345 test(m
, "exec-umask-0177.service", 0, CLD_EXITED
);
348 static void test_exec_runtimedirectory(Manager
*m
) {
349 test(m
, "exec-runtimedirectory.service", 0, CLD_EXITED
);
350 test(m
, "exec-runtimedirectory-mode.service", 0, CLD_EXITED
);
351 if (getgrnam("nobody"))
352 test(m
, "exec-runtimedirectory-owner.service", 0, CLD_EXITED
);
353 else if (getgrnam("nfsnobody"))
354 test(m
, "exec-runtimedirectory-owner-nfsnobody.service", 0, CLD_EXITED
);
356 log_error_errno(errno
, "Skipping test_exec_runtimedirectory-owner, could not find nobody/nfsnobody group: %m");
359 static void test_exec_capabilityboundingset(Manager
*m
) {
362 /* We use capsh to test if the capabilities are
363 * properly set, so be sure that it exists */
364 r
= find_binary("capsh", NULL
);
366 log_error_errno(r
, "Skipping test_exec_capabilityboundingset, could not find capsh binary: %m");
370 test(m
, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED
);
371 test(m
, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED
);
372 test(m
, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED
);
373 test(m
, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED
);
376 static void test_exec_capabilityambientset(Manager
*m
) {
379 /* Check if the kernel has support for ambient capabilities. Run
380 * the tests only if that's the case. Clearing all ambient
381 * capabilities is fine, since we are expecting them to be unset
382 * in the first place for the tests. */
383 r
= prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_CLEAR_ALL
, 0, 0, 0);
384 if (r
>= 0 || errno
!= EINVAL
) {
385 if (getpwnam("nobody")) {
386 test(m
, "exec-capabilityambientset.service", 0, CLD_EXITED
);
387 test(m
, "exec-capabilityambientset-merge.service", 0, CLD_EXITED
);
388 } else if (getpwnam("nfsnobody")) {
389 test(m
, "exec-capabilityambientset-nfsnobody.service", 0, CLD_EXITED
);
390 test(m
, "exec-capabilityambientset-merge-nfsnobody.service", 0, CLD_EXITED
);
392 log_error_errno(errno
, "Skipping test_exec_capabilityambientset, could not find nobody/nfsnobody user: %m");
394 log_error_errno(errno
, "Skipping test_exec_capabilityambientset, the kernel does not support ambient capabilities: %m");
397 static void test_exec_privatenetwork(Manager
*m
) {
400 r
= find_binary("ip", NULL
);
402 log_error_errno(r
, "Skipping test_exec_privatenetwork, could not find ip binary: %m");
406 test(m
, "exec-privatenetwork-yes.service", 0, CLD_EXITED
);
409 static void test_exec_oomscoreadjust(Manager
*m
) {
410 test(m
, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED
);
411 test(m
, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED
);
414 static void test_exec_ioschedulingclass(Manager
*m
) {
415 test(m
, "exec-ioschedulingclass-none.service", 0, CLD_EXITED
);
416 test(m
, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED
);
417 test(m
, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED
);
418 test(m
, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED
);
421 static void test_exec_spec_interpolation(Manager
*m
) {
422 test(m
, "exec-spec-interpolation.service", 0, CLD_EXITED
);
425 static void test_exec_read_only_path_suceed(Manager
*m
) {
426 test(m
, "exec-read-only-path-succeed.service", 0, CLD_EXITED
);
429 static int run_tests(UnitFileScope scope
, const test_function_t
*tests
) {
430 const test_function_t
*test
= NULL
;
436 r
= manager_new(scope
, true, &m
);
437 if (MANAGER_SKIP_TEST(r
)) {
438 log_notice_errno(r
, "Skipping test: manager_new: %m");
439 return EXIT_TEST_SKIP
;
442 assert_se(manager_startup(m
, NULL
, NULL
) >= 0);
444 for (test
= tests
; test
&& *test
; test
++)
452 int main(int argc
, char *argv
[]) {
453 static const test_function_t user_tests
[] = {
454 test_exec_workingdirectory
,
455 test_exec_personality
,
456 test_exec_ignoresigpipe
,
457 test_exec_privatetmp
,
458 test_exec_privatedevices
,
459 test_exec_privatedevices_capabilities
,
460 test_exec_protectkernelmodules
,
461 test_exec_readonlypaths
,
462 test_exec_readwritepaths
,
463 test_exec_inaccessiblepaths
,
464 test_exec_privatenetwork
,
465 test_exec_systemcallfilter
,
466 test_exec_systemcallerrornumber
,
467 test_exec_restrict_namespaces
,
470 test_exec_supplementary_groups
,
471 test_exec_dynamic_user
,
472 test_exec_environment
,
473 test_exec_environmentfile
,
474 test_exec_passenvironment
,
476 test_exec_runtimedirectory
,
477 test_exec_capabilityboundingset
,
478 test_exec_capabilityambientset
,
479 test_exec_oomscoreadjust
,
480 test_exec_ioschedulingclass
,
481 test_exec_spec_interpolation
,
482 test_exec_read_only_path_suceed
,
485 static const test_function_t system_tests
[] = {
486 test_exec_systemcall_system_mode_with_user
,
491 log_set_max_level(LOG_DEBUG
);
492 log_parse_environment();
495 /* It is needed otherwise cgroup creation fails */
497 printf("Skipping test: not root\n");
498 return EXIT_TEST_SKIP
;
501 assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
502 assert_se(set_unit_path(TEST_DIR
"/test-execute/") >= 0);
504 /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
505 * cases, otherwise (and if they are present in the environment),
506 * `manager_default_environment` will copy them into the default
507 * environment which is passed to each created job, which will make the
508 * tests that expect those not to be present to fail.
510 assert_se(unsetenv("VAR1") == 0);
511 assert_se(unsetenv("VAR2") == 0);
512 assert_se(unsetenv("VAR3") == 0);
514 r
= run_tests(UNIT_FILE_USER
, user_tests
);
518 return run_tests(UNIT_FILE_SYSTEM
, system_tests
);