1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
9 #include "device-enumerator-private.h"
10 #include "device-filter.h"
11 #include "device-util.h"
12 #include "dirent-util.h"
15 #include "sort-util.h"
16 #include "string-util.h"
19 typedef enum DeviceEnumerationType
{
20 DEVICE_ENUMERATION_TYPE_DEVICES
,
21 DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
,
22 DEVICE_ENUMERATION_TYPE_ALL
,
23 _DEVICE_ENUMERATION_TYPE_MAX
,
24 _DEVICE_ENUMERATION_TYPE_INVALID
= -EINVAL
,
25 } DeviceEnumerationType
;
27 struct sd_device_enumerator
{
30 DeviceEnumerationType type
;
31 Hashmap
*devices_by_syspath
;
33 size_t n_devices
, current_device_index
;
37 char **prioritized_subsystems
;
39 Set
*nomatch_subsystem
;
40 Hashmap
*match_sysattr
;
41 Hashmap
*nomatch_sysattr
;
42 Hashmap
*match_property
;
43 Hashmap
*match_property_required
;
48 MatchInitializedType match_initialized
;
51 _public_
int sd_device_enumerator_new(sd_device_enumerator
**ret
) {
52 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*enumerator
= NULL
;
56 enumerator
= new(sd_device_enumerator
, 1);
60 *enumerator
= (sd_device_enumerator
) {
62 .type
= _DEVICE_ENUMERATION_TYPE_INVALID
,
63 .match_initialized
= MATCH_INITIALIZED_COMPAT
,
66 *ret
= TAKE_PTR(enumerator
);
71 static void device_unref_many(sd_device
**devices
, size_t n
) {
72 assert(devices
|| n
== 0);
74 for (size_t i
= 0; i
< n
; i
++)
75 sd_device_unref(devices
[i
]);
78 static void device_enumerator_unref_devices(sd_device_enumerator
*enumerator
) {
81 hashmap_clear_with_destructor(enumerator
->devices_by_syspath
, sd_device_unref
);
82 device_unref_many(enumerator
->devices
, enumerator
->n_devices
);
83 enumerator
->devices
= mfree(enumerator
->devices
);
84 enumerator
->n_devices
= 0;
87 static sd_device_enumerator
*device_enumerator_free(sd_device_enumerator
*enumerator
) {
90 device_enumerator_unref_devices(enumerator
);
92 hashmap_free(enumerator
->devices_by_syspath
);
93 strv_free(enumerator
->prioritized_subsystems
);
94 set_free(enumerator
->match_subsystem
);
95 set_free(enumerator
->nomatch_subsystem
);
96 hashmap_free(enumerator
->match_sysattr
);
97 hashmap_free(enumerator
->nomatch_sysattr
);
98 hashmap_free(enumerator
->match_property
);
99 hashmap_free(enumerator
->match_property_required
);
100 set_free(enumerator
->match_sysname
);
101 set_free(enumerator
->nomatch_sysname
);
102 set_free(enumerator
->match_tag
);
103 set_free(enumerator
->match_parent
);
105 return mfree(enumerator
);
108 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device_enumerator
, sd_device_enumerator
, device_enumerator_free
);
110 int device_enumerator_add_prioritized_subsystem(sd_device_enumerator
*enumerator
, const char *subsystem
) {
116 if (strv_contains(enumerator
->prioritized_subsystems
, subsystem
))
119 r
= strv_extend(&enumerator
->prioritized_subsystems
, subsystem
);
123 enumerator
->scan_uptodate
= false;
128 _public_
int sd_device_enumerator_add_match_subsystem(sd_device_enumerator
*enumerator
, const char *subsystem
, int match
) {
132 assert_return(enumerator
, -EINVAL
);
133 assert_return(subsystem
, -EINVAL
);
136 set
= &enumerator
->match_subsystem
;
138 set
= &enumerator
->nomatch_subsystem
;
140 r
= set_put_strdup(set
, subsystem
);
144 enumerator
->scan_uptodate
= false;
149 _public_
int sd_device_enumerator_add_match_sysattr(sd_device_enumerator
*enumerator
, const char *sysattr
, const char *value
, int match
) {
153 assert_return(enumerator
, -EINVAL
);
154 assert_return(sysattr
, -EINVAL
);
157 hashmap
= &enumerator
->match_sysattr
;
159 hashmap
= &enumerator
->nomatch_sysattr
;
161 r
= update_match_strv(hashmap
, sysattr
, value
, /* clear_on_null = */ true);
165 enumerator
->scan_uptodate
= false;
170 _public_
int sd_device_enumerator_add_match_property(sd_device_enumerator
*enumerator
, const char *property
, const char *value
) {
173 assert_return(enumerator
, -EINVAL
);
174 assert_return(property
, -EINVAL
);
176 r
= update_match_strv(&enumerator
->match_property
, property
, value
, /* clear_on_null = */ false);
180 enumerator
->scan_uptodate
= false;
185 _public_
int sd_device_enumerator_add_match_property_required(sd_device_enumerator
*enumerator
, const char *property
, const char *value
) {
188 assert_return(enumerator
, -EINVAL
);
189 assert_return(property
, -EINVAL
);
191 r
= update_match_strv(&enumerator
->match_property_required
, property
, value
, /* clear_on_null = */ false);
195 enumerator
->scan_uptodate
= false;
200 static int device_enumerator_add_match_sysname(sd_device_enumerator
*enumerator
, const char *sysname
, bool match
) {
203 assert_return(enumerator
, -EINVAL
);
204 assert_return(sysname
, -EINVAL
);
206 r
= set_put_strdup(match
? &enumerator
->match_sysname
: &enumerator
->nomatch_sysname
, sysname
);
210 enumerator
->scan_uptodate
= false;
215 _public_
int sd_device_enumerator_add_match_sysname(sd_device_enumerator
*enumerator
, const char *sysname
) {
216 return device_enumerator_add_match_sysname(enumerator
, sysname
, true);
219 _public_
int sd_device_enumerator_add_nomatch_sysname(sd_device_enumerator
*enumerator
, const char *sysname
) {
220 return device_enumerator_add_match_sysname(enumerator
, sysname
, false);
223 _public_
int sd_device_enumerator_add_match_tag(sd_device_enumerator
*enumerator
, const char *tag
) {
226 assert_return(enumerator
, -EINVAL
);
227 assert_return(tag
, -EINVAL
);
229 r
= set_put_strdup(&enumerator
->match_tag
, tag
);
233 enumerator
->scan_uptodate
= false;
238 int device_enumerator_add_match_parent_incremental(sd_device_enumerator
*enumerator
, sd_device
*parent
) {
245 r
= sd_device_get_syspath(parent
, &path
);
249 r
= set_put_strdup(&enumerator
->match_parent
, path
);
253 enumerator
->scan_uptodate
= false;
258 _public_
int sd_device_enumerator_add_match_parent(sd_device_enumerator
*enumerator
, sd_device
*parent
) {
259 assert_return(enumerator
, -EINVAL
);
260 assert_return(parent
, -EINVAL
);
262 set_clear(enumerator
->match_parent
);
264 return device_enumerator_add_match_parent_incremental(enumerator
, parent
);
267 _public_
int sd_device_enumerator_allow_uninitialized(sd_device_enumerator
*enumerator
) {
268 assert_return(enumerator
, -EINVAL
);
270 enumerator
->match_initialized
= MATCH_INITIALIZED_ALL
;
272 enumerator
->scan_uptodate
= false;
277 int device_enumerator_add_match_is_initialized(sd_device_enumerator
*enumerator
, MatchInitializedType type
) {
278 assert_return(enumerator
, -EINVAL
);
279 assert_return(type
>= 0 && type
< _MATCH_INITIALIZED_MAX
, -EINVAL
);
281 enumerator
->match_initialized
= type
;
283 enumerator
->scan_uptodate
= false;
288 static int sound_device_compare(const char *devpath_a
, const char *devpath_b
) {
289 const char *sound_a
, *sound_b
;
295 /* For sound cards the control device must be enumerated last to make sure it's the final
296 * device node that gets ACLs applied. Applications rely on this fact and use ACL changes on
297 * the control node as an indicator that the ACL change of the entire sound card completed. The
298 * kernel makes this guarantee when creating those devices, and hence we should too when
299 * enumerating them. */
301 sound_a
= strstrafter(devpath_a
, "/sound/card");
305 sound_a
= strchr(devpath_a
, '/');
309 prefix_len
= sound_a
- devpath_a
;
311 if (!strneq(devpath_a
, devpath_b
, prefix_len
))
314 sound_b
= devpath_b
+ prefix_len
;
316 return CMP(!!startswith(sound_a
, "/controlC"),
317 !!startswith(sound_b
, "/controlC"));
320 static bool devpath_is_late_block(const char *devpath
) {
323 return strstr(devpath
, "/block/md") || strstr(devpath
, "/block/dm-");
326 static int device_compare(sd_device
* const *a
, sd_device
* const *b
) {
327 const char *devpath_a
, *devpath_b
;
335 assert_se(sd_device_get_devpath(*(sd_device
**) a
, &devpath_a
) >= 0);
336 assert_se(sd_device_get_devpath(*(sd_device
**) b
, &devpath_b
) >= 0);
338 r
= sound_device_compare(devpath_a
, devpath_b
);
342 /* md and dm devices are enumerated after all other devices */
343 r
= CMP(devpath_is_late_block(devpath_a
), devpath_is_late_block(devpath_b
));
347 return path_compare(devpath_a
, devpath_b
);
350 static int enumerator_sort_devices(sd_device_enumerator
*enumerator
) {
351 size_t n_sorted
= 0, n
= 0;
358 if (enumerator
->sorted
)
361 devices
= new(sd_device
*, hashmap_size(enumerator
->devices_by_syspath
));
365 STRV_FOREACH(prioritized_subsystem
, enumerator
->prioritized_subsystems
) {
371 HASHMAP_FOREACH_KEY(device
, syspath
, enumerator
->devices_by_syspath
) {
372 _cleanup_free_
char *p
= NULL
;
375 if (sd_device_get_subsystem(device
, &subsys
) < 0)
378 if (!streq(subsys
, *prioritized_subsystem
))
381 devices
[n
++] = sd_device_ref(device
);
384 _cleanup_free_
char *q
= NULL
;
386 r
= path_extract_directory(p
?: syspath
, &q
);
387 if (r
== -EADDRNOTAVAIL
)
392 device
= hashmap_get(enumerator
->devices_by_syspath
, q
);
394 devices
[n
++] = sd_device_ref(device
);
396 free_and_replace(p
, q
);
402 /* We cannot remove multiple entries in the loop HASHMAP_FOREACH_KEY() above. */
403 for (size_t i
= m
; i
< n
; i
++) {
404 r
= sd_device_get_syspath(devices
[i
], &syspath
);
408 assert_se(hashmap_remove(enumerator
->devices_by_syspath
, syspath
) == devices
[i
]);
409 sd_device_unref(devices
[i
]);
416 typesafe_qsort(devices
+ n_sorted
, n
- n_sorted
, device_compare
);
420 HASHMAP_FOREACH(device
, enumerator
->devices_by_syspath
)
421 devices
[n
++] = sd_device_ref(device
);
423 /* Move all devices back to the hashmap. Otherwise, devices added by
424 * udev_enumerate_add_syspath() -> device_enumerator_add_device() may not be listed. */
425 for (size_t i
= 0; i
< n_sorted
; i
++) {
428 r
= sd_device_get_syspath(devices
[i
], &syspath
);
432 r
= hashmap_put(enumerator
->devices_by_syspath
, syspath
, devices
[i
]);
437 sd_device_ref(devices
[i
]);
440 typesafe_qsort(devices
+ n_sorted
, n
- n_sorted
, device_compare
);
442 device_unref_many(enumerator
->devices
, enumerator
->n_devices
);
444 enumerator
->n_devices
= n
;
445 free_and_replace(enumerator
->devices
, devices
);
447 enumerator
->sorted
= true;
451 device_unref_many(devices
, n
);
456 int device_enumerator_add_device(sd_device_enumerator
*enumerator
, sd_device
*device
) {
460 assert_return(enumerator
, -EINVAL
);
461 assert_return(device
, -EINVAL
);
463 r
= sd_device_get_syspath(device
, &syspath
);
467 r
= hashmap_ensure_put(&enumerator
->devices_by_syspath
, &string_hash_ops
, syspath
, device
);
468 if (IN_SET(r
, -EEXIST
, 0))
473 sd_device_ref(device
);
475 enumerator
->sorted
= false;
479 static bool match_property(Hashmap
*properties
, sd_device
*device
, bool match_all
) {
480 const char *property_pattern
;
481 char * const *value_patterns
;
485 /* Unlike device_match_sysattr(), this accepts device that has at least one matching property. */
487 if (hashmap_isempty(properties
))
490 HASHMAP_FOREACH_KEY(value_patterns
, property_pattern
, properties
) {
493 FOREACH_DEVICE_PROPERTY(device
, property
, value
) {
494 if (fnmatch(property_pattern
, property
, 0) != 0)
497 match
= strv_fnmatch(value_patterns
, value
);
506 if (!match
&& match_all
)
513 static bool match_tag(sd_device_enumerator
*enumerator
, sd_device
*device
) {
519 SET_FOREACH(tag
, enumerator
->match_tag
)
520 if (!sd_device_has_tag(device
, tag
))
526 static bool match_sysname(sd_device_enumerator
*enumerator
, const char *sysname
) {
530 return set_fnmatch(enumerator
->match_sysname
, enumerator
->nomatch_sysname
, sysname
);
533 static int match_initialized(sd_device_enumerator
*enumerator
, sd_device
*device
) {
539 if (enumerator
->match_initialized
== MATCH_INITIALIZED_ALL
)
542 r
= sd_device_get_is_initialized(device
);
543 if (r
== -ENOENT
) /* this is necessarily racey, so ignore missing devices */
548 if (enumerator
->match_initialized
== MATCH_INITIALIZED_COMPAT
) {
549 /* only devices that have no devnode/ifindex or have a db entry are accepted. */
553 if (sd_device_get_devnum(device
, NULL
) >= 0)
556 if (sd_device_get_ifindex(device
, NULL
) >= 0)
562 return (enumerator
->match_initialized
== MATCH_INITIALIZED_NO
) == (r
== 0);
565 static bool match_subsystem(sd_device_enumerator
*enumerator
, const char *subsystem
) {
571 return set_fnmatch(enumerator
->match_subsystem
, enumerator
->nomatch_subsystem
, subsystem
);
574 typedef enum MatchFlag
{
575 MATCH_SYSNAME
= 1u << 0,
576 MATCH_SUBSYSTEM
= 1u << 1,
577 MATCH_PARENT
= 1u << 2,
580 MATCH_ALL
= (1u << 4) - 1,
583 static int test_matches(
584 sd_device_enumerator
*enumerator
,
593 if (FLAGS_SET(flags
, MATCH_SYSNAME
)) {
596 r
= sd_device_get_sysname(device
, &sysname
);
600 if (!match_sysname(enumerator
, sysname
))
604 if (FLAGS_SET(flags
, MATCH_SUBSYSTEM
)) {
605 const char *subsystem
;
607 r
= sd_device_get_subsystem(device
, &subsystem
);
613 if (!match_subsystem(enumerator
, subsystem
))
617 if (FLAGS_SET(flags
, MATCH_PARENT
) &&
618 !device_match_parent(device
, enumerator
->match_parent
, NULL
))
621 if (FLAGS_SET(flags
, MATCH_TAG
) &&
622 !match_tag(enumerator
, device
))
625 r
= match_initialized(enumerator
, device
);
629 if (!match_property(enumerator
->match_property
, device
, /* match_all = */ false))
632 if (!match_property(enumerator
->match_property_required
, device
, /* match_all = */ true))
635 if (!device_match_sysattr(device
, enumerator
->match_sysattr
, enumerator
->nomatch_sysattr
))
641 static int enumerator_add_parent_devices(
642 sd_device_enumerator
*enumerator
,
652 r
= sd_device_get_parent(device
, &device
);
653 if (r
== -ENOENT
) /* Reached the top? */
658 r
= test_matches(enumerator
, device
, flags
);
664 r
= device_enumerator_add_device(enumerator
, device
);
665 if (r
<= 0) /* r == 0 means the device already exists, then no need to go further up. */
670 int device_enumerator_add_parent_devices(sd_device_enumerator
*enumerator
, sd_device
*device
) {
671 return enumerator_add_parent_devices(enumerator
, device
, MATCH_ALL
& (~MATCH_PARENT
));
674 static bool relevant_sysfs_subdir(const struct dirent
*de
) {
677 if (de
->d_name
[0] == '.')
680 /* Also filter out regular files and such, i.e. stuff that definitely isn't a kobject path. (Note
681 * that we rely on the fact that sysfs fills in d_type here, i.e. doesn't do DT_UNKNOWN) */
682 return IN_SET(de
->d_type
, DT_DIR
, DT_LNK
);
685 static int enumerator_scan_dir_and_add_devices(
686 sd_device_enumerator
*enumerator
,
689 const char *subdir2
) {
691 _cleanup_closedir_
DIR *dir
= NULL
;
698 path
= strjoina("/sys/", basedir
, "/");
701 path
= strjoina(path
, subdir1
, "/");
704 path
= strjoina(path
, subdir2
, "/");
708 bool ignore
= errno
== ENOENT
;
710 /* this is necessarily racey, so ignore missing directories */
711 log_debug_errno(errno
,
712 "sd-device-enumerator: Failed to open directory %s%s: %m",
713 path
, ignore
? ", ignoring" : "");
714 return ignore
? 0 : -errno
;
717 FOREACH_DIRENT_ALL(de
, dir
, return -errno
) {
718 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
719 char syspath
[strlen(path
) + 1 + strlen(de
->d_name
) + 1];
721 if (!relevant_sysfs_subdir(de
))
724 if (!match_sysname(enumerator
, de
->d_name
))
727 (void) sprintf(syspath
, "%s%s", path
, de
->d_name
);
729 k
= sd_device_new_from_syspath(&device
, syspath
);
732 /* this is necessarily racey, so ignore missing devices */
738 k
= test_matches(enumerator
, device
, MATCH_ALL
& (~MATCH_SYSNAME
)); /* sysname is already tested. */
745 k
= device_enumerator_add_device(enumerator
, device
);
749 /* Also include all potentially matching parent devices in the enumeration. These are things
750 * like root busses — e.g. /sys/devices/pci0000:00/ or /sys/devices/pnp0/, which ar not
751 * linked from /sys/class/ or /sys/bus/, hence pick them up explicitly here. */
752 k
= enumerator_add_parent_devices(enumerator
, device
, MATCH_ALL
);
760 static int enumerator_scan_dir(
761 sd_device_enumerator
*enumerator
,
764 const char *subsystem
) {
766 _cleanup_closedir_
DIR *dir
= NULL
;
770 path
= strjoina("/sys/", basedir
);
774 bool ignore
= errno
== ENOENT
;
776 log_debug_errno(errno
,
777 "sd-device-enumerator: Failed to open directory %s%s: %m",
778 path
, ignore
? ", ignoring" : "");
779 return ignore
? 0 : -errno
;
782 FOREACH_DIRENT_ALL(de
, dir
, return -errno
) {
785 if (!relevant_sysfs_subdir(de
))
788 if (!match_subsystem(enumerator
, subsystem
?: de
->d_name
))
791 k
= enumerator_scan_dir_and_add_devices(enumerator
, basedir
, de
->d_name
, subdir
);
799 static int enumerator_scan_devices_tag(sd_device_enumerator
*enumerator
, const char *tag
) {
800 _cleanup_closedir_
DIR *dir
= NULL
;
807 path
= strjoina("/run/udev/tags/", tag
);
811 bool ignore
= errno
== ENOENT
;
813 log_debug_errno(errno
,
814 "sd-device-enumerator: Failed to open directory %s%s: %m",
815 path
, ignore
? ", ignoring" : "");
816 return ignore
? 0 : -errno
;
819 /* TODO: filter away subsystems? */
821 FOREACH_DIRENT_ALL(de
, dir
, return -errno
) {
822 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
825 if (de
->d_name
[0] == '.')
828 k
= sd_device_new_from_device_id(&device
, de
->d_name
);
831 /* this is necessarily racy, so ignore missing devices */
837 /* Generated from tag, hence not necessary to check tag again. */
838 k
= test_matches(enumerator
, device
, MATCH_ALL
& (~MATCH_TAG
));
844 k
= device_enumerator_add_device(enumerator
, device
);
854 static int enumerator_scan_devices_tags(sd_device_enumerator
*enumerator
) {
860 SET_FOREACH(tag
, enumerator
->match_tag
) {
863 k
= enumerator_scan_devices_tag(enumerator
, tag
);
871 static int parent_add_child(sd_device_enumerator
*enumerator
, const char *path
, MatchFlag flags
) {
872 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
875 r
= sd_device_new_from_syspath(&device
, path
);
877 /* this is necessarily racy, so ignore missing devices */
882 r
= test_matches(enumerator
, device
, flags
);
886 return device_enumerator_add_device(enumerator
, device
);
889 static int parent_crawl_children(sd_device_enumerator
*enumerator
, const char *path
, Set
**stack
) {
890 _cleanup_closedir_
DIR *dir
= NULL
;
899 bool ignore
= errno
== ENOENT
;
901 log_debug_errno(errno
,
902 "sd-device-enumerator: Failed to open directory %s%s: %m",
903 path
, ignore
? ", ignoring" : "");
904 return ignore
? 0 : -errno
;
907 FOREACH_DIRENT_ALL(de
, dir
, return -errno
) {
908 _cleanup_free_
char *child
= NULL
;
911 if (de
->d_name
[0] == '.')
914 if (de
->d_type
!= DT_DIR
)
917 child
= path_join(path
, de
->d_name
);
921 /* Let's check sysname filter earlier. The other tests require the sd-device object created
922 * from the path, thus much costly. */
923 if (match_sysname(enumerator
, de
->d_name
)) {
924 k
= parent_add_child(enumerator
, child
, MATCH_ALL
& (~(MATCH_SYSNAME
|MATCH_PARENT
)));
929 k
= set_ensure_consume(stack
, &path_hash_ops_free
, TAKE_PTR(child
));
937 static int enumerator_scan_devices_children(sd_device_enumerator
*enumerator
) {
938 _cleanup_set_free_ Set
*stack
= NULL
;
944 SET_FOREACH(path
, enumerator
->match_parent
) {
945 k
= parent_add_child(enumerator
, path
, MATCH_ALL
& (~MATCH_PARENT
));
949 k
= parent_crawl_children(enumerator
, path
, &stack
);
955 _cleanup_free_
char *p
= NULL
;
957 p
= set_steal_first(stack
);
961 k
= parent_crawl_children(enumerator
, p
, &stack
);
967 static int enumerator_scan_devices_all(sd_device_enumerator
*enumerator
) {
970 k
= enumerator_scan_dir(enumerator
, "bus", "devices", NULL
);
972 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan /sys/bus: %m");
974 k
= enumerator_scan_dir(enumerator
, "class", NULL
, NULL
);
976 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan /sys/class: %m");
981 int device_enumerator_scan_devices(sd_device_enumerator
*enumerator
) {
986 if (enumerator
->scan_uptodate
&&
987 enumerator
->type
== DEVICE_ENUMERATION_TYPE_DEVICES
)
990 device_enumerator_unref_devices(enumerator
);
992 if (!set_isempty(enumerator
->match_tag
)) {
993 k
= enumerator_scan_devices_tags(enumerator
);
996 } else if (enumerator
->match_parent
) {
997 k
= enumerator_scan_devices_children(enumerator
);
1001 k
= enumerator_scan_devices_all(enumerator
);
1006 enumerator
->scan_uptodate
= true;
1007 enumerator
->type
= DEVICE_ENUMERATION_TYPE_DEVICES
;
1012 _public_ sd_device
*sd_device_enumerator_get_device_first(sd_device_enumerator
*enumerator
) {
1013 assert_return(enumerator
, NULL
);
1015 if (device_enumerator_scan_devices(enumerator
) < 0)
1018 if (enumerator_sort_devices(enumerator
) < 0)
1021 enumerator
->current_device_index
= 0;
1023 if (enumerator
->n_devices
== 0)
1026 return enumerator
->devices
[0];
1029 _public_ sd_device
*sd_device_enumerator_get_device_next(sd_device_enumerator
*enumerator
) {
1030 assert_return(enumerator
, NULL
);
1032 if (!enumerator
->scan_uptodate
||
1033 !enumerator
->sorted
||
1034 enumerator
->type
!= DEVICE_ENUMERATION_TYPE_DEVICES
||
1035 enumerator
->current_device_index
+ 1 >= enumerator
->n_devices
)
1038 return enumerator
->devices
[++enumerator
->current_device_index
];
1041 int device_enumerator_scan_subsystems(sd_device_enumerator
*enumerator
) {
1046 if (enumerator
->scan_uptodate
&&
1047 enumerator
->type
== DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
)
1050 device_enumerator_unref_devices(enumerator
);
1053 if (match_subsystem(enumerator
, "module")) {
1054 k
= enumerator_scan_dir_and_add_devices(enumerator
, "module", NULL
, NULL
);
1056 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan modules: %m");
1059 /* subsystems (only buses support coldplug) */
1060 if (match_subsystem(enumerator
, "subsystem")) {
1061 k
= enumerator_scan_dir_and_add_devices(enumerator
, "bus", NULL
, NULL
);
1063 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan subsystems: %m");
1066 /* subsystem drivers */
1067 if (match_subsystem(enumerator
, "drivers")) {
1068 k
= enumerator_scan_dir(enumerator
, "bus", "drivers", "drivers");
1070 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan drivers: %m");
1073 enumerator
->scan_uptodate
= true;
1074 enumerator
->type
= DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
;
1079 _public_ sd_device
*sd_device_enumerator_get_subsystem_first(sd_device_enumerator
*enumerator
) {
1080 assert_return(enumerator
, NULL
);
1082 if (device_enumerator_scan_subsystems(enumerator
) < 0)
1085 if (enumerator_sort_devices(enumerator
) < 0)
1088 enumerator
->current_device_index
= 0;
1090 if (enumerator
->n_devices
== 0)
1093 return enumerator
->devices
[0];
1096 _public_ sd_device
*sd_device_enumerator_get_subsystem_next(sd_device_enumerator
*enumerator
) {
1097 assert_return(enumerator
, NULL
);
1099 if (!enumerator
->scan_uptodate
||
1100 !enumerator
->sorted
||
1101 enumerator
->type
!= DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
||
1102 enumerator
->current_device_index
+ 1 >= enumerator
->n_devices
)
1105 return enumerator
->devices
[++enumerator
->current_device_index
];
1108 int device_enumerator_scan_devices_and_subsystems(sd_device_enumerator
*enumerator
) {
1113 if (enumerator
->scan_uptodate
&&
1114 enumerator
->type
== DEVICE_ENUMERATION_TYPE_ALL
)
1117 device_enumerator_unref_devices(enumerator
);
1119 if (!set_isempty(enumerator
->match_tag
))
1120 r
= enumerator_scan_devices_tags(enumerator
);
1121 else if (enumerator
->match_parent
)
1122 r
= enumerator_scan_devices_children(enumerator
);
1126 r
= enumerator_scan_devices_all(enumerator
);
1128 if (match_subsystem(enumerator
, "module")) {
1129 k
= enumerator_scan_dir_and_add_devices(enumerator
, "module", NULL
, NULL
);
1131 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan modules: %m");
1133 if (match_subsystem(enumerator
, "subsystem")) {
1134 k
= enumerator_scan_dir_and_add_devices(enumerator
, "bus", NULL
, NULL
);
1136 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan subsystems: %m");
1139 if (match_subsystem(enumerator
, "drivers")) {
1140 k
= enumerator_scan_dir(enumerator
, "bus", "drivers", "drivers");
1142 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan drivers: %m");
1146 enumerator
->scan_uptodate
= true;
1147 enumerator
->type
= DEVICE_ENUMERATION_TYPE_ALL
;
1152 sd_device
*device_enumerator_get_first(sd_device_enumerator
*enumerator
) {
1153 assert_return(enumerator
, NULL
);
1155 if (!enumerator
->scan_uptodate
)
1158 if (enumerator_sort_devices(enumerator
) < 0)
1161 enumerator
->current_device_index
= 0;
1163 if (enumerator
->n_devices
== 0)
1166 return enumerator
->devices
[0];
1169 sd_device
*device_enumerator_get_next(sd_device_enumerator
*enumerator
) {
1170 assert_return(enumerator
, NULL
);
1172 if (!enumerator
->scan_uptodate
||
1173 !enumerator
->sorted
||
1174 enumerator
->current_device_index
+ 1 >= enumerator
->n_devices
)
1177 return enumerator
->devices
[++enumerator
->current_device_index
];
1180 sd_device
**device_enumerator_get_devices(sd_device_enumerator
*enumerator
, size_t *ret_n_devices
) {
1182 assert(ret_n_devices
);
1184 if (!enumerator
->scan_uptodate
)
1187 if (enumerator_sort_devices(enumerator
) < 0)
1190 *ret_n_devices
= enumerator
->n_devices
;
1191 return enumerator
->devices
;