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