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