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