]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/test-sd-device-monitor.c
device-util: Declare iterator variables inline
[thirdparty/systemd.git] / src / libsystemd / sd-device / test-sd-device-monitor.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 "path-util.h"
14 #include "stat-util.h"
15 #include "string-util.h"
16 #include "tests.h"
17 #include "virt.h"
18
19 static int monitor_handler(sd_device_monitor *m, sd_device *d, void *userdata) {
20 const char *s, *syspath = userdata;
21
22 assert_se(sd_device_get_syspath(d, &s) >= 0);
23 assert_se(streq(s, syspath));
24
25 return sd_event_exit(sd_device_monitor_get_event(m), 100);
26 }
27
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;
31 const char *syspath;
32
33 log_info("/* %s */", __func__);
34
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);
38
39 assert_se(sd_device_get_syspath(loopback, &syspath) >= 0);
40
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);
44
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);
49
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);
52 }
53
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;
57
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));
60
61 assert_se(sd_device_get_syspath(device, &syspath) >= 0);
62
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);
66
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);
71
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);
76 }
77
78 if (tag_filter)
79 FOREACH_DEVICE_TAG(device, tag)
80 assert_se(sd_device_monitor_filter_add_match_tag(monitor_client, tag) >= 0);
81
82 if ((subsystem_filter || tag_filter) && use_bpf)
83 assert_se(sd_device_monitor_filter_update(monitor_client) >= 0);
84
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);
87 }
88
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;
93
94 log_device_info(device, "/* %s */", __func__);
95
96 assert_se(sd_device_get_syspath(device, &syspath) >= 0);
97 assert_se(sd_device_get_subsystem(device, &subsystem) >= 0);
98
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);
102
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);
108
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) {
112 const char *p, *s;
113
114 assert_se(sd_device_get_syspath(d, &p) >= 0);
115 assert_se(sd_device_get_subsystem(d, &s) >= 0);
116
117 assert_se(device_add_property(d, "ACTION", "add") >= 0);
118 assert_se(device_add_property(d, "SEQNUM", "10") >= 0);
119
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);
122 }
123
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);
127 }
128
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;
132 const char *syspath;
133
134 log_device_info(device, "/* %s */", __func__);
135
136 assert_se(sd_device_get_syspath(device, &syspath) >= 0);
137
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);
141
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);
147
148 assert_se(sd_device_enumerator_new(&e) >= 0);
149 FOREACH_DEVICE(e, d) {
150 const char *p;
151
152 assert_se(sd_device_get_syspath(d, &p) >= 0);
153
154 assert_se(device_add_property(d, "ACTION", "add") >= 0);
155 assert_se(device_add_property(d, "SEQNUM", "10") >= 0);
156
157 log_device_debug(d, "Sending device syspath:%s", p);
158 assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0);
159 }
160
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);
164
165 }
166
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;
171
172 log_device_info(device, "/* %s(%s) */", __func__, sysattr);
173
174 assert_se(sd_device_get_syspath(device, &syspath) >= 0);
175 assert_se(sd_device_get_sysattr_value(device, sysattr, &sysattr_value) >= 0);
176
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);
180
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);
186
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) {
190 const char *p;
191
192 assert_se(sd_device_get_syspath(d, &p) >= 0);
193
194 assert_se(device_add_property(d, "ACTION", "add") >= 0);
195 assert_se(device_add_property(d, "SEQNUM", "10") >= 0);
196
197 log_device_debug(d, "Sending device syspath:%s", p);
198 assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0);
199
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. */
203 break;
204 }
205
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);
209
210 }
211
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;
216 sd_device *parent;
217 int r;
218
219 log_device_info(device, "/* %s */", __func__);
220
221 assert_se(sd_device_get_syspath(device, &syspath) >= 0);
222 r = sd_device_get_parent(device, &parent);
223 if (r < 0)
224 return (void) log_device_info(device, "Device does not have parent, skipping.");
225 assert_se(sd_device_get_syspath(parent, &parent_syspath) >= 0);
226
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);
230
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);
236
237 assert_se(sd_device_enumerator_new(&e) >= 0);
238 FOREACH_DEVICE(e, d) {
239 const char *p;
240
241 assert_se(sd_device_get_syspath(d, &p) >= 0);
242 if (path_startswith(p, parent_syspath))
243 continue;
244
245 assert_se(device_add_property(d, "ACTION", "add") >= 0);
246 assert_se(device_add_property(d, "SEQNUM", "10") >= 0);
247
248 log_device_debug(d, "Sending device syspath:%s", p);
249 assert_se(device_monitor_send_device(monitor_server, monitor_client, d) >= 0);
250
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. */
254 break;
255 }
256
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);
260
261 }
262
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;
265 const char *syspath;
266
267 log_device_info(device, "/* %s */", __func__);
268
269 assert_se(sd_device_get_syspath(device, &syspath) >= 0);
270
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);
274
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);
279
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);
282
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);
285
286 assert_se(sd_device_monitor_filter_remove(monitor_client) >= 0);
287
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);
290 }
291
292 int main(int argc, char *argv[]) {
293 _cleanup_(sd_device_unrefp) sd_device *loopback = NULL, *sda = NULL;
294 int r;
295
296 test_setup_logging(LOG_INFO);
297
298 if (getuid() != 0)
299 return log_tests_skipped("not root");
300
301 if (path_is_read_only_fs("/sys") > 0)
302 return log_tests_skipped("Running in container");
303
304 test_receive_device_fail();
305
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);
310
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);
318
319 test_subsystem_filter(loopback);
320 test_tag_filter(loopback);
321 test_sysattr_filter(loopback, "ifindex");
322 test_sd_device_monitor_filter_remove(loopback);
323
324 r = sd_device_new_from_subsystem_sysname(&sda, "block", "sda");
325 if (r < 0) {
326 log_info_errno(r, "Failed to create sd_device for sda, skipping remaining tests: %m");
327 return 0;
328 }
329
330 assert_se(device_add_property(sda, "ACTION", "change") >= 0);
331 assert_se(device_add_property(sda, "SEQNUM", "11") >= 0);
332
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);
340
341 test_parent_filter(sda);
342
343 return 0;
344 }