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"
35 static int devices_scan_subsystem(struct udev *udev,
36 const char *basedir, const char *subsystem, const char *subdir,
37 struct list_head *device_list)
44 len = strlcpy(path, udev_get_sys_path(udev), sizeof(path));
45 strlcat(path, basedir, sizeof(path));
46 strlcat(path, "/", sizeof(path));
47 strlcat(path, subsystem, sizeof(path));
49 strlcat(path, subdir, sizeof(path));
53 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
54 char devpath[PATH_SIZE];
56 if (dent->d_name[0] == '.')
58 strlcpy(devpath, &path[len], sizeof(devpath));
59 strlcat(devpath, "/", sizeof(devpath));
60 strlcat(devpath, dent->d_name, sizeof(devpath));
61 sysfs_resolve_link(devpath, sizeof(devpath));
62 name_list_add(device_list, devpath, 1);
68 static int devices_scan_subsystems(struct udev *udev,
69 const char *basedir, const char *subsystem, const char *subdir,
70 struct list_head *device_list)
76 if (subsystem != NULL)
77 return devices_scan_subsystem(udev, basedir, subsystem, subdir, device_list);
79 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
80 strlcat(path, basedir, sizeof(path));
84 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
85 if (dent->d_name[0] == '.')
87 devices_scan_subsystem(udev, basedir, dent->d_name, subdir, device_list);
93 static int devices_delay(struct udev *udev, const char *devpath)
95 static const char *delay_device_list[] = {
102 for (i = 0; delay_device_list[i] != NULL; i++) {
103 if (strstr(devpath, delay_device_list[i]) != NULL) {
104 log_info(udev, "delaying: %s\n", devpath);
111 static int devices_call(struct udev *udev, const char *devpath,
112 int (*cb)(struct udev *udev,
113 const char *devpath, const char *subsystem, const char *name,
118 char subsystem[NAME_SIZE];
121 name = strrchr(devpath, '/');
126 if (util_get_sys_subsystem(udev, devpath, subsystem, sizeof(subsystem)) < 2)
128 *cb_rc = cb(udev, devpath, subsystem, name, data);
133 * udev_devices_enumerate:
134 * @udev_device: udev device
135 * @cb: function to be called for every property found
136 * @data: data to be passed to the function
138 * Retrieve the property key/value pairs belonging to the
139 * udev device. For every key/value pair, the passed function will be
140 * called. If the function returns 1, remaning properties will be
143 * Returns: the number of properties passed to the caller, or a negative value on error
145 int udev_devices_enumerate(struct udev *udev, const char *subsystem,
146 int (*cb)(struct udev *udev,
147 const char *devpath, const char *subsystem, const char *name, void *data),
150 char base[PATH_SIZE];
152 struct list_head device_list;
153 struct name_entry *loop_device;
154 struct name_entry *tmp_device;
158 INIT_LIST_HEAD(&device_list);
160 /* if we have /sys/subsystem/, forget all the old stuff */
161 strlcpy(base, sysfs_path, sizeof(base));
162 strlcat(base, "/subsystem", sizeof(base));
163 if (stat(base, &statbuf) == 0) {
164 devices_scan_subsystems(udev, "/subsystem", subsystem, "/devices", &device_list);
166 devices_scan_subsystems(udev, "/bus", subsystem, "/devices", &device_list);
167 devices_scan_subsystems(udev, "/class", subsystem, NULL, &device_list);
170 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
171 if (devices_delay(udev, loop_device->name))
174 if (devices_call(udev, loop_device->name, cb, data, &cb_rc) == 0)
176 list_del(&loop_device->node);
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);