1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2013 Thomas H.P. Andersen
12 #include "alloc-util.h"
16 #include "exec-util.h"
23 #include "string-util.h"
26 static int here = 0, here2 = 0, here3 = 0;
27 void *ignore_stdout_args[] = {&here, &here2, &here3};
29 /* noop handlers, just check that arguments are passed correctly */
30 static int ignore_stdout_func(int fd, void *arg) {
37 static int ignore_stdout_func2(int fd, void *arg) {
39 assert(arg == &here2);
44 static int ignore_stdout_func3(int fd, void *arg) {
46 assert(arg == &here3);
52 static const gather_stdout_callback_t ignore_stdout[] = {
58 static void test_execute_directory(bool gather_stdout) {
59 char template_lo[] = "/tmp/test-exec-util.lo.XXXXXXX";
60 char template_hi[] = "/tmp/test-exec-util.hi.XXXXXXX";
61 const char * dirs[] = {template_hi, template_lo, NULL};
62 const char *name, *name2, *name3,
63 *overridden, *override,
65 *masked2, *mask2, /* the mask is non-executable */
66 *masked2e, *mask2e; /* the mask is executable */
68 log_info("/* %s (%s) */", __func__, gather_stdout ? "gathering stdout" : "asynchronous");
70 assert_se(mkdtemp(template_lo));
71 assert_se(mkdtemp(template_hi));
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");
80 masked2 = strjoina(template_lo, "/masked2");
81 mask2 = strjoina(template_hi, "/masked2");
82 masked2e = strjoina(template_lo, "/masked2e");
83 mask2e = strjoina(template_hi, "/masked2e");
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);
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);
106 assert_se(symlink("/dev/null", mask) == 0);
107 assert_se(touch(mask2) == 0);
108 assert_se(touch(mask2e) == 0);
109 assert_se(touch(name3) >= 0);
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);
116 assert_se(chmod(masked2, 0755) == 0);
117 assert_se(chmod(masked2e, 0755) == 0);
118 assert_se(chmod(mask2e, 0755) == 0);
121 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL);
123 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, NULL);
125 assert_se(chdir(template_lo) == 0);
126 assert_se(access("it_works", F_OK) >= 0);
127 assert_se(access("failed", F_OK) < 0);
129 assert_se(chdir(template_hi) == 0);
130 assert_se(access("it_works2", F_OK) >= 0);
131 assert_se(access("failed", F_OK) < 0);
133 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
134 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
137 static 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;
145 assert_se(mkdtemp(template_lo));
146 assert_se(mkdtemp(template_hi));
148 output = strjoina(template_hi, "/output");
150 log_info("/* %s >>%s */", __func__, output);
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");
161 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
162 assert_se(write_string_file(name, t, WRITE_STRING_FILE_CREATE) == 0);
164 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
165 assert_se(write_string_file(name2, t, WRITE_STRING_FILE_CREATE) == 0);
167 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
168 assert_se(write_string_file(name3, t, WRITE_STRING_FILE_CREATE) == 0);
170 t = strjoina("#!/bin/sh\necho OVERRIDDEN >>", output);
171 assert_se(write_string_file(overridden, t, WRITE_STRING_FILE_CREATE) == 0);
173 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
174 assert_se(write_string_file(override, t, WRITE_STRING_FILE_CREATE) == 0);
176 t = strjoina("#!/bin/sh\necho MASKED >>", output);
177 assert_se(write_string_file(masked, t, WRITE_STRING_FILE_CREATE) == 0);
179 assert_se(symlink("/dev/null", mask) == 0);
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);
188 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL);
190 assert_se(read_full_file(output, &contents, NULL) >= 0);
191 assert_se(streq(contents, "30-override\n80-foo\n90-bar\nlast\n"));
193 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
194 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
197 static int gather_stdout_one(int fd, void *arg) {
202 assert_se(read(fd, buf, sizeof buf) >= 0);
205 assert_se(t = strndup(buf, sizeof buf));
206 assert_se(strv_push(s, t) >= 0);
210 static int gather_stdout_two(int fd, void *arg) {
211 char ***s = arg, **t;
214 assert_se(write(fd, *t, strlen(*t)) == (ssize_t) strlen(*t));
219 static int gather_stdout_three(int fd, void *arg) {
223 assert_se(read(fd, buf, sizeof buf - 1) > 0);
225 assert_se(*s = strndup(buf, sizeof buf));
230 const gather_stdout_callback_t gather_stdout[] = {
236 static 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;
242 char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
243 _cleanup_free_ char *output = NULL;
245 void* args[] = {&tmp, &tmp, &output};
247 assert_se(mkdtemp(template));
249 log_info("/* %s */", __func__);
252 name = strjoina(template, "/10-foo");
253 name2 = strjoina(template, "/20-bar");
254 name3 = strjoina(template, "/30-last");
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);
266 assert_se(chmod(name, 0755) == 0);
267 assert_se(chmod(name2, 0755) == 0);
268 assert_se(chmod(name3, 0755) == 0);
270 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_stdout, args, NULL);
273 log_info("got: %s", output);
275 assert_se(streq(output, "a\nb\nc\nd\n"));
278 #if 0 /// UNNEEDED by elogind
279 static void test_environment_gathering(void) {
280 char template[] = "/tmp/test-exec-util.XXXXXXX", **p;
281 const char *dirs[] = {template, NULL};
282 const char *name, *name2, *name3;
285 char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
286 _cleanup_strv_free_ char **env = NULL;
288 void* const args[] = { &tmp, &tmp, &env };
290 assert_se(mkdtemp(template));
292 log_info("/* %s */", __func__);
295 name = strjoina(template, "/10-foo");
296 name2 = strjoina(template, "/20-bar");
297 name3 = strjoina(template, "/30-last");
299 assert_se(write_string_file(name,
302 WRITE_STRING_FILE_CREATE) == 0);
303 assert_se(write_string_file(name2,
305 "echo A=22:$A\n\n\n", /* substitution from previous generator */
306 WRITE_STRING_FILE_CREATE) == 0);
307 assert_se(write_string_file(name3,
312 "echo C=001\n" /* variable overwriting */
313 /* various invalid entries */
320 /* test variable assignment without newline */
321 "echo PATH=$PATH:/no/such/file", /* no newline */
322 WRITE_STRING_FILE_CREATE) == 0);
324 assert_se(chmod(name, 0755) == 0);
325 assert_se(chmod(name2, 0755) == 0);
326 assert_se(chmod(name3, 0755) == 0);
328 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL);
332 log_info("got env: \"%s\"", *p);
334 assert_se(streq(strv_env_get(env, "A"), "22:23:24"));
335 assert_se(streq(strv_env_get(env, "B"), "12"));
336 assert_se(streq(strv_env_get(env, "C"), "001"));
337 assert_se(endswith(strv_env_get(env, "PATH"), ":/no/such/file"));
341 int main(int argc, char *argv[]) {
342 log_set_max_level(LOG_DEBUG);
343 log_parse_environment();
346 test_execute_directory(true);
347 test_execute_directory(false);
348 test_execution_order();
349 test_stdout_gathering();
350 #if 0 /// UNNEEDED by elogind
351 test_environment_gathering();