]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-exec-util.c
Merge pull request #15442 from poettering/fido2
[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
3c14dc61
TM
118 if (access(name, X_OK) < 0 && ERRNO_IS_PRIVILEGE(errno))
119 return;
120
c6e47247 121 if (gather_stdout)
4b05f0c9 122 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
c6e47247 123 else
4b05f0c9 124 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, NULL, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
89711996
ZJS
125
126 assert_se(chdir(template_lo) == 0);
127 assert_se(access("it_works", F_OK) >= 0);
128 assert_se(access("failed", F_OK) < 0);
129
130 assert_se(chdir(template_hi) == 0);
131 assert_se(access("it_works2", F_OK) >= 0);
132 assert_se(access("failed", F_OK) < 0);
133
134 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
135 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
136}
137
c6e47247
ZJS
138static void test_execution_order(void) {
139 char template_lo[] = "/tmp/test-exec-util-lo.XXXXXXX";
140 char template_hi[] = "/tmp/test-exec-util-hi.XXXXXXX";
141 const char *dirs[] = {template_hi, template_lo, NULL};
142 const char *name, *name2, *name3, *overridden, *override, *masked, *mask;
143 const char *output, *t;
144 _cleanup_free_ char *contents = NULL;
145
146 assert_se(mkdtemp(template_lo));
147 assert_se(mkdtemp(template_hi));
148
149 output = strjoina(template_hi, "/output");
150
151 log_info("/* %s >>%s */", __func__, output);
152
153 /* write files in "random" order */
154 name2 = strjoina(template_lo, "/90-bar");
155 name = strjoina(template_hi, "/80-foo");
156 name3 = strjoina(template_lo, "/last");
157 overridden = strjoina(template_lo, "/30-override");
158 override = strjoina(template_hi, "/30-override");
159 masked = strjoina(template_lo, "/10-masked");
160 mask = strjoina(template_hi, "/10-masked");
161
162 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
163 assert_se(write_string_file(name, t, WRITE_STRING_FILE_CREATE) == 0);
164
165 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
166 assert_se(write_string_file(name2, t, WRITE_STRING_FILE_CREATE) == 0);
167
168 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
169 assert_se(write_string_file(name3, t, WRITE_STRING_FILE_CREATE) == 0);
170
171 t = strjoina("#!/bin/sh\necho OVERRIDDEN >>", output);
172 assert_se(write_string_file(overridden, t, WRITE_STRING_FILE_CREATE) == 0);
173
174 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
175 assert_se(write_string_file(override, t, WRITE_STRING_FILE_CREATE) == 0);
176
177 t = strjoina("#!/bin/sh\necho MASKED >>", output);
178 assert_se(write_string_file(masked, t, WRITE_STRING_FILE_CREATE) == 0);
179
180 assert_se(symlink("/dev/null", mask) == 0);
181
182 assert_se(chmod(name, 0755) == 0);
183 assert_se(chmod(name2, 0755) == 0);
184 assert_se(chmod(name3, 0755) == 0);
185 assert_se(chmod(overridden, 0755) == 0);
186 assert_se(chmod(override, 0755) == 0);
187 assert_se(chmod(masked, 0755) == 0);
188
3c14dc61
TM
189 if (access(name, X_OK) < 0 && ERRNO_IS_PRIVILEGE(errno))
190 return;
191
4b05f0c9 192 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
c6e47247
ZJS
193
194 assert_se(read_full_file(output, &contents, NULL) >= 0);
195 assert_se(streq(contents, "30-override\n80-foo\n90-bar\nlast\n"));
196
197 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
198 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
199}
200
201static int gather_stdout_one(int fd, void *arg) {
202 char ***s = arg, *t;
203 char buf[128] = {};
204
205 assert_se(s);
206 assert_se(read(fd, buf, sizeof buf) >= 0);
207 safe_close(fd);
208
209 assert_se(t = strndup(buf, sizeof buf));
210 assert_se(strv_push(s, t) >= 0);
211
212 return 0;
213}
214static int gather_stdout_two(int fd, void *arg) {
215 char ***s = arg, **t;
216
217 STRV_FOREACH(t, *s)
218 assert_se(write(fd, *t, strlen(*t)) == (ssize_t) strlen(*t));
219 safe_close(fd);
220
221 return 0;
222}
223static int gather_stdout_three(int fd, void *arg) {
224 char **s = arg;
225 char buf[128] = {};
226
227 assert_se(read(fd, buf, sizeof buf - 1) > 0);
228 safe_close(fd);
229 assert_se(*s = strndup(buf, sizeof buf));
230
231 return 0;
232}
233
4fa3993b 234const gather_stdout_callback_t gather_stdout[] = {
c6e47247
ZJS
235 gather_stdout_one,
236 gather_stdout_two,
237 gather_stdout_three,
238};
239
c6e47247
ZJS
240static void test_stdout_gathering(void) {
241 char template[] = "/tmp/test-exec-util.XXXXXXX";
242 const char *dirs[] = {template, NULL};
243 const char *name, *name2, *name3;
244 int r;
245
246 char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
247 _cleanup_free_ char *output = NULL;
248
249 void* args[] = {&tmp, &tmp, &output};
250
251 assert_se(mkdtemp(template));
252
253 log_info("/* %s */", __func__);
254
255 /* write files */
256 name = strjoina(template, "/10-foo");
257 name2 = strjoina(template, "/20-bar");
258 name3 = strjoina(template, "/30-last");
259
260 assert_se(write_string_file(name,
261 "#!/bin/sh\necho a\necho b\necho c\n",
262 WRITE_STRING_FILE_CREATE) == 0);
263 assert_se(write_string_file(name2,
264 "#!/bin/sh\necho d\n",
265 WRITE_STRING_FILE_CREATE) == 0);
266 assert_se(write_string_file(name3,
267 "#!/bin/sh\nsleep 1",
268 WRITE_STRING_FILE_CREATE) == 0);
269
270 assert_se(chmod(name, 0755) == 0);
271 assert_se(chmod(name2, 0755) == 0);
272 assert_se(chmod(name3, 0755) == 0);
273
3c14dc61
TM
274 if (access(name, X_OK) < 0 && ERRNO_IS_PRIVILEGE(errno))
275 return;
276
4b05f0c9 277 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_stdout, args, NULL, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
c6e47247
ZJS
278 assert_se(r >= 0);
279
280 log_info("got: %s", output);
281
282 assert_se(streq(output, "a\nb\nc\nd\n"));
283}
284
3303d1b2
ZJS
285static void test_environment_gathering(void) {
286 char template[] = "/tmp/test-exec-util.XXXXXXX", **p;
287 const char *dirs[] = {template, NULL};
78ec1bb4 288 const char *name, *name2, *name3, *old;
3303d1b2
ZJS
289 int r;
290
291 char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
292 _cleanup_strv_free_ char **env = NULL;
293
294 void* const args[] = { &tmp, &tmp, &env };
295
296 assert_se(mkdtemp(template));
297
298 log_info("/* %s */", __func__);
299
300 /* write files */
301 name = strjoina(template, "/10-foo");
302 name2 = strjoina(template, "/20-bar");
303 name3 = strjoina(template, "/30-last");
304
305 assert_se(write_string_file(name,
306 "#!/bin/sh\n"
307 "echo A=23\n",
308 WRITE_STRING_FILE_CREATE) == 0);
309 assert_se(write_string_file(name2,
310 "#!/bin/sh\n"
311 "echo A=22:$A\n\n\n", /* substitution from previous generator */
312 WRITE_STRING_FILE_CREATE) == 0);
313 assert_se(write_string_file(name3,
314 "#!/bin/sh\n"
315 "echo A=$A:24\n"
316 "echo B=12\n"
317 "echo C=000\n"
184d1904
ZJS
318 "echo C=001\n" /* variable overwriting */
319 /* various invalid entries */
320 "echo unset A\n"
321 "echo unset A=\n"
322 "echo unset A=B\n"
323 "echo unset \n"
324 "echo A B=C\n"
325 "echo A\n"
326 /* test variable assignment without newline */
327 "echo PATH=$PATH:/no/such/file", /* no newline */
3303d1b2
ZJS
328 WRITE_STRING_FILE_CREATE) == 0);
329
330 assert_se(chmod(name, 0755) == 0);
331 assert_se(chmod(name2, 0755) == 0);
332 assert_se(chmod(name3, 0755) == 0);
333
78ec1bb4 334 /* When booting in containers or without initramfs there might not be
04193fb2 335 * any PATH in the environment and if there is no PATH /bin/sh built-in
78ec1bb4
DJL
336 * PATH may leak and override systemd's DEFAULT_PATH which is not
337 * good. Force our own PATH in environment, to prevent expansion of sh
338 * built-in $PATH */
339 old = getenv("PATH");
340 r = setenv("PATH", "no-sh-built-in-path", 1);
341 assert_se(r >= 0);
342
3c14dc61
TM
343 if (access(name, X_OK) < 0 && ERRNO_IS_PRIVILEGE(errno))
344 return;
345
4b05f0c9 346 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL, NULL, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
78ec1bb4
DJL
347 assert_se(r >= 0);
348
349 STRV_FOREACH(p, env)
350 log_info("got env: \"%s\"", *p);
351
352 assert_se(streq(strv_env_get(env, "A"), "22:23:24"));
353 assert_se(streq(strv_env_get(env, "B"), "12"));
354 assert_se(streq(strv_env_get(env, "C"), "001"));
355 assert_se(streq(strv_env_get(env, "PATH"), "no-sh-built-in-path:/no/such/file"));
356
357 /* now retest with "default" path passed in, as created by
358 * manager_default_environment */
359 env = strv_free(env);
bea1a013
LP
360 env = strv_new("PATH=" DEFAULT_PATH);
361 assert_se(env);
78ec1bb4 362
4b05f0c9 363 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL, env, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS);
3303d1b2
ZJS
364 assert_se(r >= 0);
365
366 STRV_FOREACH(p, env)
367 log_info("got env: \"%s\"", *p);
368
369 assert_se(streq(strv_env_get(env, "A"), "22:23:24"));
370 assert_se(streq(strv_env_get(env, "B"), "12"));
371 assert_se(streq(strv_env_get(env, "C"), "001"));
78ec1bb4
DJL
372 assert_se(streq(strv_env_get(env, "PATH"), DEFAULT_PATH ":/no/such/file"));
373
374 /* reset environ PATH */
04193fb2
ZJS
375 if (old)
376 (void) setenv("PATH", old, 1);
377 else
378 (void) unsetenv("PATH");
3303d1b2
ZJS
379}
380
4b05f0c9
MK
381static void test_error_catching(void) {
382 char template[] = "/tmp/test-exec-util.XXXXXXX";
383 const char *dirs[] = {template, NULL};
384 const char *name, *name2, *name3;
385 int r;
386
387 assert_se(mkdtemp(template));
388
389 log_info("/* %s */", __func__);
390
391 /* write files */
392 name = strjoina(template, "/10-foo");
393 name2 = strjoina(template, "/20-bar");
394 name3 = strjoina(template, "/30-last");
395
396 assert_se(write_string_file(name,
397 "#!/bin/sh\necho a\necho b\necho c\n",
398 WRITE_STRING_FILE_CREATE) == 0);
399 assert_se(write_string_file(name2,
400 "#!/bin/sh\nexit 42\n",
401 WRITE_STRING_FILE_CREATE) == 0);
402 assert_se(write_string_file(name3,
403 "#!/bin/sh\nexit 12",
404 WRITE_STRING_FILE_CREATE) == 0);
405
406 assert_se(chmod(name, 0755) == 0);
407 assert_se(chmod(name2, 0755) == 0);
408 assert_se(chmod(name3, 0755) == 0);
409
3c14dc61
TM
410 if (access(name, X_OK) < 0 && ERRNO_IS_PRIVILEGE(errno))
411 return;
412
4b05f0c9
MK
413 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, NULL, NULL, EXEC_DIR_NONE);
414
415 /* we should exit with the error code of the first script that failed */
416 assert_se(r == 42);
417}
418
b3d59367
AZ
419static void test_exec_command_flags_from_strv(void) {
420 ExecCommandFlags flags = 0;
421 char **valid_strv = STRV_MAKE("no-env-expand", "no-setuid", "ignore-failure");
422 char **invalid_strv = STRV_MAKE("no-env-expand", "no-setuid", "nonexistent-option", "ignore-failure");
423 int r;
424
425 r = exec_command_flags_from_strv(valid_strv, &flags);
426
427 assert_se(r == 0);
428 assert_se(FLAGS_SET(flags, EXEC_COMMAND_NO_ENV_EXPAND));
429 assert_se(FLAGS_SET(flags, EXEC_COMMAND_NO_SETUID));
430 assert_se(FLAGS_SET(flags, EXEC_COMMAND_IGNORE_FAILURE));
431 assert_se(!FLAGS_SET(flags, EXEC_COMMAND_AMBIENT_MAGIC));
432 assert_se(!FLAGS_SET(flags, EXEC_COMMAND_FULLY_PRIVILEGED));
433
434 r = exec_command_flags_from_strv(invalid_strv, &flags);
435
436 assert_se(r == -EINVAL);
437}
438
439static void test_exec_command_flags_to_strv(void) {
440 _cleanup_strv_free_ char **opts = NULL, **empty_opts = NULL, **invalid_opts = NULL;
441 ExecCommandFlags flags = 0;
442 int r;
443
444 flags |= (EXEC_COMMAND_AMBIENT_MAGIC|EXEC_COMMAND_NO_ENV_EXPAND|EXEC_COMMAND_IGNORE_FAILURE);
445
446 r = exec_command_flags_to_strv(flags, &opts);
447
448 assert_se(r == 0);
449 assert_se(strv_equal(opts, STRV_MAKE("ignore-failure", "ambient", "no-env-expand")));
450
451 r = exec_command_flags_to_strv(0, &empty_opts);
452
453 assert_se(r == 0);
454 assert_se(strv_equal(empty_opts, STRV_MAKE_EMPTY));
455
456 flags = _EXEC_COMMAND_FLAGS_INVALID;
457
458 r = exec_command_flags_to_strv(flags, &invalid_opts);
459
460 assert_se(r == -EINVAL);
461}
462
89711996 463int main(int argc, char *argv[]) {
6d7c4033 464 test_setup_logging(LOG_DEBUG);
89711996 465
c6e47247
ZJS
466 test_execute_directory(true);
467 test_execute_directory(false);
468 test_execution_order();
469 test_stdout_gathering();
3303d1b2 470 test_environment_gathering();
4b05f0c9 471 test_error_catching();
b3d59367
AZ
472 test_exec_command_flags_from_strv();
473 test_exec_command_flags_to_strv();
89711996
ZJS
474
475 return 0;
476}