diff options
author | Kay Sievers <kay.sievers@vrfy.org> | 2008-09-30 13:42:26 +0200 |
---|---|---|
committer | Kay Sievers <kay.sievers@vrfy.org> | 2008-09-30 13:42:26 +0200 |
commit | c97f839eed652c3e05eb71bc2ba5fbe40678e7b7 (patch) | |
tree | 3e12c6ec853720c92ddb470460dfa3cf1892776c /udev/lib | |
parent | 4aa0b15efbb44dbcbb0a4fa6e7bb45e70e1ec537 (diff) |
libudev: enumerate - scan devices and subsystems, add subsystem and attribute filter
Diffstat (limited to 'udev/lib')
-rw-r--r-- | udev/lib/exported_symbols | 9 | ||||
-rw-r--r-- | udev/lib/libudev-device.c | 12 | ||||
-rw-r--r-- | udev/lib/libudev-enumerate.c | 322 | ||||
-rw-r--r-- | udev/lib/libudev.h | 7 | ||||
-rw-r--r-- | udev/lib/test-libudev.c | 14 |
5 files changed, 240 insertions, 124 deletions
diff --git a/udev/lib/exported_symbols b/udev/lib/exported_symbols index 21b433e9f5..edd3c76f3d 100644 --- a/udev/lib/exported_symbols +++ b/udev/lib/exported_symbols @@ -32,12 +32,17 @@ udev_device_get_devnum udev_device_get_seqnum udev_device_get_attr_value udev_enumerate_new -udev_enumerate_scan_devices -udev_enumerate_scan_subsystems udev_enumerate_ref udev_enumerate_unref udev_enumerate_get_udev udev_enumerate_get_list_entry +udev_enumerate_add_match_subsystem +udev_enumerate_add_nomatch_subsystem +udev_enumerate_add_match_attr +udev_enumerate_add_nomatch_attr +udev_enumerate_scan_devices +udev_enumerate_scan_subsystems +udev_enumerate_add_device udev_monitor_new_from_socket udev_monitor_new_from_netlink udev_monitor_enable_receiving diff --git a/udev/lib/libudev-device.c b/udev/lib/libudev-device.c index 4cdc957093..f5465e9892 100644 --- a/udev/lib/libudev-device.c +++ b/udev/lib/libudev-device.c @@ -318,21 +318,19 @@ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, de /* fallback to search sys devices for the major/minor */ if (type == 'b') - udev_enumerate_scan_devices(udev_enumerate, "block", NULL); + udev_enumerate_add_match_subsystem(udev_enumerate, "block"); else if (type == 'c') - udev_enumerate_scan_devices(udev_enumerate, "!block", NULL); + udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block"); + udev_enumerate_scan_devices(udev_enumerate); udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) { struct udev_device *device_loop; device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry)); if (device_loop != NULL) { if (udev_device_get_devnum(device_loop) == devnum) { - const char *subsystem; - - subsystem = udev_device_get_subsystem(device_loop); - if (type == 'b' && strcmp(subsystem, "block") != 0) + if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0) continue; - if (type == 'c' && strcmp(subsystem, "block") == 0) + if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0) continue; device = device_loop; break; diff --git a/udev/lib/libudev-enumerate.c b/udev/lib/libudev-enumerate.c index 266164ae51..0377649cfd 100644 --- a/udev/lib/libudev-enumerate.c +++ b/udev/lib/libudev-enumerate.c @@ -24,6 +24,7 @@ #include <errno.h> #include <string.h> #include <dirent.h> +#include <fnmatch.h> #include <sys/stat.h> #include "libudev.h" @@ -32,9 +33,37 @@ struct udev_enumerate { struct udev *udev; int refcount; + struct list_node attr_match_list; + struct list_node attr_nomatch_list; + struct list_node subsystem_match_list; + struct list_node subsystem_nomatch_list; struct list_node devices_list; }; +/** + * udev_enumerate_new: + * @udev: udev library context + * + * Returns: an enumeration context + **/ +struct udev_enumerate *udev_enumerate_new(struct udev *udev) +{ + struct udev_enumerate *udev_enumerate; + + udev_enumerate = malloc(sizeof(struct udev_enumerate)); + if (udev_enumerate == NULL) + return NULL; + memset(udev_enumerate, 0x00, (sizeof(struct udev_enumerate))); + udev_enumerate->refcount = 1; + udev_enumerate->udev = udev; + list_init(&udev_enumerate->devices_list); + list_init(&udev_enumerate->attr_match_list); + list_init(&udev_enumerate->attr_nomatch_list); + list_init(&udev_enumerate->subsystem_match_list); + list_init(&udev_enumerate->subsystem_nomatch_list); + return udev_enumerate; +} + struct udev_enumerate *udev_enumerate_ref(struct udev_enumerate *udev_enumerate) { if (udev_enumerate == NULL) @@ -51,6 +80,10 @@ void udev_enumerate_unref(struct udev_enumerate *udev_enumerate) if (udev_enumerate->refcount > 0) return; list_cleanup(udev_enumerate->udev, &udev_enumerate->devices_list); + list_cleanup(udev_enumerate->udev, &udev_enumerate->attr_match_list); + list_cleanup(udev_enumerate->udev, &udev_enumerate->attr_nomatch_list); + list_cleanup(udev_enumerate->udev, &udev_enumerate->subsystem_match_list); + list_cleanup(udev_enumerate->udev, &udev_enumerate->subsystem_nomatch_list); free(udev_enumerate); } @@ -68,22 +101,123 @@ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *ude return list_get_entry(&udev_enumerate->devices_list); } -static int devices_scan_subsystem(struct udev *udev, - const char *basedir, const char *subsystem, const char *subdir, - struct list_node *devices_list) +int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem) { + if (udev_enumerate == NULL) + return -EINVAL; + if (subsystem == NULL) + return 0; + if (list_entry_add(udev_enumerate_get_udev(udev_enumerate), + &udev_enumerate->subsystem_match_list, subsystem, NULL, 1, 0) == NULL) + return -ENOMEM; + return 0; +} + +int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem) +{ + if (udev_enumerate == NULL) + return -EINVAL; + if (subsystem == NULL) + return 0; + if (list_entry_add(udev_enumerate_get_udev(udev_enumerate), + &udev_enumerate->subsystem_nomatch_list, subsystem, NULL, 1, 0) == NULL) + return -ENOMEM; + return 0; +} + +int udev_enumerate_add_match_attr(struct udev_enumerate *udev_enumerate, const char *attr, const char *value) +{ + if (udev_enumerate == NULL) + return -EINVAL; + if (attr == NULL) + return 0; + if (list_entry_add(udev_enumerate_get_udev(udev_enumerate), + &udev_enumerate->attr_match_list, attr, value, 1, 0) == NULL) + return -ENOMEM; + return 0; +} + +int udev_enumerate_add_nomatch_attr(struct udev_enumerate *udev_enumerate, const char *attr, const char *value) +{ + if (udev_enumerate == NULL) + return -EINVAL; + if (attr == NULL) + return 0; + if (list_entry_add(udev_enumerate_get_udev(udev_enumerate), + &udev_enumerate->attr_nomatch_list, attr, value, 1, 0) == NULL) + return -ENOMEM; + return 0; +} + +static int match_attr_value(struct udev *udev, const char *syspath, const char *attr, const char *match_val) +{ + struct udev_device *device; + const char *val = NULL; + int match = 0; + + device = udev_device_new_from_syspath(udev, syspath); + if (device == NULL) + return -EINVAL; + val = udev_device_get_attr_value(device, attr); + if (val == NULL) + goto exit; + if (match_val == NULL) { + match = 1; + goto exit; + } + if (fnmatch(match_val, val, 0) == 0) { + match = 1; + goto exit; + } +exit: + udev_device_unref(device); + return match; +} + +static int match_attr(struct udev_enumerate *udev_enumerate, const char *syspath) +{ + struct udev *udev = udev_enumerate_get_udev(udev_enumerate); + struct udev_list_entry *list_entry; + + /* skip list */ + udev_list_entry_foreach(list_entry, list_get_entry(&udev_enumerate->attr_nomatch_list)) { + if (match_attr_value(udev, syspath, + udev_list_entry_get_name(list_entry), + udev_list_entry_get_value(list_entry))) + return 0; + } + /* include list */ + if (list_get_entry(&udev_enumerate->attr_match_list) != NULL) { + udev_list_entry_foreach(list_entry, list_get_entry(&udev_enumerate->attr_match_list)) { + /* anything that does not match, will make it FALSE */ + if (!match_attr_value(udev, syspath, + udev_list_entry_get_name(list_entry), + udev_list_entry_get_value(list_entry))) + return 0; + } + return 1; + } + return 1; +} + +static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate, const char *basedir, const char *subdir1, const char *subdir2) +{ + struct udev *udev = udev_enumerate_get_udev(udev_enumerate); char path[UTIL_PATH_SIZE]; DIR *dir; struct dirent *dent; util_strlcpy(path, udev_get_sys_path(udev), sizeof(path)); + util_strlcat(path, "/", sizeof(path)); util_strlcat(path, basedir, sizeof(path)); - if (subsystem != NULL) { + if (subdir1 != NULL) { util_strlcat(path, "/", sizeof(path)); - util_strlcat(path, subsystem, sizeof(path)); + util_strlcat(path, subdir1, sizeof(path)); + } + if (subdir2 != NULL) { + util_strlcat(path, "/", sizeof(path)); + util_strlcat(path, subdir2, sizeof(path)); } - if (subdir != NULL) - util_strlcat(path, subdir, sizeof(path)); dir = opendir(path); if (dir == NULL) return -1; @@ -102,47 +236,54 @@ static int devices_scan_subsystem(struct udev *udev, if (stat(filename, &statbuf) != 0) continue; util_resolve_sys_link(udev, syspath, sizeof(syspath)); - list_entry_add(udev, devices_list, syspath, NULL, 1, 1); + if (!match_attr(udev_enumerate, syspath)) + continue; + list_entry_add(udev, &udev_enumerate->devices_list, syspath, NULL, 1, 1); } closedir(dir); return 0; } -static int devices_scan_subsystems(struct udev *udev, - 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) +static int match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem) { - if (subsystem_include_list != NULL) { - struct udev_list_entry *list_entry; - - /* if list of subsystems to scan is given, just use this list */ - udev_list_entry_foreach(list_entry, subsystem_include_list) { - if (udev_list_entry_get_by_name(subsystem_exclude_list, udev_list_entry_get_name(list_entry)) != NULL) - continue; - 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); + struct udev_list_entry *list_entry; + + udev_list_entry_foreach(list_entry, list_get_entry(&udev_enumerate->subsystem_nomatch_list)) { + if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0) + return 0; + } + if (list_get_entry(&udev_enumerate->subsystem_match_list) != NULL) { + udev_list_entry_foreach(list_entry, list_get_entry(&udev_enumerate->subsystem_match_list)) { + if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0) + return 1; } - closedir(dir); + return 0; } + return 1; +} + +static int scan_dir(struct udev_enumerate *udev_enumerate, const char *basedir, const char *subdir, const char *match) +{ + struct udev *udev = udev_enumerate_get_udev(udev_enumerate); + + char path[UTIL_PATH_SIZE]; + DIR *dir; + struct dirent *dent; + + util_strlcpy(path, udev_get_sys_path(udev), sizeof(path)); + util_strlcat(path, "/", 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 (!match_subsystem(udev_enumerate, match != NULL ? match : dent->d_name)) + continue; + scan_dir_and_add_devices(udev_enumerate, basedir, dent->d_name, subdir); + } + closedir(dir); return 0; } @@ -157,7 +298,6 @@ static int devices_delay(struct udev *udev, const char *syspath) int i; len = strlen(udev_get_sys_path(udev)); - for (i = 0; delay_device_list[i] != NULL; i++) { if (strstr(&syspath[len], delay_device_list[i]) != NULL) { info(udev, "delaying: %s\n", syspath); @@ -167,103 +307,66 @@ static int devices_delay(struct udev *udev, const char *syspath) return 0; } -/** - * udev_enumerate_new: - * @udev: udev library context - * - * Returns: an enumeration context - **/ -struct udev_enumerate *udev_enumerate_new(struct udev *udev) +int udev_enumerate_add_device(struct udev_enumerate *udev_enumerate, struct udev_device *udev_device) { - struct udev_enumerate *udev_enumerate; + struct udev *udev = udev_enumerate_get_udev(udev_enumerate); + struct udev_list_entry *list_entry; - udev_enumerate = malloc(sizeof(struct udev_enumerate)); if (udev_enumerate == NULL) - return NULL; - memset(udev_enumerate, 0x00, (sizeof(struct udev_enumerate))); - udev_enumerate->refcount = 1; - udev_enumerate->udev = udev; - list_init(&udev_enumerate->devices_list); - return udev_enumerate; + return -EINVAL; + if (udev_device == NULL) + return 0; + list_entry_add(udev, + &udev_enumerate->devices_list, + udev_device_get_syspath(udev_device), NULL, 1, 1); + /* 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))) + list_entry_move_to_end(list_entry); + } + return 0; } /** * udev_enumerate_scan_devices: * @udev_enumerate: udev enumeration context - * @subsystem: the list of names of subsystems to look for devices * - * Returns: 0 on success. + * Returns: a negative value on error. **/ -int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate, const char *subsystem, ...) +int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate) { struct udev *udev = udev_enumerate_get_udev(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_enumerate == NULL) return -EINVAL; - - 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); - util_strlcpy(base, udev_get_sys_path(udev), sizeof(base)); util_strlcat(base, "/subsystem", sizeof(base)); if (stat(base, &statbuf) == 0) { /* we have /subsystem/, forget all the old stuff */ info(udev, "searching '/subsystem/*/devices/*' dir\n"); - devices_scan_subsystems(udev, "/subsystem", "/devices", - list_get_entry(&subsystem_include_list), - list_get_entry(&subsystem_exclude_list), - &udev_enumerate->devices_list); + scan_dir(udev_enumerate, "subsystem", "devices", NULL); } else { info(udev, "searching '/bus/*/devices/*' dir\n"); - devices_scan_subsystems(udev, "/bus", "/devices", - list_get_entry(&subsystem_include_list), - list_get_entry(&subsystem_exclude_list), - &udev_enumerate->devices_list); + scan_dir(udev_enumerate, "bus", "devices", NULL); info(udev, "searching '/class/*' dir\n"); - devices_scan_subsystems(udev, "/class", NULL, - list_get_entry(&subsystem_include_list), - list_get_entry(&subsystem_exclude_list), - &udev_enumerate->devices_list); + scan_dir(udev_enumerate, "class", NULL, NULL); /* if block isn't a class, scan /block/ */ util_strlcpy(base, udev_get_sys_path(udev), sizeof(base)); util_strlcat(base, "/class/block", sizeof(base)); if (stat(base, &statbuf) != 0) { - struct udev_list_entry *include_list = list_get_entry(&subsystem_include_list); - struct udev_list_entry *exclude_list = list_get_entry(&subsystem_exclude_list); - int include_block = (include_list == NULL || udev_list_entry_get_by_name(include_list, "block") != NULL); - int exclude_block = (udev_list_entry_get_by_name(exclude_list, "block") != NULL); - - if (include_block && !exclude_block) { + if (match_subsystem(udev_enumerate, "block")) { info(udev, "searching '/block/*' dir\n"); /* scan disks */ - devices_scan_subsystem(udev, "/block", NULL, NULL, &udev_enumerate->devices_list); + scan_dir_and_add_devices(udev_enumerate, "block", NULL, NULL); /* scan partitions */ info(udev, "searching '/block/*/*' dir\n"); - devices_scan_subsystems(udev, "/block", NULL, - NULL, NULL, - &udev_enumerate->devices_list); + scan_dir(udev_enumerate, "block", NULL, "block"); } } } - - 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))) @@ -276,7 +379,7 @@ int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate, const cha * udev_enumerate_scan_subsystems: * @udev_enumerate: udev enumeration context * - * Returns: 0 on success. + * Returns: a negative value on error. **/ int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate) { @@ -287,18 +390,17 @@ int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate) if (udev_enumerate == NULL) return -EINVAL; - util_strlcpy(base, udev_get_sys_path(udev), sizeof(base)); util_strlcat(base, "/subsystem", sizeof(base)); if (stat(base, &statbuf) == 0) - subsysdir = "/subsystem"; + subsysdir = "subsystem"; else - subsysdir = "/bus"; - info(udev, "searching '%s/*' dir\n", subsysdir); - devices_scan_subsystem(udev, subsysdir, NULL, NULL, &udev_enumerate->devices_list); + subsysdir = "bus"; + if (match_subsystem(udev_enumerate, "subsystem")) { + info(udev, "searching '%s/*' dir\n", subsysdir); + scan_dir_and_add_devices(udev_enumerate, subsysdir, NULL, NULL); + } info(udev, "searching '%s/*/drivers/*' dir\n", subsysdir); - devices_scan_subsystems(udev, subsysdir, "/drivers", - NULL, NULL, - &udev_enumerate->devices_list); + scan_dir(udev_enumerate, subsysdir, "drivers", "drivers"); return 0; } diff --git a/udev/lib/libudev.h b/udev/lib/libudev.h index 427fed7f61..49d24f1476 100644 --- a/udev/lib/libudev.h +++ b/udev/lib/libudev.h @@ -98,7 +98,12 @@ extern struct udev_enumerate *udev_enumerate_new(struct udev *udev); extern struct udev_enumerate *udev_enumerate_ref(struct udev_enumerate *udev_enumerate); extern struct udev *udev_enumerate_get_udev(struct udev_enumerate *udev_enumerate); extern void udev_enumerate_unref(struct udev_enumerate *udev_enumerate); -extern int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate, const char *subsystem, ...); +extern int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem); +extern int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem); +extern int udev_enumerate_add_match_attr(struct udev_enumerate *udev_enumerate, const char *attr, const char *value); +extern int udev_enumerate_add_nomatch_attr(struct udev_enumerate *udev_enumerate, const char *attr, const char *value); +extern int udev_enumerate_add_device(struct udev_enumerate *udev_enumerate, struct udev_device *udev_device); +extern int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate); extern int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate); extern struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate); diff --git a/udev/lib/test-libudev.c b/udev/lib/test-libudev.c index 0f45413d50..5e9897c715 100644 --- a/udev/lib/test-libudev.c +++ b/udev/lib/test-libudev.c @@ -309,7 +309,8 @@ int main(int argc, char *argv[], char *envp[]) udev_enumerate = udev_enumerate_new(udev); if (udev_enumerate == NULL) return -1; - udev_enumerate_scan_devices(udev_enumerate, subsystem, NULL); + udev_enumerate_add_match_subsystem(udev_enumerate, subsystem); + udev_enumerate_scan_devices(udev_enumerate); test_enumerate_print_list(udev_enumerate); udev_enumerate_unref(udev_enumerate); @@ -317,7 +318,8 @@ int main(int argc, char *argv[], char *envp[]) udev_enumerate = udev_enumerate_new(udev); if (udev_enumerate == NULL) return -1; - udev_enumerate_scan_devices(udev_enumerate, "block", NULL); + udev_enumerate_add_match_subsystem(udev_enumerate,"block"); + udev_enumerate_scan_devices(udev_enumerate); test_enumerate_print_list(udev_enumerate); udev_enumerate_unref(udev_enumerate); @@ -325,7 +327,8 @@ int main(int argc, char *argv[], char *envp[]) udev_enumerate = udev_enumerate_new(udev); if (udev_enumerate == NULL) return -1; - udev_enumerate_scan_devices(udev_enumerate, "!block", NULL); + udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block"); + udev_enumerate_scan_devices(udev_enumerate); test_enumerate_print_list(udev_enumerate); udev_enumerate_unref(udev_enumerate); @@ -333,7 +336,10 @@ int main(int argc, char *argv[], char *envp[]) udev_enumerate = udev_enumerate_new(udev); if (udev_enumerate == NULL) return -1; - udev_enumerate_scan_devices(udev_enumerate, "pci", "mem", "vc", NULL); + udev_enumerate_add_match_subsystem(udev_enumerate, "pci"); + udev_enumerate_add_match_subsystem(udev_enumerate, "mem"); + udev_enumerate_add_match_subsystem(udev_enumerate, "vc"); + udev_enumerate_scan_devices(udev_enumerate); test_enumerate_print_list(udev_enumerate); udev_enumerate_unref(udev_enumerate); |