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/>.
34 #include "libudev-private.h"
36 #include "string-util.h"
37 #include "udev-util.h"
39 static const struct option options
[] = {
40 { "device", required_argument
, NULL
, 'd' },
41 { "config", required_argument
, NULL
, 'f' },
42 { "page", required_argument
, NULL
, 'p' },
43 { "blacklisted", no_argument
, NULL
, 'b' },
44 { "whitelisted", no_argument
, NULL
, 'g' },
45 { "replace-whitespace", no_argument
, NULL
, 'u' },
46 { "sg-version", required_argument
, NULL
, 's' },
47 { "verbose", no_argument
, NULL
, 'v' },
48 { "version", no_argument
, NULL
, 'V' }, /* don't advertise -V */
49 { "export", no_argument
, NULL
, 'x' },
50 { "help", no_argument
, NULL
, 'h' },
54 static bool all_good
= false;
55 static bool dev_specified
= false;
56 static char config_file
[MAX_PATH_LEN
] = "/etc/scsi_id.config";
57 static enum page_code default_page_code
= PAGE_UNSPECIFIED
;
58 static int sg_version
= 4;
59 static bool reformat_serial
= false;
60 static bool export
= false;
61 static char vendor_str
[64];
62 static char model_str
[64];
63 static char vendor_enc_str
[256];
64 static char model_enc_str
[256];
65 static char revision_str
[16];
66 static char type_str
[16];
68 static void set_type(const char *from
, char *to
, size_t len
)
72 const char *type
= "generic";
74 type_num
= strtoul(from
, &eptr
, 0);
102 strscpy(to
, len
, type
);
108 * buf points to an '=' followed by a quoted string ("foo") or a string ending
109 * with a space or ','.
111 * Return a pointer to the NUL terminated string, returns NULL if no
114 static char *get_value(char **buffer
)
116 static const char *quote_string
= "\"\n";
117 static const char *comma_string
= ",\n";
121 if (**buffer
== '"') {
123 * skip leading quote, terminate when quote seen
130 val
= strsep(buffer
, end
);
131 if (val
&& end
== quote_string
)
133 * skip trailing quote
137 while (isspace(**buffer
))
143 static int argc_count(char *opts
)
146 while (*opts
!= '\0')
155 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
156 * if vendor and model are set find the first OPTIONS line in the config
157 * file that matches. Set argc and argv to match the OPTIONS string.
159 * vendor and model can end in '\n'.
161 static int get_file_options(struct udev
*udev
,
162 const char *vendor
, const char *model
,
163 int *argc
, char ***newargv
)
166 _cleanup_fclose_
FILE *f
;
169 char *vendor_in
, *model_in
, *options_in
; /* read in from file */
174 f
= fopen(config_file
, "re");
179 log_error_errno(errno
, "can't open %s: %m", config_file
);
185 * Allocate a buffer rather than put it on the stack so we can
186 * keep it around to parse any options (any allocated newargv
187 * points into this buffer for its strings).
189 buffer
= malloc(MAX_BUFFER_LEN
);
196 vendor_in
= model_in
= options_in
= NULL
;
198 buf
= fgets(buffer
, MAX_BUFFER_LEN
, f
);
202 if (buf
[strlen(buffer
) - 1] != '\n') {
203 log_error("Config file line %d too long", lineno
);
207 while (isspace(*buf
))
210 /* blank or all whitespace line */
218 str1
= strsep(&buf
, "=");
219 if (str1
&& strcaseeq(str1
, "VENDOR")) {
220 str1
= get_value(&buf
);
227 str1
= strsep(&buf
, "=");
228 if (str1
&& strcaseeq(str1
, "MODEL")) {
229 str1
= get_value(&buf
);
235 str1
= strsep(&buf
, "=");
239 if (str1
&& strcaseeq(str1
, "OPTIONS")) {
240 str1
= get_value(&buf
);
249 * Only allow: [vendor=foo[,model=bar]]options=stuff
251 if (!options_in
|| (!vendor_in
&& model_in
)) {
252 log_error("Error parsing config file line %d '%s'", lineno
, buffer
);
256 if (vendor
== NULL
) {
257 if (vendor_in
== NULL
)
259 } else if (vendor_in
&&
260 strneq(vendor
, vendor_in
, strlen(vendor_in
)) &&
262 (strneq(model
, model_in
, strlen(model_in
))))) {
264 * Matched vendor and optionally model.
266 * Note: a short vendor_in or model_in can
267 * give a partial match (that is FOO
275 if (vendor_in
!= NULL
|| model_in
!= NULL
||
276 options_in
!= NULL
) {
278 * Something matched. Allocate newargv, and store
279 * values found in options_in.
281 strcpy(buffer
, options_in
);
282 c
= argc_count(buffer
) + 2;
283 *newargv
= calloc(c
, sizeof(**newargv
));
290 * argv[0] at 0 is skipped by getopt, but
291 * store the buffer address there for
294 (*newargv
)[c
] = buffer
;
295 for (c
= 1; c
< *argc
; c
++)
296 (*newargv
)[c
] = strsep(&buffer
, " \t");
308 static void help(void) {
309 printf("Usage: %s [OPTION...] DEVICE\n\n"
310 "SCSI device identification.\n\n"
311 " -h --help Print this message\n"
312 " --version Print version of the program\n\n"
313 " -d --device= Device node for SG_IO commands\n"
314 " -f --config= Location of config file\n"
315 " -p --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
316 " -s --sg-version=3|4 Use SGv3 or SGv4\n"
317 " -b --blacklisted Treat device as blacklisted\n"
318 " -g --whitelisted Treat device as whitelisted\n"
319 " -u --replace-whitespace Replace all whitespace by underscores\n"
320 " -v --verbose Verbose logging\n"
321 " -x --export Print values as environment keys\n"
322 , program_invocation_short_name
);
326 static int set_options(struct udev
*udev
,
327 int argc
, char **argv
,
333 * optind is a global extern used by getopt. Since we can call
334 * set_options twice (once for command line, and once for config
335 * file) we have to reset this back to 1.
338 while ((option
= getopt_long(argc
, argv
, "d:f:gp:uvVxh", options
, NULL
)) >= 0)
345 dev_specified
= true;
346 strscpy(maj_min_dev
, MAX_PATH_LEN
, optarg
);
350 strscpy(config_file
, MAX_PATH_LEN
, optarg
);
362 if (streq(optarg
, "0x80"))
363 default_page_code
= PAGE_80
;
364 else if (streq(optarg
, "0x83"))
365 default_page_code
= PAGE_83
;
366 else if (streq(optarg
, "pre-spc3-83"))
367 default_page_code
= PAGE_83_PRE_SPC3
;
369 log_error("Unknown page code '%s'", optarg
);
375 sg_version
= atoi(optarg
);
376 if (sg_version
< 3 || sg_version
> 4) {
377 log_error("Unknown SG version '%s'", optarg
);
383 reformat_serial
= true;
387 log_set_target(LOG_TARGET_CONSOLE
);
388 log_set_max_level(LOG_DEBUG
);
393 printf("%s\n", VERSION
);
404 assert_not_reached("Unknown option");
407 if (optind
< argc
&& !dev_specified
) {
408 dev_specified
= true;
409 strscpy(maj_min_dev
, MAX_PATH_LEN
, argv
[optind
]);
415 static int per_dev_options(struct udev
*udev
,
416 struct scsi_id_device
*dev_scsi
, int *good_bad
, int *page_code
)
420 char **newargv
= NULL
;
423 *good_bad
= all_good
;
424 *page_code
= default_page_code
;
426 retval
= get_file_options(udev
, vendor_str
, model_str
, &newargc
, &newargv
);
428 optind
= 1; /* reset this global extern */
429 while (retval
== 0) {
430 option
= getopt_long(newargc
, newargv
, "bgp:", options
, NULL
);
444 if (streq(optarg
, "0x80")) {
445 *page_code
= PAGE_80
;
446 } else if (streq(optarg
, "0x83")) {
447 *page_code
= PAGE_83
;
448 } else if (streq(optarg
, "pre-spc3-83")) {
449 *page_code
= PAGE_83_PRE_SPC3
;
451 log_error("Unknown page code '%s'", optarg
);
457 log_error("Unknown or bad option '%c' (0x%x)", option
, option
);
470 static int set_inq_values(struct udev
*udev
, struct scsi_id_device
*dev_scsi
, const char *path
)
474 dev_scsi
->use_sg
= sg_version
;
476 retval
= scsi_std_inquiry(udev
, dev_scsi
, path
);
480 udev_util_encode_string(dev_scsi
->vendor
, vendor_enc_str
, sizeof(vendor_enc_str
));
481 udev_util_encode_string(dev_scsi
->model
, model_enc_str
, sizeof(model_enc_str
));
483 util_replace_whitespace(dev_scsi
->vendor
, vendor_str
, sizeof(vendor_str
));
484 util_replace_chars(vendor_str
, NULL
);
485 util_replace_whitespace(dev_scsi
->model
, model_str
, sizeof(model_str
));
486 util_replace_chars(model_str
, NULL
);
487 set_type(dev_scsi
->type
, type_str
, sizeof(type_str
));
488 util_replace_whitespace(dev_scsi
->revision
, revision_str
, sizeof(revision_str
));
489 util_replace_chars(revision_str
, NULL
);
494 * scsi_id: try to get an id, if one is found, printf it to stdout.
495 * returns a value passed to exit() - 0 if printed an id, else 1.
497 static int scsi_id(struct udev
*udev
, char *maj_min_dev
)
499 struct scsi_id_device dev_scsi
= {};
504 if (set_inq_values(udev
, &dev_scsi
, maj_min_dev
) < 0) {
509 /* get per device (vendor + model) options from the config file */
510 per_dev_options(udev
, &dev_scsi
, &good_dev
, &page_code
);
516 /* read serial number from mode pages (no values for optical drives) */
517 scsi_get_serial(udev
, &dev_scsi
, maj_min_dev
, page_code
, MAX_SERIAL_LEN
);
520 char serial_str
[MAX_SERIAL_LEN
];
522 printf("ID_SCSI=1\n");
523 printf("ID_VENDOR=%s\n", vendor_str
);
524 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str
);
525 printf("ID_MODEL=%s\n", model_str
);
526 printf("ID_MODEL_ENC=%s\n", model_enc_str
);
527 printf("ID_REVISION=%s\n", revision_str
);
528 printf("ID_TYPE=%s\n", type_str
);
529 if (dev_scsi
.serial
[0] != '\0') {
530 util_replace_whitespace(dev_scsi
.serial
, serial_str
, sizeof(serial_str
));
531 util_replace_chars(serial_str
, NULL
);
532 printf("ID_SERIAL=%s\n", serial_str
);
533 util_replace_whitespace(dev_scsi
.serial_short
, serial_str
, sizeof(serial_str
));
534 util_replace_chars(serial_str
, NULL
);
535 printf("ID_SERIAL_SHORT=%s\n", serial_str
);
537 if (dev_scsi
.wwn
[0] != '\0') {
538 printf("ID_WWN=0x%s\n", dev_scsi
.wwn
);
539 if (dev_scsi
.wwn_vendor_extension
[0] != '\0') {
540 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi
.wwn_vendor_extension
);
541 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi
.wwn
, dev_scsi
.wwn_vendor_extension
);
543 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi
.wwn
);
545 if (dev_scsi
.tgpt_group
[0] != '\0')
546 printf("ID_TARGET_PORT=%s\n", dev_scsi
.tgpt_group
);
547 if (dev_scsi
.unit_serial_number
[0] != '\0')
548 printf("ID_SCSI_SERIAL=%s\n", dev_scsi
.unit_serial_number
);
552 if (dev_scsi
.serial
[0] == '\0') {
557 if (reformat_serial
) {
558 char serial_str
[MAX_SERIAL_LEN
];
560 util_replace_whitespace(dev_scsi
.serial
, serial_str
, sizeof(serial_str
));
561 util_replace_chars(serial_str
, NULL
);
562 printf("%s\n", serial_str
);
566 printf("%s\n", dev_scsi
.serial
);
571 int main(int argc
, char **argv
)
573 _cleanup_udev_unref_
struct udev
*udev
;
575 char maj_min_dev
[MAX_PATH_LEN
];
577 char **newargv
= NULL
;
579 log_parse_environment();
587 * Get config file options.
589 retval
= get_file_options(udev
, NULL
, NULL
, &newargc
, &newargv
);
597 if (set_options(udev
, newargc
, newargv
, maj_min_dev
) < 0) {
604 * Get command line options (overriding any config file settings).
606 if (set_options(udev
, argc
, argv
, maj_min_dev
) < 0)
609 if (!dev_specified
) {
610 log_error("No device specified.");
615 retval
= scsi_id(udev
, maj_min_dev
);