]> git.ozlabs.org Git - petitboot/commitdiff
discover: Fix CDROM handling
authorJeremy Kerr <jk@ozlabs.org>
Fri, 29 Nov 2013 02:47:10 +0000 (10:47 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 2 Dec 2013 09:00:16 +0000 (17:00 +0800)
Currently, we don't handle CDROM devices well; we'll try to mount on
boot, and not detect any media changes. Also, the default rules shipping
with udev will put the CDROM tray into a locked state, blocking eject
from working.

This change adds a set of cdrom utility functions, which the udev code
can use to properly initialise cdrom devices and handle eject and media
change requests.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/Makefile.am
discover/cdrom.c [new file with mode: 0644]
discover/cdrom.h [new file with mode: 0644]
discover/udev.c

index 2a7885d7c7c1ebb379578766a579ec21b9f8c5cc..84c0436a9ade56835d4a737dce769bcd47d084fb 100644 (file)
@@ -32,6 +32,8 @@ sbin_PROGRAMS = pb-discover
 pb_discover_SOURCES = \
        boot.c \
        boot.h \
+       cdrom.c \
+       cdrom.h \
        device-handler.c \
        device-handler.h \
        discover-server.c \
diff --git a/discover/cdrom.c b/discover/cdrom.c
new file mode 100644 (file)
index 0000000..0e88301
--- /dev/null
@@ -0,0 +1,81 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <limits.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <linux/cdrom.h>
+
+#include <log/log.h>
+
+#include "cdrom.h"
+
+static int cdrom_open(const char *devpath, const char *loc)
+{
+       int fd;
+
+       fd = open(devpath, O_RDONLY | O_NONBLOCK);
+       if (fd < 0)
+               pb_log("%s: can't open %s: %s\n", loc, devpath,
+                               strerror(errno));
+
+       return fd;
+}
+
+void cdrom_init(const char *devpath)
+{
+       int fd, rc;
+
+       fd = cdrom_open(devpath, __func__);
+       if (fd < 0)
+               return;
+
+       /* We disable autoclose so that any attempted mount() operation doesn't
+        * close the tray, and disable CDO_LOCK to prevent the lock status
+        * changing on open()/close()
+        */
+       rc = ioctl(fd, CDROM_CLEAR_OPTIONS, CDO_LOCK | CDO_AUTO_CLOSE);
+       if (rc < 0)
+               pb_debug("%s: CLEAR CDO_LOCK|CDO_AUTO_CLOSE failed: %s\n",
+                               __func__, strerror(errno));
+
+       close(fd);
+}
+
+bool cdrom_media_present(const char *devpath)
+{
+       int fd, rc;
+
+       fd = cdrom_open(devpath, __func__);
+       if (fd < 0)
+               return false;
+
+       rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
+
+       close(fd);
+
+       return rc == CDS_DISC_OK;
+}
+
+void cdrom_eject(const char *devpath)
+{
+       int fd, rc;
+
+       fd = cdrom_open(devpath, __func__);
+       if (fd < 0)
+               return;
+
+       /* unlock cdrom device */
+       rc = ioctl(fd, CDROM_LOCKDOOR, 0);
+       if (rc < 0)
+               pb_log("%s: CDROM_LOCKDOOR(unlock) failed: %s\n",
+                               __func__, strerror(errno));
+
+       rc = ioctl(fd, CDROMEJECT, 0);
+       if (rc < 0)
+               pb_log("%s: CDROM_EJECT failed: %s\n",
+                               __func__, strerror(errno));
+       close(fd);
+}
+
diff --git a/discover/cdrom.h b/discover/cdrom.h
new file mode 100644 (file)
index 0000000..2e9de06
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef CDROM_H
+#define CDROM_H
+
+void cdrom_init(const char *devpath);
+void cdrom_eject(const char *devpath);
+bool cdrom_media_present(const char *devpath);
+
+#endif /* CDROM_H */
+
index 5aa2898b0e55ffdb18a59f802e3e6100ed8fbe88..80029d6820d225f001940d696ec3c67c63ced878 100644 (file)
@@ -22,6 +22,7 @@
 #include "udev.h"
 #include "pb-discover.h"
 #include "device-handler.h"
+#include "cdrom.h"
 
 struct pb_udev {
        struct udev *udev;
@@ -64,7 +65,9 @@ static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
        const char *serial;
        const char *path;
        const char *name;
+       const char *node;
        const char *prop;
+       bool cdrom;
 
        name = udev_device_get_sysname(dev);
        if (!name) {
@@ -83,6 +86,7 @@ static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
                return 0;
        }
 
+       node = udev_device_get_devnode(dev);
        path = udev_device_get_devpath(dev);
        if (path && (strstr(path, "virtual/block/loop")
                        || strstr(path, "virtual/block/ram"))) {
@@ -90,6 +94,17 @@ static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
                return 0;
        }
 
+       cdrom = node && !!udev_device_get_property_value(dev, "ID_CDROM");
+       if (cdrom) {
+               /* CDROMs require a little initialisation, to get
+                * petitboot-compatible tray behaviour */
+               cdrom_init(node);
+               if (!cdrom_media_present(node)) {
+                       pb_debug("SKIP: %s: no media present\n", name);
+                       return 0;
+               }
+       }
+
        /* We have enough info to create the device and start discovery */
        ddev = device_lookup_by_id(udev->handler, name);
        if (ddev) {
@@ -105,7 +120,7 @@ static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
 
        ddev = discover_device_create(udev->handler, name);
 
-       ddev->device_path = talloc_strdup(ddev, udev_device_get_devnode(dev));
+       ddev->device_path = talloc_strdup(ddev, node);
 
        prop = udev_device_get_property_value(dev, "ID_FS_UUID");
        if (prop)
@@ -141,16 +156,80 @@ 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)
+{
+       struct discover_device *ddev;
+       const char *name, *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;
+
+               pb_debug("udev: eject request\n");
+
+               /* 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);
+               }
+
+               if (eject)
+                       cdrom_eject(node);
+
+               return 0;
+       }
+
+       if (udev_device_get_property_value(dev, "DISK_MEDIA_CHANGE")) {
+               if (cdrom_media_present(node))
+                       return udev_handle_dev_add(udev, dev);
+               else
+                       return udev_handle_dev_remove(udev, dev);
+       }
+
+       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));
 
+#ifdef DEBUG
+       {
+               struct udev_list_entry *list;
+               const char *name;
+
+               list = udev_device_get_properties_list_entry(dev);
+               name = udev_device_get_sysname(dev);
+
+               pb_debug("%s: action %s, device %s\n", __func__, action, name);
+               pb_debug("%s properties:\n", __func__);
+
+               for (; list; list = udev_list_entry_get_next(list))
+                       pb_log("\t%-20s: %s\n", udev_list_entry_get_name(list),
+                                       udev_list_entry_get_value(list));
+       } while (0);
+#endif
+
        if (!strcmp(action, "add"))
                return udev_handle_dev_add(udev, dev);
 
        else if (!strcmp(action, "remove"))
                return udev_handle_dev_remove(udev, dev);
 
+       else if (!strcmp(action, "change"))
+               return udev_handle_dev_change(udev, dev);
+
        return 0;
 }