1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright © IBM Corp. 2003
4 * Copyright © SUSE Linux Products GmbH, 2006
23 #include "libudev-private.h"
25 #include "string-util.h"
26 #include "udev-util.h"
28 static const struct option options
[] = {
29 { "device", required_argument
, NULL
, 'd' },
30 { "config", required_argument
, NULL
, 'f' },
31 { "page", required_argument
, NULL
, 'p' },
32 { "blacklisted", no_argument
, NULL
, 'b' },
33 { "whitelisted", no_argument
, NULL
, 'g' },
34 { "replace-whitespace", no_argument
, NULL
, 'u' },
35 { "sg-version", required_argument
, NULL
, 's' },
36 { "verbose", no_argument
, NULL
, 'v' },
37 { "version", no_argument
, NULL
, 'V' }, /* don't advertise -V */
38 { "export", no_argument
, NULL
, 'x' },
39 { "help", no_argument
, NULL
, 'h' },
43 static bool all_good
= false;
44 static bool dev_specified
= false;
45 static char config_file
[MAX_PATH_LEN
] = "/etc/scsi_id.config";
46 static enum page_code default_page_code
= PAGE_UNSPECIFIED
;
47 static int sg_version
= 4;
48 static bool reformat_serial
= false;
49 static bool export
= false;
50 static char vendor_str
[64];
51 static char model_str
[64];
52 static char vendor_enc_str
[256];
53 static char model_enc_str
[256];
54 static char revision_str
[16];
55 static char type_str
[16];
57 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
)
105 static const char *quote_string
= "\"\n";
106 static const char *comma_string
= ",\n";
110 if (**buffer
== '"') {
112 * skip leading quote, terminate when quote seen
119 val
= strsep(buffer
, end
);
120 if (val
&& end
== quote_string
)
122 * skip trailing quote
126 while (isspace(**buffer
))
132 static int argc_count(char *opts
)
135 while (*opts
!= '\0')
144 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
145 * if vendor and model are set find the first OPTIONS line in the config
146 * file that matches. Set argc and argv to match the OPTIONS string.
148 * vendor and model can end in '\n'.
150 static int get_file_options(struct udev
*udev
,
151 const char *vendor
, const char *model
,
152 int *argc
, char ***newargv
)
154 _cleanup_free_
char *buffer
= NULL
;
155 _cleanup_fclose_
FILE *f
;
158 char *vendor_in
, *model_in
, *options_in
; /* read in from file */
163 f
= fopen(config_file
, "re");
168 log_error_errno(errno
, "can't open %s: %m", config_file
);
174 * Allocate a buffer rather than put it on the stack so we can
175 * keep it around to parse any options (any allocated newargv
176 * points into this buffer for its strings).
178 buffer
= malloc(MAX_BUFFER_LEN
);
185 vendor_in
= model_in
= options_in
= NULL
;
187 buf
= fgets(buffer
, MAX_BUFFER_LEN
, f
);
191 if (buf
[strlen(buffer
) - 1] != '\n') {
192 log_error("Config file line %d too long", lineno
);
196 while (isspace(*buf
))
199 /* blank or all whitespace line */
207 str1
= strsep(&buf
, "=");
208 if (str1
&& strcaseeq(str1
, "VENDOR")) {
209 str1
= get_value(&buf
);
216 str1
= strsep(&buf
, "=");
217 if (str1
&& strcaseeq(str1
, "MODEL")) {
218 str1
= get_value(&buf
);
224 str1
= strsep(&buf
, "=");
228 if (str1
&& strcaseeq(str1
, "OPTIONS")) {
229 str1
= get_value(&buf
);
238 * Only allow: [vendor=foo[,model=bar]]options=stuff
240 if (!options_in
|| (!vendor_in
&& model_in
)) {
241 log_error("Error parsing config file line %d '%s'", lineno
, buffer
);
245 if (vendor
== NULL
) {
246 if (vendor_in
== NULL
)
248 } else if (vendor_in
&&
249 startswith(vendor
, vendor_in
) &&
250 (!model_in
|| startswith(model
, model_in
))) {
252 * Matched vendor and optionally model.
254 * Note: a short vendor_in or model_in can
255 * give a partial match (that is FOO
263 if (vendor_in
!= NULL
|| model_in
!= NULL
||
264 options_in
!= NULL
) {
266 * Something matched. Allocate newargv, and store
267 * values found in options_in.
269 strcpy(buffer
, options_in
);
270 c
= argc_count(buffer
) + 2;
271 *newargv
= calloc(c
, sizeof(**newargv
));
278 * argv[0] at 0 is skipped by getopt, but
279 * store the buffer address there for
282 (*newargv
)[c
] = buffer
;
283 for (c
= 1; c
< *argc
; c
++)
284 (*newargv
)[c
] = strsep(&buffer
, " \t");
295 static void help(void) {
296 printf("Usage: %s [OPTION...] DEVICE\n\n"
297 "SCSI device identification.\n\n"
298 " -h --help Print this message\n"
299 " --version Print version of the program\n\n"
300 " -d --device= Device node for SG_IO commands\n"
301 " -f --config= Location of config file\n"
302 " -p --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
303 " -s --sg-version=3|4 Use SGv3 or SGv4\n"
304 " -b --blacklisted Treat device as blacklisted\n"
305 " -g --whitelisted Treat device as whitelisted\n"
306 " -u --replace-whitespace Replace all whitespace by underscores\n"
307 " -v --verbose Verbose logging\n"
308 " -x --export Print values as environment keys\n"
309 , program_invocation_short_name
);
313 static int set_options(struct udev
*udev
,
314 int argc
, char **argv
,
320 * optind is a global extern used by getopt. Since we can call
321 * set_options twice (once for command line, and once for config
322 * file) we have to reset this back to 1.
325 while ((option
= getopt_long(argc
, argv
, "d:f:gp:uvVxhbs:", options
, NULL
)) >= 0)
332 dev_specified
= true;
333 strscpy(maj_min_dev
, MAX_PATH_LEN
, optarg
);
337 strscpy(config_file
, MAX_PATH_LEN
, optarg
);
349 if (streq(optarg
, "0x80"))
350 default_page_code
= PAGE_80
;
351 else if (streq(optarg
, "0x83"))
352 default_page_code
= PAGE_83
;
353 else if (streq(optarg
, "pre-spc3-83"))
354 default_page_code
= PAGE_83_PRE_SPC3
;
356 log_error("Unknown page code '%s'", optarg
);
362 sg_version
= atoi(optarg
);
363 if (sg_version
< 3 || sg_version
> 4) {
364 log_error("Unknown SG version '%s'", optarg
);
370 reformat_serial
= true;
374 log_set_target(LOG_TARGET_CONSOLE
);
375 log_set_max_level(LOG_DEBUG
);
380 printf("%s\n", PACKAGE_VERSION
);
391 assert_not_reached("Unknown option");
394 if (optind
< argc
&& !dev_specified
) {
395 dev_specified
= true;
396 strscpy(maj_min_dev
, MAX_PATH_LEN
, argv
[optind
]);
402 static int per_dev_options(struct udev
*udev
,
403 struct scsi_id_device
*dev_scsi
, int *good_bad
, int *page_code
)
407 char **newargv
= NULL
;
410 *good_bad
= all_good
;
411 *page_code
= default_page_code
;
413 retval
= get_file_options(udev
, vendor_str
, model_str
, &newargc
, &newargv
);
415 optind
= 1; /* reset this global extern */
416 while (retval
== 0) {
417 option
= getopt_long(newargc
, newargv
, "bgp:", options
, NULL
);
431 if (streq(optarg
, "0x80")) {
432 *page_code
= PAGE_80
;
433 } else if (streq(optarg
, "0x83")) {
434 *page_code
= PAGE_83
;
435 } else if (streq(optarg
, "pre-spc3-83")) {
436 *page_code
= PAGE_83_PRE_SPC3
;
438 log_error("Unknown page code '%s'", optarg
);
444 log_error("Unknown or bad option '%c' (0x%x)", option
, option
);
457 static int set_inq_values(struct udev
*udev
, struct scsi_id_device
*dev_scsi
, const char *path
)
461 dev_scsi
->use_sg
= sg_version
;
463 retval
= scsi_std_inquiry(udev
, dev_scsi
, path
);
467 udev_util_encode_string(dev_scsi
->vendor
, vendor_enc_str
, sizeof(vendor_enc_str
));
468 udev_util_encode_string(dev_scsi
->model
, model_enc_str
, sizeof(model_enc_str
));
470 util_replace_whitespace(dev_scsi
->vendor
, vendor_str
, sizeof(vendor_str
));
471 util_replace_chars(vendor_str
, NULL
);
472 util_replace_whitespace(dev_scsi
->model
, model_str
, sizeof(model_str
));
473 util_replace_chars(model_str
, NULL
);
474 set_type(dev_scsi
->type
, type_str
, sizeof(type_str
));
475 util_replace_whitespace(dev_scsi
->revision
, revision_str
, sizeof(revision_str
));
476 util_replace_chars(revision_str
, NULL
);
481 * scsi_id: try to get an id, if one is found, printf it to stdout.
482 * returns a value passed to exit() - 0 if printed an id, else 1.
484 static int scsi_id(struct udev
*udev
, char *maj_min_dev
)
486 struct scsi_id_device dev_scsi
= {};
491 if (set_inq_values(udev
, &dev_scsi
, maj_min_dev
) < 0) {
496 /* get per device (vendor + model) options from the config file */
497 per_dev_options(udev
, &dev_scsi
, &good_dev
, &page_code
);
503 /* read serial number from mode pages (no values for optical drives) */
504 scsi_get_serial(udev
, &dev_scsi
, maj_min_dev
, page_code
, MAX_SERIAL_LEN
);
507 char serial_str
[MAX_SERIAL_LEN
];
509 printf("ID_SCSI=1\n");
510 printf("ID_VENDOR=%s\n", vendor_str
);
511 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str
);
512 printf("ID_MODEL=%s\n", model_str
);
513 printf("ID_MODEL_ENC=%s\n", model_enc_str
);
514 printf("ID_REVISION=%s\n", revision_str
);
515 printf("ID_TYPE=%s\n", type_str
);
516 if (dev_scsi
.serial
[0] != '\0') {
517 util_replace_whitespace(dev_scsi
.serial
, serial_str
, sizeof(serial_str
));
518 util_replace_chars(serial_str
, NULL
);
519 printf("ID_SERIAL=%s\n", serial_str
);
520 util_replace_whitespace(dev_scsi
.serial_short
, serial_str
, sizeof(serial_str
));
521 util_replace_chars(serial_str
, NULL
);
522 printf("ID_SERIAL_SHORT=%s\n", serial_str
);
524 if (dev_scsi
.wwn
[0] != '\0') {
525 printf("ID_WWN=0x%s\n", dev_scsi
.wwn
);
526 if (dev_scsi
.wwn_vendor_extension
[0] != '\0') {
527 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi
.wwn_vendor_extension
);
528 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi
.wwn
, dev_scsi
.wwn_vendor_extension
);
530 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi
.wwn
);
532 if (dev_scsi
.tgpt_group
[0] != '\0')
533 printf("ID_TARGET_PORT=%s\n", dev_scsi
.tgpt_group
);
534 if (dev_scsi
.unit_serial_number
[0] != '\0')
535 printf("ID_SCSI_SERIAL=%s\n", dev_scsi
.unit_serial_number
);
539 if (dev_scsi
.serial
[0] == '\0') {
544 if (reformat_serial
) {
545 char serial_str
[MAX_SERIAL_LEN
];
547 util_replace_whitespace(dev_scsi
.serial
, serial_str
, sizeof(serial_str
));
548 util_replace_chars(serial_str
, NULL
);
549 printf("%s\n", serial_str
);
553 printf("%s\n", dev_scsi
.serial
);
558 int main(int argc
, char **argv
) {
559 _cleanup_(udev_unrefp
) struct udev
*udev
;
561 char maj_min_dev
[MAX_PATH_LEN
];
563 char **newargv
= NULL
;
565 log_set_target(LOG_TARGET_AUTO
);
567 log_parse_environment();
575 * Get config file options.
577 retval
= get_file_options(udev
, NULL
, NULL
, &newargc
, &newargv
);
585 if (set_options(udev
, newargc
, newargv
, maj_min_dev
) < 0) {
592 * Get command line options (overriding any config file settings).
594 if (set_options(udev
, argc
, argv
, maj_min_dev
) < 0)
597 if (!dev_specified
) {
598 log_error("No device specified.");
603 retval
= scsi_id(udev
, maj_min_dev
);