discover: move device sources to the device handler
[petitboot] / discover / udev.c
index 7a99eeaf9d772433c4e7f507a4be2f5e7f5dfa06..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;
@@ -79,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;
        }
 
@@ -87,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;
        }
 
@@ -97,14 +102,14 @@ 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_debug("SKIP: %s: no ID_FS_TYPE property\n", name);
+               pb_log("SKIP: %s: no ID_FS_TYPE property\n", name);
                return 0;
        }
 
@@ -116,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;
                }
        }
 
@@ -164,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;
 }
 
@@ -355,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);
 
@@ -387,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;
@@ -400,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,
@@ -421,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;
 
@@ -464,8 +469,3 @@ fail_new:
        talloc_free(udev);
        return NULL;
 }
-
-void udev_destroy(struct pb_udev *udev)
-{
-       talloc_free(udev);
-}