discover: move device sources to the device handler
[petitboot] / discover / udev.c
index e3495e99efe7f6f69ad33ba42dae26c07c2900d6..320dffd03c1676d5f401153bb24ed80bb3c21e55 100644 (file)
 #include "device-handler.h"
 #include "cdrom.h"
 
+/* We set a default monitor buffer size, as we may not process monitor
+ * events while performing device discvoery. systemd uses a 128M buffer, so
+ * we'll do the same here */
+static const int monitor_bufsize = 128 * 1024 * 1024;
+
 struct pb_udev {
        struct udev *udev;
        struct udev_monitor *monitor;
@@ -69,6 +74,7 @@ static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
        const char *path;
        const char *node;
        const char *prop;
+       const char *type;
        bool cdrom;
 
        typestr = udev_device_get_devtype(dev);
@@ -78,7 +84,7 @@ static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
        }
 
        if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) {
-               pb_debug("SKIP %s: invalid type %s\n", name, typestr);
+               pb_log("SKIP %s: invalid type %s\n", name, typestr);
                return 0;
        }
 
@@ -86,7 +92,7 @@ static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
        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);
+               pb_log("SKIP: %s: ignored (path=%s)\n", name, path);
                return 0;
        }
 
@@ -96,11 +102,16 @@ static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
                 * petitboot-compatible tray behaviour */
                cdrom_init(node);
                if (!cdrom_media_present(node)) {
-                       pb_debug("SKIP: %s: no media present\n", name);
+                       pb_log("SKIP: %s: no media present\n", name);
                        return 0;
                }
        }
 
+       type = udev_device_get_property_value(dev, "ID_FS_TYPE");
+       if (!type) {
+               pb_log("SKIP: %s: no ID_FS_TYPE property\n", name);
+               return 0;
+       }
 
        /* We may see multipath devices; they'll have the same uuid as an
         * existing device, so only parse the first. */
@@ -110,7 +121,7 @@ static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
                if (ddev) {
                        pb_log("SKIP: %s UUID [%s] already present (as %s)\n",
                                        name, uuid, ddev->device->id);
-                       return -1;
+                       return 0;
                }
        }
 
@@ -158,7 +169,7 @@ static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
                return udev_handle_block_add(udev, dev, name);
        }
 
-       pb_debug("SKIP %s: unknown subsystem %s\n", name, subsys);
+       pb_log("SKIP %s: unknown subsystem %s\n", name, subsys);
        return -1;
 }
 
@@ -183,18 +194,16 @@ static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
        return 0;
 }
 
-static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev)
+/* returns true if further event processing should stop (eg., we've
+ * ejected the cdrom)
+ */
+static bool udev_handle_cdrom_events(struct pb_udev *udev,
+               struct udev_device *dev, struct discover_device *ddev)
 {
-       struct discover_device *ddev;
-       const char *name, *node;
+       const char *node;
 
-       name = udev_device_get_sysname(dev);
        node = udev_device_get_devnode(dev);
 
-       /* we're only interested in CDROM change events at present */
-       if (!udev_device_get_property_value(dev, "ID_CDROM"))
-               return 0;
-
        /* handle CDROM eject requests */
        if (udev_device_get_property_value(dev, "DISK_EJECT_REQUEST")) {
                bool eject = false;
@@ -204,26 +213,51 @@ static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev)
                /* If the device is mounted, cdrom_id's own eject request may
                 * have failed. So, we'll need to do our own here.
                 */
-               ddev = device_lookup_by_id(udev->handler, name);
                if (ddev) {
                        eject = ddev->mounted;
                        udev_handle_dev_remove(udev, dev);
+                       return false;
                }
 
                if (eject)
                        cdrom_eject(node);
 
-               return 0;
+               return true;
        }
 
        if (udev_device_get_property_value(dev, "DISK_MEDIA_CHANGE")) {
                if (cdrom_media_present(node))
-                       return udev_handle_dev_add(udev, dev);
+                       udev_handle_dev_add(udev, dev);
                else
-                       return udev_handle_dev_remove(udev, dev);
+                       udev_handle_dev_remove(udev, dev);
+               return true;
        }
 
