]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/test-udev-event.c
dissect: use DISKSEQ when waiting for block devices
[thirdparty/systemd.git] / src / udev / test-udev-event.c
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"
8 #include "util.h"
9
10 #define BUF_SIZE 1024
11
12 static void test_event_spawn_core(bool with_pidfd, const char *cmd, char result_buf[BUF_SIZE]) {
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));
20 assert_se(udev_event_spawn(event, 5 * USEC_PER_SEC, SIGKILL, false, cmd, result_buf, BUF_SIZE) == 0);
21
22 assert_se(unsetenv("SYSTEMD_PIDFD") >= 0);
23 }
24
25 static void test_event_spawn_cat(bool with_pidfd) {
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
35 test_event_spawn_core(with_pidfd, cmd, result_buf);
36
37 assert_se(lines = strv_split_newlines(result_buf));
38 strv_print(lines);
39
40 assert_se(strv_contains(lines, "INTERFACE=lo"));
41 assert_se(strv_contains(lines, "IFINDEX=1"));
42 }
43
44 static void test_event_spawn_self(const char *self, const char *arg, bool with_pidfd) {
45 _cleanup_strv_free_ char **lines = NULL;
46 _cleanup_free_ char *cmd = NULL;
47 char result_buf[BUF_SIZE];
48
49 log_debug("/* %s(%s, %s) */", __func__, arg, yes_no(with_pidfd));
50
51 assert_se(cmd = strjoin(self, " ", arg));
52
53 test_event_spawn_core(with_pidfd, cmd, result_buf);
54
55 assert_se(lines = strv_split_newlines(result_buf));
56 strv_print(lines);
57
58 assert_se(strv_contains(lines, "aaa"));
59 assert_se(strv_contains(lines, "bbb"));
60 }
61
62 static void test1(void) {
63 fprintf(stdout, "aaa\nbbb");
64 fprintf(stderr, "ccc\nddd");
65 }
66
67 static void test2(void) {
68 char buf[16384];
69
70 fprintf(stdout, "aaa\nbbb");
71
72 memset(buf, 'a', sizeof(buf) - 1);
73 char_array_0(buf);
74 fputs(buf, stderr);
75 }
76
77 int main(int argc, char *argv[]) {
78 _cleanup_free_ char *self = NULL;
79
80 if (argc > 1) {
81 if (streq(argv[1], "test1"))
82 test1();
83 else if (streq(argv[1], "test2"))
84 test2();
85 else
86 assert_not_reached("unknown command.");
87
88 return 0;
89 }
90
91 test_setup_logging(LOG_DEBUG);
92
93 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
94
95 test_event_spawn_cat(true);
96 test_event_spawn_cat(false);
97
98 assert_se(path_make_absolute_cwd(argv[0], &self) >= 0);
99 path_simplify(self);
100
101 test_event_spawn_self(self, "test1", true);
102 test_event_spawn_self(self, "test1", false);
103
104 test_event_spawn_self(self, "test2", true);
105 test_event_spawn_self(self, "test2", false);
106
107 return 0;
108 }