chiark / gitweb /
udevd: clarify worker exit status
[elogind.git] / extras / cdrom_id / cdrom_id.c
1 /*
2  * cdrom_id - optical drive and media information prober
3  *
4  * Copyright (C) 2008-2010 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 <time.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 "libudev.h"
42 #include "libudev-private.h"
43
44 static int debug;
45
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)
49 {
50         if (debug) {
51                 fprintf(stderr, "%s: ", fn);
52                 vfprintf(stderr, format, args);
53         } else {
54                 vsyslog(priority, format, args);
55         }
56 }
57
58 /* device info */
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;
79
80 /* media info */
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;
104
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;
112
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)
117
118 static int is_mounted(const char *device)
119 {
120         struct stat statbuf;
121         FILE *fp;
122         int maj, min;
123         int mounted = 0;
124
125         if (stat(device, &statbuf) < 0)
126                 return -ENODEV;
127
128         fp = fopen("/proc/self/mountinfo", "r");
129         if (fp == NULL)
130                 return -ENOSYS;
131         while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
132                 if (makedev(maj, min) == statbuf.st_rdev) {
133                         mounted = 1;
134                         break;
135                 }
136         }
137         fclose(fp);
138         return mounted;
139 }
140
141 static void info_scsi_cmd_err(struct udev *udev, char *cmd, int err)
142 {
143         if (err == -1) {
144                 info(udev, "%s failed\n", cmd);
145                 return;
146         }
147         info(udev, "%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err));
148 }
149
150 struct scsi_cmd {
151         struct cdrom_generic_command cgc;
152         union {
153                 struct request_sense s;
154                 unsigned char u[18];
155         } _sense;
156         struct sg_io_hdr sg_io;
157 };
158
159 static void scsi_cmd_init(struct udev *udev, struct scsi_cmd *cmd, unsigned char *buf, size_t bufsize)
160 {
161         memset(cmd, 0x00, sizeof(struct scsi_cmd));
162         memset(buf, 0x00, bufsize);
163         cmd->cgc.quiet = 1;
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;
171 }
172
173 static void scsi_cmd_set(struct udev *udev, struct scsi_cmd *cmd, size_t i, unsigned char arg)
174 {
175         cmd->sg_io.cmd_len = i + 1;
176         cmd->cgc.cmd[i] = arg;
177 }
178
179 #define CHECK_CONDITION 0x01
180
181 static int scsi_cmd_run(struct udev *udev, struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize)
182 {
183         int ret = 0;
184
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))
189                 return -1;
190
191         if ((cmd->sg_io.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
192                 errno = EIO;
193                 ret = -1;
194                 if (cmd->sg_io.masked_status & CHECK_CONDITION) {
195                         ret = ERRCODE(cmd->_sense.u);
196                         if (ret == 0)
197                                 ret = -1;
198                 }
199         }
200         return ret;
201 }
202
203 static int cd_capability_compat(struct udev *udev, int fd)
204 {
205         int capability;
206
207         capability = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
208         if (capability < 0) {
209                 info(udev, "CDROM_GET_CAPABILITY failed\n");
210                 return -1;
211         }
212
213         if (capability & CDC_CD_R)
214                 cd_cd_r = 1;
215         if (capability & CDC_CD_RW)
216                 cd_cd_rw = 1;
217         if (capability & CDC_DVD)
218                 cd_dvd_rom = 1;
219         if (capability & CDC_DVD_R)
220                 cd_dvd_r = 1;
221         if (capability & CDC_DVD_RAM)
222                 cd_dvd_ram = 1;
223         if (capability & CDC_MRW)
224                 cd_mrw = 1;
225         if (capability & CDC_MRW_W)
226                 cd_mrw_w = 1;
227         return 0;
228 }
229
230 static int cd_media_compat(struct udev *udev, int fd)
231 {
232         if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK) {
233                 info(udev, "CDROM_DRIVE_STATUS != CDS_DISC_OK\n");
234                 return -1;
235         }
236         cd_media = 1;
237         return 0;
238 }
239
240 static int cd_inquiry(struct udev *udev, int fd) {
241         struct scsi_cmd sc;
242         unsigned char inq[128];
243         int err;
244
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);
250         if ((err != 0)) {
251                 info_scsi_cmd_err(udev, "INQUIRY", err);
252                 return -1;
253         }
254
255         if ((inq[0] & 0x1F) != 5) {
256                 info(udev, "not an MMC unit\n");
257                 return -1;
258         }
259
260         info(udev, "INQUIRY: [%.8s][%.16s][%.4s]\n", inq + 8, inq + 16, inq + 32);
261         return 0;
262 }
263
264 static void feature_profile_media(struct udev *udev, int cur_profile)
265 {
266         switch (cur_profile) {
267         case 0x03:
268         case 0x04:
269         case 0x05:
270                 info(udev, "profile 0x%02x \n", cur_profile);
271                 cd_media = 1;
272                 cd_media_mo = 1;
273                 break;
274         case 0x08:
275                 info(udev, "profile 0x%02x media_cd_rom\n", cur_profile);
276                 cd_media = 1;
277                 cd_media_cd_rom = 1;
278                 break;
279         case 0x09:
280                 info(udev, "profile 0x%02x media_cd_r\n", cur_profile);
281                 cd_media = 1;
282                 cd_media_cd_r = 1;
283                 break;
284         case 0x0a:
285                 info(udev, "profile 0x%02x media_cd_rw\n", cur_profile);
286                 cd_media = 1;
287                 cd_media_cd_rw = 1;
288                 break;
289         case 0x10:
290                 info(udev, "profile 0x%02x media_dvd_ro\n", cur_profile);
291                 cd_media = 1;
292                 cd_media_dvd_rom = 1;
293                 break;
294         case 0x11:
295                 info(udev, "profile 0x%02x media_dvd_r\n", cur_profile);
296                 cd_media = 1;
297                 cd_media_dvd_r = 1;
298                 break;
299         case 0x12:
300                 info(udev, "profile 0x%02x media_dvd_ram\n", cur_profile);
301                 cd_media = 1;
302                 cd_media_dvd_ram = 1;
303                 break;
304         case 0x13:
305                 info(udev, "profile 0x%02x media_dvd_rw_ro\n", cur_profile);
306                 cd_media = 1;
307                 cd_media_dvd_rw = 1;
308                 cd_media_dvd_rw_ro = 1;
309                 break;
310         case 0x14:
311                 info(udev, "profile 0x%02x media_dvd_rw_seq\n", cur_profile);
312                 cd_media = 1;
313                 cd_media_dvd_rw = 1;
314                 cd_media_dvd_rw_seq = 1;
315                 break;
316         case 0x1B:
317                 info(udev, "profile 0x%02x media_dvd_plus_r\n", cur_profile);
318                 cd_media = 1;
319                 cd_media_dvd_plus_r = 1;
320                 break;
321         case 0x1A:
322                 info(udev, "profile 0x%02x media_dvd_plus_rw\n", cur_profile);
323                 cd_media = 1;
324                 cd_media_dvd_plus_rw = 1;
325                 break;
326         case 0x2A:
327                 info(udev, "profile 0x%02x media_dvd_plus_rw_dl\n", cur_profile);
328                 cd_media = 1;
329                 cd_media_dvd_plus_rw_dl = 1;
330                 break;
331         case 0x2B:
332                 info(udev, "profile 0x%02x media_dvd_plus_r_dl\n", cur_profile);
333                 cd_media = 1;
334                 cd_media_dvd_plus_r_dl = 1;
335                 break;
336         case 0x40:
337                 info(udev, "profile 0x%02x media_bd\n", cur_profile);
338                 cd_media = 1;
339                 cd_media_bd = 1;
340                 break;
341         case 0x41:
342         case 0x42:
343                 info(udev, "profile 0x%02x media_bd_r\n", cur_profile);
344                 cd_media = 1;
345                 cd_media_bd_r = 1;
346                 break;
347         case 0x43:
348                 info(udev, "profile 0x%02x media_bd_re\n", cur_profile);
349                 cd_media = 1;
350                 cd_media_bd_re = 1;
351                 break;
352         case 0x50:
353                 info(udev, "profile 0x%02x media_hddvd\n", cur_profile);
354                 cd_media = 1;
355                 cd_media_hddvd = 1;
356                 break;
357         case 0x51:
358                 info(udev, "profile 0x%02x media_hddvd_r\n", cur_profile);
359                 cd_media = 1;
360                 cd_media_hddvd_r = 1;
361                 break;
362         case 0x52:
363                 info(udev, "profile 0x%02x media_hddvd_rw\n", cur_profile);
364                 cd_media = 1;
365                 cd_media_hddvd_rw = 1;
366                 break;
367         default:
368                 info(udev, "profile 0x%02x <ignored>\n", cur_profile);
369                 break;
370         }
371 }
372
373 static int feature_profiles(struct udev *udev, const unsigned char *profiles, size_t size)
374 {
375         unsigned int i;
376
377         for (i = 0; i+4 <= size; i += 4) {
378                 int profile;
379
380                 profile = profiles[i] << 8 | profiles[i+1];
381                 switch (profile) {
382                 case 0x03:
383                 case 0x04:
384                 case 0x05:
385                         info(udev, "profile 0x%02x mo\n", profile);
386                         cd_mo = 1;
387                         break;
388                 case 0x08:
389                         info(udev, "profile 0x%02x cd_rom\n", profile);
390                         cd_cd_rom = 1;
391                         break;
392                 case 0x09:
393                         info(udev, "profile 0x%02x cd_r\n", profile);
394                         cd_cd_r = 1;
395                         break;
396                 case 0x0A:
397                         info(udev, "profile 0x%02x cd_rw\n", profile);
398                         cd_cd_rw = 1;
399                         break;
400                 case 0x10:
401                         info(udev, "profile 0x%02x dvd_rom\n", profile);
402                         cd_dvd_rom = 1;
403                         break;
404                 case 0x12:
405                         info(udev, "profile 0x%02x dvd_ram\n", profile);
406                         cd_dvd_ram = 1;
407                         break;
408                 case 0x13:
409                 case 0x14:
410                         info(udev, "profile 0x%02x dvd_rw\n", profile);
411                         cd_dvd_rw = 1;
412                         break;
413                 case 0x1B:
414                         info(udev, "profile 0x%02x dvd_plus_r\n", profile);
415                         cd_dvd_plus_r = 1;
416                         break;
417                 case 0x1A:
418                         info(udev, "profile 0x%02x dvd_plus_rw\n", profile);
419                         cd_dvd_plus_rw = 1;
420                         break;
421                 case 0x2A:
422                         info(udev, "profile 0x%02x dvd_plus_rw_dl\n", profile);
423                         cd_dvd_plus_rw_dl = 1;
424                         break;
425                 case 0x2B:
426                         info(udev, "profile 0x%02x dvd_plus_r_dl\n", profile);
427                         cd_dvd_plus_r_dl = 1;
428                         break;
429                 case 0x40:
430                         cd_bd = 1;
431                         info(udev, "profile 0x%02x bd\n", profile);
432                         break;
433                 case 0x41:
434                 case 0x42:
435                         cd_bd_r = 1;
436                         info(udev, "profile 0x%02x bd_r\n", profile);
437                         break;
438                 case 0x43:
439                         cd_bd_re = 1;
440                         info(udev, "profile 0x%02x bd_re\n", profile);
441                         break;
442                 case 0x50:
443                         cd_hddvd = 1;
444                         info(udev, "profile 0x%02x hddvd\n", profile);
445                         break;
446                 case 0x51:
447                         cd_hddvd_r = 1;
448                         info(udev, "profile 0x%02x hddvd_r\n", profile);
449                         break;
450                 case 0x52:
451                         cd_hddvd_rw = 1;
452                         info(udev, "profile 0x%02x hddvd_rw\n", profile);
453                         break;
454                 default:
455                         info(udev, "profile 0x%02x <ignored>\n", profile);
456                         break;
457                 }
458         }
459         return 0;
460 }
461
462 /* returns 0 if media was detected */
463 static int cd_profiles_old_mmc(struct udev *udev, int fd)
464 {
465         struct scsi_cmd sc;
466         int err;
467
468         unsigned char header[32];
469
470         scsi_cmd_init(udev, &sc, header, sizeof(header));
471         scsi_cmd_set(udev, &sc, 0, 0x51);
472         scsi_cmd_set(udev, &sc, 8, sizeof(header));
473         scsi_cmd_set(udev, &sc, 9, 0);
474         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
475         if ((err != 0)) {
476                 info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
477                 if (cd_media == 1) {
478                         info(udev, "no current profile, but disc is present; assuming CD-ROM\n");
479                         cd_media_cd_rom = 1;
480                         return 0;
481                 } else {
482                         info(udev, "no current profile, assuming no media\n");
483                         return -1;
484                 }
485         };
486
487         cd_media = 1;
488
489         if (header[2] & 16) {
490                 cd_media_cd_rw = 1;
491                 info(udev, "profile 0x0a media_cd_rw\n");
492         } else if ((header[2] & 3) < 2 && cd_cd_r) {
493                 cd_media_cd_r = 1;
494                 info(udev, "profile 0x09 media_cd_r\n");
495         } else {
496                 cd_media_cd_rom = 1;
497                 info(udev, "profile 0x08 media_cd_rom\n");
498         }
499         return 0;
500 }
501
502 /* returns 0 if media was detected */
503 static int cd_profiles(struct udev *udev, int fd)
504 {
505         struct scsi_cmd sc;
506         unsigned char features[65530];
507         unsigned int cur_profile = 0;
508         unsigned int len;
509         unsigned int i;
510         int err;
511         int ret;
512
513         ret = -1;
514
515         /* First query the current profile */
516         scsi_cmd_init(udev, &sc, features, sizeof(features));
517         scsi_cmd_set(udev, &sc, 0, 0x46);
518         scsi_cmd_set(udev, &sc, 8, 8);
519         scsi_cmd_set(udev, &sc, 9, 0);
520         err = scsi_cmd_run(udev, &sc, fd, features, 8);
521         if ((err != 0)) {
522                 info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
523                 /* handle pre-MMC2 drives which do not support GET CONFIGURATION */
524                 if (SK(err) == 0x5 && ASC(err) == 0x20) {
525                         info(udev, "drive is pre-MMC2 and does not support 46h get configuration command\n");
526                         info(udev, "trying to work around the problem\n");
527                         ret = cd_profiles_old_mmc(udev, fd);
528                 }
529                 goto out;
530         }
531
532         cur_profile = features[6] << 8 | features[7];
533         if (cur_profile > 0) {
534                 info(udev, "current profile 0x%02x\n", cur_profile);
535                 feature_profile_media (udev, cur_profile);
536                 ret = 0; /* we have media */
537         } else {
538                 info(udev, "no current profile, assuming no media\n");
539         }
540
541         len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
542         info(udev, "GET CONFIGURATION: size of features buffer 0x%04x\n", len);
543
544         if (len > sizeof(features)) {
545                 info(udev, "can not get features in a single query, truncating\n");
546                 len = sizeof(features);
547         } else if (len <= 8) {
548                 len = sizeof(features);
549         }
550
551         /* Now get the full feature buffer */
552         scsi_cmd_init(udev, &sc, features,  len);
553         scsi_cmd_set(udev, &sc, 0, 0x46);
554         scsi_cmd_set(udev, &sc, 7, ( len >> 8 ) & 0xff);
555         scsi_cmd_set(udev, &sc, 8, len & 0xff);
556         scsi_cmd_set(udev, &sc, 9, 0);
557         err = scsi_cmd_run(udev, &sc, fd, features, len);
558         if ((err != 0)) {
559                 info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
560                 return -1;
561         }
562
563         /* parse the length once more, in case the drive decided to have other features suddenly :) */
564         len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
565         info(udev, "GET CONFIGURATION: size of features buffer 0x%04x\n", len);
566
567         if (len > sizeof(features)) {
568                 info(udev, "can not get features in a single query, truncating\n");
569                 len = sizeof(features);
570         }
571
572         /* device features */
573         for (i = 8; i+4 < len; i += (4 + features[i+3])) {
574                 unsigned int feature;
575
576                 feature = features[i] << 8 | features[i+1];
577
578                 switch (feature) {
579                 case 0x00:
580                         info(udev, "GET CONFIGURATION: feature 'profiles', with %i entries\n", features[i+3] / 4);
581                         feature_profiles(udev, &features[i]+4, features[i+3]);
582                         break;
583                 default:
584                         info(udev, "GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes\n", feature, features[i+3]);
585                         break;
586                 }
587         }
588 out:
589         return ret;
590 }
591
592 static int cd_media_info(struct udev *udev, int fd)
593 {
594         struct scsi_cmd sc;
595         unsigned char header[32];
596         static const char *media_status[] = {
597                 "blank",
598                 "appendable",
599                 "complete",
600                 "other"
601         };
602         int err;
603
604         scsi_cmd_init(udev, &sc, header, sizeof(header));
605         scsi_cmd_set(udev, &sc, 0, 0x51);
606         scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
607         scsi_cmd_set(udev, &sc, 9, 0);
608         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
609         if ((err != 0)) {
610                 info_scsi_cmd_err(udev, "READ DISC INFORMATION", err);
611                 return -1;
612         };
613
614         cd_media = 1;
615         info(udev, "disk type %02x\n", header[8]);
616         info(udev, "hardware reported media status: %s\n", media_status[header[2] & 3]);
617
618         /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */
619         if (!cd_media_cd_rom)
620                 cd_media_state = media_status[header[2] & 3];
621
622         /* fresh DVD-RW in restricted overwite mode reports itself as
623          * "appendable"; change it to "blank" to make it consistent with what
624          * gets reported after blanking, and what userspace expects  */
625         if (cd_media_dvd_rw_ro && (header[2] & 3) == 1)
626                 cd_media_state = media_status[0];
627
628         /* DVD+RW discs (and DVD-RW in restricted mode) once formatted are
629          * always "complete", DVD-RAM are "other" or "complete" if the disc is
630          * write protected; we need to check the contents if it is blank */
631         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) {
632                 unsigned char buffer[32 * 2048];
633                 unsigned char result, len;
634                 int block, offset;
635
636                 if (cd_media_dvd_ram) {
637                         /* a write protected dvd-ram may report "complete" status */
638
639                         unsigned char dvdstruct[8];
640                         unsigned char format[12];
641
642                         scsi_cmd_init(udev, &sc, dvdstruct, sizeof(dvdstruct));
643                         scsi_cmd_set(udev, &sc, 0, 0xAD);
644                         scsi_cmd_set(udev, &sc, 7, 0xC0);
645                         scsi_cmd_set(udev, &sc, 9, sizeof(dvdstruct));
646                         scsi_cmd_set(udev, &sc, 11, 0);
647                         err = scsi_cmd_run(udev, &sc, fd, dvdstruct, sizeof(dvdstruct));
648                         if ((err != 0)) {
649                                 info_scsi_cmd_err(udev, "READ DVD STRUCTURE", err);
650                                 return -1;
651                         }
652                         if (dvdstruct[4] & 0x02) {
653                                 cd_media_state = media_status[2];
654                                 info(udev, "write-protected DVD-RAM media inserted\n");
655                                 goto determined;
656                         }
657
658                         /* let's make sure we don't try to read unformatted media */
659                         scsi_cmd_init(udev, &sc, format, sizeof(format));
660                         scsi_cmd_set(udev, &sc, 0, 0x23);
661                         scsi_cmd_set(udev, &sc, 8, sizeof(format));
662                         scsi_cmd_set(udev, &sc, 9, 0);
663                         err = scsi_cmd_run(udev, &sc, fd, format, sizeof(format));
664                         if ((err != 0)) {
665                                 info_scsi_cmd_err(udev, "READ DVD FORMAT CAPACITIES", err);
666                                 return -1;
667                         }
668
669                         len = format[3];
670                         if (len & 7 || len < 16) {
671                                 info(udev, "invalid format capacities length\n");
672                                 return -1;
673                         }
674
675                         switch(format[8] & 3) {
676                             case 1:
677                                 info(udev, "unformatted DVD-RAM media inserted\n");
678                                 /* This means that last format was interrupted
679                                  * or failed, blank dvd-ram discs are factory
680                                  * formatted. Take no action here as it takes
681                                  * quite a while to reformat a dvd-ram and it's
682                                  * not automatically started */
683                                 goto determined;
684
685                             case 2:
686                                 info(udev, "formatted DVD-RAM media inserted\n");
687                                 break;
688
689                             case 3:
690                                 cd_media = 0; //return no media
691                                 info(udev, "format capacities returned no media\n");
692                                 return -1;
693                         }
694                 }
695
696                 /* Take a closer look at formatted media (unformatted DVD+RW
697                  * has "blank" status", DVD-RAM was examined earlier) and check
698                  * for ISO and UDF PVDs or a fs superblock presence and do it
699                  * in one ioctl (we need just sectors 0 and 16) */
700                 scsi_cmd_init(udev, &sc, buffer, sizeof(buffer));
701                 scsi_cmd_set(udev, &sc, 0, 0x28);
702                 scsi_cmd_set(udev, &sc, 5, 0);
703                 scsi_cmd_set(udev, &sc, 8, 32);
704                 scsi_cmd_set(udev, &sc, 9, 0);
705                 err = scsi_cmd_run(udev, &sc, fd, buffer, sizeof(buffer));
706                 if ((err != 0)) {
707                         cd_media = 0;
708                         info_scsi_cmd_err(udev, "READ FIRST 32 BLOCKS", err);
709                         return -1;
710                 }
711
712                 /* if any non-zero data is found in sector 16 (iso and udf) or
713                  * eventually 0 (fat32 boot sector, ext2 superblock, etc), disc
714                  * is assumed non-blank */
715                 result = 0;
716
717                 for (block = 32768; block >= 0 && !result; block -= 32768) {
718                         offset = block;
719                         while (offset < (block + 2048) && !result) {
720                                 result = buffer [offset];
721                                 offset++;
722                         }
723                 }
724
725                 if (!result) {
726                         cd_media_state = media_status[0];
727                         info(udev, "no data in blocks 0 or 16, assuming blank\n");
728                 } else {
729                         info(udev, "data in blocks 0 or 16, assuming complete\n");
730                 }
731         }
732
733 determined:
734         /* "other" is e. g. DVD-RAM, can't append sessions there; DVDs in
735          * restricted overwrite mode can never append, only in sequential mode */
736         if ((header[2] & 3) < 2 && !cd_media_dvd_rw_ro)
737                 cd_media_session_next = header[10] << 8 | header[5];
738         cd_media_session_count = header[9] << 8 | header[4];
739         cd_media_track_count = header[11] << 8 | header[6];
740
741         return 0;
742 }
743
744 static int cd_media_toc(struct udev *udev, int fd)
745 {
746         struct scsi_cmd sc;
747         unsigned char header[12];
748         unsigned char toc[65536];
749         unsigned int len, i, num_tracks;
750         unsigned char *p;
751         int err;
752
753         scsi_cmd_init(udev, &sc, header, sizeof(header));
754         scsi_cmd_set(udev, &sc, 0, 0x43);
755         scsi_cmd_set(udev, &sc, 6, 1);
756         scsi_cmd_set(udev, &sc, 8, sizeof(header) & 0xff);
757         scsi_cmd_set(udev, &sc, 9, 0);
758         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
759         if ((err != 0)) {
760                 info_scsi_cmd_err(udev, "READ TOC", err);
761                 return -1;
762         }
763
764         len = (header[0] << 8 | header[1]) + 2;
765         info(udev, "READ TOC: len: %d, start track: %d, end track: %d\n", len, header[2], header[3]);
766         if (len > sizeof(toc))
767                 return -1;
768         if (len < 2)
769                 return -1;
770         /* 2: first track, 3: last track */
771         num_tracks = header[3] - header[2] + 1;
772
773         /* empty media has no tracks */
774         if (len < 8)
775                 return 0;
776
777         scsi_cmd_init(udev, &sc, toc, sizeof(toc));
778         scsi_cmd_set(udev, &sc, 0, 0x43);
779         scsi_cmd_set(udev, &sc, 6, header[2]); /* First Track/Session Number */
780         scsi_cmd_set(udev, &sc, 7, (len >> 8) & 0xff);
781         scsi_cmd_set(udev, &sc, 8, len & 0xff);
782         scsi_cmd_set(udev, &sc, 9, 0);
783         err = scsi_cmd_run(udev, &sc, fd, toc, len);
784         if ((err != 0)) {
785                 info_scsi_cmd_err(udev, "READ TOC (tracks)", err);
786                 return -1;
787         }
788
789         /* Take care to not iterate beyond the last valid track as specified in
790          * the TOC, but also avoid going beyond the TOC length, just in case
791          * the last track number is invalidly large */
792         for (p = toc+4, i = 4; i < len-8 && num_tracks > 0; i += 8, p += 8, --num_tracks) {
793                 unsigned int block;
794                 unsigned int is_data_track;
795
796                 is_data_track = (p[1] & 0x04) != 0;
797
798                 block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
799                 info(udev, "track=%u info=0x%x(%s) start_block=%u\n",
800                      p[2], p[1] & 0x0f, is_data_track ? "data":"audio", block);
801
802                 if (is_data_track)
803                         cd_media_track_count_data++;
804                 else
805                         cd_media_track_count_audio++;
806         }
807
808         scsi_cmd_init(udev, &sc, header, sizeof(header));
809         scsi_cmd_set(udev, &sc, 0, 0x43);
810         scsi_cmd_set(udev, &sc, 2, 1); /* Session Info */
811         scsi_cmd_set(udev, &sc, 8, sizeof(header));
812         scsi_cmd_set(udev, &sc, 9, 0);
813         err = scsi_cmd_run(udev, &sc, fd, header, sizeof(header));
814         if ((err != 0)) {
815                 info_scsi_cmd_err(udev, "READ TOC (multi session)", err);
816                 return -1;
817         }
818         len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7];
819         info(udev, "last track %u starts at block %u\n", header[4+2], len);
820         cd_media_session_last_offset = (unsigned long long int)len * 2048;
821         return 0;
822 }
823
824 int main(int argc, char *argv[])
825 {
826         struct udev *udev;
827         static const struct option options[] = {
828                 { "export", no_argument, NULL, 'x' },
829                 { "debug", no_argument, NULL, 'd' },
830                 { "help", no_argument, NULL, 'h' },
831                 {}
832         };
833         const char *node = NULL;
834         int export = 0;
835         int fd = -1;
836         int cnt;
837         int rc = 0;
838
839         udev = udev_new();
840         if (udev == NULL)
841                 goto exit;
842
843         udev_log_init("cdrom_id");
844         udev_set_log_fn(udev, log_fn);
845
846         while (1) {
847                 int option;
848
849                 option = getopt_long(argc, argv, "dxh", options, NULL);
850                 if (option == -1)
851                         break;
852
853                 switch (option) {
854                 case 'd':
855                         debug = 1;
856                         if (udev_get_log_priority(udev) < LOG_INFO)
857                                 udev_set_log_priority(udev, LOG_INFO);
858                         break;
859                 case 'x':
860                         export = 1;
861                         break;
862                 case 'h':
863                         printf("Usage: cdrom_id [options] <device>\n"
864                                "  --export        export key/value pairs\n"
865                                "  --debug         debug to stderr\n"
866                                "  --help          print this help text\n\n");
867                         goto exit;
868                 default:
869                         rc = 1;
870                         goto exit;
871                 }
872         }
873
874         node = argv[optind];
875         if (!node) {
876                 err(udev, "no device\n");
877                 fprintf(stderr, "no device\n");
878                 rc = 1;
879                 goto exit;
880         }
881
882         srand((unsigned int)getpid());
883         for (cnt = 20; cnt > 0; cnt--) {
884                 struct timespec duration;
885
886                 fd = open(node, O_RDONLY|O_NONBLOCK|(is_mounted(node) ? 0 : O_EXCL));
887                 if (fd >= 0 || errno != EBUSY)
888                         break;
889                 duration.tv_sec = 0;
890                 duration.tv_nsec = (100 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
891                 nanosleep(&duration, NULL);
892         }
893         if (fd < 0) {
894                 info(udev, "unable to open '%s'\n", node);
895                 fprintf(stderr, "unable to open '%s'\n", node);
896                 rc = 1;
897                 goto exit;
898         }
899         info(udev, "probing: '%s'\n", node);
900
901         /* same data as original cdrom_id */
902         if (cd_capability_compat(udev, fd) < 0) {
903                 rc = 1;
904                 goto exit;
905         }
906
907         /* check for media - don't bail if there's no media as we still need to
908          * to read profiles */
909         cd_media_compat(udev, fd);
910
911         /* check if drive talks MMC */
912         if (cd_inquiry(udev, fd) < 0)
913                 goto print;
914
915         /* read drive and possibly current profile */
916         if (cd_profiles(udev, fd) != 0)
917                 goto print;
918
919         /* at this point we are guaranteed to have media in the
920          * drive - find out more about it */
921
922         /* get session/track info */
923         cd_media_toc(udev, fd);
924
925         /* get writable media state */
926         cd_media_info(udev, fd);
927
928 print:
929         printf("ID_CDROM=1\n");
930         if (cd_cd_rom)
931                 printf("ID_CDROM_CD=1\n");
932         if (cd_cd_r)
933                 printf("ID_CDROM_CD_R=1\n");
934         if (cd_cd_rw)
935                 printf("ID_CDROM_CD_RW=1\n");
936         if (cd_dvd_rom)
937                 printf("ID_CDROM_DVD=1\n");
938         if (cd_dvd_r)
939                 printf("ID_CDROM_DVD_R=1\n");
940         if (cd_dvd_rw)
941                 printf("ID_CDROM_DVD_RW=1\n");
942         if (cd_dvd_ram)
943                 printf("ID_CDROM_DVD_RAM=1\n");
944         if (cd_dvd_plus_r)
945                 printf("ID_CDROM_DVD_PLUS_R=1\n");
946         if (cd_dvd_plus_rw)
947                 printf("ID_CDROM_DVD_PLUS_RW=1\n");
948         if (cd_dvd_plus_r_dl)
949                 printf("ID_CDROM_DVD_PLUS_R_DL=1\n");
950         if (cd_dvd_plus_rw_dl)
951                 printf("ID_CDROM_DVD_PLUS_RW_DL=1\n");
952         if (cd_bd)
953                 printf("ID_CDROM_BD=1\n");
954         if (cd_bd_r)
955                 printf("ID_CDROM_BD_R=1\n");
956         if (cd_bd_re)
957                 printf("ID_CDROM_BD_RE=1\n");
958         if (cd_hddvd)
959                 printf("ID_CDROM_HDDVD=1\n");
960         if (cd_hddvd_r)
961                 printf("ID_CDROM_HDDVD_R=1\n");
962         if (cd_hddvd_rw)
963                 printf("ID_CDROM_HDDVD_RW=1\n");
964         if (cd_mo)
965                 printf("ID_CDROM_MO=1\n");
966         if (cd_mrw)
967                 printf("ID_CDROM_MRW=1\n");
968         if (cd_mrw_w)
969                 printf("ID_CDROM_MRW_W=1\n");
970
971         if (cd_media)
972                 printf("ID_CDROM_MEDIA=1\n");
973         if (cd_media_mo)
974                 printf("ID_CDROM_MEDIA_MO=1\n");
975         if (cd_media_mrw)
976                 printf("ID_CDROM_MEDIA_MRW=1\n");
977         if (cd_media_mrw_w)
978                 printf("ID_CDROM_MEDIA_MRW_W=1\n");
979         if (cd_media_cd_rom)
980                 printf("ID_CDROM_MEDIA_CD=1\n");
981         if (cd_media_cd_r)
982                 printf("ID_CDROM_MEDIA_CD_R=1\n");
983         if (cd_media_cd_rw)
984                 printf("ID_CDROM_MEDIA_CD_RW=1\n");
985         if (cd_media_dvd_rom)
986                 printf("ID_CDROM_MEDIA_DVD=1\n");
987         if (cd_media_dvd_r)
988                 printf("ID_CDROM_MEDIA_DVD_R=1\n");
989         if (cd_media_dvd_ram)
990                 printf("ID_CDROM_MEDIA_DVD_RAM=1\n");
991         if (cd_media_dvd_rw)
992                 printf("ID_CDROM_MEDIA_DVD_RW=1\n");
993         if (cd_media_dvd_plus_r)
994                 printf("ID_CDROM_MEDIA_DVD_PLUS_R=1\n");
995         if (cd_media_dvd_plus_rw)
996                 printf("ID_CDROM_MEDIA_DVD_PLUS_RW=1\n");
997         if (cd_media_dvd_plus_rw_dl)
998                 printf("ID_CDROM_MEDIA_DVD_PLUS_RW_DL=1\n");
999         if (cd_media_dvd_plus_r_dl)
1000                 printf("ID_CDROM_MEDIA_DVD_PLUS_R_DL=1\n");
1001         if (cd_media_bd)
1002                 printf("ID_CDROM_MEDIA_BD=1\n");
1003         if (cd_media_bd_r)
1004                 printf("ID_CDROM_MEDIA_BD_R=1\n");
1005         if (cd_media_bd_re)
1006                 printf("ID_CDROM_MEDIA_BD_RE=1\n");
1007         if (cd_media_hddvd)
1008                 printf("ID_CDROM_MEDIA_HDDVD=1\n");
1009         if (cd_media_hddvd_r)
1010                 printf("ID_CDROM_MEDIA_HDDVD_R=1\n");
1011         if (cd_media_hddvd_rw)
1012                 printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
1013
1014         if (cd_media_state != NULL)
1015                 printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state);
1016         if (cd_media_session_next > 0)
1017                 printf("ID_CDROM_MEDIA_SESSION_NEXT=%d\n", cd_media_session_next);
1018         if (cd_media_session_count > 0)
1019                 printf("ID_CDROM_MEDIA_SESSION_COUNT=%d\n", cd_media_session_count);
1020         if (cd_media_session_count > 1 && cd_media_session_last_offset > 0)
1021                 printf("ID_CDROM_MEDIA_SESSION_LAST_OFFSET=%llu\n", cd_media_session_last_offset);
1022         if (cd_media_track_count > 0)
1023                 printf("ID_CDROM_MEDIA_TRACK_COUNT=%d\n", cd_media_track_count);
1024         if (cd_media_track_count_audio > 0)
1025                 printf("ID_CDROM_MEDIA_TRACK_COUNT_AUDIO=%d\n", cd_media_track_count_audio);
1026         if (cd_media_track_count_data > 0)
1027                 printf("ID_CDROM_MEDIA_TRACK_COUNT_DATA=%d\n", cd_media_track_count_data);
1028 exit:
1029         if (fd >= 0)
1030                 close(fd);
1031         udev_unref(udev);
1032         udev_log_close();
1033         return rc;
1034 }
1035