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/>.
21 #include "sd-device.h"
23 #include "device-enumerator-private.h"
24 #include "device-util.h"
25 #include "dirent-util.h"
29 #include "string-util.h"
33 #define DEVICE_ENUMERATE_MAX_DEPTH 256
35 typedef enum DeviceEnumerationType
{
36 DEVICE_ENUMERATION_TYPE_DEVICES
,
37 DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
,
38 _DEVICE_ENUMERATION_TYPE_MAX
,
39 _DEVICE_ENUMERATION_TYPE_INVALID
= -1,
40 } DeviceEnumerationType
;
42 struct sd_device_enumerator
{
45 DeviceEnumerationType type
;
50 Set
*nomatch_subsystem
;
51 Hashmap
*match_sysattr
;
52 Hashmap
*nomatch_sysattr
;
53 Hashmap
*match_property
;
56 sd_device
*match_parent
;
57 bool match_allow_uninitialized
;
60 _public_
int sd_device_enumerator_new(sd_device_enumerator
**ret
) {
61 _cleanup_device_enumerator_unref_ sd_device_enumerator
*enumerator
= NULL
;
65 enumerator
= new0(sd_device_enumerator
, 1);
69 enumerator
->n_ref
= 1;
70 enumerator
->type
= _DEVICE_ENUMERATION_TYPE_INVALID
;
78 _public_ sd_device_enumerator
*sd_device_enumerator_ref(sd_device_enumerator
*enumerator
) {
79 assert_return(enumerator
, NULL
);
81 assert_se((++ enumerator
->n_ref
) >= 2);
86 _public_ sd_device_enumerator
*sd_device_enumerator_unref(sd_device_enumerator
*enumerator
) {
87 if (enumerator
&& (-- enumerator
->n_ref
) == 0) {
90 while ((device
= prioq_pop(enumerator
->devices
)))
91 sd_device_unref(device
);
93 prioq_free(enumerator
->devices
);
95 set_free_free(enumerator
->match_subsystem
);
96 set_free_free(enumerator
->nomatch_subsystem
);
97 hashmap_free_free_free(enumerator
->match_sysattr
);
98 hashmap_free_free_free(enumerator
->nomatch_sysattr
);
99 hashmap_free_free_free(enumerator
->match_property
);
100 set_free_free(enumerator
->match_sysname
);
101 set_free_free(enumerator
->match_tag
);
102 sd_device_unref(enumerator
->match_parent
);
110 _public_
int sd_device_enumerator_add_match_subsystem(sd_device_enumerator
*enumerator
, const char *subsystem
, int match
) {
114 assert_return(enumerator
, -EINVAL
);
115 assert_return(subsystem
, -EINVAL
);
118 set
= &enumerator
->match_subsystem
;
120 set
= &enumerator
->nomatch_subsystem
;
122 r
= set_ensure_allocated(set
, NULL
);
126 r
= set_put_strdup(*set
, subsystem
);
130 enumerator
->scan_uptodate
= false;
135 _public_
int sd_device_enumerator_add_match_sysattr(sd_device_enumerator
*enumerator
, const char *_sysattr
, const char *_value
, int match
) {
136 _cleanup_free_
char *sysattr
= NULL
, *value
= NULL
;
140 assert_return(enumerator
, -EINVAL
);
141 assert_return(_sysattr
, -EINVAL
);
144 hashmap
= &enumerator
->match_sysattr
;
146 hashmap
= &enumerator
->nomatch_sysattr
;
148 r
= hashmap_ensure_allocated(hashmap
, NULL
);
152 sysattr
= strdup(_sysattr
);
157 value
= strdup(_value
);
162 r
= hashmap_put(*hashmap
, sysattr
, value
);
169 enumerator
->scan_uptodate
= false;
174 _public_
int sd_device_enumerator_add_match_property(sd_device_enumerator
*enumerator
, const char *_property
, const char *_value
) {
175 _cleanup_free_
char *property
= NULL
, *value
= NULL
;
178 assert_return(enumerator
, -EINVAL
);
179 assert_return(_property
, -EINVAL
);
181 r
= hashmap_ensure_allocated(&enumerator
->match_property
, NULL
);
185 property
= strdup(_property
);
190 value
= strdup(_value
);
195 r
= hashmap_put(enumerator
->match_property
, property
, value
);
202 enumerator
->scan_uptodate
= false;
207 _public_
int sd_device_enumerator_add_match_sysname(sd_device_enumerator
*enumerator
, const char *sysname
) {
210 assert_return(enumerator
, -EINVAL
);
211 assert_return(sysname
, -EINVAL
);
213 r
= set_ensure_allocated(&enumerator
->match_sysname
, NULL
);
217 r
= set_put_strdup(enumerator
->match_sysname
, sysname
);
221 enumerator
->scan_uptodate
= false;
226 _public_
int sd_device_enumerator_add_match_tag(sd_device_enumerator
*enumerator
, const char *tag
) {
229 assert_return(enumerator
, -EINVAL
);
230 assert_return(tag
, -EINVAL
);
232 r
= set_ensure_allocated(&enumerator
->match_tag
, NULL
);
236 r
= set_put_strdup(enumerator
->match_tag
, tag
);
240 enumerator
->scan_uptodate
= false;
245 _public_
int sd_device_enumerator_add_match_parent(sd_device_enumerator
*enumerator
, sd_device
*parent
) {
246 assert_return(enumerator
, -EINVAL
);
247 assert_return(parent
, -EINVAL
);
249 sd_device_unref(enumerator
->match_parent
);
250 enumerator
->match_parent
= sd_device_ref(parent
);
252 enumerator
->scan_uptodate
= false;
257 _public_
int sd_device_enumerator_allow_uninitialized(sd_device_enumerator
*enumerator
) {
258 assert_return(enumerator
, -EINVAL
);
260 enumerator
->match_allow_uninitialized
= true;
262 enumerator
->scan_uptodate
= false;
267 int device_enumerator_add_match_is_initialized(sd_device_enumerator
*enumerator
) {
268 assert_return(enumerator
, -EINVAL
);
270 enumerator
->match_allow_uninitialized
= false;
272 enumerator
->scan_uptodate
= false;
277 static int device_compare(const void *_a
, const void *_b
) {
278 sd_device
*a
= (sd_device
*)_a
, *b
= (sd_device
*)_b
;
279 const char *devpath_a
, *devpath_b
, *sound_a
;
280 bool delay_a
, delay_b
;
282 assert_se(sd_device_get_devpath(a
, &devpath_a
) >= 0);
283 assert_se(sd_device_get_devpath(b
, &devpath_b
) >= 0);
285 sound_a
= strstr(devpath_a
, "/sound/card");
287 /* For sound cards the control device must be enumerated last to
288 * make sure it's the final device node that gets ACLs applied.
289 * Applications rely on this fact and use ACL changes on the
290 * control node as an indicator that the ACL change of the
291 * entire sound card completed. The kernel makes this guarantee
292 * when creating those devices, and hence we should too when
293 * enumerating them. */
294 sound_a
+= strlen("/sound/card");
295 sound_a
= strchr(sound_a
, '/');
300 prefix_len
= sound_a
- devpath_a
;
302 if (strncmp(devpath_a
, devpath_b
, prefix_len
) == 0) {
305 sound_b
= devpath_b
+ prefix_len
;
307 if (startswith(sound_a
, "/controlC") &&
308 !startswith(sound_b
, "/contolC"))
311 if (!startswith(sound_a
, "/controlC") &&
312 startswith(sound_b
, "/controlC"))
318 /* md and dm devices are enumerated after all other devices */
319 delay_a
= strstr(devpath_a
, "/block/md") || strstr(devpath_a
, "/block/dm-");
320 delay_b
= strstr(devpath_b
, "/block/md") || strstr(devpath_b
, "/block/dm-");
321 if (delay_a
!= delay_b
)
322 return delay_a
- delay_b
;
324 return strcmp(devpath_a
, devpath_b
);
327 int device_enumerator_add_device(sd_device_enumerator
*enumerator
, sd_device
*device
) {
330 assert_return(enumerator
, -EINVAL
);
331 assert_return(device
, -EINVAL
);
333 r
= prioq_ensure_allocated(&enumerator
->devices
, device_compare
);
337 r
= prioq_put(enumerator
->devices
, device
, NULL
);
341 sd_device_ref(device
);
346 static bool match_sysattr_value(sd_device
*device
, const char *sysattr
, const char *match_value
) {
353 r
= sd_device_get_sysattr_value(device
, sysattr
, &value
);
360 if (fnmatch(match_value
, value
, 0) == 0)
366 static bool match_sysattr(sd_device_enumerator
*enumerator
, sd_device
*device
) {
374 HASHMAP_FOREACH_KEY(value
, sysattr
, enumerator
->nomatch_sysattr
, i
)
375 if (match_sysattr_value(device
, sysattr
, value
))
378 HASHMAP_FOREACH_KEY(value
, sysattr
, enumerator
->match_sysattr
, i
)
379 if (!match_sysattr_value(device
, sysattr
, value
))
385 static bool match_property(sd_device_enumerator
*enumerator
, sd_device
*device
) {
386 const char *property
;
393 if (hashmap_isempty(enumerator
->match_property
))
396 HASHMAP_FOREACH_KEY(value
, property
, enumerator
->match_property
, i
) {
397 const char *property_dev
, *value_dev
;
399 FOREACH_DEVICE_PROPERTY(device
, property_dev
, value_dev
) {
400 if (fnmatch(property
, property_dev
, 0) != 0)
403 if (!value
&& !value_dev
)
406 if (!value
|| !value_dev
)
409 if (fnmatch(value
, value_dev
, 0) == 0)
417 static bool match_tag(sd_device_enumerator
*enumerator
, sd_device
*device
) {
424 SET_FOREACH(tag
, enumerator
->match_tag
, i
)
425 if (!sd_device_has_tag(device
, tag
))
431 static bool match_parent(sd_device_enumerator
*enumerator
, sd_device
*device
) {
432 const char *devpath
, *devpath_dev
;
438 if (!enumerator
->match_parent
)
441 r
= sd_device_get_devpath(enumerator
->match_parent
, &devpath
);
444 r
= sd_device_get_devpath(device
, &devpath_dev
);
447 return startswith(devpath_dev
, devpath
);
450 static bool match_sysname(sd_device_enumerator
*enumerator
, const char *sysname
) {
451 const char *sysname_match
;
457 if (set_isempty(enumerator
->match_sysname
))
460 SET_FOREACH(sysname_match
, enumerator
->match_sysname
, i
)
461 if (fnmatch(sysname_match
, sysname
, 0) == 0)
467 static int enumerator_scan_dir_and_add_devices(sd_device_enumerator
*enumerator
, const char *basedir
, const char *subdir1
, const char *subdir2
) {
468 _cleanup_closedir_
DIR *dir
= NULL
;
476 path
= strjoina("/sys/", basedir
, "/");
479 path
= strjoina(path
, subdir1
, "/");
482 path
= strjoina(path
, subdir2
, "/");
488 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
489 _cleanup_device_unref_ sd_device
*device
= NULL
;
490 char syspath
[strlen(path
) + 1 + strlen(dent
->d_name
) + 1];
492 int ifindex
, initialized
, k
;
494 if (dent
->d_name
[0] == '.')
497 if (!match_sysname(enumerator
, dent
->d_name
))
500 (void)sprintf(syspath
, "%s%s", path
, dent
->d_name
);
502 k
= sd_device_new_from_syspath(&device
, syspath
);
505 /* this is necessarily racey, so ignore missing devices */
511 k
= sd_device_get_devnum(device
, &devnum
);
517 k
= sd_device_get_ifindex(device
, &ifindex
);
523 k
= sd_device_get_is_initialized(device
, &initialized
);
530 * All devices with a device node or network interfaces
531 * possibly need udev to adjust the device node permission
532 * or context, or rename the interface before it can be
533 * reliably used from other processes.
535 * For now, we can only check these types of devices, we
536 * might not store a database, and have no way to find out
537 * for all other types of devices.
539 if (!enumerator
->match_allow_uninitialized
&&
541 (major(devnum
) > 0 || ifindex
> 0))
544 if (!match_parent(enumerator
, device
))
547 if (!match_tag(enumerator
, device
))
550 if (!match_property(enumerator
, device
))
553 if (!match_sysattr(enumerator
, device
))
556 k
= device_enumerator_add_device(enumerator
, device
);
564 static bool match_subsystem(sd_device_enumerator
*enumerator
, const char *subsystem
) {
565 const char *subsystem_match
;
573 SET_FOREACH(subsystem_match
, enumerator
->nomatch_subsystem
, i
)
574 if (fnmatch(subsystem_match
, subsystem
, 0) == 0)
577 if (set_isempty(enumerator
->match_subsystem
))
580 SET_FOREACH(subsystem_match
, enumerator
->match_subsystem
, i
)
581 if (fnmatch(subsystem_match
, subsystem
, 0) == 0)
587 static int enumerator_scan_dir(sd_device_enumerator
*enumerator
, const char *basedir
, const char *subdir
, const char *subsystem
) {
588 _cleanup_closedir_
DIR *dir
= NULL
;
593 path
= strjoina("/sys/", basedir
);
599 log_debug(" device-enumerator: scanning %s", path
);
601 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
604 if (dent
->d_name
[0] == '.')
607 if (!match_subsystem(enumerator
, subsystem
? : dent
->d_name
))
610 k
= enumerator_scan_dir_and_add_devices(enumerator
, basedir
, dent
->d_name
, subdir
);
618 static int enumerator_scan_devices_tag(sd_device_enumerator
*enumerator
, const char *tag
) {
619 _cleanup_closedir_
DIR *dir
= NULL
;
627 path
= strjoina("/run/udev/tags/", tag
);
634 log_error("sd-device-enumerator: could not open tags directory %s: %m", path
);
639 /* TODO: filter away subsystems? */
641 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
642 _cleanup_device_unref_ sd_device
*device
= NULL
;
643 const char *subsystem
, *sysname
;
646 if (dent
->d_name
[0] == '.')
649 k
= sd_device_new_from_device_id(&device
, dent
->d_name
);
652 /* this is necessarily racy, so ignore missing devices */
658 k
= sd_device_get_subsystem(device
, &subsystem
);
664 if (!match_subsystem(enumerator
, subsystem
))
667 k
= sd_device_get_sysname(device
, &sysname
);
673 if (!match_sysname(enumerator
, sysname
))
676 if (!match_parent(enumerator
, device
))
679 if (!match_property(enumerator
, device
))
682 if (!match_sysattr(enumerator
, device
))
685 k
= device_enumerator_add_device(enumerator
, device
);
695 static int enumerator_scan_devices_tags(sd_device_enumerator
*enumerator
) {
702 SET_FOREACH(tag
, enumerator
->match_tag
, i
) {
703 r
= enumerator_scan_devices_tag(enumerator
, tag
);
711 static int parent_add_child(sd_device_enumerator
*enumerator
, const char *path
) {
712 _cleanup_device_unref_ sd_device
*device
= NULL
;
713 const char *subsystem
, *sysname
;
716 r
= sd_device_new_from_syspath(&device
, path
);
718 /* this is necessarily racy, so ignore missing devices */
723 r
= sd_device_get_subsystem(device
, &subsystem
);
729 if (!match_subsystem(enumerator
, subsystem
))
732 r
= sd_device_get_sysname(device
, &sysname
);
736 if (!match_sysname(enumerator
, sysname
))
739 if (!match_property(enumerator
, device
))
742 if (!match_sysattr(enumerator
, device
))
745 r
= device_enumerator_add_device(enumerator
, device
);
752 static int parent_crawl_children(sd_device_enumerator
*enumerator
, const char *path
, unsigned maxdepth
) {
753 _cleanup_closedir_
DIR *dir
= NULL
;
759 log_debug("sd-device-enumerate: could not open parent directory %s: %m", path
);
763 FOREACH_DIRENT_ALL(dent
, dir
, return -errno
) {
764 _cleanup_free_
char *child
= NULL
;
767 if (dent
->d_name
[0] == '.')
770 if (dent
->d_type
!= DT_DIR
)
773 child
= strjoin(path
, "/", dent
->d_name
, NULL
);
777 k
= parent_add_child(enumerator
, child
);
782 parent_crawl_children(enumerator
, child
, maxdepth
- 1);
784 log_debug("device-enumerate: max depth reached, %s: ignoring devices", child
);
790 static int enumerator_scan_devices_children(sd_device_enumerator
*enumerator
) {
794 r
= sd_device_get_syspath(enumerator
->match_parent
, &path
);
798 k
= parent_add_child(enumerator
, path
);
802 k
= parent_crawl_children(enumerator
, path
, DEVICE_ENUMERATE_MAX_DEPTH
);
809 static int enumerator_scan_devices_all(sd_device_enumerator
*enumerator
) {
812 log_debug("device-enumerator: scan all dirs");
814 if (access("/sys/subsystem", F_OK
) >= 0) {
815 /* we have /subsystem/, forget all the old stuff */
816 r
= enumerator_scan_dir(enumerator
, "subsystem", "devices", NULL
);
818 return log_debug_errno(r
, "device-enumerator: failed to scan /sys/subsystem: %m");
822 k
= enumerator_scan_dir(enumerator
, "bus", "devices", NULL
);
824 log_debug_errno(k
, "device-enumerator: failed to scan /sys/bus: %m");
828 k
= enumerator_scan_dir(enumerator
, "class", NULL
, NULL
);
830 log_debug_errno(k
, "device-enumerator: failed to scan /sys/class: %m");
838 int device_enumerator_scan_devices(sd_device_enumerator
*enumerator
) {
844 if (enumerator
->scan_uptodate
&&
845 enumerator
->type
== DEVICE_ENUMERATION_TYPE_DEVICES
)
848 while ((device
= prioq_pop(enumerator
->devices
)))
849 sd_device_unref(device
);
851 if (!set_isempty(enumerator
->match_tag
)) {
852 r
= enumerator_scan_devices_tags(enumerator
);
855 } else if (enumerator
->match_parent
) {
856 r
= enumerator_scan_devices_children(enumerator
);
860 r
= enumerator_scan_devices_all(enumerator
);
865 enumerator
->scan_uptodate
= true;
870 _public_ sd_device
*sd_device_enumerator_get_device_first(sd_device_enumerator
*enumerator
) {
873 assert_return(enumerator
, NULL
);
875 r
= device_enumerator_scan_devices(enumerator
);
879 enumerator
->type
= DEVICE_ENUMERATION_TYPE_DEVICES
;
881 return prioq_peek(enumerator
->devices
);
884 _public_ sd_device
*sd_device_enumerator_get_device_next(sd_device_enumerator
*enumerator
) {
885 assert_return(enumerator
, NULL
);
887 if (!enumerator
->scan_uptodate
||
888 enumerator
->type
!= DEVICE_ENUMERATION_TYPE_DEVICES
)
891 sd_device_unref(prioq_pop(enumerator
->devices
));
893 return prioq_peek(enumerator
->devices
);
896 int device_enumerator_scan_subsystems(sd_device_enumerator
*enumerator
) {
898 const char *subsysdir
;
903 if (enumerator
->scan_uptodate
&&
904 enumerator
->type
== DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
)
907 while ((device
= prioq_pop(enumerator
->devices
)))
908 sd_device_unref(device
);
911 if (match_subsystem(enumerator
, "module")) {
912 k
= enumerator_scan_dir_and_add_devices(enumerator
, "module", NULL
, NULL
);
914 log_debug_errno(k
, "device-enumerator: failed to scan modules: %m");
919 if (access("/sys/subsystem", F_OK
) >= 0)
920 subsysdir
= "subsystem";
924 /* subsystems (only buses support coldplug) */
925 if (match_subsystem(enumerator
, "subsystem")) {
926 k
= enumerator_scan_dir_and_add_devices(enumerator
, subsysdir
, NULL
, NULL
);
928 log_debug_errno(k
, "device-enumerator: failed to scan subsystems: %m");
933 /* subsystem drivers */
934 if (match_subsystem(enumerator
, "drivers")) {
935 k
= enumerator_scan_dir(enumerator
, subsysdir
, "drivers", "drivers");
937 log_debug_errno(k
, "device-enumerator: failed to scan drivers: %m");
942 enumerator
->scan_uptodate
= true;
947 _public_ sd_device
*sd_device_enumerator_get_subsystem_first(sd_device_enumerator
*enumerator
) {
950 assert_return(enumerator
, NULL
);
952 r
= device_enumerator_scan_subsystems(enumerator
);
956 enumerator
->type
= DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
;
958 return prioq_peek(enumerator
->devices
);
961 _public_ sd_device
*sd_device_enumerator_get_subsystem_next(sd_device_enumerator
*enumerator
) {
962 assert_return(enumerator
, NULL
);
964 if (enumerator
->scan_uptodate
||
965 enumerator
->type
!= DEVICE_ENUMERATION_TYPE_SUBSYSTEMS
)
968 sd_device_unref(prioq_pop(enumerator
->devices
));
970 return prioq_peek(enumerator
->devices
);
973 sd_device
*device_enumerator_get_first(sd_device_enumerator
*enumerator
) {
974 assert_return(enumerator
, NULL
);
976 return prioq_peek(enumerator
->devices
);
979 sd_device
*device_enumerator_get_next(sd_device_enumerator
*enumerator
) {
980 assert_return(enumerator
, NULL
);
982 sd_device_unref(prioq_pop(enumerator
->devices
));
984 return prioq_peek(enumerator
->devices
);