]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/test-udev-spawn.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "mountpoint-util.h"
5 #include "signal-util.h"
8 #include "udev-event.h"
9 #include "udev-spawn.h"
13 static void test_event_spawn_core(bool with_pidfd
, const char *cmd
, char *result_buf
, size_t buf_size
) {
14 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
15 _cleanup_(udev_event_freep
) UdevEvent
*event
= NULL
;
17 assert_se(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd
), 1) >= 0);
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
));
21 assert_se(udev_event_spawn(event
, 5 * USEC_PER_SEC
, SIGKILL
, false, cmd
, result_buf
, buf_size
, NULL
) == 0);
23 assert_se(unsetenv("SYSTEMD_PIDFD") >= 0);
26 static void test_event_spawn_cat(bool with_pidfd
, size_t buf_size
) {
27 _cleanup_strv_free_
char **lines
= NULL
;
28 _cleanup_free_
char *cmd
= NULL
;
29 char result_buf
[BUF_SIZE
];
31 log_debug("/* %s(%s) */", __func__
, yes_no(with_pidfd
));
33 assert_se(find_executable("cat", &cmd
) >= 0);
34 assert_se(strextend_with_separator(&cmd
, " ", "/sys/class/net/lo/uevent"));
36 test_event_spawn_core(with_pidfd
, cmd
, result_buf
,
37 buf_size
>= BUF_SIZE
? BUF_SIZE
: buf_size
);
39 assert_se(lines
= strv_split_newlines(result_buf
));
42 if (buf_size
>= BUF_SIZE
) {
43 assert_se(strv_contains(lines
, "INTERFACE=lo"));
44 assert_se(strv_contains(lines
, "IFINDEX=1"));
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
];
53 log_debug("/* %s(%s, %s) */", __func__
, arg
, yes_no(with_pidfd
));
55 assert_se(cmd
= strjoin(self
, " ", arg
));
57 test_event_spawn_core(with_pidfd
, cmd
, result_buf
, BUF_SIZE
);
59 assert_se(lines
= strv_split_newlines(result_buf
));
62 assert_se(strv_contains(lines
, "aaa"));
63 assert_se(strv_contains(lines
, "bbb"));
66 static void test1(void) {
67 fprintf(stdout
, "aaa\nbbb");
68 fprintf(stderr
, "ccc\nddd");
71 static void test2(void) {
74 fprintf(stdout
, "aaa\nbbb");
76 memset(buf
, 'a', sizeof(buf
) - 1);
81 int main(int argc
, char *argv
[]) {
82 _cleanup_free_
char *self
= NULL
;
84 if (path_is_mount_point("/sys", NULL
, 0) <= 0)
85 return log_tests_skipped("/sys is not mounted");
88 if (streq(argv
[1], "test1"))
90 else if (streq(argv
[1], "test2"))
98 test_setup_logging(LOG_DEBUG
);
100 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
, -1) >= 0);
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);
107 assert_se(path_make_absolute_cwd(argv
[0], &self
) >= 0);
110 test_event_spawn_self(self
, "test1", true);
111 test_event_spawn_self(self
, "test1", false);
113 test_event_spawn_self(self
, "test2", true);
114 test_event_spawn_self(self
, "test2", false);