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