chiark / gitweb /
556499b8118188887ade898e622332b521a224af
[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 #include <linux/cdrom.h>
30
31 #include "../../logging.h"
32 #include "../../udev_utils.h"
33
34 #ifdef USE_LOG
35 void log_message(int priority, const char *format, ...)
36 {
37         va_list args;
38         static int udev_log = -1;
39
40         if (udev_log == -1) {
41                 const char *value;
42
43                 value = getenv("UDEV_LOG");
44                 if (value)
45                         udev_log = log_priority(value);
46                 else
47                         udev_log = LOG_ERR;
48         }
49
50         if (priority > udev_log)
51                 return;
52
53         va_start(args, format);
54         vsyslog(priority, format, args);
55         va_end(args);
56 }
57 #endif
58
59 int main(int argc, char *argv[])
60 {
61         const char *node = NULL;
62         int i;
63         int export = 0;
64         int fd;
65         int rc = 0;
66         int result;
67
68         logging_init("cdrom_id");
69
70         for (i = 1 ; i < argc; i++) {
71                 char *arg = argv[i];
72
73                 if (strcmp(arg, "--export") == 0) {
74                         export = 1;
75                 } else
76                         node = arg;
77         }
78         if (!node) {
79                 err("no node specified");
80                 rc = 1;
81                 goto exit;
82         }
83
84         fd = open(node, O_RDONLY);
85         if (fd < 0)
86                 if (errno == ENOMEDIUM)
87                         fd = open(node, O_RDONLY|O_NONBLOCK);
88         if (fd < 0) {
89                 err("unable to open '%s'", node);
90                 rc = 1;
91                 goto exit;
92         }
93
94         result = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
95         if (result < 0) {
96                 err("CDROM_GET_CABILITY failed for '%s'", node);
97                 rc = 3;
98                 goto close;
99         }
100
101         printf("ID_CDROM=1\n");
102
103         if (result & CDC_CD_R)
104                 printf("ID_CDROM_CD_R=1\n");
105         if (result & CDC_CD_RW)
106                 printf("ID_CDROM_CD_RW=1\n");
107
108         if (result & CDC_DVD)
109                 printf("ID_CDROM_DVD=1\n");
110         if (result & CDC_DVD_R)
111                 printf("ID_CDROM_DVD_R=1\n");
112         if (result & CDC_DVD_RAM)
113                 printf("ID_CDROM_DVD_RAM=1\n");
114
115         if (result & CDC_MRW)
116                 printf("ID_CDROM_MRW=1\n");
117         if (result & CDC_MRW_W)
118                 printf("ID_CDROM_MRW_W=1\n");
119
120         if (result & CDC_RAM)
121                 printf("ID_CDROM_RAM=1\n");
122         goto close;
123
124 close:
125         close(fd);
126 exit:
127         logging_close();
128         return rc;
129 }