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