1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
4 Copyright 2013 Thomas H.P. Andersen
13 #include "alloc-util.h"
17 #include "exec-util.h"
24 #include "string-util.h"
27 static int here = 0, here2 = 0, here3 = 0;
28 void *ignore_stdout_args[] = {&here, &here2, &here3};
30 /* noop handlers, just check that arguments are passed correctly */
31 static int ignore_stdout_func(int fd, void *arg) {
38 static int ignore_stdout_func2(int fd, void *arg) {
40 assert(arg == &here2);
45 static int ignore_stdout_func3(int fd, void *arg) {
47 assert(arg == &here3);
53 static const gather_stdout_callback_t ignore_stdout[] = {
59 static void test_execute_directory(bool gather_stdout) {
60 char template_lo[] = "/tmp/test-exec-util.lo.XXXXXXX";
61 char template_hi[] = "/tmp/test-exec-util.hi.XXXXXXX";
62 const char * dirs[] = {template_hi, template_lo, NULL};
63 const char *name, *name2, *name3,
64 *overridden, *override,
66 *masked2, *mask2, /* the mask is non-executable */
67 *masked2e, *mask2e; /* the mask is executable */
69 log_info("/* %s (%s) */", __func__, gather_stdout ? "gathering stdout" : "asynchronous");
71 assert_se(mkdtemp(template_lo));
72 assert_se(mkdtemp(template_hi));
74 name = strjoina(template_lo, "/script");
75 name2 = strjoina(template_hi, "/script2");
76 name3 = strjoina(template_lo, "/useless");
77 overridden = strjoina(template_lo, "/overridden");
78 override = strjoina(template_hi, "/overridden");
79 masked = strjoina(template_lo, "/masked");
80 mask = strjoina(template_hi, "/masked");
81 masked2 = strjoina(template_lo, "/masked2");
82 mask2 = strjoina(template_hi, "/masked2");
83 masked2e = strjoina(template_lo, "/masked2e");
84 mask2e = strjoina(template_hi, "/masked2e");
86 assert_se(write_string_file(name,
87 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works",
88 WRITE_STRING_FILE_CREATE) == 0);
89 assert_se(write_string_file(name2,
90 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2",
91 WRITE_STRING_FILE_CREATE) == 0);
92 assert_se(write_string_file(overridden,
93 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
94 WRITE_STRING_FILE_CREATE) == 0);
95 assert_se(write_string_file(override,
96 "#!/bin/sh\necho 'Executing '$0",
97 WRITE_STRING_FILE_CREATE) == 0);
98 assert_se(write_string_file(masked,
99 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
100 WRITE_STRING_FILE_CREATE) == 0);
101 assert_se(write_string_file(masked2,
102 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
103 WRITE_STRING_FILE_CREATE) == 0);
104 assert_se(write_string_file(masked2e,
105 "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed",
106 WRITE_STRING_FILE_CREATE) == 0);
107 assert_se(symlink("/dev/null", mask) == 0);
108 assert_se(touch(mask2) == 0);
109 assert_se(touch(mask2e) == 0);
110 assert_se(touch(name3) >= 0);
112 assert_se(chmod(name, 0755) == 0);
113 assert_se(chmod(name2, 0755) == 0);
114 assert_se(chmod(overridden, 0755) == 0);
115 assert_se(chmod(override, 0755) == 0);
116 assert_se(chmod(masked, 0755) == 0);
117 assert_se(chmod(masked2, 0755) == 0);
118 assert_se(chmod(masked2e, 0755) == 0);
119 assert_se(chmod(mask2e, 0755) == 0);
122 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL);
124 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, NULL);
126 assert_se(chdir(template_lo) == 0);
127 assert_se(access("it_works", F_OK) >= 0);
128 assert_se(access("failed", F_OK) < 0);
130 assert_se(chdir(template_hi) == 0);
131 assert_se(access("it_works2", F_OK) >= 0);
132 assert_se(access("failed", F_OK) < 0);
134 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
135 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
138 static 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;
146 assert_se(mkdtemp(template_lo));
147 assert_se(mkdtemp(template_hi));
149 output = strjoina(template_hi, "/output");
151 log_info("/* %s >>%s */", __func__, output);
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");
162 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
163 assert_se(write_string_file(name, t, WRITE_STRING_FILE_CREATE) == 0);
165 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
166 assert_se(write_string_file(name2, t, WRITE_STRING_FILE_CREATE) == 0);
168 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
169 assert_se(write_string_file(name3, t, WRITE_STRING_FILE_CREATE) == 0);
171 t = strjoina("#!/bin/sh\necho OVERRIDDEN >>", output);
172 assert_se(write_string_file(overridden, t, WRITE_STRING_FILE_CREATE) == 0);
174 t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
175 assert_se(write_string_file(override, t, WRITE_STRING_FILE_CREATE) == 0);
177 t = strjoina("#!/bin/sh\necho MASKED >>", output);
178 assert_se(write_string_file(masked, t, WRITE_STRING_FILE_CREATE) == 0);
180 assert_se(symlink("/dev/null", mask) == 0);
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);
189 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL);
191 assert_se(read_full_file(output, &contents, NULL) >= 0);
192 assert_se(streq(contents, "30-override\n80-foo\n90-bar\nlast\n"));
194 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
195 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
198 static int gather_stdout_one(int fd, void *arg) {
203 assert_se(read(fd, buf, sizeof buf) >= 0);
206 assert_se(t = strndup(buf, sizeof buf));
207 assert_se(strv_push(s, t) >= 0);
211 static int gather_stdout_two(int fd, void *arg) {
212 char ***s = arg, **t;
215 assert_se(write(fd, *t, strlen(*t)) == (ssize_t) strlen(*t));
220 static int gather_stdout_three(int fd, void *arg) {
224 assert_se(read(fd, buf, sizeof buf - 1) > 0);
226 assert_se(*s = strndup(buf, sizeof buf));
231 const gather_stdout_callback_t gather_stdout[] = {
237 static void test_stdout_gathering(void) {
238 char template[] = "/tmp/test-exec-util.XXXXXXX";
239 const char *dirs[] = {template, NULL};
240 const char *name, *name2, *name3;
243 char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
244 _cleanup_free_ char *output = NULL;
246 void* args[] = {&tmp, &tmp, &output};
248 assert_se(mkdtemp(template));
250 log_info("/* %s */", __func__);
253 name = strjoina(template, "/10-foo");
254 name2 = strjoina(template, "/20-bar");
255 name3 = strjoina(template, "/30-last");
257 assert_se(write_string_file(name,
258 "#!/bin/sh\necho a\necho b\necho c\n",
259 WRITE_STRING_FILE_CREATE) == 0);
260 assert_se(write_string_file(name2,
261 "#!/bin/sh\necho d\n",
262 WRITE_STRING_FILE_CREATE) == 0);
263 assert_se(write_string_file(name3,
264 "#!/bin/sh\nsleep 1",
265 WRITE_STRING_FILE_CREATE) == 0);
267 assert_se(chmod(name, 0755) == 0);
268 assert_se(chmod(name2, 0755) == 0);
269 assert_se(chmod(name3, 0755) == 0);
271 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_stdout, args, NULL);
274 log_info("got: %s", output);
276 assert_se(streq(output, "a\nb\nc\nd\n"));
279 #if 0 /// UNNEEDED by elogind
280 static void test_environment_gathering(void) {
281 char template[] = "/tmp/test-exec-util.XXXXXXX", **p;
282 const char *dirs[] = {template, NULL};
283 const char *name, *name2, *name3;
286 char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
287 _cleanup_strv_free_ char **env = NULL;
289 void* const args[] = { &tmp, &tmp, &env };
291 assert_se(mkdtemp(template));
293 log_info("/* %s */", __func__);
296 name = strjoina(template, "/10-foo");
297 name2 = strjoina(template, "/20-bar");
298 name3 = strjoina(template, "/30-last");
300 assert_se(write_string_file(name,
303 WRITE_STRING_FILE_CREATE) == 0);
304 assert_se(write_string_file(name2,
306 "echo A=22:$A\n\n\n", /* substitution from previous generator */
307 WRITE_STRING_FILE_CREATE) == 0);
308 assert_se(write_string_file(name3,
313 "echo C=001\n" /* variable overwriting */
314 /* various invalid entries */
321 /* test variable assignment without newline */
322 "echo PATH=$PATH:/no/such/file", /* no newline */
323 WRITE_STRING_FILE_CREATE) == 0);
325 assert_se(chmod(name, 0755) == 0);
326 assert_se(chmod(name2, 0755) == 0);
327 assert_se(chmod(name3, 0755) == 0);
329 r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL);
333 log_info("got env: \"%s\"", *p);
335 assert_se(streq(strv_env_get(env, "A"), "22:23:24"));
336 assert_se(streq(strv_env_get(env, "B"), "12"));
337 assert_se(streq(strv_env_get(env, "C"), "001"));
338 assert_se(endswith(strv_env_get(env, "PATH"), ":/no/such/file"));
342 int main(int argc, char *argv[]) {
343 log_set_max_level(LOG_DEBUG);
344 log_parse_environment();
347 test_execute_directory(true);
348 test_execute_directory(false);
349 test_execution_order();
350 test_stdout_gathering();
351 #if 0 /// UNNEEDED by elogind
352 test_environment_gathering();