]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/test-udev-spawn.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include "signal-util.h"
7 #include "udev-event.h"
8 #include "udev-spawn.h"
12 static void test_event_spawn_core(bool with_pidfd
, const char *cmd
, char *result_buf
, size_t 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
, NULL
) == 0);
22 assert_se(unsetenv("SYSTEMD_PIDFD") >= 0);
25 static void test_event_spawn_cat(bool with_pidfd
, size_t buf_size
) {
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
,
36 buf_size
>= BUF_SIZE
? BUF_SIZE
: buf_size
);
38 assert_se(lines
= strv_split_newlines(result_buf
));
41 if (buf_size
>= BUF_SIZE
) {
42 assert_se(strv_contains(lines
, "INTERFACE=lo"));
43 assert_se(strv_contains(lines
, "IFINDEX=1"));
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
];
52 log_debug("/* %s(%s, %s) */", __func__
, arg
, yes_no(with_pidfd
));
54 assert_se(cmd
= strjoin(self
, " ", arg
));
56 test_event_spawn_core(with_pidfd
, cmd
, result_buf
, BUF_SIZE
);
58 assert_se(lines
= strv_split_newlines(result_buf
));
61 assert_se(strv_contains(lines
, "aaa"));
62 assert_se(strv_contains(lines
, "bbb"));
65 static void test1(void) {
66 fprintf(stdout
, "aaa\nbbb");
67 fprintf(stderr
, "ccc\nddd");
70 static void test2(void) {
73 fprintf(stdout
, "aaa\nbbb");
75 memset(buf
, 'a', sizeof(buf
) - 1);
80 int main(int argc
, char *argv
[]) {
81 _cleanup_free_
char *self
= NULL
;
84 if (streq(argv
[1], "test1"))
86 else if (streq(argv
[1], "test2"))
94 test_setup_logging(LOG_DEBUG
);
96 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, -1) >= 0);
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);
103 assert_se(path_make_absolute_cwd(argv
[0], &self
) >= 0);
106 test_event_spawn_self(self
, "test1", true);
107 test_event_spawn_self(self
, "test1", false);
109 test_event_spawn_self(self
, "test2", true);
110 test_event_spawn_self(self
, "test2", false);