2 * Copyright (C) IBM Corp. 2003
3 * Copyright (C) SUSE Linux Products GmbH, 2006
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 #include "libudev-private.h"
37 #include "string-util.h"
38 #include "udev-util.h"
40 static const struct option options
[] = {
41 { "device", required_argument
, NULL
, 'd' },
42 { "config", required_argument
, NULL
, 'f' },
43 { "page", required_argument
, NULL
, 'p' },
44 { "blacklisted", no_argument
, NULL
, 'b' },
45 { "whitelisted", no_argument
, NULL
, 'g' },
46 { "replace-whitespace", no_argument
, NULL
, 'u' },
47 { "sg-version", required_argument
, NULL
, 's' },
48 { "verbose", no_argument
, NULL
, 'v' },
49 { "version", no_argument
, NULL
, 'V' }, /* don't advertise -V */
50 { "export", no_argument
, NULL
, 'x' },
51 { "help", no_argument
, NULL
, 'h' },
55 static bool all_good
= false;
56 static bool dev_specified
= false;
57 static char config_file
[MAX_PATH_LEN
] = "/etc/scsi_id.config";
58 static enum page_code default_page_code
= PAGE_UNSPECIFIED
;
59 static int sg_version
= 4;
60 static bool reformat_serial
= false;
61 static bool export
= false;
62 static char vendor_str
[64];
63 static char model_str
[64];
64 static char vendor_enc_str
[256];
65 static char model_enc_str
[256];
66 static char revision_str
[16];
67 static char type_str
[16];
69 static void set_type(const char *from
, char *to
, size_t len
)
73 const char *type
= "generic";
75 type_num
= strtoul(from
, &eptr
, 0);
103 strscpy(to
, len
, type
);
109 * buf points to an '=' followed by a quoted string ("foo") or a string ending
110 * with a space or ','.
112 * Return a pointer to the NUL terminated string, returns NULL if no
115 static char *get_value(char **buffer
)
117 static const char *quote_string
= "\"\n";
118 static const char *comma_string
= ",\n";
122 if (**buffer
== '"') {
124 * skip leading quote, terminate when quote seen
131 val
= strsep(buffer
, end
);
132 if (val
&& end
== quote_string
)
134 * skip trailing quote
138 while (isspace(**buffer
))
144 static int argc_count(char *opts
)
147 while (*opts
!= '\0')
156 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
157 * if vendor and model are set find the first OPTIONS line in the config
158 * file that matches. Set argc and argv to match the OPTIONS string.
160 * vendor and model can end in '\n'.
162 static int get_file_options(struct udev
*udev
,
163 const char *vendor
, const char *model
,
164 int *argc
, char ***newargv
)
167 _cleanup_fclose_
FILE *f
;
170 char *vendor_in
, *model_in
, *options_in
; /* read in from file */
175 f
= fopen(config_file
, "re");
180 log_error_errno(errno
, "can't open %s: %m", config_file
);
186 * Allocate a buffer rather than put it on the stack so we can
187 * keep it around to parse any options (any allocated newargv
188 * points into this buffer for its strings).
190 buffer
= malloc(MAX_BUFFER_LEN
);
197 vendor_in
= model_in
= options_in
= NULL
;
199 buf
= fgets(buffer
, MAX_BUFFER_LEN
, f
);
203 if (buf
[strlen(buffer
) - 1] != '\n') {
204 log_error("Config file line %d too long", lineno
);
208 while (isspace(*buf
))
211 /* blank or all whitespace line */
219 str1
= strsep(&buf
, "=");
220 if (str1
&& strcaseeq(str1
, "VENDOR")) {
221 str1
= get_value(&buf
);
228 str1
= strsep(&buf
, "=");
229 if (str1
&& strcaseeq(str1
, "MODEL")) {
230 str1
= get_value(&buf
);
236 str1
= strsep(&buf
, "=");
240 if (str1
&& strcaseeq(str1
, "OPTIONS")) {
241 str1
= get_value(&buf
);
250 * Only allow: [vendor=foo[,model=bar]]options=stuff
252 if (!options_in
|| (!vendor_in
&& model_in
)) {
253 log_error("Error parsing config file line %d '%s'", lineno
, buffer
);
257 if (vendor
== NULL
) {
258 if (vendor_in
== NULL
)
260 } else if (vendor_in
&&
261 strneq(vendor
, vendor_in
, strlen(vendor_in
)) &&
263 (strneq(model
, model_in
, strlen(model_in
))))) {
265 * Matched vendor and optionally model.
267 * Note: a short vendor_in or model_in can
268 * give a partial match (that is FOO
276 if (vendor_in
!= NULL
|| model_in
!= NULL
||
277 options_in
!= NULL
) {
279 * Something matched. Allocate newargv, and store
280 * values found in options_in.
282 strcpy(buffer
, options_in
);
283 c
= argc_count(buffer
) + 2;
284 *newargv
= calloc(c
, sizeof(**newargv
));
291 * argv[0] at 0 is skipped by getopt, but
292 * store the buffer address there for
295 (*newargv
)[c
] = buffer
;
296 for (c
= 1; c
< *argc
; c
++)
297 (*newargv
)[c
] = strsep(&buffer
, " \t");
309 static void help(void) {
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
);
327 static int set_options(struct udev
*udev
,
328 int argc
, char **argv
,
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.
339 while ((option
= getopt_long(argc
, argv
, "d:f:gp:uvVxh", options
, NULL
)) >= 0)
346 dev_specified
= true;
347 strscpy(maj_min_dev
, MAX_PATH_LEN
, optarg
);
351 strscpy(config_file
, MAX_PATH_LEN
, optarg
);
363 if (streq(optarg
, "0x80"))
364 default_page_code
= PAGE_80
;
365 else if (streq(optarg
, "0x83"))
366 default_page_code
= PAGE_83
;
367 else if (streq(optarg
, "pre-spc3-83"))
368 default_page_code
= PAGE_83_PRE_SPC3
;
370 log_error("Unknown page code '%s'", optarg
);
376 sg_version
= atoi(optarg
);
377 if (sg_version
< 3 || sg_version
> 4) {
378 log_error("Unknown SG version '%s'", optarg
);
384 reformat_serial
= true;
388 log_set_target(LOG_TARGET_CONSOLE
);
389 log_set_max_level(LOG_DEBUG
);
394 printf("%s\n", VERSION
);
405 assert_not_reached("Unknown option");
408 if (optind
< argc
&& !dev_specified
) {
409 dev_specified
= true;
410 strscpy(maj_min_dev
, MAX_PATH_LEN
, argv
[optind
]);
416 static int per_dev_options(struct udev
*udev
,
417 struct scsi_id_device
*dev_scsi
, int *good_bad
, int *page_code
)
421 char **newargv
= NULL
;
424 *good_bad
= all_good
;
425 *page_code
= default_page_code
;
427 retval
= get_file_options(udev
, vendor_str
, model_str
, &newargc
, &newargv
);
429 optind
= 1; /* reset this global extern */
430 while (retval
== 0) {
431 option
= getopt_long(newargc
, newargv
, "bgp:", options
, NULL
);
445 if (streq(optarg
, "0x80")) {
446 *page_code
= PAGE_80
;
447 } else if (streq(optarg
, "0x83")) {
448 *page_code
= PAGE_83
;
449 } else if (streq(optarg
, "pre-spc3-83")) {
450 *page_code
= PAGE_83_PRE_SPC3
;
452 log_error("Unknown page code '%s'", optarg
);
458 log_error("Unknown or bad option '%c' (0x%x)", option
, option
);
471 static int set_inq_values(struct udev
*udev
, struct scsi_id_device
*dev_scsi
, const char *path
)
475 dev_scsi
->use_sg
= sg_version
;
477 retval
= scsi_std_inquiry(udev
, dev_scsi
, path
);
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
));
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
);
495 * scsi_id: try to get an id, if one is found, printf it to stdout.
496 * returns a value passed to exit() - 0 if printed an id, else 1.
498 static int scsi_id(struct udev
*udev
, char *maj_min_dev
)
500 struct scsi_id_device dev_scsi
= {};
505 if (set_inq_values(udev
, &dev_scsi
, maj_min_dev
) < 0) {
510 /* get per device (vendor + model) options from the config file */
511 per_dev_options(udev
, &dev_scsi
, &good_dev
, &page_code
);
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
);
521 char serial_str
[MAX_SERIAL_LEN
];
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
);
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
);
544 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi
.wwn
);
546 if (dev_scsi
.tgpt_group
[0] != '\0')
547 printf("ID_TARGET_PORT=%s\n", dev_scsi
.tgpt_group
);
548 if (dev_scsi
.unit_serial_number
[0] != '\0')
549 printf("ID_SCSI_SERIAL=%s\n", dev_scsi
.unit_serial_number
);
553 if (dev_scsi
.serial
[0] == '\0') {
558 if (reformat_serial
) {
559 char serial_str
[MAX_SERIAL_LEN
];
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
);
567 printf("%s\n", dev_scsi
.serial
);
572 int main(int argc
, char **argv
)
574 _cleanup_udev_unref_
struct udev
*udev
;
576 char maj_min_dev
[MAX_PATH_LEN
];
578 char **newargv
= NULL
;
580 log_parse_environment();
588 * Get config file options.
590 retval
= get_file_options(udev
, NULL
, NULL
, &newargc
, &newargv
);
598 if (set_options(udev
, newargc
, newargv
, maj_min_dev
) < 0) {
605 * Get command line options (overriding any config file settings).
607 if (set_options(udev
, argc
, argv
, maj_min_dev
) < 0)
610 if (!dev_specified
) {
611 log_error("No device specified.");
616 retval
= scsi_id(udev
, maj_min_dev
);