chiark / gitweb /
tree-wide: drop 'This file is part of systemd' blurb
[elogind.git] / src / test / test-exec-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright 2010 Lennart Poettering
4   Copyright 2013 Thomas H.P. Andersen
5 ***/
6
7 #include <errno.h>
8 #include <string.h>
9 #include <sys/stat.h>
10 #include <sys/wait.h>
11 #include <unistd.h>
12
13 #include "alloc-util.h"
14 #include "copy.h"
15 #include "def.h"
16 #include "env-util.h"
17 #include "exec-util.h"
18 #include "fd-util.h"
19 #include "fileio.h"
20 #include "fs-util.h"
21 #include "log.h"
22 #include "macro.h"
23 #include "rm-rf.h"
24 #include "string-util.h"
25 #include "strv.h"
26
27 static int here = 0, here2 = 0, here3 = 0;
28 void *ignore_stdout_args[] = {&here, &here2, &here3};
29
30 /* noop handlers, just check that arguments are passed correctly */
31 static int ignore_stdout_func(int fd, void *arg) {
32         assert(fd >= 0);
33         assert(arg == &here);
34         safe_close(fd);
35
36         return 0;
37 }
38 static int ignore_stdout_func2(int fd, void *arg) {
39         assert(fd >= 0);
40         assert(arg == &here2);
41         safe_close(fd);
42
43         return 0;
44 }
45 static int ignore_stdout_func3(int fd, void *arg) {
46         assert(fd >= 0);
47         assert(arg == &here3);
48         safe_close(fd);
49
50         return 0;
51 }
52
53 static const gather_stdout_callback_t ignore_stdout[] = {
54         ignore_stdout_func,
55         ignore_stdout_func2,
56         ignore_stdout_func3,
57 };
58
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,
65                 *masked, *mask,
66                 *masked2, *mask2,   /* the mask is non-executable */
67                 *masked2e, *mask2e; /* the mask is executable */
68
69         log_info("/* %s (%s) */", __func__, gather_stdout ? "gathering stdout" : "asynchronous");
70
71         assert_se(mkdtemp(template_lo));
72         assert_se(mkdtemp(template_hi));
73
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");
85
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);
111
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);
120
121         if (gather_stdout)
122                 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL);
123         else
124                 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL, NULL, NULL);
125
126         assert_se(chdir(template_lo) == 0);
127         assert_se(access("it_works", F_OK) >= 0);
128         assert_se(access("failed", F_OK) < 0);
129
130         assert_se(chdir(template_hi) == 0);
131         assert_se(access("it_works2", F_OK) >= 0);
132         assert_se(access("failed", F_OK) < 0);
133
134         (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
135         (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
136 }
137
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;
145
146         assert_se(mkdtemp(template_lo));
147         assert_se(mkdtemp(template_hi));
148
149         output = strjoina(template_hi, "/output");
150
151         log_info("/* %s >>%s */", __func__, output);
152
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");
161
162         t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
163         assert_se(write_string_file(name, t, WRITE_STRING_FILE_CREATE) == 0);
164
165         t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
166         assert_se(write_string_file(name2, t, WRITE_STRING_FILE_CREATE) == 0);
167
168         t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
169         assert_se(write_string_file(name3, t, WRITE_STRING_FILE_CREATE) == 0);
170
171         t = strjoina("#!/bin/sh\necho OVERRIDDEN >>", output);
172         assert_se(write_string_file(overridden, t, WRITE_STRING_FILE_CREATE) == 0);
173
174         t = strjoina("#!/bin/sh\necho $(basename $0) >>", output);
175         assert_se(write_string_file(override, t, WRITE_STRING_FILE_CREATE) == 0);
176
177         t = strjoina("#!/bin/sh\necho MASKED >>", output);
178         assert_se(write_string_file(masked, t, WRITE_STRING_FILE_CREATE) == 0);
179
180         assert_se(symlink("/dev/null", mask) == 0);
181
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);
188
189         execute_directories(dirs, DEFAULT_TIMEOUT_USEC, ignore_stdout, ignore_stdout_args, NULL);
190
191         assert_se(read_full_file(output, &contents, NULL) >= 0);
192         assert_se(streq(contents, "30-override\n80-foo\n90-bar\nlast\n"));
193
194         (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
195         (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
196 }
197
198 static int gather_stdout_one(int fd, void *arg) {
199         char ***s = arg, *t;
200         char buf[128] = {};
201
202         assert_se(s);
203         assert_se(read(fd, buf, sizeof buf) >= 0);
204         safe_close(fd);
205
206         assert_se(t = strndup(buf, sizeof buf));
207         assert_se(strv_push(s, t) >= 0);
208
209         return 0;
210 }
211 static int gather_stdout_two(int fd, void *arg) {
212         char ***s = arg, **t;
213
214         STRV_FOREACH(t, *s)
215                 assert_se(write(fd, *t, strlen(*t)) == (ssize_t) strlen(*t));
216         safe_close(fd);
217
218         return 0;
219 }
220 static int gather_stdout_three(int fd, void *arg) {
221         char **s = arg;
222         char buf[128] = {};
223
224         assert_se(read(fd, buf, sizeof buf - 1) > 0);
225         safe_close(fd);
226         assert_se(*s = strndup(buf, sizeof buf));
227
228         return 0;
229 }
230
231 const gather_stdout_callback_t gather_stdout[] = {
232         gather_stdout_one,
233         gather_stdout_two,
234         gather_stdout_three,
235 };
236
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;
241         int r;
242
243         char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
244         _cleanup_free_ char *output = NULL;
245
246         void* args[] = {&tmp, &tmp, &output};
247
248         assert_se(mkdtemp(template));
249
250         log_info("/* %s */", __func__);
251
252         /* write files */
253         name = strjoina(template, "/10-foo");
254         name2 = strjoina(template, "/20-bar");
255         name3 = strjoina(template, "/30-last");
256
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);
266
267         assert_se(chmod(name, 0755) == 0);
268         assert_se(chmod(name2, 0755) == 0);
269         assert_se(chmod(name3, 0755) == 0);
270
271         r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_stdout, args, NULL);
272         assert_se(r >= 0);
273
274         log_info("got: %s", output);
275
276         assert_se(streq(output, "a\nb\nc\nd\n"));
277 }
278
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;
284         int r;
285
286         char **tmp = NULL; /* this is only used in the forked process, no cleanup here */
287         _cleanup_strv_free_ char **env = NULL;
288
289         void* const args[] = { &tmp, &tmp, &env };
290
291         assert_se(mkdtemp(template));
292
293         log_info("/* %s */", __func__);
294
295         /* write files */
296         name = strjoina(template, "/10-foo");
297         name2 = strjoina(template, "/20-bar");
298         name3 = strjoina(template, "/30-last");
299
300         assert_se(write_string_file(name,
301                                     "#!/bin/sh\n"
302                                     "echo A=23\n",
303                                     WRITE_STRING_FILE_CREATE) == 0);
304         assert_se(write_string_file(name2,
305                                     "#!/bin/sh\n"
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,
309                                     "#!/bin/sh\n"
310                                     "echo A=$A:24\n"
311                                     "echo B=12\n"
312                                     "echo C=000\n"
313                                     "echo C=001\n"                    /* variable overwriting */
314                                      /* various invalid entries */
315                                     "echo unset A\n"
316                                     "echo unset A=\n"
317                                     "echo unset A=B\n"
318                                     "echo unset \n"
319                                     "echo A B=C\n"
320                                     "echo A\n"
321                                     /* test variable assignment without newline */
322                                     "echo PATH=$PATH:/no/such/file",   /* no newline */
323                                     WRITE_STRING_FILE_CREATE) == 0);
324
325         assert_se(chmod(name, 0755) == 0);
326         assert_se(chmod(name2, 0755) == 0);
327         assert_se(chmod(name3, 0755) == 0);
328
329         r = execute_directories(dirs, DEFAULT_TIMEOUT_USEC, gather_environment, args, NULL);
330         assert_se(r >= 0);
331
332         STRV_FOREACH(p, env)
333                 log_info("got env: \"%s\"", *p);
334
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"));
339 }
340 #endif // 0
341
342 int main(int argc, char *argv[]) {
343         log_set_max_level(LOG_DEBUG);
344         log_parse_environment();
345         log_open();
346
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();
353 #endif // 0
354
355         return 0;
356 }