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/>.
32 #include "libudev-private.h"
34 static int devices_scan_subsystem(struct udev *udev,
35 const char *basedir, const char *subsystem, const char *subdir,
36 struct list_head *device_list)
38 char path[UTIL_PATH_SIZE];
43 len = util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
44 util_strlcat(path, basedir, sizeof(path));
45 util_strlcat(path, "/", sizeof(path));
46 util_strlcat(path, subsystem, sizeof(path));
48 util_strlcat(path, subdir, sizeof(path));
52 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
53 char devpath[UTIL_PATH_SIZE];
55 if (dent->d_name[0] == '.')
57 util_strlcpy(devpath, &path[len], sizeof(devpath));
58 util_strlcat(devpath, "/", sizeof(devpath));
59 util_strlcat(devpath, dent->d_name, sizeof(devpath));
60 util_resolve_sys_link(udev, devpath, sizeof(devpath));
61 util_name_list_add(udev, device_list, devpath, NULL, 1);
67 static int devices_scan_subsystems(struct udev *udev,
68 const char *basedir, const char *subsystem, const char *subdir,
69 struct list_head *device_list)
71 char path[UTIL_PATH_SIZE];
75 if (subsystem != NULL)
76 return devices_scan_subsystem(udev, basedir, subsystem, subdir, device_list);
78 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
79 util_strlcat(path, basedir, sizeof(path));
83 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
84 if (dent->d_name[0] == '.')
86 devices_scan_subsystem(udev, basedir, dent->d_name, subdir, device_list);
92 static int devices_delay(struct udev *udev, const char *devpath)
94 static const char *delay_device_list[] = {
101 for (i = 0; delay_device_list[i] != NULL; i++) {
102 if (strstr(devpath, delay_device_list[i]) != NULL) {
103 info(udev, "delaying: %s\n", devpath);
110 static int devices_call(struct udev *udev, const char *devpath,
111 int (*cb)(struct udev *udev,
112 const char *devpath, const char *subsystem, const char *name,
117 char subsystem[UTIL_PATH_SIZE];
120 name = strrchr(devpath, '/');
125 if (util_get_sys_subsystem(udev, devpath, subsystem, sizeof(subsystem)) < 2)
127 *cb_rc = cb(udev, devpath, subsystem, name, data);
132 * udev_enumerate_devices:
133 * @udev_device: udev device
134 * @cb: function to be called for every property found
135 * @data: data to be passed to the function
137 * Retrieve the property key/value pairs belonging to the
138 * udev device. For every key/value pair, the passed function will be
139 * called. If the function returns 1, remaning properties will be
142 * Returns: the number of properties passed to the caller, or a negative value on error
144 int udev_enumerate_devices(struct udev *udev, const char *subsystem,
145 int (*cb)(struct udev *udev,
146 const char *devpath, const char *subsystem, const char *name, void *data),
149 char base[UTIL_PATH_SIZE];
151 struct list_head device_list;
152 struct util_name_entry *loop_device;
153 struct util_name_entry *tmp_device;
157 INIT_LIST_HEAD(&device_list);
159 /* if we have /sys/subsystem/, forget all the old stuff */
160 util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
161 util_strlcat(base, "/subsystem", sizeof(base));
162 if (stat(base, &statbuf) == 0) {
163 devices_scan_subsystems(udev, "/subsystem", subsystem, "/devices", &device_list);
165 devices_scan_subsystems(udev, "/bus", subsystem, "/devices", &device_list);
166 devices_scan_subsystems(udev, "/class", subsystem, NULL, &device_list);
169 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
170 if (devices_delay(udev, loop_device->name))
173 if (devices_call(udev, loop_device->name, cb, data, &cb_rc) == 0)
175 list_del(&loop_device->node);
176 free(loop_device->name);
180 /* handle remaining delayed devices */
181 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
183 if (devices_call(udev, loop_device->name, cb, data, &cb_rc) == 0)
185 list_del(&loop_device->node);
186 free(loop_device->name);