2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/param.h>
33 #include "libudev-private.h"
36 * SECTION:libudev-enumerate
37 * @short_description: lookup and sort sys devices
39 * Lookup devices in the sys filesystem, filter devices by properties,
40 * and return a sorted list of devices.
51 * Opaque object representing one device lookup/sort context.
53 struct udev_enumerate {
56 struct udev_list sysattr_match_list;
57 struct udev_list sysattr_nomatch_list;
58 struct udev_list subsystem_match_list;
59 struct udev_list subsystem_nomatch_list;
60 struct udev_list sysname_match_list;
61 struct udev_list properties_match_list;
62 struct udev_list tags_match_list;
63 struct udev_device *parent_match;
64 struct udev_list devices_list;
65 struct syspath *devices;
66 unsigned int devices_cur;
67 unsigned int devices_max;
68 bool devices_uptodate:1;
69 bool match_is_initialized;
74 * @udev: udev library context
76 * Create an enumeration context to scan /sys.
78 * Returns: an enumeration context.
80 _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev)
82 struct udev_enumerate *udev_enumerate;
84 udev_enumerate = calloc(1, sizeof(struct udev_enumerate));
85 if (udev_enumerate == NULL)
87 udev_enumerate->refcount = 1;
88 udev_enumerate->udev = udev;
89 udev_list_init(udev, &udev_enumerate->sysattr_match_list, false);
90 udev_list_init(udev, &udev_enumerate->sysattr_nomatch_list, false);
91 udev_list_init(udev, &udev_enumerate->subsystem_match_list, true);
92 udev_list_init(udev, &udev_enumerate->subsystem_nomatch_list, true);
93 udev_list_init(udev, &udev_enumerate->sysname_match_list, true);
94 udev_list_init(udev, &udev_enumerate->properties_match_list, false);
95 udev_list_init(udev, &udev_enumerate->tags_match_list, true);
96 udev_list_init(udev, &udev_enumerate->devices_list, false);
97 return udev_enumerate;
101 * udev_enumerate_ref:
102 * @udev_enumerate: context
104 * Take a reference of a enumeration context.
106 * Returns: the passed enumeration context
108 _public_ struct udev_enumerate *udev_enumerate_ref(struct udev_enumerate *udev_enumerate)
110 if (udev_enumerate == NULL)
112 udev_enumerate->refcount++;
113 return udev_enumerate;
117 * udev_enumerate_unref:
118 * @udev_enumerate: context
120 * Drop a reference of an enumeration context. If the refcount reaches zero,
121 * all resources of the enumeration context will be released.
123 * Returns: the passed enumeration context if it has still an active reference, or #NULL otherwise.
125 _public_ struct udev_enumerate *udev_enumerate_unref(struct udev_enumerate *udev_enumerate)
129 if (udev_enumerate == NULL)
131 udev_enumerate->refcount--;
132 if (udev_enumerate->refcount > 0)
133 return udev_enumerate;
134 udev_list_cleanup(&udev_enumerate->sysattr_match_list);
135 udev_list_cleanup(&udev_enumerate->sysattr_nomatch_list);
136 udev_list_cleanup(&udev_enumerate->subsystem_match_list);
137 udev_list_cleanup(&udev_enumerate->subsystem_nomatch_list);
138 udev_list_cleanup(&udev_enumerate->sysname_match_list);
139 udev_list_cleanup(&udev_enumerate->properties_match_list);
140 udev_list_cleanup(&udev_enumerate->tags_match_list);
141 udev_device_unref(udev_enumerate->parent_match);
142 udev_list_cleanup(&udev_enumerate->devices_list);
143 for (i = 0; i < udev_enumerate->devices_cur; i++)
144 free(udev_enumerate->devices[i].syspath);
145 free(udev_enumerate->devices);
146 free(udev_enumerate);
151 * udev_enumerate_get_udev:
152 * @udev_enumerate: context
154 * Get the udev library context.
156 * Returns: a pointer to the context.
158 _public_ struct udev *udev_enumerate_get_udev(struct udev_enumerate *udev_enumerate)
160 if (udev_enumerate == NULL)
162 return udev_enumerate->udev;
165 static int syspath_add(struct udev_enumerate *udev_enumerate, const char *syspath)
168 struct syspath *entry;
170 /* double array size if needed */
171 if (udev_enumerate->devices_cur >= udev_enumerate->devices_max) {
175 add = udev_enumerate->devices_max;
178 buf = realloc(udev_enumerate->devices, (udev_enumerate->devices_max + add) * sizeof(struct syspath));
181 udev_enumerate->devices = buf;
182 udev_enumerate->devices_max += add;
185 path = strdup(syspath);
188 entry = &udev_enumerate->devices[udev_enumerate->devices_cur];
189 entry->syspath = path;
190 entry->len = strlen(path);
191 udev_enumerate->devices_cur++;
192 udev_enumerate->devices_uptodate = false;
196 static int syspath_cmp(const void *p1, const void *p2)
198 const struct syspath *path1 = p1;
199 const struct syspath *path2 = p2;
203 len = MIN(path1->len, path2->len);
204 ret = memcmp(path1->syspath, path2->syspath, len);
206 if (path1->len < path2->len)
208 else if (path1->len > path2->len)
214 /* For devices that should be moved to the absolute end of the list */
215 static bool devices_delay_end(struct udev *udev, const char *syspath)
217 static const char *delay_device_list[] = {
224 for (i = 0; delay_device_list[i] != NULL; i++) {
225 if (strstr(syspath + strlen("/sys"), delay_device_list[i]) != NULL)
231 /* For devices that should just be moved a little bit later, just
232 * before the point where some common path prefix changes. Returns the
233 * number of characters that make up that common prefix */
234 static size_t devices_delay_later(struct udev *udev, const char *syspath)
238 /* For sound cards the control device must be enumerated last
239 * to make sure it's the final device node that gets ACLs
240 * applied. Applications rely on this fact and use ACL changes
241 * on the control node as an indicator that the ACL change of
242 * the entire sound card completed. The kernel makes this
243 * guarantee when creating those devices, and hence we should
244 * too when enumerating them. */
246 if ((c = strstr(syspath, "/sound/card"))) {
248 c += strcspn(c, "/");
250 if (startswith(c, "/controlC"))
251 return c - syspath + 1;
258 * udev_enumerate_get_list_entry:
259 * @udev_enumerate: context
261 * Get the first entry of the sorted list of device paths.
263 * Returns: a udev_list_entry.
265 _public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate)
267 if (udev_enumerate == NULL)
269 if (!udev_enumerate->devices_uptodate) {
272 struct syspath *prev = NULL, *move_later = NULL;
273 size_t move_later_prefix = 0;
275 udev_list_cleanup(&udev_enumerate->devices_list);
276 qsort(udev_enumerate->devices, udev_enumerate->devices_cur, sizeof(struct syspath), syspath_cmp);
278 max = udev_enumerate->devices_cur;
279 for (i = 0; i < max; i++) {
280 struct syspath *entry = &udev_enumerate->devices[i];
282 /* skip duplicated entries */
284 entry->len == prev->len &&
285 memcmp(entry->syspath, prev->syspath, entry->len) == 0)
289 /* skip to be delayed devices, and add them to the end of the list */
290 if (devices_delay_end(udev_enumerate->udev, entry->syspath)) {
291 syspath_add(udev_enumerate, entry->syspath);
292 /* need to update prev here for the case realloc() gives a different address */
293 prev = &udev_enumerate->devices[i];
297 /* skip to be delayed devices, and move the to
298 * the point where the prefix changes. We can
299 * only move one item at a time. */
301 move_later_prefix = devices_delay_later(udev_enumerate->udev, entry->syspath);
303 if (move_later_prefix > 0) {
310 strncmp(entry->syspath, move_later->syspath, move_later_prefix) != 0) {
312 udev_list_entry_add(&udev_enumerate->devices_list, move_later->syspath, NULL);
316 udev_list_entry_add(&udev_enumerate->devices_list, entry->syspath, NULL);
320 udev_list_entry_add(&udev_enumerate->devices_list, move_later->syspath, NULL);
322 /* add and cleanup delayed devices from end of list */
323 for (i = max; i < udev_enumerate->devices_cur; i++) {
324 struct syspath *entry = &udev_enumerate->devices[i];
326 udev_list_entry_add(&udev_enumerate->devices_list, entry->syspath, NULL);
327 free(entry->syspath);
329 udev_enumerate->devices_cur = max;
331 udev_enumerate->devices_uptodate = true;
333 return udev_list_get_entry(&udev_enumerate->devices_list);
337 * udev_enumerate_add_match_subsystem:
338 * @udev_enumerate: context
339 * @subsystem: filter for a subsystem of the device to include in the list
341 * Match only devices belonging to a certain kernel subsystem.
343 * Returns: 0 on success, otherwise a negative error value.
345 _public_ int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
347 if (udev_enumerate == NULL)
349 if (subsystem == NULL)
351 if (udev_list_entry_add(&udev_enumerate->subsystem_match_list, subsystem, NULL) == NULL)
357 * udev_enumerate_add_nomatch_subsystem:
358 * @udev_enumerate: context
359 * @subsystem: filter for a subsystem of the device to exclude from the list
361 * Match only devices not belonging to a certain kernel subsystem.
363 * Returns: 0 on success, otherwise a negative error value.
365 _public_ int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
367 if (udev_enumerate == NULL)
369 if (subsystem == NULL)
371 if (udev_list_entry_add(&udev_enumerate->subsystem_nomatch_list, subsystem, NULL) == NULL)
377 * udev_enumerate_add_match_sysattr:
378 * @udev_enumerate: context
379 * @sysattr: filter for a sys attribute at the device to include in the list
380 * @value: optional value of the sys attribute
382 * Match only devices with a certain /sys device attribute.
384 * Returns: 0 on success, otherwise a negative error value.
386 _public_ int udev_enumerate_add_match_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
388 if (udev_enumerate == NULL)
392 if (udev_list_entry_add(&udev_enumerate->sysattr_match_list, sysattr, value) == NULL)
398 * udev_enumerate_add_nomatch_sysattr:
399 * @udev_enumerate: context
400 * @sysattr: filter for a sys attribute at the device to exclude from the list
401 * @value: optional value of the sys attribute
403 * Match only devices not having a certain /sys device attribute.
405 * Returns: 0 on success, otherwise a negative error value.
407 _public_ int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
409 if (udev_enumerate == NULL)
413 if (udev_list_entry_add(&udev_enumerate->sysattr_nomatch_list, sysattr, value) == NULL)
418 static int match_sysattr_value(struct udev_device *dev, const char *sysattr, const char *match_val)
420 const char *val = NULL;
423 val = udev_device_get_sysattr_value(dev, sysattr);
426 if (match_val == NULL) {
430 if (fnmatch(match_val, val, 0) == 0) {
439 * udev_enumerate_add_match_property:
440 * @udev_enumerate: context
441 * @property: filter for a property of the device to include in the list
442 * @value: value of the property
444 * Match only devices with a certain property.
446 * Returns: 0 on success, otherwise a negative error value.
448 _public_ int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value)
450 if (udev_enumerate == NULL)
452 if (property == NULL)
454 if (udev_list_entry_add(&udev_enumerate->properties_match_list, property, value) == NULL)
460 * udev_enumerate_add_match_tag:
461 * @udev_enumerate: context
462 * @tag: filter for a tag of the device to include in the list
464 * Match only devices with a certain tag.
466 * Returns: 0 on success, otherwise a negative error value.
468 _public_ int udev_enumerate_add_match_tag(struct udev_enumerate *udev_enumerate, const char *tag)
470 if (udev_enumerate == NULL)
474 if (udev_list_entry_add(&udev_enumerate->tags_match_list, tag, NULL) == NULL)
480 * udev_enumerate_add_match_parent:
481 * @udev_enumerate: context
482 * @parent: parent device where to start searching
484 * Return the devices on the subtree of one given device. The parent
485 * itself is included in the list.
487 * A reference for the device is held until the udev_enumerate context
490 * Returns: 0 on success, otherwise a negative error value.
492 _public_ int udev_enumerate_add_match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *parent)
494 if (udev_enumerate == NULL)
498 if (udev_enumerate->parent_match != NULL)
499 udev_device_unref(udev_enumerate->parent_match);
500 udev_enumerate->parent_match = udev_device_ref(parent);
505 * udev_enumerate_add_match_is_initialized:
506 * @udev_enumerate: context
508 * Match only devices which udev has set up already. This makes
509 * sure, that the device node permissions and context are properly set
510 * and that network devices are fully renamed.
512 * Usually, devices which are found in the kernel but not already
513 * handled by udev, have still pending events. Services should subscribe
514 * to monitor events and wait for these devices to become ready, instead
515 * of using uninitialized devices.
517 * For now, this will not affect devices which do not have a device node
518 * and are not network interfaces.
520 * Returns: 0 on success, otherwise a negative error value.
522 _public_ int udev_enumerate_add_match_is_initialized(struct udev_enumerate *udev_enumerate)
524 if (udev_enumerate == NULL)
526 udev_enumerate->match_is_initialized = true;
531 * udev_enumerate_add_match_sysname:
532 * @udev_enumerate: context
533 * @sysname: filter for the name of the device to include in the list
535 * Match only devices with a given /sys device name.
537 * Returns: 0 on success, otherwise a negative error value.
539 _public_ int udev_enumerate_add_match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
541 if (udev_enumerate == NULL)
545 if (udev_list_entry_add(&udev_enumerate->sysname_match_list, sysname, NULL) == NULL)
550 static bool match_sysattr(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
552 struct udev_list_entry *list_entry;
555 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysattr_nomatch_list)) {
556 if (match_sysattr_value(dev, udev_list_entry_get_name(list_entry),
557 udev_list_entry_get_value(list_entry)))
561 if (udev_list_get_entry(&udev_enumerate->sysattr_match_list) != NULL) {
562 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysattr_match_list)) {
563 /* anything that does not match, will make it FALSE */
564 if (!match_sysattr_value(dev, udev_list_entry_get_name(list_entry),
565 udev_list_entry_get_value(list_entry)))
573 static bool match_property(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
575 struct udev_list_entry *list_entry;
578 /* no match always matches */
579 if (udev_list_get_entry(&udev_enumerate->properties_match_list) == NULL)
582 /* loop over matches */
583 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->properties_match_list)) {
584 const char *match_key = udev_list_entry_get_name(list_entry);
585 const char *match_value = udev_list_entry_get_value(list_entry);
586 struct udev_list_entry *property_entry;
588 /* loop over device properties */
589 udev_list_entry_foreach(property_entry, udev_device_get_properties_list_entry(dev)) {
590 const char *dev_key = udev_list_entry_get_name(property_entry);
591 const char *dev_value = udev_list_entry_get_value(property_entry);
593 if (fnmatch(match_key, dev_key, 0) != 0)
595 if (match_value == NULL && dev_value == NULL) {
599 if (match_value == NULL || dev_value == NULL)
601 if (fnmatch(match_value, dev_value, 0) == 0) {
611 static bool match_tag(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
613 struct udev_list_entry *list_entry;
615 /* no match always matches */
616 if (udev_list_get_entry(&udev_enumerate->tags_match_list) == NULL)
619 /* loop over matches */
620 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->tags_match_list))
621 if (!udev_device_has_tag(dev, udev_list_entry_get_name(list_entry)))
627 static bool match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
629 if (udev_enumerate->parent_match == NULL)
632 return startswith(udev_device_get_devpath(dev), udev_device_get_devpath(udev_enumerate->parent_match));
635 static bool match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
637 struct udev_list_entry *list_entry;
639 if (udev_list_get_entry(&udev_enumerate->sysname_match_list) == NULL)
642 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysname_match_list)) {
643 if (fnmatch(udev_list_entry_get_name(list_entry), sysname, 0) != 0)
650 static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate,
651 const char *basedir, const char *subdir1, const char *subdir2)
653 char path[UTIL_PATH_SIZE];
660 l = util_strpcpyl(&s, sizeof(path), "/sys/", basedir, NULL);
662 l = util_strpcpyl(&s, l, "/", subdir1, NULL);
664 util_strpcpyl(&s, l, "/", subdir2, NULL);
668 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
669 char syspath[UTIL_PATH_SIZE];
670 struct udev_device *dev;
672 if (dent->d_name[0] == '.')
675 if (!match_sysname(udev_enumerate, dent->d_name))
678 util_strscpyl(syspath, sizeof(syspath), path, "/", dent->d_name, NULL);
679 dev = udev_device_new_from_syspath(udev_enumerate->udev, syspath);
683 if (udev_enumerate->match_is_initialized) {
685 * All devices with a device node or network interfaces
686 * possibly need udev to adjust the device node permission
687 * or context, or rename the interface before it can be
688 * reliably used from other processes.
690 * For now, we can only check these types of devices, we
691 * might not store a database, and have no way to find out
692 * for all other types of devices.
694 if (!udev_device_get_is_initialized(dev) &&
695 (major(udev_device_get_devnum(dev)) > 0 || udev_device_get_ifindex(dev) > 0))
698 if (!match_parent(udev_enumerate, dev))
700 if (!match_tag(udev_enumerate, dev))
702 if (!match_property(udev_enumerate, dev))
704 if (!match_sysattr(udev_enumerate, dev))
707 syspath_add(udev_enumerate, udev_device_get_syspath(dev));
709 udev_device_unref(dev);
715 static bool match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
717 struct udev_list_entry *list_entry;
719 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->subsystem_nomatch_list)) {
720 if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0)
723 if (udev_list_get_entry(&udev_enumerate->subsystem_match_list) != NULL) {
724 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->subsystem_match_list)) {
725 if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0)
733 static int scan_dir(struct udev_enumerate *udev_enumerate, const char *basedir, const char *subdir, const char *subsystem)
735 char path[UTIL_PATH_SIZE];
739 util_strscpyl(path, sizeof(path), "/sys/", basedir, NULL);
743 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
744 if (dent->d_name[0] == '.')
746 if (!match_subsystem(udev_enumerate, subsystem != NULL ? subsystem : dent->d_name))
748 scan_dir_and_add_devices(udev_enumerate, basedir, dent->d_name, subdir);
755 * udev_enumerate_add_syspath:
756 * @udev_enumerate: context
757 * @syspath: path of a device
759 * Add a device to the list of devices, to retrieve it back sorted in dependency order.
761 * Returns: 0 on success, otherwise a negative error value.
763 _public_ int udev_enumerate_add_syspath(struct udev_enumerate *udev_enumerate, const char *syspath)
765 struct udev_device *udev_device;
767 if (udev_enumerate == NULL)
771 /* resolve to real syspath */
772 udev_device = udev_device_new_from_syspath(udev_enumerate->udev, syspath);
773 if (udev_device == NULL)
775 syspath_add(udev_enumerate, udev_device_get_syspath(udev_device));
776 udev_device_unref(udev_device);
780 static int scan_devices_tags(struct udev_enumerate *udev_enumerate)
782 struct udev_list_entry *list_entry;
784 /* scan only tagged devices, use tags reverse-index, instead of searching all devices in /sys */
785 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->tags_match_list)) {
788 char path[UTIL_PATH_SIZE];
790 util_strscpyl(path, sizeof(path), "/run/udev/tags/", udev_list_entry_get_name(list_entry), NULL);
794 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
795 struct udev_device *dev;
797 if (dent->d_name[0] == '.')
800 dev = udev_device_new_from_device_id(udev_enumerate->udev, dent->d_name);
804 if (!match_subsystem(udev_enumerate, udev_device_get_subsystem(dev)))
806 if (!match_sysname(udev_enumerate, udev_device_get_sysname(dev)))
808 if (!match_parent(udev_enumerate, dev))
810 if (!match_property(udev_enumerate, dev))
812 if (!match_sysattr(udev_enumerate, dev))
815 syspath_add(udev_enumerate, udev_device_get_syspath(dev));
817 udev_device_unref(dev);
824 static int parent_add_child(struct udev_enumerate *enumerate, const char *path)
826 struct udev_device *dev;
828 dev = udev_device_new_from_syspath(enumerate->udev, path);
832 if (!match_subsystem(enumerate, udev_device_get_subsystem(dev)))
834 if (!match_sysname(enumerate, udev_device_get_sysname(dev)))
836 if (!match_property(enumerate, dev))
838 if (!match_sysattr(enumerate, dev))
841 syspath_add(enumerate, udev_device_get_syspath(dev));
842 udev_device_unref(dev);
846 static int parent_crawl_children(struct udev_enumerate *enumerate, const char *path, int maxdepth)
855 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
858 if (dent->d_name[0] == '.')
860 if (dent->d_type != DT_DIR)
862 if (asprintf(&child, "%s/%s", path, dent->d_name) < 0)
864 parent_add_child(enumerate, child);
866 parent_crawl_children(enumerate, child, maxdepth-1);
874 static int scan_devices_children(struct udev_enumerate *enumerate)
878 path = udev_device_get_syspath(enumerate->parent_match);
879 parent_add_child(enumerate, path);
880 return parent_crawl_children(enumerate, path, 256);
883 static int scan_devices_all(struct udev_enumerate *udev_enumerate)
887 if (stat("/sys/subsystem", &statbuf) == 0) {
888 /* we have /subsystem/, forget all the old stuff */
889 scan_dir(udev_enumerate, "subsystem", "devices", NULL);
891 scan_dir(udev_enumerate, "bus", "devices", NULL);
892 scan_dir(udev_enumerate, "class", NULL, NULL);
898 * udev_enumerate_scan_devices:
899 * @udev_enumerate: udev enumeration context
901 * Scan /sys for all devices which match the given filters. No matches
902 * will return all currently available devices.
904 * Returns: 0 on success, otherwise a negative error value.
906 _public_ int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
908 if (udev_enumerate == NULL)
911 /* efficiently lookup tags only, we maintain a reverse-index */
912 if (udev_list_get_entry(&udev_enumerate->tags_match_list) != NULL)
913 return scan_devices_tags(udev_enumerate);
915 /* walk the subtree of one parent device only */
916 if (udev_enumerate->parent_match != NULL)
917 return scan_devices_children(udev_enumerate);
919 /* scan devices of all subsystems */
920 return scan_devices_all(udev_enumerate);
924 * udev_enumerate_scan_subsystems:
925 * @udev_enumerate: udev enumeration context
927 * Scan /sys for all kernel subsystems, including buses, classes, drivers.
929 * Returns: 0 on success, otherwise a negative error value.
931 _public_ int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate)
934 const char *subsysdir;
936 if (udev_enumerate == NULL)
939 /* all kernel modules */
940 if (match_subsystem(udev_enumerate, "module"))
941 scan_dir_and_add_devices(udev_enumerate, "module", NULL, NULL);
943 if (stat("/sys/subsystem", &statbuf) == 0)
944 subsysdir = "subsystem";
948 /* all subsystems (only buses support coldplug) */
949 if (match_subsystem(udev_enumerate, "subsystem"))
950 scan_dir_and_add_devices(udev_enumerate, subsysdir, NULL, NULL);
952 /* all subsystem drivers */
953 if (match_subsystem(udev_enumerate, "drivers"))
954 scan_dir(udev_enumerate, subsysdir, "drivers", "drivers");