free(udev_enumerate);
}
+struct udev *udev_enumerate_get_udev(struct udev_enumerate *udev_enumerate)
+{
+ if (udev_enumerate == NULL)
+ return NULL;
+ return udev_enumerate->udev;
+}
+
struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate)
{
if (udev_enumerate == NULL)
static int devices_scan_subsystem(struct udev *udev,
const char *basedir, const char *subsystem, const char *subdir,
- struct list_node *device_list)
+ struct list_node *devices_list)
{
char path[UTIL_PATH_SIZE];
DIR *dir;
util_strlcat(syspath, "/", sizeof(syspath));
util_strlcat(syspath, dent->d_name, sizeof(syspath));
util_resolve_sys_link(udev, syspath, sizeof(syspath));
- list_entry_add(udev, device_list, syspath, NULL, 1, 1);
+ list_entry_add(udev, devices_list, syspath, NULL, 1, 1);
}
closedir(dir);
return 0;
}
static int devices_scan_subsystems(struct udev *udev,
- const char *basedir, const char *subsystem, const char *subdir,
- struct list_node *device_list)
+ const char *basedir, const char *subdir,
+ struct udev_list_entry *subsystem_include_list,
+ struct udev_list_entry *subsystem_exclude_list,
+ struct list_node *devices_list)
{
- char path[UTIL_PATH_SIZE];
- DIR *dir;
- struct dirent *dent;
-
- if (subsystem != NULL)
- return devices_scan_subsystem(udev, basedir, subsystem, subdir, device_list);
+ if (subsystem_include_list != NULL) {
+ struct udev_list_entry *list_entry;
- util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
- util_strlcat(path, basedir, sizeof(path));
- dir = opendir(path);
- if (dir == NULL)
- return -1;
- for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
- if (dent->d_name[0] == '.')
- continue;
- devices_scan_subsystem(udev, basedir, dent->d_name, subdir, device_list);
+ /* if list of subsystems to scan is given, just use this list */
+ udev_list_entry_foreach(list_entry, subsystem_include_list)
+ devices_scan_subsystem(udev, basedir, udev_list_entry_get_name(list_entry), subdir, devices_list);
+ } else {
+ char path[UTIL_PATH_SIZE];
+ DIR *dir;
+ struct dirent *dent;
+
+ /* if no list of subsystems to scan is given, scan all, and possible exclude some subsystems */
+ util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
+ util_strlcat(path, basedir, sizeof(path));
+ dir = opendir(path);
+ if (dir == NULL)
+ return -1;
+ for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
+ if (dent->d_name[0] == '.')
+ continue;
+ if (udev_list_entry_get_by_name(subsystem_exclude_list, dent->d_name) != NULL)
+ continue;
+ devices_scan_subsystem(udev, basedir, dent->d_name, subdir, devices_list);
+ }
+ closedir(dir);
}
- closedir(dir);
return 0;
}
}
/**
- * udev_enumerate_new_from_subsystems:
+ * udev_enumerate_new_from_devices:
* @udev: udev library context
- * @subsystem: the subsystem to enumerate
+ * @subsystem: the list of names of subsystems to look for devices
*
* Returns: an enumeration context
**/
-struct udev_enumerate *udev_enumerate_new_from_subsystems(struct udev *udev, const char *subsystem)
+struct udev_enumerate *udev_enumerate_new_from_devices(struct udev *udev, const char *subsystem, ...)
{
struct udev_enumerate *udev_enumerate;
+ va_list vargs;
+ const char *arg;
char base[UTIL_PATH_SIZE];
struct stat statbuf;
+ struct list_node subsystem_include_list;
+ struct list_node subsystem_exclude_list;
struct udev_list_entry *list_entry;
if (udev == NULL)
udev_enumerate->udev = udev;
list_init(&udev_enumerate->devices_list);
+ va_start(vargs, subsystem);
+ list_init(&subsystem_include_list);
+ list_init(&subsystem_exclude_list);
+ for (arg = subsystem; arg != NULL; arg = va_arg(vargs, const char *)) {
+ if (arg[0] != '!')
+ list_entry_add(udev, &subsystem_include_list, arg, NULL, 1, 0);
+ else
+ list_entry_add(udev, &subsystem_exclude_list, &arg[1], NULL, 1, 0);
+ }
+ va_end(vargs);
+
/* if we have /sys/subsystem/, forget all the old stuff */
util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
util_strlcat(base, "/subsystem", sizeof(base));
if (stat(base, &statbuf) == 0) {
info(udev, "searching 'subsystem/*/devices/*' dir\n");
- devices_scan_subsystems(udev, "/subsystem", subsystem, "/devices", &udev_enumerate->devices_list);
+ devices_scan_subsystems(udev, "/subsystem", "/devices",
+ list_get_entry(&subsystem_include_list),
+ list_get_entry(&subsystem_exclude_list),
+ &udev_enumerate->devices_list);
} else {
info(udev, "searching 'bus/*/devices/*' dir\n");
- devices_scan_subsystems(udev, "/bus", subsystem, "/devices", &udev_enumerate->devices_list);
+ devices_scan_subsystems(udev, "/bus", "/devices",
+ list_get_entry(&subsystem_include_list),
+ list_get_entry(&subsystem_exclude_list),
+ &udev_enumerate->devices_list);
info(udev, "searching 'class/*' dir\n");
- devices_scan_subsystems(udev, "/class", subsystem, NULL, &udev_enumerate->devices_list);
+ devices_scan_subsystems(udev, "/class", NULL,
+ list_get_entry(&subsystem_include_list),
+ list_get_entry(&subsystem_exclude_list),
+ &udev_enumerate->devices_list);
}
+ list_cleanup(udev, &subsystem_include_list);
+ list_cleanup(udev, &subsystem_exclude_list);
+
/* sort delayed devices to the end of the list */
udev_list_entry_foreach(list_entry, list_get_entry(&udev_enumerate->devices_list)) {
if (devices_delay(udev, udev_list_entry_get_name(list_entry)))
}
return udev_enumerate;
}
+
+struct udev_enumerate *udev_enumerate_new_from_subsystems(struct udev *udev)
+{
+ return NULL;
+}
return 0;
}
-static int test_enumerate(struct udev *udev, const char *subsystem)
+static int test_enumerate_print_list(struct udev_enumerate *enumerate)
{
- struct udev_enumerate *enumerate;
struct udev_list_entry *list_entry;
int count = 0;
- enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
- if (enumerate == NULL)
- return -1;
- list_entry = udev_enumerate_get_list_entry(enumerate);
- while (list_entry != NULL) {
+ udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
struct udev_device *device;
- device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
+ device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
+ udev_list_entry_get_name(list_entry));
if (device != NULL) {
printf("device: '%s' (%s) '%s'\n",
udev_device_get_syspath(device),
udev_device_unref(device);
count++;
}
- list_entry = udev_list_entry_get_next(list_entry);
}
- udev_enumerate_unref(enumerate);
printf("found %i devices\n\n", count);
return count;
}
{ "version", 0, NULL, 'V' },
{}
};
+ struct udev_enumerate *enumerate;
const char *syspath = "/devices/virtual/mem/null";
const char *subsystem = NULL;
const char *socket = "@/org/kernel/udev/monitor";
test_device(udev, syspath);
test_device_devnum(udev);
test_device_parents(udev, syspath);
- test_enumerate(udev, subsystem);
+
+ printf("enumerate '%s'\n", subsystem == NULL ? "<all>" : subsystem);
+ enumerate = udev_enumerate_new_from_devices(udev, subsystem, NULL);
+ if (enumerate == NULL)
+ return -1;
+ test_enumerate_print_list(enumerate);
+ udev_enumerate_unref(enumerate);
+
+ printf("enumerate 'block'\n");
+ enumerate = udev_enumerate_new_from_devices(udev, "block", NULL);
+ if (enumerate == NULL)
+ return -1;
+ test_enumerate_print_list(enumerate);
+ udev_enumerate_unref(enumerate);
+
+ printf("enumerate '!block'\n");
+ enumerate = udev_enumerate_new_from_devices(udev, "!block", NULL);
+ if (enumerate == NULL)
+ return -1;
+ test_enumerate_print_list(enumerate);
+ udev_enumerate_unref(enumerate);
+
+ printf("enumerate 'pci, mem, vc'\n");
+ enumerate = udev_enumerate_new_from_devices(udev, "pci", "mem", "vc", NULL);
+ if (enumerate == NULL)
+ return -1;
+ test_enumerate_print_list(enumerate);
+ udev_enumerate_unref(enumerate);
+
test_monitor(udev, socket);
out:
udev_unref(udev);