diff options
Diffstat (limited to 'udev/lib/libudev-enumerate.c')
-rw-r--r-- | udev/lib/libudev-enumerate.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/udev/lib/libudev-enumerate.c b/udev/lib/libudev-enumerate.c index 618ffbc3e9..abc89d5cab 100644 --- a/udev/lib/libudev-enumerate.c +++ b/udev/lib/libudev-enumerate.c @@ -39,6 +39,7 @@ struct udev_enumerate { struct udev_list_node sysattr_nomatch_list; struct udev_list_node subsystem_match_list; struct udev_list_node subsystem_nomatch_list; + struct udev_list_node properties_match_list; struct udev_list_node devices_list; int devices_sorted; }; @@ -63,6 +64,7 @@ struct udev_enumerate *udev_enumerate_new(struct udev *udev) udev_list_init(&udev_enumerate->sysattr_nomatch_list); udev_list_init(&udev_enumerate->subsystem_match_list); udev_list_init(&udev_enumerate->subsystem_nomatch_list); + udev_list_init(&udev_enumerate->properties_match_list); return udev_enumerate; } @@ -86,6 +88,7 @@ void udev_enumerate_unref(struct udev_enumerate *udev_enumerate) udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->sysattr_nomatch_list); udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->subsystem_match_list); udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->subsystem_nomatch_list); + udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->properties_match_list); free(udev_enumerate); } @@ -178,6 +181,18 @@ exit: return match; } +int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value) +{ + if (udev_enumerate == NULL) + return -EINVAL; + if (property == NULL) + return 0; + if (udev_list_entry_add(udev_enumerate_get_udev(udev_enumerate), + &udev_enumerate->properties_match_list, property, value, 1, 0) == NULL) + return -ENOMEM; + return 0; +} + static int match_sysattr(struct udev_enumerate *udev_enumerate, const char *syspath) { struct udev *udev = udev_enumerate_get_udev(udev_enumerate); @@ -204,6 +219,51 @@ static int match_sysattr(struct udev_enumerate *udev_enumerate, const char *sysp return 1; } +static int match_property(struct udev_enumerate *udev_enumerate, const char *syspath) +{ + struct udev_device *dev; + struct udev_list_entry *list_entry; + int match = 0; + + /* no match always matches */ + if (udev_list_get_entry(&udev_enumerate->properties_match_list) == NULL) + return 1; + + /* no device does not match */ + dev = udev_device_new_from_syspath(udev_enumerate->udev, syspath); + if (dev == NULL) + return 0; + + /* loop over matches */ + udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->properties_match_list)) { + struct udev_list_entry *property_entry; + + /* loop over device properties */ + udev_list_entry_foreach(property_entry, udev_device_get_properties_list_entry(dev)) { + if (fnmatch(udev_list_entry_get_name(list_entry), udev_list_entry_get_name(property_entry), 0) == 0) { + const char *match_value; + const char *dev_value; + + match_value = udev_list_entry_get_value(list_entry); + dev_value = udev_list_entry_get_value(property_entry); + if (match_value == NULL && dev_value == NULL) { + match = 1; + goto out; + } + if (match_value == NULL || dev_value == NULL) + continue; + if (fnmatch(match_value, dev_value, 0) == 0) { + match = 1; + goto out; + } + } + } + } +out: + udev_device_unref(dev); + return match; +} + static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate, const char *basedir, const char *subdir1, const char *subdir2) { @@ -248,6 +308,8 @@ static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate, continue; if (!match_sysattr(udev_enumerate, syspath)) continue; + if (!match_property(udev_enumerate, syspath)) + continue; udev_list_entry_add(udev, &udev_enumerate->devices_list, syspath, NULL, 1, 1); } closedir(dir); |