summaryrefslogtreecommitdiffstats
path: root/discover
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2013-11-29 10:47:10 +0800
committerJeremy Kerr <jk@ozlabs.org>2013-12-02 17:00:16 +0800
commit9de5d31f93dab36cfb451e9ad724d47311b54424 (patch)
tree47dbc8efa3f62da140d6edc045fdebf2aa73ac1c /discover
parentf4d1abf7d2d066ee183d9cd588f336dc63f0da00 (diff)
downloadtalos-petitboot-9de5d31f93dab36cfb451e9ad724d47311b54424.tar.gz
talos-petitboot-9de5d31f93dab36cfb451e9ad724d47311b54424.zip
discover: Fix CDROM handling
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>
Diffstat (limited to 'discover')
-rw-r--r--discover/Makefile.am2
-rw-r--r--discover/cdrom.c81
-rw-r--r--discover/cdrom.h9
-rw-r--r--discover/udev.c81
4 files changed, 172 insertions, 1 deletions
diff --git a/discover/Makefile.am b/discover/Makefile.am
index 2a7885d..84c0436 100644
--- a/discover/Makefile.am
+++ b/discover/Makefile.am
@@ -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
index 0000000..0e88301
--- /dev/null
+++ b/discover/cdrom.c
@@ -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
index 0000000..2e9de06
--- /dev/null
+++ b/discover/cdrom.h
@@ -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 */
+
diff --git a/discover/udev.c b/discover/udev.c
index 5aa2898..80029d6 100644
--- a/discover/udev.c
+++ b/discover/udev.c
@@ -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;
}
OpenPOWER on IntegriCloud