discover: Change parsers to explicitly request configuration files
[petitboot] / discover / udev.c
index 7fc5595f9b72f17dfac18d48ef9987c0fd96c03c..48ab745b4f371ac410048dc3ab7b93f9dc3ff4fb 100644 (file)
@@ -13,6 +13,7 @@
 #include <sys/un.h>
 
 #include <log/log.h>
+#include <types/types.h>
 #include <talloc/talloc.h>
 #include <waiter/waiter.h>
 #include <system/system.h>
 #include "pb-discover.h"
 #include "device-handler.h"
 
-#if defined(DEBUG)
-#define DBG(fmt, args...) pb_log("DBG: " fmt, ## args)
-#define DBGS(fmt, args...) \
-       pb_log("DBG:%s:%d: " fmt, __func__, __LINE__, ## args)
-#else
-#define DBG(fmt, args...)
-#define DBGS(fmt, args...)
-#endif
-
 struct pb_udev {
        struct udev *udev;
        struct udev_monitor *monitor;
@@ -50,98 +42,114 @@ 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) {
-               DBG("property: %s - %s\n",
-                       udev_list_entry_get_name(entry),
-                       udev_device_get_property_value(dev,
-                               udev_list_entry_get_name(entry)));
-
-               event_set_param(event,udev_list_entry_get_name(entry),
+       udev_list_entry_foreach(entry, list)
+               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 *serial;
+       const char *path;
+       const char *name;
+       const char *prop;
+
+       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);
+       /* we may also see multipath devices; same dev nodes (hence id), but
+        * different serial numbers */
+       serial = udev_device_get_property_value(dev, "ID_SERIAL");
+       if (serial && device_lookup_by_serial(udev->handler, serial))
+               return -1;
+
+       ddev = discover_device_create(udev->handler, name);
+
+       ddev->device_path = udev_device_get_devnode(dev);
+       prop = udev_device_get_property_value(dev, "ID_FS_UUID");
+       if (prop)
+               ddev->uuid = talloc_strdup(ddev, prop);
+       prop = udev_device_get_property_value(dev, "ID_FS_LABEL");
+       if (prop)
+               ddev->label = talloc_strdup(ddev, prop);
+       ddev->device->type = DEVICE_TYPE_DISK;
+
+       udev_setup_device_params(dev, ddev);
 
-       event->type = EVENT_TYPE_UDEV;
-       event->action = eva;
-       event->device = devpath;
+       device_handler_discover(udev->handler, ddev);
 
-       event->n_params = 0;
-       event->params = NULL;
-       event_set_param(event, "DEVNAME", devnode);
+       return 0;
+}
 
-       udev_setup_event_params(dev, event);
+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;
 }
 
@@ -303,10 +311,10 @@ struct pb_udev *udev_init(struct waitset *waitset,
        if (result)
                goto fail_monitor;
 
-       waiter_register(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
+       waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
                udev_process, udev->monitor);
 
-       pb_log("%s: waiting on udev\n", __func__);
+       pb_debug("%s: waiting on udev\n", __func__);
 
        return udev;