discover: Fix dead code in udev_handle_cdrom_events()
[petitboot] / discover / udev.c
index 9e1a5ce8a5155294efe33b5ee817853c3c57fc43..537ef10263fa11193f03057d0335de48910abc0a 100644 (file)
@@ -25,6 +25,7 @@
 #include "pb-discover.h"
 #include "device-handler.h"
 #include "cdrom.h"
+#include "devmapper.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
@@ -41,11 +42,15 @@ static int udev_destructor(void *p)
 {
        struct pb_udev *udev = p;
 
-       udev_monitor_unref(udev->monitor);
-       udev->monitor = NULL;
+       if (udev->monitor) {
+               udev_monitor_unref(udev->monitor);
+               udev->monitor = NULL;
+       }
 
-       udev_unref(udev->udev);
-       udev->udev = NULL;
+       if (udev->udev) {
+               udev_unref(udev->udev);
+               udev->udev = NULL;
+       }
 
        return 0;
 }
@@ -69,13 +74,21 @@ static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
                const char *name)
 {
        struct discover_device *ddev;
+       unsigned int i = 0;
        const char *typestr;
        const char *uuid;
        const char *path;
        const char *node;
        const char *prop;
        const char *type;
-       bool cdrom;
+       const char *devname;
+       const char *ignored_types[] = {
+               "linux_raid_member",
+               "swap",
+               "LVM2_member",
+               NULL,
+       };
+       bool cdrom, usb;
 
        typestr = udev_device_get_devtype(dev);
        if (!typestr) {
@@ -90,12 +103,16 @@ static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
 
        node = udev_device_get_devnode(dev);
        path = udev_device_get_devpath(dev);
-       if (path && (strstr(path, "virtual/block/loop")
-                       || strstr(path, "virtual/block/ram"))) {
+       if (path && strstr(path, "virtual/block/loop")) {
                pb_log("SKIP: %s: ignored (path=%s)\n", name, path);
                return 0;
        }
 
+       if (path && strstr(path, "virtual/block/ram")) {
+               device_handler_add_ramdisk(udev->handler, node);
+               return 0;
+       }
+
        cdrom = node && !!udev_device_get_property_value(dev, "ID_CDROM");
        if (cdrom) {
                /* CDROMs require a little initialisation, to get
@@ -107,12 +124,28 @@ static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
                }
        }
 
+       /* If our environment's udev can recognise them explictly skip any
+        * device mapper devices we encounter */
+       devname = udev_device_get_property_value(dev, "DM_NAME");
+       if (devname) {
+               pb_debug("SKIP: dm-device %s\n", devname);
+               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;
        }
 
+       while (ignored_types[i]) {
+               if (!strncmp(type, ignored_types[i], strlen(ignored_types[i]))) {
+                       pb_log("SKIP: %s: ignore '%s' filesystem\n", name, type);
+                       return 0;
+               }
+               i++;
+       }
+
        /* We may see multipath devices; they'll have the same uuid as an
         * existing device, so only parse the first. */
        uuid = udev_device_get_property_value(dev, "ID_FS_UUID");
@@ -134,10 +167,21 @@ static int udev_handle_block_add(struct pb_udev *udev, struct udev_device *dev,
        prop = udev_device_get_property_value(dev, "ID_FS_LABEL");
        if (prop)
                ddev->label = talloc_strdup(ddev, prop);
-       ddev->device->type = cdrom ? DEVICE_TYPE_OPTICAL : DEVICE_TYPE_DISK;
+
+       usb = !!udev_device_get_property_value(dev, "ID_USB_DRIVER");
+       if (cdrom)
+               ddev->device->type = DEVICE_TYPE_OPTICAL;
+       else
+               ddev->device->type = usb ? DEVICE_TYPE_USB : DEVICE_TYPE_DISK;
 
        udev_setup_device_params(dev, ddev);
 
+       /* Create a snapshot for all disks, unless it is an assembled RAID array */
+       if ((ddev->device->type == DEVICE_TYPE_DISK ||
+            ddev->device->type == DEVICE_TYPE_USB) &&
+           !udev_device_get_property_value(dev, "MD_LEVEL"))
+               devmapper_init_snapshot(udev->handler, ddev);
+
        device_handler_discover(udev->handler, ddev);
 
        return 0;
@@ -201,13 +245,12 @@ static bool udev_handle_cdrom_events(struct pb_udev *udev,
                struct udev_device *dev, struct discover_device *ddev)
 {
        const char *node;
+       bool eject = false;
 
        node = udev_device_get_devnode(dev);
 
        /* handle CDROM eject requests */
        if (udev_device_get_property_value(dev, "DISK_EJECT_REQUEST")) {
-               bool eject = false;
-
                pb_debug("udev: eject request\n");
 
                /* If the device is mounted, cdrom_id's own eject request may
@@ -216,7 +259,6 @@ static bool udev_handle_cdrom_events(struct pb_udev *udev,
                if (ddev) {
                        eject = ddev->mounted;
                        udev_handle_dev_remove(udev, dev);
-                       return false;
                }
 
                if (eject)
@@ -317,11 +359,6 @@ static int udev_enumerate(struct udev *udev)
 
        list = udev_enumerate_get_list_entry(enumerate);
 
-       if (!list) {
-               pb_log("udev_enumerate_get_list_entry failed\n");
-               goto fail;
-       }
-
        udev_list_entry_foreach(entry, list) {
                const char *syspath;
                struct udev_device *dev;
@@ -426,7 +463,7 @@ struct pb_udev *udev_init(struct device_handler *handler,
        struct pb_udev *udev;
        int result;
 
-       udev = talloc(handler, struct pb_udev);
+       udev = talloc_zero(handler, struct pb_udev);
        talloc_set_destructor(udev, udev_destructor);
        udev->handler = handler;
 
@@ -434,7 +471,7 @@ struct pb_udev *udev_init(struct device_handler *handler,
 
        if (!udev->udev) {
                pb_log("udev_new failed\n");
-               goto fail_new;
+               goto fail;
        }
 
        udev_set_userdata(udev->udev, udev);
@@ -443,11 +480,11 @@ struct pb_udev *udev_init(struct device_handler *handler,
 
        result = udev_setup_monitor(udev->udev, &udev->monitor);
        if (result)
-               goto fail_monitor;
+               goto fail;
 
        result = udev_enumerate(udev->udev);
        if (result)
-               goto fail_enumerate;
+               goto fail;
 
        waiter_register_io(waitset, udev_monitor_get_fd(udev->monitor), WAIT_IN,
                udev_process, udev->monitor);
@@ -456,11 +493,7 @@ struct pb_udev *udev_init(struct device_handler *handler,
 
        return udev;
 
-fail_monitor:
-       udev_monitor_unref(udev->monitor);
-fail_enumerate:
-       udev_unref(udev->udev);
-fail_new:
+fail:
        talloc_free(udev);
        return NULL;
 }