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/>.
33 #include "libudev-private.h"
36 static const struct option options
[] = {
37 { "device", required_argument
, NULL
, 'd' },
38 { "config", required_argument
, NULL
, 'f' },
39 { "page", required_argument
, NULL
, 'p' },
40 { "blacklisted", no_argument
, NULL
, 'b' },
41 { "whitelisted", no_argument
, NULL
, 'g' },
42 { "replace-whitespace", no_argument
, NULL
, 'u' },
43 { "sg-version", required_argument
, NULL
, 's' },
44 { "verbose", no_argument
, NULL
, 'v' },
45 { "version", no_argument
, NULL
, 'V' },
46 { "export", no_argument
, NULL
, 'x' },
47 { "help", no_argument
, NULL
, 'h' },
51 static const char short_options
[] = "d:f:ghip:uvVx";
52 static const char dev_short_options
[] = "bgp:";
55 static int dev_specified
;
56 static char config_file
[MAX_PATH_LEN
] = SYSCONFDIR
"/scsi_id.config";
57 static enum page_code default_page_code
;
58 static int sg_version
= 4;
59 static int use_stderr
;
61 static int reformat_serial
;
63 static char vendor_str
[64];
64 static char model_str
[64];
65 static char vendor_enc_str
[256];
66 static char model_enc_str
[256];
67 static char revision_str
[16];
68 static char type_str
[16];
70 static void log_fn(struct udev
*udev
, int priority
,
71 const char *file
, int line
, const char *fn
,
72 const char *format
, va_list args
)
74 vsyslog(priority
, format
, args
);
77 static void set_type(const char *from
, char *to
, size_t len
)
81 char *type
= "generic";
83 type_num
= strtoul(from
, &eptr
, 0);
111 util_strscpy(to
, len
, type
);
117 * buf points to an '=' followed by a quoted string ("foo") or a string ending
118 * with a space or ','.
120 * Return a pointer to the NUL terminated string, returns NULL if no
123 static char *get_value(char **buffer
)
125 static char *quote_string
= "\"\n";
126 static char *comma_string
= ",\n";
130 if (**buffer
== '"') {
132 * skip leading quote, terminate when quote seen
139 val
= strsep(buffer
, end
);
140 if (val
&& end
== quote_string
)
142 * skip trailing quote
146 while (isspace(**buffer
))
152 static int argc_count(char *opts
)
155 while (*opts
!= '\0')
164 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
165 * if vendor and model are set find the first OPTIONS line in the config
166 * file that matches. Set argc and argv to match the OPTIONS string.
168 * vendor and model can end in '\n'.
170 static int get_file_options(struct udev
*udev
,
171 const char *vendor
, const char *model
,
172 int *argc
, char ***newargv
)
178 char *vendor_in
, *model_in
, *options_in
; /* read in from file */
183 dbg(udev
, "vendor='%s'; model='%s'\n", vendor
, model
);
184 fd
= fopen(config_file
, "r");
186 dbg(udev
, "can't open %s\n", config_file
);
187 if (errno
== ENOENT
) {
190 err(udev
, "can't open %s: %s\n", config_file
, strerror(errno
));
196 * Allocate a buffer rather than put it on the stack so we can
197 * keep it around to parse any options (any allocated newargv
198 * points into this buffer for its strings).
200 buffer
= malloc(MAX_BUFFER_LEN
);
203 err(udev
, "can't allocate memory\n");
210 vendor_in
= model_in
= options_in
= NULL
;
212 buf
= fgets(buffer
, MAX_BUFFER_LEN
, fd
);
216 if (buf
[strlen(buffer
) - 1] != '\n') {
217 err(udev
, "Config file line %d too long\n", lineno
);
221 while (isspace(*buf
))
224 /* blank or all whitespace line */
232 dbg(udev
, "lineno %d: '%s'\n", lineno
, buf
);
233 str1
= strsep(&buf
, "=");
234 if (str1
&& strcasecmp(str1
, "VENDOR") == 0) {
235 str1
= get_value(&buf
);
242 str1
= strsep(&buf
, "=");
243 if (str1
&& strcasecmp(str1
, "MODEL") == 0) {
244 str1
= get_value(&buf
);
250 str1
= strsep(&buf
, "=");
254 if (str1
&& strcasecmp(str1
, "OPTIONS") == 0) {
255 str1
= get_value(&buf
);
262 dbg(udev
, "config file line %d:\n"
263 " vendor '%s'; model '%s'; options '%s'\n",
264 lineno
, vendor_in
, model_in
, options_in
);
266 * Only allow: [vendor=foo[,model=bar]]options=stuff
268 if (!options_in
|| (!vendor_in
&& model_in
)) {
269 err(udev
, "Error parsing config file line %d '%s'\n", lineno
, buffer
);
273 if (vendor
== NULL
) {
274 if (vendor_in
== NULL
) {
275 dbg(udev
, "matched global option\n");
278 } else if ((vendor_in
&& strncmp(vendor
, vendor_in
,
279 strlen(vendor_in
)) == 0) &&
280 (!model_in
|| (strncmp(model
, model_in
,
281 strlen(model_in
)) == 0))) {
283 * Matched vendor and optionally model.
285 * Note: a short vendor_in or model_in can
286 * give a partial match (that is FOO
289 dbg(udev
, "matched vendor/model\n");
292 dbg(udev
, "no match\n");
297 if (vendor_in
!= NULL
|| model_in
!= NULL
||
298 options_in
!= NULL
) {
300 * Something matched. Allocate newargv, and store
301 * values found in options_in.
303 strcpy(buffer
, options_in
);
304 c
= argc_count(buffer
) + 2;
305 *newargv
= calloc(c
, sizeof(**newargv
));
307 err(udev
, "can't allocate memory\n");
313 * argv[0] at 0 is skipped by getopt, but
314 * store the buffer address there for
317 (*newargv
)[c
] = buffer
;
318 for (c
= 1; c
< *argc
; c
++)
319 (*newargv
)[c
] = strsep(&buffer
, " \t");
332 static int set_options(struct udev
*udev
,
333 int argc
, char **argv
, const char *short_opts
,
339 * optind is a global extern used by getopt. Since we can call
340 * set_options twice (once for command line, and once for config
341 * file) we have to reset this back to 1.
345 option
= getopt_long(argc
, argv
, short_opts
, options
, NULL
);
350 dbg(udev
, "option '%c' arg '%s'\n", option
, optarg
);
352 dbg(udev
, "option '%c'\n", option
);
361 util_strscpy(maj_min_dev
, MAX_PATH_LEN
, optarg
);
369 util_strscpy(config_file
, MAX_PATH_LEN
, optarg
);
377 printf("Usage: scsi_id OPTIONS <device>\n"
378 " --device= device node for SG_IO commands\n"
379 " --config= location of config file\n"
380 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
381 " --sg-version=3|4 use SGv3 or SGv4\n"
382 " --blacklisted threat device as blacklisted\n"
383 " --whitelisted threat device as whitelisted\n"
384 " --replace-whitespace replace all whitespaces by underscores\n"
385 " --verbose verbose logging\n"
386 " --version print version\n"
387 " --export print values as environment keys\n"
388 " --help print this help text\n\n");
392 if (strcmp(optarg
, "0x80") == 0) {
393 default_page_code
= PAGE_80
;
394 } else if (strcmp(optarg
, "0x83") == 0) {
395 default_page_code
= PAGE_83
;
396 } else if (strcmp(optarg
, "pre-spc3-83") == 0) {
397 default_page_code
= PAGE_83_PRE_SPC3
;
399 err(udev
, "Unknown page code '%s'\n", optarg
);
405 sg_version
= atoi(optarg
);
406 if (sg_version
< 3 || sg_version
> 4) {
407 err(udev
, "Unknown SG version '%s'\n", optarg
);
425 printf("%s\n", VERSION
);
433 if (optind
< argc
&& !dev_specified
) {
435 util_strscpy(maj_min_dev
, MAX_PATH_LEN
, argv
[optind
]);
440 static int per_dev_options(struct udev
*udev
,
441 struct scsi_id_device
*dev_scsi
, int *good_bad
, int *page_code
)
445 char **newargv
= NULL
;
448 *good_bad
= all_good
;
449 *page_code
= default_page_code
;
451 retval
= get_file_options(udev
, vendor_str
, model_str
, &newargc
, &newargv
);
453 optind
= 1; /* reset this global extern */
454 while (retval
== 0) {
455 option
= getopt_long(newargc
, newargv
, dev_short_options
, options
, NULL
);
460 dbg(udev
, "option '%c' arg '%s'\n", option
, optarg
);
462 dbg(udev
, "option '%c'\n", option
);
474 if (strcmp(optarg
, "0x80") == 0) {
475 *page_code
= PAGE_80
;
476 } else if (strcmp(optarg
, "0x83") == 0) {
477 *page_code
= PAGE_83
;
478 } else if (strcmp(optarg
, "pre-spc3-83") == 0) {
479 *page_code
= PAGE_83_PRE_SPC3
;
481 err(udev
, "Unknown page code '%s'\n", optarg
);
487 err(udev
, "Unknown or bad option '%c' (0x%x)\n", option
, option
);
500 static int set_inq_values(struct udev
*udev
, struct scsi_id_device
*dev_scsi
, const char *path
)
504 dev_scsi
->use_sg
= sg_version
;
506 retval
= scsi_std_inquiry(udev
, dev_scsi
, path
);
510 udev_util_encode_string(dev_scsi
->vendor
, vendor_enc_str
, sizeof(vendor_enc_str
));
511 udev_util_encode_string(dev_scsi
->model
, model_enc_str
, sizeof(model_enc_str
));
513 util_replace_whitespace(dev_scsi
->vendor
, vendor_str
, sizeof(vendor_str
));
514 util_replace_chars(vendor_str
, NULL
);
515 util_replace_whitespace(dev_scsi
->model
, model_str
, sizeof(model_str
));
516 util_replace_chars(model_str
, NULL
);
517 set_type(dev_scsi
->type
, type_str
, sizeof(type_str
));
518 util_replace_whitespace(dev_scsi
->revision
, revision_str
, sizeof(revision_str
));
519 util_replace_chars(revision_str
, NULL
);
524 * scsi_id: try to get an id, if one is found, printf it to stdout.
525 * returns a value passed to exit() - 0 if printed an id, else 1.
527 static int scsi_id(struct udev
*udev
, char *maj_min_dev
)
529 struct scsi_id_device dev_scsi
;
534 memset(&dev_scsi
, 0x00, sizeof(struct scsi_id_device
));
536 if (set_inq_values(udev
, &dev_scsi
, maj_min_dev
) < 0) {
541 /* get per device (vendor + model) options from the config file */
542 per_dev_options(udev
, &dev_scsi
, &good_dev
, &page_code
);
543 dbg(udev
, "per dev options: good %d; page code 0x%x\n", good_dev
, page_code
);
549 /* read serial number from mode pages (no values for optical drives) */
550 scsi_get_serial(udev
, &dev_scsi
, maj_min_dev
, page_code
, MAX_SERIAL_LEN
);
553 char serial_str
[MAX_SERIAL_LEN
];
555 printf("ID_SCSI=1\n");
556 printf("ID_VENDOR=%s\n", vendor_str
);
557 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str
);
558 printf("ID_MODEL=%s\n", model_str
);
559 printf("ID_MODEL_ENC=%s\n", model_enc_str
);
560 printf("ID_REVISION=%s\n", revision_str
);
561 printf("ID_TYPE=%s\n", type_str
);
562 if (dev_scsi
.serial
[0] != '\0') {
563 util_replace_whitespace(dev_scsi
.serial
, serial_str
, sizeof(serial_str
));
564 util_replace_chars(serial_str
, NULL
);
565 printf("ID_SERIAL=%s\n", serial_str
);
566 util_replace_whitespace(dev_scsi
.serial_short
, serial_str
, sizeof(serial_str
));
567 util_replace_chars(serial_str
, NULL
);
568 printf("ID_SERIAL_SHORT=%s\n", serial_str
);
570 if (dev_scsi
.wwn
[0] != '\0') {
571 printf("ID_WWN=0x%s\n", dev_scsi
.wwn
);
572 if (dev_scsi
.wwn_vendor_extension
[0] != '\0') {
573 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi
.wwn_vendor_extension
);
574 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi
.wwn
, dev_scsi
.wwn_vendor_extension
);
576 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi
.wwn
);
579 if (dev_scsi
.tgpt_group
[0] != '\0') {
580 printf("ID_TARGET_PORT=%s\n", dev_scsi
.tgpt_group
);
582 if (dev_scsi
.unit_serial_number
[0] != '\0') {
583 printf("ID_SCSI_SERIAL=%s\n", dev_scsi
.unit_serial_number
);
588 if (dev_scsi
.serial
[0] == '\0') {
593 if (reformat_serial
) {
594 char serial_str
[MAX_SERIAL_LEN
];
596 util_replace_whitespace(dev_scsi
.serial
, serial_str
, sizeof(serial_str
));
597 util_replace_chars(serial_str
, NULL
);
598 printf("%s\n", serial_str
);
602 printf("%s\n", dev_scsi
.serial
);
607 int main(int argc
, char **argv
)
611 char maj_min_dev
[MAX_PATH_LEN
];
619 udev_log_init("scsi_id");
620 udev_set_log_fn(udev
, log_fn
);
623 * Get config file options.
626 retval
= get_file_options(udev
, NULL
, NULL
, &newargc
, &newargv
);
631 if (newargv
&& (retval
== 0)) {
632 if (set_options(udev
, newargc
, newargv
, short_options
, maj_min_dev
) < 0) {
640 * Get command line options (overriding any config file settings).
642 if (set_options(udev
, argc
, argv
, short_options
, maj_min_dev
) < 0)
645 if (!dev_specified
) {
646 err(udev
, "no device specified\n");
651 retval
= scsi_id(udev
, maj_min_dev
);