]> git.ipfire.org Git - thirdparty/systemd.git/blob - extras/scsi_id/scsi_id.c
d630cf902b16de4531aab7fefbb6a14d03458ba5
[thirdparty/systemd.git] / extras / scsi_id / scsi_id.c
1 /*
2 * scsi_id.c
3 *
4 * Main section of the scsi_id program
5 *
6 * Copyright (C) IBM Corp. 2003
7 * Copyright (C) SUSE Linux Products GmbH, 2006
8 *
9 * Author:
10 * Patrick Mansfield<patmans@us.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation version 2 of the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <signal.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <syslog.h>
25 #include <stdarg.h>
26 #include <ctype.h>
27 #include <getopt.h>
28 #include <sys/stat.h>
29
30 #include "../../udev/udev.h"
31 #include "scsi_id.h"
32
33 static const struct option options[] = {
34 { "device", required_argument, NULL, 'd' },
35 { "config", required_argument, NULL, 'f' },
36 { "page", required_argument, NULL, 'p' },
37 { "blacklisted", no_argument, NULL, 'b' },
38 { "whitelisted", no_argument, NULL, 'g' },
39 { "replace-whitespace", no_argument, NULL, 'u' },
40 { "sg-version", required_argument, NULL, 's' },
41 { "verbose", no_argument, NULL, 'v' },
42 { "version", no_argument, NULL, 'V' },
43 { "export", no_argument, NULL, 'x' },
44 { "help", no_argument, NULL, 'h' },
45 {}
46 };
47
48 static const char short_options[] = "d:f:ghip:uvVx";
49 static const char dev_short_options[] = "bgp:";
50
51 static int all_good;
52 static int dev_specified;
53 static char config_file[MAX_PATH_LEN] = SYSCONFDIR "/scsi_id.config";
54 static enum page_code default_page_code;
55 static int sg_version = 4;
56 static int use_stderr;
57 static int debug;
58 static int reformat_serial;
59 static int export;
60 static char vendor_str[64];
61 static char model_str[64];
62 static char vendor_enc_str[256];
63 static char model_enc_str[256];
64 static char revision_str[16];
65 static char type_str[16];
66
67 static void log_fn(struct udev *udev, int priority,
68 const char *file, int line, const char *fn,
69 const char *format, va_list args)
70 {
71 vsyslog(priority, format, args);
72 }
73
74 static void set_type(const char *from, char *to, size_t len)
75 {
76 int type_num;
77 char *eptr;
78 char *type = "generic";
79
80 type_num = strtoul(from, &eptr, 0);
81 if (eptr != from) {
82 switch (type_num) {
83 case 0:
84 type = "disk";
85 break;
86 case 1:
87 type = "tape";
88 break;
89 case 4:
90 type = "optical";
91 break;
92 case 5:
93 type = "cd";
94 break;
95 case 7:
96 type = "optical";
97 break;
98 case 0xe:
99 type = "disk";
100 break;
101 case 0xf:
102 type = "optical";
103 break;
104 default:
105 break;
106 }
107 }
108 util_strlcpy(to, type, len);
109 }
110
111 /*
112 * get_value:
113 *
114 * buf points to an '=' followed by a quoted string ("foo") or a string ending
115 * with a space or ','.
116 *
117 * Return a pointer to the NUL terminated string, returns NULL if no
118 * matches.
119 */
120 static char *get_value(char **buffer)
121 {
122 static char *quote_string = "\"\n";
123 static char *comma_string = ",\n";
124 char *val;
125 char *end;
126
127 if (**buffer == '"') {
128 /*
129 * skip leading quote, terminate when quote seen
130 */
131 (*buffer)++;
132 end = quote_string;
133 } else {
134 end = comma_string;
135 }
136 val = strsep(buffer, end);
137 if (val && end == quote_string)
138 /*
139 * skip trailing quote
140 */
141 (*buffer)++;
142
143 while (isspace(**buffer))
144 (*buffer)++;
145
146 return val;
147 }
148
149 static int argc_count(char *opts)
150 {
151 int i = 0;
152 while (*opts != '\0')
153 if (*opts++ == ' ')
154 i++;
155 return i;
156 }
157
158 /*
159 * get_file_options:
160 *
161 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
162 * if vendor and model are set find the first OPTIONS line in the config
163 * file that matches. Set argc and argv to match the OPTIONS string.
164 *
165 * vendor and model can end in '\n'.
166 */
167 static int get_file_options(struct udev *udev,
168 const char *vendor, const char *model,
169 int *argc, char ***newargv)
170 {
171 char *buffer;
172 FILE *fd;
173 char *buf;
174 char *str1;
175 char *vendor_in, *model_in, *options_in; /* read in from file */
176 int lineno;
177 int c;
178 int retval = 0;
179
180 dbg(udev, "vendor='%s'; model='%s'\n", vendor, model);
181 fd = fopen(config_file, "r");
182 if (fd == NULL) {
183 dbg(udev, "can't open %s\n", config_file);
184 if (errno == ENOENT) {
185 return 1;
186 } else {
187 err(udev, "can't open %s: %s\n", config_file, strerror(errno));
188 return -1;
189 }
190 }
191
192 /*
193 * Allocate a buffer rather than put it on the stack so we can
194 * keep it around to parse any options (any allocated newargv
195 * points into this buffer for its strings).
196 */
197 buffer = malloc(MAX_BUFFER_LEN);
198 if (!buffer) {
199 err(udev, "can't allocate memory\n");
200 return -1;
201 }
202
203 *newargv = NULL;
204 lineno = 0;
205 while (1) {
206 vendor_in = model_in = options_in = NULL;
207
208 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
209 if (buf == NULL)
210 break;
211 lineno++;
212 if (buf[strlen(buffer) - 1] != '\n') {
213 err(udev, "Config file line %d too long\n", lineno);
214 break;
215 }
216
217 while (isspace(*buf))
218 buf++;
219
220 /* blank or all whitespace line */
221 if (*buf == '\0')
222 continue;
223
224 /* comment line */
225 if (*buf == '#')
226 continue;
227
228 dbg(udev, "lineno %d: '%s'\n", lineno, buf);
229 str1 = strsep(&buf, "=");
230 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
231 str1 = get_value(&buf);
232 if (!str1) {
233 retval = -1;
234 break;
235 }
236 vendor_in = str1;
237
238 str1 = strsep(&buf, "=");
239 if (str1 && strcasecmp(str1, "MODEL") == 0) {
240 str1 = get_value(&buf);
241 if (!str1) {
242 retval = -1;
243 break;
244 }
245 model_in = str1;
246 str1 = strsep(&buf, "=");
247 }
248 }
249
250 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
251 str1 = get_value(&buf);
252 if (!str1) {
253 retval = -1;
254 break;
255 }
256 options_in = str1;
257 }
258 dbg(udev, "config file line %d:\n"
259 " vendor '%s'; model '%s'; options '%s'\n",
260 lineno, vendor_in, model_in, options_in);
261 /*
262 * Only allow: [vendor=foo[,model=bar]]options=stuff
263 */
264 if (!options_in || (!vendor_in && model_in)) {
265 err(udev, "Error parsing config file line %d '%s'\n", lineno, buffer);
266 retval = -1;
267 break;
268 }
269 if (vendor == NULL) {
270 if (vendor_in == NULL) {
271 dbg(udev, "matched global option\n");
272 break;
273 }
274 } else if ((vendor_in && strncmp(vendor, vendor_in,
275 strlen(vendor_in)) == 0) &&
276 (!model_in || (strncmp(model, model_in,
277 strlen(model_in)) == 0))) {
278 /*
279 * Matched vendor and optionally model.
280 *
281 * Note: a short vendor_in or model_in can
282 * give a partial match (that is FOO
283 * matches FOOBAR).
284 */
285 dbg(udev, "matched vendor/model\n");
286 break;
287 } else {
288 dbg(udev, "no match\n");
289 }
290 }
291
292 if (retval == 0) {
293 if (vendor_in != NULL || model_in != NULL ||
294 options_in != NULL) {
295 /*
296 * Something matched. Allocate newargv, and store
297 * values found in options_in.
298 */
299 strcpy(buffer, options_in);
300 c = argc_count(buffer) + 2;
301 *newargv = calloc(c, sizeof(**newargv));
302 if (!*newargv) {
303 err(udev, "can't allocate memory\n");
304 retval = -1;
305 } else {
306 *argc = c;
307 c = 0;
308 /*
309 * argv[0] at 0 is skipped by getopt, but
310 * store the buffer address there for
311 * later freeing
312 */
313 (*newargv)[c] = buffer;
314 for (c = 1; c < *argc; c++)
315 (*newargv)[c] = strsep(&buffer, " \t");
316 }
317 } else {
318 /* No matches */
319 retval = 1;
320 }
321 }
322 if (retval != 0)
323 free(buffer);
324 fclose(fd);
325 return retval;
326 }
327
328 static int set_options(struct udev *udev,
329 int argc, char **argv, const char *short_opts,
330 char *maj_min_dev)
331 {
332 int option;
333
334 /*
335 * optind is a global extern used by getopt. Since we can call
336 * set_options twice (once for command line, and once for config
337 * file) we have to reset this back to 1.
338 */
339 optind = 1;
340 while (1) {
341 option = getopt_long(argc, argv, short_opts, options, NULL);
342 if (option == -1)
343 break;
344
345 if (optarg)
346 dbg(udev, "option '%c' arg '%s'\n", option, optarg);
347 else
348 dbg(udev, "option '%c'\n", option);
349
350 switch (option) {
351 case 'b':
352 all_good = 0;
353 break;
354
355 case 'd':
356 dev_specified = 1;
357 util_strlcpy(maj_min_dev, optarg, MAX_PATH_LEN);
358 break;
359
360 case 'e':
361 use_stderr = 1;
362 break;
363
364 case 'f':
365 util_strlcpy(config_file, optarg, MAX_PATH_LEN);
366 break;
367
368 case 'g':
369 all_good = 1;
370 break;
371
372 case 'h':
373 printf("Usage: scsi_id OPTIONS <device>\n"
374 " --device= device node for SG_IO commands\n"
375 " --config= location of config file\n"
376 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
377 " --sg-version=3|4 use SGv3 or SGv4\n"
378 " --blacklisted threat device as blacklisted\n"
379 " --whitelisted threat device as whitelisted\n"
380 " --replace-whitespace replace all whitespaces by underscores\n"
381 " --verbose verbose logging\n"
382 " --version print version\n"
383 " --export print values as environment keys\n"
384 " --help print this help text\n\n");
385 exit(0);
386
387 case 'p':
388 if (strcmp(optarg, "0x80") == 0) {
389 default_page_code = PAGE_80;
390 } else if (strcmp(optarg, "0x83") == 0) {
391 default_page_code = PAGE_83;
392 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
393 default_page_code = PAGE_83_PRE_SPC3;
394 } else {
395 err(udev, "Unknown page code '%s'\n", optarg);
396 return -1;
397 }
398 break;
399
400 case 's':
401 sg_version = atoi(optarg);
402 if (sg_version < 3 || sg_version > 4) {
403 err(udev, "Unknown SG version '%s'\n", optarg);
404 return -1;
405 }
406 break;
407
408 case 'u':
409 reformat_serial = 1;
410 break;
411
412 case 'x':
413 export = 1;
414 break;
415
416 case 'v':
417 debug++;
418 break;
419
420 case 'V':
421 printf("%s\n", VERSION);
422 exit(0);
423 break;
424
425 default:
426 exit(1);
427 }
428 }
429 if (optind < argc && !dev_specified) {
430 dev_specified = 1;
431 util_strlcpy(maj_min_dev, argv[optind], MAX_PATH_LEN);
432 }
433 return 0;
434 }
435
436 static int per_dev_options(struct udev *udev,
437 struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
438 {
439 int retval;
440 int newargc;
441 char **newargv = NULL;
442 int option;
443
444 *good_bad = all_good;
445 *page_code = default_page_code;
446
447 retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
448
449 optind = 1; /* reset this global extern */
450 while (retval == 0) {
451 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
452 if (option == -1)
453 break;
454
455 if (optarg)
456 dbg(udev, "option '%c' arg '%s'\n", option, optarg);
457 else
458 dbg(udev, "option '%c'\n", option);
459
460 switch (option) {
461 case 'b':
462 *good_bad = 0;
463 break;
464
465 case 'g':
466 *good_bad = 1;
467 break;
468
469 case 'p':
470 if (strcmp(optarg, "0x80") == 0) {
471 *page_code = PAGE_80;
472 } else if (strcmp(optarg, "0x83") == 0) {
473 *page_code = PAGE_83;
474 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
475 *page_code = PAGE_83_PRE_SPC3;
476 } else {
477 err(udev, "Unknown page code '%s'\n", optarg);
478 retval = -1;
479 }
480 break;
481
482 default:
483 err(udev, "Unknown or bad option '%c' (0x%x)\n", option, option);
484 retval = -1;
485 break;
486 }
487 }
488
489 if (newargv) {
490 free(newargv[0]);
491 free(newargv);
492 }
493 return retval;
494 }
495
496 static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
497 {
498 int retval;
499
500 dev_scsi->use_sg = sg_version;
501
502 retval = scsi_std_inquiry(udev, dev_scsi, path);
503 if (retval)
504 return retval;
505
506 udev_util_encode_string(dev_scsi->vendor, vendor_enc_str, sizeof(vendor_enc_str));
507 udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
508
509 udev_util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
510 udev_util_replace_chars(vendor_str, NULL);
511 udev_util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
512 udev_util_replace_chars(model_str, NULL);
513 set_type(dev_scsi->type, type_str, sizeof(type_str));
514 udev_util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
515 udev_util_replace_chars(revision_str, NULL);
516 return 0;
517 }
518
519 /*
520 * format_serial: replace to whitespaces by underscores for calling
521 * programs that use the serial for device naming (multipath, Suse
522 * naming, etc...)
523 */
524 static void format_serial(char *serial)
525 {
526 char *p = serial, *q;
527
528 q = p;
529 while (*p != '\0') {
530 if (isspace(*p)) {
531 if (q > serial && q[-1] != '_') {
532 *q = '_';
533 q++;
534 }
535 } else {
536 *q = *p;
537 q++;
538 }
539 p++;
540 }
541 *q = '\0';
542 }
543
544 /*
545 * scsi_id: try to get an id, if one is found, printf it to stdout.
546 * returns a value passed to exit() - 0 if printed an id, else 1.
547 */
548 static int scsi_id(struct udev *udev, char *maj_min_dev)
549 {
550 struct scsi_id_device dev_scsi;
551 int good_dev;
552 int page_code;
553 int retval = 0;
554
555 memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
556
557 if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
558 retval = 1;
559 goto out;
560 }
561
562 /* get per device (vendor + model) options from the config file */
563 per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
564 dbg(udev, "per dev options: good %d; page code 0x%x\n", good_dev, page_code);
565 if (!good_dev) {
566 retval = 1;
567 goto out;
568 }
569
570 /* read serial number from mode pages (no values for optical drives) */
571 scsi_get_serial(udev, &dev_scsi, maj_min_dev, page_code, MAX_SERIAL_LEN);
572
573 if (export) {
574 char serial_str[MAX_SERIAL_LEN];
575
576 printf("ID_VENDOR=%s\n", vendor_str);
577 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str);
578 printf("ID_MODEL=%s\n", model_str);
579 printf("ID_MODEL_ENC=%s\n", model_enc_str);
580 printf("ID_REVISION=%s\n", revision_str);
581 printf("ID_TYPE=%s\n", type_str);
582 if (dev_scsi.serial[0] != '\0') {
583 udev_util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
584 udev_util_replace_chars(serial_str, NULL);
585 printf("ID_SERIAL=%s\n", serial_str);
586 udev_util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
587 udev_util_replace_chars(serial_str, NULL);
588 printf("ID_SERIAL_SHORT=%s\n", serial_str);
589 }
590 goto out;
591 }
592
593 if (dev_scsi.serial[0] == '\0') {
594 retval = 1;
595 goto out;
596 }
597 if (reformat_serial)
598 format_serial(dev_scsi.serial);
599 printf("%s\n", dev_scsi.serial);
600 out:
601 return retval;
602 }
603
604 int main(int argc, char **argv)
605 {
606 struct udev *udev;
607 int retval = 0;
608 char maj_min_dev[MAX_PATH_LEN];
609 int newargc;
610 char **newargv;
611
612 udev = udev_new();
613 if (udev == NULL)
614 goto exit;
615
616 logging_init("scsi_id");
617 udev_set_log_fn(udev, log_fn);
618
619 /*
620 * Get config file options.
621 */
622 newargv = NULL;
623 retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
624 if (retval < 0) {
625 retval = 1;
626 goto exit;
627 }
628 if (newargv && (retval == 0)) {
629 if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
630 retval = 2;
631 goto exit;
632 }
633 free(newargv);
634 }
635
636 /*
637 * Get command line options (overriding any config file settings).
638 */
639 if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
640 exit(1);
641
642 if (!dev_specified) {
643 err(udev, "no device specified\n");
644 retval = 1;
645 goto exit;
646 }
647
648 retval = scsi_id(udev, maj_min_dev);
649
650 exit:
651 udev_unref(udev);
652 logging_close();
653 return retval;
654 }