]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-exec-util.c
sd-bus: Add sd_bus_message_peek_type docs
[thirdparty/systemd.git] / src / test / test-exec-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
89711996
ZJS
2
3#include <errno.h>
89711996
ZJS
4#include <sys/stat.h>
5#include <sys/wait.h>
6#include <unistd.h>
7
c6e47247
ZJS
8#include "alloc-util.h"
9#include "copy.h"
89711996 10#include "def.h"
3303d1b2 11#include "env-util.h"
89711996 12#include "exec-util.h"
c6e47247 13#include "fd-util.h"
89711996
ZJS
14#include "fileio.h"
15#include "fs-util.h"
16#include "log.h"
17#include "macro.h"
78ec1bb4 18#include "path-util.h"
89711996
ZJS
19#include "rm-rf.h"
20#include "string-util.h"
c6e47247 21#include "strv.h"
6d7c4033 22#include "tests.h"
89711996 23
c6e47247
ZJS
24static int here = 0, here2 = 0, here3 = 0;
25void *ignore_stdout_args[] = {&here, &here2, &here3};
26
27/* noop handlers, just check that arguments are passed correctly */
28static int ignore_stdout_func(int fd, void *arg) {
29 assert(fd >= 0);
30 assert(arg == &here);
31 safe_close(fd);
32
33 return 0;
34}
35static int ignore_stdout_func2(int fd, void *arg) {
36 assert(fd >= 0);
37 assert(arg == &here2);
38 safe_close(fd);
39
40 return 0;
41}
42static int ignore_stdout_func3(int fd, void *arg) {
43 assert(fd >= 0);
44 assert(arg == &here3);
45 safe_close(fd);
46
47 return 0;
48}
49
50static const gather_stdout_callback_t ignore_stdout[] = {
51 ignore_stdout_func,
52 ignore_stdout_func2,
53 ignore_stdout_func3,
54};
55
56static void test_execute_directory(bool gather_stdout) {
f66137fb
ZJS
57 char template_lo[] = "/tmp/test-exec-util.lo.XXXXXXX";
58 char template_hi[] = "/tmp/test-exec-util.hi.XXXXXXX";
89711996 59 const char * dirs[] = {template_hi, template_lo, NULL};
f66137fb
ZJS
60 const char *name, *name2, *name3,
61 *overridden, *override,
62 *masked, *mask,
63 *masked2, *mask2, /* the mask is non-executable */
64 *masked2e, *mask2e; /* the mask is executable */
89711996 65
c6e47247
ZJS
66 log_info("/* %s (%s) */", __func__, gather_stdout ? "gathering stdout" : "asynchronous");
67
89711996
ZJS
68 assert_se(mkdtemp(template_lo));
69 assert_se(mkdtemp(template_hi));
70
71 name = strjoina(template_lo, "/script");
72 name2 = strjoina(template_hi, "/script2");
73 name3 = strjoina(template_lo, "/useless");
74 overridden = strjoina(template_lo, "/overridden");
75 override = strjoina(template_hi, "/overridden");
76 masked = strjoina(template_lo, "/masked");
77 mask = strjoina(template_hi, "/masked");
f66137fb
ZJS
78 masked2 = strjoina(template_lo, "/masked2");
79 mask2 = strjoina(template_hi, "/masked2");
80 masked2e = strjoina(template_lo, "/masked2e");
81 mask2e = strjoina(template_hi, "/masked2e");
89711996 82
c6e47247
ZJS
83 assert_se(write_string_file(name,
84 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works",
85 WRITE_STRING_FILE_CREATE) == 0);
86 assert_se(write_string_file(name2,
87 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2",
88 WRITE_STRING_FILE_CREATE) == 0);
89 assert_se(write_string_file(overridden,
90 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
91 WRITE_STRING_FILE_CREATE) == 0);
92 assert_se(write_string_file(override,
93 "#!/bin/sh\necho 'Executing '$0",
94 WRITE_STRING_FILE_CREATE) == 0);
95 assert_se(write_string_file(masked,
96 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
97 WRITE_STRING_FILE_CREATE) == 0);
f66137fb
ZJS
98 assert_se(write_string_file(masked2,
99 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
100 WRITE_STRING_FILE_CREATE) == 0);
101 assert_se(write_string_file(masked2e,
102 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
103 WRITE_STRING_FILE_CREATE) == 0);
89711996 104 assert_se(symlink("/dev/null", mask) == 0);
f66137fb
ZJS
105 assert_se(touch(mask2) == 0);
106 assert_se(touch(mask2e) == 0);
c6e47247
ZJS
107 assert_se(touch(name3) >= 0);
108
89711996
ZJS
109 assert_se(chmod(name, 0755) == 0);
110 assert_se(chmod(name2, 0755) == 0);
111 assert_se(chmod(overridden, 0755) == 0);
112 assert_se(chmod(override, 0755) == 0);
113 assert_se(chmod(masked, 0755) == 0);
f66137fb
ZJS
114 assert_se(chmod(masked2, 0755) == 0);
115 assert_se(chmod(masked2e, 0755) == 0);
116 assert_se(chmod(mask2e, 0755) == 0);
89711996 117
c6e47247 118 if (gather_stdout)
4b05f0c9 119 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
c6e47247 120 else
4b05f0c9 121 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, NULL, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
89711996
ZJS
122
123 assert_se(chdir(template_lo) == 0);
124 assert_se(access("it_works", F_OK) >= 0);
125 assert_se(access("failed", F_OK) < 0);
126
127 assert_se(chdir(template_hi) == 0);
128 assert_se(access("it_works2", F_OK) >= 0);
129 assert_se(access("failed", F_OK) < 0);
130
131 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
132 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
133}
134
c6e47247
ZJS
135static void test_execution_order(void) {
136 char template_lo[] = "/tmp/test-exec-util-lo.XXXXXXX";
137 char template_hi[] = "/tmp/test-exec-util-hi.XXXXXXX";
138 const char *dirs[] = {template_hi, template_lo, NULL};
139 const char *name, *name2, *name3, *overridden, *override, *masked, *mask;
140 const char *output, *t;
141 _cleanup_free_ char *contents = NULL;
142
143 assert_se(mkdtemp(template_lo));
144 assert_se(mkdtemp(template_hi));
145
146 output = strjoina(template_hi, "/output");
147
148 log_info("/* %s >>%s */", __func__, output);
149
150 /* write files in "random" order */
151 name2 = strjoina(template_lo, "/90-bar");
152 name = strjoina(template_hi, "/80-foo");
153 name3 = strjoina(template_lo, "/last");
154 overridden = strjoina(template_lo, "/30-override");
155 override = strjoina(template_hi, "/30-override");
156 masked = strjoina(template_lo, "/10-masked");
157 mask = strjoina(template_hi, "/10-masked");
158
159 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
160 assert_se(write_string_file(name, t, WRITE_STRING_FILE_CREATE) == 0);
161
162 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
163 assert_se(write_string_file(name2, t, WRITE_STRING_FILE_CREATE) == 0);
164
165 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
166 assert_se(write_string_file(name3, t, WRITE_STRING_FILE_CREATE) == 0);
167
168 t = strjoina("#!/bin/sh\necho OVERRIDDEN >>", output);
169 assert_se(write_string_file(overridden, t, WRITE_STRING_FILE_CREATE) == 0);
170
171 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
172 assert_se(write_string_file(override, t, WRITE_STRING_FILE_CREATE) == 0);
173
174 t = strjoina("#!/bin/sh\necho MASKED >>", output);
175 assert_se(write_string_file(masked, t, WRITE_STRING_FILE_CREATE) == 0);
176
177 assert_se(symlink("/dev/null", mask) == 0);
178
179 assert_se(chmod(name, 0755) == 0);
180 assert_se(chmod(name2, 0755) == 0);
181 assert_se(chmod(name3, 0755) == 0);
182 assert_se(chmod(overridden, 0755) == 0);
183 assert_se(chmod(override, 0755) == 0);
184 assert_se(chmod(masked, 0755) == 0);
185
4b05f0c9 186 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
c6e47247
ZJS
187
188 assert_se(read_full_file(output, &contents, NULL) >= 0);
189 assert_se(streq(contents, "30-override\n80-foo\n90-bar\nlast\n"));
190
191 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
192 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
193}
194
195static int gather_stdout_one(int fd, void *arg) {
196 char ***s = arg, *t;
197 char buf[128] = {};
198
199 assert_se(s);
200 assert_se(read(fd, buf, sizeof buf) >= 0);
201 safe_close(fd);
202
203 assert_se(t = strndup(buf, sizeof buf));
204 assert_se(strv_push(s, t) >= 0);
205
206 return 0;
207}
208static int gather_stdout_two(int fd, void *arg) {
209 char ***s = arg, **t;
210
211 STRV_FOREACH(t, *s)
212 assert_se(write(fd, *t, strlen(*t)) == (ssize_t) strlen(*t));
213 safe_close(fd);
214
215 return 0;
216}
217static int gather_stdout_three(int fd, void *arg) {
218 char **s = arg;
219 char buf[128] = {};
220
221 assert_se(read(fd, buf, sizeof buf - 1) > 0);
222 safe_close(fd);
223 assert_se(*s = strndup(buf, sizeof buf));
224
225 return 0;
226}
227
4fa3993b 228const gather_stdout_callback_t gather_stdout[] = {
c6e47247
ZJS
229 gather_stdout_one,
230 gather_stdout_two,
231 gather_stdout_three,
232};
233
c6e47247
ZJS
234static void test_stdout_gathering(void) {
235 char template[] = "/tmp/test-exec-util.XXXXXXX";
236 const char *dirs[] = {template, NULL};
237 const char *name, *name2, *name3;
238 int r;
239
240 char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
241 _cleanup_free_ char *output = NULL;
242
243 void* args[] = {&tmp, &tmp, &output};
244
245 assert_se(mkdtemp(template));
246
247 log_info("/* %s */", __func__);
248
249 /* write files */
250 name = strjoina(template, "/10-foo");
251 name2 = strjoina(template, "/20-bar");
252 name3 = strjoina(template, "/30-last");
253
254 assert_se(write_string_file(name,
255 "#!/bin/sh\necho a\necho b\necho c\n",
256 WRITE_STRING_FILE_CREATE) == 0);
257 assert_se(write_string_file(name2,
258 "#!/bin/sh\necho d\n",
259 WRITE_STRING_FILE_CREATE) == 0);
260 assert_se(write_string_file(name3,
261 "#!/bin/sh\nsleep 1",
262 WRITE_STRING_FILE_CREATE) == 0);
263
264 assert_se(chmod(name, 0755) == 0);
265 assert_se(chmod(name2, 0755) == 0);
266 assert_se(chmod(name3, 0755) == 0);
267
4b05f0c9 268 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_stdout, args, NULL, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
c6e47247
ZJS
269 assert_se(r >= 0);
270
271 log_info("got: %s", output);
272
273 assert_se(streq(output, "a\nb\nc\nd\n"));
274}
275
3303d1b2
ZJS
276static void test_environment_gathering(void) {
277 char template[] = "/tmp/test-exec-util.XXXXXXX", **p;
278 const char *dirs[] = {template, NULL};
78ec1bb4 279 const char *name, *name2, *name3, *old;
3303d1b2
ZJS
280 int r;
281
282 char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
283 _cleanup_strv_free_ char **env = NULL;
284
285 void* const args[] = { &tmp, &tmp, &env };
286
287 assert_se(mkdtemp(template));
288
289 log_info("/* %s */", __func__);
290
291 /* write files */
292 name = strjoina(template, "/10-foo");
293 name2 = strjoina(template, "/20-bar");
294 name3 = strjoina(template, "/30-last");
295
296 assert_se(write_string_file(name,
297 "#!/bin/sh\n"
298 "echo A=23\n",
299 WRITE_STRING_FILE_CREATE) == 0);
300 assert_se(write_string_file(name2,
301 "#!/bin/sh\n"
302 "echo A=22:$A\n\n\n", /* substitution from previous generator */
303 WRITE_STRING_FILE_CREATE) == 0);
304 assert_se(write_string_file(name3,
305 "#!/bin/sh\n"
306 "echo A=$A:24\n"
307 "echo B=12\n"
308 "echo C=000\n"
184d1904
ZJS
309 "echo C=001\n" /* variable overwriting */
310 /* various invalid entries */
311 "echo unset A\n"
312 "echo unset A=\n"
313 "echo unset A=B\n"
314 "echo unset \n"
315 "echo A B=C\n"
316 "echo A\n"
317 /* test variable assignment without newline */
318 "echo PATH=$PATH:/no/such/file", /* no newline */
3303d1b2
ZJS
319 WRITE_STRING_FILE_CREATE) == 0);
320
321 assert_se(chmod(name, 0755) == 0);
322 assert_se(chmod(name2, 0755) == 0);
323 assert_se(chmod(name3, 0755) == 0);
324
78ec1bb4 325 /* When booting in containers or without initramfs there might not be
04193fb2 326 * any PATH in the environment and if there is no PATH /bin/sh built-in
78ec1bb4
DJL
327 * PATH may leak and override systemd's DEFAULT_PATH which is not
328 * good. Force our own PATH in environment, to prevent expansion of sh
329 * built-in $PATH */
330 old = getenv("PATH");
331 r = setenv("PATH", "no-sh-built-in-path", 1);
332 assert_se(r >= 0);
333
4b05f0c9 334 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
78ec1bb4
DJL
335 assert_se(r >= 0);
336
337 STRV_FOREACH(p, env)
338 log_info("got env: \"%s\"", *p);
339
340 assert_se(streq(strv_env_get(env, "A"), "22:23:24"));
341 assert_se(streq(strv_env_get(env, "B"), "12"));
342 assert_se(streq(strv_env_get(env, "C"), "001"));
343 assert_se(streq(strv_env_get(env, "PATH"), "no-sh-built-in-path:/no/such/file"));
344
345 /* now retest with "default" path passed in, as created by
346 * manager_default_environment */
347 env = strv_free(env);
bea1a013
LP
348 env = strv_new("PATH=" DEFAULT_PATH);
349 assert_se(env);
78ec1bb4 350
4b05f0c9 351 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL, env, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
3303d1b2
ZJS
352 assert_se(r >= 0);
353
354 STRV_FOREACH(p, env)
355 log_info("got env: \"%s\"", *p);
356
357 assert_se(streq(strv_env_get(env, "A"), "22:23:24"));
358 assert_se(streq(strv_env_get(env, "B"), "12"));
359 assert_se(streq(strv_env_get(env, "C"), "001"));
78ec1bb4
DJL
360 assert_se(streq(strv_env_get(env, "PATH"), DEFAULT_PATH ":/no/such/file"));
361
362 /* reset environ PATH */
04193fb2
ZJS
363 if (old)
364 (void) setenv("PATH", old, 1);
365 else
366 (void) unsetenv("PATH");
3303d1b2
ZJS
367}
368
4b05f0c9
MK
369static void test_error_catching(void) {
370 char template[] = "/tmp/test-exec-util.XXXXXXX";
371 const char *dirs[] = {template, NULL};
372 const char *name, *name2, *name3;
373 int r;
374
375 assert_se(mkdtemp(template));
376
377 log_info("/* %s */", __func__);
378
379 /* write files */
380 name = strjoina(template, "/10-foo");
381 name2 = strjoina(template, "/20-bar");
382 name3 = strjoina(template, "/30-last");
383
384 assert_se(write_string_file(name,
385 "#!/bin/sh\necho a\necho b\necho c\n",
386 WRITE_STRING_FILE_CREATE) == 0);
387 assert_se(write_string_file(name2,
388 "#!/bin/sh\nexit 42\n",
389 WRITE_STRING_FILE_CREATE) == 0);
390 assert_se(write_string_file(name3,
391 "#!/bin/sh\nexit 12",
392 WRITE_STRING_FILE_CREATE) == 0);
393
394 assert_se(chmod(name, 0755) == 0);
395 assert_se(chmod(name2, 0755) == 0);
396 assert_se(chmod(name3, 0755) == 0);
397
398 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, NULL, NULL, EXEC_DIR_NONE);
399
400 /* we should exit with the error code of the first script that failed */
401 assert_se(r == 42);
402}
403
b3d59367
AZ
404static void test_exec_command_flags_from_strv(void) {
405 ExecCommandFlags flags = 0;
406 char **valid_strv = STRV_MAKE("no-env-expand", "no-setuid", "ignore-failure");
407 char **invalid_strv = STRV_MAKE("no-env-expand", "no-setuid", "nonexistent-option", "ignore-failure");
408 int r;
409
410 r = exec_command_flags_from_strv(valid_strv, &flags);
411
412 assert_se(r == 0);
413 assert_se(FLAGS_SET(flags, EXEC_COMMAND_NO_ENV_EXPAND));
414 assert_se(FLAGS_SET(flags, EXEC_COMMAND_NO_SETUID));
415 assert_se(FLAGS_SET(flags, EXEC_COMMAND_IGNORE_FAILURE));
416 assert_se(!FLAGS_SET(flags, EXEC_COMMAND_AMBIENT_MAGIC));
417 assert_se(!FLAGS_SET(flags, EXEC_COMMAND_FULLY_PRIVILEGED));
418
419 r = exec_command_flags_from_strv(invalid_strv, &flags);
420
421 assert_se(r == -EINVAL);
422}
423
424static void test_exec_command_flags_to_strv(void) {
425 _cleanup_strv_free_ char **opts = NULL, **empty_opts = NULL, **invalid_opts = NULL;
426 ExecCommandFlags flags = 0;
427 int r;
428
429 flags |= (EXEC_COMMAND_AMBIENT_MAGIC|EXEC_COMMAND_NO_ENV_EXPAND|EXEC_COMMAND_IGNORE_FAILURE);
430
431 r = exec_command_flags_to_strv(flags, &opts);
432
433 assert_se(r == 0);
434 assert_se(strv_equal(opts, STRV_MAKE("ignore-failure", "ambient", "no-env-expand")));
435
436 r = exec_command_flags_to_strv(0, &empty_opts);
437
438 assert_se(r == 0);
439 assert_se(strv_equal(empty_opts, STRV_MAKE_EMPTY));
440
441 flags = _EXEC_COMMAND_FLAGS_INVALID;
442
443 r = exec_command_flags_to_strv(flags, &invalid_opts);
444
445 assert_se(r == -EINVAL);
446}
447
89711996 448int main(int argc, char *argv[]) {
6d7c4033 449 test_setup_logging(LOG_DEBUG);
89711996 450
c6e47247
ZJS
451 test_execute_directory(true);
452 test_execute_directory(false);
453 test_execution_order();
454 test_stdout_gathering();
3303d1b2 455 test_environment_gathering();
4b05f0c9 456 test_error_catching();
b3d59367
AZ
457 test_exec_command_flags_from_strv();
458 test_exec_command_flags_to_strv();
89711996
ZJS
459
460 return 0;
461}