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);
140 assert_return(_value, -EINVAL);
143 hashmap = &enumerator->match_sysattr;
145 hashmap = &enumerator->nomatch_sysattr;
147 r = hashmap_ensure_allocated(hashmap, NULL);
151 sysattr = strdup(_sysattr);
155 value = strdup(_value);
159 r = hashmap_put(*hashmap, sysattr, value);
166 enumerator->scan_uptodate = false;
171 _public_ int sd_device_enumerator_add_match_property(sd_device_enumerator *enumerator, const char *_property, const char *_value) {
172 _cleanup_free_ char *property = NULL, *value = NULL;
175 assert_return(enumerator, -EINVAL);
176 assert_return(_property, -EINVAL);
177 assert_return(_value, -EINVAL);
179 r = hashmap_ensure_allocated(&enumerator->match_property, NULL);
183 property = strdup(_property);
187 value = strdup(_value);
191 r = hashmap_put(enumerator->match_property, property, value);
198 enumerator->scan_uptodate = false;
203 _public_ int sd_device_enumerator_add_match_sysname(sd_device_enumerator *enumerator, const char *sysname) {
206 assert_return(enumerator, -EINVAL);
207 assert_return(sysname, -EINVAL);
209 r = set_ensure_allocated(&enumerator->match_sysname, NULL);
213 r = set_put_strdup(enumerator->match_sysname, sysname);
217 enumerator->scan_uptodate = false;
222 _public_ int sd_device_enumerator_add_match_tag(sd_device_enumerator *enumerator, const char *tag) {
225 assert_return(enumerator, -EINVAL);
226 assert_return(tag, -EINVAL);
228 r = set_ensure_allocated(&enumerator->match_tag, NULL);
232 r = set_put_strdup(enumerator->match_tag, tag);
236 enumerator->scan_uptodate = false;
241 _public_ int sd_device_enumerator_add_match_parent(sd_device_enumerator *enumerator, sd_device *parent) {
242 assert_return(enumerator, -EINVAL);
243 assert_return(parent, -EINVAL);
245 sd_device_unref(enumerator->match_parent);
246 enumerator->match_parent = sd_device_ref(parent);
248 enumerator->scan_uptodate = false;
253 _public_ int sd_device_enumerator_allow_uninitialized(sd_device_enumerator *enumerator) {
254 assert_return(enumerator, -EINVAL);
256 enumerator->match_allow_uninitialized = true;
258 enumerator->scan_uptodate = false;
263 int device_enumerator_add_match_is_initialized(sd_device_enumerator *enumerator) {
264 assert_return(enumerator, -EINVAL);
266 enumerator->match_allow_uninitialized = false;
268 enumerator->scan_uptodate = false;
273 static int device_compare(const void *_a, const void *_b) {
274 sd_device *a = (sd_device *)_a, *b = (sd_device *)_b;
275 const char *devpath_a, *devpath_b, *sound_a;
276 bool delay_a = false, delay_b = false;
278 assert_se(sd_device_get_devpath(a, &devpath_a) >= 0);
279 assert_se(sd_device_get_devpath(b, &devpath_b) >= 0);
281 sound_a = strstr(devpath_a, "/sound/card");
283 /* For sound cards the control device must be enumerated last to
284 * make sure it's the final device node that gets ACLs applied.
285 * Applications rely on this fact and use ACL changes on the
286 * control node as an indicator that the ACL change of the
287 * entire sound card completed. The kernel makes this guarantee
288 * when creating those devices, and hence we should too when
289 * enumerating them. */
290 sound_a += strlen("/sound/card");
291 sound_a = strchr(sound_a, '/');
296 prefix_len = sound_a - devpath_a;
298 if (strncmp(devpath_a, devpath_b, prefix_len) == 0) {
301 sound_b = devpath_b + prefix_len;
303 if (startswith(sound_a, "/controlC") &&
304 !startswith(sound_b, "/contolC"))
307 if (!startswith(sound_a, "/controlC") &&
308 startswith(sound_b, "/controlC"))
314 /* md and dm devices are enumerated after all other devices */
315 if (strstr(devpath_a, "/block/md") || strstr(devpath_a, "/block/dm-"))
318 if (strstr(devpath_b, "/block/md") || strstr(devpath_b, "/block/dm-"))
321 if (delay_a && !delay_b)
324 if (!delay_a && delay_b)
327 return strcmp(devpath_a, devpath_b);
330 int device_enumerator_add_device(sd_device_enumerator *enumerator, sd_device *device) {
333 assert_return(enumerator, -EINVAL);
334 assert_return(device, -EINVAL);
336 r = prioq_ensure_allocated(&enumerator->devices, device_compare);
340 r = prioq_put(enumerator->devices, device, NULL);
344 sd_device_ref(device);
349 static bool match_sysattr_value(sd_device *device, const char *sysattr, const char *match_value) {
356 r = sd_device_get_sysattr_value(device, sysattr, &value);
363 if (fnmatch(match_value, value, 0) == 0)
369 static bool match_sysattr(sd_device_enumerator *enumerator, sd_device *device) {
377 HASHMAP_FOREACH_KEY(sysattr, value, enumerator->nomatch_sysattr, i)
378 if (match_sysattr_value(device, sysattr, value))
381 HASHMAP_FOREACH_KEY(sysattr, value, enumerator->match_sysattr, i)
382 if (!match_sysattr_value(device, sysattr, value))
388 static bool match_property(sd_device_enumerator *enumerator, sd_device *device) {
389 const char *property;
396 if (hashmap_isempty(enumerator->match_property))
399 HASHMAP_FOREACH_KEY(property, value, enumerator->match_property, i) {
400 const char *property_dev, *value_dev;
402 FOREACH_DEVICE_PROPERTY(device, property_dev, value_dev) {
403 if (fnmatch(property, property_dev, 0) != 0)
406 if (!value && !value_dev)
409 if (!value || !value_dev)
412 if (fnmatch(value, value_dev, 0) == 0)
420 static bool match_tag(sd_device_enumerator *enumerator, sd_device *device) {
427 SET_FOREACH(tag, enumerator->match_tag, i)
428 if (!sd_device_has_tag(device, tag))
434 static bool match_parent(sd_device_enumerator *enumerator, sd_device *device) {
435 const char *devpath, *devpath_dev;
441 if (!enumerator->match_parent)
444 r = sd_device_get_devpath(enumerator->match_parent, &devpath);
447 r = sd_device_get_devpath(device, &devpath_dev);
450 return startswith(devpath_dev, devpath);
453 static bool match_sysname(sd_device_enumerator *enumerator, const char *sysname) {
454 const char *sysname_match;
460 if (set_isempty(enumerator->match_sysname))
463 SET_FOREACH(sysname_match, enumerator->match_sysname, i)
464 if (fnmatch(sysname_match, sysname, 0) == 0)
470 static int enumerator_scan_dir_and_add_devices(sd_device_enumerator *enumerator, const char *basedir, const char *subdir1, const char *subdir2) {
471 _cleanup_closedir_ DIR *dir = NULL;
479 path = strjoina("/sys/", basedir, "/");
482 path = strjoina(path, subdir1, "/");
485 path = strjoina(path, subdir2, "/");
491 FOREACH_DIRENT_ALL(dent, dir, return -errno) {
492 _cleanup_device_unref_ sd_device *device = NULL;
493 char syspath[strlen(path) + 1 + strlen(dent->d_name) + 1];
495 int ifindex, initialized, k;
497 if (dent->d_name[0] == '.')
500 if (!match_sysname(enumerator, dent->d_name))
503 (void)sprintf(syspath, "%s%s", path, dent->d_name);
505 k = sd_device_new_from_syspath(&device, syspath);
507 log_debug_errno(k, "device-enumerator: failed to create device from syspath %s: %m", syspath);
512 k = sd_device_get_devnum(device, &devnum);
518 k = sd_device_get_ifindex(device, &ifindex);
524 k = sd_device_get_is_initialized(device, &initialized);
531 * All devices with a device node or network interfaces
532 * possibly need udev to adjust the device node permission
533 * or context, or rename the interface before it can be
534 * reliably used from other processes.
536 * For now, we can only check these types of devices, we
537 * might not store a database, and have no way to find out
538 * for all other types of devices.
540 if (!enumerator->match_allow_uninitialized &&
542 (major(devnum) > 0 || ifindex > 0))
545 if (!match_parent(enumerator, device))
548 if (!match_tag(enumerator, device))
551 if (!match_property(enumerator, device))
554 if (!match_sysattr(enumerator, device))
557 k = device_enumerator_add_device(enumerator, device);
565 static bool match_subsystem(sd_device_enumerator *enumerator, const char *subsystem) {
566 const char *subsystem_match;
574 SET_FOREACH(subsystem_match, enumerator->nomatch_subsystem, i)
575 if (fnmatch(subsystem_match, subsystem, 0) == 0)
578 if (set_isempty(enumerator->match_subsystem))
581 SET_FOREACH(subsystem_match, enumerator->match_subsystem, i)
582 if (fnmatch(subsystem_match, subsystem, 0) == 0)
588 static int enumerator_scan_dir(sd_device_enumerator *enumerator, const char *basedir, const char *subdir, const char *subsystem) {
589 _cleanup_closedir_ DIR *dir = NULL;
594 path = strjoina("/sys/", basedir);
600 log_debug(" device-enumerator: scanning %s", path);
602 FOREACH_DIRENT_ALL(dent, dir, return -errno) {
605 if (dent->d_name[0] == '.')
608 if (!match_subsystem(enumerator, subsystem ? : dent->d_name))
611 k = enumerator_scan_dir_and_add_devices(enumerator, basedir, dent->d_name, subdir);
619 static int enumerator_scan_devices_tag(sd_device_enumerator *enumerator, const char *tag) {
620 _cleanup_closedir_ DIR *dir = NULL;
628 path = strjoina("/run/udev/tags/", tag);
635 log_error("sd-device-enumerator: could not open tags directory %s: %m", path);
640 /* TODO: filter away subsystems? */
642 FOREACH_DIRENT_ALL(dent, dir, return -errno) {
643 _cleanup_device_unref_ sd_device *device = NULL;
644 const char *subsystem, *sysname;
647 if (dent->d_name[0] == '.')
650 k = sd_device_new_from_device_id(&device, dent->d_name);
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);
720 r = sd_device_get_subsystem(device, &subsystem);
724 if (!match_subsystem(enumerator, subsystem))
727 r = sd_device_get_sysname(device, &sysname);
731 if (!match_sysname(enumerator, sysname))
734 if (!match_property(enumerator, device))
737 if (!match_sysattr(enumerator, device))
740 r = device_enumerator_add_device(enumerator, device);
747 static int parent_crawl_children(sd_device_enumerator *enumerator, const char *path, unsigned maxdepth) {
748 _cleanup_closedir_ DIR *dir = NULL;
754 log_debug("sd-device-enumerate: could not open parent directory %s: %m", path);
758 FOREACH_DIRENT_ALL(dent, dir, return -errno) {
759 _cleanup_free_ char *child = NULL;
762 if (dent->d_name[0] == '.')
765 if (dent->d_type != DT_DIR)
768 k = asprintf(&child, "%s/%s", path, dent->d_name);
772 k = parent_add_child(enumerator, child);
777 parent_crawl_children(enumerator, child, maxdepth - 1);
779 log_debug("device-enumerate: max depth reached, %s: ignoring devices", child);
785 static int enumerator_scan_devices_children(sd_device_enumerator *enumerator) {
789 r = sd_device_get_syspath(enumerator->match_parent, &path);
793 k = parent_add_child(enumerator, path);
797 k = parent_crawl_children(enumerator, path, DEVICE_ENUMERATE_MAX_DEPTH);
804 static int enumerator_scan_devices_all(sd_device_enumerator *enumerator) {
807 log_debug("device-enumerator: scan all dirs");
809 if (access("/sys/subsystem", F_OK) >= 0) {
810 /* we have /subsystem/, forget all the old stuff */
811 r = enumerator_scan_dir(enumerator, "subsystem", "devices", NULL);
813 log_debug("device-enumerator: failed to scan /sys/subsystem: %s", strerror(-r));
819 k = enumerator_scan_dir(enumerator, "bus", "devices", NULL);
821 log_debug_errno(k, "device-enumerator: failed to scan /sys/bus: %m");
825 k = enumerator_scan_dir(enumerator, "class", NULL, NULL);
827 log_debug_errno(k, "device-enumerator: failed to scan /sys/class: %m");
835 int device_enumerator_scan_devices(sd_device_enumerator *enumerator) {
841 if (enumerator->scan_uptodate &&
842 enumerator->type == DEVICE_ENUMERATION_TYPE_DEVICES)
845 while ((device = prioq_pop(enumerator->devices)))
846 sd_device_unref(device);
848 if (!set_isempty(enumerator->match_tag)) {
849 r = enumerator_scan_devices_tags(enumerator);
852 } else if (enumerator->match_parent) {
853 r = enumerator_scan_devices_children(enumerator);
857 r = enumerator_scan_devices_all(enumerator);
862 enumerator->scan_uptodate = true;
867 _public_ sd_device *sd_device_enumerator_get_device_first(sd_device_enumerator *enumerator) {
870 assert_return(enumerator, NULL);
872 r = device_enumerator_scan_devices(enumerator);
876 enumerator->type = DEVICE_ENUMERATION_TYPE_DEVICES;
878 return prioq_peek(enumerator->devices);
881 _public_ sd_device *sd_device_enumerator_get_device_next(sd_device_enumerator *enumerator) {
882 assert_return(enumerator, NULL);
884 if (!enumerator->scan_uptodate ||
885 enumerator->type != DEVICE_ENUMERATION_TYPE_DEVICES)
888 sd_device_unref(prioq_pop(enumerator->devices));
890 return prioq_peek(enumerator->devices);
893 int device_enumerator_scan_subsystems(sd_device_enumerator *enumerator) {
895 const char *subsysdir;
900 if (enumerator->scan_uptodate &&
901 enumerator->type == DEVICE_ENUMERATION_TYPE_SUBSYSTEMS)
904 while ((device = prioq_pop(enumerator->devices)))
905 sd_device_unref(device);
908 if (match_subsystem(enumerator, "module")) {
909 k = enumerator_scan_dir_and_add_devices(enumerator, "module", NULL, NULL);
911 log_debug_errno(k, "device-enumerator: failed to scan modules: %m");
916 if (access("/sys/subsystem", F_OK) >= 0)
917 subsysdir = "subsystem";
921 /* subsystems (only buses support coldplug) */
922 if (match_subsystem(enumerator, "subsystem")) {
923 k = enumerator_scan_dir_and_add_devices(enumerator, subsysdir, NULL, NULL);
925 log_debug_errno(k, "device-enumerator: failed to scan subsystems: %m");
930 /* subsystem drivers */
931 if (match_subsystem(enumerator, "drivers")) {
932 k = enumerator_scan_dir(enumerator, subsysdir, "drivers", "drivers");
934 log_debug_errno(k, "device-enumerator: failed to scan drivers: %m");
939 enumerator->scan_uptodate = true;
944 _public_ sd_device *sd_device_enumerator_get_subsystem_first(sd_device_enumerator *enumerator) {
947 assert_return(enumerator, NULL);
949 r = device_enumerator_scan_subsystems(enumerator);
953 enumerator->type = DEVICE_ENUMERATION_TYPE_SUBSYSTEMS;
955 return prioq_peek(enumerator->devices);
958 _public_ sd_device *sd_device_enumerator_get_subsystem_next(sd_device_enumerator *enumerator) {
959 assert_return(enumerator, NULL);
961 if (enumerator->scan_uptodate ||
962 enumerator->type != DEVICE_ENUMERATION_TYPE_SUBSYSTEMS)
965 sd_device_unref(prioq_pop(enumerator->devices));
967 return prioq_peek(enumerator->devices);
970 sd_device *device_enumerator_get_first(sd_device_enumerator *enumerator) {
971 assert_return(enumerator, NULL);
973 return prioq_peek(enumerator->devices);
976 sd_device *device_enumerator_get_next(sd_device_enumerator *enumerator) {
977 assert_return(enumerator, NULL);
979 sd_device_unref(prioq_pop(enumerator->devices));
981 return prioq_peek(enumerator->devices);