-       return 0;
+       return false;
+}
+
+static int udev_handle_dev_change(struct pb_udev *udev, struct udev_device *dev)
+{
+       struct discover_device *ddev;
+       const char *name;
+       int rc = 0;
+
+       name = udev_device_get_sysname(dev);
+
+       ddev = device_lookup_by_id(udev->handler, name);
+
+       /* if this is a CDROM device, process eject & media change requests;
+        * these may stop further processing */
+       if (!udev_device_get_property_value(dev, "ID_CDROM")) {
+               if (udev_handle_cdrom_events(udev, dev, ddev))
+                       return 0;
+       }
+
+       /* if this is a new device, treat it as an add */
+       if (!ddev)
+               rc = udev_handle_dev_add(udev, dev);
+
+       return rc;
 }
 
 static int udev_handle_dev_action(struct udev_device *dev, const char *action)
@@ -273,12 +307,17 @@ static int udev_enumerate(struct udev *udev)
        }
 
        result = udev_enumerate_add_match_subsystem(enumerate, "block");
-
        if (result) {
                pb_log("udev_enumerate_add_match_subsystem failed\n");
                goto fail;
        }
 
+       result = udev_enumerate_add_match_is_initialized(enumerate);
+       if (result) {
+               pb_log("udev_enumerate_add_match_is_initialised failed\n");
+               goto fail;
+       }
+
        udev_enumerate_scan_devices(enumerate);
 
        list = udev_enumerate_get_list_entry(enumerate);
@@ -321,6 +360,12 @@ static int udev_setup_monitor(struct udev *udev, struct udev_monitor **monitor)
                goto out_err;
        }
 
+       result = udev_monitor_set_receive_buffer_size(m, monitor_bufsize);
+       if (result) {
+               pb_log("udev_monitor_set_rx_bufsize(%d) failed\n",
+                       monitor_bufsize);
+       }
+
        result = udev_monitor_filter_add_match_subsystem_devtype(m, "block",
                NULL);
 
@@ -353,10 +398,8 @@ static int udev_process(void *arg)
        struct udev_monitor *monitor = arg;
        struct udev_device *dev;
        const char *action;
-       int result;
 
        dev = udev_monitor_receive_device(monitor);
-
        if (!dev) {
                pb_log("udev_monitor_receive_device failed\n");
                return -1;
@@ -366,17 +409,12 @@ static int udev_process(void *arg)
 
        if (!action) {
                pb_log("udev_device_get_action failed\n");
-               goto fail;
+       } else {
+               udev_handle_dev_action(dev, action);
        }
 
-       result = udev_handle_dev_action(dev, action);
-
-       udev_device_unref(dev);
-       return result;
-
-fail:
        udev_device_unref(dev);
-       return -1;
+       return 0;
 }
 
 static void udev_log_fn(struct udev __attribute__((unused)) *udev,
@@ -387,12 +425,13 @@ static void udev_log_fn(struct udev __attribute__((unused)) *udev,
       vfprintf(pb_log_get_stream(), format, args);
 }
 
-struct pb_udev *udev_init(struct waitset *waitset,
-       struct device_handler *handler)
+struct pb_udev *udev_init(struct device_handler *handler,
+               struct waitset *waitset)
 {
+       struct pb_udev *udev;
        int result;
-       struct pb_udev *udev = talloc(NULL, struct pb_udev);
 
+       udev = talloc(handler, struct pb_udev);
        talloc_set_destructor(udev, udev_destructor);
        udev->handler = handler;
 
@@ -407,16 +446,14 @@ struct pb_udev *udev_init(struct waitset *waitset,
 
        udev_set_log_fn(udev->udev, udev_log_fn);
 
-       result = udev_enumerate(udev->udev);
-
-       if (result)
-               goto fail_enumerate;
-
        result = udev_setup_monitor(udev->udev, &udev->monitor);
-
        if (result)
                goto fail_monitor;
 
+       result = udev_enumerate(udev->udev);
+       if (result)
+               goto fail_enumerate;
+
        waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
                udev_process, udev->monitor);
 
@@ -425,14 +462,10 @@ struct pb_udev *udev_init(struct waitset *waitset,
        return udev;
 
 fail_monitor:
+       udev_monitor_unref(udev->monitor);
 fail_enumerate:
        udev_unref(udev->udev);
 fail_new:
        talloc_free(udev);
        return NULL;
 }
-
-void udev_destroy(struct pb_udev *udev)
-{
-       talloc_free(udev);
-}