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 "stat-util.h"
18 #include "string-util.h"
20 #include "time-util.h"
21 #include "tmpfile-util.h"
22 #include "udev-util.h"
24 static void test_sd_device_one(sd_device
*d
) {
25 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
26 const char *syspath
, *sysname
, *subsystem
= NULL
, *devname
, *val
;
27 bool is_block
= false;
32 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
33 assert_se(path_startswith(syspath
, "/sys"));
34 assert_se(sd_device_get_sysname(d
, &sysname
) >= 0);
36 log_info("%s(%s)", __func__
, syspath
);
38 assert_se(sd_device_new_from_syspath(&dev
, syspath
) >= 0);
39 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
40 assert_se(streq(syspath
, val
));
41 dev
= sd_device_unref(dev
);
43 assert_se(sd_device_new_from_path(&dev
, syspath
) >= 0);
44 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
45 assert_se(streq(syspath
, val
));
46 dev
= sd_device_unref(dev
);
48 r
= sd_device_get_ifindex(d
, &ifindex
);
50 assert_se(ifindex
> 0);
52 r
= sd_device_new_from_ifindex(&dev
, ifindex
);
54 log_device_warning_errno(d
, r
,
55 "Failed to create sd-device object from ifindex %i. "
56 "Maybe running on a non-host network namespace.", ifindex
);
59 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
60 assert_se(streq(syspath
, val
));
61 dev
= sd_device_unref(dev
);
64 /* This does not require the interface really exists on the network namespace.
65 * Hence, this should always succeed. */
66 assert_se(sd_device_new_from_ifname(&dev
, sysname
) >= 0);
67 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
68 assert_se(streq(syspath
, val
));
69 dev
= sd_device_unref(dev
);
71 assert_se(r
== -ENOENT
);
73 r
= sd_device_get_subsystem(d
, &subsystem
);
75 assert_se(r
== -ENOENT
);
76 else if (!streq(subsystem
, "gpio")) { /* Unfortunately, there exist /sys/class/gpio and /sys/bus/gpio.
77 * Hence, sd_device_new_from_subsystem_sysname() and
78 * sd_device_new_from_device_id() may not work as expected. */
79 const char *name
, *id
;
81 if (streq(subsystem
, "drivers"))
82 name
= strjoina(d
->driver_subsystem
, ":", sysname
);
85 assert_se(sd_device_new_from_subsystem_sysname(&dev
, subsystem
, name
) >= 0);
86 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
87 assert_se(streq(syspath
, val
));
88 dev
= sd_device_unref(dev
);
90 /* The device ID depends on subsystem. */
91 assert_se(device_get_device_id(d
, &id
) >= 0);
92 r
= sd_device_new_from_device_id(&dev
, id
);
93 if (r
== -ENODEV
&& ifindex
> 0)
94 log_device_warning_errno(d
, r
,
95 "Failed to create sd-device object from device ID \"%s\". "
96 "Maybe running on a non-host network namespace.", id
);
99 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
100 assert_se(streq(syspath
, val
));
101 dev
= sd_device_unref(dev
);
104 /* These require udev database, and reading database requires device ID. */
105 r
= sd_device_get_is_initialized(d
);
107 r
= sd_device_get_usec_since_initialized(d
, &usec
);
108 assert_se((r
>= 0 && usec
> 0) || r
== -ENODATA
);
112 r
= sd_device_get_property_value(d
, "ID_NET_DRIVER", &val
);
113 assert_se(r
>= 0 || r
== -ENOENT
);
116 is_block
= streq_ptr(subsystem
, "block");
118 r
= sd_device_get_devname(d
, &devname
);
120 r
= sd_device_new_from_devname(&dev
, devname
);
122 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
123 assert_se(streq(syspath
, val
));
124 dev
= sd_device_unref(dev
);
126 assert_se(r
== -ENODEV
|| ERRNO_IS_PRIVILEGE(r
));
128 r
= sd_device_new_from_path(&dev
, devname
);
130 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
131 assert_se(streq(syspath
, val
));
132 dev
= sd_device_unref(dev
);
134 _cleanup_close_
int fd
= -1;
135 fd
= sd_device_open(d
, O_CLOEXEC
| O_NONBLOCK
| (is_block
? O_RDONLY
: O_NOCTTY
| O_PATH
));
136 assert_se(fd
>= 0 || ERRNO_IS_PRIVILEGE(fd
));
138 assert_se(r
== -ENODEV
|| ERRNO_IS_PRIVILEGE(r
));
140 assert_se(r
== -ENOENT
);
142 r
= sd_device_get_devnum(d
, &devnum
);
144 _cleanup_free_
char *p
= NULL
;
146 assert_se(major(devnum
) > 0);
148 assert_se(sd_device_new_from_devnum(&dev
, is_block
? 'b' : 'c', devnum
) >= 0);
149 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
150 assert_se(streq(syspath
, val
));
151 dev
= sd_device_unref(dev
);
153 assert_se(asprintf(&p
, "/dev/%s/%u:%u", is_block
? "block" : "char", major(devnum
), minor(devnum
)) >= 0);
154 assert_se(sd_device_new_from_devname(&dev
, p
) >= 0);
155 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
156 assert_se(streq(syspath
, val
));
157 dev
= sd_device_unref(dev
);
159 assert_se(sd_device_new_from_path(&dev
, p
) >= 0);
160 assert_se(sd_device_get_syspath(dev
, &val
) >= 0);
161 assert_se(streq(syspath
, val
));
162 dev
= sd_device_unref(dev
);
164 assert_se(r
== -ENOENT
);
166 assert_se(sd_device_get_devpath(d
, &val
) >= 0);
168 r
= sd_device_get_devtype(d
, &val
);
169 assert_se(r
>= 0 || r
== -ENOENT
);
171 r
= sd_device_get_driver(d
, &val
);
172 assert_se(r
>= 0 || r
== -ENOENT
);
174 r
= sd_device_get_sysnum(d
, &val
);
176 assert_se(val
> sysname
);
177 assert_se(val
< sysname
+ strlen(sysname
));
178 assert_se(in_charset(val
, DIGITS
));
179 assert_se(!ascii_isdigit(val
[-1]));
181 assert_se(r
== -ENOENT
);
183 r
= sd_device_get_sysattr_value(d
, "nsid", NULL
);
187 assert_se(device_get_sysattr_unsigned(d
, "nsid", NULL
) >= 0);
188 r
= device_get_sysattr_unsigned(d
, "nsid", &x
);
190 assert_se((x
> 0) == (r
> 0));
192 assert_se(ERRNO_IS_PRIVILEGE(r
) || IN_SET(r
, -ENOENT
, -EINVAL
));
195 TEST(sd_device_enumerator_devices
) {
196 _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
;
216 assert_se(sd_device_enumerator_new(&e
) >= 0);
217 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
218 FOREACH_SUBSYSTEM(e
, d
)
219 test_sd_device_one(d
);
222 static void test_sd_device_enumerator_filter_subsystem_one(
223 const char *subsystem
,
225 unsigned *ret_n_new_dev
,
226 unsigned *ret_n_removed_dev
) {
228 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
229 unsigned n_new_dev
= 0, n_removed_dev
= 0;
232 assert_se(sd_device_enumerator_new(&e
) >= 0);
233 assert_se(sd_device_enumerator_add_match_subsystem(e
, subsystem
, true) >= 0);
234 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
236 FOREACH_DEVICE(e
, d
) {
240 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
241 t
= hashmap_remove(h
, syspath
);
244 log_warning("New device found: subsystem:%s syspath:%s", subsystem
, syspath
);
248 assert_se(!sd_device_unref(t
));
251 HASHMAP_FOREACH(d
, h
) {
254 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
255 log_warning("Device removed: subsystem:%s syspath:%s", subsystem
, syspath
);
258 assert_se(!sd_device_unref(d
));
263 *ret_n_new_dev
= n_new_dev
;
264 *ret_n_removed_dev
= n_removed_dev
;
267 static bool test_sd_device_enumerator_filter_subsystem_trial(void) {
268 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
269 _cleanup_(hashmap_freep
) Hashmap
*subsystems
;
270 unsigned n_new_dev
= 0, n_removed_dev
= 0;
275 assert_se(subsystems
= hashmap_new(&string_hash_ops
));
276 assert_se(sd_device_enumerator_new(&e
) >= 0);
277 /* See comments in TEST(sd_device_enumerator_devices). */
278 assert_se(sd_device_enumerator_add_match_subsystem(e
, "bdi", false) >= 0);
279 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
280 assert_se(sd_device_enumerator_add_match_subsystem(e
, "net", false) >= 0);
282 FOREACH_DEVICE(e
, d
) {
283 const char *syspath
, *subsystem
;
286 assert_se(sd_device_get_syspath(d
, &syspath
) >= 0);
288 r
= sd_device_get_subsystem(d
, &subsystem
);
289 assert_se(r
>= 0 || r
== -ENOENT
);
293 h
= hashmap_get(subsystems
, subsystem
);
296 assert_se(str
= strdup(subsystem
));
297 assert_se(h
= hashmap_new(&string_hash_ops
));
298 assert_se(hashmap_put(subsystems
, str
, h
) >= 0);
301 assert_se(hashmap_put(h
, syspath
, d
) >= 0);
302 assert_se(sd_device_ref(d
));
304 log_debug("Added subsystem:%s syspath:%s", subsystem
, syspath
);
307 while ((h
= hashmap_steal_first_key_and_value(subsystems
, (void**) &s
))) {
310 test_sd_device_enumerator_filter_subsystem_one(s
, TAKE_PTR(h
), &n
, &m
);
318 log_warning("%u new devices are found in re-scan", n_new_dev
);
319 if (n_removed_dev
> 0)
320 log_warning("%u devices removed in re-scan", n_removed_dev
);
322 return n_new_dev
+ n_removed_dev
== 0;
325 static bool test_sd_device_enumerator_filter_subsystem_trial_many(void) {
326 for (unsigned i
= 0; i
< 20; i
++) {
327 log_debug("%s(): trial %u", __func__
, i
);
328 if (test_sd_device_enumerator_filter_subsystem_trial())
335 static int on_inotify(sd_event_source
*s
, const struct inotify_event
*event
, void *userdata
) {
336 if (test_sd_device_enumerator_filter_subsystem_trial_many())
337 return sd_event_exit(sd_event_source_get_event(s
), 0);
339 return sd_event_exit(sd_event_source_get_event(s
), -EBUSY
);
342 TEST(sd_device_enumerator_filter_subsystem
) {
343 /* The test test_sd_device_enumerator_filter_subsystem_trial() is quite racy. Let's run the function
344 * several times after the udev queue becomes empty. */
346 if (!udev_available() || (access("/run/udev", F_OK
) < 0 && errno
== ENOENT
)) {
347 assert_se(test_sd_device_enumerator_filter_subsystem_trial_many());
351 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
352 assert_se(sd_event_default(&event
) >= 0);
353 assert_se(sd_event_add_inotify(event
, NULL
, "/run/udev" , IN_DELETE
, on_inotify
, NULL
) >= 0);
355 if (udev_queue_is_empty() == 0) {
356 log_debug("udev queue is not empty, waiting for all queued events to be processed.");
357 assert_se(sd_event_loop(event
) >= 0);
359 assert_se(test_sd_device_enumerator_filter_subsystem_trial_many());
362 TEST(sd_device_enumerator_add_match_sysattr
) {
363 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
367 assert_se(sd_device_enumerator_new(&e
) >= 0);
368 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
369 assert_se(sd_device_enumerator_add_match_subsystem(e
, "net", true) >= 0);
370 assert_se(sd_device_enumerator_add_match_sysattr(e
, "ifindex", "1", true) >= 0);
371 assert_se(sd_device_enumerator_add_match_sysattr(e
, "ifindex", "hoge", true) >= 0);
372 assert_se(sd_device_enumerator_add_match_sysattr(e
, "ifindex", "foo", true) >= 0);
373 assert_se(sd_device_enumerator_add_match_sysattr(e
, "ifindex", "bar", false) >= 0);
374 assert_se(sd_device_enumerator_add_match_sysattr(e
, "ifindex", "baz", false) >= 0);
376 dev
= sd_device_enumerator_get_device_first(e
);
378 assert_se(sd_device_get_ifindex(dev
, &ifindex
) >= 0);
379 assert_se(ifindex
== 1);
381 assert_se(!sd_device_enumerator_get_device_next(e
));
384 TEST(sd_device_enumerator_add_match_property
) {
385 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
389 assert_se(sd_device_enumerator_new(&e
) >= 0);
390 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
391 assert_se(sd_device_enumerator_add_match_subsystem(e
, "net", true) >= 0);
392 assert_se(sd_device_enumerator_add_match_sysattr(e
, "ifindex", "1", true) >= 0);
393 assert_se(sd_device_enumerator_add_match_property(e
, "IFINDE*", "1*") >= 0);
394 assert_se(sd_device_enumerator_add_match_property(e
, "IFINDE*", "hoge") >= 0);
395 assert_se(sd_device_enumerator_add_match_property(e
, "IFINDE*", NULL
) >= 0);
396 assert_se(sd_device_enumerator_add_match_property(e
, "AAAAA", "BBBB") >= 0);
397 assert_se(sd_device_enumerator_add_match_property(e
, "FOOOO", NULL
) >= 0);
399 dev
= sd_device_enumerator_get_device_first(e
);
401 assert_se(sd_device_get_ifindex(dev
, &ifindex
) >= 0);
402 assert_se(ifindex
== 1);
405 static void check_parent_match(sd_device_enumerator
*e
, sd_device
*dev
) {
410 assert_se(sd_device_get_syspath(dev
, &syspath
) >= 0);
412 FOREACH_DEVICE(e
, d
) {
415 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
416 if (streq(s
, syspath
)) {
423 log_device_debug(dev
, "not enumerated, already removed??");
424 /* If the original device not found, then the device should be already removed. */
425 assert_se(access(syspath
, F_OK
) < 0);
426 assert_se(errno
== ENOENT
);
430 TEST(sd_device_enumerator_add_match_parent
) {
431 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
435 assert_se(sd_device_enumerator_new(&e
) >= 0);
436 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
437 /* See comments in TEST(sd_device_enumerator_devices). */
438 assert_se(sd_device_enumerator_add_match_subsystem(e
, "bdi", false) >= 0);
439 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
440 assert_se(sd_device_enumerator_add_match_subsystem(e
, "net", false) >= 0);
442 if (!slow_tests_enabled())
443 assert_se(sd_device_enumerator_add_match_subsystem(e
, "block", true) >= 0);
445 FOREACH_DEVICE(e
, dev
) {
446 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*p
= NULL
;
450 assert_se(sd_device_get_syspath(dev
, &syspath
) >= 0);
452 r
= sd_device_get_parent(dev
, &parent
);
454 assert_se(ERRNO_IS_DEVICE_ABSENT(r
));
458 log_debug("> %s", syspath
);
460 assert_se(sd_device_enumerator_new(&p
) >= 0);
461 assert_se(sd_device_enumerator_allow_uninitialized(p
) >= 0);
462 assert_se(sd_device_enumerator_add_match_parent(p
, parent
) >= 0);
464 check_parent_match(p
, dev
);
466 /* If the device does not have subsystem, then it is not enumerated. */
467 r
= sd_device_get_subsystem(parent
, NULL
);
469 assert_se(r
== -ENOENT
);
472 check_parent_match(p
, parent
);
476 TEST(sd_device_get_child
) {
477 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
481 assert_se(sd_device_enumerator_new(&e
) >= 0);
482 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
483 /* See comments in TEST(sd_device_enumerator_devices). */
484 assert_se(sd_device_enumerator_add_match_subsystem(e
, "bdi", false) >= 0);
485 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
486 assert_se(sd_device_enumerator_add_match_subsystem(e
, "net", false) >= 0);
488 if (!slow_tests_enabled())
489 assert_se(sd_device_enumerator_add_match_subsystem(e
, "block", true) >= 0);
491 FOREACH_DEVICE(e
, dev
) {
492 const char *syspath
, *parent_syspath
, *expected_suffix
, *suffix
;
493 sd_device
*parent
, *child
;
496 assert_se(sd_device_get_syspath(dev
, &syspath
) >= 0);
498 r
= sd_device_get_parent(dev
, &parent
);
500 assert_se(ERRNO_IS_DEVICE_ABSENT(r
));
504 assert_se(sd_device_get_syspath(parent
, &parent_syspath
) >= 0);
505 assert_se(expected_suffix
= path_startswith(syspath
, parent_syspath
));
507 log_debug("> %s", syspath
);
509 FOREACH_DEVICE_CHILD_WITH_SUFFIX(parent
, child
, suffix
) {
515 if (!streq(suffix
, expected_suffix
))
518 assert_se(sd_device_get_syspath(child
, &s
) >= 0);
519 assert_se(streq(s
, syspath
));
527 TEST(sd_device_new_from_nulstr
) {
528 const char *devlinks
=
529 "/dev/disk/by-partuuid/1290d63a-42cc-4c71-b87c-xxxxxxxxxxxx\0"
530 "/dev/disk/by-path/pci-0000:00:0f.0-scsi-0:0:0:0-part3\0"
531 "/dev/disk/by-label/Arch\\x20Linux\0"
532 "/dev/disk/by-uuid/a07b87e5-4af5-4a59-bde9-yyyyyyyyyyyy\0"
533 "/dev/disk/by-partlabel/Arch\\x20Linux\0"
536 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
, *from_nulstr
= NULL
;
537 _cleanup_free_
char *nulstr_copy
= NULL
;
541 assert_se(sd_device_new_from_syspath(&device
, "/sys/class/net/lo") >= 0);
543 /* Yeah, of course, setting devlink to the loopback interface is nonsense. But this is just a
544 * test for generating and parsing nulstr. For issue #17772. */
545 NULSTR_FOREACH(devlink
, devlinks
) {
546 log_device_info(device
, "setting devlink: %s", devlink
);
547 assert_se(device_add_devlink(device
, devlink
) >= 0);
548 assert_se(set_contains(device
->devlinks
, devlink
));
551 /* For issue #23799 */
552 assert_se(device_add_tag(device
, "tag1", false) >= 0);
553 assert_se(device_add_tag(device
, "tag2", false) >= 0);
554 assert_se(device_add_tag(device
, "current-tag1", true) >= 0);
555 assert_se(device_add_tag(device
, "current-tag2", true) >= 0);
557 /* These properties are necessary for device_new_from_nulstr(). See device_verify(). */
558 assert_se(device_add_property_internal(device
, "SEQNUM", "1") >= 0);
559 assert_se(device_add_property_internal(device
, "ACTION", "change") >= 0);
561 assert_se(device_get_properties_nulstr(device
, &nulstr
, &len
) >= 0);
562 assert_se(nulstr_copy
= newdup(char, nulstr
, len
));
563 assert_se(device_new_from_nulstr(&from_nulstr
, nulstr_copy
, len
) >= 0);
565 assert_se(sd_device_has_tag(from_nulstr
, "tag1") == 1);
566 assert_se(sd_device_has_tag(from_nulstr
, "tag2") == 1);
567 assert_se(sd_device_has_tag(from_nulstr
, "current-tag1") == 1);
568 assert_se(sd_device_has_tag(from_nulstr
, "current-tag2") == 1);
569 assert_se(sd_device_has_current_tag(from_nulstr
, "tag1") == 0);
570 assert_se(sd_device_has_current_tag(from_nulstr
, "tag2") == 0);
571 assert_se(sd_device_has_current_tag(from_nulstr
, "current-tag1") == 1);
572 assert_se(sd_device_has_current_tag(from_nulstr
, "current-tag2") == 1);
574 NULSTR_FOREACH(devlink
, devlinks
) {
575 log_device_info(from_nulstr
, "checking devlink: %s", devlink
);
576 assert_se(set_contains(from_nulstr
->devlinks
, devlink
));
580 TEST(sd_device_new_from_path
) {
581 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
582 _cleanup_(rm_rf_physical_and_freep
) char *tmpdir
= NULL
;
586 assert_se(mkdtemp_malloc("/tmp/test-sd-device.XXXXXXX", &tmpdir
) >= 0);
588 assert_se(sd_device_enumerator_new(&e
) >= 0);
589 assert_se(sd_device_enumerator_allow_uninitialized(e
) >= 0);
590 assert_se(sd_device_enumerator_add_match_subsystem(e
, "block", true) >= 0);
591 assert_se(sd_device_enumerator_add_nomatch_sysname(e
, "loop*") >= 0);
592 assert_se(sd_device_enumerator_add_match_property(e
, "DEVNAME", "*") >= 0);
594 FOREACH_DEVICE(e
, dev
) {
595 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
596 const char *syspath
, *devpath
, *sysname
, *s
;
597 _cleanup_free_
char *path
= NULL
;
599 assert_se(sd_device_get_sysname(dev
, &sysname
) >= 0);
601 log_debug("%s(%s)", __func__
, sysname
);
603 assert_se(sd_device_get_syspath(dev
, &syspath
) >= 0);
604 assert_se(sd_device_new_from_path(&d
, syspath
) >= 0);
605 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
606 assert_se(streq(s
, syspath
));
607 d
= sd_device_unref(d
);
609 assert_se(sd_device_get_devname(dev
, &devpath
) >= 0);
610 r
= sd_device_new_from_path(&d
, devpath
);
612 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
613 assert_se(streq(s
, syspath
));
614 d
= sd_device_unref(d
);
616 assert_se(r
== -ENODEV
|| ERRNO_IS_PRIVILEGE(r
));
618 assert_se(path
= path_join(tmpdir
, sysname
));
619 assert_se(symlink(syspath
, path
) >= 0);
620 assert_se(sd_device_new_from_path(&d
, path
) >= 0);
621 assert_se(sd_device_get_syspath(d
, &s
) >= 0);
622 assert_se(streq(s
, syspath
));
626 static void test_devname_from_devnum_one(const char *path
) {
627 _cleanup_free_
char *resolved
= NULL
;
630 log_debug("> %s", path
);
632 if (stat(path
, &st
) < 0) {
633 assert_se(errno
== ENOENT
);
634 log_notice("Path %s not found, skipping test", path
);
638 assert_se(devname_from_devnum(st
.st_mode
, st
.st_rdev
, &resolved
) >= 0);
639 assert_se(path_equal(path
, resolved
));
640 resolved
= mfree(resolved
);
641 assert_se(devname_from_stat_rdev(&st
, &resolved
) >= 0);
642 assert_se(path_equal(path
, resolved
));
645 TEST(devname_from_devnum
) {
646 test_devname_from_devnum_one("/dev/null");
647 test_devname_from_devnum_one("/dev/zero");
648 test_devname_from_devnum_one("/dev/full");
649 test_devname_from_devnum_one("/dev/random");
650 test_devname_from_devnum_one("/dev/urandom");
651 test_devname_from_devnum_one("/dev/tty");
653 if (is_device_node("/run/systemd/inaccessible/blk") > 0) {
654 test_devname_from_devnum_one("/run/systemd/inaccessible/chr");
655 test_devname_from_devnum_one("/run/systemd/inaccessible/blk");
659 DEFINE_TEST_MAIN(LOG_INFO
);