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