2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
5 Copyright 2014-2015 Tom Gundersen <teg@jklm.no>
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "sd-device.h"
28 #include "device-util.h"
29 #include "device-enumerator-private.h"
31 #define DEVICE_ENUMERATE_MAX_DEPTH 256
33 typedef enum DeviceEnumerationType
{
34 DEVICE_ENUMERATION_TYPE_DEVICES
,
35 DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
,
36 _DEVICE_ENUMERATION_TYPE_MAX
,
37 _DEVICE_ENUMERATION_TYPE_INVALID
= -1,
38 } DeviceEnumerationType
;
40 struct sd_device_enumerator
{
43 DeviceEnumerationType type
;
48 Set
*nomatch_subsystem
;
49 Hashmap
*match_sysattr
;
50 Hashmap
*nomatch_sysattr
;
51 Hashmap
*match_property
;
54 sd_device
*match_parent
;
55 bool match_allow_uninitialized
;
58 _public_
int sd_device_enumerator_new(sd_device_enumerator
**ret
) {
59 _cleanup_device_enumerator_unref_ sd_device_enumerator
*enumerator
= NULL
;
63 enumerator
= new0(sd_device_enumerator
, 1);
67 enumerator
->n_ref
= 1;
68 enumerator
->type
= _DEVICE_ENUMERATION_TYPE_INVALID
;
76 _public_ sd_device_enumerator
*sd_device_enumerator_ref(sd_device_enumerator
*enumerator
) {
77 assert_return(enumerator
, NULL
);
79 assert_se((++ enumerator
->n_ref
) >= 2);
84 _public_ sd_device_enumerator
*sd_device_enumerator_unref(sd_device_enumerator
*enumerator
) {
85 if (enumerator
&& (-- enumerator
->n_ref
) == 0) {
88 while ((device
= prioq_pop(enumerator
->devices
)))
89 sd_device_unref(device
);
91 prioq_free(enumerator
->devices
);
93 set_free_free(enumerator
->match_subsystem
);
94 set_free_free(enumerator
->nomatch_subsystem
);
95 hashmap_free_free_free(enumerator
->match_sysattr
);
96 hashmap_free_free_free(enumerator
->nomatch_sysattr
);
97 hashmap_free_free_free(enumerator
->match_property
);
98 set_free_free(enumerator
->match_sysname
);
99 set_free_free(enumerator
->match_tag
);
100 sd_device_unref(enumerator
->match_parent
);
108 _public_
int sd_device_enumerator_add_match_subsystem(sd_device_enumerator
*enumerator
, const char *subsystem
, int match
) {
112 assert_return(enumerator
, -EINVAL
);
113 assert_return(subsystem
, -EINVAL
);
116 set
= &enumerator
->match_subsystem
;
118 set
= &enumerator
->nomatch_subsystem
;
120 r
= set_ensure_allocated(set
, NULL
);
124 r
= set_put_strdup(*set
, subsystem
);
128 enumerator
->scan_uptodate
= false;
133 _public_
int sd_device_enumerator_add_match_sysattr(sd_device_enumerator
*enumerator
, const char *_sysattr
, const char *_value
, int match
) {
134 _cleanup_free_
char *sysattr
= NULL
, *value
= NULL
;
138 assert_return(enumerator
, -EINVAL
);
139 assert_return(_sysattr
, -EINVAL
);
142 hashmap
= &enumerator
->match_sysattr
;
144 hashmap
= &enumerator
->nomatch_sysattr
;
146 r
= hashmap_ensure_allocated(hashmap
, NULL
);
150 sysattr
= strdup(_sysattr
);
155 value
= strdup(_value
);
160 r
= hashmap_put(*hashmap
, sysattr
, value
);
167 enumerator
->scan_uptodate
= false;
172 _public_
int sd_device_enumerator_add_match_property(sd_device_enumerator
*enumerator
, const char *_property
, const char *_value
) {
173 _cleanup_free_
char *property
= NULL
, *value
= NULL
;
176 assert_return(enumerator
, -EINVAL
);
177 assert_return(_property
, -EINVAL
);
179 r
= hashmap_ensure_allocated(&enumerator
->match_property
, NULL
);
183 property
= strdup(_property
);
188 value
= strdup(_value
);
193 r
= hashmap_put(enumerator
->match_property
, property
, value
);
200 enumerator
->scan_uptodate
= false;
205 _public_
int sd_device_enumerator_add_match_sysname(sd_device_enumerator
*enumerator
, const char *sysname
) {
208 assert_return(enumerator
, -EINVAL
);
209 assert_return(sysname
, -EINVAL
);
211 r
= set_ensure_allocated(&enumerator
->match_sysname
, NULL
);
215 r
= set_put_strdup(enumerator
->match_sysname
, sysname
);
219 enumerator
->scan_uptodate
= false;
224 _public_
int sd_device_enumerator_add_match_tag(sd_device_enumerator
*enumerator
, const char *tag
) {
227 assert_return(enumerator
, -EINVAL
);
228 assert_return(tag
, -EINVAL
);
230 r
= set_ensure_allocated(&enumerator
->match_tag
, NULL
);
234 r
= set_put_strdup(enumerator
->match_tag
, tag
);
238 enumerator
->scan_uptodate
= false;
243 _public_
int sd_device_enumerator_add_match_parent(sd_device_enumerator
*enumerator
, sd_device
*parent
) {
244 assert_return(enumerator
, -EINVAL
);
245 assert_return(parent
, -EINVAL
);
247 sd_device_unref(enumerator
->match_parent
);
248 enumerator
->match_parent
= sd_device_ref(parent
);
250 enumerator
->scan_uptodate
= false;
255 _public_
int sd_device_enumerator_allow_uninitialized(sd_device_enumerator
*enumerator
) {
256 assert_return(enumerator
, -EINVAL
);
258 enumerator
->match_allow_uninitialized
= true;
260 enumerator
->scan_uptodate
= false;
265 int device_enumerator_add_match_is_initialized(sd_device_enumerator
*enumerator
) {
266 assert_return(enumerator
, -EINVAL
);
268 enumerator
->match_allow_uninitialized
= false;
270 enumerator
->scan_uptodate
= false;
275 static int device_compare(const void *_a
, const void *_b
) {
276 sd_device
*a
= (sd_device
*)_a
, *b
= (sd_device
*)_b
;
277 const char *devpath_a
, *devpath_b
, *sound_a
;
278 bool delay_a
, delay_b
;
280 assert_se(sd_device_get_devpath(a
, &devpath_a
) >= 0);
281 assert_se(sd_device_get_devpath(b
, &devpath_b
) >= 0);
283 sound_a
= strstr(devpath_a
, "/sound/card");
285 /* For sound cards the control device must be enumerated last to
286 * make sure it's the final device node that gets ACLs applied.
287 * Applications rely on this fact and use ACL changes on the
288 * control node as an indicator that the ACL change of the
289 * entire sound card completed. The kernel makes this guarantee
290 * when creating those devices, and hence we should too when
291 * enumerating them. */
292 sound_a
+= strlen("/sound/card");
293 sound_a
= strchr(sound_a
, '/');
298 prefix_len
= sound_a
- devpath_a
;
300 if (strncmp(devpath_a
, devpath_b
, prefix_len
) == 0) {
303 sound_b
= devpath_b
+ prefix_len
;
305 if (startswith(sound_a
, "/controlC") &&
306 !startswith(sound_b
, "/contolC"))
309 if (!startswith(sound_a
, "/controlC") &&
310 startswith(sound_b
, "/controlC"))
316 /* md and dm devices are enumerated after all other devices */
317 delay_a
= strstr(devpath_a
, "/block/md") || strstr(devpath_a
, "/block/dm-");
318 delay_b
= strstr(devpath_b
, "/block/md") || strstr(devpath_b
, "/block/dm-");
319 if (delay_a
!= delay_b
)
320 return delay_a
- delay_b
;
322 return strcmp(devpath_a
, devpath_b
);
325 int device_enumerator_add_device(sd_device_enumerator
*enumerator
, sd_device
*device
) {
328 assert_return(enumerator
, -EINVAL
);
329 assert_return(device
, -EINVAL
);
331 r
= prioq_ensure_allocated(&enumerator
->devices
, device_compare
);
335 r
= prioq_put(enumerator
->devices
, device
, NULL
);
339 sd_device_ref(device
);
344 static bool match_sysattr_value(sd_device
*device
, const char *sysattr
, const char *match_value
) {
351 r
= sd_device_get_sysattr_value(device
, sysattr
, &value
);
358 if (fnmatch(match_value
, value
, 0) == 0)
364 static bool match_sysattr(sd_device_enumerator
*enumerator
, sd_device
*device
) {
372 HASHMAP_FOREACH_KEY(value
, sysattr
, enumerator
->nomatch_sysattr
, i
)
373 if (match_sysattr_value(device
, sysattr
, value
))
376 HASHMAP_FOREACH_KEY(value
, sysattr
, enumerator
->match_sysattr
, i
)
377 if (!match_sysattr_value(device
, sysattr
, value
))
383 static bool match_property(sd_device_enumerator
*enumerator
, sd_device
*device
) {
384 const char *property
;
391 if (hashmap_isempty(enumerator
->match_property
))
394 HASHMAP_FOREACH_KEY(value
, property
, enumerator
->match_property
, i
) {
395 const char *property_dev
, *value_dev
;
397 FOREACH_DEVICE_PROPERTY(device
, property_dev
, value_dev
) {
398 if (fnmatch(property
, property_dev
, 0) != 0)
401 if (!value
&& !value_dev
)
404 if (!value
|| !value_dev
)
407 if (fnmatch(value
, value_dev
, 0) == 0)
415 static bool match_tag(sd_device_enumerator
*enumerator
, sd_device
*device
) {
422 SET_FOREACH(tag
, enumerator
->match_tag
, i
)
423 if (!sd_device_has_tag(device
, tag
))
429 static bool match_parent(sd_device_enumerator
*enumerator
, sd_device
*device
) {
430 const char *devpath
, *devpath_dev
;
436 if (!enumerator
->match_parent
)
439 r
= sd_device_get_devpath(enumerator
->match_parent
, &devpath
);
442 r
= sd_device_get_devpath(device
, &devpath_dev
);
445 return startswith(devpath_dev
, devpath
);
448 static bool match_sysname(sd_device_enumerator
*enumerator
, const char *sysname
) {
449 const char *sysname_match
;
455 if (set_isempty(enumerator
->match_sysname
))
458 SET_FOREACH(sysname_match
, enumerator
->match_sysname
, i
)
459 if (fnmatch(sysname_match
, sysname
, 0) == 0)
465 static int enumerator_scan_dir_and_add_devices(sd_device_enumerator
*enumerator
, const char *basedir
, const char *subdir1
, const char *subdir2
) {
466 _cleanup_closedir_
DIR *dir
= NULL
;
474 path
= strjoina("/sys/", basedir
, "/");
477 path
= strjoina(path
, subdir1
, "/");
480 path
= strjoina(path
, subdir2
, "/");
486 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
487 _cleanup_device_unref_ sd_device
*device
= NULL
;
488 char syspath
[strlen(path
) + 1 + strlen(dent
->d_name
) + 1];
490 int ifindex
, initialized
, k
;
492 if (dent
->d_name
[0] == '.')
495 if (!match_sysname(enumerator
, dent
->d_name
))
498 (void)sprintf(syspath
, "%s%s", path
, dent
->d_name
);
500 k
= sd_device_new_from_syspath(&device
, syspath
);
503 /* this is necessarily racey, so ignore missing devices */
509 k
= sd_device_get_devnum(device
, &devnum
);
515 k
= sd_device_get_ifindex(device
, &ifindex
);
521 k
= sd_device_get_is_initialized(device
, &initialized
);
528 * All devices with a device node or network interfaces
529 * possibly need udev to adjust the device node permission
530 * or context, or rename the interface before it can be
531 * reliably used from other processes.
533 * For now, we can only check these types of devices, we
534 * might not store a database, and have no way to find out
535 * for all other types of devices.
537 if (!enumerator
->match_allow_uninitialized
&&
539 (major(devnum
) > 0 || ifindex
> 0))
542 if (!match_parent(enumerator
, device
))
545 if (!match_tag(enumerator
, device
))
548 if (!match_property(enumerator
, device
))
551 if (!match_sysattr(enumerator
, device
))
554 k
= device_enumerator_add_device(enumerator
, device
);
562 static bool match_subsystem(sd_device_enumerator
*enumerator
, const char *subsystem
) {
563 const char *subsystem_match
;
571 SET_FOREACH(subsystem_match
, enumerator
->nomatch_subsystem
, i
)
572 if (fnmatch(subsystem_match
, subsystem
, 0) == 0)
575 if (set_isempty(enumerator
->match_subsystem
))
578 SET_FOREACH(subsystem_match
, enumerator
->match_subsystem
, i
)
579 if (fnmatch(subsystem_match
, subsystem
, 0) == 0)
585 static int enumerator_scan_dir(sd_device_enumerator
*enumerator
, const char *basedir
, const char *subdir
, const char *subsystem
) {
586 _cleanup_closedir_
DIR *dir
= NULL
;
591 path
= strjoina("/sys/", basedir
);
597 log_debug(" device-enumerator: scanning %s", path
);
599 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
602 if (dent
->d_name
[0] == '.')
605 if (!match_subsystem(enumerator
, subsystem
? : dent
->d_name
))
608 k
= enumerator_scan_dir_and_add_devices(enumerator
, basedir
, dent
->d_name
, subdir
);
616 static int enumerator_scan_devices_tag(sd_device_enumerator
*enumerator
, const char *tag
) {
617 _cleanup_closedir_
DIR *dir
= NULL
;
625 path
= strjoina("/run/udev/tags/", tag
);
632 log_error("sd-device-enumerator: could not open tags directory %s: %m", path
);
637 /* TODO: filter away subsystems? */
639 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
640 _cleanup_device_unref_ sd_device
*device
= NULL
;
641 const char *subsystem
, *sysname
;
644 if (dent
->d_name
[0] == '.')
647 k
= sd_device_new_from_device_id(&device
, dent
->d_name
);
650 /* this is necessarily racy, so ignore missing devices */
656 k
= sd_device_get_subsystem(device
, &subsystem
);
662 if (!match_subsystem(enumerator
, subsystem
))
665 k
= sd_device_get_sysname(device
, &sysname
);
671 if (!match_sysname(enumerator
, sysname
))
674 if (!match_parent(enumerator
, device
))
677 if (!match_property(enumerator
, device
))
680 if (!match_sysattr(enumerator
, device
))
683 k
= device_enumerator_add_device(enumerator
, device
);
693 static int enumerator_scan_devices_tags(sd_device_enumerator
*enumerator
) {
700 SET_FOREACH(tag
, enumerator
->match_tag
, i
) {
701 r
= enumerator_scan_devices_tag(enumerator
, tag
);
709 static int parent_add_child(sd_device_enumerator
*enumerator
, const char *path
) {
710 _cleanup_device_unref_ sd_device
*device
= NULL
;
711 const char *subsystem
, *sysname
;
714 r
= sd_device_new_from_syspath(&device
, path
);
716 /* this is necessarily racy, so ignore missing devices */
721 r
= sd_device_get_subsystem(device
, &subsystem
);
725 if (!match_subsystem(enumerator
, subsystem
))
728 r
= sd_device_get_sysname(device
, &sysname
);
732 if (!match_sysname(enumerator
, sysname
))
735 if (!match_property(enumerator
, device
))
738 if (!match_sysattr(enumerator
, device
))
741 r
= device_enumerator_add_device(enumerator
, device
);
748 static int parent_crawl_children(sd_device_enumerator
*enumerator
, const char *path
, unsigned maxdepth
) {
749 _cleanup_closedir_
DIR *dir
= NULL
;
755 log_debug("sd-device-enumerate: could not open parent directory %s: %m", path
);
759 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
760 _cleanup_free_
char *child
= NULL
;
763 if (dent
->d_name
[0] == '.')
766 if (dent
->d_type
!= DT_DIR
)
769 child
= strjoin(path
, "/", dent
->d_name
, NULL
);
773 k
= parent_add_child(enumerator
, child
);
778 parent_crawl_children(enumerator
, child
, maxdepth
- 1);
780 log_debug("device-enumerate: max depth reached, %s: ignoring devices", child
);
786 static int enumerator_scan_devices_children(sd_device_enumerator
*enumerator
) {
790 r
= sd_device_get_syspath(enumerator
->match_parent
, &path
);
794 k
= parent_add_child(enumerator
, path
);
798 k
= parent_crawl_children(enumerator
, path
, DEVICE_ENUMERATE_MAX_DEPTH
);
805 static int enumerator_scan_devices_all(sd_device_enumerator
*enumerator
) {
808 log_debug("device-enumerator: scan all dirs");
810 if (access("/sys/subsystem", F_OK
) >= 0) {
811 /* we have /subsystem/, forget all the old stuff */
812 r
= enumerator_scan_dir(enumerator
, "subsystem", "devices", NULL
);
814 log_debug("device-enumerator: failed to scan /sys/subsystem: %s", strerror(-r
));
820 k
= enumerator_scan_dir(enumerator
, "bus", "devices", NULL
);
822 log_debug_errno(k
, "device-enumerator: failed to scan /sys/bus: %m");
826 k
= enumerator_scan_dir(enumerator
, "class", NULL
, NULL
);
828 log_debug_errno(k
, "device-enumerator: failed to scan /sys/class: %m");
836 int device_enumerator_scan_devices(sd_device_enumerator
*enumerator
) {
842 if (enumerator
->scan_uptodate
&&
843 enumerator
->type
== DEVICE_ENUMERATION_TYPE_DEVICES
)
846 while ((device
= prioq_pop(enumerator
->devices
)))
847 sd_device_unref(device
);
849 if (!set_isempty(enumerator
->match_tag
)) {
850 r
= enumerator_scan_devices_tags(enumerator
);
853 } else if (enumerator
->match_parent
) {
854 r
= enumerator_scan_devices_children(enumerator
);
858 r
= enumerator_scan_devices_all(enumerator
);
863 enumerator
->scan_uptodate
= true;
868 _public_ sd_device
*sd_device_enumerator_get_device_first(sd_device_enumerator
*enumerator
) {
871 assert_return(enumerator
, NULL
);
873 r
= device_enumerator_scan_devices(enumerator
);
877 enumerator
->type
= DEVICE_ENUMERATION_TYPE_DEVICES
;
879 return prioq_peek(enumerator
->devices
);
882 _public_ sd_device
*sd_device_enumerator_get_device_next(sd_device_enumerator
*enumerator
) {
883 assert_return(enumerator
, NULL
);
885 if (!enumerator
->scan_uptodate
||
886 enumerator
->type
!= DEVICE_ENUMERATION_TYPE_DEVICES
)
889 sd_device_unref(prioq_pop(enumerator
->devices
));
891 return prioq_peek(enumerator
->devices
);
894 int device_enumerator_scan_subsystems(sd_device_enumerator
*enumerator
) {
896 const char *subsysdir
;
901 if (enumerator
->scan_uptodate
&&
902 enumerator
->type
== DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
)
905 while ((device
= prioq_pop(enumerator
->devices
)))
906 sd_device_unref(device
);
909 if (match_subsystem(enumerator
, "module")) {
910 k
= enumerator_scan_dir_and_add_devices(enumerator
, "module", NULL
, NULL
);
912 log_debug_errno(k
, "device-enumerator: failed to scan modules: %m");
917 if (access("/sys/subsystem", F_OK
) >= 0)
918 subsysdir
= "subsystem";
922 /* subsystems (only buses support coldplug) */
923 if (match_subsystem(enumerator
, "subsystem")) {
924 k
= enumerator_scan_dir_and_add_devices(enumerator
, subsysdir
, NULL
, NULL
);
926 log_debug_errno(k
, "device-enumerator: failed to scan subsystems: %m");
931 /* subsystem drivers */
932 if (match_subsystem(enumerator
, "drivers")) {
933 k
= enumerator_scan_dir(enumerator
, subsysdir
, "drivers", "drivers");
935 log_debug_errno(k
, "device-enumerator: failed to scan drivers: %m");
940 enumerator
->scan_uptodate
= true;
945 _public_ sd_device
*sd_device_enumerator_get_subsystem_first(sd_device_enumerator
*enumerator
) {
948 assert_return(enumerator
, NULL
);
950 r
= device_enumerator_scan_subsystems(enumerator
);
954 enumerator
->type
= DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
;
956 return prioq_peek(enumerator
->devices
);
959 _public_ sd_device
*sd_device_enumerator_get_subsystem_next(sd_device_enumerator
*enumerator
) {
960 assert_return(enumerator
, NULL
);
962 if (enumerator
->scan_uptodate
||
963 enumerator
->type
!= DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
)
966 sd_device_unref(prioq_pop(enumerator
->devices
));
968 return prioq_peek(enumerator
->devices
);
971 sd_device
*device_enumerator_get_first(sd_device_enumerator
*enumerator
) {
972 assert_return(enumerator
, NULL
);
974 return prioq_peek(enumerator
->devices
);
977 sd_device
*device_enumerator_get_next(sd_device_enumerator
*enumerator
) {
978 assert_return(enumerator
, NULL
);
980 sd_device_unref(prioq_pop(enumerator
->devices
));
982 return prioq_peek(enumerator
->devices
);