2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include "libudev-private.h"
32 static int devices_scan_subsystem(struct udev *udev,
33 const char *basedir, const char *subsystem, const char *subdir,
34 struct list_head *device_list)
36 char path[UTIL_PATH_SIZE];
40 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
41 util_strlcat(path, basedir, sizeof(path));
42 util_strlcat(path, "/", sizeof(path));
43 util_strlcat(path, subsystem, sizeof(path));
45 util_strlcat(path, subdir, sizeof(path));
49 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
50 char syspath[UTIL_PATH_SIZE];
52 if (dent->d_name[0] == '.')
54 util_strlcpy(syspath, path, sizeof(syspath));
55 util_strlcat(syspath, "/", sizeof(syspath));
56 util_strlcat(syspath, dent->d_name, sizeof(syspath));
57 util_resolve_sys_link(udev, syspath, sizeof(syspath));
58 util_name_list_add(udev, device_list, syspath, NULL, 1);
64 static int devices_scan_subsystems(struct udev *udev,
65 const char *basedir, const char *subsystem, const char *subdir,
66 struct list_head *device_list)
68 char path[UTIL_PATH_SIZE];
72 if (subsystem != NULL)
73 return devices_scan_subsystem(udev, basedir, subsystem, subdir, device_list);
75 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
76 util_strlcat(path, basedir, sizeof(path));
80 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
81 if (dent->d_name[0] == '.')
83 devices_scan_subsystem(udev, basedir, dent->d_name, subdir, device_list);
89 static int devices_delay(struct udev *udev, const char *syspath)
91 static const char *delay_device_list[] = {
99 len = strlen(udev_get_sys_path(udev));
101 for (i = 0; delay_device_list[i] != NULL; i++) {
102 if (strstr(&syspath[len], delay_device_list[i]) != NULL) {
103 info(udev, "delaying: %s\n", syspath);
111 * udev_enumerate_devices:
112 * @udev: udev library context
113 * @subsystem: the subsystem to enumerate
114 * @cb: function to be called for every device found
115 * @data: data to be passed to the function
117 * Returns: the number of devices passed to the caller, or a negative value on error
119 int udev_enumerate_devices(struct udev *udev, const char *subsystem,
120 int (*cb)(struct udev_device *udev_device, void *data),
123 char base[UTIL_PATH_SIZE];
125 struct list_head device_list;
126 struct util_name_entry *loop_device;
127 struct util_name_entry *tmp_device;
131 INIT_LIST_HEAD(&device_list);
133 /* if we have /sys/subsystem/, forget all the old stuff */
134 util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
135 util_strlcat(base, "/subsystem", sizeof(base));
136 if (stat(base, &statbuf) == 0) {
137 devices_scan_subsystems(udev, "/subsystem", subsystem, "/devices", &device_list);
139 devices_scan_subsystems(udev, "/bus", subsystem, "/devices", &device_list);
140 devices_scan_subsystems(udev, "/class", subsystem, NULL, &device_list);
143 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
144 if (devices_delay(udev, loop_device->name))
147 struct udev_device *device;
149 device = udev_device_new_from_syspath(udev, loop_device->name);
150 if (device != NULL) {
151 cb_rc = cb(device, data);
153 udev_device_unref(device);
156 list_del(&loop_device->node);
157 free(loop_device->name);
161 /* handle remaining delayed devices */
162 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
164 struct udev_device *device;
166 device = udev_device_new_from_syspath(udev, loop_device->name);
167 if (device != NULL) {
168 cb_rc = cb(device, data);
170 udev_device_unref(device);
173 list_del(&loop_device->node);
174 free(loop_device->name);