chiark / gitweb /
update NEWS
[elogind.git] / extras / cdrom_id / cdrom_id.c
index 7c9f8cc73eb3fee57322b822bd5ac30cef9dca97..afb481d1a88fd953af6b4a417ac9631ecb4659be 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * 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
@@ -56,57 +56,57 @@ static void log_fn(struct udev *udev, int priority,
 }
 
 /* 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;
-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)
@@ -157,7 +157,7 @@ struct scsi_cmd {
 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));
@@ -237,6 +237,7 @@ static int cd_inquiry(struct udev *udev, int fd) {
        unsigned char inq[128];
        int err;
 
+       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);
@@ -265,6 +266,7 @@ static int cd_profiles(struct udev *udev, int fd)
        unsigned int i;
        int err;
 
+       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));
@@ -282,6 +284,7 @@ static int cd_profiles(struct udev *udev, int fd)
                return -1;
        }
 
+       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);
@@ -440,6 +443,7 @@ static int cd_media_info(struct udev *udev, int fd)
        };
        int err;
 
+       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);
@@ -472,6 +476,7 @@ static int cd_media_toc(struct udev *udev, int fd)
        unsigned char *p;
        int err;
 
+       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));
@@ -493,6 +498,7 @@ static int cd_media_toc(struct udev *udev, int fd)
        if (len < 8)
                return 0;
 
+       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);
@@ -520,6 +526,7 @@ static int cd_media_toc(struct udev *udev, int fd)
                        cd_media_track_count_audio++;
        }
 
+       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);
@@ -547,6 +554,7 @@ int main(int argc, char *argv[])
        const char *node = NULL;
        int export = 0;
        int fd = -1;
+       int cnt;
        int rc = 0;
 
        udev = udev_new();
@@ -592,21 +600,16 @@ int main(int argc, char *argv[])
                goto exit;
        }
 
-       if (is_mounted(node)) {
-               fd = open(node, O_RDONLY|O_NONBLOCK);
-       } else {
-               int cnt;
+       srand((unsigned int)getpid());
+       for (cnt = 20; cnt > 0; cnt--) {
                struct timespec duration;
 
-               srand((unsigned int)getpid());
-               for (cnt = 40; cnt > 0; cnt--) {
-                       fd = open(node, O_RDONLY|O_NONBLOCK|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);
-               }
+               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(udev, "unable to open '%s'\n", node);