]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/test-udev-spawn.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
8 #include "mountpoint-util.h"
10 #include "signal-util.h"
11 #include "string-util.h"
14 #include "udev-event.h"
15 #include "udev-spawn.h"
19 static void test_event_spawn_core(bool with_pidfd
, const char *cmd
, char *result_buf
, size_t buf_size
) {
20 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
21 _cleanup_(udev_event_unrefp
) UdevEvent
*event
= NULL
;
23 ASSERT_OK_ERRNO(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd
), 1));
25 ASSERT_OK(sd_device_new_from_syspath(&dev
, "/sys/class/net/lo"));
26 ASSERT_NOT_NULL((event
= udev_event_new(dev
, NULL
, EVENT_TEST_SPAWN
)));
27 ASSERT_OK_ZERO(udev_event_spawn(event
, false, cmd
, result_buf
, buf_size
, NULL
));
29 ASSERT_OK_ERRNO(unsetenv("SYSTEMD_PIDFD"));
32 static void test_event_spawn_cat(bool with_pidfd
, size_t buf_size
) {
33 _cleanup_strv_free_
char **lines
= NULL
;
34 _cleanup_free_
char *cmd
= NULL
;
35 char result_buf
[BUF_SIZE
];
37 log_debug("/* %s(%s) */", __func__
, yes_no(with_pidfd
));
39 ASSERT_OK(find_executable("cat", &cmd
));
40 ASSERT_NOT_NULL(strextend_with_separator(&cmd
, " ", "/sys/class/net/lo/uevent"));
42 test_event_spawn_core(with_pidfd
, cmd
, result_buf
,
43 buf_size
>= BUF_SIZE
? BUF_SIZE
: buf_size
);
45 ASSERT_NOT_NULL((lines
= strv_split_newlines(result_buf
)));
48 if (buf_size
>= BUF_SIZE
) {
49 ASSERT_TRUE(strv_contains(lines
, "INTERFACE=lo"));
50 ASSERT_TRUE(strv_contains(lines
, "IFINDEX=1"));
54 static void test_event_spawn_self(const char *self
, const char *arg
, bool with_pidfd
) {
55 _cleanup_strv_free_
char **lines
= NULL
;
56 _cleanup_free_
char *cmd
= NULL
;
57 char result_buf
[BUF_SIZE
];
59 log_debug("/* %s(%s, %s) */", __func__
, arg
, yes_no(with_pidfd
));
61 /* 'self' may contain spaces, hence needs to be quoted. */
62 ASSERT_NOT_NULL((cmd
= strjoin("'", self
, "' ", arg
)));
64 test_event_spawn_core(with_pidfd
, cmd
, result_buf
, BUF_SIZE
);
66 ASSERT_NOT_NULL((lines
= strv_split_newlines(result_buf
)));
69 ASSERT_TRUE(strv_contains(lines
, "aaa"));
70 ASSERT_TRUE(strv_contains(lines
, "bbb"));
73 static void test1(void) {
74 fprintf(stdout
, "aaa\nbbb");
75 fprintf(stderr
, "ccc\nddd");
78 static void test2(void) {
81 fprintf(stdout
, "aaa\nbbb");
83 memset(buf
, 'a', sizeof(buf
) - 1);
88 int main(int argc
, char *argv
[]) {
89 _cleanup_free_
char *self
= NULL
;
91 if (path_is_mount_point("/sys") <= 0)
92 return log_tests_skipped("/sys is not mounted");
95 if (streq(argv
[1], "test1"))
97 else if (streq(argv
[1], "test2"))
100 assert_not_reached();
105 test_setup_logging(LOG_DEBUG
);
107 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGCHLD
) >= 0);
109 test_event_spawn_cat(true, SIZE_MAX
);
110 test_event_spawn_cat(false, SIZE_MAX
);
111 test_event_spawn_cat(true, 5);
112 test_event_spawn_cat(false, 5);
114 assert_se(path_make_absolute_cwd(argv
[0], &self
) >= 0);
117 test_event_spawn_self(self
, "test1", true);
118 test_event_spawn_self(self
, "test1", false);
120 test_event_spawn_self(self
, "test2", true);
121 test_event_spawn_self(self
, "test2", false);