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;
86 udev_enumerate = calloc(1, sizeof(struct udev_enumerate));
87 if (udev_enumerate == NULL)
89 udev_enumerate->refcount = 1;
90 udev_enumerate->udev = udev;
91 udev_list_init(udev, &udev_enumerate->sysattr_match_list, false);
92 udev_list_init(udev, &udev_enumerate->sysattr_nomatch_list, false);
93 udev_list_init(udev, &udev_enumerate->subsystem_match_list, true);
94 udev_list_init(udev, &udev_enumerate->subsystem_nomatch_list, true);
95 udev_list_init(udev, &udev_enumerate->sysname_match_list, true);
96 udev_list_init(udev, &udev_enumerate->properties_match_list, false);
97 udev_list_init(udev, &udev_enumerate->tags_match_list, true);
98 udev_list_init(udev, &udev_enumerate->devices_list, false);
99 return udev_enumerate;
103 * udev_enumerate_ref:
104 * @udev_enumerate: context
106 * Take a reference of a enumeration context.
108 * Returns: the passed enumeration context
110 _public_ struct udev_enumerate *udev_enumerate_ref(struct udev_enumerate *udev_enumerate)
112 if (udev_enumerate == NULL)
114 udev_enumerate->refcount++;
115 return udev_enumerate;
119 * udev_enumerate_unref:
120 * @udev_enumerate: context
122 * Drop a reference of an enumeration context. If the refcount reaches zero,
123 * all resources of the enumeration context will be released.
125 * Returns: the passed enumeration context if it has still an active reference, or #NULL otherwise.
127 _public_ struct udev_enumerate *udev_enumerate_unref(struct udev_enumerate *udev_enumerate)
131 if (udev_enumerate == NULL)
133 udev_enumerate->refcount--;
134 if (udev_enumerate->refcount > 0)
135 return udev_enumerate;
136 udev_list_cleanup(&udev_enumerate->sysattr_match_list);
137 udev_list_cleanup(&udev_enumerate->sysattr_nomatch_list);
138 udev_list_cleanup(&udev_enumerate->subsystem_match_list);
139 udev_list_cleanup(&udev_enumerate->subsystem_nomatch_list);
140 udev_list_cleanup(&udev_enumerate->sysname_match_list);
141 udev_list_cleanup(&udev_enumerate->properties_match_list);
142 udev_list_cleanup(&udev_enumerate->tags_match_list);
143 udev_device_unref(udev_enumerate->parent_match);
144 udev_list_cleanup(&udev_enumerate->devices_list);
145 for (i = 0; i < udev_enumerate->devices_cur; i++)
146 free(udev_enumerate->devices[i].syspath);
147 free(udev_enumerate->devices);
148 free(udev_enumerate);
153 * udev_enumerate_get_udev:
154 * @udev_enumerate: context
156 * Get the udev library context.
158 * Returns: a pointer to the context.
160 _public_ struct udev *udev_enumerate_get_udev(struct udev_enumerate *udev_enumerate)
162 if (udev_enumerate == NULL)
164 return udev_enumerate->udev;
167 static int syspath_add(struct udev_enumerate *udev_enumerate, const char *syspath)
170 struct syspath *entry;
172 /* double array size if needed */
173 if (udev_enumerate->devices_cur >= udev_enumerate->devices_max) {
177 add = udev_enumerate->devices_max;
180 buf = realloc(udev_enumerate->devices, (udev_enumerate->devices_max + add) * sizeof(struct syspath));
183 udev_enumerate->devices = buf;
184 udev_enumerate->devices_max += add;
187 path = strdup(syspath);
190 entry = &udev_enumerate->devices[udev_enumerate->devices_cur];
191 entry->syspath = path;
192 entry->len = strlen(path);
193 udev_enumerate->devices_cur++;
194 udev_enumerate->devices_uptodate = false;
198 static int syspath_cmp(const void *p1, const void *p2)
200 const struct syspath *path1 = p1;
201 const struct syspath *path2 = p2;
205 len = MIN(path1->len, path2->len);
206 ret = memcmp(path1->syspath, path2->syspath, len);
208 if (path1->len < path2->len)
210 else if (path1->len > path2->len)
216 /* For devices that should be moved to the absolute end of the list */
217 static bool devices_delay_end(struct udev *udev, const char *syspath)
219 static const char *delay_device_list[] = {
226 for (i = 0; delay_device_list[i] != NULL; i++) {
227 if (strstr(syspath + strlen("/sys"), delay_device_list[i]) != NULL)
233 /* For devices that should just be moved a little bit later, just
234 * before the point where some common path prefix changes. Returns the
235 * number of characters that make up that common prefix */
236 static size_t devices_delay_later(struct udev *udev, const char *syspath)
240 /* For sound cards the control device must be enumerated last
241 * to make sure it's the final device node that gets ACLs
242 * applied. Applications rely on this fact and use ACL changes
243 * on the control node as an indicator that the ACL change of
244 * the entire sound card completed. The kernel makes this
245 * guarantee when creating those devices, and hence we should
246 * too when enumerating them. */
248 if ((c = strstr(syspath, "/sound/card"))) {
250 c += strcspn(c, "/");
252 if (startswith(c, "/controlC"))
253 return c - syspath + 1;
260 * udev_enumerate_get_list_entry:
261 * @udev_enumerate: context
263 * Get the first entry of the sorted list of device paths.
265 * Returns: a udev_list_entry.
267 _public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate)
269 if (udev_enumerate == NULL)
271 if (!udev_enumerate->devices_uptodate) {
274 struct syspath *prev = NULL, *move_later = NULL;
275 size_t move_later_prefix = 0;
277 udev_list_cleanup(&udev_enumerate->devices_list);
278 qsort(udev_enumerate->devices, udev_enumerate->devices_cur, sizeof(struct syspath), syspath_cmp);
280 max = udev_enumerate->devices_cur;
281 for (i = 0; i < max; i++) {
282 struct syspath *entry = &udev_enumerate->devices[i];
284 /* skip duplicated entries */
286 entry->len == prev->len &&
287 memcmp(entry->syspath, prev->syspath, entry->len) == 0)
291 /* skip to be delayed devices, and add them to the end of the list */
292 if (devices_delay_end(udev_enumerate->udev, entry->syspath)) {
293 syspath_add(udev_enumerate, entry->syspath);
294 /* need to update prev here for the case realloc() gives a different address */
295 prev = &udev_enumerate->devices[i];
299 /* skip to be delayed devices, and move the to
300 * the point where the prefix changes. We can
301 * only move one item at a time. */
303 move_later_prefix = devices_delay_later(udev_enumerate->udev, entry->syspath);
305 if (move_later_prefix > 0) {
312 !strneq(entry->syspath, move_later->syspath, move_later_prefix)) {
314 udev_list_entry_add(&udev_enumerate->devices_list, move_later->syspath, NULL);
318 udev_list_entry_add(&udev_enumerate->devices_list, entry->syspath, NULL);
322 udev_list_entry_add(&udev_enumerate->devices_list, move_later->syspath, NULL);
324 /* add and cleanup delayed devices from end of list */
325 for (i = max; i < udev_enumerate->devices_cur; i++) {
326 struct syspath *entry = &udev_enumerate->devices[i];
328 udev_list_entry_add(&udev_enumerate->devices_list, entry->syspath, NULL);
329 free(entry->syspath);
331 udev_enumerate->devices_cur = max;
333 udev_enumerate->devices_uptodate = true;
335 return udev_list_get_entry(&udev_enumerate->devices_list);
339 * udev_enumerate_add_match_subsystem:
340 * @udev_enumerate: context
341 * @subsystem: filter for a subsystem of the device to include in the list
343 * Match only devices belonging to a certain kernel subsystem.
345 * Returns: 0 on success, otherwise a negative error value.
347 _public_ int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
349 if (udev_enumerate == NULL)
351 if (subsystem == NULL)
353 if (udev_list_entry_add(&udev_enumerate->subsystem_match_list, subsystem, NULL) == NULL)
359 * udev_enumerate_add_nomatch_subsystem:
360 * @udev_enumerate: context
361 * @subsystem: filter for a subsystem of the device to exclude from the list
363 * Match only devices not belonging to a certain kernel subsystem.
365 * Returns: 0 on success, otherwise a negative error value.
367 _public_ int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
369 if (udev_enumerate == NULL)
371 if (subsystem == NULL)
373 if (udev_list_entry_add(&udev_enumerate->subsystem_nomatch_list, subsystem, NULL) == NULL)
379 * udev_enumerate_add_match_sysattr:
380 * @udev_enumerate: context
381 * @sysattr: filter for a sys attribute at the device to include in the list
382 * @value: optional value of the sys attribute
384 * Match only devices with a certain /sys device attribute.
386 * Returns: 0 on success, otherwise a negative error value.
388 _public_ int udev_enumerate_add_match_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
390 if (udev_enumerate == NULL)
394 if (udev_list_entry_add(&udev_enumerate->sysattr_match_list, sysattr, value) == NULL)
400 * udev_enumerate_add_nomatch_sysattr:
401 * @udev_enumerate: context
402 * @sysattr: filter for a sys attribute at the device to exclude from the list
403 * @value: optional value of the sys attribute
405 * Match only devices not having a certain /sys device attribute.
407 * Returns: 0 on success, otherwise a negative error value.
409 _public_ int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
411 if (udev_enumerate == NULL)
415 if (udev_list_entry_add(&udev_enumerate->sysattr_nomatch_list, sysattr, value) == NULL)
420 static int match_sysattr_value(struct udev_device *dev, const char *sysattr, const char *match_val)
422 const char *val = NULL;
425 val = udev_device_get_sysattr_value(dev, sysattr);
428 if (match_val == NULL) {
432 if (fnmatch(match_val, val, 0) == 0) {
441 * udev_enumerate_add_match_property:
442 * @udev_enumerate: context
443 * @property: filter for a property of the device to include in the list
444 * @value: value of the property
446 * Match only devices with a certain property.
448 * Returns: 0 on success, otherwise a negative error value.
450 _public_ int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value)
452 if (udev_enumerate == NULL)
454 if (property == NULL)
456 if (udev_list_entry_add(&udev_enumerate->properties_match_list, property, value) == NULL)
462 * udev_enumerate_add_match_tag:
463 * @udev_enumerate: context
464 * @tag: filter for a tag of the device to include in the list
466 * Match only devices with a certain tag.
468 * Returns: 0 on success, otherwise a negative error value.
470 _public_ int udev_enumerate_add_match_tag(struct udev_enumerate *udev_enumerate, const char *tag)
472 if (udev_enumerate == NULL)
476 if (udev_list_entry_add(&udev_enumerate->tags_match_list, tag, NULL) == NULL)
482 * udev_enumerate_add_match_parent:
483 * @udev_enumerate: context
484 * @parent: parent device where to start searching
486 * Return the devices on the subtree of one given device. The parent
487 * itself is included in the list.
489 * A reference for the device is held until the udev_enumerate context
492 * Returns: 0 on success, otherwise a negative error value.
494 _public_ int udev_enumerate_add_match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *parent)
496 if (udev_enumerate == NULL)
500 if (udev_enumerate->parent_match != NULL)
501 udev_device_unref(udev_enumerate->parent_match);
502 udev_enumerate->parent_match = udev_device_ref(parent);
507 * udev_enumerate_add_match_is_initialized:
508 * @udev_enumerate: context
510 * Match only devices which udev has set up already. This makes
511 * sure, that the device node permissions and context are properly set
512 * and that network devices are fully renamed.
514 * Usually, devices which are found in the kernel but not already
515 * handled by udev, have still pending events. Services should subscribe
516 * to monitor events and wait for these devices to become ready, instead
517 * of using uninitialized devices.
519 * For now, this will not affect devices which do not have a device node
520 * and are not network interfaces.
522 * Returns: 0 on success, otherwise a negative error value.
524 _public_ int udev_enumerate_add_match_is_initialized(struct udev_enumerate *udev_enumerate)
526 if (udev_enumerate == NULL)
528 udev_enumerate->match_is_initialized = true;
533 * udev_enumerate_add_match_sysname:
534 * @udev_enumerate: context
535 * @sysname: filter for the name of the device to include in the list
537 * Match only devices with a given /sys device name.
539 * Returns: 0 on success, otherwise a negative error value.
541 _public_ int udev_enumerate_add_match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
543 if (udev_enumerate == NULL)
547 if (udev_list_entry_add(&udev_enumerate->sysname_match_list, sysname, NULL) == NULL)
552 static bool match_sysattr(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
554 struct udev_list_entry *list_entry;
557 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysattr_nomatch_list)) {
558 if (match_sysattr_value(dev, udev_list_entry_get_name(list_entry),
559 udev_list_entry_get_value(list_entry)))
563 if (udev_list_get_entry(&udev_enumerate->sysattr_match_list) != NULL) {
564 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysattr_match_list)) {
565 /* anything that does not match, will make it FALSE */
566 if (!match_sysattr_value(dev, udev_list_entry_get_name(list_entry),
567 udev_list_entry_get_value(list_entry)))
575 static bool match_property(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
577 struct udev_list_entry *list_entry;
580 /* no match always matches */
581 if (udev_list_get_entry(&udev_enumerate->properties_match_list) == NULL)
584 /* loop over matches */
585 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->properties_match_list)) {
586 const char *match_key = udev_list_entry_get_name(list_entry);
587 const char *match_value = udev_list_entry_get_value(list_entry);
588 struct udev_list_entry *property_entry;
590 /* loop over device properties */
591 udev_list_entry_foreach(property_entry, udev_device_get_properties_list_entry(dev)) {
592 const char *dev_key = udev_list_entry_get_name(property_entry);
593 const char *dev_value = udev_list_entry_get_value(property_entry);
595 if (fnmatch(match_key, dev_key, 0) != 0)
597 if (match_value == NULL && dev_value == NULL) {
601 if (match_value == NULL || dev_value == NULL)
603 if (fnmatch(match_value, dev_value, 0) == 0) {
613 static bool match_tag(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
615 struct udev_list_entry *list_entry;
617 /* no match always matches */
618 if (udev_list_get_entry(&udev_enumerate->tags_match_list) == NULL)
621 /* loop over matches */
622 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->tags_match_list))
623 if (!udev_device_has_tag(dev, udev_list_entry_get_name(list_entry)))
629 static bool match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
631 if (udev_enumerate->parent_match == NULL)
634 return startswith(udev_device_get_devpath(dev), udev_device_get_devpath(udev_enumerate->parent_match));
637 static bool match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
639 struct udev_list_entry *list_entry;
641 if (udev_list_get_entry(&udev_enumerate->sysname_match_list) == NULL)
644 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysname_match_list)) {
645 if (fnmatch(udev_list_entry_get_name(list_entry), sysname, 0) != 0)
652 static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate,
653 const char *basedir, const char *subdir1, const char *subdir2)
655 char path[UTIL_PATH_SIZE];
662 l = strpcpyl(&s, sizeof(path), "/sys/", basedir, NULL);
664 l = strpcpyl(&s, l, "/", subdir1, NULL);
666 strpcpyl(&s, l, "/", subdir2, NULL);
670 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
671 char syspath[UTIL_PATH_SIZE];
672 struct udev_device *dev;
674 if (dent->d_name[0] == '.')
677 if (!match_sysname(udev_enumerate, dent->d_name))
680 strscpyl(syspath, sizeof(syspath), path, "/", dent->d_name, NULL);
681 dev = udev_device_new_from_syspath(udev_enumerate->udev, syspath);
685 if (udev_enumerate->match_is_initialized) {
687 * All devices with a device node or network interfaces
688 * possibly need udev to adjust the device node permission
689 * or context, or rename the interface before it can be
690 * reliably used from other processes.
692 * For now, we can only check these types of devices, we
693 * might not store a database, and have no way to find out
694 * for all other types of devices.
696 if (!udev_device_get_is_initialized(dev) &&
697 (major(udev_device_get_devnum(dev)) > 0 || udev_device_get_ifindex(dev) > 0))
700 if (!match_parent(udev_enumerate, dev))
702 if (!match_tag(udev_enumerate, dev))
704 if (!match_property(udev_enumerate, dev))
706 if (!match_sysattr(udev_enumerate, dev))
709 syspath_add(udev_enumerate, udev_device_get_syspath(dev));
711 udev_device_unref(dev);
717 static bool match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
719 struct udev_list_entry *list_entry;
721 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->subsystem_nomatch_list)) {
722 if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0)
725 if (udev_list_get_entry(&udev_enumerate->subsystem_match_list) != NULL) {
726 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->subsystem_match_list)) {
727 if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0)
735 static int scan_dir(struct udev_enumerate *udev_enumerate, const char *basedir, const char *subdir, const char *subsystem)
737 char path[UTIL_PATH_SIZE];
741 strscpyl(path, sizeof(path), "/sys/", basedir, NULL);
745 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
746 if (dent->d_name[0] == '.')
748 if (!match_subsystem(udev_enumerate, subsystem != NULL ? subsystem : dent->d_name))
750 scan_dir_and_add_devices(udev_enumerate, basedir, dent->d_name, subdir);
757 * udev_enumerate_add_syspath:
758 * @udev_enumerate: context
759 * @syspath: path of a device
761 * Add a device to the list of devices, to retrieve it back sorted in dependency order.
763 * Returns: 0 on success, otherwise a negative error value.
765 _public_ int udev_enumerate_add_syspath(struct udev_enumerate *udev_enumerate, const char *syspath)
767 struct udev_device *udev_device;
769 if (udev_enumerate == NULL)
773 /* resolve to real syspath */
774 udev_device = udev_device_new_from_syspath(udev_enumerate->udev, syspath);
775 if (udev_device == NULL)
777 syspath_add(udev_enumerate, udev_device_get_syspath(udev_device));
778 udev_device_unref(udev_device);
782 static int scan_devices_tags(struct udev_enumerate *udev_enumerate)
784 struct udev_list_entry *list_entry;
786 /* scan only tagged devices, use tags reverse-index, instead of searching all devices in /sys */
787 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->tags_match_list)) {
790 char path[UTIL_PATH_SIZE];
792 strscpyl(path, sizeof(path), "/run/udev/tags/", udev_list_entry_get_name(list_entry), NULL);
796 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
797 struct udev_device *dev;
799 if (dent->d_name[0] == '.')
802 dev = udev_device_new_from_device_id(udev_enumerate->udev, dent->d_name);
806 if (!match_subsystem(udev_enumerate, udev_device_get_subsystem(dev)))
808 if (!match_sysname(udev_enumerate, udev_device_get_sysname(dev)))
810 if (!match_parent(udev_enumerate, dev))
812 if (!match_property(udev_enumerate, dev))
814 if (!match_sysattr(udev_enumerate, dev))
817 syspath_add(udev_enumerate, udev_device_get_syspath(dev));
819 udev_device_unref(dev);
826 static int parent_add_child(struct udev_enumerate *enumerate, const char *path)
828 struct udev_device *dev;
830 dev = udev_device_new_from_syspath(enumerate->udev, path);
834 if (!match_subsystem(enumerate, udev_device_get_subsystem(dev)))
836 if (!match_sysname(enumerate, udev_device_get_sysname(dev)))
838 if (!match_property(enumerate, dev))
840 if (!match_sysattr(enumerate, dev))
843 syspath_add(enumerate, udev_device_get_syspath(dev));
844 udev_device_unref(dev);
848 static int parent_crawl_children(struct udev_enumerate *enumerate, const char *path, int maxdepth)
857 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
860 if (dent->d_name[0] == '.')
862 if (dent->d_type != DT_DIR)
864 if (asprintf(&child, "%s/%s", path, dent->d_name) < 0)
866 parent_add_child(enumerate, child);
868 parent_crawl_children(enumerate, child, maxdepth-1);
876 static int scan_devices_children(struct udev_enumerate *enumerate)
880 path = udev_device_get_syspath(enumerate->parent_match);
881 parent_add_child(enumerate, path);
882 return parent_crawl_children(enumerate, path, 256);
885 static int scan_devices_all(struct udev_enumerate *udev_enumerate)
889 if (stat("/sys/subsystem", &statbuf) == 0) {
890 /* we have /subsystem/, forget all the old stuff */
891 scan_dir(udev_enumerate, "subsystem", "devices", NULL);
893 scan_dir(udev_enumerate, "bus", "devices", NULL);
894 scan_dir(udev_enumerate, "class", NULL, NULL);
900 * udev_enumerate_scan_devices:
901 * @udev_enumerate: udev enumeration context
903 * Scan /sys for all devices which match the given filters. No matches
904 * will return all currently available devices.
906 * Returns: 0 on success, otherwise a negative error value.
908 _public_ int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
910 if (udev_enumerate == NULL)
913 /* efficiently lookup tags only, we maintain a reverse-index */
914 if (udev_list_get_entry(&udev_enumerate->tags_match_list) != NULL)
915 return scan_devices_tags(udev_enumerate);
917 /* walk the subtree of one parent device only */
918 if (udev_enumerate->parent_match != NULL)
919 return scan_devices_children(udev_enumerate);
921 /* scan devices of all subsystems */
922 return scan_devices_all(udev_enumerate);
926 * udev_enumerate_scan_subsystems:
927 * @udev_enumerate: udev enumeration context
929 * Scan /sys for all kernel subsystems, including buses, classes, drivers.
931 * Returns: 0 on success, otherwise a negative error value.
933 _public_ int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate)
936 const char *subsysdir;
938 if (udev_enumerate == NULL)
941 /* all kernel modules */
942 if (match_subsystem(udev_enumerate, "module"))
943 scan_dir_and_add_devices(udev_enumerate, "module", NULL, NULL);
945 if (stat("/sys/subsystem", &statbuf) == 0)
946 subsysdir = "subsystem";
950 /* all subsystems (only buses support coldplug) */
951 if (match_subsystem(udev_enumerate, "subsystem"))
952 scan_dir_and_add_devices(udev_enumerate, subsysdir, NULL, NULL);
954 /* all subsystem drivers */
955 if (match_subsystem(udev_enumerate, "drivers"))
956 scan_dir(udev_enumerate, subsysdir, "drivers", "drivers");