]>
Commit | Line | Data |
---|---|---|
ac714078 YW |
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | ||
3 | #include "path-util.h" | |
4 | #include "signal-util.h" | |
5 | #include "strv.h" | |
6 | #include "tests.h" | |
7 | #include "udev-event.h" | |
0b76cc2f | 8 | #include "udev-spawn.h" |
ac714078 YW |
9 | |
10 | #define BUF_SIZE 1024 | |
11 | ||
b0c01c98 | 12 | static void test_event_spawn_core(bool with_pidfd, const char *cmd, char *result_buf, size_t buf_size) { |
ac714078 YW |
13 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
14 | _cleanup_(udev_event_freep) UdevEvent *event = NULL; | |
15 | ||
16 | assert_se(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd), 1) >= 0); | |
17 | ||
18 | assert_se(sd_device_new_from_syspath(&dev, "/sys/class/net/lo") >= 0); | |
19 | assert_se(event = udev_event_new(dev, 0, NULL, LOG_DEBUG)); | |
b0c01c98 | 20 | assert_se(udev_event_spawn(event, 5 * USEC_PER_SEC, SIGKILL, false, cmd, result_buf, buf_size, NULL) == 0); |
ac714078 YW |
21 | |
22 | assert_se(unsetenv("SYSTEMD_PIDFD") >= 0); | |
23 | } | |
24 | ||
b0c01c98 | 25 | static void test_event_spawn_cat(bool with_pidfd, size_t buf_size) { |
ac714078 YW |
26 | _cleanup_strv_free_ char **lines = NULL; |
27 | _cleanup_free_ char *cmd = NULL; | |
28 | char result_buf[BUF_SIZE]; | |
29 | ||
30 | log_debug("/* %s(%s) */", __func__, yes_no(with_pidfd)); | |
31 | ||
32 | assert_se(find_executable("cat", &cmd) >= 0); | |
33 | assert_se(strextend_with_separator(&cmd, " ", "/sys/class/net/lo/uevent")); | |
34 | ||
b0c01c98 YW |
35 | test_event_spawn_core(with_pidfd, cmd, result_buf, |
36 | buf_size >= BUF_SIZE ? BUF_SIZE : buf_size); | |
ac714078 YW |
37 | |
38 | assert_se(lines = strv_split_newlines(result_buf)); | |
39 | strv_print(lines); | |
40 | ||
b0c01c98 YW |
41 | if (buf_size >= BUF_SIZE) { |
42 | assert_se(strv_contains(lines, "INTERFACE=lo")); | |
43 | assert_se(strv_contains(lines, "IFINDEX=1")); | |
44 | } | |
ac714078 YW |
45 | } |
46 | ||
47 | static void test_event_spawn_self(const char *self, const char *arg, bool with_pidfd) { | |
48 | _cleanup_strv_free_ char **lines = NULL; | |
49 | _cleanup_free_ char *cmd = NULL; | |
50 | char result_buf[BUF_SIZE]; | |
51 | ||
52 | log_debug("/* %s(%s, %s) */", __func__, arg, yes_no(with_pidfd)); | |
53 | ||
54 | assert_se(cmd = strjoin(self, " ", arg)); | |
55 | ||
b0c01c98 | 56 | test_event_spawn_core(with_pidfd, cmd, result_buf, BUF_SIZE); |
ac714078 YW |
57 | |
58 | assert_se(lines = strv_split_newlines(result_buf)); | |
59 | strv_print(lines); | |
60 | ||
61 | assert_se(strv_contains(lines, "aaa")); | |
62 | assert_se(strv_contains(lines, "bbb")); | |
63 | } | |
64 | ||
65 | static void test1(void) { | |
66 | fprintf(stdout, "aaa\nbbb"); | |
67 | fprintf(stderr, "ccc\nddd"); | |
68 | } | |
69 | ||
70 | static void test2(void) { | |
71 | char buf[16384]; | |
72 | ||
73 | fprintf(stdout, "aaa\nbbb"); | |
74 | ||
75 | memset(buf, 'a', sizeof(buf) - 1); | |
76 | char_array_0(buf); | |
77 | fputs(buf, stderr); | |
78 | } | |
79 | ||
80 | int main(int argc, char *argv[]) { | |
81 | _cleanup_free_ char *self = NULL; | |
82 | ||
83 | if (argc > 1) { | |
84 | if (streq(argv[1], "test1")) | |
85 | test1(); | |
86 | else if (streq(argv[1], "test2")) | |
87 | test2(); | |
88 | else | |
04499a70 | 89 | assert_not_reached(); |
ac714078 YW |
90 | |
91 | return 0; | |
92 | } | |
93 | ||
94 | test_setup_logging(LOG_DEBUG); | |
95 | ||
96 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0); | |
97 | ||
b0c01c98 YW |
98 | test_event_spawn_cat(true, SIZE_MAX); |
99 | test_event_spawn_cat(false, SIZE_MAX); | |
100 | test_event_spawn_cat(true, 5); | |
101 | test_event_spawn_cat(false, 5); | |
ac714078 YW |
102 | |
103 | assert_se(path_make_absolute_cwd(argv[0], &self) >= 0); | |
4ff361cc | 104 | path_simplify(self); |
ac714078 YW |
105 | |
106 | test_event_spawn_self(self, "test1", true); | |
107 | test_event_spawn_self(self, "test1", false); | |
108 | ||
109 | test_event_spawn_self(self, "test2", true); | |
110 | test_event_spawn_self(self, "test2", false); | |
111 | ||
112 | return 0; | |
113 | } |