]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
89711996 ZJS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
6 | Copyright 2013 Thomas H.P. Andersen | |
89711996 ZJS |
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 | ||
c6e47247 ZJS |
15 | #include "alloc-util.h" |
16 | #include "copy.h" | |
89711996 | 17 | #include "def.h" |
3303d1b2 | 18 | #include "env-util.h" |
89711996 | 19 | #include "exec-util.h" |
c6e47247 | 20 | #include "fd-util.h" |
89711996 ZJS |
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" | |
c6e47247 | 27 | #include "strv.h" |
89711996 | 28 | |
c6e47247 ZJS |
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) { | |
f66137fb ZJS |
62 | char template_lo[] = "/tmp/test-exec-util.lo.XXXXXXX"; |
63 | char template_hi[] = "/tmp/test-exec-util.hi.XXXXXXX"; | |
89711996 | 64 | const char * dirs[] = {template_hi, template_lo, NULL}; |
f66137fb ZJS |
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 */ | |
89711996 | 70 | |
c6e47247 ZJS |
71 | log_info("/* %s (%s) */", __func__, gather_stdout ? "gathering stdout" : "asynchronous"); |
72 | ||
89711996 ZJS |
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"); | |
f66137fb ZJS |
83 | masked2 = strjoina(template_lo, "/masked2"); |
84 | mask2 = strjoina(template_hi, "/masked2"); | |
85 | masked2e = strjoina(template_lo, "/masked2e"); | |
86 | mask2e = strjoina(template_hi, "/masked2e"); | |
89711996 | 87 | |
c6e47247 ZJS |
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); | |
f66137fb ZJS |
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); | |
89711996 | 109 | assert_se(symlink("/dev/null", mask) == 0); |
f66137fb ZJS |
110 | assert_se(touch(mask2) == 0); |
111 | assert_se(touch(mask2e) == 0); | |
c6e47247 ZJS |
112 | assert_se(touch(name3) >= 0); |
113 | ||
89711996 ZJS |
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); | |
f66137fb ZJS |
119 | assert_se(chmod(masked2, 0755) == 0); |
120 | assert_se(chmod(masked2e, 0755) == 0); | |
121 | assert_se(chmod(mask2e, 0755) == 0); | |
89711996 | 122 | |
c6e47247 ZJS |
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); | |
89711996 ZJS |
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 | ||
c6e47247 ZJS |
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 | ||
4fa3993b | 233 | const gather_stdout_callback_t gather_stdout[] = { |
c6e47247 ZJS |
234 | gather_stdout_one, |
235 | gather_stdout_two, | |
236 | gather_stdout_three, | |
237 | }; | |
238 | ||
239 | ||
240 | static 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 | ||
274 | r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_stdout, args, NULL); | |
275 | assert_se(r >= 0); | |
276 | ||
277 | log_info("got: %s", output); | |
278 | ||
279 | assert_se(streq(output, "a\nb\nc\nd\n")); | |
280 | } | |
281 | ||
3303d1b2 ZJS |
282 | static void test_environment_gathering(void) { |
283 | char template[] = "/tmp/test-exec-util.XXXXXXX", **p; | |
284 | const char *dirs[] = {template, NULL}; | |
285 | const char *name, *name2, *name3; | |
286 | int r; | |
287 | ||
288 | char **tmp = NULL; /* this is only used in the forked process, no cleanup here */ | |
289 | _cleanup_strv_free_ char **env = NULL; | |
290 | ||
291 | void* const args[] = { &tmp, &tmp, &env }; | |
292 | ||
293 | assert_se(mkdtemp(template)); | |
294 | ||
295 | log_info("/* %s */", __func__); | |
296 | ||
297 | /* write files */ | |
298 | name = strjoina(template, "/10-foo"); | |
299 | name2 = strjoina(template, "/20-bar"); | |
300 | name3 = strjoina(template, "/30-last"); | |
301 | ||
302 | assert_se(write_string_file(name, | |
303 | "#!/bin/sh\n" | |
304 | "echo A=23\n", | |
305 | WRITE_STRING_FILE_CREATE) == 0); | |
306 | assert_se(write_string_file(name2, | |
307 | "#!/bin/sh\n" | |
308 | "echo A=22:$A\n\n\n", /* substitution from previous generator */ | |
309 | WRITE_STRING_FILE_CREATE) == 0); | |
310 | assert_se(write_string_file(name3, | |
311 | "#!/bin/sh\n" | |
312 | "echo A=$A:24\n" | |
313 | "echo B=12\n" | |
314 | "echo C=000\n" | |
184d1904 ZJS |
315 | "echo C=001\n" /* variable overwriting */ |
316 | /* various invalid entries */ | |
317 | "echo unset A\n" | |
318 | "echo unset A=\n" | |
319 | "echo unset A=B\n" | |
320 | "echo unset \n" | |
321 | "echo A B=C\n" | |
322 | "echo A\n" | |
323 | /* test variable assignment without newline */ | |
324 | "echo PATH=$PATH:/no/such/file", /* no newline */ | |
3303d1b2 ZJS |
325 | WRITE_STRING_FILE_CREATE) == 0); |
326 | ||
327 | assert_se(chmod(name, 0755) == 0); | |
328 | assert_se(chmod(name2, 0755) == 0); | |
329 | assert_se(chmod(name3, 0755) == 0); | |
330 | ||
331 | r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL); | |
332 | assert_se(r >= 0); | |
333 | ||
334 | STRV_FOREACH(p, env) | |
335 | log_info("got env: \"%s\"", *p); | |
336 | ||
337 | assert_se(streq(strv_env_get(env, "A"), "22:23:24")); | |
338 | assert_se(streq(strv_env_get(env, "B"), "12")); | |
339 | assert_se(streq(strv_env_get(env, "C"), "001")); | |
340 | assert_se(endswith(strv_env_get(env, "PATH"), ":/no/such/file")); | |
341 | } | |
342 | ||
89711996 | 343 | int main(int argc, char *argv[]) { |
c6e47247 | 344 | log_set_max_level(LOG_DEBUG); |
89711996 ZJS |
345 | log_parse_environment(); |
346 | log_open(); | |
347 | ||
c6e47247 ZJS |
348 | test_execute_directory(true); |
349 | test_execute_directory(false); | |
350 | test_execution_order(); | |
351 | test_stdout_gathering(); | |
3303d1b2 | 352 | test_environment_gathering(); |
89711996 ZJS |
353 | |
354 | return 0; | |
355 | } |