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 { "replace-whitespace", 0, NULL, 'u' },
41 { "verbose", 0, NULL, 'v' },
42 { "version", 0, NULL, 'V' },
43 { "export", 0, NULL, 'x' },
44 { "help", 0, 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] = SCSI_ID_CONFIG_FILE;
54 static enum page_code default_page_code;
55 static int use_stderr;
57 static int reformat_serial;
59 static char vendor_str[64];
60 static char model_str[64];
61 static char revision_str[16];
62 static char type_str[16];
65 void log_message(int priority, const char *format, ...)
68 static int udev_log = -1;
73 value = getenv("UDEV_LOG");
75 udev_log = log_priority(value);
80 if (priority > udev_log)
83 va_start(args, format);
84 vsyslog(priority, format, args);
89 static void set_str(char *to, const char *from, size_t count)
93 /* strip trailing whitespace */
94 len = strnlen(from, count);
95 while (len && isspace(from[len-1]))
98 /* strip leading whitespace */
100 while (isspace(from[i]) && (i < len))
105 /* substitute multiple whitespace */
106 if (isspace(from[i])) {
107 while (isspace(from[i]))
112 if (from[i] == '/') {
121 static void set_type(char *to, const char *from, size_t len)
125 char *type = "generic";
127 type_num = strtoul(from, &eptr, 0);
155 strncpy(to, type, len);
162 * buf points to an '=' followed by a quoted string ("foo") or a string ending
163 * with a space or ','.
165 * Return a pointer to the NUL terminated string, returns NULL if no
168 static char *get_value(char **buffer)
170 static char *quote_string = "\"\n";
171 static char *comma_string = ",\n";
175 if (**buffer == '"') {
177 * skip leading quote, terminate when quote seen
184 val = strsep(buffer, end);
185 if (val && end == quote_string)
187 * skip trailing quote
191 while (isspace(**buffer))
197 static int argc_count(char *opts)
200 while (*opts != '\0')
209 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
210 * if vendor and model are set find the first OPTIONS line in the config
211 * file that matches. Set argc and argv to match the OPTIONS string.
213 * vendor and model can end in '\n'.
215 static int get_file_options(const char *vendor, const char *model,
216 int *argc, char ***newargv)
222 char *vendor_in, *model_in, *options_in; /* read in from file */
227 dbg("vendor='%s'; model='%s'\n", vendor, model);
228 fd = fopen(config_file, "r");
230 dbg("can't open %s\n", config_file);
231 if (errno == ENOENT) {
234 err("can't open %s: %s\n", config_file, strerror(errno));
240 * Allocate a buffer rather than put it on the stack so we can
241 * keep it around to parse any options (any allocated newargv
242 * points into this buffer for its strings).
244 buffer = malloc(MAX_BUFFER_LEN);
246 err("can't allocate memory\n");
253 vendor_in = model_in = options_in = NULL;
255 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
259 if (buf[strlen(buffer) - 1] != '\n') {
260 info("Config file line %d too long\n", lineno);
264 while (isspace(*buf))
267 /* blank or all whitespace line */
275 dbg("lineno %d: '%s'\n", lineno, buf);
276 str1 = strsep(&buf, "=");
277 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
278 str1 = get_value(&buf);
285 str1 = strsep(&buf, "=");
286 if (str1 && strcasecmp(str1, "MODEL") == 0) {
287 str1 = get_value(&buf);
293 str1 = strsep(&buf, "=");
297 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
298 str1 = get_value(&buf);
305 dbg("config file line %d:\n"
306 " vendor '%s'; model '%s'; options '%s'\n",
307 lineno, vendor_in, model_in, options_in);
309 * Only allow: [vendor=foo[,model=bar]]options=stuff
311 if (!options_in || (!vendor_in && model_in)) {
312 info("Error parsing config file line %d '%s'\n", lineno, buffer);
316 if (vendor == NULL) {
317 if (vendor_in == NULL) {
318 dbg("matched global option\n");
321 } else if ((vendor_in && strncmp(vendor, vendor_in,
322 strlen(vendor_in)) == 0) &&
323 (!model_in || (strncmp(model, model_in,
324 strlen(model_in)) == 0))) {
326 * Matched vendor and optionally model.
328 * Note: a short vendor_in or model_in can
329 * give a partial match (that is FOO
332 dbg("matched vendor/model\n");
340 if (vendor_in != NULL || model_in != NULL ||
341 options_in != NULL) {
343 * Something matched. Allocate newargv, and store
344 * values found in options_in.
346 strcpy(buffer, options_in);
347 c = argc_count(buffer) + 2;
348 *newargv = calloc(c, sizeof(**newargv));
350 err("can't allocate memory\n");
356 * argv[0] at 0 is skipped by getopt, but
357 * store the buffer address there for
360 (*newargv)[c] = buffer;
361 for (c = 1; c < *argc; c++)
362 (*newargv)[c] = strsep(&buffer, " \t");
375 static int set_options(int argc, char **argv, const char *short_opts,
381 * optind is a global extern used by getopt. Since we can call
382 * set_options twice (once for command line, and once for config
383 * file) we have to reset this back to 1.
387 option = getopt_long(argc, argv, short_opts, options, NULL);
392 dbg("option '%c' arg '%s'\n", option, optarg);
394 dbg("option '%c'\n", option);
403 strncpy(maj_min_dev, optarg, MAX_PATH_LEN);
404 maj_min_dev[MAX_PATH_LEN-1] = '\0';
412 strncpy(config_file, optarg, MAX_PATH_LEN);
413 config_file[MAX_PATH_LEN-1] = '\0';
421 printf("Usage: scsi_id OPTIONS <device>\n"
422 " --device device node for SG_IO commands\n"
423 " --config location of config file\n"
424 " --page SCSI page (0x80, 0x83, pre-spc3-83)\n"
425 " --blacklisted threat device as blacklisted\n"
426 " --whitelisted threat device as whitelisted\n"
427 " --replace-whitespace replace all whitespaces by underscores\n"
428 " --verbose verbose logging\n"
429 " --version print version\n"
430 " --export print values as environment keys\n"
431 " --help print this help text\n\n");
435 if (strcmp(optarg, "0x80") == 0) {
436 default_page_code = PAGE_80;
437 } else if (strcmp(optarg, "0x83") == 0) {
438 default_page_code = PAGE_83;
439 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
440 default_page_code = PAGE_83_PRE_SPC3;
442 info("Unknown page code '%s'\n", optarg);
460 printf("%s\n", SCSI_ID_VERSION);
468 if (optind < argc && !dev_specified) {
470 strncpy(maj_min_dev, argv[optind], MAX_PATH_LEN);
471 maj_min_dev[MAX_PATH_LEN-1] = '\0';
476 static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
480 char **newargv = NULL;
483 *good_bad = all_good;
484 *page_code = default_page_code;
486 retval = get_file_options(vendor_str, model_str, &newargc, &newargv);
488 optind = 1; /* reset this global extern */
489 while (retval == 0) {
490 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
495 dbg("option '%c' arg '%s'\n", option, optarg);
497 dbg("option '%c'\n", option);
509 if (strcmp(optarg, "0x80") == 0) {
510 *page_code = PAGE_80;
511 } else if (strcmp(optarg, "0x83") == 0) {
512 *page_code = PAGE_83;
513 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
514 *page_code = PAGE_83_PRE_SPC3;
516 info("Unknown page code '%s'\n", optarg);
522 info("Unknown or bad option '%c' (0x%x)\n", option, option);
535 static int set_inq_values(struct scsi_id_device *dev_scsi, const char *path)
539 dev_scsi->use_sg = 4;
541 retval = scsi_std_inquiry(dev_scsi, path);
545 set_str(vendor_str, dev_scsi->vendor, 8);
546 set_str(model_str, dev_scsi->model, 16);
547 set_type(type_str, dev_scsi->type, sizeof(type_str) - 1);
548 set_str(revision_str, dev_scsi->revision, sizeof(revision_str) -1);
554 * format_serial: replace to whitespaces by underscores for calling
555 * programs that use the serial for device naming (multipath, Suse
558 static void format_serial(char *serial)
560 char *p = serial, *q;
565 if (q > serial && q[-1] != '_') {
579 * scsi_id: try to get an id, if one is found, printf it to stdout.
580 * returns a value passed to exit() - 0 if printed an id, else 1. This
581 * could be expanded, for example, if we want to report a failure like no
582 * memory etc. return 2, and return 1 for expected cases (like broken
583 * device found) that do not print an id.
585 static int scsi_id(char *maj_min_dev)
588 struct scsi_id_device dev_scsi;
591 char serial_short[MAX_SERIAL_LEN] = "";
593 set_inq_values(&dev_scsi, maj_min_dev);
595 /* get per device (vendor + model) options from the config file */
596 retval = per_dev_options(&dev_scsi, &good_dev, &page_code);
597 dbg("per dev options: good %d; page code 0x%x\n", good_dev, page_code);
601 } else if (scsi_get_serial(&dev_scsi, maj_min_dev, page_code,
602 serial_short, MAX_SERIAL_LEN)) {
609 char serial_str[MAX_SERIAL_LEN];
611 printf("ID_VENDOR=%s\n", vendor_str);
612 printf("ID_MODEL=%s\n", model_str);
613 printf("ID_REVISION=%s\n", revision_str);
614 set_str(serial_str, dev_scsi.serial, sizeof(serial_str));
615 printf("ID_SERIAL=%s\n", serial_str);
616 set_str(serial_str, serial_short, sizeof(serial_str));
617 printf("ID_SERIAL_SHORT=%s\n", serial_str);
618 printf("ID_TYPE=%s\n", type_str);
621 format_serial(dev_scsi.serial);
622 printf("%s\n", dev_scsi.serial);
624 dbg("%s\n", dev_scsi.serial);
631 int main(int argc, char **argv)
634 char maj_min_dev[MAX_PATH_LEN];
638 logging_init("scsi_id");
639 dbg("argc is %d\n", argc);
642 * Get config file options.
645 retval = get_file_options(NULL, NULL, &newargc, &newargv);
650 if (newargv && (retval == 0)) {
651 if (set_options(newargc, newargv, short_options, maj_min_dev) < 0) {
659 * Get command line options (overriding any config file settings).
661 if (set_options(argc, argv, short_options, maj_min_dev) < 0)
664 if (!dev_specified) {
665 info("no device specified\n");
670 retval = scsi_id(maj_min_dev);