chiark / gitweb /
libudev: allow to store negative values in the udev database
[elogind.git] / extras / cdrom_id / cdrom_id.c
1 /*
2  * cdrom_id - optical drive and media information prober
3  *
4  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <getopt.h>
33 #include <scsi/sg.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <sys/ioctl.h>
38 #include <linux/cdrom.h>
39
40 #include "libudev.h"
41 #include "libudev-private.h"
42
43 static int debug;
44
45 static void log_fn(struct udev *udev, int priority,
46                    const char *file, int line, const char *fn,
47                    const char *format, va_list args)
48 {
49         if (debug) {
50                 fprintf(stderr, "%s: ", fn);
51                 vfprintf(stderr, format, args);
52         } else {
53                 vsyslog(priority, format, args);
54         }
55 }
56
57 /* device info */
58 static unsigned int cd_cd_rom;
59 static unsigned int cd_cd_r;
60 static unsigned int cd_cd_rw;
61 static unsigned int cd_dvd_rom;
62 static unsigned int cd_dvd_r;
63 static unsigned int cd_dvd_rw;
64 static unsigned int cd_dvd_ram;
65 static unsigned int cd_dvd_plus_r;
66 static unsigned int cd_dvd_plus_rw;
67 static unsigned int cd_dvd_plus_r_dl;
68 static unsigned int cd_dvd_plus_rw_dl;
69 static unsigned int cd_bd;
70 static unsigned int cd_bd_r;
71 static unsigned int cd_bd_re;
72 static unsigned int cd_hddvd;
73 static unsigned int cd_hddvd_r;
74 static unsigned int cd_hddvd_rw;
75 static unsigned int cd_mo;
76 static unsigned int cd_mrw;
77 static unsigned int cd_mrw_w;
78
79 /* media info */
80 static unsigned int cd_media;
81 static unsigned int cd_media_cd_rom;
82 static unsigned int cd_media_cd_r;
83 static unsigned int cd_media_cd_rw;
84 static unsigned int cd_media_dvd_rom;
85 static unsigned int cd_media_dvd_r;
86 static unsigned int cd_media_dvd_rw;
87 static unsigned int cd_media_dvd_ram;
88 static unsigned int cd_media_dvd_plus_r;
89 static unsigned int cd_media_dvd_plus_rw;
90 static unsigned int cd_media_dvd_plus_r_dl;
91 static unsigned int cd_media_dvd_plus_rw_dl;
92 static unsigned int cd_media_bd;
93 static unsigned int cd_media_bd_r;
94 static unsigned int cd_media_bd_re;
95 static unsigned int cd_media_hddvd;
96 static unsigned int cd_media_hddvd_r;
97 static unsigned int cd_media_hddvd_rw;
98 static unsigned int cd_media_mo;
99 static unsigned int cd_media_mrw;
100 static unsigned int cd_media_mrw_w;
101
102 static const char *cd_media_state;
103 static unsigned int cd_media_session_next;
104 static unsigned int cd_media_session_count;
105 static unsigned int cd_media_track_count;
106 static unsigned int cd_media_track_count_data;
107 static unsigned int cd_media_track_count_audio;
108 static unsigned long long int cd_media_session_last_offset;
109
110 #define ERRCODE(s)      ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
111 #define SK(errcode)     (((errcode) >> 16) & 0xF)
112 #define ASC(errcode)    (((errcode) >> 8) & 0xFF)
113 #define ASCQ(errcode)   ((errcode) & 0xFF)
114
115 static void info_scsi_cmd_err(struct udev *udev, char *cmd, int err)
116 {
117         if (err == -1) {
118                 info(udev, "%s failed\n", cmd);
119                 return;
120         }
121         info(udev, "%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err));
122 }
123
124 struct scsi_cmd {
125         struct cdrom_generic_command cgc;
126         union {
127                 struct request_sense s;
128                 unsigned char u[18];
129         } _sense;
130         struct sg_io_hdr sg_io;
131 };
132
133 static void scsi_cmd_set(struct udev *udev, struct scsi_cmd *cmd, size_t i, int arg)
134 {
135         if (i == 0) {
136                 memset(cmd, 0x00, sizeof(struct scsi_cmd));
137                 cmd->cgc.quiet = 1;
138                 cmd->cgc.sense = &cmd->_sense.s;
139                 memset(&cmd->sg_io, 0, sizeof(cmd->sg_io));
140                 cmd->sg_io.interface_id = 'S';
141                 cmd->sg_io.mx_sb_len = sizeof(cmd->_sense);
142                 cmd->sg_io.cmdp = cmd->cgc.cmd;
143                 cmd->sg_io.sbp = cmd->_sense.u;
144                 cmd->sg_io.flags = SG_FLAG_LUN_INHIBIT | SG_FLAG_DIRECT_IO;
145         }
146         cmd->sg_io.cmd_len = i + 1;
147         cmd->cgc.cmd[i] = arg;
148 }
149
150 #define CHECK_CONDITION 0x01
151
152 static int scsi_cmd_run(struct udev *udev, struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
153 {
154         int ret = 0;
155
156         cmd->sg_io.dxferp = buf;
157         cmd->sg_io.dxfer_len = bufsize;
158         cmd->sg_io.dxfer_direction = SG_DXFER_FROM_DEV;
159         if (ioctl(fd, SG_IO, &cmd->sg_io))
160                 return -1;
161
162         if ((cmd->sg_io.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
163                 errno = EIO;
164                 ret = -1;
165                 if (cmd->sg_io.masked_status & CHECK_CONDITION) {
166                         ret = ERRCODE(cmd->_sense.u);
167                         if (ret == 0)
168                                 ret = -1;
169                 }
170         }
171         return ret;
172 }
173
174 static int cd_capability_compat(struct udev *udev, int fd)
175 {
176         int capability;
177
178         capability = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
179         if (capability < 0) {
180                 info(udev, "CDROM_GET_CAPABILITY failed\n");
181                 return -1;
182         }
183
184         if (capability & CDC_CD_R)
185                 cd_cd_r = 1;
186         if (capability & CDC_CD_RW)
187                 cd_cd_rw = 1;
188         if (capability & CDC_DVD)
189                 cd_dvd_rom = 1;
190         if (capability & CDC_DVD_R)
191                 cd_dvd_r = 1;
192         if (capability & CDC_DVD_RAM)
193                 cd_dvd_ram = 1;
194         if (capability & CDC_MRW)
195                 cd_mrw = 1;
196         if (capability & CDC_MRW_W)
197                 cd_mrw_w = 1;
198         return 0;
199 }
200
201 static int cd_media_compat(struct udev *udev, int fd)
202 {
203         if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK) {
204                 info(udev, "CDROM_DRIVE_STATUS != CDS_DISC_OK\n");
205                 return -1;
206         }
207         cd_media = 1;
208         return 0;
209 }
210
211 static int cd_inquiry(struct udev *udev, int fd) {
212         struct scsi_cmd sc;
213         unsigned char inq[128];
214         int err;
215
216         scsi_cmd_set(udev, &sc, 0, 0x12);
217         scsi_cmd_set(udev, &sc, 4, 36);
218         scsi_cmd_set(udev, &sc, 5, 0);
219         err = scsi_cmd_run(udev, &sc, fd, inq, 36);
220         if ((err < 0)) {
221                 info_scsi_cmd_err(udev, "INQUIRY", err);
222                 return -1;
223         }
224
225         if ((inq[0] & 0x1F) != 5) {
226                 info(udev, "not an MMC unit\n");
227                 return -1;
228         }
229
230         info(udev, "INQUIRY: [%.8s][%.16s][%.4s]\n", inq + 8, inq + 16, inq + 32);
231         return 0;
232 }
233
234 static int cd_profiles(struct udev *udev, int fd)
235 {
236         struct scsi_cmd sc;
237         unsigned char header[8];
238         unsigned char profiles[512];
239         unsigned int cur_profile;
240         unsigned int len;
241         unsigned int i;
242         int err;
243
244         scsi_cmd_set(udev, &sc, 0, 0x46);
245         scsi_cmd_set(udev, &sc, 1, 0);
246         scsi_cmd_set(udev, &sc, 8, sizeof(header));
247         scsi_cmd_set(udev, &sc, 9, 0);
248         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
249         if ((err < 0)) {
250                 info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
251                 return -1;
252         }
253
254         len = 4 + (header[0] << 24 | header[1] << 16 | header[2] << 8 | header[3]);
255         info(udev, "GET CONFIGURATION: number of profiles %i\n", len);
256         if (len > sizeof(profiles)) {
257                 info(udev, "invalid number of profiles\n");
258                 return -1;
259         }
260
261         scsi_cmd_set(udev, &sc, 0, 0x46);
262         scsi_cmd_set(udev, &sc, 1, 1);
263         scsi_cmd_set(udev, &sc, 6, len >> 16);
264         scsi_cmd_set(udev, &sc, 7, len >> 8);
265         scsi_cmd_set(udev, &sc, 8, len);
266         scsi_cmd_set(udev, &sc, 9, 0);
267         err = scsi_cmd_run(udev, &sc, fd, profiles, len);
268         if ((err < 0)) {
269                 info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
270                 return -1;
271         }
272
273         /* device profiles */
274         for (i = 12; i < profiles[11]; i += 4) {
275                 unsigned int profile = (profiles[i] << 8 | profiles[i + 1]);
276                 if (profile == 0)
277                         continue;
278                 info(udev, "profile 0x%02x\n", profile);
279
280                 switch (profile) {
281                 case 0x03:
282                 case 0x04:
283                 case 0x05:
284                         cd_mo = 1;
285                 break;
286                 case 0x10:
287                         cd_dvd_rom = 1;
288                         break;
289                 case 0x12:
290                         cd_dvd_ram = 1;
291                         break;
292                 case 0x13:
293                 case 0x14:
294                         cd_dvd_rw = 1;
295                         break;
296                 case 0x1B:
297                         cd_dvd_plus_r = 1;
298                         break;
299                 case 0x1A:
300                         cd_dvd_plus_rw = 1;
301                         break;
302                 case 0x2A:
303                         cd_dvd_plus_rw_dl = 1;
304                         break;
305                 case 0x2B:
306                         cd_dvd_plus_r_dl = 1;
307                         break;
308                 case 0x40:
309                         cd_bd = 1;
310                         break;
311                 case 0x41:
312                 case 0x42:
313                         cd_bd_r = 1;
314                         break;
315                 case 0x43:
316                         cd_bd_re = 1;
317                         break;
318                 case 0x50:
319                         cd_hddvd = 1;
320                         break;
321                 case 0x51:
322                         cd_hddvd_r = 1;
323                         break;
324                 case 0x52:
325                         cd_hddvd_rw = 1;
326                         break;
327                 default:
328                         break;
329                 }
330         }
331
332         /* current media profile */
333         cur_profile = header[6] << 8 | header[7];
334         info(udev, "current profile 0x%02x\n", cur_profile);
335         if (cur_profile == 0) {
336                 info(udev, "no current profile, assuming no media\n");
337                 return -1;
338         }
339
340         cd_media = 1;
341
342         switch (cur_profile) {
343         case 0x03:
344         case 0x04:
345         case 0x05:
346                 cd_media_mo = 1;
347                 break;
348         case 0x08:
349                 cd_media_cd_rom = 1;
350                 break;
351         case 0x09:
352                 cd_media_cd_r = 1;
353                 break;
354         case 0x0a:
355                 cd_media_cd_rw = 1;
356                 break;
357         case 0x10:
358                 cd_media_dvd_rom = 1;
359                 break;
360         case 0x11:
361                 cd_media_dvd_r = 1;
362                 break;
363         case 0x12:
364                 cd_media_dvd_ram = 1;
365                 break;
366         case 0x13:
367         case 0x14:
368                 cd_media_dvd_rw = 1;
369                 break;
370         case 0x1B:
371                 cd_media_dvd_plus_r = 1;
372                 break;
373         case 0x1A:
374                 cd_media_dvd_plus_rw = 1;
375                 break;
376         case 0x2A:
377                 cd_media_dvd_plus_rw_dl = 1;
378                 break;
379         case 0x2B:
380                 cd_media_dvd_plus_r_dl = 1;
381                 break;
382         case 0x40:
383                 cd_media_bd = 1;
384                 break;
385         case 0x41:
386         case 0x42:
387                 cd_media_bd_r = 1;
388                 break;
389         case 0x43:
390                 cd_media_bd_re = 1;
391                 break;
392         case 0x50:
393                 cd_media_hddvd = 1;
394                 break;
395         case 0x51:
396                 cd_media_hddvd_r = 1;
397                 break;
398         case 0x52:
399                 cd_media_hddvd_rw = 1;
400                 break;
401         default:
402                 break;
403         }
404         return 0;
405 }
406
407 static int cd_media_info(struct udev *udev, int fd)
408 {
409         struct scsi_cmd sc;
410         unsigned char header[32];
411         static const char *media_status[] = {
412                 "blank",
413                 "appendable",
414                 "complete",
415                 "other"
416         };
417         int err;
418
419         scsi_cmd_set(udev, &sc, 0, 0x51);
420         scsi_cmd_set(udev, &sc, 8, sizeof(header));
421         scsi_cmd_set(udev, &sc, 9, 0);
422         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
423         if ((err < 0)) {
424                 info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
425                 return -1;
426         };
427
428         info(udev, "disk type %02x\n", header[8]);
429
430         /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
431         if (!cd_media_cd_rom && (header[2] & 3) < 4)
432                 cd_media_state = media_status[header[2] & 3];
433
434         if ((header[2] & 3) != 2)
435                 cd_media_session_next = header[10] << 8 | header[5];
436         cd_media_session_count = header[9] << 8 | header[4];
437         cd_media_track_count = header[11] << 8 | header[6];
438
439         return 0;
440 }
441
442 static int cd_media_toc(struct udev *udev, int fd)
443 {
444         struct scsi_cmd sc;
445         unsigned char header[12];
446         unsigned char toc[2048];
447         unsigned int len, i;
448         unsigned char *p;
449         int err;
450
451         scsi_cmd_set(udev, &sc, 0, 0x43);
452         scsi_cmd_set(udev, &sc, 6, 1);
453         scsi_cmd_set(udev, &sc, 8, sizeof(header));
454         scsi_cmd_set(udev, &sc, 9, 0);
455         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
456         if ((err < 0)) {
457                 info_scsi_cmd_err(udev, "READ TOC", err);
458                 return -1;
459         }
460
461         len = (header[0] << 8 | header[1]) + 2;
462         info(udev, "READ TOC: len: %d\n", len);
463         if (len > sizeof(toc))
464                 return -1;
465         if (len < 2)
466                 return -1;
467
468         /* empty media has no tracks */
469         if (len < 8)
470                 return 0;
471
472         scsi_cmd_set(udev, &sc, 0, 0x43);
473         scsi_cmd_set(udev, &sc, 6, header[2]); /* First Track/Session Number */
474         scsi_cmd_set(udev, &sc, 7, len >> 8);
475         scsi_cmd_set(udev, &sc, 8, len);
476         scsi_cmd_set(udev, &sc, 9, 0);
477         err = scsi_cmd_run(udev, &sc, fd, toc, len);
478         if ((err < 0)) {
479                 info_scsi_cmd_err(udev, "READ TOC (tracks)", err);
480                 return -1;
481         }
482
483         for (p = toc+4, i = 4; i < len-8; i += 8, p += 8) {
484                 unsigned int block;
485                 unsigned int is_data_track;
486
487                 is_data_track = (p[1] & 0x04) != 0;
488
489                 block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
490                 info(udev, "track=%u info=0x%x(%s) start_block=%u\n",
491                      p[2], p[1] & 0x0f, is_data_track ? "data":"audio", block);
492
493                 if (is_data_track)
494                         cd_media_track_count_data++;
495                 else
496                         cd_media_track_count_audio++;
497         }
498
499         scsi_cmd_set(udev, &sc, 0, 0x43);
500         scsi_cmd_set(udev, &sc, 2, 1); /* Session Info */
501         scsi_cmd_set(udev, &sc, 8, 12);
502         scsi_cmd_set(udev, &sc, 9, 0);
503         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
504         if ((err < 0)) {
505                 info_scsi_cmd_err(udev, "READ TOC (multi session)", err);
506                 return -1;
507         }
508         len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7];
509         info(udev, "last track %u starts at block %u\n", header[4+2], len);
510         cd_media_session_last_offset = (unsigned long long int)len * 2048;
511         return 0;
512 }
513
514 int main(int argc, char *argv[])
515 {
516         struct udev *udev;
517         static const struct option options[] = {
518                 { "export", no_argument, NULL, 'x' },
519                 { "debug", no_argument, NULL, 'd' },
520                 { "help", no_argument, NULL, 'h' },
521                 {}
522         };
523         const char *node = NULL;
524         int export = 0;
525         int fd = -1;
526         int rc = 0;
527
528         udev = udev_new();
529         if (udev == NULL)
530                 goto exit;
531
532         udev_log_init("cdrom_id");
533         udev_set_log_fn(udev, log_fn);
534
535         while (1) {
536                 int option;
537
538                 option = getopt_long(argc, argv, "dxh", options, NULL);
539                 if (option == -1)
540                         break;
541
542                 switch (option) {
543                 case 'd':
544                         debug = 1;
545                         if (udev_get_log_priority(udev) < LOG_INFO)
546                                 udev_set_log_priority(udev, LOG_INFO);
547                         break;
548                 case 'x':
549                         export = 1;
550                         break;
551                 case 'h':
552                         printf("Usage: cdrom_id [options] <device>\n"
553                                "  --export        export key/value pairs\n"
554                                "  --debug         debug to stderr\n"
555                                "  --help          print this help text\n\n");
556                         goto exit;
557                 default:
558                         rc = 1;
559                         goto exit;
560                 }
561         }
562
563         node = argv[optind];
564         if (!node) {
565                 err(udev, "no device\n");
566                 fprintf(stderr, "no device\n");
567                 rc = 1;
568                 goto exit;
569         }
570
571         fd = open(node, O_RDONLY | O_NONBLOCK);
572         if (fd < 0) {
573                 info(udev, "unable to open '%s'\n", node);
574                 fprintf(stderr, "unable to open '%s'\n", node);
575                 rc = 1;
576                 goto exit;
577         }
578         info(udev, "probing: '%s'\n", node);
579
580         /* same data as original cdrom_id */
581         if (cd_capability_compat(udev, fd) < 0) {
582                 rc = 1;
583                 goto exit;
584         }
585
586         /* check for media */
587         if (cd_media_compat(udev, fd) < 0)
588                 goto print;
589
590         /* check if drive talks MMC */
591         if (cd_inquiry(udev, fd) < 0)
592                 goto print;
593
594         /* read drive and possibly current profile */
595         if (cd_profiles(udev, fd) < 0)
596                 goto print;
597
598         /* get session/track info */
599         if (cd_media_toc(udev, fd) < 0)
600                 goto print;
601
602         /* get writable media state */
603         if (cd_media_info(udev, fd) < 0)
604                 goto print;
605
606 print:
607         printf("ID_CDROM=1\n");
608         if (cd_cd_rom)
609                 printf("ID_CDROM_CD=1\n");
610         if (cd_cd_r)
611                 printf("ID_CDROM_CD_R=1\n");
612         if (cd_cd_rw)
613                 printf("ID_CDROM_CD_RW=1\n");
614         if (cd_dvd_rom)
615                 printf("ID_CDROM_DVD=1\n");
616         if (cd_dvd_r)
617                 printf("ID_CDROM_DVD_R=1\n");
618         if (cd_dvd_rw)
619                 printf("ID_CDROM_DVD_RW=1\n");
620         if (cd_dvd_ram)
621                 printf("ID_CDROM_DVD_RAM=1\n");
622         if (cd_dvd_plus_r)
623                 printf("ID_CDROM_DVD_PLUS_R=1\n");
624         if (cd_dvd_plus_rw)
625                 printf("ID_CDROM_DVD_PLUS_RW=1\n");
626         if (cd_dvd_plus_r_dl)
627                 printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
628         if (cd_dvd_plus_rw_dl)
629                 printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
630         if (cd_bd)
631                 printf("ID_CDROM_BD=1\n");
632         if (cd_bd_r)
633                 printf("ID_CDROM_BD_R=1\n");
634         if (cd_bd_re)
635                 printf("ID_CDROM_BD_RE=1\n");
636         if (cd_hddvd)
637                 printf("ID_CDROM_HDDVD=1\n");
638         if (cd_hddvd_r)
639                 printf("ID_CDROM_HDDVD_R=1\n");
640         if (cd_hddvd_rw)
641                 printf("ID_CDROM_HDDVD_RW=1\n");
642         if (cd_mo)
643                 printf("ID_CDROM_MO=1\n");
644         if (cd_mrw)
645                 printf("ID_CDROM_MRW=1\n");
646         if (cd_mrw_w)
647                 printf("ID_CDROM_MRW_W=1\n");
648
649         if (cd_media)
650                 printf("ID_CDROM_MEDIA=1\n");
651         if (cd_media_mo)
652                 printf("ID_CDROM_MEDIA_MO=1\n");
653         if (cd_media_mrw)
654                 printf("ID_CDROM_MEDIA_MRW=1\n");
655         if (cd_media_mrw_w)
656                 printf("ID_CDROM_MEDIA_MRW_W=1\n");
657         if (cd_media_cd_rom)
658                 printf("ID_CDROM_MEDIA_CD=1\n");
659         if (cd_media_cd_r)
660                 printf("ID_CDROM_MEDIA_CD_R=1\n");
661         if (cd_media_cd_rw)
662                 printf("ID_CDROM_MEDIA_CD_RW=1\n");
663         if (cd_media_dvd_rom)
664                 printf("ID_CDROM_MEDIA_DVD=1\n");
665         if (cd_media_dvd_r)
666                 printf("ID_CDROM_MEDIA_DVD_R=1\n");
667         if (cd_media_dvd_ram)
668                 printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
669         if (cd_media_dvd_rw)
670                 printf("ID_CDROM_MEDIA_DVD_RW=1\n");
671         if (cd_media_dvd_plus_r)
672                 printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
673         if (cd_media_dvd_plus_rw)
674                 printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
675         if (cd_media_dvd_plus_rw_dl)
676                 printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
677         if (cd_media_dvd_plus_r_dl)
678                 printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
679         if (cd_media_bd)
680                 printf("ID_CDROM_MEDIA_BD=1\n");
681         if (cd_media_bd_r)
682                 printf("ID_CDROM_MEDIA_BD_R=1\n");
683         if (cd_media_bd_re)
684                 printf("ID_CDROM_MEDIA_BD_RE=1\n");
685         if (cd_media_hddvd)
686                 printf("ID_CDROM_MEDIA_HDDVD=1\n");
687         if (cd_media_hddvd_r)
688                 printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
689         if (cd_media_hddvd_rw)
690                 printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
691
692         if (cd_media_state != NULL)
693                 printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state);
694         if (cd_media_session_next > 0)
695                 printf("ID_CDROM_MEDIA_SESSION_NEXT=%d\n", cd_media_session_next);
696         if (cd_media_session_count > 0)
697                 printf("ID_CDROM_MEDIA_SESSION_COUNT=%d\n", cd_media_session_count);
698         if (cd_media_track_count > 0)
699                 printf("ID_CDROM_MEDIA_TRACK_COUNT=%d\n", cd_media_track_count);
700         if (cd_media_track_count_audio > 0)
701                 printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%d\n", cd_media_track_count_audio);
702         if (cd_media_track_count_data > 0)
703                 printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%d\n", cd_media_track_count_data);
704         if (cd_media_session_last_offset > 0)
705                 printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset);
706 exit:
707         if (fd >= 0)
708                 close(fd);
709         udev_unref(udev);
710         udev_log_close();
711         return rc;
712 }
713