2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
22 #include <sys/param.h>
25 #include "libudev-private.h"
28 * SECTION:libudev-enumerate
29 * @short_description: lookup and sort sys devices
31 * Lookup devices in the sys filesystem, filter devices by properties,
32 * and return a sorted list of devices.
43 * Opaque object representing one device lookup/sort context.
45 struct udev_enumerate
{
48 struct udev_list sysattr_match_list
;
49 struct udev_list sysattr_nomatch_list
;
50 struct udev_list subsystem_match_list
;
51 struct udev_list subsystem_nomatch_list
;
52 struct udev_list sysname_match_list
;
53 struct udev_list properties_match_list
;
54 struct udev_list tags_match_list
;
55 struct udev_device
*parent_match
;
56 struct udev_list devices_list
;
57 struct syspath
*devices
;
58 unsigned int devices_cur
;
59 unsigned int devices_max
;
60 bool devices_uptodate
:1;
61 bool match_is_initialized
;
66 * @udev: udev library context
68 * Create an enumeration context to scan /sys.
70 * Returns: an enumeration context.
72 _public_
struct udev_enumerate
*udev_enumerate_new(struct udev
*udev
)
74 struct udev_enumerate
*udev_enumerate
;
76 udev_enumerate
= calloc(1, sizeof(struct udev_enumerate
));
77 if (udev_enumerate
== NULL
)
79 udev_enumerate
->refcount
= 1;
80 udev_enumerate
->udev
= udev
;
81 udev_list_init(udev
, &udev_enumerate
->sysattr_match_list
, false);
82 udev_list_init(udev
, &udev_enumerate
->sysattr_nomatch_list
, false);
83 udev_list_init(udev
, &udev_enumerate
->subsystem_match_list
, true);
84 udev_list_init(udev
, &udev_enumerate
->subsystem_nomatch_list
, true);
85 udev_list_init(udev
, &udev_enumerate
->sysname_match_list
, true);
86 udev_list_init(udev
, &udev_enumerate
->properties_match_list
, false);
87 udev_list_init(udev
, &udev_enumerate
->tags_match_list
, true);
88 udev_list_init(udev
, &udev_enumerate
->devices_list
, false);
89 return udev_enumerate
;
94 * @udev_enumerate: context
96 * Take a reference of a enumeration context.
98 * Returns: the passed enumeration context
100 _public_
struct udev_enumerate
*udev_enumerate_ref(struct udev_enumerate
*udev_enumerate
)
102 if (udev_enumerate
== NULL
)
104 udev_enumerate
->refcount
++;
105 return udev_enumerate
;
109 * udev_enumerate_unref:
110 * @udev_enumerate: context
112 * Drop a reference of an enumeration context. If the refcount reaches zero,
113 * all resources of the enumeration context will be released.
115 * Returns: the passed enumeration context if it has still an active reference, or #NULL otherwise.
117 _public_
struct udev_enumerate
*udev_enumerate_unref(struct udev_enumerate
*udev_enumerate
)
121 if (udev_enumerate
== NULL
)
123 udev_enumerate
->refcount
--;
124 if (udev_enumerate
->refcount
> 0)
125 return udev_enumerate
;
126 udev_list_cleanup(&udev_enumerate
->sysattr_match_list
);
127 udev_list_cleanup(&udev_enumerate
->sysattr_nomatch_list
);
128 udev_list_cleanup(&udev_enumerate
->subsystem_match_list
);
129 udev_list_cleanup(&udev_enumerate
->subsystem_nomatch_list
);
130 udev_list_cleanup(&udev_enumerate
->sysname_match_list
);
131 udev_list_cleanup(&udev_enumerate
->properties_match_list
);
132 udev_list_cleanup(&udev_enumerate
->tags_match_list
);
133 udev_device_unref(udev_enumerate
->parent_match
);
134 udev_list_cleanup(&udev_enumerate
->devices_list
);
135 for (i
= 0; i
< udev_enumerate
->devices_cur
; i
++)
136 free(udev_enumerate
->devices
[i
].syspath
);
137 free(udev_enumerate
->devices
);
138 free(udev_enumerate
);
143 * udev_enumerate_get_udev:
144 * @udev_enumerate: context
146 * Get the udev library context.
148 * Returns: a pointer to the context.
150 _public_
struct udev
*udev_enumerate_get_udev(struct udev_enumerate
*udev_enumerate
)
152 if (udev_enumerate
== NULL
)
154 return udev_enumerate
->udev
;
157 static int syspath_add(struct udev_enumerate
*udev_enumerate
, const char *syspath
)
160 struct syspath
*entry
;
162 /* double array size if needed */
163 if (udev_enumerate
->devices_cur
>= udev_enumerate
->devices_max
) {
167 add
= udev_enumerate
->devices_max
;
170 buf
= realloc(udev_enumerate
->devices
, (udev_enumerate
->devices_max
+ add
) * sizeof(struct syspath
));
173 udev_enumerate
->devices
= buf
;
174 udev_enumerate
->devices_max
+= add
;
177 path
= strdup(syspath
);
180 entry
= &udev_enumerate
->devices
[udev_enumerate
->devices_cur
];
181 entry
->syspath
= path
;
182 entry
->len
= strlen(path
);
183 udev_enumerate
->devices_cur
++;
184 udev_enumerate
->devices_uptodate
= false;
188 static int syspath_cmp(const void *p1
, const void *p2
)
190 const struct syspath
*path1
= p1
;
191 const struct syspath
*path2
= p2
;
195 len
= MIN(path1
->len
, path2
->len
);
196 ret
= memcmp(path1
->syspath
, path2
->syspath
, len
);
198 if (path1
->len
< path2
->len
)
200 else if (path1
->len
> path2
->len
)
206 /* For devices that should be moved to the absolute end of the list */
207 static bool devices_delay_end(struct udev
*udev
, const char *syspath
)
209 static const char *delay_device_list
[] = {
216 for (i
= 0; delay_device_list
[i
] != NULL
; i
++) {
217 if (strstr(syspath
+ strlen("/sys"), delay_device_list
[i
]) != NULL
)
223 /* For devices that should just be moved a little bit later, just
224 * before the point where some common path prefix changes. Returns the
225 * number of characters that make up that common prefix */
226 static size_t devices_delay_later(struct udev
*udev
, const char *syspath
)
230 /* For sound cards the control device must be enumerated last
231 * to make sure it's the final device node that gets ACLs
232 * applied. Applications rely on this fact and use ACL changes
233 * on the control node as an indicator that the ACL change of
234 * the entire sound card completed. The kernel makes this
235 * guarantee when creating those devices, and hence we should
236 * too when enumerating them. */
238 if ((c
= strstr(syspath
, "/sound/card"))) {
240 c
+= strcspn(c
, "/");
242 if (startswith(c
, "/controlC"))
243 return c
- syspath
+ 1;
250 * udev_enumerate_get_list_entry:
251 * @udev_enumerate: context
253 * Get the first entry of the sorted list of device paths.
255 * Returns: a udev_list_entry.
257 _public_
struct udev_list_entry
*udev_enumerate_get_list_entry(struct udev_enumerate
*udev_enumerate
)
259 if (udev_enumerate
== NULL
)
261 if (!udev_enumerate
->devices_uptodate
) {
264 struct syspath
*prev
= NULL
, *move_later
= NULL
;
265 size_t move_later_prefix
= 0;
267 udev_list_cleanup(&udev_enumerate
->devices_list
);
268 qsort(udev_enumerate
->devices
, udev_enumerate
->devices_cur
, sizeof(struct syspath
), syspath_cmp
);
270 max
= udev_enumerate
->devices_cur
;
271 for (i
= 0; i
< max
; i
++) {
272 struct syspath
*entry
= &udev_enumerate
->devices
[i
];
274 /* skip duplicated entries */
276 entry
->len
== prev
->len
&&
277 memcmp(entry
->syspath
, prev
->syspath
, entry
->len
) == 0)
281 /* skip to be delayed devices, and add them to the end of the list */
282 if (devices_delay_end(udev_enumerate
->udev
, entry
->syspath
)) {
283 syspath_add(udev_enumerate
, entry
->syspath
);
284 /* need to update prev here for the case realloc() gives a different address */
285 prev
= &udev_enumerate
->devices
[i
];
289 /* skip to be delayed devices, and move the to
290 * the point where the prefix changes. We can
291 * only move one item at a time. */
293 move_later_prefix
= devices_delay_later(udev_enumerate
->udev
, entry
->syspath
);
295 if (move_later_prefix
> 0) {
302 strncmp(entry
->syspath
, move_later
->syspath
, move_later_prefix
) != 0) {
304 udev_list_entry_add(&udev_enumerate
->devices_list
, move_later
->syspath
, NULL
);
308 udev_list_entry_add(&udev_enumerate
->devices_list
, entry
->syspath
, NULL
);
312 udev_list_entry_add(&udev_enumerate
->devices_list
, move_later
->syspath
, NULL
);
314 /* add and cleanup delayed devices from end of list */
315 for (i
= max
; i
< udev_enumerate
->devices_cur
; i
++) {
316 struct syspath
*entry
= &udev_enumerate
->devices
[i
];
318 udev_list_entry_add(&udev_enumerate
->devices_list
, entry
->syspath
, NULL
);
319 free(entry
->syspath
);
321 udev_enumerate
->devices_cur
= max
;
323 udev_enumerate
->devices_uptodate
= true;
325 return udev_list_get_entry(&udev_enumerate
->devices_list
);
329 * udev_enumerate_add_match_subsystem:
330 * @udev_enumerate: context
331 * @subsystem: filter for a subsystem of the device to include in the list
333 * Match only devices belonging to a certain kernel subsystem.
335 * Returns: 0 on success, otherwise a negative error value.
337 _public_
int udev_enumerate_add_match_subsystem(struct udev_enumerate
*udev_enumerate
, const char *subsystem
)
339 if (udev_enumerate
== NULL
)
341 if (subsystem
== NULL
)
343 if (udev_list_entry_add(&udev_enumerate
->subsystem_match_list
, subsystem
, NULL
) == NULL
)
349 * udev_enumerate_add_nomatch_subsystem:
350 * @udev_enumerate: context
351 * @subsystem: filter for a subsystem of the device to exclude from the list
353 * Match only devices not belonging to a certain kernel subsystem.
355 * Returns: 0 on success, otherwise a negative error value.
357 _public_
int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate
*udev_enumerate
, const char *subsystem
)
359 if (udev_enumerate
== NULL
)
361 if (subsystem
== NULL
)
363 if (udev_list_entry_add(&udev_enumerate
->subsystem_nomatch_list
, subsystem
, NULL
) == NULL
)
369 * udev_enumerate_add_match_sysattr:
370 * @udev_enumerate: context
371 * @sysattr: filter for a sys attribute at the device to include in the list
372 * @value: optional value of the sys attribute
374 * Match only devices with a certain /sys device attribute.
376 * Returns: 0 on success, otherwise a negative error value.
378 _public_
int udev_enumerate_add_match_sysattr(struct udev_enumerate
*udev_enumerate
, const char *sysattr
, const char *value
)
380 if (udev_enumerate
== NULL
)
384 if (udev_list_entry_add(&udev_enumerate
->sysattr_match_list
, sysattr
, value
) == NULL
)
390 * udev_enumerate_add_nomatch_sysattr:
391 * @udev_enumerate: context
392 * @sysattr: filter for a sys attribute at the device to exclude from the list
393 * @value: optional value of the sys attribute
395 * Match only devices not having a certain /sys device attribute.
397 * Returns: 0 on success, otherwise a negative error value.
399 _public_
int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate
*udev_enumerate
, const char *sysattr
, const char *value
)
401 if (udev_enumerate
== NULL
)
405 if (udev_list_entry_add(&udev_enumerate
->sysattr_nomatch_list
, sysattr
, value
) == NULL
)
410 static int match_sysattr_value(struct udev_device
*dev
, const char *sysattr
, const char *match_val
)
412 const char *val
= NULL
;
415 val
= udev_device_get_sysattr_value(dev
, sysattr
);
418 if (match_val
== NULL
) {
422 if (fnmatch(match_val
, val
, 0) == 0) {
431 * udev_enumerate_add_match_property:
432 * @udev_enumerate: context
433 * @property: filter for a property of the device to include in the list
434 * @value: value of the property
436 * Match only devices with a certain property.
438 * Returns: 0 on success, otherwise a negative error value.
440 _public_
int udev_enumerate_add_match_property(struct udev_enumerate
*udev_enumerate
, const char *property
, const char *value
)
442 if (udev_enumerate
== NULL
)
444 if (property
== NULL
)
446 if (udev_list_entry_add(&udev_enumerate
->properties_match_list
, property
, value
) == NULL
)
452 * udev_enumerate_add_match_tag:
453 * @udev_enumerate: context
454 * @tag: filter for a tag of the device to include in the list
456 * Match only devices with a certain tag.
458 * Returns: 0 on success, otherwise a negative error value.
460 _public_
int udev_enumerate_add_match_tag(struct udev_enumerate
*udev_enumerate
, const char *tag
)
462 if (udev_enumerate
== NULL
)
466 if (udev_list_entry_add(&udev_enumerate
->tags_match_list
, tag
, NULL
) == NULL
)
472 * udev_enumerate_add_match_parent:
473 * @udev_enumerate: context
474 * @parent: parent device where to start searching
476 * Return the devices on the subtree of one given device. The parent
477 * itself is included in the list.
479 * A reference for the device is held until the udev_enumerate context
482 * Returns: 0 on success, otherwise a negative error value.
484 _public_
int udev_enumerate_add_match_parent(struct udev_enumerate
*udev_enumerate
, struct udev_device
*parent
)
486 if (udev_enumerate
== NULL
)
490 if (udev_enumerate
->parent_match
!= NULL
)
491 udev_device_unref(udev_enumerate
->parent_match
);
492 udev_enumerate
->parent_match
= udev_device_ref(parent
);
497 * udev_enumerate_add_match_is_initialized:
498 * @udev_enumerate: context
500 * Match only devices which udev has set up already. This makes
501 * sure, that the device node permissions and context are properly set
502 * and that network devices are fully renamed.
504 * Usually, devices which are found in the kernel but not already
505 * handled by udev, have still pending events. Services should subscribe
506 * to monitor events and wait for these devices to become ready, instead
507 * of using uninitialized devices.
509 * For now, this will not affect devices which do not have a device node
510 * and are not network interfaces.
512 * Returns: 0 on success, otherwise a negative error value.
514 _public_
int udev_enumerate_add_match_is_initialized(struct udev_enumerate
*udev_enumerate
)
516 if (udev_enumerate
== NULL
)
518 udev_enumerate
->match_is_initialized
= true;
523 * udev_enumerate_add_match_sysname:
524 * @udev_enumerate: context
525 * @sysname: filter for the name of the device to include in the list
527 * Match only devices with a given /sys device name.
529 * Returns: 0 on success, otherwise a negative error value.
531 _public_
int udev_enumerate_add_match_sysname(struct udev_enumerate
*udev_enumerate
, const char *sysname
)
533 if (udev_enumerate
== NULL
)
537 if (udev_list_entry_add(&udev_enumerate
->sysname_match_list
, sysname
, NULL
) == NULL
)
542 static bool match_sysattr(struct udev_enumerate
*udev_enumerate
, struct udev_device
*dev
)
544 struct udev_list_entry
*list_entry
;
547 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_enumerate
->sysattr_nomatch_list
)) {
548 if (match_sysattr_value(dev
, udev_list_entry_get_name(list_entry
),
549 udev_list_entry_get_value(list_entry
)))
553 if (udev_list_get_entry(&udev_enumerate
->sysattr_match_list
) != NULL
) {
554 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_enumerate
->sysattr_match_list
)) {
555 /* anything that does not match, will make it FALSE */
556 if (!match_sysattr_value(dev
, udev_list_entry_get_name(list_entry
),
557 udev_list_entry_get_value(list_entry
)))
565 static bool match_property(struct udev_enumerate
*udev_enumerate
, struct udev_device
*dev
)
567 struct udev_list_entry
*list_entry
;
570 /* no match always matches */
571 if (udev_list_get_entry(&udev_enumerate
->properties_match_list
) == NULL
)
574 /* loop over matches */
575 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_enumerate
->properties_match_list
)) {
576 const char *match_key
= udev_list_entry_get_name(list_entry
);
577 const char *match_value
= udev_list_entry_get_value(list_entry
);
578 struct udev_list_entry
*property_entry
;
580 /* loop over device properties */
581 udev_list_entry_foreach(property_entry
, udev_device_get_properties_list_entry(dev
)) {
582 const char *dev_key
= udev_list_entry_get_name(property_entry
);
583 const char *dev_value
= udev_list_entry_get_value(property_entry
);
585 if (fnmatch(match_key
, dev_key
, 0) != 0)
587 if (match_value
== NULL
&& dev_value
== NULL
) {
591 if (match_value
== NULL
|| dev_value
== NULL
)
593 if (fnmatch(match_value
, dev_value
, 0) == 0) {
603 static bool match_tag(struct udev_enumerate
*udev_enumerate
, struct udev_device
*dev
)
605 struct udev_list_entry
*list_entry
;
607 /* no match always matches */
608 if (udev_list_get_entry(&udev_enumerate
->tags_match_list
) == NULL
)
611 /* loop over matches */
612 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_enumerate
->tags_match_list
))
613 if (!udev_device_has_tag(dev
, udev_list_entry_get_name(list_entry
)))
619 static bool match_parent(struct udev_enumerate
*udev_enumerate
, struct udev_device
*dev
)
621 if (udev_enumerate
->parent_match
== NULL
)
624 return startswith(udev_device_get_devpath(dev
), udev_device_get_devpath(udev_enumerate
->parent_match
));
627 static bool match_sysname(struct udev_enumerate
*udev_enumerate
, const char *sysname
)
629 struct udev_list_entry
*list_entry
;
631 if (udev_list_get_entry(&udev_enumerate
->sysname_match_list
) == NULL
)
634 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_enumerate
->sysname_match_list
)) {
635 if (fnmatch(udev_list_entry_get_name(list_entry
), sysname
, 0) != 0)
642 static int scan_dir_and_add_devices(struct udev_enumerate
*udev_enumerate
,
643 const char *basedir
, const char *subdir1
, const char *subdir2
)
645 char path
[UTIL_PATH_SIZE
];
652 l
= util_strpcpyl(&s
, sizeof(path
), "/sys/", basedir
, NULL
);
654 l
= util_strpcpyl(&s
, l
, "/", subdir1
, NULL
);
656 util_strpcpyl(&s
, l
, "/", subdir2
, NULL
);
660 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
661 char syspath
[UTIL_PATH_SIZE
];
662 struct udev_device
*dev
;
664 if (dent
->d_name
[0] == '.')
667 if (!match_sysname(udev_enumerate
, dent
->d_name
))
670 util_strscpyl(syspath
, sizeof(syspath
), path
, "/", dent
->d_name
, NULL
);
671 dev
= udev_device_new_from_syspath(udev_enumerate
->udev
, syspath
);
675 if (udev_enumerate
->match_is_initialized
) {
677 * All devices with a device node or network interfaces
678 * possibly need udev to adjust the device node permission
679 * or context, or rename the interface before it can be
680 * reliably used from other processes.
682 * For now, we can only check these types of devices, we
683 * might not store a database, and have no way to find out
684 * for all other types of devices.
686 if (!udev_device_get_is_initialized(dev
) &&
687 (major(udev_device_get_devnum(dev
)) > 0 || udev_device_get_ifindex(dev
) > 0))
690 if (!match_parent(udev_enumerate
, dev
))
692 if (!match_tag(udev_enumerate
, dev
))
694 if (!match_property(udev_enumerate
, dev
))
696 if (!match_sysattr(udev_enumerate
, dev
))
699 syspath_add(udev_enumerate
, udev_device_get_syspath(dev
));
701 udev_device_unref(dev
);
707 static bool match_subsystem(struct udev_enumerate
*udev_enumerate
, const char *subsystem
)
709 struct udev_list_entry
*list_entry
;
711 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_enumerate
->subsystem_nomatch_list
)) {
712 if (fnmatch(udev_list_entry_get_name(list_entry
), subsystem
, 0) == 0)
715 if (udev_list_get_entry(&udev_enumerate
->subsystem_match_list
) != NULL
) {
716 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_enumerate
->subsystem_match_list
)) {
717 if (fnmatch(udev_list_entry_get_name(list_entry
), subsystem
, 0) == 0)
725 static int scan_dir(struct udev_enumerate
*udev_enumerate
, const char *basedir
, const char *subdir
, const char *subsystem
)
727 char path
[UTIL_PATH_SIZE
];
731 util_strscpyl(path
, sizeof(path
), "/sys/", basedir
, NULL
);
735 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
736 if (dent
->d_name
[0] == '.')
738 if (!match_subsystem(udev_enumerate
, subsystem
!= NULL
? subsystem
: dent
->d_name
))
740 scan_dir_and_add_devices(udev_enumerate
, basedir
, dent
->d_name
, subdir
);
747 * udev_enumerate_add_syspath:
748 * @udev_enumerate: context
749 * @syspath: path of a device
751 * Add a device to the list of devices, to retrieve it back sorted in dependency order.
753 * Returns: 0 on success, otherwise a negative error value.
755 _public_
int udev_enumerate_add_syspath(struct udev_enumerate
*udev_enumerate
, const char *syspath
)
757 struct udev_device
*udev_device
;
759 if (udev_enumerate
== NULL
)
763 /* resolve to real syspath */
764 udev_device
= udev_device_new_from_syspath(udev_enumerate
->udev
, syspath
);
765 if (udev_device
== NULL
)
767 syspath_add(udev_enumerate
, udev_device_get_syspath(udev_device
));
768 udev_device_unref(udev_device
);
772 static int scan_devices_tags(struct udev_enumerate
*udev_enumerate
)
774 struct udev_list_entry
*list_entry
;
776 /* scan only tagged devices, use tags reverse-index, instead of searching all devices in /sys */
777 udev_list_entry_foreach(list_entry
, udev_list_get_entry(&udev_enumerate
->tags_match_list
)) {
780 char path
[UTIL_PATH_SIZE
];
782 util_strscpyl(path
, sizeof(path
), "/run/udev/tags/", udev_list_entry_get_name(list_entry
), NULL
);
786 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
787 struct udev_device
*dev
;
789 if (dent
->d_name
[0] == '.')
792 dev
= udev_device_new_from_id_filename(udev_enumerate
->udev
, dent
->d_name
);
796 if (!match_subsystem(udev_enumerate
, udev_device_get_subsystem(dev
)))
798 if (!match_sysname(udev_enumerate
, udev_device_get_sysname(dev
)))
800 if (!match_parent(udev_enumerate
, dev
))
802 if (!match_property(udev_enumerate
, dev
))
804 if (!match_sysattr(udev_enumerate
, dev
))
807 syspath_add(udev_enumerate
, udev_device_get_syspath(dev
));
809 udev_device_unref(dev
);
816 static int parent_add_child(struct udev_enumerate
*enumerate
, const char *path
)
818 struct udev_device
*dev
;
820 dev
= udev_device_new_from_syspath(enumerate
->udev
, path
);
824 if (!match_subsystem(enumerate
, udev_device_get_subsystem(dev
)))
826 if (!match_sysname(enumerate
, udev_device_get_sysname(dev
)))
828 if (!match_property(enumerate
, dev
))
830 if (!match_sysattr(enumerate
, dev
))
833 syspath_add(enumerate
, udev_device_get_syspath(dev
));
834 udev_device_unref(dev
);
838 static int parent_crawl_children(struct udev_enumerate
*enumerate
, const char *path
, int maxdepth
)
847 for (dent
= readdir(d
); dent
!= NULL
; dent
= readdir(d
)) {
850 if (dent
->d_name
[0] == '.')
852 if (dent
->d_type
!= DT_DIR
)
854 if (asprintf(&child
, "%s/%s", path
, dent
->d_name
) < 0)
856 parent_add_child(enumerate
, child
);
858 parent_crawl_children(enumerate
, child
, maxdepth
-1);
866 static int scan_devices_children(struct udev_enumerate
*enumerate
)
870 path
= udev_device_get_syspath(enumerate
->parent_match
);
871 parent_add_child(enumerate
, path
);
872 return parent_crawl_children(enumerate
, path
, 256);
875 static int scan_devices_all(struct udev_enumerate
*udev_enumerate
)
879 if (stat("/sys/subsystem", &statbuf
) == 0) {
880 /* we have /subsystem/, forget all the old stuff */
881 scan_dir(udev_enumerate
, "subsystem", "devices", NULL
);
883 scan_dir(udev_enumerate
, "bus", "devices", NULL
);
884 scan_dir(udev_enumerate
, "class", NULL
, NULL
);
890 * udev_enumerate_scan_devices:
891 * @udev_enumerate: udev enumeration context
893 * Scan /sys for all devices which match the given filters. No matches
894 * will return all currently available devices.
896 * Returns: 0 on success, otherwise a negative error value.
898 _public_
int udev_enumerate_scan_devices(struct udev_enumerate
*udev_enumerate
)
900 if (udev_enumerate
== NULL
)
903 /* efficiently lookup tags only, we maintain a reverse-index */
904 if (udev_list_get_entry(&udev_enumerate
->tags_match_list
) != NULL
)
905 return scan_devices_tags(udev_enumerate
);
907 /* walk the subtree of one parent device only */
908 if (udev_enumerate
->parent_match
!= NULL
)
909 return scan_devices_children(udev_enumerate
);
911 /* scan devices of all subsystems */
912 return scan_devices_all(udev_enumerate
);
916 * udev_enumerate_scan_subsystems:
917 * @udev_enumerate: udev enumeration context
919 * Scan /sys for all kernel subsystems, including buses, classes, drivers.
921 * Returns: 0 on success, otherwise a negative error value.
923 _public_
int udev_enumerate_scan_subsystems(struct udev_enumerate
*udev_enumerate
)
926 const char *subsysdir
;
928 if (udev_enumerate
== NULL
)
931 /* all kernel modules */
932 if (match_subsystem(udev_enumerate
, "module"))
933 scan_dir_and_add_devices(udev_enumerate
, "module", NULL
, NULL
);
935 if (stat("/sys/subsystem", &statbuf
) == 0)
936 subsysdir
= "subsystem";
940 /* all subsystems (only buses support coldplug) */
941 if (match_subsystem(udev_enumerate
, "subsystem"))
942 scan_dir_and_add_devices(udev_enumerate
, subsysdir
, NULL
, NULL
);
944 /* all subsystem drivers */
945 if (match_subsystem(udev_enumerate
, "drivers"))
946 scan_dir(udev_enumerate
, subsysdir
, "drivers", "drivers");