]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-execute.c
test: add tests for simple ReadOnlyPaths= case
[thirdparty/systemd.git] / src / test / test-execute.c
CommitLineData
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"
83f12b27
FS
33#ifdef HAVE_SECCOMP
34#include "seccomp-util.h"
35#endif
8b3aa503 36#include "test-helper.h"
f4f15635
LP
37#include "unit.h"
38#include "util.h"
4dd4cb8f 39#include "virt.h"
281e05b6
RC
40
41typedef void (*test_function_t)(Manager *m);
42
43static 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
73static void test(Manager *m, const char *unit_name, int status_expected, int code_expected) {
74 Unit *unit;
75
76 assert_se(unit_name);
77
78 assert_se(manager_load_unit(m, unit_name, NULL, NULL, &unit) >= 0);
79 assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
80 check(m, unit, status_expected, code_expected);
81}
82
83static void test_exec_workingdirectory(Manager *m) {
84 assert_se(mkdir_p("/tmp/test-exec_workingdirectory", 0755) >= 0);
85
86 test(m, "exec-workingdirectory.service", 0, CLD_EXITED);
87
c6878637 88 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
281e05b6
RC
89}
90
91static void test_exec_personality(Manager *m) {
281e05b6
RC
92#if defined(__x86_64__)
93 test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
7517f51e
HB
94
95#elif defined(__s390__)
96 test(m, "exec-personality-s390.service", 0, CLD_EXITED);
97
12591863
JS
98#elif defined(__powerpc64__)
99# if __BYTE_ORDER == __BIG_ENDIAN
100 test(m, "exec-personality-ppc64.service", 0, CLD_EXITED);
101# else
102 test(m, "exec-personality-ppc64le.service", 0, CLD_EXITED);
103# endif
104
105#elif defined(__aarch64__)
106 test(m, "exec-personality-aarch64.service", 0, CLD_EXITED);
107
5798eb4c 108#elif defined(__i386__)
7517f51e 109 test(m, "exec-personality-x86.service", 0, CLD_EXITED);
281e05b6
RC
110#endif
111}
112
113static void test_exec_ignoresigpipe(Manager *m) {
114 test(m, "exec-ignoresigpipe-yes.service", 0, CLD_EXITED);
115 test(m, "exec-ignoresigpipe-no.service", SIGPIPE, CLD_KILLED);
116}
117
118static void test_exec_privatetmp(Manager *m) {
119 assert_se(touch("/tmp/test-exec_privatetmp") >= 0);
120
121 test(m, "exec-privatetmp-yes.service", 0, CLD_EXITED);
122 test(m, "exec-privatetmp-no.service", 0, CLD_EXITED);
123
124 unlink("/tmp/test-exec_privatetmp");
125}
126
127static void test_exec_privatedevices(Manager *m) {
4dd4cb8f
SM
128 if (detect_container() > 0) {
129 log_notice("testing in container, skipping private device tests");
130 return;
131 }
281e05b6
RC
132 test(m, "exec-privatedevices-yes.service", 0, CLD_EXITED);
133 test(m, "exec-privatedevices-no.service", 0, CLD_EXITED);
134}
135
615a1f4b
DH
136static void test_exec_privatedevices_capabilities(Manager *m) {
137 if (detect_container() > 0) {
138 log_notice("testing in container, skipping private device tests");
139 return;
140 }
141 test(m, "exec-privatedevices-yes-capability-mknod.service", 0, CLD_EXITED);
142 test(m, "exec-privatedevices-no-capability-mknod.service", 0, CLD_EXITED);
143}
144
f78b36f0
DH
145static void test_exec_readonlypaths(Manager *m) {
146 test(m, "exec-readonlypaths.service", 0, CLD_EXITED);
147}
148
281e05b6
RC
149static void test_exec_systemcallfilter(Manager *m) {
150#ifdef HAVE_SECCOMP
83f12b27
FS
151 if (!is_seccomp_available())
152 return;
281e05b6
RC
153 test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
154 test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
155 test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
156 test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
83f12b27 157
281e05b6
RC
158#endif
159}
160
161static void test_exec_systemcallerrornumber(Manager *m) {
162#ifdef HAVE_SECCOMP
83f12b27
FS
163 if (is_seccomp_available())
164 test(m, "exec-systemcallerrornumber.service", 1, CLD_EXITED);
281e05b6
RC
165#endif
166}
167
19c0b0b9
RC
168static void test_exec_systemcall_system_mode_with_user(Manager *m) {
169#ifdef HAVE_SECCOMP
83f12b27
FS
170 if (!is_seccomp_available())
171 return;
19c0b0b9
RC
172 if (getpwnam("nobody"))
173 test(m, "exec-systemcallfilter-system-user.service", 0, CLD_EXITED);
50f130c2
RC
174 else if (getpwnam("nfsnobody"))
175 test(m, "exec-systemcallfilter-system-user-nfsnobody.service", 0, CLD_EXITED);
19c0b0b9 176 else
50f130c2 177 log_error_errno(errno, "Skipping test_exec_systemcall_system_mode_with_user, could not find nobody/nfsnobody user: %m");
19c0b0b9
RC
178#endif
179}
180
281e05b6 181static void test_exec_user(Manager *m) {
cc7fa4fb
RC
182 if (getpwnam("nobody"))
183 test(m, "exec-user.service", 0, CLD_EXITED);
50f130c2
RC
184 else if (getpwnam("nfsnobody"))
185 test(m, "exec-user-nfsnobody.service", 0, CLD_EXITED);
ff4ca461 186 else
50f130c2 187 log_error_errno(errno, "Skipping test_exec_user, could not find nobody/nfsnobody user: %m");
281e05b6
RC
188}
189
190static void test_exec_group(Manager *m) {
cc7fa4fb
RC
191 if (getgrnam("nobody"))
192 test(m, "exec-group.service", 0, CLD_EXITED);
50f130c2
RC
193 else if (getgrnam("nfsnobody"))
194 test(m, "exec-group-nfsnobody.service", 0, CLD_EXITED);
ff4ca461 195 else
50f130c2 196 log_error_errno(errno, "Skipping test_exec_group, could not find nobody/nfsnobody group: %m");
281e05b6
RC
197}
198
199static void test_exec_environment(Manager *m) {
200 test(m, "exec-environment.service", 0, CLD_EXITED);
201 test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
202 test(m, "exec-environment-empty.service", 0, CLD_EXITED);
203}
204
03bd70dd
RC
205static void test_exec_environmentfile(Manager *m) {
206 static const char e[] =
207 "VAR1='word1 word2'\n"
208 "VAR2=word3 \n"
209 "# comment1\n"
210 "\n"
211 "; comment2\n"
212 " ; # comment3\n"
213 "line without an equal\n"
214 "VAR3='$word 5 6'\n";
215 int r;
216
217 r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
218 assert_se(r == 0);
219
220 test(m, "exec-environmentfile.service", 0, CLD_EXITED);
221
222 unlink("/tmp/test-exec_environmentfile.conf");
223}
224
4c80d201 225static void test_exec_passenvironment(Manager *m) {
e1abca2e
FB
226 /* test-execute runs under MANAGER_USER which, by default, forwards all
227 * variables present in the environment, but only those that are
228 * present _at the time it is created_!
229 *
230 * So these PassEnvironment checks are still expected to work, since we
231 * are ensuring the variables are not present at manager creation (they
232 * are unset explicitly in main) and are only set here.
233 *
234 * This is still a good approximation of how a test for MANAGER_SYSTEM
235 * would work.
236 */
4c80d201
FB
237 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
238 assert_se(setenv("VAR2", "word3", 1) == 0);
239 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
240 test(m, "exec-passenvironment.service", 0, CLD_EXITED);
241 test(m, "exec-passenvironment-repeated.service", 0, CLD_EXITED);
242 test(m, "exec-passenvironment-empty.service", 0, CLD_EXITED);
243 assert_se(unsetenv("VAR1") == 0);
244 assert_se(unsetenv("VAR2") == 0);
245 assert_se(unsetenv("VAR3") == 0);
246 test(m, "exec-passenvironment-absent.service", 0, CLD_EXITED);
247}
248
27c5347c
RC
249static void test_exec_umask(Manager *m) {
250 test(m, "exec-umask-default.service", 0, CLD_EXITED);
251 test(m, "exec-umask-0177.service", 0, CLD_EXITED);
252}
253
cc3ddc85
RC
254static void test_exec_runtimedirectory(Manager *m) {
255 test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
256 test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
cc7fa4fb
RC
257 if (getgrnam("nobody"))
258 test(m, "exec-runtimedirectory-owner.service", 0, CLD_EXITED);
50f130c2
RC
259 else if (getgrnam("nfsnobody"))
260 test(m, "exec-runtimedirectory-owner-nfsnobody.service", 0, CLD_EXITED);
ff4ca461 261 else
50f130c2 262 log_error_errno(errno, "Skipping test_exec_runtimedirectory-owner, could not find nobody/nfsnobody group: %m");
ff4ca461
RC
263}
264
265static void test_exec_capabilityboundingset(Manager *m) {
266 int r;
267
268 /* We use capsh to test if the capabilities are
269 * properly set, so be sure that it exists */
270 r = find_binary("capsh", NULL);
271 if (r < 0) {
272 log_error_errno(r, "Skipping test_exec_capabilityboundingset, could not find capsh binary: %m");
273 return;
274 }
275
276 test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
277 test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
278 test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
279 test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
cc3ddc85
RC
280}
281
70d7aea5
IP
282static void test_exec_capabilityambientset(Manager *m) {
283 int r;
284
285 /* Check if the kernel has support for ambient capabilities. Run
286 * the tests only if that's the case. Clearing all ambient
287 * capabilities is fine, since we are expecting them to be unset
288 * in the first place for the tests. */
289 r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
290 if (r >= 0 || errno != EINVAL) {
50f130c2 291 if (getpwnam("nobody")) {
50f130c2
RC
292 test(m, "exec-capabilityambientset.service", 0, CLD_EXITED);
293 test(m, "exec-capabilityambientset-merge.service", 0, CLD_EXITED);
34f5ff46
RC
294 } else if (getpwnam("nfsnobody")) {
295 test(m, "exec-capabilityambientset-nfsnobody.service", 0, CLD_EXITED);
296 test(m, "exec-capabilityambientset-merge-nfsnobody.service", 0, CLD_EXITED);
50f130c2
RC
297 } else
298 log_error_errno(errno, "Skipping test_exec_capabilityambientset, could not find nobody/nfsnobody user: %m");
299 } else
300 log_error_errno(errno, "Skipping test_exec_capabilityambientset, the kernel does not support ambient capabilities: %m");
70d7aea5
IP
301}
302
63447f11
RC
303static void test_exec_privatenetwork(Manager *m) {
304 int r;
305
306 r = find_binary("ip", NULL);
307 if (r < 0) {
308 log_error_errno(r, "Skipping test_exec_privatenetwork, could not find ip binary: %m");
309 return;
310 }
311
312 test(m, "exec-privatenetwork-yes.service", 0, CLD_EXITED);
313}
314
c388dfea
RC
315static void test_exec_oomscoreadjust(Manager *m) {
316 test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
317 test(m, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED);
318}
319
a6226758
RC
320static void test_exec_ioschedulingclass(Manager *m) {
321 test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
322 test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
323 test(m, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED);
324 test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
325}
326
25254999
EV
327static void test_exec_spec_interpolation(Manager *m) {
328 test(m, "exec-spec-interpolation.service", 0, CLD_EXITED);
329}
330
463d0d15 331static int run_tests(UnitFileScope scope, test_function_t *tests) {
19c0b0b9
RC
332 test_function_t *test = NULL;
333 Manager *m = NULL;
334 int r;
335
336 assert_se(tests);
337
463d0d15 338 r = manager_new(scope, true, &m);
19c0b0b9 339 if (MANAGER_SKIP_TEST(r)) {
2179fd10 340 log_notice_errno(r, "Skipping test: manager_new: %m");
19c0b0b9
RC
341 return EXIT_TEST_SKIP;
342 }
343 assert_se(r >= 0);
344 assert_se(manager_startup(m, NULL, NULL) >= 0);
345
346 for (test = tests; test && *test; test++)
347 (*test)(m);
348
349 manager_free(m);
350
351 return 0;
352}
353
281e05b6 354int main(int argc, char *argv[]) {
19c0b0b9 355 test_function_t user_tests[] = {
281e05b6
RC
356 test_exec_workingdirectory,
357 test_exec_personality,
358 test_exec_ignoresigpipe,
359 test_exec_privatetmp,
360 test_exec_privatedevices,
615a1f4b 361 test_exec_privatedevices_capabilities,
f78b36f0 362 test_exec_readonlypaths,
63447f11 363 test_exec_privatenetwork,
281e05b6
RC
364 test_exec_systemcallfilter,
365 test_exec_systemcallerrornumber,
366 test_exec_user,
367 test_exec_group,
368 test_exec_environment,
03bd70dd 369 test_exec_environmentfile,
4c80d201 370 test_exec_passenvironment,
27c5347c 371 test_exec_umask,
cc3ddc85 372 test_exec_runtimedirectory,
ff4ca461 373 test_exec_capabilityboundingset,
70d7aea5 374 test_exec_capabilityambientset,
c388dfea 375 test_exec_oomscoreadjust,
a6226758 376 test_exec_ioschedulingclass,
25254999 377 test_exec_spec_interpolation,
281e05b6
RC
378 NULL,
379 };
19c0b0b9
RC
380 test_function_t system_tests[] = {
381 test_exec_systemcall_system_mode_with_user,
382 NULL,
383 };
281e05b6
RC
384 int r;
385
386 log_parse_environment();
387 log_open();
388
607ff5f9
DH
389 /* It is needed otherwise cgroup creation fails */
390 if (getuid() != 0) {
391 printf("Skipping test: not root\n");
392 return EXIT_TEST_SKIP;
393 }
394
cc3ddc85 395 assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
ac400816 396 assert_se(set_unit_path(TEST_DIR "/test-execute/") >= 0);
281e05b6 397
e1abca2e
FB
398 /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
399 * cases, otherwise (and if they are present in the environment),
400 * `manager_default_environment` will copy them into the default
401 * environment which is passed to each created job, which will make the
402 * tests that expect those not to be present to fail.
403 */
404 assert_se(unsetenv("VAR1") == 0);
405 assert_se(unsetenv("VAR2") == 0);
406 assert_se(unsetenv("VAR3") == 0);
407
463d0d15 408 r = run_tests(UNIT_FILE_USER, user_tests);
19c0b0b9
RC
409 if (r != 0)
410 return r;
281e05b6 411
463d0d15 412 return run_tests(UNIT_FILE_SYSTEM, system_tests);
281e05b6 413}