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];
42 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
43 util_strlcat(path, basedir, sizeof(path));
44 util_strlcat(path, "/", sizeof(path));
45 util_strlcat(path, subsystem, sizeof(path));
47 util_strlcat(path, subdir, sizeof(path));
51 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
52 char syspath[UTIL_PATH_SIZE];
54 if (dent->d_name[0] == '.')
56 util_strlcpy(syspath, path, sizeof(syspath));
57 util_strlcat(syspath, "/", sizeof(syspath));
58 util_strlcat(syspath, dent->d_name, sizeof(syspath));
59 util_resolve_sys_link(udev, syspath, sizeof(syspath));
60 util_name_list_add(udev, device_list, syspath, NULL, 1);
66 static int devices_scan_subsystems(struct udev *udev,
67 const char *basedir, const char *subsystem, const char *subdir,
68 struct list_head *device_list)
70 char path[UTIL_PATH_SIZE];
74 if (subsystem != NULL)
75 return devices_scan_subsystem(udev, basedir, subsystem, subdir, device_list);
77 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
78 util_strlcat(path, basedir, sizeof(path));
82 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
83 if (dent->d_name[0] == '.')
85 devices_scan_subsystem(udev, basedir, dent->d_name, subdir, device_list);
91 static int devices_delay(struct udev *udev, const char *syspath)
93 static const char *delay_device_list[] = {
101 len = strlen(udev_get_sys_path(udev));
103 for (i = 0; delay_device_list[i] != NULL; i++) {
104 if (strstr(&syspath[len], delay_device_list[i]) != NULL) {
105 info(udev, "delaying: %s\n", syspath);
112 static int devices_call(struct udev *udev, const char *syspath,
113 int (*cb)(struct udev *udev,
114 const char *syspath, const char *subsystem, const char *name,
119 char subsystem[UTIL_PATH_SIZE];
122 name = strrchr(syspath, '/');
127 if (util_get_sys_subsystem(udev, syspath, subsystem, sizeof(subsystem)) < 2)
129 *cb_rc = cb(udev, syspath, subsystem, name, data);
134 * udev_enumerate_devices:
135 * @udev_device: udev device
136 * @cb: function to be called for every property found
137 * @data: data to be passed to the function
139 * Retrieve the property key/value pairs belonging to the
140 * udev device. For every key/value pair, the passed function will be
141 * called. If the function returns 1, remaning properties will be
144 * Returns: the number of properties passed to the caller, or a negative value on error
146 int udev_enumerate_devices(struct udev *udev, const char *subsystem,
147 int (*cb)(struct udev *udev,
148 const char *syspath, const char *subsystem, const char *name, void *data),
151 char base[UTIL_PATH_SIZE];
153 struct list_head device_list;
154 struct util_name_entry *loop_device;
155 struct util_name_entry *tmp_device;
159 INIT_LIST_HEAD(&device_list);
161 /* if we have /sys/subsystem/, forget all the old stuff */
162 util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
163 util_strlcat(base, "/subsystem", sizeof(base));
164 if (stat(base, &statbuf) == 0) {
165 devices_scan_subsystems(udev, "/subsystem", subsystem, "/devices", &device_list);
167 devices_scan_subsystems(udev, "/bus", subsystem, "/devices", &device_list);
168 devices_scan_subsystems(udev, "/class", subsystem, NULL, &device_list);
171 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
172 if (devices_delay(udev, loop_device->name))
175 if (devices_call(udev, loop_device->name, cb, data, &cb_rc) == 0)
177 list_del(&loop_device->node);
178 free(loop_device->name);
182 /* handle remaining delayed devices */
183 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
185 if (devices_call(udev, loop_device->name, cb, data, &cb_rc) == 0)
187 list_del(&loop_device->node);
188 free(loop_device->name);