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