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