]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-exec-util.c
Merge pull request #8765 from poettering/test-fixes
[thirdparty/systemd.git] / src / test / test-exec-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 Copyright 2013 Thomas H.P. Andersen
7 ***/
8
9 #include <errno.h>
10 #include <string.h>
11 #include <sys/stat.h>
12 #include <sys/wait.h>
13 #include <unistd.h>
14
15 #include "alloc-util.h"
16 #include "copy.h"
17 #include "def.h"
18 #include "env-util.h"
19 #include "exec-util.h"
20 #include "fd-util.h"
21 #include "fileio.h"
22 #include "fs-util.h"
23 #include "log.h"
24 #include "macro.h"
25 #include "rm-rf.h"
26 #include "string-util.h"
27 #include "strv.h"
28
29 static int here = 0, here2 = 0, here3 = 0;
30 void *ignore_stdout_args[] = {&here, &here2, &here3};
31
32 /* noop handlers, just check that arguments are passed correctly */
33 static int ignore_stdout_func(int fd, void *arg) {
34 assert(fd >= 0);
35 assert(arg == &here);
36 safe_close(fd);
37
38 return 0;
39 }
40 static int ignore_stdout_func2(int fd, void *arg) {
41 assert(fd >= 0);
42 assert(arg == &here2);
43 safe_close(fd);
44
45 return 0;
46 }
47 static int ignore_stdout_func3(int fd, void *arg) {
48 assert(fd >= 0);
49 assert(arg == &here3);
50 safe_close(fd);
51
52 return 0;
53 }
54
55 static const gather_stdout_callback_t ignore_stdout[] = {
56 ignore_stdout_func,
57 ignore_stdout_func2,
58 ignore_stdout_func3,
59 };
60
61 static void test_execute_directory(bool gather_stdout) {
62 char template_lo[] = "/tmp/test-exec-util.lo.XXXXXXX";
63 char template_hi[] = "/tmp/test-exec-util.hi.XXXXXXX";
64 const char * dirs[] = {template_hi, template_lo, NULL};
65 const char *name, *name2, *name3,
66 *overridden, *override,
67 *masked, *mask,
68 *masked2, *mask2, /* the mask is non-executable */
69 *masked2e, *mask2e; /* the mask is executable */
70
71 log_info("/* %s (%s) */", __func__, gather_stdout ? "gathering stdout" : "asynchronous");
72
73 assert_se(mkdtemp(template_lo));
74 assert_se(mkdtemp(template_hi));
75
76 name = strjoina(template_lo, "/script");
77 name2 = strjoina(template_hi, "/script2");
78 name3 = strjoina(template_lo, "/useless");
79 overridden = strjoina(template_lo, "/overridden");
80 override = strjoina(template_hi, "/overridden");
81 masked = strjoina(template_lo, "/masked");
82 mask = strjoina(template_hi, "/masked");
83 masked2 = strjoina(template_lo, "/masked2");
84 mask2 = strjoina(template_hi, "/masked2");
85 masked2e = strjoina(template_lo, "/masked2e");
86 mask2e = strjoina(template_hi, "/masked2e");
87
88 assert_se(write_string_file(name,
89 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works",
90 WRITE_STRING_FILE_CREATE) == 0);
91 assert_se(write_string_file(name2,
92 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2",
93 WRITE_STRING_FILE_CREATE) == 0);
94 assert_se(write_string_file(overridden,
95 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
96 WRITE_STRING_FILE_CREATE) == 0);
97 assert_se(write_string_file(override,
98 "#!/bin/sh\necho 'Executing '$0",
99 WRITE_STRING_FILE_CREATE) == 0);
100 assert_se(write_string_file(masked,
101 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
102 WRITE_STRING_FILE_CREATE) == 0);
103 assert_se(write_string_file(masked2,
104 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
105 WRITE_STRING_FILE_CREATE) == 0);
106 assert_se(write_string_file(masked2e,
107 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
108 WRITE_STRING_FILE_CREATE) == 0);
109 assert_se(symlink("/dev/null", mask) == 0);
110 assert_se(touch(mask2) == 0);
111 assert_se(touch(mask2e) == 0);
112 assert_se(touch(name3) >= 0);
113
114 assert_se(chmod(name, 0755) == 0);
115 assert_se(chmod(name2, 0755) == 0);
116 assert_se(chmod(overridden, 0755) == 0);
117 assert_se(chmod(override, 0755) == 0);
118 assert_se(chmod(masked, 0755) == 0);
119 assert_se(chmod(masked2, 0755) == 0);
120 assert_se(chmod(masked2e, 0755) == 0);
121 assert_se(chmod(mask2e, 0755) == 0);
122
123 if (gather_stdout)
124 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL);
125 else
126 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, NULL);
127
128 assert_se(chdir(template_lo) == 0);
129 assert_se(access("it_works", F_OK) >= 0);
130 assert_se(access("failed", F_OK) < 0);
131
132 assert_se(chdir(template_hi) == 0);
133 assert_se(access("it_works2", F_OK) >= 0);
134 assert_se(access("failed", F_OK) < 0);
135
136 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
137 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
138 }
139
140 static void test_execution_order(void) {
141 char template_lo[] = "/tmp/test-exec-util-lo.XXXXXXX";
142 char template_hi[] = "/tmp/test-exec-util-hi.XXXXXXX";
143 const char *dirs[] = {template_hi, template_lo, NULL};
144 const char *name, *name2, *name3, *overridden, *override, *masked, *mask;
145 const char *output, *t;
146 _cleanup_free_ char *contents = NULL;
147
148 assert_se(mkdtemp(template_lo));
149 assert_se(mkdtemp(template_hi));
150
151 output = strjoina(template_hi, "/output");
152
153 log_info("/* %s >>%s */", __func__, output);
154
155 /* write files in "random" order */
156 name2 = strjoina(template_lo, "/90-bar");
157 name = strjoina(template_hi, "/80-foo");
158 name3 = strjoina(template_lo, "/last");
159 overridden = strjoina(template_lo, "/30-override");
160 override = strjoina(template_hi, "/30-override");
161 masked = strjoina(template_lo, "/10-masked");
162 mask = strjoina(template_hi, "/10-masked");
163
164 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
165 assert_se(write_string_file(name, t, WRITE_STRING_FILE_CREATE) == 0);
166
167 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
168 assert_se(write_string_file(name2, t, WRITE_STRING_FILE_CREATE) == 0);
169
170 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
171 assert_se(write_string_file(name3, t, WRITE_STRING_FILE_CREATE) == 0);
172
173 t = strjoina("#!/bin/sh\necho OVERRIDDEN >>", output);
174 assert_se(write_string_file(overridden, t, WRITE_STRING_FILE_CREATE) == 0);
175
176 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
177 assert_se(write_string_file(override, t, WRITE_STRING_FILE_CREATE) == 0);
178
179 t = strjoina("#!/bin/sh\necho MASKED >>", output);
180 assert_se(write_string_file(masked, t, WRITE_STRING_FILE_CREATE) == 0);
181
182 assert_se(symlink("/dev/null", mask) == 0);
183
184 assert_se(chmod(name, 0755) == 0);
185 assert_se(chmod(name2, 0755) == 0);
186 assert_se(chmod(name3, 0755) == 0);
187 assert_se(chmod(overridden, 0755) == 0);
188 assert_se(chmod(override, 0755) == 0);
189 assert_se(chmod(masked, 0755) == 0);
190
191 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL);
192
193 assert_se(read_full_file(output, &contents, NULL) >= 0);
194 assert_se(streq(contents, "30-override\n80-foo\n90-bar\nlast\n"));
195
196 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
197 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
198 }
199
200 static int gather_stdout_one(int fd, void *arg) {
201 char ***s = arg, *t;
202 char buf[128] = {};
203
204 assert_se(s);
205 assert_se(read(fd, buf, sizeof buf) >= 0);
206 safe_close(fd);
207
208 assert_se(t = strndup(buf, sizeof buf));
209 assert_se(strv_push(s, t) >= 0);
210
211 return 0;
212 }
213 static int gather_stdout_two(int fd, void *arg) {
214 char ***s = arg, **t;
215
216 STRV_FOREACH(t, *s)
217 assert_se(write(fd, *t, strlen(*t)) == (ssize_t) strlen(*t));
218 safe_close(fd);
219
220 return 0;
221 }
222 static int gather_stdout_three(int fd, void *arg) {
223 char **s = arg;
224 char buf[128] = {};
225
226 assert_se(read(fd, buf, sizeof buf - 1) > 0);
227 safe_close(fd);
228 assert_se(*s = strndup(buf, sizeof buf));
229
230 return 0;
231 }
232
233 const gather_stdout_callback_t gather_stdout[] = {
234 gather_stdout_one,
235 gather_stdout_two,
236 gather_stdout_three,
237 };
238
239 static void test_stdout_gathering(void) {
240 char template[] = "/tmp/test-exec-util.XXXXXXX";
241 const char *dirs[] = {template, NULL};
242 const char *name, *name2, *name3;
243 int r;
244
245 char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
246 _cleanup_free_ char *output = NULL;
247
248 void* args[] = {&tmp, &tmp, &output};
249
250 assert_se(mkdtemp(template));
251
252 log_info("/* %s */", __func__);
253
254 /* write files */
255 name = strjoina(template, "/10-foo");
256 name2 = strjoina(template, "/20-bar");
257 name3 = strjoina(template, "/30-last");
258
259 assert_se(write_string_file(name,
260 "#!/bin/sh\necho a\necho b\necho c\n",
261 WRITE_STRING_FILE_CREATE) == 0);
262 assert_se(write_string_file(name2,
263 "#!/bin/sh\necho d\n",
264 WRITE_STRING_FILE_CREATE) == 0);
265 assert_se(write_string_file(name3,
266 "#!/bin/sh\nsleep 1",
267 WRITE_STRING_FILE_CREATE) == 0);
268
269 assert_se(chmod(name, 0755) == 0);
270 assert_se(chmod(name2, 0755) == 0);
271 assert_se(chmod(name3, 0755) == 0);
272
273 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_stdout, args, NULL);
274 assert_se(r >= 0);
275
276 log_info("got: %s", output);
277
278 assert_se(streq(output, "a\nb\nc\nd\n"));
279 }
280
281 static void test_environment_gathering(void) {
282 char template[] = "/tmp/test-exec-util.XXXXXXX", **p;
283 const char *dirs[] = {template, NULL};
284 const char *name, *name2, *name3;
285 int r;
286
287 char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
288 _cleanup_strv_free_ char **env = NULL;
289
290 void* const args[] = { &tmp, &tmp, &env };
291
292 assert_se(mkdtemp(template));
293
294 log_info("/* %s */", __func__);
295
296 /* write files */
297 name = strjoina(template, "/10-foo");
298 name2 = strjoina(template, "/20-bar");
299 name3 = strjoina(template, "/30-last");
300
301 assert_se(write_string_file(name,
302 "#!/bin/sh\n"
303 "echo A=23\n",
304 WRITE_STRING_FILE_CREATE) == 0);
305 assert_se(write_string_file(name2,
306 "#!/bin/sh\n"
307 "echo A=22:$A\n\n\n", /* substitution from previous generator */
308 WRITE_STRING_FILE_CREATE) == 0);
309 assert_se(write_string_file(name3,
310 "#!/bin/sh\n"
311 "echo A=$A:24\n"
312 "echo B=12\n"
313 "echo C=000\n"
314 "echo C=001\n" /* variable overwriting */
315 /* various invalid entries */
316 "echo unset A\n"
317 "echo unset A=\n"
318 "echo unset A=B\n"
319 "echo unset \n"
320 "echo A B=C\n"
321 "echo A\n"
322 /* test variable assignment without newline */
323 "echo PATH=$PATH:/no/such/file", /* no newline */
324 WRITE_STRING_FILE_CREATE) == 0);
325
326 assert_se(chmod(name, 0755) == 0);
327 assert_se(chmod(name2, 0755) == 0);
328 assert_se(chmod(name3, 0755) == 0);
329
330 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL);
331 assert_se(r >= 0);
332
333 STRV_FOREACH(p, env)
334 log_info("got env: \"%s\"", *p);
335
336 assert_se(streq(strv_env_get(env, "A"), "22:23:24"));
337 assert_se(streq(strv_env_get(env, "B"), "12"));
338 assert_se(streq(strv_env_get(env, "C"), "001"));
339 assert_se(endswith(strv_env_get(env, "PATH"), ":/no/such/file"));
340 }
341
342 int main(int argc, char *argv[]) {
343 log_set_max_level(LOG_DEBUG);
344 log_parse_environment();
345 log_open();
346
347 test_execute_directory(true);
348 test_execute_directory(false);
349 test_execution_order();
350 test_stdout_gathering();
351 test_environment_gathering();
352
353 return 0;
354 }