chiark / gitweb /
Merge gregkh@ehlo.org:/home/kay/public_html/pub/scm/linux/hotplug/udev-kay
[elogind.git] / extras / ata_id / ata_id.c
1 /*
2  * ata_id - reads product/serial number from ATA drives
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This library is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU Lesser General Public
8  *      License as published by the Free Software Foundation; either
9  *      version 2.1 of the License, or (at your option) any later version.
10  *
11  *      This library 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 GNU
14  *      Lesser General Public License for more details.
15  *
16  *      You should have received a copy of the GNU Lesser General Public
17  *      License along with this library; if not, write to the Free Software
18  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <linux/types.h>
35 #include <linux/hdreg.h>
36
37 #include "../../logging.h"
38 #include "../../udev_utils.h"
39
40 #ifdef USE_LOG
41 void log_message(int priority, const char *format, ...)
42 {
43         va_list args;
44         static int udev_log = -1;
45
46         if (udev_log == -1) {
47                 const char *value;
48
49                 value = getenv("UDEV_LOG");
50                 if (value)
51                         udev_log = log_priority(value);
52                 else
53                         udev_log = LOG_ERR;
54         }
55
56         if (priority > udev_log)
57                 return;
58
59         va_start(args, format);
60         vsyslog(priority, format, args);
61         va_end(args);
62 }
63 #endif
64
65 static void set_str(char *to, const unsigned char *from, int count)
66 {
67         int i, j;
68         int len;
69
70         /* strip trailing whitespace */
71         len = strnlen(from, count);
72         while (isspace(from[len-1]))
73                 len--;
74
75         /* strip leading whitespace */
76         i = 0;
77         while (isspace(from[i]) && (i < len))
78                 i++;
79
80         j = 0;
81         while (i < len) {
82                 /* substitute multiple whitespace */
83                 if (isspace(from[i])) {
84                         while (isspace(from[i]))
85                                 i++;
86                         to[j++] = '_';
87                 }
88                 /* skip chars */
89                 if (from[i] == '/') {
90                         i++;
91                         continue;
92                 }
93                 to[j++] = from[i++];
94         }
95         to[j] = '\0';
96 }
97
98 int main(int argc, char *argv[])
99 {
100         struct hd_driveid id;
101         char model[41];
102         char serial[21];
103         char revision[9];
104         const char *node = NULL;
105         int i;
106         int export = 0;
107         int fd;
108         int rc = 0;
109
110         logging_init("ata_id");
111
112         for (i = 1 ; i < argc; i++) {
113                 char *arg = argv[i];
114
115                 if (strcmp(arg, "--export") == 0) {
116                         export = 1;
117                 } else
118                         node = arg;
119         }
120         if (!node) {
121                 err("no node specified");
122                 rc = 1;
123                 goto exit;
124         }
125
126         fd = open(node, O_RDONLY);
127         if (fd < 0)
128                 if (errno == ENOMEDIUM)
129                         fd = open(node, O_RDONLY|O_NONBLOCK);
130         if (fd < 0) {
131                 err("unable to open '%s'", node);
132                 rc = 1;
133                 goto exit;
134         }
135
136         if (ioctl(fd, HDIO_GET_IDENTITY, &id)) {
137                 err("HDIO_GET_IDENTITY failed for '%s'", node);
138                 rc = 3;
139                 goto close;
140         }
141
142         set_str(model, id.model, 40);
143         set_str(serial, id.serial_no, 20);
144         set_str(revision, id.fw_rev, 8);
145
146         if (export) {
147                 if ((id.config >> 8) & 0x80) {
148                         /* This is an ATAPI device */
149                         switch ((id.config >> 8) & 0x1f) {
150                         case 0:
151                                 printf("ID_TYPE=cd\n");
152                                 break;
153                         case 1:
154                                 printf("ID_TYPE=tape\n");
155                                 break;
156                         case 5:
157                                 printf("ID_TYPE=cd\n");
158                                 break;
159                         case 7:
160                                 printf("ID_TYPE=optical\n");
161                                 break;
162                         default:
163                                 printf("ID_TYPE=generic\n");
164                                 break;
165                         }
166                 } else {
167                         printf("ID_TYPE=disk\n");
168                 }
169                 printf("ID_MODEL=%s\n", model);
170                 printf("ID_SERIAL=%s\n", serial);
171                 printf("ID_REVISION=%s\n", revision);
172         } else {
173                 if (serial[0] != '\0')
174                         printf("%s_%s\n", model, serial);
175                 else
176                         printf("%s\n", model);
177         }
178
179 close:
180         close(fd);
181 exit:
182         logging_close();
183         return rc;
184 }