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
;
374 if (!device_in_subsystem(device
, *prioritized_subsystem
))
377 devices
[n
++] = sd_device_ref(device
);
380 _cleanup_free_
char *q
= NULL
;
382 r
= path_extract_directory(p
?: syspath
, &q
);
383 if (r
== -EADDRNOTAVAIL
)
388 device
= hashmap_get(enumerator
->devices_by_syspath
, q
);
390 devices
[n
++] = sd_device_ref(device
);
392 free_and_replace(p
, q
);
398 /* We cannot remove multiple entries in the loop HASHMAP_FOREACH_KEY() above. */
399 for (size_t i
= m
; i
< n
; i
++) {
400 r
= sd_device_get_syspath(devices
[i
], &syspath
);
404 assert_se(hashmap_remove(enumerator
->devices_by_syspath
, syspath
) == devices
[i
]);
405 sd_device_unref(devices
[i
]);
412 typesafe_qsort(devices
+ n_sorted
, n
- n_sorted
, device_compare
);
416 HASHMAP_FOREACH(device
, enumerator
->devices_by_syspath
)
417 devices
[n
++] = sd_device_ref(device
);
419 /* Move all devices back to the hashmap. Otherwise, devices added by
420 * udev_enumerate_add_syspath() -> device_enumerator_add_device() may not be listed. */
421 for (size_t i
= 0; i
< n_sorted
; i
++) {
424 r
= sd_device_get_syspath(devices
[i
], &syspath
);
428 r
= hashmap_put(enumerator
->devices_by_syspath
, syspath
, devices
[i
]);
433 sd_device_ref(devices
[i
]);
436 typesafe_qsort(devices
+ n_sorted
, n
- n_sorted
, device_compare
);
438 device_unref_many(enumerator
->devices
, enumerator
->n_devices
);
440 enumerator
->n_devices
= n
;
441 free_and_replace(enumerator
->devices
, devices
);
443 enumerator
->sorted
= true;
447 device_unref_many(devices
, n
);
452 int device_enumerator_add_device(sd_device_enumerator
*enumerator
, sd_device
*device
) {
456 assert_return(enumerator
, -EINVAL
);
457 assert_return(device
, -EINVAL
);
459 r
= sd_device_get_syspath(device
, &syspath
);
463 r
= hashmap_ensure_put(&enumerator
->devices_by_syspath
, &string_hash_ops
, syspath
, device
);
464 if (IN_SET(r
, -EEXIST
, 0))
469 sd_device_ref(device
);
471 enumerator
->sorted
= false;
475 static bool match_property(Hashmap
*properties
, sd_device
*device
, bool match_all
) {
476 const char *property_pattern
;
477 char * const *value_patterns
;
481 /* Unlike device_match_sysattr(), this accepts device that has at least one matching property. */
483 if (hashmap_isempty(properties
))
486 HASHMAP_FOREACH_KEY(value_patterns
, property_pattern
, properties
) {
489 FOREACH_DEVICE_PROPERTY(device
, property
, value
) {
490 if (fnmatch(property_pattern
, property
, 0) != 0)
493 match
= strv_fnmatch(value_patterns
, value
);
502 if (!match
&& match_all
)
509 static bool match_tag(sd_device_enumerator
*enumerator
, sd_device
*device
) {
515 SET_FOREACH(tag
, enumerator
->match_tag
)
516 if (!sd_device_has_tag(device
, tag
))
522 static bool match_sysname(sd_device_enumerator
*enumerator
, const char *sysname
) {
526 return set_fnmatch(enumerator
->match_sysname
, enumerator
->nomatch_sysname
, sysname
);
529 static int match_initialized(sd_device_enumerator
*enumerator
, sd_device
*device
) {
535 if (enumerator
->match_initialized
== MATCH_INITIALIZED_ALL
)
538 r
= sd_device_get_is_initialized(device
);
539 if (r
== -ENOENT
) /* this is necessarily racey, so ignore missing devices */
544 if (enumerator
->match_initialized
== MATCH_INITIALIZED_COMPAT
) {
545 /* only devices that have no devnode/ifindex or have a db entry are accepted. */
549 if (sd_device_get_devnum(device
, NULL
) >= 0)
552 if (sd_device_get_ifindex(device
, NULL
) >= 0)
558 return (enumerator
->match_initialized
== MATCH_INITIALIZED_NO
) == (r
== 0);
561 static bool match_subsystem(sd_device_enumerator
*enumerator
, const char *subsystem
) {
567 return set_fnmatch(enumerator
->match_subsystem
, enumerator
->nomatch_subsystem
, subsystem
);
570 typedef enum MatchFlag
{
571 MATCH_SYSNAME
= 1u << 0,
572 MATCH_SUBSYSTEM
= 1u << 1,
573 MATCH_PARENT
= 1u << 2,
576 MATCH_ALL
= (1u << 4) - 1,
579 static int test_matches(
580 sd_device_enumerator
*enumerator
,
589 if (FLAGS_SET(flags
, MATCH_SYSNAME
)) {
592 r
= sd_device_get_sysname(device
, &sysname
);
596 if (!match_sysname(enumerator
, sysname
))
600 if (FLAGS_SET(flags
, MATCH_SUBSYSTEM
)) {
601 const char *subsystem
;
603 r
= sd_device_get_subsystem(device
, &subsystem
);
609 if (!match_subsystem(enumerator
, subsystem
))
613 if (FLAGS_SET(flags
, MATCH_PARENT
) &&
614 !device_match_parent(device
, enumerator
->match_parent
, NULL
))
617 if (FLAGS_SET(flags
, MATCH_TAG
) &&
618 !match_tag(enumerator
, device
))
621 r
= match_initialized(enumerator
, device
);
625 if (!match_property(enumerator
->match_property
, device
, /* match_all = */ false))
628 if (!match_property(enumerator
->match_property_required
, device
, /* match_all = */ true))
631 if (!device_match_sysattr(device
, enumerator
->match_sysattr
, enumerator
->nomatch_sysattr
))
637 static int enumerator_add_parent_devices(
638 sd_device_enumerator
*enumerator
,
648 r
= sd_device_get_parent(device
, &device
);
649 if (r
== -ENOENT
) /* Reached the top? */
654 r
= test_matches(enumerator
, device
, flags
);
660 r
= device_enumerator_add_device(enumerator
, device
);
661 if (r
<= 0) /* r == 0 means the device already exists, then no need to go further up. */
666 int device_enumerator_add_parent_devices(sd_device_enumerator
*enumerator
, sd_device
*device
) {
667 return enumerator_add_parent_devices(enumerator
, device
, MATCH_ALL
& (~MATCH_PARENT
));
670 static bool relevant_sysfs_subdir(const struct dirent
*de
) {
673 if (de
->d_name
[0] == '.')
676 /* Also filter out regular files and such, i.e. stuff that definitely isn't a kobject path. (Note
677 * that we rely on the fact that sysfs fills in d_type here, i.e. doesn't do DT_UNKNOWN) */
678 return IN_SET(de
->d_type
, DT_DIR
, DT_LNK
);
681 static int enumerator_scan_dir_and_add_devices(
682 sd_device_enumerator
*enumerator
,
685 const char *subdir2
) {
687 _cleanup_closedir_
DIR *dir
= NULL
;
694 path
= strjoina("/sys/", basedir
, "/");
697 path
= strjoina(path
, subdir1
, "/");
700 path
= strjoina(path
, subdir2
, "/");
704 bool ignore
= errno
== ENOENT
;
706 /* this is necessarily racey, so ignore missing directories */
707 log_debug_errno(errno
,
708 "sd-device-enumerator: Failed to open directory %s%s: %m",
709 path
, ignore
? ", ignoring" : "");
710 return ignore
? 0 : -errno
;
713 FOREACH_DIRENT_ALL(de
, dir
, return -errno
) {
714 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
715 char syspath
[strlen(path
) + 1 + strlen(de
->d_name
) + 1];
717 if (!relevant_sysfs_subdir(de
))
720 if (!match_sysname(enumerator
, de
->d_name
))
723 (void) sprintf(syspath
, "%s%s", path
, de
->d_name
);
725 k
= sd_device_new_from_syspath(&device
, syspath
);
728 /* this is necessarily racey, so ignore missing devices */
734 k
= test_matches(enumerator
, device
, MATCH_ALL
& (~MATCH_SYSNAME
)); /* sysname is already tested. */
741 k
= device_enumerator_add_device(enumerator
, device
);
745 /* Also include all potentially matching parent devices in the enumeration. These are things
746 * like root busses — e.g. /sys/devices/pci0000:00/ or /sys/devices/pnp0/, which ar not
747 * linked from /sys/class/ or /sys/bus/, hence pick them up explicitly here. */
748 k
= enumerator_add_parent_devices(enumerator
, device
, MATCH_ALL
);
756 static int enumerator_scan_dir(
757 sd_device_enumerator
*enumerator
,
760 const char *subsystem
) {
762 _cleanup_closedir_
DIR *dir
= NULL
;
766 path
= strjoina("/sys/", basedir
);
770 bool ignore
= errno
== ENOENT
;
772 log_debug_errno(errno
,
773 "sd-device-enumerator: Failed to open directory %s%s: %m",
774 path
, ignore
? ", ignoring" : "");
775 return ignore
? 0 : -errno
;
778 FOREACH_DIRENT_ALL(de
, dir
, return -errno
) {
781 if (!relevant_sysfs_subdir(de
))
784 if (!match_subsystem(enumerator
, subsystem
?: de
->d_name
))
787 k
= enumerator_scan_dir_and_add_devices(enumerator
, basedir
, de
->d_name
, subdir
);
795 static int enumerator_scan_devices_tag(sd_device_enumerator
*enumerator
, const char *tag
) {
796 _cleanup_closedir_
DIR *dir
= NULL
;
803 path
= strjoina("/run/udev/tags/", tag
);
807 bool ignore
= errno
== ENOENT
;
809 log_debug_errno(errno
,
810 "sd-device-enumerator: Failed to open directory %s%s: %m",
811 path
, ignore
? ", ignoring" : "");
812 return ignore
? 0 : -errno
;
815 /* TODO: filter away subsystems? */
817 FOREACH_DIRENT_ALL(de
, dir
, return -errno
) {
818 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
821 if (de
->d_name
[0] == '.')
824 k
= sd_device_new_from_device_id(&device
, de
->d_name
);
827 /* this is necessarily racy, so ignore missing devices */
833 /* Generated from tag, hence not necessary to check tag again. */
834 k
= test_matches(enumerator
, device
, MATCH_ALL
& (~MATCH_TAG
));
840 k
= device_enumerator_add_device(enumerator
, device
);
850 static int enumerator_scan_devices_tags(sd_device_enumerator
*enumerator
) {
856 SET_FOREACH(tag
, enumerator
->match_tag
) {
859 k
= enumerator_scan_devices_tag(enumerator
, tag
);
867 static int parent_add_child(sd_device_enumerator
*enumerator
, const char *path
, MatchFlag flags
) {
868 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
871 r
= sd_device_new_from_syspath(&device
, path
);
873 /* this is necessarily racy, so ignore missing devices */
878 r
= test_matches(enumerator
, device
, flags
);
882 return device_enumerator_add_device(enumerator
, device
);
885 static int parent_crawl_children(sd_device_enumerator
*enumerator
, const char *path
, Set
**stack
) {
886 _cleanup_closedir_
DIR *dir
= NULL
;
895 bool ignore
= errno
== ENOENT
;
897 log_debug_errno(errno
,
898 "sd-device-enumerator: Failed to open directory %s%s: %m",
899 path
, ignore
? ", ignoring" : "");
900 return ignore
? 0 : -errno
;
903 FOREACH_DIRENT_ALL(de
, dir
, return -errno
) {
904 _cleanup_free_
char *child
= NULL
;
907 if (de
->d_name
[0] == '.')
910 if (de
->d_type
!= DT_DIR
)
913 child
= path_join(path
, de
->d_name
);
917 /* Let's check sysname filter earlier. The other tests require the sd-device object created
918 * from the path, thus much costly. */
919 if (match_sysname(enumerator
, de
->d_name
)) {
920 k
= parent_add_child(enumerator
, child
, MATCH_ALL
& (~(MATCH_SYSNAME
|MATCH_PARENT
)));
925 k
= set_ensure_consume(stack
, &path_hash_ops_free
, TAKE_PTR(child
));
933 static int enumerator_scan_devices_children(sd_device_enumerator
*enumerator
) {
934 _cleanup_set_free_ Set
*stack
= NULL
;
940 SET_FOREACH(path
, enumerator
->match_parent
) {
941 k
= parent_add_child(enumerator
, path
, MATCH_ALL
& (~MATCH_PARENT
));
945 k
= parent_crawl_children(enumerator
, path
, &stack
);
951 _cleanup_free_
char *p
= NULL
;
953 p
= set_steal_first(stack
);
957 k
= parent_crawl_children(enumerator
, p
, &stack
);
963 static int enumerator_scan_devices_all(sd_device_enumerator
*enumerator
) {
966 k
= enumerator_scan_dir(enumerator
, "bus", "devices", NULL
);
968 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan /sys/bus: %m");
970 k
= enumerator_scan_dir(enumerator
, "class", NULL
, NULL
);
972 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan /sys/class: %m");
977 int device_enumerator_scan_devices(sd_device_enumerator
*enumerator
) {
982 if (enumerator
->scan_uptodate
&&
983 enumerator
->type
== DEVICE_ENUMERATION_TYPE_DEVICES
)
986 device_enumerator_unref_devices(enumerator
);
988 if (!set_isempty(enumerator
->match_tag
)) {
989 k
= enumerator_scan_devices_tags(enumerator
);
992 } else if (enumerator
->match_parent
) {
993 k
= enumerator_scan_devices_children(enumerator
);
997 k
= enumerator_scan_devices_all(enumerator
);
1002 enumerator
->scan_uptodate
= true;
1003 enumerator
->type
= DEVICE_ENUMERATION_TYPE_DEVICES
;
1008 _public_ sd_device
*sd_device_enumerator_get_device_first(sd_device_enumerator
*enumerator
) {
1009 assert_return(enumerator
, NULL
);
1011 if (device_enumerator_scan_devices(enumerator
) < 0)
1014 if (enumerator_sort_devices(enumerator
) < 0)
1017 enumerator
->current_device_index
= 0;
1019 if (enumerator
->n_devices
== 0)
1022 return enumerator
->devices
[0];
1025 _public_ sd_device
*sd_device_enumerator_get_device_next(sd_device_enumerator
*enumerator
) {
1026 assert_return(enumerator
, NULL
);
1028 if (!enumerator
->scan_uptodate
||
1029 !enumerator
->sorted
||
1030 enumerator
->type
!= DEVICE_ENUMERATION_TYPE_DEVICES
||
1031 enumerator
->current_device_index
+ 1 >= enumerator
->n_devices
)
1034 return enumerator
->devices
[++enumerator
->current_device_index
];
1037 int device_enumerator_scan_subsystems(sd_device_enumerator
*enumerator
) {
1042 if (enumerator
->scan_uptodate
&&
1043 enumerator
->type
== DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
)
1046 device_enumerator_unref_devices(enumerator
);
1049 if (match_subsystem(enumerator
, "module")) {
1050 k
= enumerator_scan_dir_and_add_devices(enumerator
, "module", NULL
, NULL
);
1052 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan modules: %m");
1055 /* subsystems (only buses support coldplug) */
1056 if (match_subsystem(enumerator
, "subsystem")) {
1057 k
= enumerator_scan_dir_and_add_devices(enumerator
, "bus", NULL
, NULL
);
1059 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan subsystems: %m");
1062 /* subsystem drivers */
1063 if (match_subsystem(enumerator
, "drivers")) {
1064 k
= enumerator_scan_dir(enumerator
, "bus", "drivers", "drivers");
1066 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan drivers: %m");
1069 enumerator
->scan_uptodate
= true;
1070 enumerator
->type
= DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
;
1075 _public_ sd_device
*sd_device_enumerator_get_subsystem_first(sd_device_enumerator
*enumerator
) {
1076 assert_return(enumerator
, NULL
);
1078 if (device_enumerator_scan_subsystems(enumerator
) < 0)
1081 if (enumerator_sort_devices(enumerator
) < 0)
1084 enumerator
->current_device_index
= 0;
1086 if (enumerator
->n_devices
== 0)
1089 return enumerator
->devices
[0];
1092 _public_ sd_device
*sd_device_enumerator_get_subsystem_next(sd_device_enumerator
*enumerator
) {
1093 assert_return(enumerator
, NULL
);
1095 if (!enumerator
->scan_uptodate
||
1096 !enumerator
->sorted
||
1097 enumerator
->type
!= DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
||
1098 enumerator
->current_device_index
+ 1 >= enumerator
->n_devices
)
1101 return enumerator
->devices
[++enumerator
->current_device_index
];
1104 int device_enumerator_scan_devices_and_subsystems(sd_device_enumerator
*enumerator
) {
1109 if (enumerator
->scan_uptodate
&&
1110 enumerator
->type
== DEVICE_ENUMERATION_TYPE_ALL
)
1113 device_enumerator_unref_devices(enumerator
);
1115 if (!set_isempty(enumerator
->match_tag
))
1116 r
= enumerator_scan_devices_tags(enumerator
);
1117 else if (enumerator
->match_parent
)
1118 r
= enumerator_scan_devices_children(enumerator
);
1122 r
= enumerator_scan_devices_all(enumerator
);
1124 if (match_subsystem(enumerator
, "module")) {
1125 k
= enumerator_scan_dir_and_add_devices(enumerator
, "module", NULL
, NULL
);
1127 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan modules: %m");
1129 if (match_subsystem(enumerator
, "subsystem")) {
1130 k
= enumerator_scan_dir_and_add_devices(enumerator
, "bus", NULL
, NULL
);
1132 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan subsystems: %m");
1135 if (match_subsystem(enumerator
, "drivers")) {
1136 k
= enumerator_scan_dir(enumerator
, "bus", "drivers", "drivers");
1138 r
= log_debug_errno(k
, "sd-device-enumerator: Failed to scan drivers: %m");
1142 enumerator
->scan_uptodate
= true;
1143 enumerator
->type
= DEVICE_ENUMERATION_TYPE_ALL
;
1148 sd_device
*device_enumerator_get_first(sd_device_enumerator
*enumerator
) {
1149 assert_return(enumerator
, NULL
);
1151 if (!enumerator
->scan_uptodate
)
1154 if (enumerator_sort_devices(enumerator
) < 0)
1157 enumerator
->current_device_index
= 0;
1159 if (enumerator
->n_devices
== 0)
1162 return enumerator
->devices
[0];
1165 sd_device
*device_enumerator_get_next(sd_device_enumerator
*enumerator
) {
1166 assert_return(enumerator
, NULL
);
1168 if (!enumerator
->scan_uptodate
||
1169 !enumerator
->sorted
||
1170 enumerator
->current_device_index
+ 1 >= enumerator
->n_devices
)
1173 return enumerator
->devices
[++enumerator
->current_device_index
];
1176 sd_device
**device_enumerator_get_devices(sd_device_enumerator
*enumerator
, size_t *ret_n_devices
) {
1178 assert(ret_n_devices
);
1180 if (!enumerator
->scan_uptodate
)
1183 if (enumerator_sort_devices(enumerator
) < 0)
1186 *ret_n_devices
= enumerator
->n_devices
;
1187 return enumerator
->devices
;