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.
32 #include "../../udev/udev.h"
35 static const struct option options[] = {
36 { "device", 1, NULL, 'd' },
37 { "config", 1, NULL, 'f' },
38 { "page", 1, NULL, 'p' },
39 { "blacklisted", 0, NULL, 'b' },
40 { "whitelisted", 0, NULL, 'g' },
41 { "replace-whitespace", 0, NULL, 'u' },
42 { "sg-version", 1, NULL, 's' },
43 { "verbose", 0, NULL, 'v' },
44 { "version", 0, NULL, 'V' },
45 { "export", 0, NULL, 'x' },
46 { "help", 0, NULL, 'h' },
50 static const char short_options[] = "d:f:ghip:uvVx";
51 static const char dev_short_options[] = "bgp:";
54 static int dev_specified;
55 static char config_file[MAX_PATH_LEN] = SYSCONFDIR "/scsi_id.config";
56 static enum page_code default_page_code;
57 static int sg_version = 4;
58 static int use_stderr;
60 static int reformat_serial;
62 static char vendor_str[64];
63 static char model_str[64];
64 static char revision_str[16];
65 static char type_str[16];
68 void log_message(int priority, const char *format, ...)
71 static int udev_log = -1;
76 value = getenv("UDEV_LOG");
78 udev_log = log_priority(value);
83 if (priority > udev_log)
86 va_start(args, format);
87 vsyslog(priority, format, args);
92 static void set_str(char *to, const char *from, size_t count)
96 /* strip trailing whitespace */
97 len = strnlen(from, count);
98 while (len && isspace(from[len-1]))
101 /* strip leading whitespace */
103 while (isspace(from[i]) && (i < len))
108 /* substitute multiple whitespace */
109 if (isspace(from[i])) {
110 while (isspace(from[i]))
115 if (from[i] == '/') {
124 static void set_type(char *to, const char *from, size_t len)
128 char *type = "generic";
130 type_num = strtoul(from, &eptr, 0);
158 strncpy(to, type, len);
165 * buf points to an '=' followed by a quoted string ("foo") or a string ending
166 * with a space or ','.
168 * Return a pointer to the NUL terminated string, returns NULL if no
171 static char *get_value(char **buffer)
173 static char *quote_string = "\"\n";
174 static char *comma_string = ",\n";
178 if (**buffer == '"') {
180 * skip leading quote, terminate when quote seen
187 val = strsep(buffer, end);
188 if (val && end == quote_string)
190 * skip trailing quote
194 while (isspace(**buffer))
200 static int argc_count(char *opts)
203 while (*opts != '\0')
212 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
213 * if vendor and model are set find the first OPTIONS line in the config
214 * file that matches. Set argc and argv to match the OPTIONS string.
216 * vendor and model can end in '\n'.
218 static int get_file_options(const char *vendor, const char *model,
219 int *argc, char ***newargv)
225 char *vendor_in, *model_in, *options_in; /* read in from file */
230 dbg("vendor='%s'; model='%s'\n", vendor, model);
231 fd = fopen(config_file, "r");
233 dbg("can't open %s\n", config_file);
234 if (errno == ENOENT) {
237 err("can't open %s: %s\n", config_file, strerror(errno));
243 * Allocate a buffer rather than put it on the stack so we can
244 * keep it around to parse any options (any allocated newargv
245 * points into this buffer for its strings).
247 buffer = malloc(MAX_BUFFER_LEN);
249 err("can't allocate memory\n");
256 vendor_in = model_in = options_in = NULL;
258 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
262 if (buf[strlen(buffer) - 1] != '\n') {
263 err("Config file line %d too long\n", lineno);
267 while (isspace(*buf))
270 /* blank or all whitespace line */
278 dbg("lineno %d: '%s'\n", lineno, buf);
279 str1 = strsep(&buf, "=");
280 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
281 str1 = get_value(&buf);
288 str1 = strsep(&buf, "=");
289 if (str1 && strcasecmp(str1, "MODEL") == 0) {
290 str1 = get_value(&buf);
296 str1 = strsep(&buf, "=");
300 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
301 str1 = get_value(&buf);
308 dbg("config file line %d:\n"
309 " vendor '%s'; model '%s'; options '%s'\n",
310 lineno, vendor_in, model_in, options_in);
312 * Only allow: [vendor=foo[,model=bar]]options=stuff
314 if (!options_in || (!vendor_in && model_in)) {
315 err("Error parsing config file line %d '%s'\n", lineno, buffer);
319 if (vendor == NULL) {
320 if (vendor_in == NULL) {
321 dbg("matched global option\n");
324 } else if ((vendor_in && strncmp(vendor, vendor_in,
325 strlen(vendor_in)) == 0) &&
326 (!model_in || (strncmp(model, model_in,
327 strlen(model_in)) == 0))) {
329 * Matched vendor and optionally model.
331 * Note: a short vendor_in or model_in can
332 * give a partial match (that is FOO
335 dbg("matched vendor/model\n");
343 if (vendor_in != NULL || model_in != NULL ||
344 options_in != NULL) {
346 * Something matched. Allocate newargv, and store
347 * values found in options_in.
349 strcpy(buffer, options_in);
350 c = argc_count(buffer) + 2;
351 *newargv = calloc(c, sizeof(**newargv));
353 err("can't allocate memory\n");
359 * argv[0] at 0 is skipped by getopt, but
360 * store the buffer address there for
363 (*newargv)[c] = buffer;
364 for (c = 1; c < *argc; c++)
365 (*newargv)[c] = strsep(&buffer, " \t");
378 static int set_options(int argc, char **argv, const char *short_opts,
384 * optind is a global extern used by getopt. Since we can call
385 * set_options twice (once for command line, and once for config
386 * file) we have to reset this back to 1.
390 option = getopt_long(argc, argv, short_opts, options, NULL);
395 dbg("option '%c' arg '%s'\n", option, optarg);
397 dbg("option '%c'\n", option);
406 strncpy(maj_min_dev, optarg, MAX_PATH_LEN);
407 maj_min_dev[MAX_PATH_LEN-1] = '\0';
415 strncpy(config_file, optarg, MAX_PATH_LEN);
416 config_file[MAX_PATH_LEN-1] = '\0';
424 printf("Usage: scsi_id OPTIONS <device>\n"
425 " --device= device node for SG_IO commands\n"
426 " --config= location of config file\n"
427 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
428 " --sg-version=3|4 use SGv3 or SGv4\n"
429 " --blacklisted threat device as blacklisted\n"
430 " --whitelisted threat device as whitelisted\n"
431 " --replace-whitespace replace all whitespaces by underscores\n"
432 " --verbose verbose logging\n"
433 " --version print version\n"
434 " --export print values as environment keys\n"
435 " --help print this help text\n\n");
439 if (strcmp(optarg, "0x80") == 0) {
440 default_page_code = PAGE_80;
441 } else if (strcmp(optarg, "0x83") == 0) {
442 default_page_code = PAGE_83;
443 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
444 default_page_code = PAGE_83_PRE_SPC3;
446 err("Unknown page code '%s'\n", optarg);
452 sg_version = atoi(optarg);
453 if (sg_version < 3 || sg_version > 4) {
454 err("Unknown SG version '%s'\n", optarg);
472 printf("%s\n", VERSION);
480 if (optind < argc && !dev_specified) {
482 strncpy(maj_min_dev, argv[optind], MAX_PATH_LEN);
483 maj_min_dev[MAX_PATH_LEN-1] = '\0';
488 static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
492 char **newargv = NULL;
495 *good_bad = all_good;
496 *page_code = default_page_code;
498 retval = get_file_options(vendor_str, model_str, &newargc, &newargv);
500 optind = 1; /* reset this global extern */
501 while (retval == 0) {
502 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
507 dbg("option '%c' arg '%s'\n", option, optarg);
509 dbg("option '%c'\n", option);
521 if (strcmp(optarg, "0x80") == 0) {
522 *page_code = PAGE_80;
523 } else if (strcmp(optarg, "0x83") == 0) {
524 *page_code = PAGE_83;
525 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
526 *page_code = PAGE_83_PRE_SPC3;
528 err("Unknown page code '%s'\n", optarg);
534 err("Unknown or bad option '%c' (0x%x)\n", option, option);
547 static int set_inq_values(struct scsi_id_device *dev_scsi, const char *path)
551 dev_scsi->use_sg = sg_version;
553 retval = scsi_std_inquiry(dev_scsi, path);
557 set_str(vendor_str, dev_scsi->vendor, sizeof(vendor_str));
558 set_str(model_str, dev_scsi->model, sizeof(model_str));
559 set_type(type_str, dev_scsi->type, sizeof(type_str));
560 set_str(revision_str, dev_scsi->revision, sizeof(revision_str));
566 * format_serial: replace to whitespaces by underscores for calling
567 * programs that use the serial for device naming (multipath, Suse
570 static void format_serial(char *serial)
572 char *p = serial, *q;
577 if (q > serial && q[-1] != '_') {
591 * scsi_id: try to get an id, if one is found, printf it to stdout.
592 * returns a value passed to exit() - 0 if printed an id, else 1. This
593 * could be expanded, for example, if we want to report a failure like no
594 * memory etc. return 2, and return 1 for expected cases (like broken
595 * device found) that do not print an id.
597 static int scsi_id(char *maj_min_dev)
600 struct scsi_id_device dev_scsi;
603 char serial_short[MAX_SERIAL_LEN] = "";
605 set_inq_values(&dev_scsi, maj_min_dev);
607 /* get per device (vendor + model) options from the config file */
608 retval = per_dev_options(&dev_scsi, &good_dev, &page_code);
609 dbg("per dev options: good %d; page code 0x%x\n", good_dev, page_code);
613 } else if (scsi_get_serial(&dev_scsi, maj_min_dev, page_code,
614 serial_short, MAX_SERIAL_LEN)) {
621 char serial_str[MAX_SERIAL_LEN];
623 printf("ID_VENDOR=%s\n", vendor_str);
624 printf("ID_MODEL=%s\n", model_str);
625 printf("ID_REVISION=%s\n", revision_str);
626 set_str(serial_str, dev_scsi.serial, sizeof(serial_str));
627 printf("ID_SERIAL=%s\n", serial_str);
628 set_str(serial_str, serial_short, sizeof(serial_str));
629 printf("ID_SERIAL_SHORT=%s\n", serial_str);
630 printf("ID_TYPE=%s\n", type_str);
633 format_serial(dev_scsi.serial);
634 printf("%s\n", dev_scsi.serial);
636 dbg("%s\n", dev_scsi.serial);
643 int main(int argc, char **argv)
646 char maj_min_dev[MAX_PATH_LEN];
650 logging_init("scsi_id");
651 dbg("argc is %d\n", argc);
654 * Get config file options.
657 retval = get_file_options(NULL, NULL, &newargc, &newargv);
662 if (newargv && (retval == 0)) {
663 if (set_options(newargc, newargv, short_options, maj_min_dev) < 0) {
671 * Get command line options (overriding any config file settings).
673 if (set_options(argc, argv, short_options, maj_min_dev) < 0)
676 if (!dev_specified) {
677 err("no device specified\n");
682 retval = scsi_id(maj_min_dev);