1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "device-monitor-private.h"
10 #include "device-private.h"
11 #include "device-util.h"
13 #include "path-util.h"
14 #include "stat-util.h"
15 #include "string-util.h"
19 static int monitor_handler(sd_device_monitor
*m
, sd_device
*d
, void *userdata
) {
20 const char *s
, *syspath
= userdata
;
22 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
23 assert_se(streq(s
, syspath
));
25 return sd_event_exit(sd_device_monitor_get_event(m
), 100);
28 static void test_receive_device_fail(void) {
29 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor_server
= NULL
, *monitor_client
= NULL
;
30 _cleanup_(sd_device_unrefp
) sd_device
*loopback
= NULL
;
33 log_info("/* %s */", __func__
);
35 /* Try to send device with invalid action and without seqnum. */
36 assert_se(sd_device_new_from_syspath(&loopback
, "/sys/class/net/lo") >= 0);
37 assert_se(device_add_property(loopback
, "ACTION", "hoge") >= 0);
39 assert_se(sd_device_get_syspath(loopback
, &syspath
) >= 0);
41 assert_se(device_monitor_new_full(&monitor_server
, MONITOR_GROUP_NONE
, -1) >= 0);
42 assert_se(sd_device_monitor_set_description(monitor_server
, "sender") >= 0);
43 assert_se(sd_device_monitor_start(monitor_server
, NULL
, NULL
) >= 0);
45 assert_se(device_monitor_new_full(&monitor_client
, MONITOR_GROUP_NONE
, -1) >= 0);
46 assert_se(sd_device_monitor_set_description(monitor_client
, "receiver") >= 0);
47 assert_se(device_monitor_allow_unicast_sender(monitor_client
, monitor_server
) >= 0);
48 assert_se(sd_device_monitor_start(monitor_client
, monitor_handler
, (void *) syspath
) >= 0);
50 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, loopback
) >= 0);
51 assert_se(sd_event_run(sd_device_monitor_get_event(monitor_client
), 0) >= 0);
54 static void test_send_receive_one(sd_device
*device
, bool subsystem_filter
, bool tag_filter
, bool use_bpf
) {
55 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor_server
= NULL
, *monitor_client
= NULL
;
56 const char *syspath
, *subsystem
, *devtype
= NULL
;
58 log_device_info(device
, "/* %s(subsystem_filter=%s, tag_filter=%s, use_bpf=%s) */", __func__
,
59 true_false(subsystem_filter
), true_false(tag_filter
), true_false(use_bpf
));
61 assert_se(sd_device_get_syspath(device
, &syspath
) >= 0);
63 assert_se(device_monitor_new_full(&monitor_server
, MONITOR_GROUP_NONE
, -1) >= 0);
64 assert_se(sd_device_monitor_set_description(monitor_server
, "sender") >= 0);
65 assert_se(sd_device_monitor_start(monitor_server
, NULL
, NULL
) >= 0);
67 assert_se(device_monitor_new_full(&monitor_client
, MONITOR_GROUP_NONE
, -1) >= 0);
68 assert_se(sd_device_monitor_set_description(monitor_client
, "receiver") >= 0);
69 assert_se(device_monitor_allow_unicast_sender(monitor_client
, monitor_server
) >= 0);
70 assert_se(sd_device_monitor_start(monitor_client
, monitor_handler
, (void *) syspath
) >= 0);
72 if (subsystem_filter
) {
73 assert_se(sd_device_get_subsystem(device
, &subsystem
) >= 0);
74 (void) sd_device_get_devtype(device
, &devtype
);
75 assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client
, subsystem
, devtype
) >= 0);
79 FOREACH_DEVICE_TAG(device
, tag
)
80 assert_se(sd_device_monitor_filter_add_match_tag(monitor_client
, tag
) >= 0);
82 if ((subsystem_filter
|| tag_filter
) && use_bpf
)
83 assert_se(sd_device_monitor_filter_update(monitor_client
) >= 0);
85 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, device
) >= 0);
86 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client
)) == 100);
89 static void test_subsystem_filter(sd_device
*device
) {
90 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor_server
= NULL
, *monitor_client
= NULL
;
91 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
92 const char *syspath
, *subsystem
;
94 log_device_info(device
, "/* %s */", __func__
);
96 assert_se(sd_device_get_syspath(device
, &syspath
) >= 0);
97 assert_se(sd_device_get_subsystem(device
, &subsystem
) >= 0);
99 assert_se(device_monitor_new_full(&monitor_server
, MONITOR_GROUP_NONE
, -1) >= 0);
100 assert_se(sd_device_monitor_set_description(monitor_server
, "sender") >= 0);
101 assert_se(sd_device_monitor_start(monitor_server
, NULL
, NULL
) >= 0);
103 assert_se(device_monitor_new_full(&monitor_client
, MONITOR_GROUP_NONE
, -1) >= 0);
104 assert_se(sd_device_monitor_set_description(monitor_client
, "receiver") >= 0);
105 assert_se(device_monitor_allow_unicast_sender(monitor_client
, monitor_server
) >= 0);
106 assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client
, subsystem
, NULL
) >= 0);
107 assert_se(sd_device_monitor_start(monitor_client
, monitor_handler
, (void *) syspath
) >= 0);
109 assert_se(sd_device_enumerator_new(&e
) >= 0);
110 assert_se(sd_device_enumerator_add_match_subsystem(e
, subsystem
, false) >= 0);
111 FOREACH_DEVICE(e
, d
) {
114 assert_se(sd_device_get_syspath(d
, &p
) >= 0);
115 assert_se(sd_device_get_subsystem(d
, &s
) >= 0);
117 assert_se(device_add_property(d
, "ACTION", "add") >= 0);
118 assert_se(device_add_property(d
, "SEQNUM", "10") >= 0);
120 log_device_debug(d
, "Sending device subsystem:%s syspath:%s", s
, p
);
121 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, d
) >= 0);
124 log_device_info(device
, "Sending device subsystem:%s syspath:%s", subsystem
, syspath
);
125 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, device
) >= 0);
126 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client
)) == 100);
129 static void test_tag_filter(sd_device
*device
) {
130 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor_server
= NULL
, *monitor_client
= NULL
;
131 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
134 log_device_info(device
, "/* %s */", __func__
);
136 assert_se(sd_device_get_syspath(device
, &syspath
) >= 0);
138 assert_se(device_monitor_new_full(&monitor_server
, MONITOR_GROUP_NONE
, -1) >= 0);
139 assert_se(sd_device_monitor_set_description(monitor_server
, "sender") >= 0);
140 assert_se(sd_device_monitor_start(monitor_server
, NULL
, NULL
) >= 0);
142 assert_se(device_monitor_new_full(&monitor_client
, MONITOR_GROUP_NONE
, -1) >= 0);
143 assert_se(sd_device_monitor_set_description(monitor_client
, "receiver") >= 0);
144 assert_se(device_monitor_allow_unicast_sender(monitor_client
, monitor_server
) >= 0);
145 assert_se(sd_device_monitor_filter_add_match_tag(monitor_client
, "TEST_SD_DEVICE_MONITOR") >= 0);
146 assert_se(sd_device_monitor_start(monitor_client
, monitor_handler
, (void *) syspath
) >= 0);
148 assert_se(sd_device_enumerator_new(&e
) >= 0);
149 FOREACH_DEVICE(e
, d
) {
152 assert_se(sd_device_get_syspath(d
, &p
) >= 0);
154 assert_se(device_add_property(d
, "ACTION", "add") >= 0);
155 assert_se(device_add_property(d
, "SEQNUM", "10") >= 0);
157 log_device_debug(d
, "Sending device syspath:%s", p
);
158 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, d
) >= 0);
161 log_device_info(device
, "Sending device syspath:%s", syspath
);
162 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, device
) >= 0);
163 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client
)) == 100);
167 static void test_sysattr_filter(sd_device
*device
, const char *sysattr
) {
168 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor_server
= NULL
, *monitor_client
= NULL
;
169 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
170 const char *syspath
, *sysattr_value
;
172 log_device_info(device
, "/* %s(%s) */", __func__
, sysattr
);
174 assert_se(sd_device_get_syspath(device
, &syspath
) >= 0);
175 assert_se(sd_device_get_sysattr_value(device
, sysattr
, &sysattr_value
) >= 0);
177 assert_se(device_monitor_new_full(&monitor_server
, MONITOR_GROUP_NONE
, -1) >= 0);
178 assert_se(sd_device_monitor_set_description(monitor_server
, "sender") >= 0);
179 assert_se(sd_device_monitor_start(monitor_server
, NULL
, NULL
) >= 0);
181 assert_se(device_monitor_new_full(&monitor_client
, MONITOR_GROUP_NONE
, -1) >= 0);
182 assert_se(sd_device_monitor_set_description(monitor_client
, "receiver") >= 0);
183 assert_se(device_monitor_allow_unicast_sender(monitor_client
, monitor_server
) >= 0);
184 assert_se(sd_device_monitor_filter_add_match_sysattr(monitor_client
, sysattr
, sysattr_value
, true) >= 0);
185 assert_se(sd_device_monitor_start(monitor_client
, monitor_handler
, (void *) syspath
) >= 0);
187 assert_se(sd_device_enumerator_new(&e
) >= 0);
188 assert_se(sd_device_enumerator_add_match_sysattr(e
, sysattr
, sysattr_value
, false) >= 0);
189 FOREACH_DEVICE(e
, d
) {
192 assert_se(sd_device_get_syspath(d
, &p
) >= 0);
194 assert_se(device_add_property(d
, "ACTION", "add") >= 0);
195 assert_se(device_add_property(d
, "SEQNUM", "10") >= 0);
197 log_device_debug(d
, "Sending device syspath:%s", p
);
198 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, d
) >= 0);
200 /* The sysattr filter is not implemented in BPF yet. So, sending multiple devices may fills up
201 * buffer and device_monitor_send_device() may return EAGAIN. Let's send one device here,
202 * which should be filtered out by the receiver. */
206 log_device_info(device
, "Sending device syspath:%s", syspath
);
207 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, device
) >= 0);
208 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client
)) == 100);
212 static void test_parent_filter(sd_device
*device
) {
213 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor_server
= NULL
, *monitor_client
= NULL
;
214 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
215 const char *syspath
, *parent_syspath
;
219 log_device_info(device
, "/* %s */", __func__
);
221 assert_se(sd_device_get_syspath(device
, &syspath
) >= 0);
222 r
= sd_device_get_parent(device
, &parent
);
224 return (void) log_device_info(device
, "Device does not have parent, skipping.");
225 assert_se(sd_device_get_syspath(parent
, &parent_syspath
) >= 0);
227 assert_se(device_monitor_new_full(&monitor_server
, MONITOR_GROUP_NONE
, -1) >= 0);
228 assert_se(sd_device_monitor_set_description(monitor_server
, "sender") >= 0);
229 assert_se(sd_device_monitor_start(monitor_server
, NULL
, NULL
) >= 0);
231 assert_se(device_monitor_new_full(&monitor_client
, MONITOR_GROUP_NONE
, -1) >= 0);
232 assert_se(sd_device_monitor_set_description(monitor_client
, "receiver") >= 0);
233 assert_se(device_monitor_allow_unicast_sender(monitor_client
, monitor_server
) >= 0);
234 assert_se(sd_device_monitor_filter_add_match_parent(monitor_client
, parent
, true) >= 0);
235 assert_se(sd_device_monitor_start(monitor_client
, monitor_handler
, (void *) syspath
) >= 0);
237 assert_se(sd_device_enumerator_new(&e
) >= 0);
238 FOREACH_DEVICE(e
, d
) {
241 assert_se(sd_device_get_syspath(d
, &p
) >= 0);
242 if (path_startswith(p
, parent_syspath
))
245 assert_se(device_add_property(d
, "ACTION", "add") >= 0);
246 assert_se(device_add_property(d
, "SEQNUM", "10") >= 0);
248 log_device_debug(d
, "Sending device syspath:%s", p
);
249 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, d
) >= 0);
251 /* The parent filter is not implemented in BPF yet. So, sending multiple devices may fills up
252 * buffer and device_monitor_send_device() may return EAGAIN. Let's send one device here,
253 * which should be filtered out by the receiver. */
257 log_device_info(device
, "Sending device syspath:%s", syspath
);
258 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, device
) >= 0);
259 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client
)) == 100);
263 static void test_sd_device_monitor_filter_remove(sd_device
*device
) {
264 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor_server
= NULL
, *monitor_client
= NULL
;
267 log_device_info(device
, "/* %s */", __func__
);
269 assert_se(sd_device_get_syspath(device
, &syspath
) >= 0);
271 assert_se(device_monitor_new_full(&monitor_server
, MONITOR_GROUP_NONE
, -1) >= 0);
272 assert_se(sd_device_monitor_set_description(monitor_server
, "sender") >= 0);
273 assert_se(sd_device_monitor_start(monitor_server
, NULL
, NULL
) >= 0);
275 assert_se(device_monitor_new_full(&monitor_client
, MONITOR_GROUP_NONE
, -1) >= 0);
276 assert_se(sd_device_monitor_set_description(monitor_client
, "receiver") >= 0);
277 assert_se(device_monitor_allow_unicast_sender(monitor_client
, monitor_server
) >= 0);
278 assert_se(sd_device_monitor_start(monitor_client
, monitor_handler
, (void *) syspath
) >= 0);
280 assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client
, "hoge", NULL
) >= 0);
281 assert_se(sd_device_monitor_filter_update(monitor_client
) >= 0);
283 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, device
) >= 0);
284 assert_se(sd_event_run(sd_device_monitor_get_event(monitor_client
), 0) >= 0);
286 assert_se(sd_device_monitor_filter_remove(monitor_client
) >= 0);
288 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, device
) >= 0);
289 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client
)) == 100);
292 int main(int argc
, char *argv
[]) {
293 _cleanup_(sd_device_unrefp
) sd_device
*loopback
= NULL
, *sda
= NULL
;
296 test_setup_logging(LOG_INFO
);
299 return log_tests_skipped("not root");
301 if (path_is_read_only_fs("/sys") > 0)
302 return log_tests_skipped("Running in container");
304 test_receive_device_fail();
306 assert_se(sd_device_new_from_syspath(&loopback
, "/sys/class/net/lo") >= 0);
307 assert_se(device_add_property(loopback
, "ACTION", "add") >= 0);
308 assert_se(device_add_property(loopback
, "SEQNUM", "10") >= 0);
309 assert_se(device_add_tag(loopback
, "TEST_SD_DEVICE_MONITOR", true) >= 0);
311 test_send_receive_one(loopback
, false, false, false);
312 test_send_receive_one(loopback
, true, false, false);
313 test_send_receive_one(loopback
, false, true, false);
314 test_send_receive_one(loopback
, true, true, false);
315 test_send_receive_one(loopback
, true, false, true);
316 test_send_receive_one(loopback
, false, true, true);
317 test_send_receive_one(loopback
, true, true, true);
319 test_subsystem_filter(loopback
);
320 test_tag_filter(loopback
);
321 test_sysattr_filter(loopback
, "ifindex");
322 test_sd_device_monitor_filter_remove(loopback
);
324 r
= sd_device_new_from_subsystem_sysname(&sda
, "block", "sda");
326 log_info_errno(r
, "Failed to create sd_device for sda, skipping remaining tests: %m");
330 assert_se(device_add_property(sda
, "ACTION", "change") >= 0);
331 assert_se(device_add_property(sda
, "SEQNUM", "11") >= 0);
333 test_send_receive_one(sda
, false, false, false);
334 test_send_receive_one(sda
, true, false, false);
335 test_send_receive_one(sda
, false, true, false);
336 test_send_receive_one(sda
, true, true, false);
337 test_send_receive_one(sda
, true, false, true);
338 test_send_receive_one(sda
, false, true, true);
339 test_send_receive_one(sda
, true, true, true);
341 test_parent_filter(sda
);