2 * cdrom_id - optical drive and media information prober
4 * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <linux/cdrom.h>
42 #include "libudev-private.h"
46 static void log_fn(struct udev *udev, int priority,
47 const char *file, int line, const char *fn,
48 const char *format, va_list args)
51 fprintf(stderr, "%s: ", fn);
52 vfprintf(stderr, format, args);
54 vsyslog(priority, format, args);
59 static unsigned int cd_cd_rom = 0;
60 static unsigned int cd_cd_r = 0;
61 static unsigned int cd_cd_rw = 0;
62 static unsigned int cd_dvd_rom = 0;
63 static unsigned int cd_dvd_r = 0;
64 static unsigned int cd_dvd_rw = 0;
65 static unsigned int cd_dvd_ram = 0;
66 static unsigned int cd_dvd_plus_r = 0;
67 static unsigned int cd_dvd_plus_rw = 0;
68 static unsigned int cd_dvd_plus_r_dl = 0;
69 static unsigned int cd_dvd_plus_rw_dl = 0;
70 static unsigned int cd_bd = 0;
71 static unsigned int cd_bd_r = 0;
72 static unsigned int cd_bd_re = 0;
73 static unsigned int cd_hddvd = 0;
74 static unsigned int cd_hddvd_r = 0;
75 static unsigned int cd_hddvd_rw = 0;
76 static unsigned int cd_mo = 0;
77 static unsigned int cd_mrw = 0;
78 static unsigned int cd_mrw_w = 0;
81 static unsigned int cd_media = 0;
82 static unsigned int cd_media_cd_rom = 0;
83 static unsigned int cd_media_cd_r = 0;
84 static unsigned int cd_media_cd_rw = 0;
85 static unsigned int cd_media_dvd_rom = 0;
86 static unsigned int cd_media_dvd_r = 0;
87 static unsigned int cd_media_dvd_rw = 0;
88 static unsigned int cd_media_dvd_rw_ro = 0; /* restricted overwrite mode */
89 static unsigned int cd_media_dvd_rw_seq = 0; /* sequential mode */
90 static unsigned int cd_media_dvd_ram = 0;
91 static unsigned int cd_media_dvd_plus_r = 0;
92 static unsigned int cd_media_dvd_plus_rw = 0;
93 static unsigned int cd_media_dvd_plus_r_dl = 0;
94 static unsigned int cd_media_dvd_plus_rw_dl = 0;
95 static unsigned int cd_media_bd = 0;
96 static unsigned int cd_media_bd_r = 0;
97 static unsigned int cd_media_bd_re = 0;
98 static unsigned int cd_media_hddvd = 0;
99 static unsigned int cd_media_hddvd_r = 0;
100 static unsigned int cd_media_hddvd_rw = 0;
101 static unsigned int cd_media_mo = 0;
102 static unsigned int cd_media_mrw = 0;
103 static unsigned int cd_media_mrw_w = 0;
105 static const char *cd_media_state = NULL;
106 static unsigned int cd_media_session_next = 0;
107 static unsigned int cd_media_session_count = 0;
108 static unsigned int cd_media_track_count = 0;
109 static unsigned int cd_media_track_count_data = 0;
110 static unsigned int cd_media_track_count_audio = 0;
111 static unsigned long long int cd_media_session_last_offset = 0;
113 #define ERRCODE(s) ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
114 #define SK(errcode) (((errcode) >> 16) & 0xF)
115 #define ASC(errcode) (((errcode) >> 8) & 0xFF)
116 #define ASCQ(errcode) ((errcode) & 0xFF)
118 static int is_mounted(const char *device)
125 if (stat(device, &statbuf) < 0)
128 fp = fopen("/proc/self/mountinfo", "r");
131 while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
132 if (makedev(maj, min) == statbuf.st_rdev) {
141 static void info_scsi_cmd_err(struct udev *udev, char *cmd, int err)
144 info(udev, "%s failed\n", cmd);
147 info(udev, "%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err));
151 struct cdrom_generic_command cgc;
153 struct request_sense s;
156 struct sg_io_hdr sg_io;
159 static void scsi_cmd_init(struct udev *udev, struct scsi_cmd *cmd, unsigned char *buf, size_t bufsize)
161 memset(cmd, 0x00, sizeof(struct scsi_cmd));
162 memset(buf, 0x00, bufsize);
164 cmd->cgc.sense = &cmd->_sense.s;
165 memset(&cmd->sg_io, 0, sizeof(cmd->sg_io));
166 cmd->sg_io.interface_id = 'S';
167 cmd->sg_io.mx_sb_len = sizeof(cmd->_sense);
168 cmd->sg_io.cmdp = cmd->cgc.cmd;
169 cmd->sg_io.sbp = cmd->_sense.u;
170 cmd->sg_io.flags = SG_FLAG_LUN_INHIBIT | SG_FLAG_DIRECT_IO;
173 static void scsi_cmd_set(struct udev *udev, struct scsi_cmd *cmd, size_t i, unsigned char arg)
175 cmd->sg_io.cmd_len = i + 1;
176 cmd->cgc.cmd[i] = arg;
179 #define CHECK_CONDITION 0x01
181 static int scsi_cmd_run(struct udev *udev, struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
185 cmd->sg_io.dxferp = buf;
186 cmd->sg_io.dxfer_len = bufsize;
187 cmd->sg_io.dxfer_direction = SG_DXFER_FROM_DEV;
188 if (ioctl(fd, SG_IO, &cmd->sg_io))
191 if ((cmd->sg_io.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
194 if (cmd->sg_io.masked_status & CHECK_CONDITION) {
195 ret = ERRCODE(cmd->_sense.u);
203 static int cd_capability_compat(struct udev *udev, int fd)
207 capability = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
208 if (capability < 0) {
209 info(udev, "CDROM_GET_CAPABILITY failed\n");
213 if (capability & CDC_CD_R)
215 if (capability & CDC_CD_RW)
217 if (capability & CDC_DVD)
219 if (capability & CDC_DVD_R)
221 if (capability & CDC_DVD_RAM)
223 if (capability & CDC_MRW)
225 if (capability & CDC_MRW_W)
230 static int cd_media_compat(struct udev *udev, int fd)
232 if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK) {
233 info(udev, "CDROM_DRIVE_STATUS != CDS_DISC_OK\n");
240 static int cd_inquiry(struct udev *udev, int fd) {
242 unsigned char inq[128];
245 scsi_cmd_init(udev, &sc, inq, sizeof(inq));
246 scsi_cmd_set(udev, &sc, 0, 0x12);
247 scsi_cmd_set(udev, &sc, 4, 36);
248 scsi_cmd_set(udev, &sc, 5, 0);
249 err = scsi_cmd_run(udev, &sc, fd, inq, 36);
251 info_scsi_cmd_err(udev, "INQUIRY", err);
255 if ((inq[0] & 0x1F) != 5) {
256 info(udev, "not an MMC unit\n");
260 info(udev, "INQUIRY: [%.8s][%.16s][%.4s]\n", inq + 8, inq + 16, inq + 32);
264 static int feature_profiles(struct udev *udev, const unsigned char *profiles, size_t size)
268 for (i = 0; i+4 <= size; i += 4) {
271 profile = profiles[i] << 8 | profiles[i+1];
276 info(udev, "profile 0x%02x mo\n", profile);
280 info(udev, "profile 0x%02x cd_rom\n", profile);
284 info(udev, "profile 0x%02x cd_r\n", profile);
288 info(udev, "profile 0x%02x cd_rw\n", profile);
292 info(udev, "profile 0x%02x dvd_rom\n", profile);
296 info(udev, "profile 0x%02x dvd_ram\n", profile);
301 info(udev, "profile 0x%02x dvd_rw\n", profile);
305 info(udev, "profile 0x%02x dvd_plus_r\n", profile);
309 info(udev, "profile 0x%02x dvd_plus_rw\n", profile);
313 info(udev, "profile 0x%02x dvd_plus_rw_dl\n", profile);
314 cd_dvd_plus_rw_dl = 1;
317 info(udev, "profile 0x%02x dvd_plus_r_dl\n", profile);
318 cd_dvd_plus_r_dl = 1;
322 info(udev, "profile 0x%02x bd\n", profile);
327 info(udev, "profile 0x%02x bd_r\n", profile);
331 info(udev, "profile 0x%02x bd_re\n", profile);
335 info(udev, "profile 0x%02x hddvd\n", profile);
339 info(udev, "profile 0x%02x hddvd_r\n", profile);
343 info(udev, "profile 0x%02x hddvd_rw\n", profile);
346 info(udev, "profile 0x%02x <ignored>\n", profile);
353 static int cd_profiles_broken_mmc(struct udev *udev, int fd)
356 unsigned char buffer[16 * 2048];
359 /* we land here if both the standard and the old MMC commands failed;
360 * this might happen on broken wanna-be-CD USB drives like the IronKey.
361 * As a last resort, try to read data to see whether it has a medium.
363 scsi_cmd_init(udev, &sc, buffer, sizeof(buffer));
364 scsi_cmd_set(udev, &sc, 0, 0x28);
365 scsi_cmd_set(udev, &sc, 5, 0);
366 scsi_cmd_set(udev, &sc, 8, 16);
367 scsi_cmd_set(udev, &sc, 9, 0);
368 err = scsi_cmd_run(udev, &sc, fd, buffer, sizeof(buffer));
370 info(udev, "READ DATA failed, assuming no medium\n");
374 /* that's all we can claim if we can read data */
375 info(udev, "READ DATA succeeded, assuming CD-ROM medium\n");
381 static int cd_profiles_old_mmc(struct udev *udev, int fd)
386 unsigned char header[32];
388 scsi_cmd_init(udev, &sc, header, sizeof(header));
389 scsi_cmd_set(udev, &sc, 0, 0x51);
390 scsi_cmd_set(udev, &sc, 8, sizeof(header));
391 scsi_cmd_set(udev, &sc, 9, 0);
392 err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
394 info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
395 info(udev, "no current profile, probing readability of medium\n");
396 return cd_profiles_broken_mmc(udev, fd);
401 if (header[2] & 16) {
403 info(udev, "profile 0x0a media_cd_rw\n");
404 } else if ((header[2] & 3) < 2 && cd_cd_r) {
406 info(udev, "profile 0x09 media_cd_r\n");
409 info(udev, "profile 0x08 media_cd_rom\n");
414 static int cd_profiles(struct udev *udev, int fd)
417 unsigned char features[65530];
418 unsigned int cur_profile = 0;
423 scsi_cmd_init(udev, &sc, features, sizeof(features));
424 scsi_cmd_set(udev, &sc, 0, 0x46);
425 scsi_cmd_set(udev, &sc, 7, (sizeof(features) >> 8) & 0xff);
426 scsi_cmd_set(udev, &sc, 8, sizeof(features) & 0xff);
427 scsi_cmd_set(udev, &sc, 9, 0);
428 err = scsi_cmd_run(udev, &sc, fd, features, sizeof(features));
430 info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
431 /* handle pre-MMC2 drives which do not support GET CONFIGURATION */
432 if (SK(err) == 0x5 && ASC(err) == 0x20) {
433 info(udev, "drive is pre-MMC2 and does not support 46h get configuration command\n");
434 info(udev, "trying to work around the problem\n");
435 return cd_profiles_old_mmc(udev, fd);
440 len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
441 info(udev, "GET CONFIGURATION: size of features buffer 0x%04x\n", len);
443 if (len > sizeof(features)) {
444 info(udev, "can not get features in a single query, truncating\n");
445 len = sizeof(features);
448 /* device features */
449 for (i = 8; i+4 < len; i += (4 + features[i+3])) {
450 unsigned int feature;
452 feature = features[i] << 8 | features[i+1];
456 info(udev, "GET CONFIGURATION: feature 'profiles', with %i entries\n", features[i+3] / 4);
457 feature_profiles(udev, &features[i]+4, features[i+3]);
460 info(udev, "GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes\n", feature, features[i+3]);
465 cur_profile = features[6] << 8 | features[7];
466 if (cur_profile > 0) {
467 info(udev, "current profile 0x%02x\n", cur_profile);
469 info(udev, "no current profile, assuming no media\n");
473 switch (cur_profile) {
477 info(udev, "profile 0x%02x \n", cur_profile);
482 info(udev, "profile 0x%02x media_cd_rom\n", cur_profile);
487 info(udev, "profile 0x%02x media_cd_r\n", cur_profile);
492 info(udev, "profile 0x%02x media_cd_rw\n", cur_profile);
497 info(udev, "profile 0x%02x media_dvd_ro\n", cur_profile);
499 cd_media_dvd_rom = 1;
502 info(udev, "profile 0x%02x media_dvd_r\n", cur_profile);
507 info(udev, "profile 0x%02x media_dvd_ram\n", cur_profile);
509 cd_media_dvd_ram = 1;
512 info(udev, "profile 0x%02x media_dvd_rw_ro\n", cur_profile);
515 cd_media_dvd_rw_ro = 1;
518 info(udev, "profile 0x%02x media_dvd_rw_seq\n", cur_profile);
521 cd_media_dvd_rw_seq = 1;
524 info(udev, "profile 0x%02x media_dvd_plus_r\n", cur_profile);
526 cd_media_dvd_plus_r = 1;
529 info(udev, "profile 0x%02x media_dvd_plus_rw\n", cur_profile);
531 cd_media_dvd_plus_rw = 1;
534 info(udev, "profile 0x%02x media_dvd_plus_rw_dl\n", cur_profile);
536 cd_media_dvd_plus_rw_dl = 1;
539 info(udev, "profile 0x%02x media_dvd_plus_r_dl\n", cur_profile);
541 cd_media_dvd_plus_r_dl = 1;
544 info(udev, "profile 0x%02x media_bd\n", cur_profile);
550 info(udev, "profile 0x%02x media_bd_r\n", cur_profile);
555 info(udev, "profile 0x%02x media_bd_re\n", cur_profile);
560 info(udev, "profile 0x%02x media_hddvd\n", cur_profile);
565 info(udev, "profile 0x%02x media_hddvd_r\n", cur_profile);
567 cd_media_hddvd_r = 1;
570 info(udev, "profile 0x%02x media_hddvd_rw\n", cur_profile);
572 cd_media_hddvd_rw = 1;
575 info(udev, "profile 0x%02x <ignored>\n", cur_profile);
581 static int cd_media_info(struct udev *udev, int fd)
584 unsigned char header[32];
585 static const char *media_status[] = {
593 scsi_cmd_init(udev, &sc, header, sizeof(header));
594 scsi_cmd_set(udev, &sc, 0, 0x51);
595 scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
596 scsi_cmd_set(udev, &sc, 9, 0);
597 err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
599 info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
604 info(udev, "disk type %02x\n", header[8]);
605 info(udev, "hardware reported media status: %s\n", media_status[header[2] & 3]);
607 /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
608 if (!cd_media_cd_rom)
609 cd_media_state = media_status[header[2] & 3];
611 /* fresh DVD-RW in restricted overwite mode reports itself as
612 * "appendable"; change it to "blank" to make it consistent with what
613 * gets reported after blanking, and what userspace expects */
614 if (cd_media_dvd_rw_ro && (header[2] & 3) == 1)
615 cd_media_state = media_status[0];
617 /* DVD+RW discs (and DVD-RW in restricted mode) once formatted are
618 * always "complete", DVD-RAM are "other" or "complete" if the disc is
619 * write protected; we need to check the contents if it is blank */
620 if ((cd_media_dvd_rw_ro || cd_media_dvd_plus_rw || cd_media_dvd_plus_rw_dl || cd_media_dvd_ram) && (header[2] & 3) > 1) {
621 unsigned char buffer[32 * 2048];
622 unsigned char result, len;
625 if (cd_media_dvd_ram) {
626 /* a write protected dvd-ram may report "complete" status */
628 unsigned char dvdstruct[8];
629 unsigned char format[12];
631 scsi_cmd_init(udev, &sc, dvdstruct, sizeof(dvdstruct));
632 scsi_cmd_set(udev, &sc, 0, 0xAD);
633 scsi_cmd_set(udev, &sc, 7, 0xC0);
634 scsi_cmd_set(udev, &sc, 9, sizeof(dvdstruct));
635 scsi_cmd_set(udev, &sc, 11, 0);
636 err = scsi_cmd_run(udev, &sc, fd, dvdstruct, sizeof(dvdstruct));
638 info_scsi_cmd_err(udev, "READ DVD STRUCTURE", err);
641 if (dvdstruct[4] & 0x02) {
642 cd_media_state = media_status[2];
643 info(udev, "write-protected DVD-RAM media inserted\n");
647 /* let's make sure we don't try to read unformatted media */
648 scsi_cmd_init(udev, &sc, format, sizeof(format));
649 scsi_cmd_set(udev, &sc, 0, 0x23);
650 scsi_cmd_set(udev, &sc, 8, sizeof(format));
651 scsi_cmd_set(udev, &sc, 9, 0);
652 err = scsi_cmd_run(udev, &sc, fd, format, sizeof(format));
654 info_scsi_cmd_err(udev, "READ DVD FORMAT CAPACITIES", err);
659 if (len & 7 || len < 16) {
660 info(udev, "invalid format capacities length\n");
664 switch(format[8] & 3) {
666 info(udev, "unformatted DVD-RAM media inserted\n");
667 /* This means that last format was interrupted
668 * or failed, blank dvd-ram discs are factory
669 * formatted. Take no action here as it takes
670 * quite a while to reformat a dvd-ram and it's
671 * not automatically started */
675 info(udev, "formatted DVD-RAM media inserted\n");
679 cd_media = 0; //return no media
680 info(udev, "format capacities returned no media\n");
685 /* Take a closer look at formatted media (unformatted DVD+RW
686 * has "blank" status", DVD-RAM was examined earlier) and check
687 * for ISO and UDF PVDs or a fs superblock presence and do it
688 * in one ioctl (we need just sectors 0 and 16) */
689 scsi_cmd_init(udev, &sc, buffer, sizeof(buffer));
690 scsi_cmd_set(udev, &sc, 0, 0x28);
691 scsi_cmd_set(udev, &sc, 5, 0);
692 scsi_cmd_set(udev, &sc, 8, 32);
693 scsi_cmd_set(udev, &sc, 9, 0);
694 err = scsi_cmd_run(udev, &sc, fd, buffer, sizeof(buffer));
696 info_scsi_cmd_err(udev, "READ FIRST 32 BLOCKS", err);
700 /* if any non-zero data is found in sector 16 (iso and udf) or
701 * eventually 0 (fat32 boot sector, ext2 superblock, etc), disc
702 * is assumed non-blank */
705 for (block = 32768; block >= 0 && !result; block -= 32768) {
707 while (offset < (block + 2048) && !result) {
708 result = buffer [offset];
714 cd_media_state = media_status[0];
715 info(udev, "no data in blocks 0 or 16, assuming blank\n");
717 info(udev, "data in blocks 0 or 16, assuming complete\n");
722 /* "other" is e. g. DVD-RAM, can't append sessions there; DVDs in
723 * restricted overwrite mode can never append, only in sequential mode */
724 if ((header[2] & 3) < 2 && !cd_media_dvd_rw_ro)
725 cd_media_session_next = header[10] << 8 | header[5];
726 cd_media_session_count = header[9] << 8 | header[4];
727 cd_media_track_count = header[11] << 8 | header[6];
732 static int cd_media_toc(struct udev *udev, int fd)
735 unsigned char header[12];
736 unsigned char toc[2048];
741 scsi_cmd_init(udev, &sc, header, sizeof(header));
742 scsi_cmd_set(udev, &sc, 0, 0x43);
743 scsi_cmd_set(udev, &sc, 6, 1);
744 scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
745 scsi_cmd_set(udev, &sc, 9, 0);
746 err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
748 info_scsi_cmd_err(udev, "READ TOC", err);
752 len = (header[0] << 8 | header[1]) + 2;
753 info(udev, "READ TOC: len: %d\n", len);
754 if (len > sizeof(toc))
759 /* empty media has no tracks */
763 scsi_cmd_init(udev, &sc, toc, sizeof(toc));
764 scsi_cmd_set(udev, &sc, 0, 0x43);
765 scsi_cmd_set(udev, &sc, 6, header[2]); /* First Track/Session Number */
766 scsi_cmd_set(udev, &sc, 7, (len >> 8) & 0xff);
767 scsi_cmd_set(udev, &sc, 8, len & 0xff);
768 scsi_cmd_set(udev, &sc, 9, 0);
769 err = scsi_cmd_run(udev, &sc, fd, toc, len);
771 info_scsi_cmd_err(udev, "READ TOC (tracks)", err);
775 for (p = toc+4, i = 4; i < len-8; i += 8, p += 8) {
777 unsigned int is_data_track;
779 is_data_track = (p[1] & 0x04) != 0;
781 block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
782 info(udev, "track=%u info=0x%x(%s) start_block=%u\n",
783 p[2], p[1] & 0x0f, is_data_track ? "data":"audio", block);
786 cd_media_track_count_data++;
788 cd_media_track_count_audio++;
791 scsi_cmd_init(udev, &sc, header, sizeof(header));
792 scsi_cmd_set(udev, &sc, 0, 0x43);
793 scsi_cmd_set(udev, &sc, 2, 1); /* Session Info */
794 scsi_cmd_set(udev, &sc, 8, sizeof(header));
795 scsi_cmd_set(udev, &sc, 9, 0);
796 err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
798 info_scsi_cmd_err(udev, "READ TOC (multi session)", err);
801 len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7];
802 info(udev, "last track %u starts at block %u\n", header[4+2], len);
803 cd_media_session_last_offset = (unsigned long long int)len * 2048;
807 int main(int argc, char *argv[])
810 static const struct option options[] = {
811 { "export", no_argument, NULL, 'x' },
812 { "debug", no_argument, NULL, 'd' },
813 { "help", no_argument, NULL, 'h' },
816 const char *node = NULL;
826 udev_log_init("cdrom_id");
827 udev_set_log_fn(udev, log_fn);
832 option = getopt_long(argc, argv, "dxh", options, NULL);
839 if (udev_get_log_priority(udev) < LOG_INFO)
840 udev_set_log_priority(udev, LOG_INFO);
846 printf("Usage: cdrom_id [options] <device>\n"
847 " --export export key/value pairs\n"
848 " --debug debug to stderr\n"
849 " --help print this help text\n\n");
859 err(udev, "no device\n");
860 fprintf(stderr, "no device\n");
865 srand((unsigned int)getpid());
866 for (cnt = 20; cnt > 0; cnt--) {
867 struct timespec duration;
869 fd = open(node, O_RDONLY|O_NONBLOCK|(is_mounted(node) ? 0 : O_EXCL));
870 if (fd >= 0 || errno != EBUSY)
873 duration.tv_nsec = (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
874 nanosleep(&duration, NULL);
877 info(udev, "unable to open '%s'\n", node);
878 fprintf(stderr, "unable to open '%s'\n", node);
882 info(udev, "probing: '%s'\n", node);
884 /* same data as original cdrom_id */
885 if (cd_capability_compat(udev, fd) < 0) {
890 /* check for media - don't bail if there's no media as we still need to
891 * to read profiles */
892 cd_media_compat(udev, fd);
894 /* check if drive talks MMC */
895 if (cd_inquiry(udev, fd) < 0)
898 /* read drive and possibly current profile */
899 if (cd_profiles(udev, fd) < 0)
902 /* get session/track info */
903 cd_media_toc(udev, fd);
905 /* get writable media state */
906 cd_media_info(udev, fd);
909 printf("ID_CDROM=1\n");
911 printf("ID_CDROM_CD=1\n");
913 printf("ID_CDROM_CD_R=1\n");
915 printf("ID_CDROM_CD_RW=1\n");
917 printf("ID_CDROM_DVD=1\n");
919 printf("ID_CDROM_DVD_R=1\n");
921 printf("ID_CDROM_DVD_RW=1\n");
923 printf("ID_CDROM_DVD_RAM=1\n");
925 printf("ID_CDROM_DVD_PLUS_R=1\n");
927 printf("ID_CDROM_DVD_PLUS_RW=1\n");
928 if (cd_dvd_plus_r_dl)
929 printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
930 if (cd_dvd_plus_rw_dl)
931 printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
933 printf("ID_CDROM_BD=1\n");
935 printf("ID_CDROM_BD_R=1\n");
937 printf("ID_CDROM_BD_RE=1\n");
939 printf("ID_CDROM_HDDVD=1\n");
941 printf("ID_CDROM_HDDVD_R=1\n");
943 printf("ID_CDROM_HDDVD_RW=1\n");
945 printf("ID_CDROM_MO=1\n");
947 printf("ID_CDROM_MRW=1\n");
949 printf("ID_CDROM_MRW_W=1\n");
952 printf("ID_CDROM_MEDIA=1\n");
954 printf("ID_CDROM_MEDIA_MO=1\n");
956 printf("ID_CDROM_MEDIA_MRW=1\n");
958 printf("ID_CDROM_MEDIA_MRW_W=1\n");
960 printf("ID_CDROM_MEDIA_CD=1\n");
962 printf("ID_CDROM_MEDIA_CD_R=1\n");
964 printf("ID_CDROM_MEDIA_CD_RW=1\n");
965 if (cd_media_dvd_rom)
966 printf("ID_CDROM_MEDIA_DVD=1\n");
968 printf("ID_CDROM_MEDIA_DVD_R=1\n");
969 if (cd_media_dvd_ram)
970 printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
972 printf("ID_CDROM_MEDIA_DVD_RW=1\n");
973 if (cd_media_dvd_plus_r)
974 printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
975 if (cd_media_dvd_plus_rw)
976 printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
977 if (cd_media_dvd_plus_rw_dl)
978 printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
979 if (cd_media_dvd_plus_r_dl)
980 printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
982 printf("ID_CDROM_MEDIA_BD=1\n");
984 printf("ID_CDROM_MEDIA_BD_R=1\n");
986 printf("ID_CDROM_MEDIA_BD_RE=1\n");
988 printf("ID_CDROM_MEDIA_HDDVD=1\n");
989 if (cd_media_hddvd_r)
990 printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
991 if (cd_media_hddvd_rw)
992 printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
994 if (cd_media_state != NULL)
995 printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state);
996 if (cd_media_session_next > 0)
997 printf("ID_CDROM_MEDIA_SESSION_NEXT=%d\n", cd_media_session_next);
998 if (cd_media_session_count > 0)
999 printf("ID_CDROM_MEDIA_SESSION_COUNT=%d\n", cd_media_session_count);
1000 if (cd_media_session_count > 1 && cd_media_session_last_offset > 0)
1001 printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset);
1002 if (cd_media_track_count > 0)
1003 printf("ID_CDROM_MEDIA_TRACK_COUNT=%d\n", cd_media_track_count);
1004 if (cd_media_track_count_audio > 0)
1005 printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%d\n", cd_media_track_count_audio);
1006 if (cd_media_track_count_data > 0)
1007 printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%d\n", cd_media_track_count_data);