chiark / gitweb /
update NEWS
[elogind.git] / extras / cdrom_id / cdrom_id.c
index acf7a97e416d067a85c978c114ffa9444a0a810d..afb481d1a88fd953af6b4a417ac9631ecb4659be 100644 (file)
@@ -1,21 +1,20 @@
 /*
  * cdrom_id - optical drive and media information prober
  *
- * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
  *
- *     This program is free software; you can redistribute it and/or modify it
- *     under the terms of the GNU General Public License as published by the
- *     Free Software Foundation version 2 of the License.
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
  *
- *     This program is distributed in the hope that it will be useful, but
- *     WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *     General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License along
- *     with this program; if not, write to the Free Software Foundation, Inc.,
- *     675 Mass Ave, Cambridge, MA 02139, USA.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #ifndef _GNU_SOURCE
@@ -31,6 +30,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <getopt.h>
+#include <time.h>
 #include <scsi/sg.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <linux/cdrom.h>
 
-#include "../../udev/udev.h"
-
-#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
-#endif
+#include "libudev.h"
+#include "libudev-private.h"
 
 static int debug;
 
-#ifdef USE_LOG
-void log_message(int priority, const char *format, ...)
+static void log_fn(struct udev *udev, int priority,
+                  const char *file, int line, const char *fn,
+                  const char *format, va_list args)
 {
-       va_list args;
-       static int udev_log = -1;
-
-       if (udev_log == -1) {
-               const char *value;
-
-               value = getenv("UDEV_LOG");
-               if (value)
-                       udev_log = log_priority(value);
-               else
-                       udev_log = LOG_ERR;
-
-               if (debug && udev_log < LOG_INFO)
-                       udev_log = LOG_INFO;
-       }
-
-       if (priority > udev_log)
-               return;
-
-       va_start(args, format);
        if (debug) {
-               fprintf(stderr, "[%d] ", (int) getpid());
+               fprintf(stderr, "%s: ", fn);
                vfprintf(stderr, format, args);
-       } else
+       } else {
                vsyslog(priority, format, args);
-       va_end(args);
+       }
 }
-#endif
 
 /* device info */
-static unsigned int cd_cd_rom;
-static unsigned int cd_cd_r;
-static unsigned int cd_cd_rw;
-static unsigned int cd_dvd_rom;
-static unsigned int cd_dvd_r;
-static unsigned int cd_dvd_rw;
-static unsigned int cd_dvd_ram;
-static unsigned int cd_dvd_plus_r;
-static unsigned int cd_dvd_plus_rw;
-static unsigned int cd_dvd_plus_r_dl;
-static unsigned int cd_dvd_plus_rw_dl;
-static unsigned int cd_bd;
-static unsigned int cd_bd_r;
-static unsigned int cd_bd_re;
-static unsigned int cd_hddvd;
-static unsigned int cd_hddvd_r;
-static unsigned int cd_hddvd_rw;
-static unsigned int cd_mo;
-static unsigned int cd_mrw;
-static unsigned int cd_mrw_w;
+static unsigned int cd_cd_rom = 0;
+static unsigned int cd_cd_r = 0;
+static unsigned int cd_cd_rw = 0;
+static unsigned int cd_dvd_rom = 0;
+static unsigned int cd_dvd_r = 0;
+static unsigned int cd_dvd_rw = 0;
+static unsigned int cd_dvd_ram = 0;
+static unsigned int cd_dvd_plus_r = 0;
+static unsigned int cd_dvd_plus_rw = 0;
+static unsigned int cd_dvd_plus_r_dl = 0;
+static unsigned int cd_dvd_plus_rw_dl = 0;
+static unsigned int cd_bd = 0;
+static unsigned int cd_bd_r = 0;
+static unsigned int cd_bd_re = 0;
+static unsigned int cd_hddvd = 0;
+static unsigned int cd_hddvd_r = 0;
+static unsigned int cd_hddvd_rw = 0;
+static unsigned int cd_mo = 0;
+static unsigned int cd_mrw = 0;
+static unsigned int cd_mrw_w = 0;
 
 /* media info */
