chiark / gitweb /
d0e20ee6d78994b1171cdee206dbebeef37270f7
[elogind.git] / src / test / test-exec-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright © 2013 Thomas H.P. Andersen
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
12 #include "alloc-util.h"
13 #include "copy.h"
14 #include "def.h"
15 #include "env-util.h"
16 #include "exec-util.h"
17 #include "fd-util.h"
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"
24 #include "strv.h"
25
26 static int here = 0, here2 = 0, here3 = 0;
27 void *ignore_stdout_args[] = {&here, &here2, &here3};
28
29 /* noop handlers, just check that arguments are passed correctly */
30 static 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 }
37 static 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 }
44 static 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
52 static const gather_stdout_callback_t ignore_stdout[] = {
53         ignore_stdout_func,
54         ignore_stdout_func2,
55         ignore_stdout_func3,
56 };
57
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,
64                 *masked, *mask,
65                 *masked2, *mask2,   /* the mask is non-executable */
66                 *masked2e, *mask2e; /* the mask is executable */
67
68         log_info("/* %s (%s) */", __func__, gather_stdout ? "gathering stdout" : "asynchronous");
69
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");
80         masked2 = strjoina(template_lo, "/masked2");
81         mask2 = strjoina(template_hi, "/masked2");
82         masked2e = strjoina(template_lo, "/masked2e");
83         mask2e = strjoina(template_hi, "/masked2e");
84
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);
110
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);
119
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);
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
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;
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
197 static 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 }
210 static 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 }
219 static 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
230 const gather_stdout_callback_t gather_stdout[] = {
231         gather_stdout_one,
232         gather_stdout_two,
233         gather_stdout_three,
234 };
235
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;
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
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;
283         int r;
284
285         char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
286         _cleanup_strv_free_ char **env = NULL;
287
288         void* const args[] = { &tmp, &tmp, &env };
289
290         assert_se(mkdtemp(template));
291
292         log_info("/* %s */", __func__);
293
294         /* write files */
295         name = strjoina(template, "/10-foo");
296         name2 = strjoina(template, "/20-bar");
297         name3 = strjoina(template, "/30-last");
298
299         assert_se(write_string_file(name,
300                                     "#!/bin/sh\n"
301                                     "echo A=23\n",
302                                     WRITE_STRING_FILE_CREATE) == 0);
303         assert_se(write_string_file(name2,
304                                     "#!/bin/sh\n"
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,
308                                     "#!/bin/sh\n"
309                                     "echo A=$A:24\n"
310                                     "echo B=12\n"
311                                     "echo C=000\n"
312                                     "echo C=001\n"                    /* variable overwriting */
313                                      /* various invalid entries */
314                                     "echo unset A\n"
315                                     "echo unset A=\n"
316                                     "echo unset A=B\n"
317                                     "echo unset \n"
318                                     "echo A B=C\n"
319                                     "echo A\n"
320                                     /* test variable assignment without newline */
321                                     "echo PATH=$PATH:/no/such/file",   /* no newline */
322                                     WRITE_STRING_FILE_CREATE) == 0);
323
324         assert_se(chmod(name, 0755) == 0);
325         assert_se(chmod(name2, 0755) == 0);
326         assert_se(chmod(name3, 0755) == 0);
327
328         r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL);
329         assert_se(r >= 0);
330
331         STRV_FOREACH(p, env)
332                 log_info("got env: \"%s\"", *p);
333
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"));
338 }
339 #endif // 0
340
341 int main(int argc, char *argv[]) {
342         log_set_max_level(LOG_DEBUG);
343         log_parse_environment();
344         log_open();
345
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();
352 #endif // 0
353
354         return 0;
355 }