Debian Codegraph @

src/libsystemd/sd-device/test-sd-device-monitor.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-device.h"
6 #include "sd-event.h"
7
8 #include "device-monitor-private.h"
9 #include "device-private.h"
10 #include "device-util.h"
11 #include "io-util.h"
12 #include "mountpoint-util.h"
13 #include "path-util.h"
14 #include "socket-util.h"
15 #include "stat-util.h"
16 #include "string-util.h"
17 #include "tests.h"
18 #include "time-util.h"
19
20 static void prepare_loopback(sd_device **ret) {
21 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
22
23 assert(ret);
24
25 ASSERT_OK(sd_device_new_from_syspath(&dev, "/sys/class/net/lo"));
26 ASSERT_OK(device_add_property(dev, "ACTION", "add"));
27 ASSERT_OK(device_add_property(dev, "SEQNUM", "10"));
28 ASSERT_OK(device_add_tag(dev, "TEST_SD_DEVICE_MONITOR", true));
29
30 *ret = TAKE_PTR(dev);
31 }
32
33 static int prepare_sda(sd_device **ret) {
34 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
35 int r;
36
37 assert(ret);
38
39 r = sd_device_new_from_subsystem_sysname(&dev, "block", "sda");
40 if (r < 0)
41 return r;
42
43 ASSERT_OK(device_add_property(dev, "ACTION", "change"));
44 ASSERT_OK(device_add_property(dev, "SEQNUM", "11"));
45
46 *ret = TAKE_PTR(dev);
47 return 0;
48 }
49
50 static int monitor_handler(sd_device_monitor *m, sd_device *d, void *userdata) {
51 const char *s, *syspath = userdata;
52
53 ASSERT_OK(sd_device_get_syspath(d, &s));
54 ASSERT_STREQ(s, syspath);
55
56 return sd_event_exit(sd_device_monitor_get_event(m), 100);
57 }
58
59 static void prepare_monitor(sd_device_monitor **ret_server, sd_device_monitor **ret_client, union sockaddr_union *ret_address) {
60 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
61
62 assert(ret_server);
63 assert(ret_client);
64
65 ASSERT_OK(device_monitor_new_full(&monitor_server, MONITOR_GROUP_NONE, -EBADF));
66 ASSERT_OK(sd_device_monitor_set_description(monitor_server, "sender"));
67 ASSERT_OK(sd_device_monitor_start(monitor_server, NULL, NULL));
68
69 ASSERT_OK(device_monitor_new_full(&monitor_client, MONITOR_GROUP_NONE, -EBADF));
70 ASSERT_OK(sd_device_monitor_set_description(monitor_client, "client"));
71 ASSERT_OK(device_monitor_allow_unicast_sender(monitor_client, monitor_server));
72 ASSERT_OK(device_monitor_get_address(monitor_client, ret_address));
73
74 *ret_server = TAKE_PTR(monitor_server);
75 *ret_client = TAKE_PTR(monitor_client);
76 }
77
78 static void send_by_enumerator(
79 sd_device_monitor *monitor_server,
80 const union sockaddr_union *address,
81 sd_device_enumerator *e,
82 size_t n,
83 const char *syspath_filter) {
84
85 size_t i = 0;
86
87 FOREACH_DEVICE(e, d) {
88 const char *p, *s;
89
90 ASSERT_OK(sd_device_get_syspath(d, &p));
91 ASSERT_OK(sd_device_get_subsystem(d, &s));
92
93 if (syspath_filter && path_startswith(p, syspath_filter))
94 continue;
95
96 ASSERT_OK(device_add_property(d, "ACTION", "add"));
97 ASSERT_OK(device_add_property(d, "SEQNUM", "10"));
98
99 log_device_debug(d, "Sending device subsystem:%s syspath:%s", s, p);
100 ASSERT_OK(device_monitor_send(monitor_server, address, d));
101
102 /* The sysattr and parent filters are not implemented in BPF yet. So, sending multiple
103 * devices may fills up buffer and device_monitor_send() may return EAGAIN. Let's send only a
104 * few devices here, which should be filtered out by the receiver. */
105 if (n != SIZE_MAX && ++i >= n)
106 break;
107 }
108 }
109
110 TEST(sd_device_monitor_is_running) {
111 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *m = NULL;
112
113 ASSERT_OK_ZERO(sd_device_monitor_is_running(NULL));
114
115 ASSERT_OK(device_monitor_new_full(&m, MONITOR_GROUP_NONE, -EBADF));
116 ASSERT_OK_ZERO(sd_device_monitor_is_running(m));
117 ASSERT_OK(sd_device_monitor_start(m, NULL, NULL));
118 ASSERT_OK_POSITIVE(sd_device_monitor_is_running(m));
119 ASSERT_OK(sd_device_monitor_stop(m));
120 ASSERT_OK_ZERO(sd_device_monitor_is_running(m));
121 }
122
123 TEST(sd_device_monitor_start_stop) {
124 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
125 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
126 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
127 union sockaddr_union sa;
128 const char *syspath;
129
130 prepare_loopback(&device);
131
132 ASSERT_OK(sd_device_get_syspath(device, &syspath));
133
134 prepare_monitor(&monitor_server, &monitor_client, &sa);
135
136 /* Sending devices before starting client. */
137 ASSERT_OK(sd_device_enumerator_new(&e));
138 send_by_enumerator(monitor_server, &sa, e, 5, syspath);
139
140 /* sd_device_monitor_start() can be called multiple times. */
141 ASSERT_OK(sd_device_monitor_start(monitor_client, NULL, NULL));
142 ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath));
143
144 /* Sending devices after client being started. */
145 send_by_enumerator(monitor_server, &sa, e, 5, syspath);
146
147 /* sd_device_monitor_stop() can be called multiple times. */
148 ASSERT_OK(sd_device_monitor_stop(monitor_client));
149 ASSERT_OK(sd_device_monitor_stop(monitor_client));
150
151 /* Sending devices before restarting client. */
152 send_by_enumerator(monitor_server, &sa, e, 5, syspath);
153
154 /* Restart monitor, and check if the previously sent devices are ignored. */
155 ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath));
156 ASSERT_OK(device_monitor_send(monitor_server, &sa, device));
157 ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100);
158 }
159
160 TEST(refuse_invalid_device) {
161 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
162 _cleanup_(sd_device_unrefp) sd_device *loopback = NULL;
163 union sockaddr_union sa;
164 const char *syspath;
165
166 /* Try to send device with invalid action and without seqnum. */
167 ASSERT_OK(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo"));
168 ASSERT_OK(device_add_property(loopback, "ACTION", "hoge"));
169
170 ASSERT_OK(sd_device_get_syspath(loopback, &syspath));
171
172 prepare_monitor(&monitor_server, &monitor_client, &sa);
173
174 ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath));
175 ASSERT_OK(device_monitor_send(monitor_server, &sa, loopback));
176 ASSERT_OK(sd_event_run(sd_device_monitor_get_event(monitor_client), 0));
177 }
178
179 static void test_send_receive_one(sd_device *device, bool subsystem_filter, bool tag_filter, bool use_bpf) {
180 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
181 const char *syspath, *subsystem, *devtype = NULL;
182 union sockaddr_union sa;
183
184 log_device_info(device, "/* %s(subsystem_filter=%s, tag_filter=%s, use_bpf=%s) */", __func__,
185 true_false(subsystem_filter), true_false(tag_filter), true_false(use_bpf));
186
187 ASSERT_OK(sd_device_get_syspath(device, &syspath));
188
189 prepare_monitor(&monitor_server, &monitor_client, &sa);
190
191 if (subsystem_filter) {
192 ASSERT_OK(sd_device_get_subsystem(device, &subsystem));
193 (void) sd_device_get_devtype(device, &devtype);
194 ASSERT_OK(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, devtype));
195 }
196
197 if (tag_filter)
198 FOREACH_DEVICE_TAG(device, tag)
199 ASSERT_OK(sd_device_monitor_filter_add_match_tag(monitor_client, tag));
200
201 if ((subsystem_filter || tag_filter) && use_bpf)
202 ASSERT_OK(sd_device_monitor_filter_update(monitor_client));
203
204 ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath));
205 ASSERT_OK(device_monitor_send(monitor_server, &sa, device));
206 ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100);
207 }
208
209 TEST(sd_device_monitor_send_receive) {
210 _cleanup_(sd_device_unrefp) sd_device *loopback = NULL, *sda = NULL;
211 int r;
212
213 prepare_loopback(&loopback);
214 test_send_receive_one(loopback, false, false, false);
215 test_send_receive_one(loopback, true, false, false);
216 test_send_receive_one(loopback, false, true, false);
217 test_send_receive_one(loopback, true, true, false);
218 test_send_receive_one(loopback, true, false, true);
219 test_send_receive_one(loopback, false, true, true);
220 test_send_receive_one(loopback, true, true, true);
221
222 r = prepare_sda(&sda);
223 if (r < 0)
224 return (void) log_tests_skipped_errno(r, "Failed to create sd_device for sda");
225
226 test_send_receive_one(sda, false, false, false);
227 test_send_receive_one(sda, true, false, false);
228 test_send_receive_one(sda, false, true, false);
229 test_send_receive_one(sda, true, true, false);
230 test_send_receive_one(sda, true, false, true);
231 test_send_receive_one(sda, false, true, true);
232 test_send_receive_one(sda, true, true, true);
233 }
234
235 TEST(sd_device_monitor_filter_add_match_subsystem_devtype) {
236 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
237 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
238 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
239 const char *syspath, *subsystem;
240 union sockaddr_union sa;
241
242 prepare_loopback(&device);
243
244 ASSERT_OK(sd_device_get_syspath(device, &syspath));
245 ASSERT_OK(sd_device_get_subsystem(device, &subsystem));
246
247 prepare_monitor(&monitor_server, &monitor_client, &sa);
248
249 ASSERT_OK(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, subsystem, NULL));
250 ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath));
251
252 ASSERT_OK(sd_device_enumerator_new(&e));
253 ASSERT_OK(sd_device_enumerator_add_match_subsystem(e, subsystem, false));
254 send_by_enumerator(monitor_server, &sa, e, SIZE_MAX, NULL);
255
256 log_device_info(device, "Sending device subsystem:%s syspath:%s", subsystem, syspath);
257 ASSERT_OK(device_monitor_send(monitor_server, &sa, device));
258 ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100);
259 }
260
261 TEST(sd_device_monitor_filter_add_match_tag) {
262 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
263 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
264 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
265 union sockaddr_union sa;
266 const char *syspath;
267
268 prepare_loopback(&device);
269
270 ASSERT_OK(sd_device_get_syspath(device, &syspath));
271
272 prepare_monitor(&monitor_server, &monitor_client, &sa);
273
274 ASSERT_OK(sd_device_monitor_filter_add_match_tag(monitor_client, "TEST_SD_DEVICE_MONITOR"));
275 ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath));
276
277 ASSERT_OK(sd_device_enumerator_new(&e));
278 send_by_enumerator(monitor_server, &sa, e, SIZE_MAX, NULL);
279
280 log_device_info(device, "Sending device syspath:%s", syspath);
281 ASSERT_OK(device_monitor_send(monitor_server, &sa, device));
282 ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100);
283 }
284
285 TEST(sd_device_monitor_filter_add_match_sysattr) {
286 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
287 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
288 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
289 static const char *sysattr = "ifindex";
290 const char *syspath, *sysattr_value;
291 union sockaddr_union sa;
292
293 prepare_loopback(&device);
294
295 ASSERT_OK(sd_device_get_syspath(device, &syspath));
296 ASSERT_OK(sd_device_get_sysattr_value(device, sysattr, &sysattr_value));
297
298 prepare_monitor(&monitor_server, &monitor_client, &sa);
299
300 ASSERT_OK(sd_device_monitor_filter_add_match_sysattr(monitor_client, sysattr, sysattr_value, true));
301 ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath));
302
303 ASSERT_OK(sd_device_enumerator_new(&e));
304 ASSERT_OK(sd_device_enumerator_add_match_sysattr(e, sysattr, sysattr_value, false));
305 send_by_enumerator(monitor_server, &sa, e, 5, NULL);
306
307 log_device_info(device, "Sending device syspath:%s", syspath);
308 ASSERT_OK(device_monitor_send(monitor_server, &sa, device));
309 ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100);
310 }
311
312 TEST(sd_device_monitor_add_match_parent) {
313 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
314 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
315 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
316 const char *syspath, *parent_syspath;
317 union sockaddr_union sa;
318 sd_device *parent;
319 int r;
320
321 r = prepare_sda(&device);
322 if (r < 0)
323 return (void) log_tests_skipped_errno(r, "Failed to create sd_device for sda");
324
325 ASSERT_OK(sd_device_get_syspath(device, &syspath));
326
327 r = sd_device_get_parent(device, &parent);
328 if (r < 0)
329 return (void) log_tests_skipped("sda does not have parent");
330
331 ASSERT_OK(sd_device_get_syspath(parent, &parent_syspath));
332
333 prepare_monitor(&monitor_server, &monitor_client, &sa);
334
335 ASSERT_OK(sd_device_monitor_filter_add_match_parent(monitor_client, parent, true));
336 ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath));
337
338 ASSERT_OK(sd_device_enumerator_new(&e));
339 send_by_enumerator(monitor_server, &sa, e, 5, parent_syspath);
340
341 log_device_info(device, "Sending device syspath:%s", syspath);
342 ASSERT_OK(device_monitor_send(monitor_server, &sa, device));
343 ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100);
344 }
345
346 TEST(sd_device_monitor_filter_remove) {
347 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
348 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
349 union sockaddr_union sa;
350 const char *syspath;
351
352 prepare_loopback(&device);
353
354 ASSERT_OK(sd_device_get_syspath(device, &syspath));
355
356 prepare_monitor(&monitor_server, &monitor_client, &sa);
357
358 ASSERT_OK(sd_device_monitor_filter_add_match_subsystem_devtype(monitor_client, "hoge", NULL));
359 ASSERT_OK(sd_device_monitor_start(monitor_client, monitor_handler, (void *) syspath));
360
361 ASSERT_OK(device_monitor_send(monitor_server, &sa, device));
362 ASSERT_OK(sd_event_run(sd_device_monitor_get_event(monitor_client), 0));
363
364 ASSERT_OK(sd_device_monitor_filter_remove(monitor_client));
365
366 ASSERT_OK(device_monitor_send(monitor_server, &sa, device));
367 ASSERT_EQ(sd_event_loop(sd_device_monitor_get_event(monitor_client)), 100);
368 }
369
370 TEST(sd_device_monitor_receive) {
371 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL;
372 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
373 union sockaddr_union sa;
374 const char *syspath;
375 int fd, r;
376
377 prepare_loopback(&device);
378
379 ASSERT_OK(sd_device_get_syspath(device, &syspath));
380
381 prepare_monitor(&monitor_server, &monitor_client, &sa);
382
383 ASSERT_OK(device_monitor_send(monitor_server, &sa, device));
384
385 ASSERT_OK(fd = sd_device_monitor_get_fd(monitor_client));
386
387 for (;;) {
388 usec_t timeout;
389 int events;
390
391 ASSERT_OK(events = sd_device_monitor_get_events(monitor_client));
392 ASSERT_EQ(events, (int) EPOLLIN);
393 ASSERT_OK(sd_device_monitor_get_timeout(monitor_client, &timeout));
394 ASSERT_EQ(timeout, USEC_INFINITY);
395
396 r = fd_wait_for_event(fd, events, MAX(10 * USEC_PER_SEC, timeout));
397 if (r == -EINTR)
398 continue;
399 ASSERT_OK_POSITIVE(r);
400 break;
401 }
402
403 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
404 ASSERT_OK_POSITIVE(sd_device_monitor_receive(monitor_client, &dev));
405
406 const char *s;
407 ASSERT_OK(sd_device_get_syspath(dev, &s));
408 ASSERT_STREQ(s, syspath);
409 }
410
411 static int intro(void) {
412 if (getuid() != 0)
413 return log_tests_skipped("not root");
414
415 if (path_is_mount_point("/sys") <= 0)
416 return log_tests_skipped("/sys is not mounted");
417
418 if (path_is_read_only_fs("/sys") > 0)
419 return log_tests_skipped("Running in container");
420
421 if (access("/sys/class/net/lo", F_OK) < 0)
422 return log_tests_skipped_errno(errno, "Loopback network interface 'lo' does not exist");
423
424 return EXIT_SUCCESS;
425 }
426
427 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);