1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright © IBM Corp. 2003
4 * Copyright © SUSE Linux Products GmbH, 2006
20 #include "alloc-util.h"
23 #include "libudev-util.h"
25 #include "string-util.h"
27 #include "udev-util.h"
29 static const struct option options
[] = {
30 { "device", required_argument
, NULL
, 'd' },
31 { "config", required_argument
, NULL
, 'f' },
32 { "page", required_argument
, NULL
, 'p' },
33 { "blacklisted", no_argument
, NULL
, 'b' },
34 { "whitelisted", no_argument
, NULL
, 'g' },
35 { "replace-whitespace", no_argument
, NULL
, 'u' },
36 { "sg-version", required_argument
, NULL
, 's' },
37 { "verbose", no_argument
, NULL
, 'v' },
38 { "version", no_argument
, NULL
, 'V' }, /* don't advertise -V */
39 { "export", no_argument
, NULL
, 'x' },
40 { "help", no_argument
, NULL
, 'h' },
44 static bool all_good
= false;
45 static bool dev_specified
= false;
46 static char config_file
[MAX_PATH_LEN
] = "/etc/scsi_id.config";
47 static enum page_code default_page_code
= PAGE_UNSPECIFIED
;
48 static int sg_version
= 4;
49 static bool reformat_serial
= false;
50 static bool export
= false;
51 static char vendor_str
[64];
52 static char model_str
[64];
53 static char vendor_enc_str
[256];
54 static char model_enc_str
[256];
55 static char revision_str
[16];
56 static char type_str
[16];
58 static void set_type(const char *from
, char *to
, size_t len
) {
61 const char *type
= "generic";
63 type_num
= strtoul(from
, &eptr
, 0);
91 strscpy(to
, len
, type
);
97 * buf points to an '=' followed by a quoted string ("foo") or a string ending
98 * with a space or ','.
100 * Return a pointer to the NUL terminated string, returns NULL if no
103 static char *get_value(char **buffer
) {
104 static const char *quote_string
= "\"\n";
105 static const char *comma_string
= ",\n";
109 if (**buffer
== '"') {
111 * skip leading quote, terminate when quote seen
118 val
= strsep(buffer
, end
);
119 if (val
&& end
== quote_string
)
121 * skip trailing quote
125 while (isspace(**buffer
))
131 static int argc_count(char *opts
) {
133 while (*opts
!= '\0')
142 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
143 * if vendor and model are set find the first OPTIONS line in the config
144 * file that matches. Set argc and argv to match the OPTIONS string.
146 * vendor and model can end in '\n'.
148 static int get_file_options(const char *vendor
, const char *model
,
149 int *argc
, char ***newargv
) {
150 _cleanup_free_
char *buffer
= NULL
;
151 _cleanup_fclose_
FILE *f
;
154 char *vendor_in
, *model_in
, *options_in
; /* read in from file */
159 f
= fopen(config_file
, "re");
164 log_error_errno(errno
, "can't open %s: %m", config_file
);
170 * Allocate a buffer rather than put it on the stack so we can
171 * keep it around to parse any options (any allocated newargv
172 * points into this buffer for its strings).
174 buffer
= malloc(MAX_BUFFER_LEN
);
181 vendor_in
= model_in
= options_in
= NULL
;
183 buf
= fgets(buffer
, MAX_BUFFER_LEN
, f
);
187 if (buf
[strlen(buffer
) - 1] != '\n') {
188 log_error("Config file line %d too long", lineno
);
192 while (isspace(*buf
))
195 /* blank or all whitespace line */
203 str1
= strsep(&buf
, "=");
204 if (str1
&& strcaseeq(str1
, "VENDOR")) {
205 str1
= get_value(&buf
);
212 str1
= strsep(&buf
, "=");
213 if (str1
&& strcaseeq(str1
, "MODEL")) {
214 str1
= get_value(&buf
);
220 str1
= strsep(&buf
, "=");
224 if (str1
&& strcaseeq(str1
, "OPTIONS")) {
225 str1
= get_value(&buf
);
234 * Only allow: [vendor=foo[,model=bar]]options=stuff
236 if (!options_in
|| (!vendor_in
&& model_in
)) {
237 log_error("Error parsing config file line %d '%s'", lineno
, buffer
);
241 if (vendor
== NULL
) {
242 if (vendor_in
== NULL
)
244 } else if (vendor_in
&&
245 startswith(vendor
, vendor_in
) &&
246 (!model_in
|| startswith(model
, model_in
))) {
248 * Matched vendor and optionally model.
250 * Note: a short vendor_in or model_in can
251 * give a partial match (that is FOO
259 if (vendor_in
!= NULL
|| model_in
!= NULL
||
260 options_in
!= NULL
) {
262 * Something matched. Allocate newargv, and store
263 * values found in options_in.
265 strcpy(buffer
, options_in
);
266 c
= argc_count(buffer
) + 2;
267 *newargv
= calloc(c
, sizeof(**newargv
));
274 * argv[0] at 0 is skipped by getopt, but
275 * store the buffer address there for
278 (*newargv
)[c
] = buffer
;
279 for (c
= 1; c
< *argc
; c
++)
280 (*newargv
)[c
] = strsep(&buffer
, " \t");
291 static void help(void) {
292 printf("Usage: %s [OPTION...] DEVICE\n\n"
293 "SCSI device identification.\n\n"
294 " -h --help Print this message\n"
295 " --version Print version of the program\n\n"
296 " -d --device= Device node for SG_IO commands\n"
297 " -f --config= Location of config file\n"
298 " -p --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
299 " -s --sg-version=3|4 Use SGv3 or SGv4\n"
300 " -b --blacklisted Treat device as blacklisted\n"
301 " -g --whitelisted Treat device as whitelisted\n"
302 " -u --replace-whitespace Replace all whitespace by underscores\n"
303 " -v --verbose Verbose logging\n"
304 " -x --export Print values as environment keys\n"
305 , program_invocation_short_name
);
309 static int set_options(int argc
, char **argv
,
314 * optind is a global extern used by getopt. Since we can call
315 * set_options twice (once for command line, and once for config
316 * file) we have to reset this back to 1.
319 while ((option
= getopt_long(argc
, argv
, "d:f:gp:uvVxhbs:", options
, NULL
)) >= 0)
326 dev_specified
= true;
327 strscpy(maj_min_dev
, MAX_PATH_LEN
, optarg
);
331 strscpy(config_file
, MAX_PATH_LEN
, optarg
);
343 if (streq(optarg
, "0x80"))
344 default_page_code
= PAGE_80
;
345 else if (streq(optarg
, "0x83"))
346 default_page_code
= PAGE_83
;
347 else if (streq(optarg
, "pre-spc3-83"))
348 default_page_code
= PAGE_83_PRE_SPC3
;
350 log_error("Unknown page code '%s'", optarg
);
356 sg_version
= atoi(optarg
);
357 if (sg_version
< 3 || sg_version
> 4) {
358 log_error("Unknown SG version '%s'", optarg
);
364 reformat_serial
= true;
368 log_set_target(LOG_TARGET_CONSOLE
);
369 log_set_max_level(LOG_DEBUG
);
374 printf("%s\n", GIT_VERSION
);
385 assert_not_reached("Unknown option");
388 if (optind
< argc
&& !dev_specified
) {
389 dev_specified
= true;
390 strscpy(maj_min_dev
, MAX_PATH_LEN
, argv
[optind
]);
396 static int per_dev_options(struct scsi_id_device
*dev_scsi
, int *good_bad
, int *page_code
) {
399 char **newargv
= NULL
;
402 *good_bad
= all_good
;
403 *page_code
= default_page_code
;
405 retval
= get_file_options(vendor_str
, model_str
, &newargc
, &newargv
);
407 optind
= 1; /* reset this global extern */
408 while (retval
== 0) {
409 option
= getopt_long(newargc
, newargv
, "bgp:", options
, NULL
);
423 if (streq(optarg
, "0x80")) {
424 *page_code
= PAGE_80
;
425 } else if (streq(optarg
, "0x83")) {
426 *page_code
= PAGE_83
;
427 } else if (streq(optarg
, "pre-spc3-83")) {
428 *page_code
= PAGE_83_PRE_SPC3
;
430 log_error("Unknown page code '%s'", optarg
);
436 log_error("Unknown or bad option '%c' (0x%x)", option
, option
);
449 static int set_inq_values(struct scsi_id_device
*dev_scsi
, const char *path
) {
452 dev_scsi
->use_sg
= sg_version
;
454 retval
= scsi_std_inquiry(dev_scsi
, path
);
458 udev_util_encode_string(dev_scsi
->vendor
, vendor_enc_str
, sizeof(vendor_enc_str
));
459 udev_util_encode_string(dev_scsi
->model
, model_enc_str
, sizeof(model_enc_str
));
461 util_replace_whitespace(dev_scsi
->vendor
, vendor_str
, sizeof(vendor_str
)-1);
462 util_replace_chars(vendor_str
, NULL
);
463 util_replace_whitespace(dev_scsi
->model
, model_str
, sizeof(model_str
)-1);
464 util_replace_chars(model_str
, NULL
);
465 set_type(dev_scsi
->type
, type_str
, sizeof(type_str
));
466 util_replace_whitespace(dev_scsi
->revision
, revision_str
, sizeof(revision_str
)-1);
467 util_replace_chars(revision_str
, NULL
);
472 * scsi_id: try to get an id, if one is found, printf it to stdout.
473 * returns a value passed to exit() - 0 if printed an id, else 1.
475 static int scsi_id(char *maj_min_dev
) {
476 struct scsi_id_device dev_scsi
= {};
481 if (set_inq_values(&dev_scsi
, maj_min_dev
) < 0) {
486 /* get per device (vendor + model) options from the config file */
487 per_dev_options(&dev_scsi
, &good_dev
, &page_code
);
493 /* read serial number from mode pages (no values for optical drives) */
494 scsi_get_serial(&dev_scsi
, maj_min_dev
, page_code
, MAX_SERIAL_LEN
);
497 char serial_str
[MAX_SERIAL_LEN
];
499 printf("ID_SCSI=1\n");
500 printf("ID_VENDOR=%s\n", vendor_str
);
501 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str
);
502 printf("ID_MODEL=%s\n", model_str
);
503 printf("ID_MODEL_ENC=%s\n", model_enc_str
);
504 printf("ID_REVISION=%s\n", revision_str
);
505 printf("ID_TYPE=%s\n", type_str
);
506 if (dev_scsi
.serial
[0] != '\0') {
507 util_replace_whitespace(dev_scsi
.serial
, serial_str
, sizeof(serial_str
)-1);
508 util_replace_chars(serial_str
, NULL
);
509 printf("ID_SERIAL=%s\n", serial_str
);
510 util_replace_whitespace(dev_scsi
.serial_short
, serial_str
, sizeof(serial_str
)-1);
511 util_replace_chars(serial_str
, NULL
);
512 printf("ID_SERIAL_SHORT=%s\n", serial_str
);
514 if (dev_scsi
.wwn
[0] != '\0') {
515 printf("ID_WWN=0x%s\n", dev_scsi
.wwn
);
516 if (dev_scsi
.wwn_vendor_extension
[0] != '\0') {
517 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi
.wwn_vendor_extension
);
518 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi
.wwn
, dev_scsi
.wwn_vendor_extension
);
520 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi
.wwn
);
522 if (dev_scsi
.tgpt_group
[0] != '\0')
523 printf("ID_TARGET_PORT=%s\n", dev_scsi
.tgpt_group
);
524 if (dev_scsi
.unit_serial_number
[0] != '\0')
525 printf("ID_SCSI_SERIAL=%s\n", dev_scsi
.unit_serial_number
);
529 if (dev_scsi
.serial
[0] == '\0') {
534 if (reformat_serial
) {
535 char serial_str
[MAX_SERIAL_LEN
];
537 util_replace_whitespace(dev_scsi
.serial
, serial_str
, sizeof(serial_str
)-1);
538 util_replace_chars(serial_str
, NULL
);
539 printf("%s\n", serial_str
);
543 printf("%s\n", dev_scsi
.serial
);
548 int main(int argc
, char **argv
) {
550 char maj_min_dev
[MAX_PATH_LEN
];
552 char **newargv
= NULL
;
554 log_set_target(LOG_TARGET_AUTO
);
556 log_parse_environment();
560 * Get config file options.
562 retval
= get_file_options(NULL
, NULL
, &newargc
, &newargv
);
570 if (set_options(newargc
, newargv
, maj_min_dev
) < 0) {
577 * Get command line options (overriding any config file settings).
579 if (set_options(argc
, argv
, maj_min_dev
) < 0)
582 if (!dev_specified
) {
583 log_error("No device specified.");
588 retval
= scsi_id(maj_min_dev
);