From f611bde3f182e9a4befb48a0160d1831708aca67 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Thu, 19 Sep 2013 17:16:53 +0800 Subject: discover: Remove unnecessary event passing Currently, we pass "events" between the udev, user-event and device-handler layers. These events all get sent through device_handler_event, then de-multiplexed to an appropriate handler, depending on their source. Instead, just export relevant device_handler functions, and have the (old) event sources call these functions directly. This also means we can include a lot more of the device hander code in the parser tests. Signed-off-by: Jeremy Kerr --- discover/udev.c | 124 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 67 insertions(+), 57 deletions(-) (limited to 'discover/udev.c') diff --git a/discover/udev.c b/discover/udev.c index f9eb26d..1c5cf71 100644 --- a/discover/udev.c +++ b/discover/udev.c @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -50,93 +51,102 @@ static int udev_destructor(void *p) return 0; } -static void udev_setup_event_params(struct udev_device *dev, - struct event *event) +static void udev_setup_device_params(struct udev_device *udev, + struct discover_device *dev) { struct udev_list_entry *list, *entry; - list = udev_device_get_properties_list_entry(dev); + list = udev_device_get_properties_list_entry(udev); if (!list) return; udev_list_entry_foreach(entry, list) - event_set_param(event,udev_list_entry_get_name(entry), + discover_device_set_param(dev, + udev_list_entry_get_name(entry), udev_list_entry_get_value(entry)); } -static int udev_handle_dev_action(struct udev_device *dev, const char *action) +static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev) { - const char *devtype; - const char *devpath; - const char *devnode; - struct pb_udev *udev; - struct event *event; - enum event_action eva = 0; - - assert(dev); - assert(action); - - devtype = udev_device_get_devtype(dev); /* DEVTYPE */ - - if (!devtype) { - pb_log("udev_device_get_devtype failed\n"); + struct discover_device *ddev; + const char *typestr; + const char *path; + const char *name; + + name = udev_device_get_sysname(dev); + if (!name) { + pb_debug("udev_device_get_sysname failed\n"); return -1; } - devpath = udev_device_get_devpath(dev); /* DEVPATH */ - - if (!devpath) { - pb_log("udev_device_get_devpath failed\n"); + typestr = udev_device_get_devtype(dev); + if (!typestr) { + pb_debug("udev_device_get_devtype failed\n"); return -1; } - devnode = udev_device_get_devnode(dev); /* DEVNAME */ - - if (!devnode) { - pb_log("udev_device_get_devnode failed\n"); - return -1; + if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) { + pb_debug("SKIP %s: invalid type %s\n", name, typestr); + return 0; } - /* Ignore non disk or partition, ram, loop. */ - - if (!(strstr(devtype, "disk") || strstr(devtype, "partition")) - || strstr(devpath, "virtual/block/loop") - || strstr(devpath, "virtual/block/ram")) { - pb_log("SKIP: %s - %s\n", devtype, devnode); + path = udev_device_get_devpath(dev); + if (path && (strstr(path, "virtual/block/loop") + || strstr(path, "virtual/block/ram"))) { + pb_debug("SKIP: %s: ignored (path=%s)\n", name, path); return 0; } - if (!strcmp(action, "add")) { - pb_log("ADD: %s - %s\n", devtype, devnode); - eva = EVENT_ACTION_ADD; - } else if (!strcmp(action, "remove")) { - pb_log("REMOVE: %s - %s\n", devtype, devnode); - eva = EVENT_ACTION_REMOVE; - } else { - pb_log("SKIP: %s: %s - %s\n", action, devtype, devnode); - return 0; + /* We have enough info to create the device and start discovery */ + ddev = device_lookup_by_id(udev->handler, name); + if (ddev) { + pb_debug("device %s is already present?\n", name); + return -1; } - event = talloc(NULL, struct event); + ddev = discover_device_create(udev->handler, name); - event->type = EVENT_TYPE_UDEV; - event->action = eva; - event->device = devnode; + ddev->device_path = udev_device_get_devnode(dev); + ddev->uuid = udev_device_get_property_value(dev, "ID_FS_UUID"); + ddev->label = udev_device_get_property_value(dev, "ID_FS_LABEL"); + ddev->device->type = DEVICE_TYPE_DISK; - event->n_params = 0; - event->params = NULL; - event_set_param(event, "path", devpath); - event_set_param(event, "node", devnode); - event_set_param(event, "type", devtype); + udev_setup_device_params(dev, ddev); - udev_setup_event_params(dev, event); + device_handler_discover(udev->handler, ddev, CONF_METHOD_LOCAL_FILE); + + return 0; +} + +static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev) +{ + struct discover_device *ddev; + const char *name; + + name = udev_device_get_sysname(dev); + if (!name) { + pb_debug("udev_device_get_sysname failed\n"); + return -1; + } + + ddev = device_lookup_by_id(udev->handler, name); + if (!ddev) + return 0; + + device_handler_remove(udev->handler, ddev); + + return 0; +} +static int udev_handle_dev_action(struct udev_device *dev, const char *action) +{ + struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev)); - udev = udev_get_userdata(udev_device_get_udev(dev)); - assert(udev); + if (!strcmp(action, "add")) + return udev_handle_dev_add(udev, dev); - device_handler_event(udev->handler, event); + else if (!strcmp(action, "remove")) + return udev_handle_dev_remove(udev, dev); - talloc_free(event); return 0; } -- cgit v1.2.1