# ------------------------------------------------------------------------------
# libudev
# ------------------------------------------------------------------------------
-LIBUDEV_CURRENT=7
+LIBUDEV_CURRENT=8
LIBUDEV_REVISION=0
-LIBUDEV_AGE=7
+LIBUDEV_AGE=8
SUBDIRS += libudev/docs
udev_monitor_get_fd
udev_monitor_receive_device
udev_monitor_filter_add_match_subsystem_devtype
+udev_monitor_filter_add_match_tag
udev_monitor_filter_update
udev_monitor_filter_remove
</SECTION>
udev_enumerate_add_match_sysattr
udev_enumerate_add_nomatch_sysattr
udev_enumerate_add_match_property
+udev_enumerate_add_match_tag
udev_enumerate_add_match_sysname
udev_enumerate_add_syspath
udev_enumerate_scan_devices
udev_monitor_get_fd
udev_monitor_receive_device
udev_monitor_filter_add_match_subsystem_devtype
+udev_monitor_filter_add_match_tag
udev_monitor_filter_update
udev_monitor_filter_remove
udev_queue_new
/*
* libudev - interface to udev device information
*
- * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
#include <stdio.h>
#include <string.h>
#include <stddef.h>
+#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "libudev.h"
#include "libudev-private.h"
+static int udev_device_tag_index(struct udev_device *udev_device, bool add)
+{
+ struct udev *udev = udev_device_get_udev(udev_device);
+ struct udev_list_entry *list_entry;
+
+ udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device)) {
+ char filename[UTIL_PATH_SIZE];
+
+ util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/tags/",
+ udev_list_entry_get_name(list_entry), "/",
+ udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
+
+ if (add) {
+ util_create_path(udev, filename);
+ symlink(udev_device_get_devpath(udev_device), filename);
+ if (udev_device_get_sysname_old(udev_device) != NULL) {
+ char filename_old[UTIL_PATH_SIZE];
+
+ util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/tags/",
+ udev_list_entry_get_name(list_entry),
+ udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname_old(udev_device), NULL);
+ unlink(filename_old);
+ }
+ } else {
+ unlink(filename);
+ util_delete_path(udev, filename);
+ }
+ }
+ return 0;
+}
+
int udev_device_update_db(struct udev_device *udev_device)
{
struct udev *udev = udev_device_get_udev(udev_device);
udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
if (udev_list_entry_get_flags(list_entry))
goto file;
+ if (udev_device_get_tags_list_entry(udev_device) != NULL)
+ goto file;
if (udev_device_get_devlink_priority(udev_device) != 0)
goto file;
if (udev_device_get_event_timeout(udev_device) >= 0)
if (f == NULL) {
err(udev, "unable to create temporary db file '%s': %m\n", filename_tmp);
return -1;
- }
+ }
if (udev_device_get_devnode(udev_device) != NULL) {
fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
udev_list_entry_get_name(list_entry),
udev_list_entry_get_value(list_entry));
}
+ udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
+ fprintf(f, "G:%s\n", udev_list_entry_get_name(list_entry));
fclose(f);
rename(filename_tmp, filename);
info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
out:
+ udev_device_tag_index(udev_device, true);
return 0;
}
struct udev *udev = udev_device_get_udev(udev_device);
char filename[UTIL_PATH_SIZE];
+ udev_device_tag_index(udev_device, false);
util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
unlink(filename);
char filename_old[UTIL_PATH_SIZE];
char filename[UTIL_PATH_SIZE];
+ if (strcmp(udev_device_get_sysname(udev_device), udev_device_get_sysname_old(udev_device)) == 0)
+ return 0;
+
util_strscpyl(filename_old, sizeof(filename_old), udev_get_dev_path(udev), "/.udev/db/",
udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname_old(udev_device), NULL);
util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
+ udev_device_tag_index(udev_device, true);
return rename(filename_old, filename);
}
struct udev_list_node devlinks_list;
struct udev_list_node properties_list;
struct udev_list_node sysattr_list;
+ struct udev_list_node tags_list;
unsigned long long int seqnum;
int event_timeout;
int timeout;
dev_t devnum;
int watch_handle;
int maj, min;
- unsigned int parent_set:1;
- unsigned int subsystem_set:1;
- unsigned int devtype_set:1;
- unsigned int devlinks_uptodate:1;
- unsigned int envp_uptodate:1;
- unsigned int driver_set:1;
- unsigned int info_loaded:1;
+ bool parent_set;
+ bool subsystem_set;
+ bool devtype_set;
+ bool devlinks_uptodate;
+ bool envp_uptodate;
+ bool tags_uptodate;
+ bool driver_set;
+ bool info_loaded;
+ bool db_loaded;
+ bool uevent_loaded;
};
struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
{
- udev_device->envp_uptodate = 0;
+ udev_device->envp_uptodate = false;
if (value == NULL) {
struct udev_list_entry *list_entry;
}
if (slink[0] != '\0')
udev_device_add_devlink(udev_device, slink, 0);
+ } else if (strncmp(property, "TAGS=", 5) == 0) {
+ char tags[UTIL_PATH_SIZE];
+ char *next;
+
+ util_strscpy(tags, sizeof(tags), &property[5]);
+ next = strchr(tags, ':');
+ if (next != NULL) {
+ next++;
+ while (next[0] != '\0') {
+ char *tag;
+
+ tag = next;
+ next = strchr(tag, ':');
+ if (next == NULL)
+ break;
+ next[0] = '\0';
+ next++;
+ udev_device_add_tag(udev_device, tag);
+ }
+ }
} else if (strncmp(property, "DRIVER=", 7) == 0) {
udev_device_set_driver(udev_device, &property[7]);
} else if (strncmp(property, "ACTION=", 7) == 0) {
char line[UTIL_LINE_SIZE];
FILE *f;
+ if (udev_device->db_loaded)
+ return 0;
+
util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/",
udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
case 'E':
udev_device_add_property_from_string(udev_device, val);
break;
+ case 'G':
+ udev_device_add_tag(udev_device, val);
+ break;
case 'W':
udev_device_set_watch_handle(udev_device, atoi(val));
break;
fclose(f);
info(udev_device->udev, "device %p filled with db file data\n", udev_device);
+ udev_device->db_loaded = true;
return 0;
}
int maj = 0;
int min = 0;
+ if (udev_device->uevent_loaded)
+ return 0;
+
util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
f = fopen(filename, "r");
if (f == NULL)
}
udev_device->devnum = makedev(maj, min);
-
fclose(f);
+ udev_device->uevent_loaded = true;
return 0;
}
-static void device_load_info(struct udev_device *device)
-{
- device->info_loaded = 1;
- udev_device_read_uevent_file(device);
- udev_device_read_db(device);
-}
-
void udev_device_set_info_loaded(struct udev_device *device)
{
- device->info_loaded = 1;
+ device->info_loaded = true;
}
struct udev_device *udev_device_new(struct udev *udev)
udev_list_init(&udev_device->devlinks_list);
udev_list_init(&udev_device->properties_list);
udev_list_init(&udev_device->sysattr_list);
+ udev_list_init(&udev_device->tags_list);
udev_device->event_timeout = -1;
udev_device->watch_handle = -1;
/* copy global properties */
util_strscpy(path, sizeof(path), syspath);
util_resolve_sys_link(udev, path, sizeof(path));
- if (strncmp(&syspath[len], "/devices/", 9) == 0) {
+ if (strncmp(&path[len], "/devices/", 9) == 0) {
char file[UTIL_PATH_SIZE];
/* all "devices" require a "uevent" file */
if (udev_device == NULL)
return NULL;
if (!udev_device->parent_set) {
- udev_device->parent_set = 1;
+ udev_device->parent_set = true;
udev_device->parent_device = device_new_from_parent(udev_device);
}
if (udev_device->parent_device != NULL)
free(udev_device->devtype);
udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
+ udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
+ udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
free(udev_device->action);
free(udev_device->driver);
free(udev_device->devpath_old);
free(udev_device->sysname_old);
free(udev_device->knodename);
- udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
free(udev_device->envp);
free(udev_device->monitor_buf);
dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
if (udev_device == NULL)
return NULL;
if (!udev_device->info_loaded)
- device_load_info(udev_device);
+ udev_device_read_db(udev_device);
return udev_device->devnode;
}
if (udev_device == NULL)
return NULL;
if (!udev_device->subsystem_set) {
- udev_device->subsystem_set = 1;
+ udev_device->subsystem_set = true;
/* read "subsystem" link */
if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
udev_device_set_subsystem(udev_device, subsystem);
if (udev_device == NULL)
return NULL;
if (!udev_device->devtype_set) {
- udev_device->devtype_set = 1;
- if (!udev_device->info_loaded)
- udev_device_read_uevent_file(udev_device);
+ udev_device->devtype_set = true;
+ udev_device_read_uevent_file(udev_device);
}
return udev_device->devtype;
}
if (udev_device == NULL)
return NULL;
if (!udev_device->info_loaded)
- device_load_info(udev_device);
+ udev_device_read_db(udev_device);
return udev_list_get_entry(&udev_device->devlinks_list);
}
void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
{
- udev_device->devlinks_uptodate = 0;
+ udev_device->devlinks_uptodate = false;
udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
}
{
if (udev_device == NULL)
return NULL;
- if (!udev_device->info_loaded)
- device_load_info(udev_device);
+ if (!udev_device->info_loaded) {
+ udev_device_read_uevent_file(udev_device);
+ udev_device_read_db(udev_device);
+ }
if (!udev_device->devlinks_uptodate) {
char symlinks[UTIL_PATH_SIZE];
struct udev_list_entry *list_entry;
- udev_device->devlinks_uptodate = 1;
+ udev_device->devlinks_uptodate = true;
list_entry = udev_device_get_devlinks_list_entry(udev_device);
if (list_entry != NULL) {
char *s;
udev_device_add_property(udev_device, "DEVLINKS", symlinks);
}
}
+ if (!udev_device->tags_uptodate) {
+ udev_device->tags_uptodate = true;
+ if (udev_device_get_tags_list_entry(udev_device) != NULL) {
+ char tags[UTIL_PATH_SIZE];
+ struct udev_list_entry *list_entry;
+ char *s;
+ size_t l;
+
+ s = tags;
+ l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
+ udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
+ l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
+ udev_device_add_property(udev_device, "TAGS", tags);
+ }
+ }
return udev_list_get_entry(&udev_device->properties_list);
}
if (udev_device == NULL)
return NULL;
if (!udev_device->driver_set) {
- udev_device->driver_set = 1;
+ udev_device->driver_set = true;
if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
udev_device->driver = strdup(driver);
}
if (udev_device == NULL)
return makedev(0, 0);
if (!udev_device->info_loaded)
- device_load_info(udev_device);
+ udev_device_read_uevent_file(udev_device);
return udev_device->devnum;
}
udev_device->subsystem = strdup(subsystem);
if (udev_device->subsystem == NULL)
return -ENOMEM;
- udev_device->subsystem_set = 1;
+ udev_device->subsystem_set = true;
udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
return 0;
}
udev_device->devtype = strdup(devtype);
if (udev_device->devtype == NULL)
return -ENOMEM;
- udev_device->devtype_set = 1;
+ udev_device->devtype_set = true;
udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
return 0;
}
{
struct udev_list_entry *list_entry;
- udev_device->devlinks_uptodate = 0;
+ udev_device->devlinks_uptodate = false;
list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
if (list_entry == NULL)
return -ENOMEM;
return 0;
}
+int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
+{
+ if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
+ return -EINVAL;
+ udev_device->tags_uptodate = false;
+ if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, 1, 0) != NULL)
+ return 0;
+ return -ENOMEM;
+}
+
+void udev_device_cleanup_tags_list(struct udev_device *udev_device)
+{
+ udev_device->tags_uptodate = false;
+ udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
+}
+
+struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
+{
+ return udev_list_get_entry(&udev_device->tags_list);
+}
+
+int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
+{
+ struct udev_list_entry *list_entry;
+
+ if (!udev_device->info_loaded)
+ udev_device_read_db(udev_device);
+ list_entry = udev_device_get_tags_list_entry(udev_device);
+ list_entry = udev_list_entry_get_by_name(list_entry, tag);
+ if (list_entry != NULL)
+ return 1;
+ return 0;
+}
+
#define ENVP_SIZE 128
#define MONITOR_BUF_SIZE 4096
static int update_envp_monitor_buf(struct udev_device *udev_device)
}
udev_device->envp[i] = NULL;
udev_device->monitor_buf_len = s - udev_device->monitor_buf;
- udev_device->envp_uptodate = 1;
+ udev_device->envp_uptodate = true;
dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
i, udev_device->monitor_buf_len);
return 0;
udev_device->driver = strdup(driver);
if (udev_device->driver == NULL)
return -ENOMEM;
- udev_device->driver_set = 1;
+ udev_device->driver_set = true;
udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
return 0;
}
int udev_device_get_event_timeout(struct udev_device *udev_device)
{
if (!udev_device->info_loaded)
- device_load_info(udev_device);
+ udev_device_read_db(udev_device);
return udev_device->event_timeout;
}
int udev_device_get_devlink_priority(struct udev_device *udev_device)
{
if (!udev_device->info_loaded)
- device_load_info(udev_device);
+ udev_device_read_db(udev_device);
return udev_device->devlink_priority;
}
int udev_device_get_watch_handle(struct udev_device *udev_device)
{
if (!udev_device->info_loaded)
- device_load_info(udev_device);
+ udev_device_read_db(udev_device);
return udev_device->watch_handle;
}
/*
* libudev - interface to udev device information
*
- * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
struct udev_list_node subsystem_nomatch_list;
struct udev_list_node sysname_match_list;
struct udev_list_node properties_match_list;
+ struct udev_list_node tags_match_list;
struct udev_list_node devices_list;
struct syspath *devices;
unsigned int devices_cur;
udev_list_init(&udev_enumerate->subsystem_nomatch_list);
udev_list_init(&udev_enumerate->sysname_match_list);
udev_list_init(&udev_enumerate->properties_match_list);
+ udev_list_init(&udev_enumerate->tags_match_list);
udev_list_init(&udev_enumerate->devices_list);
return udev_enumerate;
}
udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->subsystem_nomatch_list);
udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->sysname_match_list);
udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->properties_match_list);
+ udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->tags_match_list);
udev_list_cleanup_entries(udev_enumerate->udev, &udev_enumerate->devices_list);
for (i = 0; i < udev_enumerate->devices_cur; i++)
free(udev_enumerate->devices[i].syspath);
}
/* For devices that should be moved to the absolute end of the list */
-static int devices_delay_end(struct udev *udev, const char *syspath)
+static bool devices_delay_end(struct udev *udev, const char *syspath)
{
static const char *delay_device_list[] = {
"/block/md",
for (i = 0; delay_device_list[i] != NULL; i++) {
if (strstr(&syspath[len], delay_device_list[i]) != NULL) {
dbg(udev, "delaying: %s\n", syspath);
- return 1;
+ return true;
}
}
- return 0;
+ return false;
}
/* For devices that should just be moved a little bit later, just
return 0;
}
-static int match_sysattr_value(struct udev *udev, const char *syspath, const char *sysattr, const char *match_val)
+static int match_sysattr_value(struct udev_device *dev, const char *sysattr, const char *match_val)
{
- struct udev_device *device;
const char *val = NULL;
bool match = false;
- device = udev_device_new_from_syspath(udev, syspath);
- if (device == NULL)
- return -EINVAL;
- val = udev_device_get_sysattr_value(device, sysattr);
+ val = udev_device_get_sysattr_value(dev, sysattr);
if (val == NULL)
goto exit;
if (match_val == NULL) {
goto exit;
}
exit:
- udev_device_unref(device);
return match;
}
return 0;
}
+/**
+ * udev_enumerate_add_match_tag:
+ * @udev_enumerate: context
+ * @tag: filter for a tag of the device to include in the list
+ *
+ * Returns: 0 on success, otherwise a negative error value.
+ */
+int udev_enumerate_add_match_tag(struct udev_enumerate *udev_enumerate, const char *tag)
+{
+ if (udev_enumerate == NULL)
+ return -EINVAL;
+ if (tag == NULL)
+ return 0;
+ if (udev_list_entry_add(udev_enumerate_get_udev(udev_enumerate),
+ &udev_enumerate->tags_match_list, tag, NULL, 1, 0) == NULL)
+ return -ENOMEM;
+ return 0;
+}
+
/**
* udev_enumerate_add_match_sysname:
* @udev_enumerate: context
return 0;
}
-static int match_sysattr(struct udev_enumerate *udev_enumerate, const char *syspath)
+static bool match_sysattr(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
{
- struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
struct udev_list_entry *list_entry;
/* skip list */
udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysattr_nomatch_list)) {
- if (match_sysattr_value(udev, syspath,
- udev_list_entry_get_name(list_entry),
- udev_list_entry_get_value(list_entry)))
- return 0;
+ if (match_sysattr_value(dev, udev_list_entry_get_name(list_entry),
+ udev_list_entry_get_value(list_entry)))
+ return false;
}
/* include list */
if (udev_list_get_entry(&udev_enumerate->sysattr_match_list) != NULL) {
udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysattr_match_list)) {
/* anything that does not match, will make it FALSE */
- if (!match_sysattr_value(udev, syspath,
- udev_list_entry_get_name(list_entry),
- udev_list_entry_get_value(list_entry)))
- return 0;
+ if (!match_sysattr_value(dev, udev_list_entry_get_name(list_entry),
+ udev_list_entry_get_value(list_entry)))
+ return false;
}
- return 1;
+ return true;
}
- return 1;
+ return true;
}
-static int match_property(struct udev_enumerate *udev_enumerate, const char *syspath)
+static bool match_property(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
{
- struct udev_device *dev;
struct udev_list_entry *list_entry;
- int match = false;
+ bool match = false;
/* 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;
+ return true;
/* loop over matches */
udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->properties_match_list)) {
}
}
out:
- udev_device_unref(dev);
return match;
}
-static int match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
+static bool match_tag(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
+{
+ struct udev_list_entry *list_entry;
+
+ /* no match always matches */
+ if (udev_list_get_entry(&udev_enumerate->tags_match_list) == NULL)
+ return true;
+
+ /* loop over matches */
+ udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->tags_match_list))
+ if (!udev_device_has_tag(dev, udev_list_entry_get_name(list_entry)))
+ return false;
+
+ return true;
+}
+
+static bool match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
{
struct udev_list_entry *list_entry;
if (udev_list_get_entry(&udev_enumerate->sysname_match_list) == NULL)
- return 1;
+ return true;
udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysname_match_list)) {
if (fnmatch(udev_list_entry_get_name(list_entry), sysname, 0) != 0)
continue;
- return 1;
+ return true;
}
- return 0;
+ return false;
}
static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate,
util_strpcpyl(&s, l, "/", subdir2, NULL);
dir = opendir(path);
if (dir == NULL)
- return -1;
+ return -ENOENT;
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
char syspath[UTIL_PATH_SIZE];
- char filename[UTIL_PATH_SIZE];
- struct stat statbuf;
+ struct udev_device *dev;
if (dent->d_name[0] == '.')
continue;
+
if (!match_sysname(udev_enumerate, dent->d_name))
continue;
util_strscpyl(syspath, sizeof(syspath), path, "/", dent->d_name, NULL);
- if (!match_property(udev_enumerate, syspath))
- continue;
- if (lstat(syspath, &statbuf) != 0)
- continue;
- if (S_ISREG(statbuf.st_mode))
+ dev = udev_device_new_from_syspath(udev_enumerate->udev, syspath);
+ if (dev == NULL)
continue;
- if (S_ISLNK(statbuf.st_mode))
- util_resolve_sys_link(udev, syspath, sizeof(syspath));
- util_strscpyl(filename, sizeof(filename), syspath, "/uevent", NULL);
- if (stat(filename, &statbuf) != 0)
- continue;
- if (!match_sysattr(udev_enumerate, syspath))
- continue;
- syspath_add(udev_enumerate, syspath);
+ if (!match_tag(udev_enumerate, dev))
+ goto nomatch;
+ if (!match_property(udev_enumerate, dev))
+ goto nomatch;
+ if (!match_sysattr(udev_enumerate, dev))
+ goto nomatch;
+
+ syspath_add(udev_enumerate, udev_device_get_syspath(dev));
+nomatch:
+ udev_device_unref(dev);
}
closedir(dir);
return 0;
}
-static int match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
+static bool match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
{
struct udev_list_entry *list_entry;
udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->subsystem_nomatch_list)) {
if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0)
- return 0;
+ return false;
}
if (udev_list_get_entry(&udev_enumerate->subsystem_match_list) != NULL) {
udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->subsystem_match_list)) {
if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0)
- return 1;
+ return true;
}
- return 0;
+ return false;
}
- return 1;
+ return true;
}
static int scan_dir(struct udev_enumerate *udev_enumerate, const char *basedir, const char *subdir, const char *subsystem)
if (udev_enumerate == NULL)
return -EINVAL;
- util_strscpyl(base, sizeof(base), udev_get_sys_path(udev), "/subsystem", NULL);
- if (stat(base, &statbuf) == 0) {
- /* we have /subsystem/, forget all the old stuff */
- dbg(udev, "searching '/subsystem/*/devices/*' dir\n");
- scan_dir(udev_enumerate, "subsystem", "devices", NULL);
+
+ if (udev_list_get_entry(&udev_enumerate->tags_match_list) != NULL) {
+ struct udev_list_entry *list_entry;
+
+ /* scan only tagged devices, use tags reverse-index, instead of searching all devices in /sys */
+ udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->tags_match_list)) {
+ DIR *dir;
+ struct dirent *dent;
+ char path[UTIL_PATH_SIZE];
+
+ util_strscpyl(path, sizeof(path), udev_get_dev_path(udev), "/.udev/tags/",
+ udev_list_entry_get_name(list_entry), NULL);
+ dir = opendir(path);
+ if (dir == NULL)
+ continue;
+ for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
+ struct udev_device *dev;
+ char syspath[UTIL_PATH_SIZE];
+ char *s;
+ size_t l;
+ ssize_t len;
+
+ if (dent->d_name[0] == '.')
+ continue;
+
+ s = syspath;
+ l = util_strpcpyl(&s, sizeof(syspath), udev_get_sys_path(udev), NULL);
+ len = readlinkat(dirfd(dir), dent->d_name, s, l);
+ if (len <= 0 || (size_t)len == l)
+ continue;
+ s[len] = '\0';
+
+ dev = udev_device_new_from_syspath(udev_enumerate->udev, syspath);
+ if (dev == NULL)
+ continue;
+ syspath_add(udev_enumerate, udev_device_get_syspath(dev));
+ udev_device_unref(dev);
+ }
+ }
} else {
+ util_strscpyl(base, sizeof(base), udev_get_sys_path(udev), "/subsystem", NULL);
+ if (stat(base, &statbuf) == 0) {
+ /* we have /subsystem/, forget all the old stuff */
+ dbg(udev, "searching '/subsystem/*/devices/*' dir\n");
+ scan_dir(udev_enumerate, "subsystem", "devices", NULL);
+ } else {
dbg(udev, "searching '/bus/*/devices/*' dir\n");
- scan_dir(udev_enumerate, "bus", "devices", NULL);
- dbg(udev, "searching '/class/*' dir\n");
- scan_dir(udev_enumerate, "class", NULL, NULL);
+ scan_dir(udev_enumerate, "bus", "devices", NULL);
+ dbg(udev, "searching '/class/*' dir\n");
+ scan_dir(udev_enumerate, "class", NULL, NULL);
+ }
}
+
return 0;
}
if (udev_enumerate == NULL)
return -EINVAL;
+
util_strscpyl(base, sizeof(base), udev_get_sys_path(udev), "/subsystem", NULL);
if (stat(base, &statbuf) == 0)
subsysdir = "subsystem";
/*
* libudev - interface to udev device information
*
- * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
struct sockaddr_un sun;
socklen_t addrlen;
struct udev_list_node filter_subsystem_list;
+ struct udev_list_node filter_tag_list;
};
enum udev_monitor_netlink_group {
UDEV_MONITOR_UDEV,
};
-#define UDEV_MONITOR_MAGIC 0xcafe1dea
+#define UDEV_MONITOR_MAGIC 0xfeedcafe
struct udev_monitor_netlink_header {
- /* udev version text */
- char version[16];
+ /* "libudev" prefix to distinguish libudev and kernel messages */
+ char prefix[8];
/*
* magic to protect against daemon <-> library message format mismatch
* used in the kernel from socket filter rules; needs to be stored in network order
*/
unsigned int magic;
- /* properties buffer */
- unsigned short properties_off;
- unsigned short properties_len;
+ /* total length of header structure known to the sender */
+ unsigned int header_size;
+ /* properties string buffer */
+ unsigned int properties_off;
+ unsigned int properties_len;
/*
- * hashes of some common device properties strings to filter with socket filters in
- * the client used in the kernel from socket filter rules; needs to be stored in
- * network order
+ * hashes of primary device properties strings, to let libudev subscribers
+ * use in-kernel socket filters; values need to be stored in network order
*/
- unsigned int filter_subsystem;
- unsigned int filter_devtype;
+ unsigned int filter_subsystem_hash;
+ unsigned int filter_devtype_hash;
+ unsigned int filter_tag_bloom_hi;
+ unsigned int filter_tag_bloom_lo;
};
static struct udev_monitor *udev_monitor_new(struct udev *udev)
udev_monitor->refcount = 1;
udev_monitor->udev = udev;
udev_list_init(&udev_monitor->filter_subsystem_list);
+ udev_list_init(&udev_monitor->filter_tag_list);
return udev_monitor;
}
*/
int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
{
- static struct sock_filter ins[256];
- static struct sock_fprog filter;
+ struct sock_filter ins[512];
+ struct sock_fprog filter;
unsigned int i;
struct udev_list_entry *list_entry;
int err;
- if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
+ if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL &&
+ udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
return 0;
memset(ins, 0x00, sizeof(ins));
/* wrong magic, pass packet */
bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
- /* add all subsystem match values */
- udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
- unsigned int hash;
-
- /* load filter_subsystem value in A */
- bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem));
- hash = util_string_hash32(udev_list_entry_get_name(list_entry));
- if (udev_list_entry_get_value(list_entry) == NULL) {
- /* jump if subsystem does not match */
- bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
- } else {
- /* jump if subsystem does not match */
- bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
-
- /* load filter_devtype value in A */
- bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype));
- /* jump if value does not match */
- hash = util_string_hash32(udev_list_entry_get_value(list_entry));
- bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
+ if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) {
+ int tag_matches;
+
+ /* count tag matches, to calculate end of tag match block */
+ tag_matches = 0;
+ udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list))
+ tag_matches++;
+
+ /* add all tags matches */
+ udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
+ uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry));
+ uint32_t tag_bloom_hi = tag_bloom_bits >> 32;
+ uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff;
+
+ /* load device bloom bits in A */
+ bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi));
+ /* clear bits (tag bits & bloom bits) */
+ bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi);
+ /* jump to next tag if it does not match */
+ bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3);
+
+ /* load device bloom bits in A */
+ bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo));
+ /* clear bits (tag bits & bloom bits) */
+ bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo);
+ /* jump behind end of tag match block if tag matches */
+ tag_matches--;
+ bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0);
}
- /* matched, pass packet */
- bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
+ /* nothing matched, drop packet */
+ bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
+ }
- if (i+1 >= ARRAY_SIZE(ins))
- return -1;
+ /* add all subsystem matches */
+ if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) {
+ udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
+ unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry));
+
+ /* load device subsystem value in A */
+ bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash));
+ if (udev_list_entry_get_value(list_entry) == NULL) {
+ /* jump if subsystem does not match */
+ bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
+ } else {
+ /* jump if subsystem does not match */
+ bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
+
+ /* load device devtype value in A */
+ bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash));
+ /* jump if value does not match */
+ hash = util_string_hash32(udev_list_entry_get_value(list_entry));
+ bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
+ }
+
+ /* matched, pass packet */
+ bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
+
+ if (i+1 >= ARRAY_SIZE(ins))
+ return -1;
+ }
+
+ /* nothing matched, drop packet */
+ bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
}
- /* nothing matched, drop packet */
- bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
+
+ /* matched, pass packet */
+ bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
/* install filter */
filter.len = i;
if (udev_monitor->sock >= 0)
close(udev_monitor->sock);
udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list);
+ udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_tag_list);
dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
free(udev_monitor);
}
struct udev_list_entry *list_entry;
if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
- return 1;
-
+ goto tag;
udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
const char *subsys = udev_list_entry_get_name(list_entry);
const char *dsubsys = udev_device_get_subsystem(udev_device);
devtype = udev_list_entry_get_value(list_entry);
if (devtype == NULL)
- return 1;
+ goto tag;
ddevtype = udev_device_get_devtype(udev_device);
if (ddevtype == NULL)
continue;
if (strcmp(ddevtype, devtype) == 0)
+ goto tag;
+ }
+ return 0;
+
+tag:
+ if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
+ return 1;
+ udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
+ const char *tag = udev_list_entry_get_name(list_entry);
+
+ if (udev_device_has_tag(udev_device, tag))
return 1;
}
return 0;
return NULL;
}
- if (strncmp(buf, "udev-", 5) == 0) {
+ if (memcmp(buf, "libudev", 8) == 0) {
/* udev message needs proper version magic */
nlh = (struct udev_monitor_netlink_header *) buf;
if (nlh->magic != htonl(UDEV_MONITOR_MAGIC))
return NULL;
- if (nlh->properties_off < sizeof(struct udev_monitor_netlink_header))
- return NULL;
if (nlh->properties_off+32 > buflen)
return NULL;
bufpos = nlh->properties_off;
int udev_monitor_send_device(struct udev_monitor *udev_monitor,
struct udev_monitor *destination, struct udev_device *udev_device)
{
- struct msghdr smsg;
- struct iovec iov[2];
const char *buf;
ssize_t blen;
ssize_t count;
blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
if (blen < 32)
- return -1;
+ return -EINVAL;
if (udev_monitor->sun.sun_family != 0) {
+ struct msghdr smsg;
+ struct iovec iov[2];
const char *action;
char header[2048];
char *s;
smsg.msg_iovlen = 2;
smsg.msg_name = &udev_monitor->sun;
smsg.msg_namelen = udev_monitor->addrlen;
- } else if (udev_monitor->snl.nl_family != 0) {
+ count = sendmsg(udev_monitor->sock, &smsg, 0);
+ info(udev_monitor->udev, "passed %zi bytes to socket monitor %p\n", count, udev_monitor);
+ return count;
+ }
+
+ if (udev_monitor->snl.nl_family != 0) {
+ struct msghdr smsg;
+ struct iovec iov[2];
const char *val;
struct udev_monitor_netlink_header nlh;
-
+ struct udev_list_entry *list_entry;
+ uint64_t tag_bloom_bits;
/* add versioned header */
memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
- util_strscpy(nlh.version, sizeof(nlh.version), "udev-" VERSION);
+ memcpy(nlh.prefix, "libudev", 8);
nlh.magic = htonl(UDEV_MONITOR_MAGIC);
+ nlh.header_size = sizeof(struct udev_monitor_netlink_header);
val = udev_device_get_subsystem(udev_device);
- nlh.filter_subsystem = htonl(util_string_hash32(val));
+ nlh.filter_subsystem_hash = htonl(util_string_hash32(val));
val = udev_device_get_devtype(udev_device);
if (val != NULL)
- nlh.filter_devtype = htonl(util_string_hash32(val));
+ nlh.filter_devtype_hash = htonl(util_string_hash32(val));
iov[0].iov_base = &nlh;
iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
+ /* add tag bloom filter */
+ tag_bloom_bits = 0;
+ udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
+ tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry));
+ if (tag_bloom_bits > 0) {
+ nlh.filter_tag_bloom_hi = htonl(tag_bloom_bits >> 32);
+ nlh.filter_tag_bloom_lo = htonl(tag_bloom_bits & 0xffffffff);
+ }
+
/* add properties list */
nlh.properties_off = iov[0].iov_len;
nlh.properties_len = blen;
else
smsg.msg_name = &udev_monitor->snl_destination;
smsg.msg_namelen = sizeof(struct sockaddr_nl);
- } else {
- return -1;
+ count = sendmsg(udev_monitor->sock, &smsg, 0);
+ info(udev_monitor->udev, "passed %zi bytes to netlink monitor %p\n", count, udev_monitor);
+ return count;
}
- count = sendmsg(udev_monitor->sock, &smsg, 0);
- info(udev_monitor->udev, "passed %zi bytes to monitor %p\n", count, udev_monitor);
- return count;
+ return -EINVAL;
}
/**
* @subsystem: the subsystem value to match the incoming devices against
* @devtype: the devtype value to match the incoming devices against
*
+ * This filer is efficiently executed inside the kernel, and libudev subscribers
+ * will usually not be woken up for devices which do not match.
+ *
* The filter must be installed before the monitor is switched to listening mode.
*
* Returns: 0 on success, otherwise a negative error value.
if (udev_monitor == NULL)
return -EINVAL;
if (subsystem == NULL)
- return 0;
+ return -EINVAL;
if (udev_list_entry_add(udev_monitor->udev,
&udev_monitor->filter_subsystem_list, subsystem, devtype, 0, 0) == NULL)
return -ENOMEM;
return 0;
}
+/**
+ * udev_monitor_filter_add_match_tag:
+ * @udev_monitor: the monitor
+ * @tag: the name of a tag
+ *
+ * This filer is efficiently executed inside the kernel, and libudev subscribers
+ * will usually not be woken up for devices which do not match.
+ *
+ * The filter must be installed before the monitor is switched to listening mode.
+ *
+ * Returns: 0 on success, otherwise a negative error value.
+ */
+int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag)
+{
+ if (udev_monitor == NULL)
+ return -EINVAL;
+ if (tag == NULL)
+ return -EINVAL;
+ if (udev_list_entry_add(udev_monitor->udev,
+ &udev_monitor->filter_tag_list, tag, NULL, 0, 0) == NULL)
+ return -ENOMEM;
+ return 0;
+}
+
/**
* udev_monitor_filter_remove:
* @udev_monitor: monitor
#include <syslog.h>
#include <signal.h>
+#include <stdint.h>
+#include <stdbool.h>
#include "libudev.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old);
const char *udev_device_get_knodename(struct udev_device *udev_device);
int udev_device_add_tag(struct udev_device *udev_device, const char *tag);
-struct udev_list_entry *udev_device_get_tag_list_entry(struct udev_device *udev_device);
+void udev_device_cleanup_tags_list(struct udev_device *udev_device);
+struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device);
int udev_device_has_tag(struct udev_device *udev_device, const char *tag);
int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename);
int udev_device_get_timeout(struct udev_device *udev_device);
int udev_util_replace_whitespace(const char *str, char *to, size_t len);
int udev_util_replace_chars(char *str, const char *white);
int udev_util_encode_string(const char *str, char *str_enc, size_t len);
-unsigned int util_string_hash32(const char *str);
+unsigned int util_string_hash32(const char *key);
+uint64_t util_string_bloom64(const char *str);
/* libudev-util-private.c */
int util_create_path(struct udev *udev, const char *path);
return -1;
}
+/*
+ * http://sites.google.com/site/murmurhash/
+ *
+ * All code is released to the public domain. For business purposes,
+ * Murmurhash is under the MIT license.
+ *
+ */
+static unsigned int murmur_hash2(const char *key, int len, unsigned int seed)
+{
+ /*
+ * 'm' and 'r' are mixing constants generated offline.
+ * They're not really 'magic', they just happen to work well.
+ */
+ const unsigned int m = 0x5bd1e995;
+ const int r = 24;
+
+ /* initialize the hash to a 'random' value */
+ unsigned int h = seed ^ len;
+
+ /* mix 4 bytes at a time into the hash */
+ const unsigned char * data = (const unsigned char *)key;
+
+ while(len >= 4) {
+ unsigned int k = *(unsigned int *)data;
+
+ k *= m;
+ k ^= k >> r;
+ k *= m;
+ h *= m;
+ h ^= k;
+
+ data += 4;
+ len -= 4;
+ }
+
+ /* handle the last few bytes of the input array */
+ switch(len) {
+ case 3:
+ h ^= data[2] << 16;
+ case 2:
+ h ^= data[1] << 8;
+ case 1:
+ h ^= data[0];
+ h *= m;
+ };
+
+ /* do a few final mixes of the hash to ensure the last few bytes are well-incorporated */
+ h ^= h >> 13;
+ h *= m;
+ h ^= h >> 15;
+
+ return h;
+}
+
unsigned int util_string_hash32(const char *str)
{
- unsigned int hash = 0;
+ return murmur_hash2(str, strlen(str), 0);
+}
- while (str[0] != '\0') {
- hash += str[0] << 4;
- hash += str[0] >> 4;
- hash *= 11;
- str++;
- }
- return hash;
+/* get a bunch of bit numbers out of the hash, and set the bits in our bit field */
+uint64_t util_string_bloom64(const char *str)
+{
+ uint64_t bits = 0;
+ unsigned int hash = util_string_hash32(str);
+
+ bits |= 1LLU << (hash & 63);
+ bits |= 1LLU << ((hash >> 6) & 63);
+ bits |= 1LLU << ((hash >> 12) & 63);
+ bits |= 1LLU << ((hash >> 18) & 63);
+ return bits;
}
/*
* libudev - interface to udev device information
*
- * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
/* in-kernel socket filters to select messages that get delivered to a listener */
int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor,
const char *subsystem, const char *devtype);
+int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag);
int udev_monitor_filter_update(struct udev_monitor *udev_monitor);
int udev_monitor_filter_remove(struct udev_monitor *udev_monitor);
int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value);
int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value);
int udev_enumerate_add_match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname);
+int udev_enumerate_add_match_tag(struct udev_enumerate *udev_enumerate, const char *tag);
int udev_enumerate_add_syspath(struct udev_enumerate *udev_enumerate, const char *syspath);
/* run enumeration with active filters */
int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate);
if (strcmp(udev_device_get_action(dev), "remove") == 0) {
udev_device_read_db(dev);
- udev_device_set_info_loaded(dev);
udev_device_delete_db(dev);
if (major(udev_device_get_devnum(dev)) != 0)
TK_A_GROUP_ID, /* gid_t */
TK_A_MODE_ID, /* mode_t */
TK_A_ENV, /* val, attr */
+ TK_A_TAG, /* val */
TK_A_NAME, /* val */
TK_A_DEVLINK, /* val */
TK_A_EVENT_TIMEOUT, /* int */
[TK_A_GROUP_ID] = "A GROUP_ID",
[TK_A_MODE_ID] = "A MODE_ID",
[TK_A_ENV] = "A ENV",
+ [TK_A_TAG] = "A ENV",
[TK_A_NAME] = "A NAME",
[TK_A_DEVLINK] = "A DEVLINK",
[TK_A_EVENT_TIMEOUT] = "A EVENT_TIMEOUT",
dbg(rules->udev, "%s %s '%s' '%s'(%s)\n",
token_str(type), operation_str(op), attr, value, string_glob_str(glob));
break;
+ case TK_A_TAG:
+ dbg(rules->udev, "%s %s '%s'\n", token_str(type), operation_str(op), value);
+ break;
case TK_A_STRING_ESCAPE_NONE:
case TK_A_STRING_ESCAPE_REPLACE:
dbg(rules->udev, "%s\n", token_str(type));
case TK_A_MODE:
case TK_A_NAME:
case TK_A_GOTO:
+ case TK_A_TAG:
token->key.value_off = add_string(rule_tmp->rules, value);
break;
case TK_M_ENV:
continue;
}
+ if (strcmp(key, "TAG") == 0) {
+ rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
+ continue;
+ }
+
if (strcmp(key, "PROGRAM") == 0) {
rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
continue;
}
break;
}
+ case TK_A_TAG:
+ udev_device_add_tag(event->dev, &rules->buf[cur->key.value_off]);
+ break;
case TK_A_NAME:
{
const char *name = &rules->buf[cur->key.value_off];
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><option>TAG</option></term>
+ <listitem>
+ <para>Attach a tag to a device. This is used to filter events for users
+ of libudev's monitor functionality, or to enumerate a group of tagged
+ devices. The implementation can only work efficiently if only a few
+ tags are attached to a device. It is only meant to be used in
+ contexts with specific device filter requirements, and not as a
+ general-purpose flag. Excessive use might result in inefficient event
+ handling.</para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><option>RUN</option></term>
<listitem>
int print_kernel = 0;
int print_udev = 0;
struct udev_list_node subsystem_match_list;
+ struct udev_list_node tag_match_list;
struct udev_monitor *udev_monitor = NULL;
struct udev_monitor *kernel_monitor = NULL;
fd_set readfds;
{ "kernel", no_argument, NULL, 'k' },
{ "udev", no_argument, NULL, 'u' },
{ "subsystem-match", required_argument, NULL, 's' },
+ { "tag-match", required_argument, NULL, 't' },
{ "help", no_argument, NULL, 'h' },
{}
};
udev_list_init(&subsystem_match_list);
+ udev_list_init(&tag_match_list);
while (1) {
- option = getopt_long(argc, argv, "epkus:h", options, NULL);
+ option = getopt_long(argc, argv, "pekus:t:h", options, NULL);
if (option == -1)
break;
udev_list_entry_add(udev, &subsystem_match_list, subsys, devtype, 0, 0);
break;
}
+ case 't':
+ udev_list_entry_add(udev, &tag_match_list, optarg, NULL, 0, 0);
+ break;
case 'h':
printf("Usage: udevadm monitor [--property] [--kernel] [--udev] [--help]\n"
" --property print the event properties\n"
" --kernel print kernel uevents\n"
" --udev print udev events\n"
" --subsystem-match=<subsystem[/devtype]> filter events by subsystem\n"
+ " --tag-match=<tag> filter events by tag\n"
" --help\n\n");
default:
goto out;
fprintf(stderr, "error: unable to apply subsystem filter '%s'\n", subsys);
}
+ udev_list_entry_foreach(entry, udev_list_get_entry(&tag_match_list)) {
+ const char *tag = udev_list_entry_get_name(entry);
+
+ if (udev_monitor_filter_add_match_tag(udev_monitor, tag) < 0)
+ fprintf(stderr, "error: unable to apply tag filter '%s'\n", tag);
+ }
+
if (udev_monitor_enable_receiving(udev_monitor) < 0) {
fprintf(stderr, "error: unable to subscribe to udev events\n");
rc = 2;
udev_monitor_unref(udev_monitor);
udev_monitor_unref(kernel_monitor);
udev_list_cleanup_entries(udev, &subsystem_match_list);
+ udev_list_cleanup_entries(udev, &tag_match_list);
return rc;
}
{ "attr-match", required_argument, NULL, 'a' },
{ "attr-nomatch", required_argument, NULL, 'A' },
{ "property-match", required_argument, NULL, 'p' },
+ { "tag-match", required_argument, NULL, 'g' },
{ "sysname-match", required_argument, NULL, 'y' },
{ "help", no_argument, NULL, 'h' },
{}
const char *val;
char buf[UTIL_PATH_SIZE];
- option = getopt_long(argc, argv, "vnFo:t:hcp:s:S:a:A:y:", options, NULL);
+ option = getopt_long(argc, argv, "vng:o:t:hcp:s:S:a:A:y:", options, NULL);
if (option == -1)
break;
key = keyval(optarg, &val, buf, sizeof(buf));
udev_enumerate_add_match_property(udev_enumerate, key, val);
break;
+ case 'g':
+ udev_enumerate_add_match_tag(udev_enumerate, optarg);
+ break;
case 'y':
udev_enumerate_add_match_sysname(udev_enumerate, optarg);
break;
" --attr-match=<file[=<value>]> trigger devices with a matching attribute\n"
" --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
" --property-match=<key>=<value> trigger devices with a matching property\n"
+ " --tag-match=<key>=<value> trigger devices with a matching property\n"
" --sysname-match=<name> trigger devices with a matching name\n"
" --help\n\n");
goto exit;
specified multiple times and supports shell style pattern matching.</para>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><option>--tag-match=<replaceable>property</replaceable></option></term>
+ <listitem>
+ <para>Trigger events for devices with a matching tag. This option can be
+ specified multiple times.</para>
+ </listitem>
+ </varlistentry>
<varlistentry>
<term><option>--sysname-match=<replaceable>name</replaceable></option></term>
<listitem>
<para>Filter events by subsystem[/devtype]. Only udev events with a matching subsystem value will pass.</para>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><option>--tag-match=<replaceable>string</replaceable></option></term>
+ <listitem>
+ <para>Filter events by property. Only udev events with a given tag attached will pass.</para>
+ </listitem>
+ </varlistentry>
<varlistentry>
<term><option>--help</option></term>
<listitem>