]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/test-sd-device-monitor.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / libsystemd / sd-device / test-sd-device-monitor.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <stdbool.h>
4 #include <unistd.h>
5
6 #include "sd-device.h"
7 #include "sd-event.h"
8
9 #include "device-monitor-private.h"
10 #include "device-private.h"
11 #include "device-util.h"
12 #include "macro.h"
13 #include "string-util.h"
14 #include "tests.h"
15 #include "util.h"
16 #include "virt.h"
17
18 static int monitor_handler(sd_device_monitor *m, sd_device *d, void *userdata) {
19 const char *s, *syspath = userdata;
20
21 assert_se(sd_device_get_syspath(d, &s) >= 0);
22 assert_se(streq(s, syspath));
23
24 return sd_event_exit(sd_device_monitor_get_event(m), 0);
25 }
26
27 static int test_loopback(bool subsystem_filter, bool tag_filter, bool use_bpf) {
28 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
29 _cleanup_(sd_device_unrefp) sd_device *loopback = NULL;
30 const char *syspath, *subsystem, *tag;
31 int r;
32
33 log_info("/* %s(subsystem_filter=%s, tag_filter=%s, use_bpf=%s) */", __func__,
34 true_false(subsystem_filter), true_false(tag_filter), true_false(use_bpf));
35
36 assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0);
37 assert_se(sd_device_get_syspath(loopback, &syspath) >= 0);
38 assert_se(device_add_property(loopback, "ACTION", "add") >= 0);
39 assert_se(device_add_property(loopback, "SEQNUM", "10") >= 0);
40
41 assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0);
42 assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0);
43 assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0);
44
45 assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0);
46 assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0);
47 assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0);
48 assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0);
49
50 if (subsystem_filter) {
51 assert_se(sd_device_get_subsystem(loopback, &subsystem) >= 0);
52 assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, NULL) >= 0);
53 }
54
55 if (tag_filter)
56 FOREACH_DEVICE_TAG(loopback, tag)
57 assert_se(sd_device_monitor_filter_add_match_tag(monitor_client, tag) >= 0);
58
59 if ((subsystem_filter || tag_filter) && use_bpf)
60 assert_se(sd_device_monitor_filter_update(monitor_client) >= 0);
61
62 r = device_monitor_send_device(monitor_server, monitor_client, loopback);
63 if (r < 0)
64 return log_error_errno(r, "Failed to send loopback device: %m");
65
66 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 0);
67
68 return 0;
69 }
70
71 static void test_subsystem_filter(void) {
72 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
73 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
74 _cleanup_(sd_device_unrefp) sd_device *loopback = NULL;
75 const char *syspath, *subsystem, *p, *s;
76 sd_device *d;
77
78 log_info("/* %s */", __func__);
79
80 assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0);
81 assert_se(sd_device_get_syspath(loopback, &syspath) >= 0);
82 assert_se(sd_device_get_subsystem(loopback, &subsystem) >= 0);
83 assert_se(device_add_property(loopback, "ACTION", "add") >= 0);
84 assert_se(device_add_property(loopback, "SEQNUM", "10") >= 0);
85
86 assert_se(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -1) >= 0);
87 assert_se(sd_device_monitor_start(monitor_server, NULL, NULL) >= 0);
88 assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_server), "sender") >= 0);
89
90 assert_se(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -1) >= 0);
91 assert_se(device_monitor_allow_unicast_sender(monitor_client, monitor_server) >= 0);
92 assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, NULL) >= 0);
93 assert_se(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath) >= 0);
94 assert_se(sd_event_source_set_description(sd_device_monitor_get_event_source(monitor_client), "receiver") >= 0);
95
96 assert_se(sd_device_enumerator_new(&e) >= 0);
97 assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, false) >= 0);
98 FOREACH_DEVICE(e, d) {
99 assert_se(sd_device_get_syspath(d, &p) >= 0);
100 assert_se(sd_device_get_subsystem(d, &s) >= 0);
101
102 log_info("Sending device subsystem:%s syspath:%s", s, p);
103 assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0);
104 }
105
106 log_info("Sending device subsystem:%s syspath:%s", subsystem, syspath);
107 assert_se(device_monitor_send_device(monitor_server, monitor_client, loopback) >= 0);
108 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client)) == 0);
109 }
110
111 int main(int argc, char *argv[]) {
112 int r;
113
114 test_setup_logging(LOG_INFO);
115
116 if (getuid() != 0)
117 return log_tests_skipped("not root");
118
119 r = test_loopback(false, false, false);
120 if (r < 0) {
121 assert_se(r == -EPERM && detect_container() > 0);
122 return log_tests_skipped("Running in container? Skipping remaining tests");
123 }
124
125 assert_se(test_loopback( true, false, false) >= 0);
126 assert_se(test_loopback(false, true, false) >= 0);
127 assert_se(test_loopback( true, true, false) >= 0);
128 assert_se(test_loopback( true, false, true) >= 0);
129 assert_se(test_loopback(false, true, true) >= 0);
130 assert_se(test_loopback( true, true, true) >= 0);
131
132 test_subsystem_filter();
133
134 return 0;
135 }