-static unsigned int cd_media_cd_rom;
-static unsigned int cd_media_cd_r;
-static unsigned int cd_media_cd_rw;
-static unsigned int cd_media_dvd_rom;
-static unsigned int cd_media_dvd_r;
-static unsigned int cd_media_dvd_rw;
-static unsigned int cd_media_dvd_ram;
-static unsigned int cd_media_dvd_plus_r;
-static unsigned int cd_media_dvd_plus_rw;
-static unsigned int cd_media_dvd_plus_r_dl;
-static unsigned int cd_media_dvd_plus_rw_dl;
-static unsigned int cd_media_bd;
-static unsigned int cd_media_bd_r;
-static unsigned int cd_media_bd_re;
-static unsigned int cd_media_hddvd;
-static unsigned int cd_media_hddvd_r;
-static unsigned int cd_media_hddvd_rw;
-static unsigned int cd_media_mo;
-static unsigned int cd_media_mrw;
-static unsigned int cd_media_mrw_w;
-
-static const char *cd_media_state;
-static unsigned int cd_media_session_next;
-static unsigned int cd_media_session_count;
-static unsigned int cd_media_track_count;
-static unsigned int cd_media_track_count_data;
-static unsigned int cd_media_track_count_audio;
-static unsigned long long int cd_media_session_last_offset;
+static unsigned int cd_media = 0;
+static unsigned int cd_media_cd_rom = 0;
+static unsigned int cd_media_cd_r = 0;
+static unsigned int cd_media_cd_rw = 0;
+static unsigned int cd_media_dvd_rom = 0;
+static unsigned int cd_media_dvd_r = 0;
+static unsigned int cd_media_dvd_rw = 0;
+static unsigned int cd_media_dvd_ram = 0;
+static unsigned int cd_media_dvd_plus_r = 0;
+static unsigned int cd_media_dvd_plus_rw = 0;
+static unsigned int cd_media_dvd_plus_r_dl = 0;
+static unsigned int cd_media_dvd_plus_rw_dl = 0;
+static unsigned int cd_media_bd = 0;
+static unsigned int cd_media_bd_r = 0;
+static unsigned int cd_media_bd_re = 0;
+static unsigned int cd_media_hddvd = 0;
+static unsigned int cd_media_hddvd_r = 0;
+static unsigned int cd_media_hddvd_rw = 0;
+static unsigned int cd_media_mo = 0;
+static unsigned int cd_media_mrw = 0;
+static unsigned int cd_media_mrw_w = 0;
+
+static const char *cd_media_state = NULL;
+static unsigned int cd_media_session_next = 0;
+static unsigned int cd_media_session_count = 0;
+static unsigned int cd_media_track_count = 0;
+static unsigned int cd_media_track_count_data = 0;
+static unsigned int cd_media_track_count_audio = 0;
+static unsigned long long int cd_media_session_last_offset = 0;
 
 #define ERRCODE(s)     ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
 #define SK(errcode)    (((errcode) >> 16) & 0xF)
 #define ASC(errcode)   (((errcode) >> 8) & 0xFF)
 #define ASCQ(errcode)  ((errcode) & 0xFF)
 
