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