]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/scsi_id/scsi_id.c
scsi_id: cleanup
[thirdparty/systemd.git] / src / udev / scsi_id / scsi_id.c
CommitLineData
e0f83fbe 1/*
c521693b 2 * Copyright (C) IBM Corp. 2003
3d94fb87 3 * Copyright (C) SUSE Linux Products GmbH, 2006
c521693b 4 *
8b5651bb
KS
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.
c521693b 9 *
8b5651bb
KS
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/>.
c521693b
GKH
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
ed142bdb
ZJS
21#include <stdarg.h>
22#include <stdbool.h>
c521693b
GKH
23#include <unistd.h>
24#include <signal.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <string.h>
28#include <syslog.h>
c521693b 29#include <ctype.h>
9eaa50d0 30#include <getopt.h>
c521693b 31#include <sys/stat.h>
c521693b 32
9060b066
KS
33#include "libudev.h"
34#include "libudev-private.h"
1aa1e248 35#include "scsi_id.h"
ed142bdb 36#include "udev-util.h"
c521693b 37
9eaa50d0 38static const struct option options[] = {
ed142bdb
ZJS
39 { "device", required_argument, NULL, 'd' },
40 { "config", required_argument, NULL, 'f' },
41 { "page", required_argument, NULL, 'p' },
42 { "blacklisted", no_argument, NULL, 'b' },
43 { "whitelisted", no_argument, NULL, 'g' },
44 { "replace-whitespace", no_argument, NULL, 'u' },
45 { "sg-version", required_argument, NULL, 's' },
46 { "verbose", no_argument, NULL, 'v' },
47 { "version", no_argument, NULL, 'V' },
48 { "export", no_argument, NULL, 'x' },
49 { "help", no_argument, NULL, 'h' },
912541b0 50 {}
9eaa50d0
KS
51};
52
ed142bdb
ZJS
53static bool all_good = false;
54static bool dev_specified = false;
ff944daa 55static char config_file[MAX_PATH_LEN] = "/etc/scsi_id.config";
ed142bdb 56static enum page_code default_page_code = PAGE_UNSPECIFIED;
025570aa 57static int sg_version = 4;
ed142bdb
ZJS
58static int debug = 0;
59static bool reformat_serial = false;
60static bool export = false;
34129109
KS
61static char vendor_str[64];
62static char model_str[64];
ad88f940
DZ
63static char vendor_enc_str[256];
64static char model_enc_str[256];
aaff09a3
KS
65static char revision_str[16];
66static char type_str[16];
c521693b 67
9091e686 68_printf_(6,0)
7d563a17 69static void log_fn(struct udev *udev, int priority,
912541b0
KS
70 const char *file, int line, const char *fn,
71 const char *format, va_list args)
c521693b 72{
912541b0 73 vsyslog(priority, format, args);
c521693b
GKH
74}
75
92f43136 76static void set_type(const char *from, char *to, size_t len)
aaff09a3 77{
912541b0
KS
78 int type_num;
79 char *eptr;
4b0060e6 80 const char *type = "generic";
912541b0
KS
81
82 type_num = strtoul(from, &eptr, 0);
83 if (eptr != from) {
84 switch (type_num) {
85 case 0:
86 type = "disk";
87 break;
88 case 1:
89 type = "tape";
90 break;
91 case 4:
92 type = "optical";
93 break;
94 case 5:
95 type = "cd";
96 break;
97 case 7:
98 type = "optical";
99 break;
100 case 0xe:
101 type = "disk";
102 break;
103 case 0xf:
104 type = "optical";
105 break;
106 default:
107 break;
108 }
109 }
d5a89d7d 110 strscpy(to, len, type);
34129109
KS
111}
112
c521693b
GKH
113/*
114 * get_value:
115 *
116 * buf points to an '=' followed by a quoted string ("foo") or a string ending
117 * with a space or ','.
118 *
119 * Return a pointer to the NUL terminated string, returns NULL if no
120 * matches.
121 */
122static char *get_value(char **buffer)
123{
4b0060e6
KS
124 static const char *quote_string = "\"\n";
125 static const char *comma_string = ",\n";
912541b0 126 char *val;
4b0060e6 127 const char *end;
912541b0
KS
128
129 if (**buffer == '"') {
130 /*
131 * skip leading quote, terminate when quote seen
132 */
133 (*buffer)++;
134 end = quote_string;
135 } else {
136 end = comma_string;
137 }
138 val = strsep(buffer, end);
139 if (val && end == quote_string)
140 /*
141 * skip trailing quote
142 */
143 (*buffer)++;
144
145 while (isspace(**buffer))
146 (*buffer)++;
147
148 return val;
c521693b
GKH
149}
150
151static int argc_count(char *opts)
152{
912541b0
KS
153 int i = 0;
154 while (*opts != '\0')
155 if (*opts++ == ' ')
156 i++;
157 return i;
c521693b
GKH
158}
159
160/*
161 * get_file_options:
162 *
163 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
164 * if vendor and model are set find the first OPTIONS line in the config
165 * file that matches. Set argc and argv to match the OPTIONS string.
166 *
167 * vendor and model can end in '\n'.
168 */
7d563a17 169static int get_file_options(struct udev *udev,
912541b0
KS
170 const char *vendor, const char *model,
171 int *argc, char ***newargv)
c521693b 172{
912541b0 173 char *buffer;
ed142bdb 174 _cleanup_fclose_ FILE *f;
912541b0
KS
175 char *buf;
176 char *str1;
177 char *vendor_in, *model_in, *options_in; /* read in from file */
178 int lineno;
179 int c;
180 int retval = 0;
181
ed142bdb
ZJS
182 f = fopen(config_file, "re");
183 if (f == NULL) {
184 if (errno == ENOENT)
912541b0 185 return 1;
ed142bdb 186 else {
f5f6d0e2 187 log_error("can't open %s: %m\n", config_file);
912541b0
KS
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);
ed142bdb 198 if (!buffer)
0d0f0c50 199 return log_oom();
912541b0
KS
200
201 *newargv = NULL;
202 lineno = 0;
203 while (1) {
204 vendor_in = model_in = options_in = NULL;
205
ed142bdb 206 buf = fgets(buffer, MAX_BUFFER_LEN, f);
912541b0
KS
207 if (buf == NULL)
208 break;
209 lineno++;
210 if (buf[strlen(buffer) - 1] != '\n') {
baa30fbc 211 log_error("Config file line %d too long\n", lineno);
912541b0
KS
212 break;
213 }
214
215 while (isspace(*buf))
216 buf++;
217
218 /* blank or all whitespace line */
219 if (*buf == '\0')
220 continue;
221
222 /* comment line */
223 if (*buf == '#')
224 continue;
225
912541b0 226 str1 = strsep(&buf, "=");
b43d1d01 227 if (str1 && strcaseeq(str1, "VENDOR")) {
912541b0
KS
228 str1 = get_value(&buf);
229 if (!str1) {
0d0f0c50 230 retval = log_oom();
912541b0
KS
231 break;
232 }
233 vendor_in = str1;
234
235 str1 = strsep(&buf, "=");
b43d1d01 236 if (str1 && strcaseeq(str1, "MODEL")) {
912541b0
KS
237 str1 = get_value(&buf);
238 if (!str1) {
0d0f0c50 239 retval = log_oom();
912541b0
KS
240 break;
241 }
242 model_in = str1;
243 str1 = strsep(&buf, "=");
244 }
245 }
246
b43d1d01 247 if (str1 && strcaseeq(str1, "OPTIONS")) {
912541b0
KS
248 str1 = get_value(&buf);
249 if (!str1) {
0d0f0c50 250 retval = log_oom();
912541b0
KS
251 break;
252 }
253 options_in = str1;
254 }
baa30fbc 255
912541b0
KS
256 /*
257 * Only allow: [vendor=foo[,model=bar]]options=stuff
258 */
259 if (!options_in || (!vendor_in && model_in)) {
baa30fbc 260 log_error("Error parsing config file line %d '%s'\n", lineno, buffer);
912541b0
KS
261 retval = -1;
262 break;
263 }
264 if (vendor == NULL) {
baa30fbc 265 if (vendor_in == NULL)
912541b0 266 break;
ed142bdb
ZJS
267 } else if (vendor_in &&
268 strneq(vendor, vendor_in, strlen(vendor_in)) &&
269 (!model_in ||
270 (strneq(model, model_in, strlen(model_in))))) {
912541b0
KS
271 /*
272 * Matched vendor and optionally model.
273 *
274 * Note: a short vendor_in or model_in can
275 * give a partial match (that is FOO
276 * matches FOOBAR).
277 */
912541b0 278 break;
912541b0
KS
279 }
280 }
281
282 if (retval == 0) {
283 if (vendor_in != NULL || model_in != NULL ||
284 options_in != NULL) {
285 /*
286 * Something matched. Allocate newargv, and store
287 * values found in options_in.
288 */
289 strcpy(buffer, options_in);
290 c = argc_count(buffer) + 2;
291 *newargv = calloc(c, sizeof(**newargv));
292 if (!*newargv) {
0d0f0c50 293 retval = log_oom();
912541b0
KS
294 } else {
295 *argc = c;
296 c = 0;
297 /*
298 * argv[0] at 0 is skipped by getopt, but
299 * store the buffer address there for
300 * later freeing
301 */
302 (*newargv)[c] = buffer;
303 for (c = 1; c < *argc; c++)
304 (*newargv)[c] = strsep(&buffer, " \t");
305 }
306 } else {
307 /* No matches */
308 retval = 1;
309 }
310 }
311 if (retval != 0)
312 free(buffer);
912541b0 313 return retval;
c521693b
GKH
314}
315
7d563a17 316static int set_options(struct udev *udev,
ed142bdb 317 int argc, char **argv,
912541b0 318 char *maj_min_dev)
c521693b 319{
912541b0
KS
320 int option;
321
322 /*
323 * optind is a global extern used by getopt. Since we can call
324 * set_options twice (once for command line, and once for config
325 * file) we have to reset this back to 1.
326 */
327 optind = 1;
ed142bdb 328 while ((option = getopt_long(argc, argv, "d:f:ghp:uvVx", options, NULL)) >= 0)
912541b0
KS
329 switch (option) {
330 case 'b':
ed142bdb 331 all_good = false;
912541b0
KS
332 break;
333
334 case 'd':
ed142bdb 335 dev_specified = true;
d5a89d7d 336 strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
912541b0
KS
337 break;
338
912541b0 339 case 'f':
d5a89d7d 340 strscpy(config_file, MAX_PATH_LEN, optarg);
912541b0
KS
341 break;
342
343 case 'g':
ed142bdb 344 all_good = true;
912541b0
KS
345 break;
346
347 case 'h':
ed142bdb 348 printf("Usage: scsi_id [OPTION...] DEVICE\n"
912541b0
KS
349 " --device= device node for SG_IO commands\n"
350 " --config= location of config file\n"
351 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
352 " --sg-version=3|4 use SGv3 or SGv4\n"
353 " --blacklisted threat device as blacklisted\n"
354 " --whitelisted threat device as whitelisted\n"
ed142bdb 355 " --replace-whitespace replace all whitespace by underscores\n"
912541b0
KS
356 " --verbose verbose logging\n"
357 " --version print version\n"
358 " --export print values as environment keys\n"
359 " --help print this help text\n\n");
360 exit(0);
361
362 case 'p':
ed142bdb 363 if (streq(optarg, "0x80"))
912541b0 364 default_page_code = PAGE_80;
ed142bdb 365 else if (streq(optarg, "0x83"))
912541b0 366 default_page_code = PAGE_83;
ed142bdb 367 else if (streq(optarg, "pre-spc3-83"))
912541b0 368 default_page_code = PAGE_83_PRE_SPC3;
ed142bdb 369 else {
baa30fbc 370 log_error("Unknown page code '%s'\n", optarg);
912541b0
KS
371 return -1;
372 }
373 break;
374
375 case 's':
376 sg_version = atoi(optarg);
377 if (sg_version < 3 || sg_version > 4) {
baa30fbc 378 log_error("Unknown SG version '%s'\n", optarg);
912541b0
KS
379 return -1;
380 }
381 break;
382
383 case 'u':
ed142bdb 384 reformat_serial = true;
912541b0
KS
385 break;
386
387 case 'v':
388 debug++;
389 break;
390
391 case 'V':
392 printf("%s\n", VERSION);
393 exit(0);
394 break;
395
ed142bdb
ZJS
396 case 'x':
397 export = true;
398 break;
399
400 case '?':
401 return -1;
402
912541b0 403 default:
ed142bdb 404 assert_not_reached("Unknown option");
912541b0 405 }
ed142bdb 406
912541b0 407 if (optind < argc && !dev_specified) {
ed142bdb 408 dev_specified = true;
d5a89d7d 409 strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
912541b0 410 }
ed142bdb 411
912541b0 412 return 0;
c521693b
GKH
413}
414
7d563a17 415static int per_dev_options(struct udev *udev,
912541b0 416 struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
c521693b 417{
912541b0
KS
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) {
ed142bdb 430 option = getopt_long(newargc, newargv, "bgp:", options, NULL);
912541b0
KS
431 if (option == -1)
432 break;
433
912541b0
KS
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':
090be865 444 if (streq(optarg, "0x80")) {
912541b0 445 *page_code = PAGE_80;
090be865 446 } else if (streq(optarg, "0x83")) {
912541b0 447 *page_code = PAGE_83;
090be865 448 } else if (streq(optarg, "pre-spc3-83")) {
912541b0
KS
449 *page_code = PAGE_83_PRE_SPC3;
450 } else {
baa30fbc 451 log_error("Unknown page code '%s'\n", optarg);
912541b0
KS
452 retval = -1;
453 }
454 break;
455
456 default:
baa30fbc 457 log_error("Unknown or bad option '%c' (0x%x)\n", option, option);
912541b0
KS
458 retval = -1;
459 break;
460 }
461 }
462
463 if (newargv) {
464 free(newargv[0]);
465 free(newargv);
466 }
467 return retval;
c521693b
GKH
468}
469
7d563a17 470static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
87cf9f5a 471{
912541b0 472 int retval;
87cf9f5a 473
912541b0 474 dev_scsi->use_sg = sg_version;
78d9ecfd 475
912541b0
KS
476 retval = scsi_std_inquiry(udev, dev_scsi, path);
477 if (retval)
478 return retval;
87cf9f5a 479
912541b0
KS
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));
ad88f940 482
912541b0
KS
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;
87cf9f5a
HR
491}
492
c521693b
GKH
493/*
494 * scsi_id: try to get an id, if one is found, printf it to stdout.
caf4c97a 495 * returns a value passed to exit() - 0 if printed an id, else 1.
c521693b 496 */
7d563a17 497static int scsi_id(struct udev *udev, char *maj_min_dev)
c521693b 498{
ed142bdb 499 struct scsi_id_device dev_scsi = {};
912541b0
KS
500 int good_dev;
501 int page_code;
502 int retval = 0;
503
912541b0
KS
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);
912541b0
KS
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 }
546 if (dev_scsi.tgpt_group[0] != '\0') {
547 printf("ID_TARGET_PORT=%s\n", dev_scsi.tgpt_group);
548 }
549 if (dev_scsi.unit_serial_number[0] != '\0') {
550 printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
551 }
552 goto out;
553 }
554
555 if (dev_scsi.serial[0] == '\0') {
556 retval = 1;
557 goto out;
558 }
559
560 if (reformat_serial) {
561 char serial_str[MAX_SERIAL_LEN];
562
563 util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
564 util_replace_chars(serial_str, NULL);
565 printf("%s\n", serial_str);
566 goto out;
567 }
568
569 printf("%s\n", dev_scsi.serial);
caf4c97a 570out:
912541b0 571 return retval;
c521693b
GKH
572}
573
574int main(int argc, char **argv)
575{
ed142bdb 576 _cleanup_udev_unref_ struct udev *udev;
912541b0
KS
577 int retval = 0;
578 char maj_min_dev[MAX_PATH_LEN];
579 int newargc;
ed142bdb 580 char **newargv = NULL;
912541b0
KS
581
582 udev = udev_new();
583 if (udev == NULL)
584 goto exit;
585
baa30fbc 586 log_open();
912541b0
KS
587 udev_set_log_fn(udev, log_fn);
588
589 /*
590 * Get config file options.
591 */
912541b0
KS
592 retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
593 if (retval < 0) {
594 retval = 1;
595 goto exit;
596 }
ed142bdb
ZJS
597 if (retval == 0) {
598 assert(newargv);
599
600 if (set_options(udev, newargc, newargv, maj_min_dev) < 0) {
912541b0
KS
601 retval = 2;
602 goto exit;
603 }
912541b0
KS
604 }
605
606 /*
607 * Get command line options (overriding any config file settings).
608 */
ed142bdb 609 if (set_options(udev, argc, argv, maj_min_dev) < 0)
912541b0
KS
610 exit(1);
611
612 if (!dev_specified) {
baa30fbc 613 log_error("no device specified\n");
912541b0
KS
614 retval = 1;
615 goto exit;
616 }
617
618 retval = scsi_id(udev, maj_min_dev);
1aa1e248
KS
619
620exit:
ed142bdb
ZJS
621 if (newargv) {
622 free(newargv[0]);
623 free(newargv);
624 }
baa30fbc 625 log_close();
912541b0 626 return retval;
c521693b 627}