-static void info_scsi_cmd_err(const char *cmd, int err)
+static int is_mounted(const char *device)
+{
+       struct stat statbuf;
+       FILE *fp;
+       int maj, min;
+       int mounted = 0;
+
+       if (stat(device, &statbuf) < 0)
+               return -ENODEV;
+
+       fp = fopen("/proc/self/mountinfo", "r");
+       if (fp == NULL)
+               return -ENOSYS;
+       while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
+               if (makedev(maj, min) == statbuf.st_rdev) {
+                       mounted = 1;
+                       break;
+               }
+       }
+       fclose(fp);
+       return mounted;
+}
+
+static void info_scsi_cmd_err(struct udev *udev, char *cmd, int err)
 {
        if (err == -1) {
-               info("%s failed\n", cmd);
+               info(udev, "%s failed\n", cmd);
                return;
        }
-       info("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err));
+       info(udev, "%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err));
 }
 
 struct scsi_cmd {
@@ -153,10 +154,10 @@ struct scsi_cmd {
        struct sg_io_hdr sg_io;
 };
 
-static void scsi_cmd_set(struct scsi_cmd *cmd, size_t i, int arg)
+static void scsi_cmd_set(struct udev *udev, struct scsi_cmd *cmd, size_t i, int arg)
 {
        if (i == 0) {
-               memset(cmd, 0x00, sizeof(struct scsi_cmd));
+               memset(cmd, 0, sizeof(struct scsi_cmd));
                cmd->cgc.quiet = 1;
                cmd->cgc.sense = &cmd->_sense.s;
                memset(&cmd->sg_io, 0, sizeof(cmd->sg_io));
@@ -172,7 +173,7 @@ static void scsi_cmd_set(struct scsi_cmd *cmd, size_t i, int arg)
 
 #define CHECK_CONDITION 0x01
 
-static int scsi_cmd_run(struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
+static int scsi_cmd_run(struct udev *udev, struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
 {
        int ret = 0;
 
@@ -194,57 +195,68 @@ static int scsi_cmd_run(struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t
        return ret;
 }
 
-static int cd_capability_compat(int fd)
+static int cd_capability_compat(struct udev *udev, int fd)
 {
-       int capabilty;
+       int capability;
 
-       capabilty = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
-       if (capabilty < 0) {
-               info("CDROM_GET_CAPABILITY failed\n");
+       capability = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
+       if (capability < 0) {
+               info(udev, "CDROM_GET_CAPABILITY failed\n");
                return -1;
        }
 
-       if (capabilty & CDC_CD_R)
+       if (capability & CDC_CD_R)
                cd_cd_r = 1;
-       if (capabilty & CDC_CD_RW)
+       if (capability & CDC_CD_RW)
                cd_cd_rw = 1;
-       if (capabilty & CDC_DVD)
+       if (capability & CDC_DVD)
                cd_dvd_rom = 1;
-       if (capabilty & CDC_DVD_R)
+       if (capability & CDC_DVD_R)
                cd_dvd_r = 1;
-       if (capabilty & CDC_DVD_RAM)
+       if (capability & CDC_DVD_RAM)
                cd_dvd_ram = 1;
-       if (capabilty & CDC_MRW)
+       if (capability & CDC_MRW)
                cd_mrw = 1;
-       if (capabilty & CDC_MRW_W)
+       if (capability & CDC_MRW_W)
                cd_mrw_w = 1;
        return 0;
 }
 
-static int cd_inquiry(int fd) {
+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];
        int err;
 
-       scsi_cmd_set(&sc, 0, 0x12);
-       scsi_cmd_set(&sc, 4, 36);
-       scsi_cmd_set(&sc, 5, 0);
-       err = scsi_cmd_run(&sc, fd, inq, 36);
+       memset(inq, 0, sizeof(inq));
+       scsi_cmd_set(udev, &sc, 0, 0x12);
+       scsi_cmd_set(udev, &sc, 4, 36);
+       scsi_cmd_set(udev, &sc, 5, 0);
+       err = scsi_cmd_run(udev, &sc, fd, inq, 36);
        if ((err < 0)) {
-               info_scsi_cmd_err("INQUIRY", err);
+               info_scsi_cmd_err(udev, "INQUIRY", err);
                return -1;
        }
 
        if ((inq[0] & 0x1F) != 5) {
-               info("not an MMC unit\n");
+               info(udev, "not an MMC unit\n");
                return -1;
        }
 
-       info("INQUIRY: [%.8s][%.16s][%.4s]\n", inq + 8, inq + 16, inq + 32);
+       info(udev, "INQUIRY: [%.8s][%.16s][%.4s]\n", inq + 8, inq + 16, inq + 32);
        return 0;
 }
 
-static int cd_profiles(int fd)
+static int cd_profiles(struct udev *udev, int fd)
 {
        struct scsi_cmd sc;
        unsigned char header[8];
@@ -254,32 +266,34 @@ static int cd_profiles(int fd)
        unsigned int i;
        int err;
 
-       scsi_cmd_set(&sc, 0, 0x46);
-       scsi_cmd_set(&sc, 1, 0);
-       scsi_cmd_set(&sc, 8, sizeof(header));
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, header, sizeof(header));
+       memset(header, 0, sizeof(header));
+       scsi_cmd_set(udev, &sc, 0, 0x46);
+       scsi_cmd_set(udev, &sc, 1, 0);
+       scsi_cmd_set(udev, &sc, 8, sizeof(header));
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
        if ((err < 0)) {
-               info_scsi_cmd_err("GET CONFIGURATION", err);
+               info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
                return -1;
        }
 
        len = 4 + (header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3]);
-       info("GET CONFIGURATION: number of profiles %i\n", len);
+       info(udev, "GET CONFIGURATION: number of profiles %i\n", len);
        if (len > sizeof(profiles)) {
-               info("invalid number of profiles\n");
+               info(udev, "invalid number of profiles\n");
                return -1;
        }
 
-       scsi_cmd_set(&sc, 0, 0x46);
-       scsi_cmd_set(&sc, 1, 1);
-       scsi_cmd_set(&sc, 6, len >> 16);
-       scsi_cmd_set(&sc, 7, len >> 8);
-       scsi_cmd_set(&sc, 8, len);
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, profiles, len);
+       memset(profiles, 0, sizeof(profiles));
+       scsi_cmd_set(udev, &sc, 0, 0x46);
+       scsi_cmd_set(udev, &sc, 1, 1);
+       scsi_cmd_set(udev, &sc, 6, len >> 16);
+       scsi_cmd_set(udev, &sc, 7, len >> 8);
+       scsi_cmd_set(udev, &sc, 8, len);
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, profiles, len);
        if ((err < 0)) {
-               info_scsi_cmd_err("GET CONFIGURATION", err);
+               info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
                return -1;
        }
 
@@ -288,7 +302,7 @@ static int cd_profiles(int fd)
                unsigned int profile = (profiles[i] << 8 | profiles[i + 1]);
                if (profile == 0)
                        continue;
-               info("profile 0x%02x\n", profile);
+               info(udev, "profile 0x%02x\n", profile);
 
                switch (profile) {
                case 0x03:
@@ -344,12 +358,14 @@ static int cd_profiles(int fd)
 
        /* current media profile */
        cur_profile = header[6] << 8 | header[7];
-       info("current profile 0x%02x\n", cur_profile);
+       info(udev, "current profile 0x%02x\n", cur_profile);
        if (cur_profile == 0) {
-               info("no current profile, assuming no media\n");
+               info(udev, "no current profile, assuming no media\n");
                return -1;
        }
 
+       cd_media = 1;
+
        switch (cur_profile) {
        case 0x03:
        case 0x04:
@@ -415,7 +431,7 @@ static int cd_profiles(int fd)
        return 0;
 }
 
-static int cd_media_info(int fd)
+static int cd_media_info(struct udev *udev, int fd)
 {
        struct scsi_cmd sc;
        unsigned char header[32];
@@ -427,19 +443,22 @@ static int cd_media_info(int fd)
        };
        int err;
 
-       scsi_cmd_set(&sc, 0, 0x51);
-       scsi_cmd_set(&sc, 8, sizeof(header));
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, header, sizeof(header));
+       memset(header, 0, sizeof(header));
+       scsi_cmd_set(udev, &sc, 0, 0x51);
+       scsi_cmd_set(udev, &sc, 8, sizeof(header));
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
        if ((err < 0)) {
-               info_scsi_cmd_err("READ DISC INFORMATION", err);
+               info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
                return -1;
        };
 
-       info("disk type %02x\n", header[8]);
+       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];
@@ -448,7 +467,7 @@ static int cd_media_info(int fd)
        return 0;
 }
 
-static int cd_media_toc(int fd)
+static int cd_media_toc(struct udev *udev, int fd)
 {
        struct scsi_cmd sc;
        unsigned char header[12];
@@ -457,18 +476,19 @@ static int cd_media_toc(int fd)
        unsigned char *p;
        int err;
 
-       scsi_cmd_set(&sc, 0, 0x43);
-       scsi_cmd_set(&sc, 6, 1);
-       scsi_cmd_set(&sc, 8, sizeof(header));
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, header, sizeof(header));
+       memset(header, 0, sizeof(header));
+       scsi_cmd_set(udev, &sc, 0, 0x43);
+       scsi_cmd_set(udev, &sc, 6, 1);
+       scsi_cmd_set(udev, &sc, 8, sizeof(header));
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
        if ((err < 0)) {
-               info_scsi_cmd_err("READ TOC", err);
+               info_scsi_cmd_err(udev, "READ TOC", err);
                return -1;
        }
 
        len = (header[0] << 8 | header[1]) + 2;
-       info("READ TOC: len: %d\n", len);
+       info(udev, "READ TOC: len: %d\n", len);
        if (len > sizeof(toc))
                return -1;
        if (len < 2)
@@ -478,14 +498,15 @@ static int cd_media_toc(int fd)
        if (len < 8)
                return 0;
 
-       scsi_cmd_set(&sc, 0, 0x43);
-       scsi_cmd_set(&sc, 6, header[2]); /* First Track/Session Number */
-       scsi_cmd_set(&sc, 7, len >> 8);
-       scsi_cmd_set(&sc, 8, len);
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, toc, len);
+       memset(toc, 0, sizeof(toc));
+       scsi_cmd_set(udev, &sc, 0, 0x43);
+       scsi_cmd_set(udev, &sc, 6, header[2]); /* First Track/Session Number */
+       scsi_cmd_set(udev, &sc, 7, len >> 8);
+       scsi_cmd_set(udev, &sc, 8, len);
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, toc, len);
        if ((err < 0)) {
-               info_scsi_cmd_err("READ TOC (tracks)", err);
+               info_scsi_cmd_err(udev, "READ TOC (tracks)", err);
                return -1;
        }
 
@@ -496,7 +517,7 @@ static int cd_media_toc(int fd)
                is_data_track = (p[1] & 0x04) != 0;
 
                block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
-               info("track=%u info=0x%x(%s) start_block=%u\n",
+               info(udev, "track=%u info=0x%x(%s) start_block=%u\n",
                     p[2], p[1] & 0x0f, is_data_track ? "data":"audio", block);
 
                if (is_data_track)
@@ -505,35 +526,43 @@ static int cd_media_toc(int fd)
                        cd_media_track_count_audio++;
        }
 
-       scsi_cmd_set(&sc, 0, 0x43);
-       scsi_cmd_set(&sc, 2, 1); /* Session Info */
-       scsi_cmd_set(&sc, 8, 12);
-       scsi_cmd_set(&sc, 9, 0);
-       err = scsi_cmd_run(&sc, fd, header, sizeof(header));
+       memset(header, 0, sizeof (header));
+       scsi_cmd_set(udev, &sc, 0, 0x43);
+       scsi_cmd_set(udev, &sc, 2, 1); /* Session Info */
+       scsi_cmd_set(udev, &sc, 8, 12);
+       scsi_cmd_set(udev, &sc, 9, 0);
+       err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
        if ((err < 0)) {
-               info_scsi_cmd_err("READ TOC (multi session)", err);
+               info_scsi_cmd_err(udev, "READ TOC (multi session)", err);
                return -1;
        }
        len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7];
