]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-execute.c
hwdb: 60-keyboard: remove line causing a syntax error (#3999)
[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 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
83 static 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
88 (void) rm_rf("/tmp/test-exec_workingdirectory", REMOVE_ROOT|REMOVE_PHYSICAL);
89 }
90
91 static void test_exec_personality(Manager *m) {
92 #if defined(__x86_64__)
93 test(m, "exec-personality-x86-64.service", 0, CLD_EXITED);
94
95 #elif defined(__s390__)
96 test(m, "exec-personality-s390.service", 0, CLD_EXITED);
97
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
108 #elif defined(__i386__)
109 test(m, "exec-personality-x86.service", 0, CLD_EXITED);
110 #endif
111 }
112
113 static 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
118 static 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
127 static void test_exec_privatedevices(Manager *m) {
128 if (detect_container() > 0) {
129 log_notice("testing in container, skipping private device tests");
130 return;
131 }
132 test(m, "exec-privatedevices-yes.service", 0, CLD_EXITED);
133 test(m, "exec-privatedevices-no.service", 0, CLD_EXITED);
134 }
135
136 static void test_exec_systemcallfilter(Manager *m) {
137 #ifdef HAVE_SECCOMP
138 if (!is_seccomp_available())
139 return;
140 test(m, "exec-systemcallfilter-not-failing.service", 0, CLD_EXITED);
141 test(m, "exec-systemcallfilter-not-failing2.service", 0, CLD_EXITED);
142 test(m, "exec-systemcallfilter-failing.service", SIGSYS, CLD_KILLED);
143 test(m, "exec-systemcallfilter-failing2.service", SIGSYS, CLD_KILLED);
144
145 #endif
146 }
147
148 static void test_exec_systemcallerrornumber(Manager *m) {
149 #ifdef HAVE_SECCOMP
150 if (is_seccomp_available())
151 test(m, "exec-systemcallerrornumber.service", 1, CLD_EXITED);
152 #endif
153 }
154
155 static void test_exec_systemcall_system_mode_with_user(Manager *m) {
156 #ifdef HAVE_SECCOMP
157 if (!is_seccomp_available())
158 return;
159 if (getpwnam("nobody"))
160 test(m, "exec-systemcallfilter-system-user.service", 0, CLD_EXITED);
161 else if (getpwnam("nfsnobody"))
162 test(m, "exec-systemcallfilter-system-user-nfsnobody.service", 0, CLD_EXITED);
163 else
164 log_error_errno(errno, "Skipping test_exec_systemcall_system_mode_with_user, could not find nobody/nfsnobody user: %m");
165 #endif
166 }
167
168 static void test_exec_user(Manager *m) {
169 if (getpwnam("nobody"))
170 test(m, "exec-user.service", 0, CLD_EXITED);
171 else if (getpwnam("nfsnobody"))
172 test(m, "exec-user-nfsnobody.service", 0, CLD_EXITED);
173 else
174 log_error_errno(errno, "Skipping test_exec_user, could not find nobody/nfsnobody user: %m");
175 }
176
177 static void test_exec_group(Manager *m) {
178 if (getgrnam("nobody"))
179 test(m, "exec-group.service", 0, CLD_EXITED);
180 else if (getgrnam("nfsnobody"))
181 test(m, "exec-group-nfsnobody.service", 0, CLD_EXITED);
182 else
183 log_error_errno(errno, "Skipping test_exec_group, could not find nobody/nfsnobody group: %m");
184 }
185
186 static void test_exec_environment(Manager *m) {
187 test(m, "exec-environment.service", 0, CLD_EXITED);
188 test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
189 test(m, "exec-environment-empty.service", 0, CLD_EXITED);
190 }
191
192 static void test_exec_environmentfile(Manager *m) {
193 static const char e[] =
194 "VAR1='word1 word2'\n"
195 "VAR2=word3 \n"
196 "# comment1\n"
197 "\n"
198 "; comment2\n"
199 " ; # comment3\n"
200 "line without an equal\n"
201 "VAR3='$word 5 6'\n";
202 int r;
203
204 r = write_string_file("/tmp/test-exec_environmentfile.conf", e, WRITE_STRING_FILE_CREATE);
205 assert_se(r == 0);
206
207 test(m, "exec-environmentfile.service", 0, CLD_EXITED);
208
209 unlink("/tmp/test-exec_environmentfile.conf");
210 }
211
212 static void test_exec_passenvironment(Manager *m) {
213 /* test-execute runs under MANAGER_USER which, by default, forwards all
214 * variables present in the environment, but only those that are
215 * present _at the time it is created_!
216 *
217 * So these PassEnvironment checks are still expected to work, since we
218 * are ensuring the variables are not present at manager creation (they
219 * are unset explicitly in main) and are only set here.
220 *
221 * This is still a good approximation of how a test for MANAGER_SYSTEM
222 * would work.
223 */
224 assert_se(setenv("VAR1", "word1 word2", 1) == 0);
225 assert_se(setenv("VAR2", "word3", 1) == 0);
226 assert_se(setenv("VAR3", "$word 5 6", 1) == 0);
227 test(m, "exec-passenvironment.service", 0, CLD_EXITED);
228 test(m, "exec-passenvironment-repeated.service", 0, CLD_EXITED);
229 test(m, "exec-passenvironment-empty.service", 0, CLD_EXITED);
230 assert_se(unsetenv("VAR1") == 0);
231 assert_se(unsetenv("VAR2") == 0);
232 assert_se(unsetenv("VAR3") == 0);
233 test(m, "exec-passenvironment-absent.service", 0, CLD_EXITED);
234 }
235
236 static void test_exec_umask(Manager *m) {
237 test(m, "exec-umask-default.service", 0, CLD_EXITED);
238 test(m, "exec-umask-0177.service", 0, CLD_EXITED);
239 }
240
241 static void test_exec_runtimedirectory(Manager *m) {
242 test(m, "exec-runtimedirectory.service", 0, CLD_EXITED);
243 test(m, "exec-runtimedirectory-mode.service", 0, CLD_EXITED);
244 if (getgrnam("nobody"))
245 test(m, "exec-runtimedirectory-owner.service", 0, CLD_EXITED);
246 else if (getgrnam("nfsnobody"))
247 test(m, "exec-runtimedirectory-owner-nfsnobody.service", 0, CLD_EXITED);
248 else
249 log_error_errno(errno, "Skipping test_exec_runtimedirectory-owner, could not find nobody/nfsnobody group: %m");
250 }
251
252 static void test_exec_capabilityboundingset(Manager *m) {
253 int r;
254
255 /* We use capsh to test if the capabilities are
256 * properly set, so be sure that it exists */
257 r = find_binary("capsh", NULL);
258 if (r < 0) {
259 log_error_errno(r, "Skipping test_exec_capabilityboundingset, could not find capsh binary: %m");
260 return;
261 }
262
263 test(m, "exec-capabilityboundingset-simple.service", 0, CLD_EXITED);
264 test(m, "exec-capabilityboundingset-reset.service", 0, CLD_EXITED);
265 test(m, "exec-capabilityboundingset-merge.service", 0, CLD_EXITED);
266 test(m, "exec-capabilityboundingset-invert.service", 0, CLD_EXITED);
267 }
268
269 static void test_exec_capabilityambientset(Manager *m) {
270 int r;
271
272 /* Check if the kernel has support for ambient capabilities. Run
273 * the tests only if that's the case. Clearing all ambient
274 * capabilities is fine, since we are expecting them to be unset
275 * in the first place for the tests. */
276 r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0);
277 if (r >= 0 || errno != EINVAL) {
278 if (getpwnam("nobody")) {
279 test(m, "exec-capabilityambientset.service", 0, CLD_EXITED);
280 test(m, "exec-capabilityambientset-merge.service", 0, CLD_EXITED);
281 } else if (getpwnam("nfsnobody")) {
282 test(m, "exec-capabilityambientset-nfsnobody.service", 0, CLD_EXITED);
283 test(m, "exec-capabilityambientset-merge-nfsnobody.service", 0, CLD_EXITED);
284 } else
285 log_error_errno(errno, "Skipping test_exec_capabilityambientset, could not find nobody/nfsnobody user: %m");
286 } else
287 log_error_errno(errno, "Skipping test_exec_capabilityambientset, the kernel does not support ambient capabilities: %m");
288 }
289
290 static void test_exec_privatenetwork(Manager *m) {
291 int r;
292
293 r = find_binary("ip", NULL);
294 if (r < 0) {
295 log_error_errno(r, "Skipping test_exec_privatenetwork, could not find ip binary: %m");
296 return;
297 }
298
299 test(m, "exec-privatenetwork-yes.service", 0, CLD_EXITED);
300 }
301
302 static void test_exec_oomscoreadjust(Manager *m) {
303 test(m, "exec-oomscoreadjust-positive.service", 0, CLD_EXITED);
304 test(m, "exec-oomscoreadjust-negative.service", 0, CLD_EXITED);
305 }
306
307 static void test_exec_ioschedulingclass(Manager *m) {
308 test(m, "exec-ioschedulingclass-none.service", 0, CLD_EXITED);
309 test(m, "exec-ioschedulingclass-idle.service", 0, CLD_EXITED);
310 test(m, "exec-ioschedulingclass-realtime.service", 0, CLD_EXITED);
311 test(m, "exec-ioschedulingclass-best-effort.service", 0, CLD_EXITED);
312 }
313
314 static void test_exec_spec_interpolation(Manager *m) {
315 test(m, "exec-spec-interpolation.service", 0, CLD_EXITED);
316 }
317
318 static int run_tests(UnitFileScope scope, test_function_t *tests) {
319 test_function_t *test = NULL;
320 Manager *m = NULL;
321 int r;
322
323 assert_se(tests);
324
325 r = manager_new(scope, true, &m);
326 if (MANAGER_SKIP_TEST(r)) {
327 printf("Skipping test: manager_new: %s\n", strerror(-r));
328 return EXIT_TEST_SKIP;
329 }
330 assert_se(r >= 0);
331 assert_se(manager_startup(m, NULL, NULL) >= 0);
332
333 for (test = tests; test && *test; test++)
334 (*test)(m);
335
336 manager_free(m);
337
338 return 0;
339 }
340
341 int main(int argc, char *argv[]) {
342 test_function_t user_tests[] = {
343 test_exec_workingdirectory,
344 test_exec_personality,
345 test_exec_ignoresigpipe,
346 test_exec_privatetmp,
347 test_exec_privatedevices,
348 test_exec_privatenetwork,
349 test_exec_systemcallfilter,
350 test_exec_systemcallerrornumber,
351 test_exec_user,
352 test_exec_group,
353 test_exec_environment,
354 test_exec_environmentfile,
355 test_exec_passenvironment,
356 test_exec_umask,
357 test_exec_runtimedirectory,
358 test_exec_capabilityboundingset,
359 test_exec_capabilityambientset,
360 test_exec_oomscoreadjust,
361 test_exec_ioschedulingclass,
362 test_exec_spec_interpolation,
363 NULL,
364 };
365 test_function_t system_tests[] = {
366 test_exec_systemcall_system_mode_with_user,
367 NULL,
368 };
369 int r;
370
371 log_parse_environment();
372 log_open();
373
374 /* It is needed otherwise cgroup creation fails */
375 if (getuid() != 0) {
376 printf("Skipping test: not root\n");
377 return EXIT_TEST_SKIP;
378 }
379
380 assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
381 assert_se(set_unit_path(TEST_DIR "/test-execute/") >= 0);
382
383 /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
384 * cases, otherwise (and if they are present in the environment),
385 * `manager_default_environment` will copy them into the default
386 * environment which is passed to each created job, which will make the
387 * tests that expect those not to be present to fail.
388 */
389 assert_se(unsetenv("VAR1") == 0);
390 assert_se(unsetenv("VAR2") == 0);
391 assert_se(unsetenv("VAR3") == 0);
392
393 r = run_tests(UNIT_FILE_USER, user_tests);
394 if (r != 0)
395 return r;
396
397 return run_tests(UNIT_FILE_SYSTEM, system_tests);
398 }