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.h"
32 #include "scsi_id_version.h"
34 static const struct option options[] = {
35 { "device", 1, NULL, 'd' },
36 { "config", 1, NULL, 'f' },
37 { "page", 1, NULL, 'p' },
38 { "blacklisted", 0, NULL, 'b' },
39 { "whitelisted", 0, NULL, 'g' },
40 { "prefix-bus-id", 0, NULL, 'i' },
41 { "replace-whitespace", 0, NULL, 'u' },
42 { "verbose", 0, NULL, 'v' },
43 { "version", 0, NULL, 'V' },
44 { "export", 0, NULL, 'x' },
45 { "help", 0, NULL, 'h' },
49 static const char short_options[] = "d:f:ghip:uvVx";
50 static const char dev_short_options[] = "bgp:";
53 static int dev_specified;
54 static char config_file[MAX_PATH_LEN] = SCSI_ID_CONFIG_FILE;
55 static int display_bus_id;
56 static enum page_code default_page_code;
57 static int use_stderr;
59 static int reformat_serial;
61 static char vendor_str[64];
62 static char model_str[64];
63 static char revision_str[16];
64 static char type_str[16];
67 void log_message(int priority, const char *format, ...)
70 static int udev_log = -1;
75 value = getenv("UDEV_LOG");
77 udev_log = log_priority(value);
82 if (priority > udev_log)
85 va_start(args, format);
86 vsyslog(priority, format, args);
91 static void set_str(char *to, const char *from, size_t count)
95 /* strip trailing whitespace */
96 len = strnlen(from, count);
97 while (len && isspace(from[len-1]))
100 /* strip leading whitespace */
102 while (isspace(from[i]) && (i < len))
107 /* substitute multiple whitespace */
108 if (isspace(from[i])) {
109 while (isspace(from[i]))
114 if (from[i] == '/') {
123 static void set_type(char *to, const char *from, size_t len)
127 char *type = "generic";
129 type_num = strtoul(from, &eptr, 0);
157 strncpy(to, type, len);
164 * buf points to an '=' followed by a quoted string ("foo") or a string ending
165 * with a space or ','.
167 * Return a pointer to the NUL terminated string, returns NULL if no
170 static char *get_value(char **buffer)
172 static char *quote_string = "\"\n";
173 static char *comma_string = ",\n";
177 if (**buffer == '"') {
179 * skip leading quote, terminate when quote seen
186 val = strsep(buffer, end);
187 if (val && end == quote_string)
189 * skip trailing quote
193 while (isspace(**buffer))
199 static int argc_count(char *opts)
202 while (*opts != '\0')
211 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
212 * if vendor and model are set find the first OPTIONS line in the config
213 * file that matches. Set argc and argv to match the OPTIONS string.
215 * vendor and model can end in '\n'.
217 static int get_file_options(const char *vendor, const char *model,
218 int *argc, char ***newargv)
224 char *vendor_in, *model_in, *options_in; /* read in from file */
229 dbg("vendor='%s'; model='%s'\n", vendor, model);
230 fd = fopen(config_file, "r");
232 dbg("can't open %s\n", config_file);
233 if (errno == ENOENT) {
236 err("can't open %s: %s\n", config_file, strerror(errno));
242 * Allocate a buffer rather than put it on the stack so we can
243 * keep it around to parse any options (any allocated newargv
244 * points into this buffer for its strings).
246 buffer = malloc(MAX_BUFFER_LEN);
248 err("can't allocate memory\n");
255 vendor_in = model_in = options_in = NULL;
257 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
261 if (buf[strlen(buffer) - 1] != '\n') {
262 info("Config file line %d too long\n", lineno);
266 while (isspace(*buf))
269 /* blank or all whitespace line */
277 dbg("lineno %d: '%s'\n", lineno, buf);
278 str1 = strsep(&buf, "=");
279 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
280 str1 = get_value(&buf);
287 str1 = strsep(&buf, "=");
288 if (str1 && strcasecmp(str1, "MODEL") == 0) {
289 str1 = get_value(&buf);
295 str1 = strsep(&buf, "=");
299 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
300 str1 = get_value(&buf);
307 dbg("config file line %d:\n"
308 " vendor '%s'; model '%s'; options '%s'\n",
309 lineno, vendor_in, model_in, options_in);
311 * Only allow: [vendor=foo[,model=bar]]options=stuff
313 if (!options_in || (!vendor_in && model_in)) {
314 info("Error parsing config file line %d '%s'\n", lineno, buffer);
318 if (vendor == NULL) {
319 if (vendor_in == NULL) {
320 dbg("matched global option\n");
323 } else if ((vendor_in && strncmp(vendor, vendor_in,
324 strlen(vendor_in)) == 0) &&
325 (!model_in || (strncmp(model, model_in,
326 strlen(model_in)) == 0))) {
328 * Matched vendor and optionally model.
330 * Note: a short vendor_in or model_in can
331 * give a partial match (that is FOO
334 dbg("matched vendor/model\n");
342 if (vendor_in != NULL || model_in != NULL ||
343 options_in != NULL) {
345 * Something matched. Allocate newargv, and store
346 * values found in options_in.
348 strcpy(buffer, options_in);
349 c = argc_count(buffer) + 2;
350 *newargv = calloc(c, sizeof(**newargv));
352 err("can't allocate memory\n");
358 * argv[0] at 0 is skipped by getopt, but
359 * store the buffer address there for
362 (*newargv)[c] = buffer;
363 for (c = 1; c < *argc; c++)
364 (*newargv)[c] = strsep(&buffer, " \t");
377 static int set_options(int argc, char **argv, const char *short_opts,
383 * optind is a global extern used by getopt. Since we can call
384 * set_options twice (once for command line, and once for config
385 * file) we have to reset this back to 1.
389 option = getopt_long(argc, argv, short_opts, options, NULL);
394 dbg("option '%c' arg '%s'\n", option, optarg);
396 dbg("option '%c'\n", option);
405 strncpy(maj_min_dev, optarg, MAX_PATH_LEN);
406 maj_min_dev[MAX_PATH_LEN-1] = '\0';
414 strncpy(config_file, optarg, MAX_PATH_LEN);
415 config_file[MAX_PATH_LEN-1] = '\0';
423 printf("Usage: scsi_id OPTIONS <device>\n"
424 " --device device node for SG_IO commands\n"
425 " --config location of config file\n"
426 " --page SCSI page (0x80, 0x83, pre-spc3-83)\n"
427 " --blacklisted threat device as blacklisted\n"
428 " --whitelisted threat device as whitelisted\n"
429 " --prefix-bus-id prefix SCSI bus id\n"
430 " --replace-whitespace replace all whitespaces by underscores\n"
431 " --verbose verbose logging\n"
432 " --version print version\n"
433 " --export print values as environment keys\n"
434 " --help print this help text\n\n");
442 if (strcmp(optarg, "0x80") == 0) {
443 default_page_code = PAGE_80;
444 } else if (strcmp(optarg, "0x83") == 0) {
445 default_page_code = PAGE_83;
446 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
447 default_page_code = PAGE_83_PRE_SPC3;
449 info("Unknown page code '%s'\n", optarg);
467 printf("%s\n", SCSI_ID_VERSION);
475 if (optind < argc && !dev_specified) {
477 strncpy(maj_min_dev, argv[optind], MAX_PATH_LEN);
478 maj_min_dev[MAX_PATH_LEN-1] = '\0';
483 static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
487 char **newargv = NULL;
490 *good_bad = all_good;
491 *page_code = default_page_code;
493 retval = get_file_options(vendor_str, model_str, &newargc, &newargv);
495 optind = 1; /* reset this global extern */
496 while (retval == 0) {
497 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
502 dbg("option '%c' arg '%s'\n", option, optarg);
504 dbg("option '%c'\n", option);
516 if (strcmp(optarg, "0x80") == 0) {
517 *page_code = PAGE_80;
518 } else if (strcmp(optarg, "0x83") == 0) {
519 *page_code = PAGE_83;
520 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
521 *page_code = PAGE_83_PRE_SPC3;
523 info("Unknown page code '%s'\n", optarg);
529 info("Unknown or bad option '%c' (0x%x)\n", option, option);
542 static int set_inq_values(struct scsi_id_device *dev_scsi, const char *path)
546 retval = scsi_std_inquiry(dev_scsi, path);
550 set_str(vendor_str, dev_scsi->vendor, 8);
551 set_str(model_str, dev_scsi->model, 16);
552 set_type(type_str, dev_scsi->type, sizeof(type_str) - 1);
553 set_str(revision_str, dev_scsi->revision, sizeof(revision_str) -1);
559 * format_serial: replace to whitespaces by underscores for calling
560 * programs that use the serial for device naming (multipath, Suse
563 static void format_serial(char *serial)
565 char *p = serial, *q;
570 if (q > serial && q[-1] != '_') {
584 * scsi_id: try to get an id, if one is found, printf it to stdout.
585 * returns a value passed to exit() - 0 if printed an id, else 1. This
586 * could be expanded, for example, if we want to report a failure like no
587 * memory etc. return 2, and return 1 for expected cases (like broken
588 * device found) that do not print an id.
590 static int scsi_id(char *maj_min_dev)
593 struct scsi_id_device dev_scsi;
596 char serial_short[MAX_SERIAL_LEN] = "";
598 set_inq_values(&dev_scsi, maj_min_dev);
600 /* get per device (vendor + model) options from the config file */
601 retval = per_dev_options(&dev_scsi, &good_dev, &page_code);
602 dbg("per dev options: good %d; page code 0x%x\n", good_dev, page_code);
606 } else if (scsi_get_serial(&dev_scsi, maj_min_dev, page_code,
607 serial_short, MAX_SERIAL_LEN)) {
614 char serial_str[MAX_SERIAL_LEN];
616 printf("ID_VENDOR=%s\n", vendor_str);
617 printf("ID_MODEL=%s\n", model_str);
618 printf("ID_REVISION=%s\n", revision_str);
619 set_str(serial_str, dev_scsi.serial, sizeof(serial_str));
620 printf("ID_SERIAL=%s\n", serial_str);
621 set_str(serial_str, serial_short, sizeof(serial_str));
622 printf("ID_SERIAL_SHORT=%s\n", serial_str);
623 printf("ID_TYPE=%s\n", type_str);
626 format_serial(dev_scsi.serial);
627 printf("%s\n", dev_scsi.serial);
629 dbg("%s\n", dev_scsi.serial);
636 int main(int argc, char **argv)
639 char maj_min_dev[MAX_PATH_LEN];
643 logging_init("scsi_id");
644 dbg("argc is %d\n", argc);
647 * Get config file options.
650 retval = get_file_options(NULL, NULL, &newargc, &newargv);
655 if (newargv && (retval == 0)) {
656 if (set_options(newargc, newargv, short_options, maj_min_dev) < 0) {
664 * Get command line options (overriding any config file settings).
666 if (set_options(argc, argv, short_options, maj_min_dev) < 0)
669 if (!dev_specified) {
670 info("no device specified\n");
675 retval = scsi_id(maj_min_dev);