chiark / gitweb /
add "Persistent Device Naming" rules file for disks
[elogind.git] / extras / cdrom_id / cdrom_id.c
1 /*
2  * cdrom_id - determines the capabilities of cdrom drives
3  *
4  * Copyright (C) 2005 Greg Kroah-Hartman <gregkh@suse.de>
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  * Framework based on ata_id which is:
11  *      Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
12  *
13  */
14
15 #ifndef _GNU_SOURCE
16 #define _GNU_SOURCE 1
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <linux/types.h>
29
30 #include "../../logging.h"
31 #include "../../udev_utils.h"
32
33 /*
34  * Taken from the cdrom.h kernel include file.
35  * Included here as some distros don't have an updated version
36  * with all of the DVD flags.  So we just include our own, aren't
37  * we so nice...
38  */
39 #define CDROM_GET_CAPABILITY    0x5331  /* get capabilities */
40
41 /* capability flags used with the uniform CD-ROM driver */
42 #define CDC_CLOSE_TRAY          0x1     /* caddy systems _can't_ close */
43 #define CDC_OPEN_TRAY           0x2     /* but _can_ eject.  */
44 #define CDC_LOCK                0x4     /* disable manual eject */
45 #define CDC_SELECT_SPEED        0x8     /* programmable speed */
46 #define CDC_SELECT_DISC         0x10    /* select disc from juke-box */
47 #define CDC_MULTI_SESSION       0x20    /* read sessions>1 */
48 #define CDC_MCN                 0x40    /* Medium Catalog Number */
49 #define CDC_MEDIA_CHANGED       0x80    /* media changed */
50 #define CDC_PLAY_AUDIO          0x100   /* audio functions */
51 #define CDC_RESET               0x200   /* hard reset device */
52 #define CDC_IOCTLS              0x400   /* driver has non-standard ioctls */
53 #define CDC_DRIVE_STATUS        0x800   /* driver implements drive status */
54 #define CDC_GENERIC_PACKET      0x1000  /* driver implements generic packets */
55 #define CDC_CD_R                0x2000  /* drive is a CD-R */
56 #define CDC_CD_RW               0x4000  /* drive is a CD-RW */
57 #define CDC_DVD                 0x8000  /* drive is a DVD */
58 #define CDC_DVD_R               0x10000 /* drive can write DVD-R */
59 #define CDC_DVD_RAM             0x20000 /* drive can write DVD-RAM */
60 #define CDC_MO_DRIVE            0x40000 /* drive is an MO device */
61 #define CDC_MRW                 0x80000 /* drive can read MRW */
62 #define CDC_MRW_W               0x100000 /* drive can write MRW */
63 #define CDC_RAM                 0x200000 /* ok to open for WRITE */
64
65 #ifdef USE_LOG
66 void log_message(int priority, const char *format, ...)
67 {
68         va_list args;
69         static int udev_log = -1;
70
71         if (udev_log == -1) {
72                 const char *value;
73
74                 value = getenv("UDEV_LOG");
75                 if (value)
76                         udev_log = log_priority(value);
77                 else
78                         udev_log = LOG_ERR;
79         }
80
81         if (priority > udev_log)
82                 return;
83
84         va_start(args, format);
85         vsyslog(priority, format, args);
86         va_end(args);
87 }
88 #endif
89
90 int main(int argc, char *argv[])
91 {
92         const char *node = NULL;
93         int i;
94         int export = 0;
95         int fd;
96         int rc = 0;
97         int result;
98
99         logging_init("cdrom_id");
100
101         for (i = 1 ; i < argc; i++) {
102                 char *arg = argv[i];
103
104                 if (strcmp(arg, "--export") == 0) {
105                         export = 1;
106                 } else
107                         node = arg;
108         }
109         if (!node) {
110                 info("no node specified");
111                 rc = 1;
112                 goto exit;
113         }
114
115         fd = open(node, O_RDONLY|O_NONBLOCK);
116         if (fd < 0) {
117                 info("unable to open '%s'", node);
118                 rc = 1;
119                 goto exit;
120         }
121
122         result = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
123         if (result < 0) {
124                 info("CDROM_GET_CAPABILITY failed for '%s'", node);
125                 rc = 3;
126                 goto close;
127         }
128
129         printf("ID_CDROM=1\n");
130
131         if (result & CDC_CD_R)
132                 printf("ID_CDROM_CD_R=1\n");
133         if (result & CDC_CD_RW)
134                 printf("ID_CDROM_CD_RW=1\n");
135
136         if (result & CDC_DVD)
137                 printf("ID_CDROM_DVD=1\n");
138         if (result & CDC_DVD_R)
139                 printf("ID_CDROM_DVD_R=1\n");
140         if (result & CDC_DVD_RAM)
141                 printf("ID_CDROM_DVD_RAM=1\n");
142
143         if (result & CDC_MRW)
144                 printf("ID_CDROM_MRW=1\n");
145         if (result & CDC_MRW_W)
146                 printf("ID_CDROM_MRW_W=1\n");
147
148         if (result & CDC_RAM)
149                 printf("ID_CDROM_RAM=1\n");
150 close:
151         close(fd);
152 exit:
153         logging_close();
154         return rc;
155 }