-       info("last track %u starts at block %u\n", header[4+2], len);
+       info(udev, "last track %u starts at block %u\n", header[4+2], len);
        cd_media_session_last_offset = (unsigned long long int)len * 2048;
        return 0;
 }
 
 int main(int argc, char *argv[])
 {
+       struct udev *udev;
        static const struct option options[] = {
-               { "export", 0, NULL, 'x' },
-               { "debug", 0, NULL, 'd' },
-               { "help", 0, NULL, 'h' },
+               { "export", no_argument, NULL, 'x' },
+               { "debug", no_argument, NULL, 'd' },
+               { "help", no_argument, NULL, 'h' },
                {}
        };
        const char *node = NULL;
        int export = 0;
        int fd = -1;
+       int cnt;
        int rc = 0;
 
-       logging_init("cdrom_id");
+       udev = udev_new();
+       if (udev == NULL)
+               goto exit;
+
+       udev_log_init("cdrom_id");
+       udev_set_log_fn(udev, log_fn);
 
        while (1) {
                int option;
@@ -545,6 +574,8 @@ int main(int argc, char *argv[])
                switch (option) {
                case 'd':
                        debug = 1;
+                       if (udev_get_log_priority(udev) < LOG_INFO)
+                               udev_set_log_priority(udev, LOG_INFO);
                        break;
                case 'x':
                        export = 1;
@@ -563,42 +594,55 @@ int main(int argc, char *argv[])
 
        node = argv[optind];
        if (!node) {
-               err("no device\n");
+               err(udev, "no device\n");
                fprintf(stderr, "no device\n");
                rc = 1;
                goto exit;
        }
 
-       fd = open(node, O_RDONLY | O_NONBLOCK);
+       srand((unsigned int)getpid());
+       for (cnt = 20; cnt > 0; cnt--) {
+               struct timespec duration;
+
+               fd = open(node, O_RDONLY|O_NONBLOCK|(is_mounted(node) ? 0 : O_EXCL));
+               if (fd >= 0 || errno != EBUSY)
+                       break;
+               duration.tv_sec = 0;
+               duration.tv_nsec = (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
+               nanosleep(&duration, NULL);
+       }
        if (fd < 0) {
-               info("unable to open '%s'\n", node);
+               info(udev, "unable to open '%s'\n", node);
+               fprintf(stderr, "unable to open '%s'\n", node);
                rc = 1;
                goto exit;
        }
-       info("probing: '%s'\n", node);
+       info(udev, "probing: '%s'\n", node);
 
        /* same data as original cdrom_id */
-       if (cd_capability_compat(fd) < 0) {
+       if (cd_capability_compat(udev, fd) < 0) {
                rc = 1;
                goto exit;
        }
 
-       /* check drive */
-       if (cd_inquiry(fd) < 0) {
-               rc = 2;
-               goto exit;
-       }
+       /* check for media - don't bail if there's no media as we still need to
+         * to read profiles */
+       cd_media_compat(udev, fd);
+
+       /* check if drive talks MMC */
+       if (cd_inquiry(udev, fd) < 0)
+               goto print;
 
        /* read drive and possibly current profile */
-       if (cd_profiles(fd) < 0)
+       if (cd_profiles(udev, fd) < 0)
                goto print;
 
        /* get session/track info */
-       if (cd_media_toc(fd) < 0)
+       if (cd_media_toc(udev, fd) < 0)
                goto print;
 
        /* get writable media state */
-       if (cd_media_info(fd) < 0)
+       if (cd_media_info(udev, fd) < 0)
                goto print;
 
 print:
@@ -644,6 +688,8 @@ print:
        if (cd_mrw_w)
                printf("ID_CDROM_MRW_W=1\n");
 
+       if (cd_media)
+               printf("ID_CDROM_MEDIA=1\n");
        if (cd_media_mo)
                printf("ID_CDROM_MEDIA_MO=1\n");
        if (cd_media_mrw)
@@ -693,16 +739,17 @@ print:
                printf("ID_CDROM_MEDIA_SESSION_COUNT=%d\n", cd_media_session_count);
        if (cd_media_track_count > 0)
                printf("ID_CDROM_MEDIA_TRACK_COUNT=%d\n", cd_media_track_count);
-       if (cd_media_track_count_audio)
+       if (cd_media_track_count_audio > 0)
                printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%d\n", cd_media_track_count_audio);
-       if (cd_media_track_count_data)
+       if (cd_media_track_count_data > 0)
                printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%d\n", cd_media_track_count_data);
-       if (cd_media_session_last_offset)
+       if (cd_media_session_last_offset > 0)
                printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset);
 exit:
        if (fd >= 0)
                close(fd);
-       logging_close();
+       udev_unref(udev);
+       udev_log_close();
        return rc;
 }