]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/test-sd-device.c
Merge pull request #24456 from yuwata/network-tcp-congctl
[thirdparty/systemd.git] / src / libsystemd / sd-device / test-sd-device.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <ctype.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6
7 #include "device-enumerator-private.h"
8 #include "device-internal.h"
9 #include "device-private.h"
10 #include "device-util.h"
11 #include "errno-util.h"
12 #include "fd-util.h"
13 #include "hashmap.h"
14 #include "nulstr-util.h"
15 #include "path-util.h"
16 #include "rm-rf.h"
17 #include "string-util.h"
18 #include "tests.h"
19 #include "time-util.h"
20 #include "tmpfile-util.h"
21 #include "udev-util.h"
22
23 static void test_sd_device_one(sd_device *d) {
24 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
25 const char *syspath, *sysname, *subsystem = NULL, *devname, *val;
26 bool is_block = false;
27 dev_t devnum;
28 usec_t usec;
29 int ifindex, r;
30
31 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
32 assert_se(path_startswith(syspath, "/sys"));
33 assert_se(sd_device_get_sysname(d, &sysname) >= 0);
34
35 log_info("%s(%s)", __func__, syspath);
36
37 assert_se(sd_device_new_from_syspath(&dev, syspath) >= 0);
38 assert_se(sd_device_get_syspath(dev, &val) >= 0);
39 assert_se(streq(syspath, val));
40 dev = sd_device_unref(dev);
41
42 assert_se(sd_device_new_from_path(&dev, syspath) >= 0);
43 assert_se(sd_device_get_syspath(dev, &val) >= 0);
44 assert_se(streq(syspath, val));
45 dev = sd_device_unref(dev);
46
47 r = sd_device_get_ifindex(d, &ifindex);
48 if (r >= 0) {
49 assert_se(ifindex > 0);
50
51 r = sd_device_new_from_ifindex(&dev, ifindex);
52 if (r == -ENODEV)
53 log_device_warning_errno(d, r,
54 "Failed to create sd-device object from ifindex %i. "
55 "Maybe running on a non-host network namespace.", ifindex);
56 else {
57 assert_se(r >= 0);
58 assert_se(sd_device_get_syspath(dev, &val) >= 0);
59 assert_se(streq(syspath, val));
60 dev = sd_device_unref(dev);
61 }
62
63 /* This does not require the interface really exists on the network namespace.
64 * Hence, this should always succeed. */
65 assert_se(sd_device_new_from_ifname(&dev, sysname) >= 0);
66 assert_se(sd_device_get_syspath(dev, &val) >= 0);
67 assert_se(streq(syspath, val));
68 dev = sd_device_unref(dev);
69 } else
70 assert_se(r == -ENOENT);
71
72 r = sd_device_get_subsystem(d, &subsystem);
73 if (r < 0)
74 assert_se(r == -ENOENT);
75 else if (!streq(subsystem, "gpio")) { /* Unfortunately, there exist /sys/class/gpio and /sys/bus/gpio.
76 * Hence, sd_device_new_from_subsystem_sysname() and
77 * sd_device_new_from_device_id() may not work as expected. */
78 const char *name, *id;
79
80 if (streq(subsystem, "drivers"))
81 name = strjoina(d->driver_subsystem, ":", sysname);
82 else
83 name = sysname;
84 assert_se(sd_device_new_from_subsystem_sysname(&dev, subsystem, name) >= 0);
85 assert_se(sd_device_get_syspath(dev, &val) >= 0);
86 assert_se(streq(syspath, val));
87 dev = sd_device_unref(dev);
88
89 /* The device ID depends on subsystem. */
90 assert_se(device_get_device_id(d, &id) >= 0);
91 r = sd_device_new_from_device_id(&dev, id);
92 if (r == -ENODEV && ifindex > 0)
93 log_device_warning_errno(d, r,
94 "Failed to create sd-device object from device ID \"%s\". "
95 "Maybe running on a non-host network namespace.", id);
96 else {
97 assert_se(r >= 0);
98 assert_se(sd_device_get_syspath(dev, &val) >= 0);
99 assert_se(streq(syspath, val));
100 dev = sd_device_unref(dev);
101 }
102
103 /* These require udev database, and reading database requires device ID. */
104 r = sd_device_get_is_initialized(d);
105 if (r > 0) {
106 r = sd_device_get_usec_since_initialized(d, &usec);
107 assert_se((r >= 0 && usec > 0) || r == -ENODATA);
108 } else
109 assert(r == 0);
110
111 r = sd_device_get_property_value(d, "ID_NET_DRIVER", &val);
112 assert_se(r >= 0 || r == -ENOENT);
113 }
114
115 is_block = streq_ptr(subsystem, "block");
116
117 r = sd_device_get_devname(d, &devname);
118 if (r >= 0) {
119 r = sd_device_new_from_devname(&dev, devname);
120 if (r >= 0) {
121 assert_se(sd_device_get_syspath(dev, &val) >= 0);
122 assert_se(streq(syspath, val));
123 dev = sd_device_unref(dev);
124 } else
125 assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
126
127 r = sd_device_new_from_path(&dev, devname);
128 if (r >= 0) {
129 assert_se(sd_device_get_syspath(dev, &val) >= 0);
130 assert_se(streq(syspath, val));
131 dev = sd_device_unref(dev);
132
133 _cleanup_close_ int fd = -1;
134 fd = sd_device_open(d, O_CLOEXEC| O_NONBLOCK | (is_block ? O_RDONLY : O_NOCTTY | O_PATH));
135 assert_se(fd >= 0 || ERRNO_IS_PRIVILEGE(fd));
136 } else
137 assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
138 } else
139 assert_se(r == -ENOENT);
140
141 r = sd_device_get_devnum(d, &devnum);
142 if (r >= 0) {
143 _cleanup_free_ char *p = NULL;
144
145 assert_se(major(devnum) > 0);
146
147 assert_se(sd_device_new_from_devnum(&dev, is_block ? 'b' : 'c', devnum) >= 0);
148 assert_se(sd_device_get_syspath(dev, &val) >= 0);
149 assert_se(streq(syspath, val));
150 dev = sd_device_unref(dev);
151
152 assert_se(asprintf(&p, "/dev/%s/%u:%u", is_block ? "block" : "char", major(devnum), minor(devnum)) >= 0);
153 assert_se(sd_device_new_from_devname(&dev, p) >= 0);
154 assert_se(sd_device_get_syspath(dev, &val) >= 0);
155 assert_se(streq(syspath, val));
156 dev = sd_device_unref(dev);
157
158 assert_se(sd_device_new_from_path(&dev, p) >= 0);
159 assert_se(sd_device_get_syspath(dev, &val) >= 0);
160 assert_se(streq(syspath, val));
161 dev = sd_device_unref(dev);
162 } else
163 assert_se(r == -ENOENT);
164
165 assert_se(sd_device_get_devpath(d, &val) >= 0);
166
167 r = sd_device_get_devtype(d, &val);
168 assert_se(r >= 0 || r == -ENOENT);
169
170 r = sd_device_get_driver(d, &val);
171 assert_se(r >= 0 || r == -ENOENT);
172
173 r = sd_device_get_sysnum(d, &val);
174 if (r >= 0) {
175 assert_se(val > sysname);
176 assert_se(val < sysname + strlen(sysname));
177 assert_se(in_charset(val, DIGITS));
178 assert_se(!ascii_isdigit(val[-1]));
179 } else
180 assert_se(r == -ENOENT);
181
182 r = sd_device_get_sysattr_value(d, "name_assign_type", &val);
183 assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || IN_SET(r, -ENOENT, -EINVAL));
184
185 if (r > 0) {
186 unsigned x;
187
188 assert_se(device_get_sysattr_unsigned(d, "name_assign_type", NULL) >= 0);
189 assert_se(device_get_sysattr_unsigned(d, "name_assign_type", &x) >= 0);
190 }
191 }
192
193 TEST(sd_device_enumerator_devices) {
194 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
195 sd_device *d;
196
197 assert_se(sd_device_enumerator_new(&e) >= 0);
198 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
199 /* On some CI environments, it seems some loop block devices and corresponding bdi devices sometimes
200 * disappear during running this test. Let's exclude them here for stability. */
201 assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
202 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
203 /* On CentOS CI, systemd-networkd-tests.py may be running when this test is invoked. The networkd
204 * test creates and removes many network interfaces, and may interfere with this test. */
205 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0);
206 FOREACH_DEVICE(e, d)
207 test_sd_device_one(d);
208 }
209
210 TEST(sd_device_enumerator_subsystems) {
211 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
212 sd_device *d;
213
214 assert_se(sd_device_enumerator_new(&e) >= 0);
215 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
216 FOREACH_SUBSYSTEM(e, d)
217 test_sd_device_one(d);
218 }
219
220 static void test_sd_device_enumerator_filter_subsystem_one(
221 const char *subsystem,
222 Hashmap *h,
223 unsigned *ret_n_new_dev,
224 unsigned *ret_n_removed_dev) {
225
226 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
227 unsigned n_new_dev = 0, n_removed_dev = 0;
228 sd_device *d;
229
230 assert_se(sd_device_enumerator_new(&e) >= 0);
231 assert_se(sd_device_enumerator_add_match_subsystem(e, subsystem, true) >= 0);
232 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
233
234 FOREACH_DEVICE(e, d) {
235 const char *syspath;
236 sd_device *t;
237
238 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
239 t = hashmap_remove(h, syspath);
240
241 if (!t) {
242 log_warning("New device found: subsystem:%s syspath:%s", subsystem, syspath);
243 n_new_dev++;
244 }
245
246 assert_se(!sd_device_unref(t));
247 }
248
249 HASHMAP_FOREACH(d, h) {
250 const char *syspath;
251
252 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
253 log_warning("Device removed: subsystem:%s syspath:%s", subsystem, syspath);
254 n_removed_dev++;
255
256 assert_se(!sd_device_unref(d));
257 }
258
259 hashmap_free(h);
260
261 *ret_n_new_dev = n_new_dev;
262 *ret_n_removed_dev = n_removed_dev;
263 }
264
265 static bool test_sd_device_enumerator_filter_subsystem_trial(void) {
266 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
267 _cleanup_(hashmap_freep) Hashmap *subsystems;
268 unsigned n_new_dev = 0, n_removed_dev = 0;
269 sd_device *d;
270 Hashmap *h;
271 char *s;
272
273 assert_se(subsystems = hashmap_new(&string_hash_ops));
274 assert_se(sd_device_enumerator_new(&e) >= 0);
275 /* See comments in TEST(sd_device_enumerator_devices). */
276 assert_se(sd_device_enumerator_add_match_subsystem(e, "bdi", false) >= 0);
277 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
278 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", false) >= 0);
279
280 FOREACH_DEVICE(e, d) {
281 const char *syspath, *subsystem;
282 int r;
283
284 assert_se(sd_device_get_syspath(d, &syspath) >= 0);
285
286 r = sd_device_get_subsystem(d, &subsystem);
287 assert_se(r >= 0 || r == -ENOENT);
288 if (r < 0)
289 continue;
290
291 h = hashmap_get(subsystems, subsystem);
292 if (!h) {
293 char *str;
294 assert_se(str = strdup(subsystem));
295 assert_se(h = hashmap_new(&string_hash_ops));
296 assert_se(hashmap_put(subsystems, str, h) >= 0);
297 }
298
299 assert_se(hashmap_put(h, syspath, d) >= 0);
300 assert_se(sd_device_ref(d));
301
302 log_debug("Added subsystem:%s syspath:%s", subsystem, syspath);
303 }
304
305 while ((h = hashmap_steal_first_key_and_value(subsystems, (void**) &s))) {
306 unsigned n, m;
307
308 test_sd_device_enumerator_filter_subsystem_one(s, TAKE_PTR(h), &n, &m);
309 free(s);
310
311 n_new_dev += n;
312 n_removed_dev += m;
313 }
314
315 if (n_new_dev > 0)
316 log_warning("%u new devices are found in re-scan", n_new_dev);
317 if (n_removed_dev > 0)
318 log_warning("%u devices removed in re-scan", n_removed_dev);
319
320 return n_new_dev + n_removed_dev == 0;
321 }
322
323 static bool test_sd_device_enumerator_filter_subsystem_trial_many(void) {
324 for (unsigned i = 0; i < 20; i++) {
325 log_debug("%s(): trial %u", __func__, i);
326 if (test_sd_device_enumerator_filter_subsystem_trial())
327 return true;
328 }
329
330 return false;
331 }
332
333 static int on_inotify(sd_event_source *s, const struct inotify_event *event, void *userdata) {
334 if (test_sd_device_enumerator_filter_subsystem_trial_many())
335 return sd_event_exit(sd_event_source_get_event(s), 0);
336
337 return sd_event_exit(sd_event_source_get_event(s), -EBUSY);
338 }
339
340 TEST(sd_device_enumerator_filter_subsystem) {
341 /* The test test_sd_device_enumerator_filter_subsystem_trial() is quite racy. Let's run the function
342 * several times after the udev queue becomes empty. */
343
344 if (!udev_available()) {
345 assert_se(test_sd_device_enumerator_filter_subsystem_trial_many());
346 return;
347 }
348
349 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
350 assert_se(sd_event_default(&event) >= 0);
351 assert_se(sd_event_add_inotify(event, NULL, "/run/udev" , IN_DELETE, on_inotify, NULL) >= 0);
352
353 if (udev_queue_is_empty() == 0) {
354 log_debug("udev queue is not empty, waiting for all queued events to be processed.");
355 assert_se(sd_event_loop(event) >= 0);
356 } else
357 assert_se(test_sd_device_enumerator_filter_subsystem_trial_many());
358 }
359
360 TEST(sd_device_enumerator_add_match_sysattr) {
361 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
362 sd_device *dev;
363 int ifindex;
364
365 assert_se(sd_device_enumerator_new(&e) >= 0);
366 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
367 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", true) >= 0);
368 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "1", true) >= 0);
369 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "hoge", true) >= 0);
370 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "foo", true) >= 0);
371 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "bar", false) >= 0);
372 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "baz", false) >= 0);
373
374 dev = sd_device_enumerator_get_device_first(e);
375 assert_se(dev);
376 assert_se(sd_device_get_ifindex(dev, &ifindex) >= 0);
377 assert_se(ifindex == 1);
378
379 assert_se(!sd_device_enumerator_get_device_next(e));
380 }
381
382 TEST(sd_device_enumerator_add_match_property) {
383 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
384 sd_device *dev;
385 int ifindex;
386
387 assert_se(sd_device_enumerator_new(&e) >= 0);
388 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
389 assert_se(sd_device_enumerator_add_match_subsystem(e, "net", true) >= 0);
390 assert_se(sd_device_enumerator_add_match_sysattr(e, "ifindex", "1", true) >= 0);
391 assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", "1*") >= 0);
392 assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", "hoge") >= 0);
393 assert_se(sd_device_enumerator_add_match_property(e, "IFINDE*", NULL) >= 0);
394 assert_se(sd_device_enumerator_add_match_property(e, "AAAAA", "BBBB") >= 0);
395 assert_se(sd_device_enumerator_add_match_property(e, "FOOOO", NULL) >= 0);
396
397 dev = sd_device_enumerator_get_device_first(e);
398 assert_se(dev);
399 assert_se(sd_device_get_ifindex(dev, &ifindex) >= 0);
400 assert_se(ifindex == 1);
401 }
402
403 TEST(sd_device_new_from_nulstr) {
404 const char *devlinks =
405 "/dev/disk/by-partuuid/1290d63a-42cc-4c71-b87c-xxxxxxxxxxxx\0"
406 "/dev/disk/by-path/pci-0000:00:0f.0-scsi-0:0:0:0-part3\0"
407 "/dev/disk/by-label/Arch\\x20Linux\0"
408 "/dev/disk/by-uuid/a07b87e5-4af5-4a59-bde9-yyyyyyyyyyyy\0"
409 "/dev/disk/by-partlabel/Arch\\x20Linux\0"
410 "\0";
411
412 _cleanup_(sd_device_unrefp) sd_device *device = NULL, *from_nulstr = NULL;
413 _cleanup_free_ char *nulstr_copy = NULL;
414 const char *devlink, *nulstr;
415 size_t len;
416
417 assert_se(sd_device_new_from_syspath(&device, "/sys/class/net/lo") >= 0);
418
419 /* Yeah, of course, setting devlink to the loopback interface is nonsense. But this is just a
420 * test for generating and parsing nulstr. For issue #17772. */
421 NULSTR_FOREACH(devlink, devlinks) {
422 log_device_info(device, "setting devlink: %s", devlink);
423 assert_se(device_add_devlink(device, devlink) >= 0);
424 assert_se(set_contains(device->devlinks, devlink));
425 }
426
427 /* For issue #23799 */
428 assert_se(device_add_tag(device, "tag1", false) >= 0);
429 assert_se(device_add_tag(device, "tag2", false) >= 0);
430 assert_se(device_add_tag(device, "current-tag1", true) >= 0);
431 assert_se(device_add_tag(device, "current-tag2", true) >= 0);
432
433 /* These properties are necessary for device_new_from_nulstr(). See device_verify(). */
434 assert_se(device_add_property_internal(device, "SEQNUM", "1") >= 0);
435 assert_se(device_add_property_internal(device, "ACTION", "change") >= 0);
436
437 assert_se(device_get_properties_nulstr(device, &nulstr, &len) >= 0);
438 assert_se(nulstr_copy = newdup(char, nulstr, len));
439 assert_se(device_new_from_nulstr(&from_nulstr, nulstr_copy, len) >= 0);
440
441 assert_se(sd_device_has_tag(from_nulstr, "tag1") == 1);
442 assert_se(sd_device_has_tag(from_nulstr, "tag2") == 1);
443 assert_se(sd_device_has_tag(from_nulstr, "current-tag1") == 1);
444 assert_se(sd_device_has_tag(from_nulstr, "current-tag2") == 1);
445 assert_se(sd_device_has_current_tag(from_nulstr, "tag1") == 0);
446 assert_se(sd_device_has_current_tag(from_nulstr, "tag2") == 0);
447 assert_se(sd_device_has_current_tag(from_nulstr, "current-tag1") == 1);
448 assert_se(sd_device_has_current_tag(from_nulstr, "current-tag2") == 1);
449
450 NULSTR_FOREACH(devlink, devlinks) {
451 log_device_info(from_nulstr, "checking devlink: %s", devlink);
452 assert_se(set_contains(from_nulstr->devlinks, devlink));
453 }
454 }
455
456 TEST(sd_device_new_from_path) {
457 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
458 _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL;
459 sd_device *dev;
460 int r;
461
462 assert_se(mkdtemp_malloc("/tmp/test-sd-device.XXXXXXX", &tmpdir) >= 0);
463
464 assert_se(sd_device_enumerator_new(&e) >= 0);
465 assert_se(sd_device_enumerator_allow_uninitialized(e) >= 0);
466 assert_se(sd_device_enumerator_add_match_subsystem(e, "block", true) >= 0);
467 assert_se(sd_device_enumerator_add_nomatch_sysname(e, "loop*") >= 0);
468 assert_se(sd_device_enumerator_add_match_property(e, "DEVNAME", "*") >= 0);
469
470 FOREACH_DEVICE(e, dev) {
471 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
472 const char *syspath, *devpath, *sysname, *s;
473 _cleanup_free_ char *path = NULL;
474
475 assert_se(sd_device_get_sysname(dev, &sysname) >= 0);
476
477 log_debug("%s(%s)", __func__, sysname);
478
479 assert_se(sd_device_get_syspath(dev, &syspath) >= 0);
480 assert_se(sd_device_new_from_path(&d, syspath) >= 0);
481 assert_se(sd_device_get_syspath(d, &s) >= 0);
482 assert_se(streq(s, syspath));
483 d = sd_device_unref(d);
484
485 assert_se(sd_device_get_devname(dev, &devpath) >= 0);
486 r = sd_device_new_from_path(&d, devpath);
487 if (r >= 0) {
488 assert_se(sd_device_get_syspath(d, &s) >= 0);
489 assert_se(streq(s, syspath));
490 d = sd_device_unref(d);
491 } else
492 assert_se(r == -ENODEV || ERRNO_IS_PRIVILEGE(r));
493
494 assert_se(path = path_join(tmpdir, sysname));
495 assert_se(symlink(syspath, path) >= 0);
496 assert_se(sd_device_new_from_path(&d, path) >= 0);
497 assert_se(sd_device_get_syspath(d, &s) >= 0);
498 assert_se(streq(s, syspath));
499 }
500 }
501
502 DEFINE_TEST_MAIN(LOG_INFO);