1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
14 #include "nulstr-util.h"
15 #include "path-util.h"
17 #include "string-util.h"
19 #include "time-util.h"
20 #include "tmpfile-util.h"
21 #include "udev-util.h"
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;
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);
35 log_info("%s(%s)", __func__
, syspath
);
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
);
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
);
47 r
= sd_device_get_ifindex(d
, &ifindex
);
49 assert_se(ifindex
> 0);
51 r
= sd_device_new_from_ifindex(&dev
, ifindex
);
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
);
58 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
59 assert_se(streq(syspath
, val
));
60 dev
= sd_device_unref(dev
);
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
);
70 assert_se(r
== -ENOENT
);
72 r
= sd_device_get_subsystem(d
, &subsystem
);
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
;
80 if (streq(subsystem
, "drivers"))
81 name
= strjoina(d
->driver_subsystem
, ":", 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
);
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
);
98 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
99 assert_se(streq(syspath
, val
));
100 dev
= sd_device_unref(dev
);
103 /* These require udev database, and reading database requires device ID. */
104 r
= sd_device_get_is_initialized(d
);
106 r
= sd_device_get_usec_since_initialized(d
, &usec
);
107 assert_se((r
>= 0 && usec
> 0) || r
== -ENODATA
);
111 r
= sd_device_get_property_value(d
, "ID_NET_DRIVER", &val
);
112 assert_se(r
>= 0 || r
== -ENOENT
);
115 is_block
= streq_ptr(subsystem
, "block");
117 r
= sd_device_get_devname(d
, &devname
);
119 r
= sd_device_new_from_devname(&dev
, devname
);
121 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
122 assert_se(streq(syspath
, val
));
123 dev
= sd_device_unref(dev
);
125 assert_se(r
== -ENODEV
|| ERRNO_IS_PRIVILEGE(r
));
127 r
= sd_device_new_from_path(&dev
, devname
);
129 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
130 assert_se(streq(syspath
, val
));
131 dev
= sd_device_unref(dev
);
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
));
137 assert_se(r
== -ENODEV
|| ERRNO_IS_PRIVILEGE(r
));
139 assert_se(r
== -ENOENT
);
141 r
= sd_device_get_devnum(d
, &devnum
);
143 _cleanup_free_
char *p
= NULL
;
145 assert_se(major(devnum
) > 0);
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
);
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
);
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
);
163 assert_se(r
== -ENOENT
);
165 assert_se(sd_device_get_devpath(d
, &val
) >= 0);
167 r
= sd_device_get_devtype(d
, &val
);
168 assert_se(r
>= 0 || r
== -ENOENT
);
170 r
= sd_device_get_driver(d
, &val
);
171 assert_se(r
>= 0 || r
== -ENOENT
);
173 r
= sd_device_get_sysnum(d
, &val
);
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]));
180 assert_se(r
== -ENOENT
);
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
));
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);
193 TEST(sd_device_enumerator_devices
) {
194 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
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);
207 test_sd_device_one(d
);
210 TEST(sd_device_enumerator_subsystems
) {
211 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
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
);
220 static void test_sd_device_enumerator_filter_subsystem_one(
221 const char *subsystem
,
223 unsigned *ret_n_new_dev
,
224 unsigned *ret_n_removed_dev
) {
226 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
227 unsigned n_new_dev
= 0, n_removed_dev
= 0;
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);
234 FOREACH_DEVICE(e
, d
) {
238 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
239 t
= hashmap_remove(h
, syspath
);
242 log_warning("New device found: subsystem:%s syspath:%s", subsystem
, syspath
);
246 assert_se(!sd_device_unref(t
));
249 HASHMAP_FOREACH(d
, h
) {
252 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
253 log_warning("Device removed: subsystem:%s syspath:%s", subsystem
, syspath
);
256 assert_se(!sd_device_unref(d
));
261 *ret_n_new_dev
= n_new_dev
;
262 *ret_n_removed_dev
= n_removed_dev
;
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;
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);
280 FOREACH_DEVICE(e
, d
) {
281 const char *syspath
, *subsystem
;
284 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
286 r
= sd_device_get_subsystem(d
, &subsystem
);
287 assert_se(r
>= 0 || r
== -ENOENT
);
291 h
= hashmap_get(subsystems
, subsystem
);
294 assert_se(str
= strdup(subsystem
));
295 assert_se(h
= hashmap_new(&string_hash_ops
));
296 assert_se(hashmap_put(subsystems
, str
, h
) >= 0);
299 assert_se(hashmap_put(h
, syspath
, d
) >= 0);
300 assert_se(sd_device_ref(d
));
302 log_debug("Added subsystem:%s syspath:%s", subsystem
, syspath
);
305 while ((h
= hashmap_steal_first_key_and_value(subsystems
, (void**) &s
))) {
308 test_sd_device_enumerator_filter_subsystem_one(s
, TAKE_PTR(h
), &n
, &m
);
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
);
320 return n_new_dev
+ n_removed_dev
== 0;
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())
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);
337 return sd_event_exit(sd_event_source_get_event(s
), -EBUSY
);
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. */
344 if (!udev_available()) {
345 assert_se(test_sd_device_enumerator_filter_subsystem_trial_many());
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);
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);
357 assert_se(test_sd_device_enumerator_filter_subsystem_trial_many());
360 TEST(sd_device_enumerator_add_match_sysattr
) {
361 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
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);
374 dev
= sd_device_enumerator_get_device_first(e
);
376 assert_se(sd_device_get_ifindex(dev
, &ifindex
) >= 0);
377 assert_se(ifindex
== 1);
379 assert_se(!sd_device_enumerator_get_device_next(e
));
382 TEST(sd_device_enumerator_add_match_property
) {
383 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
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);
397 dev
= sd_device_enumerator_get_device_first(e
);
399 assert_se(sd_device_get_ifindex(dev
, &ifindex
) >= 0);
400 assert_se(ifindex
== 1);
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"
412 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
, *from_nulstr
= NULL
;
413 _cleanup_free_
char *nulstr_copy
= NULL
;
414 const char *devlink
, *nulstr
;
417 assert_se(sd_device_new_from_syspath(&device
, "/sys/class/net/lo") >= 0);
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
));
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);
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);
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);
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);
450 NULSTR_FOREACH(devlink
, devlinks
) {
451 log_device_info(from_nulstr
, "checking devlink: %s", devlink
);
452 assert_se(set_contains(from_nulstr
->devlinks
, devlink
));
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
;
462 assert_se(mkdtemp_malloc("/tmp/test-sd-device.XXXXXXX", &tmpdir
) >= 0);
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);
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
;
475 assert_se(sd_device_get_sysname(dev
, &sysname
) >= 0);
477 log_debug("%s(%s)", __func__
, sysname
);
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
);
485 assert_se(sd_device_get_devname(dev
, &devpath
) >= 0);
486 r
= sd_device_new_from_path(&d
, devpath
);
488 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
489 assert_se(streq(s
, syspath
));
490 d
= sd_device_unref(d
);
492 assert_se(r
== -ENODEV
|| ERRNO_IS_PRIVILEGE(r
));
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
));
502 DEFINE_TEST_MAIN(LOG_INFO
);