1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright © IBM Corp. 2003
4 * Copyright © SUSE Linux Products GmbH, 2006
20 #include "alloc-util.h"
22 #include "libudev-private.h"
24 #include "string-util.h"
25 #include "udev-util.h"
27 static const struct option options
[] = {
28 { "device", required_argument
, NULL
, 'd' },
29 { "config", required_argument
, NULL
, 'f' },
30 { "page", required_argument
, NULL
, 'p' },
31 { "blacklisted", no_argument
, NULL
, 'b' },
32 { "whitelisted", no_argument
, NULL
, 'g' },
33 { "replace-whitespace", no_argument
, NULL
, 'u' },
34 { "sg-version", required_argument
, NULL
, 's' },
35 { "verbose", no_argument
, NULL
, 'v' },
36 { "version", no_argument
, NULL
, 'V' }, /* don't advertise -V */
37 { "export", no_argument
, NULL
, 'x' },
38 { "help", no_argument
, NULL
, 'h' },
42 static bool all_good
= false;
43 static bool dev_specified
= false;
44 static char config_file
[MAX_PATH_LEN
] = "/etc/scsi_id.config";
45 static enum page_code default_page_code
= PAGE_UNSPECIFIED
;
46 static int sg_version
= 4;
47 static bool reformat_serial
= false;
48 static bool export
= false;
49 static char vendor_str
[64];
50 static char model_str
[64];
51 static char vendor_enc_str
[256];
52 static char model_enc_str
[256];
53 static char revision_str
[16];
54 static char type_str
[16];
56 static void set_type(const char *from
, char *to
, size_t len
) {
59 const char *type
= "generic";
61 type_num
= strtoul(from
, &eptr
, 0);
89 strscpy(to
, len
, type
);
95 * buf points to an '=' followed by a quoted string ("foo") or a string ending
96 * with a space or ','.
98 * Return a pointer to the NUL terminated string, returns NULL if no
101 static char *get_value(char **buffer
) {
102 static const char *quote_string
= "\"\n";
103 static const char *comma_string
= ",\n";
107 if (**buffer
== '"') {
109 * skip leading quote, terminate when quote seen
116 val
= strsep(buffer
, end
);
117 if (val
&& end
== quote_string
)
119 * skip trailing quote
123 while (isspace(**buffer
))
129 static int argc_count(char *opts
) {
131 while (*opts
!= '\0')
140 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
141 * if vendor and model are set find the first OPTIONS line in the config
142 * file that matches. Set argc and argv to match the OPTIONS string.
144 * vendor and model can end in '\n'.
146 static int get_file_options(const char *vendor
, const char *model
,
147 int *argc
, char ***newargv
) {
148 _cleanup_free_
char *buffer
= NULL
;
149 _cleanup_fclose_
FILE *f
;
152 char *vendor_in
, *model_in
, *options_in
; /* read in from file */
157 f
= fopen(config_file
, "re");
162 log_error_errno(errno
, "can't open %s: %m", config_file
);
168 * Allocate a buffer rather than put it on the stack so we can
169 * keep it around to parse any options (any allocated newargv
170 * points into this buffer for its strings).
172 buffer
= malloc(MAX_BUFFER_LEN
);
179 vendor_in
= model_in
= options_in
= NULL
;
181 buf
= fgets(buffer
, MAX_BUFFER_LEN
, f
);
185 if (buf
[strlen(buffer
) - 1] != '\n') {
186 log_error("Config file line %d too long", lineno
);
190 while (isspace(*buf
))
193 /* blank or all whitespace line */
201 str1
= strsep(&buf
, "=");
202 if (str1
&& strcaseeq(str1
, "VENDOR")) {
203 str1
= get_value(&buf
);
210 str1
= strsep(&buf
, "=");
211 if (str1
&& strcaseeq(str1
, "MODEL")) {
212 str1
= get_value(&buf
);
218 str1
= strsep(&buf
, "=");
222 if (str1
&& strcaseeq(str1
, "OPTIONS")) {
223 str1
= get_value(&buf
);
232 * Only allow: [vendor=foo[,model=bar]]options=stuff
234 if (!options_in
|| (!vendor_in
&& model_in
)) {
235 log_error("Error parsing config file line %d '%s'", lineno
, buffer
);
239 if (vendor
== NULL
) {
240 if (vendor_in
== NULL
)
242 } else if (vendor_in
&&
243 startswith(vendor
, vendor_in
) &&
244 (!model_in
|| startswith(model
, model_in
))) {
246 * Matched vendor and optionally model.
248 * Note: a short vendor_in or model_in can
249 * give a partial match (that is FOO
257 if (vendor_in
!= NULL
|| model_in
!= NULL
||
258 options_in
!= NULL
) {
260 * Something matched. Allocate newargv, and store
261 * values found in options_in.
263 strcpy(buffer
, options_in
);
264 c
= argc_count(buffer
) + 2;
265 *newargv
= calloc(c
, sizeof(**newargv
));
272 * argv[0] at 0 is skipped by getopt, but
273 * store the buffer address there for
276 (*newargv
)[c
] = buffer
;
277 for (c
= 1; c
< *argc
; c
++)
278 (*newargv
)[c
] = strsep(&buffer
, " \t");
289 static void help(void) {
290 printf("Usage: %s [OPTION...] DEVICE\n\n"
291 "SCSI device identification.\n\n"
292 " -h --help Print this message\n"
293 " --version Print version of the program\n\n"
294 " -d --device= Device node for SG_IO commands\n"
295 " -f --config= Location of config file\n"
296 " -p --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
297 " -s --sg-version=3|4 Use SGv3 or SGv4\n"
298 " -b --blacklisted Treat device as blacklisted\n"
299 " -g --whitelisted Treat device as whitelisted\n"
300 " -u --replace-whitespace Replace all whitespace by underscores\n"
301 " -v --verbose Verbose logging\n"
302 " -x --export Print values as environment keys\n"
303 , program_invocation_short_name
);
307 static int set_options(int argc
, char **argv
,
312 * optind is a global extern used by getopt. Since we can call
313 * set_options twice (once for command line, and once for config
314 * file) we have to reset this back to 1.
317 while ((option
= getopt_long(argc
, argv
, "d:f:gp:uvVxhbs:", options
, NULL
)) >= 0)
324 dev_specified
= true;
325 strscpy(maj_min_dev
, MAX_PATH_LEN
, optarg
);
329 strscpy(config_file
, MAX_PATH_LEN
, optarg
);
341 if (streq(optarg
, "0x80"))
342 default_page_code
= PAGE_80
;
343 else if (streq(optarg
, "0x83"))
344 default_page_code
= PAGE_83
;
345 else if (streq(optarg
, "pre-spc3-83"))
346 default_page_code
= PAGE_83_PRE_SPC3
;
348 log_error("Unknown page code '%s'", optarg
);
354 sg_version
= atoi(optarg
);
355 if (sg_version
< 3 || sg_version
> 4) {
356 log_error("Unknown SG version '%s'", optarg
);
362 reformat_serial
= true;
366 log_set_target(LOG_TARGET_CONSOLE
);
367 log_set_max_level(LOG_DEBUG
);
372 printf("%s\n", PACKAGE_VERSION
);
383 assert_not_reached("Unknown option");
386 if (optind
< argc
&& !dev_specified
) {
387 dev_specified
= true;
388 strscpy(maj_min_dev
, MAX_PATH_LEN
, argv
[optind
]);
394 static int per_dev_options(struct scsi_id_device
*dev_scsi
, int *good_bad
, int *page_code
) {
397 char **newargv
= NULL
;
400 *good_bad
= all_good
;
401 *page_code
= default_page_code
;
403 retval
= get_file_options(vendor_str
, model_str
, &newargc
, &newargv
);
405 optind
= 1; /* reset this global extern */
406 while (retval
== 0) {
407 option
= getopt_long(newargc
, newargv
, "bgp:", options
, NULL
);
421 if (streq(optarg
, "0x80")) {
422 *page_code
= PAGE_80
;
423 } else if (streq(optarg
, "0x83")) {
424 *page_code
= PAGE_83
;
425 } else if (streq(optarg
, "pre-spc3-83")) {
426 *page_code
= PAGE_83_PRE_SPC3
;
428 log_error("Unknown page code '%s'", optarg
);
434 log_error("Unknown or bad option '%c' (0x%x)", option
, option
);
447 static int set_inq_values(struct scsi_id_device
*dev_scsi
, const char *path
) {
450 dev_scsi
->use_sg
= sg_version
;
452 retval
= scsi_std_inquiry(dev_scsi
, path
);
456 udev_util_encode_string(dev_scsi
->vendor
, vendor_enc_str
, sizeof(vendor_enc_str
));
457 udev_util_encode_string(dev_scsi
->model
, model_enc_str
, sizeof(model_enc_str
));
459 util_replace_whitespace(dev_scsi
->vendor
, vendor_str
, sizeof(vendor_str
));
460 util_replace_chars(vendor_str
, NULL
);
461 util_replace_whitespace(dev_scsi
->model
, model_str
, sizeof(model_str
));
462 util_replace_chars(model_str
, NULL
);
463 set_type(dev_scsi
->type
, type_str
, sizeof(type_str
));
464 util_replace_whitespace(dev_scsi
->revision
, revision_str
, sizeof(revision_str
));
465 util_replace_chars(revision_str
, NULL
);
470 * scsi_id: try to get an id, if one is found, printf it to stdout.
471 * returns a value passed to exit() - 0 if printed an id, else 1.
473 static int scsi_id(char *maj_min_dev
) {
474 struct scsi_id_device dev_scsi
= {};
479 if (set_inq_values(&dev_scsi
, maj_min_dev
) < 0) {
484 /* get per device (vendor + model) options from the config file */
485 per_dev_options(&dev_scsi
, &good_dev
, &page_code
);
491 /* read serial number from mode pages (no values for optical drives) */
492 scsi_get_serial(&dev_scsi
, maj_min_dev
, page_code
, MAX_SERIAL_LEN
);
495 char serial_str
[MAX_SERIAL_LEN
];
497 printf("ID_SCSI=1\n");
498 printf("ID_VENDOR=%s\n", vendor_str
);
499 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str
);
500 printf("ID_MODEL=%s\n", model_str
);
501 printf("ID_MODEL_ENC=%s\n", model_enc_str
);
502 printf("ID_REVISION=%s\n", revision_str
);
503 printf("ID_TYPE=%s\n", type_str
);
504 if (dev_scsi
.serial
[0] != '\0') {
505 util_replace_whitespace(dev_scsi
.serial
, serial_str
, sizeof(serial_str
));
506 util_replace_chars(serial_str
, NULL
);
507 printf("ID_SERIAL=%s\n", serial_str
);
508 util_replace_whitespace(dev_scsi
.serial_short
, serial_str
, sizeof(serial_str
));
509 util_replace_chars(serial_str
, NULL
);
510 printf("ID_SERIAL_SHORT=%s\n", serial_str
);
512 if (dev_scsi
.wwn
[0] != '\0') {
513 printf("ID_WWN=0x%s\n", dev_scsi
.wwn
);
514 if (dev_scsi
.wwn_vendor_extension
[0] != '\0') {
515 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi
.wwn_vendor_extension
);
516 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi
.wwn
, dev_scsi
.wwn_vendor_extension
);
518 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi
.wwn
);
520 if (dev_scsi
.tgpt_group
[0] != '\0')
521 printf("ID_TARGET_PORT=%s\n", dev_scsi
.tgpt_group
);
522 if (dev_scsi
.unit_serial_number
[0] != '\0')
523 printf("ID_SCSI_SERIAL=%s\n", dev_scsi
.unit_serial_number
);
527 if (dev_scsi
.serial
[0] == '\0') {
532 if (reformat_serial
) {
533 char serial_str
[MAX_SERIAL_LEN
];
535 util_replace_whitespace(dev_scsi
.serial
, serial_str
, sizeof(serial_str
));
536 util_replace_chars(serial_str
, NULL
);
537 printf("%s\n", serial_str
);
541 printf("%s\n", dev_scsi
.serial
);
546 int main(int argc
, char **argv
) {
548 char maj_min_dev
[MAX_PATH_LEN
];
550 char **newargv
= NULL
;
552 log_set_target(LOG_TARGET_AUTO
);
554 log_parse_environment();
558 * Get config file options.
560 retval
= get_file_options(NULL
, NULL
, &newargc
, &newargv
);
568 if (set_options(newargc
, newargv
, maj_min_dev
) < 0) {
575 * Get command line options (overriding any config file settings).
577 if (set_options(argc
, argv
, maj_min_dev
) < 0)
580 if (!dev_specified
) {
581 log_error("No device specified.");
586 retval
= scsi_id(maj_min_dev
);