chiark / gitweb /
move syslog wrapper to libudev
[elogind.git] / extras / cdrom_id / cdrom_id.c
index 371d6a2dbd197898c69d381d2cee4fe5267662b5..d4156ce7ac310586f90e5c28ef924353d3bb9f6a 100644 (file)
@@ -37,7 +37,8 @@
 #include <sys/ioctl.h>
 #include <linux/cdrom.h>
 
-#include "../../udev/udev.h"
+#include "libudev.h"
+#include "libudev-private.h"
 
 static int debug;
 
@@ -197,6 +198,16 @@ static int cd_capability_compat(struct udev *udev, int fd)
        return 0;
 }
 
+static int cd_media_compat(struct udev *udev, int fd)
+{
+       if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK) {
+               info(udev, "CDROM_DRIVE_STATUS != CDS_DISC_OK\n");
+               return -1;
+       }
+       cd_media = 1;
+       return 0;
+}
+
 static int cd_inquiry(struct udev *udev, int fd) {
        struct scsi_cmd sc;
        unsigned char inq[128];
@@ -416,8 +427,10 @@ static int cd_media_info(struct udev *udev, int fd)
 
        info(udev, "disk type %02x\n", header[8]);
 
-       if ((header[2] & 3) < 4)
+       /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
+       if (!cd_media_cd_rom && (header[2] & 3) < 4)
                cd_media_state = media_status[header[2] & 3];
+
        if ((header[2] & 3) != 2)
                cd_media_session_next = header[10] << 8 | header[5];
        cd_media_session_count = header[9] << 8 | header[4];
@@ -516,7 +529,7 @@ int main(int argc, char *argv[])
        if (udev == NULL)
                goto exit;
 
-       logging_init("cdrom_id");
+       udev_log_init("cdrom_id");
        udev_set_log_fn(udev, log_fn);
 
        while (1) {
@@ -558,6 +571,7 @@ int main(int argc, char *argv[])
        fd = open(node, O_RDONLY | O_NONBLOCK);
        if (fd < 0) {
                info(udev, "unable to open '%s'\n", node);
+               fprintf(stderr, "unable to open '%s'\n", node);
                rc = 1;
                goto exit;
        }
@@ -569,7 +583,11 @@ int main(int argc, char *argv[])
                goto exit;
        }
 
-       /* check drive */
+       /* check for media */
+       if (cd_media_compat(udev, fd) < 0)
+               goto print;
+
+       /* check if drive talks MMC */
        if (cd_inquiry(udev, fd) < 0)
                goto print;
 
@@ -689,7 +707,7 @@ exit:
        if (fd >= 0)
                close(fd);
        udev_unref(udev);
-       logging_close();
+       udev_log_close();
        return rc;
 }