]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/test-udev-event.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include "signal-util.h"
7 #include "udev-event.h"
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
;
16 assert_se(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd
), 1) >= 0);
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);
22 assert_se(unsetenv("SYSTEMD_PIDFD") >= 0);
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
];
30 log_debug("/* %s(%s) */", __func__
, yes_no(with_pidfd
));
32 assert_se(find_executable("cat", &cmd
) >= 0);
33 assert_se(strextend_with_separator(&cmd
, " ", "/sys/class/net/lo/uevent"));
35 test_event_spawn_core(with_pidfd
, cmd
, result_buf
);
37 assert_se(lines
= strv_split_newlines(result_buf
));
40 assert_se(strv_contains(lines
, "INTERFACE=lo"));
41 assert_se(strv_contains(lines
, "IFINDEX=1"));
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
];
49 log_debug("/* %s(%s, %s) */", __func__
, arg
, yes_no(with_pidfd
));
51 assert_se(cmd
= strjoin(self
, " ", arg
));
53 test_event_spawn_core(with_pidfd
, cmd
, result_buf
);
55 assert_se(lines
= strv_split_newlines(result_buf
));
58 assert_se(strv_contains(lines
, "aaa"));
59 assert_se(strv_contains(lines
, "bbb"));
62 static void test1(void) {
63 fprintf(stdout
, "aaa\nbbb");
64 fprintf(stderr
, "ccc\nddd");
67 static void test2(void) {
70 fprintf(stdout
, "aaa\nbbb");
72 memset(buf
, 'a', sizeof(buf
) - 1);
77 int main(int argc
, char *argv
[]) {
78 _cleanup_free_
char *self
= NULL
;
81 if (streq(argv
[1], "test1"))
83 else if (streq(argv
[1], "test2"))
86 assert_not_reached("unknown command.");
91 test_setup_logging(LOG_DEBUG
);
93 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, -1) >= 0);
95 test_event_spawn_cat(true);
96 test_event_spawn_cat(false);
98 assert_se(path_make_absolute_cwd(argv
[0], &self
) >= 0);
101 test_event_spawn_self(self
, "test1", true);
102 test_event_spawn_self(self
, "test1", false);
104 test_event_spawn_self(self
, "test2", true);
105 test_event_spawn_self(self
, "test2", false);