]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/scsi_id/scsi_id.c
Merge pull request #8807 from ChrisLesiak/systemd-update-done-mtime-fix
[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 261 } else if (vendor_in &&
0caa9946
YW
262 startswith(vendor, vendor_in) &&
263 (!model_in || startswith(model, model_in))) {
912541b0
KS
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 */
912541b0 271 break;
912541b0
KS
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));
1f6b4113 285 if (!*newargv)
0d0f0c50 286 retval = log_oom();
1f6b4113 287 else {
912541b0
KS
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);
912541b0 306 return retval;
c521693b
GKH
307}
308
7643ac9a 309static void help(void) {
5ac0162c
LP
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);
7643ac9a
ZJS
324
325}
326
7d563a17 327static int set_options(struct udev *udev,
ed142bdb 328 int argc, char **argv,
912541b0 329 char *maj_min_dev)
c521693b 330{
912541b0
KS
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;
ebc6f34a 339 while ((option = getopt_long(argc, argv, "d:f:gp:uvVxhbs:", options, NULL)) >= 0)
912541b0
KS
340 switch (option) {
341 case 'b':
ed142bdb 342 all_good = false;
912541b0
KS
343 break;
344
345 case 'd':
ed142bdb 346 dev_specified = true;
d5a89d7d 347 strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
912541b0
KS
348 break;
349
912541b0 350 case 'f':
d5a89d7d 351 strscpy(config_file, MAX_PATH_LEN, optarg);
912541b0
KS
352 break;
353
354 case 'g':
ed142bdb 355 all_good = true;
912541b0
KS
356 break;
357
358 case 'h':
7643ac9a 359 help();
a45d7127 360 exit(EXIT_SUCCESS);
912541b0
KS
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 {
9f6445e3 370 log_error("Unknown page code '%s'", 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) {
9f6445e3 378 log_error("Unknown SG version '%s'", 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':
5168f84a
LP
388 log_set_target(LOG_TARGET_CONSOLE);
389 log_set_max_level(LOG_DEBUG);
5168f84a 390 log_open();
912541b0
KS
391 break;
392
393 case 'V':
948aaa7c 394 printf("%s\n", PACKAGE_VERSION);
a45d7127 395 exit(EXIT_SUCCESS);
912541b0 396
ed142bdb
ZJS
397 case 'x':
398 export = true;
399 break;
400
401 case '?':
402 return -1;
403
912541b0 404 default:
ed142bdb 405 assert_not_reached("Unknown option");
912541b0 406 }
ed142bdb 407
912541b0 408 if (optind < argc && !dev_specified) {
ed142bdb 409 dev_specified = true;
d5a89d7d 410 strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
912541b0 411 }
ed142bdb 412
912541b0 413 return 0;
c521693b
GKH
414}
415
7d563a17 416static int per_dev_options(struct udev *udev,
912541b0 417 struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
c521693b 418{
912541b0
KS
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) {
ed142bdb 431 option = getopt_long(newargc, newargv, "bgp:", options, NULL);
912541b0
KS
432 if (option == -1)
433 break;
434
912541b0
KS
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':
090be865 445 if (streq(optarg, "0x80")) {
912541b0 446 *page_code = PAGE_80;
090be865 447 } else if (streq(optarg, "0x83")) {
912541b0 448 *page_code = PAGE_83;
090be865 449 } else if (streq(optarg, "pre-spc3-83")) {
912541b0
KS
450 *page_code = PAGE_83_PRE_SPC3;
451 } else {
9f6445e3 452 log_error("Unknown page code '%s'", optarg);
912541b0
KS
453 retval = -1;
454 }
455 break;
456
457 default:
9f6445e3 458 log_error("Unknown or bad option '%c' (0x%x)", option, option);
912541b0
KS
459 retval = -1;
460 break;
461 }
462 }
463
464 if (newargv) {
465 free(newargv[0]);
466 free(newargv);
467 }
468 return retval;
c521693b
GKH
469}
470
7d563a17 471static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
87cf9f5a 472{
912541b0 473 int retval;
87cf9f5a 474
912541b0 475 dev_scsi->use_sg = sg_version;
78d9ecfd 476
912541b0
KS
477 retval = scsi_std_inquiry(udev, dev_scsi, path);
478 if (retval)
479 return retval;
87cf9f5a 480
912541b0
KS
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));
ad88f940 483
912541b0
KS
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;
87cf9f5a
HR
492}
493
c521693b
GKH
494/*
495 * scsi_id: try to get an id, if one is found, printf it to stdout.
caf4c97a 496 * returns a value passed to exit() - 0 if printed an id, else 1.
c521693b 497 */
7d563a17 498static int scsi_id(struct udev *udev, char *maj_min_dev)
c521693b 499{
ed142bdb 500 struct scsi_id_device dev_scsi = {};
912541b0
KS
501 int good_dev;
502 int page_code;
503 int retval = 0;
504
912541b0
KS
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);
912541b0
KS
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);
1f6b4113 543 } else
912541b0 544 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi.wwn);
912541b0 545 }
1f6b4113 546 if (dev_scsi.tgpt_group[0] != '\0')
912541b0 547 printf("ID_TARGET_PORT=%s\n", dev_scsi.tgpt_group);
1f6b4113 548 if (dev_scsi.unit_serial_number[0] != '\0')
912541b0 549 printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
912541b0
KS
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);
caf4c97a 568out:
912541b0 569 return retval;
c521693b
GKH
570}
571
572int main(int argc, char **argv)
573{
ed142bdb 574 _cleanup_udev_unref_ struct udev *udev;
912541b0
KS
575 int retval = 0;
576 char maj_min_dev[MAX_PATH_LEN];
577 int newargc;
ed142bdb 578 char **newargv = NULL;
912541b0 579
b237a168
ZJS
580 log_set_target(LOG_TARGET_AUTO);
581 udev_parse_config();
5168f84a
LP
582 log_parse_environment();
583 log_open();
584
912541b0
KS
585 udev = udev_new();
586 if (udev == NULL)
587 goto exit;
588
912541b0
KS
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)
a45d7127 610 exit(EXIT_FAILURE);
912541b0
KS
611
612 if (!dev_specified) {
5ac0162c 613 log_error("No device specified.");
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}