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