| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include "sd-device.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "mountpoint-util.h" |
| 9 |
#include "path-util.h" |
| 10 |
#include "string-util.h" |
| 11 |
#include "strv.h" |
| 12 |
#include "tests.h" |
| 13 |
#include "udev-event.h" |
| 14 |
#include "udev-spawn.h" |
| 15 |
|
| 16 |
#define BUF_SIZE 1024 |
| 17 |
|
| 18 |
static void test_event_spawn_core(bool with_pidfd, const char *cmd, char *result_buf, size_t buf_size) { |
| 19 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 20 |
_cleanup_(udev_event_unrefp) UdevEvent *event = NULL; |
| 21 |
|
| 22 |
ASSERT_OK_ERRNO(setenv("SYSTEMD_PIDFD", yes_no(with_pidfd), 1)); |
| 23 |
|
| 24 |
ASSERT_OK(sd_device_new_from_syspath(&dev, "/sys/class/net/lo")); |
| 25 |
ASSERT_NOT_NULL((event = udev_event_new(dev, NULL, EVENT_TEST_SPAWN))); |
| 26 |
ASSERT_OK_ZERO(udev_event_spawn(event, false, cmd, result_buf, buf_size, NULL)); |
| 27 |
|
| 28 |
ASSERT_OK_ERRNO(unsetenv("SYSTEMD_PIDFD")); |
| 29 |
} |
| 30 |
|
| 31 |
static void test_event_spawn_cat(bool with_pidfd, size_t buf_size) { |
| 32 |
_cleanup_strv_free_ char **lines = NULL; |
| 33 |
_cleanup_free_ char *cmd = NULL; |
| 34 |
char result_buf[BUF_SIZE]; |
| 35 |
|
| 36 |
log_debug("/* %s(%s) */", __func__, yes_no(with_pidfd)); |
| 37 |
|
| 38 |
ASSERT_OK(find_executable("cat", &cmd)); |
| 39 |
ASSERT_NOT_NULL(strextend_with_separator(&cmd, " ", "/sys/class/net/lo/uevent")); |
| 40 |
|
| 41 |
test_event_spawn_core(with_pidfd, cmd, result_buf, |
| 42 |
buf_size >= BUF_SIZE ? BUF_SIZE : buf_size); |
| 43 |
|
| 44 |
ASSERT_NOT_NULL((lines = strv_split_newlines(result_buf))); |
| 45 |
strv_print(lines); |
| 46 |
|
| 47 |
if (buf_size >= BUF_SIZE) { |
| 48 |
ASSERT_TRUE(strv_contains(lines, "INTERFACE=lo")); |
| 49 |
ASSERT_TRUE(strv_contains(lines, "IFINDEX=1")); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static void test_event_spawn_self(const char *self, const char *arg, bool with_pidfd) { |
| 54 |
_cleanup_strv_free_ char **lines = NULL; |
| 55 |
_cleanup_free_ char *cmd = NULL; |
| 56 |
char result_buf[BUF_SIZE]; |
| 57 |
|
| 58 |
log_debug("/* %s(%s, %s) */", __func__, arg, yes_no(with_pidfd)); |
| 59 |
|
| 60 |
/* 'self' may contain spaces, hence needs to be quoted. */ |
| 61 |
ASSERT_NOT_NULL((cmd = strjoin("'", self, "' ", arg))); |
| 62 |
|
| 63 |
test_event_spawn_core(with_pidfd, cmd, result_buf, BUF_SIZE); |
| 64 |
|
| 65 |
ASSERT_NOT_NULL((lines = strv_split_newlines(result_buf))); |
| 66 |
strv_print(lines); |
| 67 |
|
| 68 |
ASSERT_TRUE(strv_contains(lines, "aaa")); |
| 69 |
ASSERT_TRUE(strv_contains(lines, "bbb")); |
| 70 |
} |
| 71 |
|
| 72 |
static void test1(void) { |
| 73 |
fprintf(stdout, "aaa\nbbb"); |
| 74 |
fprintf(stderr, "ccc\nddd"); |
| 75 |
} |
| 76 |
|
| 77 |
static void test2(void) { |
| 78 |
char buf[16384]; |
| 79 |
|
| 80 |
fprintf(stdout, "aaa\nbbb"); |
| 81 |
|
| 82 |
memset(buf, 'a', sizeof(buf) - 1); |
| 83 |
char_array_0(buf); |
| 84 |
fputs(buf, stderr); |
| 85 |
} |
| 86 |
|
| 87 |
int main(int argc, char *argv[]) { |
| 88 |
_cleanup_free_ char *self = NULL; |
| 89 |
|
| 90 |
if (path_is_mount_point("/sys") <= 0) |
| 91 |
return log_tests_skipped("/sys is not mounted"); |
| 92 |
|
| 93 |
if (argc > 1) { |
| 94 |
if (streq(argv[1], "test1")) |
| 95 |
test1(); |
| 96 |
else if (streq(argv[1], "test2")) |
| 97 |
test2(); |
| 98 |
else |
| 99 |
assert_not_reached(); |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
test_setup_logging(LOG_DEBUG); |
| 105 |
|
| 106 |
test_event_spawn_cat(true, SIZE_MAX); |
| 107 |
test_event_spawn_cat(false, SIZE_MAX); |
| 108 |
test_event_spawn_cat(true, 5); |
| 109 |
test_event_spawn_cat(false, 5); |
| 110 |
|
| 111 |
assert_se(path_make_absolute_cwd(argv[0], &self) >= 0); |
| 112 |
path_simplify(self); |
| 113 |
|
| 114 |
test_event_spawn_self(self, "test1", true); |
| 115 |
test_event_spawn_self(self, "test1", false); |
| 116 |
|
| 117 |
test_event_spawn_self(self, "test2", true); |
| 118 |
test_event_spawn_self(self, "test2", false); |
| 119 |
|
| 120 |
return 0; |
| 121 |
} |