3319c35
From 78a27b6a8f394c5a987c31aaf308e11556280f6a Mon Sep 17 00:00:00 2001
3319c35
From: Cole Robinson <crobinso@redhat.com>
3319c35
Date: Thu, 14 Jan 2010 16:19:40 +0000
3319c35
Subject: [PATCH] raw-posix: Detect CDROM via ioctl on linux
3319c35
3319c35
Current CDROM detection is hardcoded based on source file name.
3319c35
Make this smarter on linux by attempting a CDROM specific ioctl.
3319c35
3319c35
This makes '-cdrom /dev/sr0' succeed with no media present.
3319c35
3319c35
v2:
3319c35
    Give ioctl check higher priority than filename check.
3319c35
3319c35
v3:
3319c35
    Actually initialize 'prio' variable.
3319c35
    Check for ioctl success rather than absence of specific failure.
3319c35
3319c35
v4:
3319c35
    Explicitly mention that change is linux specific.
3319c35
3319c35
Signed-off-by: Cole Robinson <crobinso@redhat.com>
3319c35
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
3319c35
---
3319c35
 block/raw-posix.c |   20 ++++++++++++++++++--
3319c35
 1 files changed, 18 insertions(+), 2 deletions(-)
3319c35
3319c35
diff --git a/block/raw-posix.c b/block/raw-posix.c
3319c35
index c204cf9..1c777a1 100644
3319c35
--- a/block/raw-posix.c
3319c35
+++ b/block/raw-posix.c
3319c35
@@ -1142,9 +1142,25 @@ static int cdrom_open(BlockDriverState *bs, const char *filename, int flags)
3319c35
3319c35
 static int cdrom_probe_device(const char *filename)
3319c35
 {
3319c35
+    int fd, ret;
3319c35
+    int prio = 0;
3319c35
+
3319c35
     if (strstart(filename, "/dev/cd", NULL))
3319c35
-        return 100;
3319c35
-    return 0;
3319c35
+        prio = 50;
3319c35
+
3319c35
+    fd = open(filename, O_RDONLY | O_NONBLOCK);
3319c35
+    if (fd < 0) {
3319c35
+        goto out;
3319c35
+    }
3319c35
+
3319c35
+    /* Attempt to detect via a CDROM specific ioctl */
3319c35
+    ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
3319c35
+    if (ret >= 0)
3319c35
+        prio = 100;
3319c35
+
3319c35
+    close(fd);
3319c35
+out:
3319c35
+    return prio;
3319c35
 }
3319c35
3319c35
 static int cdrom_is_inserted(BlockDriverState *bs)
3319c35
-- 
3319c35
1.6.6.1
3319c35