4 * Main section of the scsi_id program
6 * Copyright (C) IBM Corp. 2003
7 * Copyright (C) SUSE Linux Products GmbH, 2006
10 * Patrick Mansfield<patmans@us.ibm.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation version 2 of the License.
30 #include "../../udev/udev.h"
33 static const struct option options
[] = {
34 { "device", required_argument
, NULL
, 'd' },
35 { "config", required_argument
, NULL
, 'f' },
36 { "page", required_argument
, NULL
, 'p' },
37 { "blacklisted", no_argument
, NULL
, 'b' },
38 { "whitelisted", no_argument
, NULL
, 'g' },
39 { "replace-whitespace", no_argument
, NULL
, 'u' },
40 { "sg-version", required_argument
, NULL
, 's' },
41 { "verbose", no_argument
, NULL
, 'v' },
42 { "version", no_argument
, NULL
, 'V' },
43 { "export", no_argument
, NULL
, 'x' },
44 { "help", no_argument
, NULL
, 'h' },
48 static const char short_options
[] = "d:f:ghip:uvVx";
49 static const char dev_short_options
[] = "bgp:";
52 static int dev_specified
;
53 static char config_file
[MAX_PATH_LEN
] = SYSCONFDIR
"/scsi_id.config";
54 static enum page_code default_page_code
;
55 static int sg_version
= 4;
56 static int use_stderr
;
58 static int reformat_serial
;
60 static char vendor_str
[64];
61 static char model_str
[64];
62 static char vendor_enc_str
[256];
63 static char model_enc_str
[256];
64 static char revision_str
[16];
65 static char type_str
[16];
67 static void log_fn(struct udev
*udev
, int priority
,
68 const char *file
, int line
, const char *fn
,
69 const char *format
, va_list args
)
71 vsyslog(priority
, format
, args
);
74 static void set_type(const char *from
, char *to
, size_t len
)
78 char *type
= "generic";
80 type_num
= strtoul(from
, &eptr
, 0);
108 util_strlcpy(to
, type
, len
);
114 * buf points to an '=' followed by a quoted string ("foo") or a string ending
115 * with a space or ','.
117 * Return a pointer to the NUL terminated string, returns NULL if no
120 static char *get_value(char **buffer
)
122 static char *quote_string
= "\"\n";
123 static char *comma_string
= ",\n";
127 if (**buffer
== '"') {
129 * skip leading quote, terminate when quote seen
136 val
= strsep(buffer
, end
);
137 if (val
&& end
== quote_string
)
139 * skip trailing quote
143 while (isspace(**buffer
))
149 static int argc_count(char *opts
)
152 while (*opts
!= '\0')
161 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
162 * if vendor and model are set find the first OPTIONS line in the config
163 * file that matches. Set argc and argv to match the OPTIONS string.
165 * vendor and model can end in '\n'.
167 static int get_file_options(struct udev
*udev
,
168 const char *vendor
, const char *model
,
169 int *argc
, char ***newargv
)
175 char *vendor_in
, *model_in
, *options_in
; /* read in from file */
180 dbg(udev
, "vendor='%s'; model='%s'\n", vendor
, model
);
181 fd
= fopen(config_file
, "r");
183 dbg(udev
, "can't open %s\n", config_file
);
184 if (errno
== ENOENT
) {
187 err(udev
, "can't open %s: %s\n", config_file
, strerror(errno
));
193 * Allocate a buffer rather than put it on the stack so we can
194 * keep it around to parse any options (any allocated newargv
195 * points into this buffer for its strings).
197 buffer
= malloc(MAX_BUFFER_LEN
);
199 err(udev
, "can't allocate memory\n");
206 vendor_in
= model_in
= options_in
= NULL
;
208 buf
= fgets(buffer
, MAX_BUFFER_LEN
, fd
);
212 if (buf
[strlen(buffer
) - 1] != '\n') {
213 err(udev
, "Config file line %d too long\n", lineno
);
217 while (isspace(*buf
))
220 /* blank or all whitespace line */
228 dbg(udev
, "lineno %d: '%s'\n", lineno
, buf
);
229 str1
= strsep(&buf
, "=");
230 if (str1
&& strcasecmp(str1
, "VENDOR") == 0) {
231 str1
= get_value(&buf
);
238 str1
= strsep(&buf
, "=");
239 if (str1
&& strcasecmp(str1
, "MODEL") == 0) {
240 str1
= get_value(&buf
);
246 str1
= strsep(&buf
, "=");
250 if (str1
&& strcasecmp(str1
, "OPTIONS") == 0) {
251 str1
= get_value(&buf
);
258 dbg(udev
, "config file line %d:\n"
259 " vendor '%s'; model '%s'; options '%s'\n",
260 lineno
, vendor_in
, model_in
, options_in
);
262 * Only allow: [vendor=foo[,model=bar]]options=stuff
264 if (!options_in
|| (!vendor_in
&& model_in
)) {
265 err(udev
, "Error parsing config file line %d '%s'\n", lineno
, buffer
);
269 if (vendor
== NULL
) {
270 if (vendor_in
== NULL
) {
271 dbg(udev
, "matched global option\n");
274 } else if ((vendor_in
&& strncmp(vendor
, vendor_in
,
275 strlen(vendor_in
)) == 0) &&
276 (!model_in
|| (strncmp(model
, model_in
,
277 strlen(model_in
)) == 0))) {
279 * Matched vendor and optionally model.
281 * Note: a short vendor_in or model_in can
282 * give a partial match (that is FOO
285 dbg(udev
, "matched vendor/model\n");
288 dbg(udev
, "no match\n");
293 if (vendor_in
!= NULL
|| model_in
!= NULL
||
294 options_in
!= NULL
) {
296 * Something matched. Allocate newargv, and store
297 * values found in options_in.
299 strcpy(buffer
, options_in
);
300 c
= argc_count(buffer
) + 2;
301 *newargv
= calloc(c
, sizeof(**newargv
));
303 err(udev
, "can't allocate memory\n");
309 * argv[0] at 0 is skipped by getopt, but
310 * store the buffer address there for
313 (*newargv
)[c
] = buffer
;
314 for (c
= 1; c
< *argc
; c
++)
315 (*newargv
)[c
] = strsep(&buffer
, " \t");
328 static int set_options(struct udev
*udev
,
329 int argc
, char **argv
, const char *short_opts
,
335 * optind is a global extern used by getopt. Since we can call
336 * set_options twice (once for command line, and once for config
337 * file) we have to reset this back to 1.
341 option
= getopt_long(argc
, argv
, short_opts
, options
, NULL
);
346 dbg(udev
, "option '%c' arg '%s'\n", option
, optarg
);
348 dbg(udev
, "option '%c'\n", option
);
357 util_strlcpy(maj_min_dev
, optarg
, MAX_PATH_LEN
);
365 util_strlcpy(config_file
, optarg
, MAX_PATH_LEN
);
373 printf("Usage: scsi_id OPTIONS <device>\n"
374 " --device= device node for SG_IO commands\n"
375 " --config= location of config file\n"
376 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
377 " --sg-version=3|4 use SGv3 or SGv4\n"
378 " --blacklisted threat device as blacklisted\n"
379 " --whitelisted threat device as whitelisted\n"
380 " --replace-whitespace replace all whitespaces by underscores\n"
381 " --verbose verbose logging\n"
382 " --version print version\n"
383 " --export print values as environment keys\n"
384 " --help print this help text\n\n");
388 if (strcmp(optarg
, "0x80") == 0) {
389 default_page_code
= PAGE_80
;
390 } else if (strcmp(optarg
, "0x83") == 0) {
391 default_page_code
= PAGE_83
;
392 } else if (strcmp(optarg
, "pre-spc3-83") == 0) {
393 default_page_code
= PAGE_83_PRE_SPC3
;
395 err(udev
, "Unknown page code '%s'\n", optarg
);
401 sg_version
= atoi(optarg
);
402 if (sg_version
< 3 || sg_version
> 4) {
403 err(udev
, "Unknown SG version '%s'\n", optarg
);
421 printf("%s\n", VERSION
);
429 if (optind
< argc
&& !dev_specified
) {
431 util_strlcpy(maj_min_dev
, argv
[optind
], MAX_PATH_LEN
);
436 static int per_dev_options(struct udev
*udev
,
437 struct scsi_id_device
*dev_scsi
, int *good_bad
, int *page_code
)
441 char **newargv
= NULL
;
444 *good_bad
= all_good
;
445 *page_code
= default_page_code
;
447 retval
= get_file_options(udev
, vendor_str
, model_str
, &newargc
, &newargv
);
449 optind
= 1; /* reset this global extern */
450 while (retval
== 0) {
451 option
= getopt_long(newargc
, newargv
, dev_short_options
, options
, NULL
);
456 dbg(udev
, "option '%c' arg '%s'\n", option
, optarg
);
458 dbg(udev
, "option '%c'\n", option
);
470 if (strcmp(optarg
, "0x80") == 0) {
471 *page_code
= PAGE_80
;
472 } else if (strcmp(optarg
, "0x83") == 0) {
473 *page_code
= PAGE_83
;
474 } else if (strcmp(optarg
, "pre-spc3-83") == 0) {
475 *page_code
= PAGE_83_PRE_SPC3
;
477 err(udev
, "Unknown page code '%s'\n", optarg
);
483 err(udev
, "Unknown or bad option '%c' (0x%x)\n", option
, option
);
496 static int set_inq_values(struct udev
*udev
, struct scsi_id_device
*dev_scsi
, const char *path
)
500 dev_scsi
->use_sg
= sg_version
;
502 retval
= scsi_std_inquiry(udev
, dev_scsi
, path
);
506 udev_util_encode_string(dev_scsi
->vendor
, vendor_enc_str
, sizeof(vendor_enc_str
));
507 udev_util_encode_string(dev_scsi
->model
, model_enc_str
, sizeof(model_enc_str
));
509 udev_util_replace_whitespace(dev_scsi
->vendor
, vendor_str
, sizeof(vendor_str
));
510 udev_util_replace_chars(vendor_str
, NULL
);
511 udev_util_replace_whitespace(dev_scsi
->model
, model_str
, sizeof(model_str
));
512 udev_util_replace_chars(model_str
, NULL
);
513 set_type(dev_scsi
->type
, type_str
, sizeof(type_str
));
514 udev_util_replace_whitespace(dev_scsi
->revision
, revision_str
, sizeof(revision_str
));
515 udev_util_replace_chars(revision_str
, NULL
);
520 * format_serial: replace to whitespaces by underscores for calling
521 * programs that use the serial for device naming (multipath, Suse
524 static void format_serial(char *serial
)
526 char *p
= serial
, *q
;
531 if (q
> serial
&& q
[-1] != '_') {
545 * scsi_id: try to get an id, if one is found, printf it to stdout.
546 * returns a value passed to exit() - 0 if printed an id, else 1.
548 static int scsi_id(struct udev
*udev
, char *maj_min_dev
)
550 struct scsi_id_device dev_scsi
;
555 memset(&dev_scsi
, 0x00, sizeof(struct scsi_id_device
));
557 if (set_inq_values(udev
, &dev_scsi
, maj_min_dev
) < 0) {
562 /* get per device (vendor + model) options from the config file */
563 per_dev_options(udev
, &dev_scsi
, &good_dev
, &page_code
);
564 dbg(udev
, "per dev options: good %d; page code 0x%x\n", good_dev
, page_code
);
570 /* read serial number from mode pages (no values for optical drives) */
571 scsi_get_serial(udev
, &dev_scsi
, maj_min_dev
, page_code
, MAX_SERIAL_LEN
);
574 char serial_str
[MAX_SERIAL_LEN
];
576 printf("ID_VENDOR=%s\n", vendor_str
);
577 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str
);
578 printf("ID_MODEL=%s\n", model_str
);
579 printf("ID_MODEL_ENC=%s\n", model_enc_str
);
580 printf("ID_REVISION=%s\n", revision_str
);
581 printf("ID_TYPE=%s\n", type_str
);
582 if (dev_scsi
.serial
[0] != '\0') {
583 udev_util_replace_whitespace(dev_scsi
.serial
, serial_str
, sizeof(serial_str
));
584 udev_util_replace_chars(serial_str
, NULL
);
585 printf("ID_SERIAL=%s\n", serial_str
);
586 udev_util_replace_whitespace(dev_scsi
.serial_short
, serial_str
, sizeof(serial_str
));
587 udev_util_replace_chars(serial_str
, NULL
);
588 printf("ID_SERIAL_SHORT=%s\n", serial_str
);
593 if (dev_scsi
.serial
[0] == '\0') {
598 format_serial(dev_scsi
.serial
);
599 printf("%s\n", dev_scsi
.serial
);
604 int main(int argc
, char **argv
)
608 char maj_min_dev
[MAX_PATH_LEN
];
616 logging_init("scsi_id");
617 udev_set_log_fn(udev
, log_fn
);
620 * Get config file options.
623 retval
= get_file_options(udev
, NULL
, NULL
, &newargc
, &newargv
);
628 if (newargv
&& (retval
== 0)) {
629 if (set_options(udev
, newargc
, newargv
, short_options
, maj_min_dev
) < 0) {
637 * Get command line options (overriding any config file settings).
639 if (set_options(udev
, argc
, argv
, short_options
, maj_min_dev
) < 0)
642 if (!dev_specified
) {
643 err(udev
, "no device specified\n");
648 retval
= scsi_id(udev
, maj_min_dev
);