1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "device-monitor-private.h"
10 #include "device-private.h"
11 #include "device-util.h"
13 #include "string-util.h"
18 static int monitor_handler(sd_device_monitor
*m
, sd_device
*d
, void *userdata
) {
19 const char *s
, *syspath
= userdata
;
21 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
22 assert_se(streq(s
, syspath
));
24 return sd_event_exit(sd_device_monitor_get_event(m
), 0);
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
;
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
));
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);
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
, NULL
) >= 0);
44 assert_se(device_monitor_new_full(&monitor_client
, MONITOR_GROUP_NONE
, -1) >= 0);
45 assert_se(device_monitor_allow_unicast_sender(monitor_client
, monitor_server
) >= 0);
46 assert_se(sd_device_monitor_start(monitor_client
, monitor_handler
, (void *) syspath
, "loopback-monitor") >= 0);
48 if (subsystem_filter
) {
49 assert_se(sd_device_get_subsystem(loopback
, &subsystem
) >= 0);
50 assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client
, subsystem
, NULL
) >= 0);
54 FOREACH_DEVICE_TAG(loopback
, tag
)
55 assert_se(sd_device_monitor_filter_add_match_tag(monitor_client
, tag
) >= 0);
57 if ((subsystem_filter
|| tag_filter
) && use_bpf
)
58 assert_se(sd_device_monitor_filter_update(monitor_client
) >= 0);
60 r
= device_monitor_send_device(monitor_server
, monitor_client
, loopback
);
62 return log_error_errno(r
, "Failed to send loopback device: %m");
64 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client
)) == 0);
69 static void test_subsystem_filter(void) {
70 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor_server
= NULL
, *monitor_client
= NULL
;
71 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
72 _cleanup_(sd_device_unrefp
) sd_device
*loopback
= NULL
;
73 const char *syspath
, *subsystem
, *p
, *s
;
76 log_info("/* %s */", __func__
);
78 assert_se(sd_device_new_from_syspath(&loopback
, "/sys/class/net/lo") >= 0);
79 assert_se(sd_device_get_syspath(loopback
, &syspath
) >= 0);
80 assert_se(sd_device_get_subsystem(loopback
, &subsystem
) >= 0);
81 assert_se(device_add_property(loopback
, "ACTION", "add") >= 0);
82 assert_se(device_add_property(loopback
, "SEQNUM", "10") >= 0);
84 assert_se(device_monitor_new_full(&monitor_server
, MONITOR_GROUP_NONE
, -1) >= 0);
85 assert_se(sd_device_monitor_start(monitor_server
, NULL
, NULL
, NULL
) >= 0);
87 assert_se(device_monitor_new_full(&monitor_client
, MONITOR_GROUP_NONE
, -1) >= 0);
88 assert_se(device_monitor_allow_unicast_sender(monitor_client
, monitor_server
) >= 0);
89 assert_se(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client
, subsystem
, NULL
) >= 0);
90 assert_se(sd_device_monitor_start(monitor_client
, monitor_handler
, (void *) syspath
, "subsystem-filter") >= 0);
92 assert_se(sd_device_enumerator_new(&e
) >= 0);
93 assert_se(sd_device_enumerator_add_match_subsystem(e
, subsystem
, false) >= 0);
94 FOREACH_DEVICE(e
, d
) {
95 assert_se(sd_device_get_syspath(d
, &p
) >= 0);
96 assert_se(sd_device_get_subsystem(d
, &s
) >= 0);
98 log_info("Sending device subsystem:%s syspath:%s", s
, p
);
99 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, d
) >= 0);
102 log_info("Sending device subsystem:%s syspath:%s", subsystem
, syspath
);
103 assert_se(device_monitor_send_device(monitor_server
, monitor_client
, loopback
) >= 0);
104 assert_se(sd_event_loop(sd_device_monitor_get_event(monitor_client
)) == 0);
107 int main(int argc
, char *argv
[]) {
110 test_setup_logging(LOG_INFO
);
113 return log_tests_skipped("not root");
115 r
= test_loopback(false, false, false);
117 assert_se(r
== -EPERM
&& detect_container() > 0);
118 return log_tests_skipped("Running in container? Skipping remaining tests");
121 assert_se(test_loopback( true, false, false) >= 0);
122 assert_se(test_loopback(false, true, false) >= 0);
123 assert_se(test_loopback( true, true, false) >= 0);
124 assert_se(test_loopback( true, false, true) >= 0);
125 assert_se(test_loopback(false, true, true) >= 0);
126 assert_se(test_loopback( true, true, true) >= 0);
128 test_subsystem_filter();