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 "mountpoint-util.h"
15 #include "nulstr-util.h"
16 #include "path-util.h"
18 #include "stat-util.h"
19 #include "string-util.h"
21 #include "time-util.h"
22 #include "tmpfile-util.h"
23 #include "udev-util.h"
25 static void test_sd_device_one(sd_device
*d
) {
26 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
27 const char *syspath
, *sysname
, *subsystem
= NULL
, *devname
, *val
;
28 bool is_block
= false;
33 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
34 assert_se(path_startswith(syspath
, "/sys"));
35 assert_se(sd_device_get_sysname(d
, &sysname
) >= 0);
37 log_info("%s(%s)", __func__
, syspath
);
39 assert_se(sd_device_new_from_syspath(&dev
, syspath
) >= 0);
40 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
41 assert_se(streq(syspath
, val
));
42 dev
= sd_device_unref(dev
);
44 assert_se(sd_device_new_from_path(&dev
, syspath
) >= 0);
45 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
46 assert_se(streq(syspath
, val
));
47 dev
= sd_device_unref(dev
);
49 r
= sd_device_get_ifindex(d
, &ifindex
);
51 assert_se(ifindex
> 0);
53 r
= sd_device_new_from_ifindex(&dev
, ifindex
);
55 log_device_warning_errno(d
, r
,
56 "Failed to create sd-device object from ifindex %i. "
57 "Maybe running on a non-host network namespace.", ifindex
);
60 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
61 assert_se(streq(syspath
, val
));
62 dev
= sd_device_unref(dev
);
65 /* This does not require the interface really exists on the network namespace.
66 * Hence, this should always succeed. */
67 assert_se(sd_device_new_from_ifname(&dev
, sysname
) >= 0);
68 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
69 assert_se(streq(syspath
, val
));
70 dev
= sd_device_unref(dev
);
72 assert_se(r
== -ENOENT
);
74 r
= sd_device_get_subsystem(d
, &subsystem
);
76 assert_se(r
== -ENOENT
);
77 else if (!streq(subsystem
, "gpio")) { /* Unfortunately, there exist /sys/class/gpio and /sys/bus/gpio.
78 * Hence, sd_device_new_from_subsystem_sysname() and
79 * sd_device_new_from_device_id() may not work as expected. */
80 const char *name
, *id
;
82 if (streq(subsystem
, "drivers"))
83 name
= strjoina(d
->driver_subsystem
, ":", sysname
);
86 assert_se(sd_device_new_from_subsystem_sysname(&dev
, subsystem
, name
) >= 0);
87 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
88 assert_se(streq(syspath
, val
));
89 dev
= sd_device_unref(dev
);
91 /* The device ID depends on subsystem. */
92 assert_se(device_get_device_id(d
, &id
) >= 0);
93 r
= sd_device_new_from_device_id(&dev
, id
);
94 if (r
== -ENODEV
&& ifindex
> 0)
95 log_device_warning_errno(d
, r
,
96 "Failed to create sd-device object from device ID \"%s\". "
97 "Maybe running on a non-host network namespace.", id
);
100 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
101 assert_se(streq(syspath
, val
));
102 dev
= sd_device_unref(dev
);
105 /* These require udev database, and reading database requires device ID. */
106 r
= sd_device_get_is_initialized(d
);
108 r
= sd_device_get_usec_since_initialized(d
, &usec
);
109 assert_se((r
>= 0 && usec
> 0) || r
== -ENODATA
);
113 r
= sd_device_get_property_value(d
, "ID_NET_DRIVER", &val
);
114 assert_se(r
>= 0 || r
== -ENOENT
);
117 is_block
= streq_ptr(subsystem
, "block");
119 r
= sd_device_get_devname(d
, &devname
);
121 r
= sd_device_new_from_devname(&dev
, devname
);
123 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
124 assert_se(streq(syspath
, val
));
125 dev
= sd_device_unref(dev
);
127 assert_se(r
== -ENODEV
|| ERRNO_IS_PRIVILEGE(r
));
129 r
= sd_device_new_from_path(&dev
, devname
);
131 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
132 assert_se(streq(syspath
, val
));
133 dev
= sd_device_unref(dev
);
135 _cleanup_close_
int fd
= -EBADF
;
136 fd
= sd_device_open(d
, O_CLOEXEC
| O_NONBLOCK
| (is_block
? O_RDONLY
: O_NOCTTY
| O_PATH
));
137 assert_se(fd
>= 0 || ERRNO_IS_PRIVILEGE(fd
));
139 assert_se(r
== -ENODEV
|| ERRNO_IS_PRIVILEGE(r
));
141 assert_se(r
== -ENOENT
);
143 r
= sd_device_get_devnum(d
, &devnum
);
145 _cleanup_free_
char *p
= NULL
;
147 assert_se(major(devnum
) > 0);
149 assert_se(sd_device_new_from_devnum(&dev
, is_block
? 'b' : 'c', devnum
) >= 0);
150 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
151 assert_se(streq(syspath
, val
));
152 dev
= sd_device_unref(dev
);
154 assert_se(asprintf(&p
, "/dev/%s/%u:%u", is_block
? "block" : "char", major(devnum
), minor(devnum
)) >= 0);
155 assert_se(sd_device_new_from_devname(&dev
, p
) >= 0);
156 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
157 assert_se(streq(syspath
, val
));
158 dev
= sd_device_unref(dev
);
160 assert_se(sd_device_new_from_path(&dev
, p
) >= 0);
161 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
162 assert_se(streq(syspath
, val
));
163 dev
= sd_device_unref(dev
);
165 assert_se(r
== -ENOENT
);
167 assert_se(sd_device_get_devpath(d
, &val
) >= 0);
169 r
= sd_device_get_devtype(d
, &val
);
170 assert_se(r
>= 0 || r
== -ENOENT
);
172 r
= sd_device_get_driver(d
, &val
);
173 assert_se(r
>= 0 || r
== -ENOENT
);
175 r
= sd_device_get_sysnum(d
, &val
);
177 assert_se(val
> sysname
);
178 assert_se(val
< sysname
+ strlen(sysname
));
179 assert_se(in_charset(val
, DIGITS
));
180 assert_se(!ascii_isdigit(val
[-1]));
182 assert_se(r
== -ENOENT
);
184 r
= sd_device_get_sysattr_value(d
, "nsid", NULL
);
188 assert_se(device_get_sysattr_unsigned(d
, "nsid", NULL
) >= 0);
189 r
= device_get_sysattr_unsigned(d
, "nsid", &x
);
191 assert_se((x
> 0) == (r
> 0));
193 assert_se(ERRNO_IS_PRIVILEGE(r
) || IN_SET(r
, -ENOENT
, -EINVAL
));
196 TEST(sd_device_enumerator_devices
) {
197 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
199 assert_se(sd_device_enumerator_new(&e
) >= 0);
200 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
201 /* On some CI environments, it seems some loop block devices and corresponding bdi devices sometimes
202 * disappear during running this test. Let's exclude them here for stability. */
203 assert_se(sd_device_enumerator_add_match_subsystem(e
, "bdi", false) >= 0);
204 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
205 /* On CentOS CI, systemd-networkd-tests.py may be running when this test is invoked. The networkd
206 * test creates and removes many network interfaces, and may interfere with this test. */
207 assert_se(sd_device_enumerator_add_match_subsystem(e
, "net", false) >= 0);
209 test_sd_device_one(d
);
212 TEST(sd_device_enumerator_subsystems
) {
213 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
215 assert_se(sd_device_enumerator_new(&e
) >= 0);
216 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
217 FOREACH_SUBSYSTEM(e
, d
)
218 test_sd_device_one(d
);
221 static void test_sd_device_enumerator_filter_subsystem_one(
222 const char *subsystem
,
224 unsigned *ret_n_new_dev
,
225 unsigned *ret_n_removed_dev
) {
227 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
228 unsigned n_new_dev
= 0, n_removed_dev
= 0;
231 assert_se(sd_device_enumerator_new(&e
) >= 0);
232 assert_se(sd_device_enumerator_add_match_subsystem(e
, subsystem
, true) >= 0);
233 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
235 FOREACH_DEVICE(e
, d
) {
239 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
240 t
= hashmap_remove(h
, syspath
);
243 log_warning("New device found: subsystem:%s syspath:%s", subsystem
, syspath
);
247 assert_se(!sd_device_unref(t
));
250 HASHMAP_FOREACH(dev
, h
) {
253 assert_se(sd_device_get_syspath(dev
, &syspath
) >= 0);
254 log_warning("Device removed: subsystem:%s syspath:%s", subsystem
, syspath
);
257 assert_se(!sd_device_unref(dev
));
262 *ret_n_new_dev
= n_new_dev
;
263 *ret_n_removed_dev
= n_removed_dev
;
266 static bool test_sd_device_enumerator_filter_subsystem_trial(void) {
267 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
268 _cleanup_hashmap_free_ Hashmap
*subsystems
= NULL
;
269 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() || (access("/run/udev", F_OK
) < 0 && errno
== ENOENT
)) {
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_enumerator_add_match_property_required
) {
404 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
408 assert_se(sd_device_enumerator_new(&e
) >= 0);
409 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
410 assert_se(sd_device_enumerator_add_match_subsystem(e
, "net", true) >= 0);
411 assert_se(sd_device_enumerator_add_match_sysattr(e
, "ifindex", "1", true) >= 0);
412 assert_se(sd_device_enumerator_add_match_property_required(e
, "IFINDE*", "1*") >= 0);
414 /* Only one required match which should be satisfied. */
415 dev
= sd_device_enumerator_get_device_first(e
);
417 assert_se(sd_device_get_ifindex(dev
, &ifindex
) >= 0);
418 assert_se(ifindex
== 1);
420 /* Now let's add a bunch of garbage properties which should not be satisfied. */
421 assert_se(sd_device_enumerator_add_match_property_required(e
, "IFINDE*", "hoge") >= 0);
422 assert_se(sd_device_enumerator_add_match_property_required(e
, "IFINDE*", NULL
) >= 0);
423 assert_se(sd_device_enumerator_add_match_property_required(e
, "AAAAA", "BBBB") >= 0);
424 assert_se(sd_device_enumerator_add_match_property_required(e
, "FOOOO", NULL
) >= 0);
426 assert_se(!sd_device_enumerator_get_device_first(e
));
429 static void check_parent_match(sd_device_enumerator
*e
, sd_device
*dev
) {
433 assert_se(sd_device_get_syspath(dev
, &syspath
) >= 0);
435 FOREACH_DEVICE(e
, d
) {
438 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
439 if (streq(s
, syspath
)) {
446 log_device_debug(dev
, "not enumerated, already removed??");
447 /* If the original device not found, then the device should be already removed. */
448 assert_se(access(syspath
, F_OK
) < 0);
449 assert_se(errno
== ENOENT
);
453 TEST(sd_device_enumerator_add_match_parent
) {
454 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
457 assert_se(sd_device_enumerator_new(&e
) >= 0);
458 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
459 /* See comments in TEST(sd_device_enumerator_devices). */
460 assert_se(sd_device_enumerator_add_match_subsystem(e
, "bdi", false) >= 0);
461 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
462 assert_se(sd_device_enumerator_add_match_subsystem(e
, "net", false) >= 0);
464 if (!slow_tests_enabled())
465 assert_se(sd_device_enumerator_add_match_subsystem(e
, "block", true) >= 0);
467 FOREACH_DEVICE(e
, dev
) {
468 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*p
= NULL
;
472 assert_se(sd_device_get_syspath(dev
, &syspath
) >= 0);
474 r
= sd_device_get_parent(dev
, &parent
);
476 assert_se(ERRNO_IS_DEVICE_ABSENT(r
));
480 log_debug("> %s", syspath
);
482 assert_se(sd_device_enumerator_new(&p
) >= 0);
483 assert_se(sd_device_enumerator_allow_uninitialized(p
) >= 0);
484 assert_se(sd_device_enumerator_add_match_parent(p
, parent
) >= 0);
486 check_parent_match(p
, dev
);
488 /* If the device does not have subsystem, then it is not enumerated. */
489 r
= sd_device_get_subsystem(parent
, NULL
);
491 assert_se(r
== -ENOENT
);
494 check_parent_match(p
, parent
);
498 TEST(sd_device_get_child
) {
499 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
502 assert_se(sd_device_enumerator_new(&e
) >= 0);
503 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
504 /* See comments in TEST(sd_device_enumerator_devices). */
505 assert_se(sd_device_enumerator_add_match_subsystem(e
, "bdi", false) >= 0);
506 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
507 assert_se(sd_device_enumerator_add_match_subsystem(e
, "net", false) >= 0);
509 if (!slow_tests_enabled())
510 assert_se(sd_device_enumerator_add_match_subsystem(e
, "block", true) >= 0);
512 FOREACH_DEVICE(e
, dev
) {
513 const char *syspath
, *parent_syspath
, *expected_suffix
, *suffix
;
517 assert_se(sd_device_get_syspath(dev
, &syspath
) >= 0);
519 r
= sd_device_get_parent(dev
, &parent
);
521 assert_se(ERRNO_IS_DEVICE_ABSENT(r
));
525 assert_se(sd_device_get_syspath(parent
, &parent_syspath
) >= 0);
526 assert_se(expected_suffix
= path_startswith(syspath
, parent_syspath
));
528 log_debug("> %s", syspath
);
530 FOREACH_DEVICE_CHILD_WITH_SUFFIX(parent
, child
, suffix
) {
536 if (!streq(suffix
, expected_suffix
))
539 assert_se(sd_device_get_syspath(child
, &s
) >= 0);
540 assert_se(streq(s
, syspath
));
548 TEST(sd_device_new_from_nulstr
) {
549 const char *devlinks
=
550 "/dev/disk/by-partuuid/1290d63a-42cc-4c71-b87c-xxxxxxxxxxxx\0"
551 "/dev/disk/by-path/pci-0000:00:0f.0-scsi-0:0:0:0-part3\0"
552 "/dev/disk/by-label/Arch\\x20Linux\0"
553 "/dev/disk/by-uuid/a07b87e5-4af5-4a59-bde9-yyyyyyyyyyyy\0"
554 "/dev/disk/by-partlabel/Arch\\x20Linux\0"
557 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
, *from_nulstr
= NULL
;
558 _cleanup_free_
char *nulstr_copy
= NULL
;
562 assert_se(sd_device_new_from_syspath(&device
, "/sys/class/net/lo") >= 0);
564 /* Yeah, of course, setting devlink to the loopback interface is nonsense. But this is just a
565 * test for generating and parsing nulstr. For issue #17772. */
566 NULSTR_FOREACH(devlink
, devlinks
) {
567 log_device_info(device
, "setting devlink: %s", devlink
);
568 assert_se(device_add_devlink(device
, devlink
) >= 0);
569 assert_se(set_contains(device
->devlinks
, devlink
));
572 /* For issue #23799 */
573 assert_se(device_add_tag(device
, "tag1", false) >= 0);
574 assert_se(device_add_tag(device
, "tag2", false) >= 0);
575 assert_se(device_add_tag(device
, "current-tag1", true) >= 0);
576 assert_se(device_add_tag(device
, "current-tag2", true) >= 0);
578 /* These properties are necessary for device_new_from_nulstr(). See device_verify(). */
579 assert_se(device_add_property_internal(device
, "SEQNUM", "1") >= 0);
580 assert_se(device_add_property_internal(device
, "ACTION", "change") >= 0);
582 assert_se(device_get_properties_nulstr(device
, &nulstr
, &len
) >= 0);
583 assert_se(nulstr_copy
= newdup(char, nulstr
, len
));
584 assert_se(device_new_from_nulstr(&from_nulstr
, nulstr_copy
, len
) >= 0);
586 assert_se(sd_device_has_tag(from_nulstr
, "tag1") == 1);
587 assert_se(sd_device_has_tag(from_nulstr
, "tag2") == 1);
588 assert_se(sd_device_has_tag(from_nulstr
, "current-tag1") == 1);
589 assert_se(sd_device_has_tag(from_nulstr
, "current-tag2") == 1);
590 assert_se(sd_device_has_current_tag(from_nulstr
, "tag1") == 0);
591 assert_se(sd_device_has_current_tag(from_nulstr
, "tag2") == 0);
592 assert_se(sd_device_has_current_tag(from_nulstr
, "current-tag1") == 1);
593 assert_se(sd_device_has_current_tag(from_nulstr
, "current-tag2") == 1);
595 NULSTR_FOREACH(devlink
, devlinks
) {
596 log_device_info(from_nulstr
, "checking devlink: %s", devlink
);
597 assert_se(set_contains(from_nulstr
->devlinks
, devlink
));
601 TEST(sd_device_new_from_path
) {
602 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
603 _cleanup_(rm_rf_physical_and_freep
) char *tmpdir
= NULL
;
606 assert_se(mkdtemp_malloc("/tmp/test-sd-device.XXXXXXX", &tmpdir
) >= 0);
608 assert_se(sd_device_enumerator_new(&e
) >= 0);
609 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
610 assert_se(sd_device_enumerator_add_match_subsystem(e
, "block", true) >= 0);
611 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
612 assert_se(sd_device_enumerator_add_match_property(e
, "DEVNAME", "*") >= 0);
614 FOREACH_DEVICE(e
, dev
) {
615 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
616 const char *syspath
, *devpath
, *sysname
, *s
;
617 _cleanup_free_
char *path
= NULL
;
619 assert_se(sd_device_get_sysname(dev
, &sysname
) >= 0);
621 log_debug("%s(%s)", __func__
, sysname
);
623 assert_se(sd_device_get_syspath(dev
, &syspath
) >= 0);
624 assert_se(sd_device_new_from_path(&d
, syspath
) >= 0);
625 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
626 assert_se(streq(s
, syspath
));
627 d
= sd_device_unref(d
);
629 assert_se(sd_device_get_devname(dev
, &devpath
) >= 0);
630 r
= sd_device_new_from_path(&d
, devpath
);
632 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
633 assert_se(streq(s
, syspath
));
634 d
= sd_device_unref(d
);
636 assert_se(r
== -ENODEV
|| ERRNO_IS_PRIVILEGE(r
));
638 assert_se(path
= path_join(tmpdir
, sysname
));
639 assert_se(symlink(syspath
, path
) >= 0);
640 assert_se(sd_device_new_from_path(&d
, path
) >= 0);
641 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
642 assert_se(streq(s
, syspath
));
646 static void test_devname_from_devnum_one(const char *path
) {
647 _cleanup_free_
char *resolved
= NULL
;
650 log_debug("> %s", path
);
652 if (stat(path
, &st
) < 0) {
653 assert_se(errno
== ENOENT
);
654 log_notice("Path %s not found, skipping test", path
);
658 assert_se(devname_from_devnum(st
.st_mode
, st
.st_rdev
, &resolved
) >= 0);
659 assert_se(path_equal(path
, resolved
));
660 resolved
= mfree(resolved
);
661 assert_se(devname_from_stat_rdev(&st
, &resolved
) >= 0);
662 assert_se(path_equal(path
, resolved
));
665 TEST(devname_from_devnum
) {
666 test_devname_from_devnum_one("/dev/null");
667 test_devname_from_devnum_one("/dev/zero");
668 test_devname_from_devnum_one("/dev/full");
669 test_devname_from_devnum_one("/dev/random");
670 test_devname_from_devnum_one("/dev/urandom");
671 test_devname_from_devnum_one("/dev/tty");
673 if (is_device_node("/run/systemd/inaccessible/blk") > 0) {
674 test_devname_from_devnum_one("/run/systemd/inaccessible/chr");
675 test_devname_from_devnum_one("/run/systemd/inaccessible/blk");
679 static int intro(void) {
680 if (path_is_mount_point("/sys", NULL
, 0) <= 0)
681 return log_tests_skipped("/sys is not mounted");
686 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO
, intro
);