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 { "sg-version", 1, NULL, 's' },
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 enum page_code default_page_code;
56 static int sg_version = 4;
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 err("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 err("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=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
427 " --sg-version=3|4 use SGv3 or SGv4\n"
428 " --blacklisted threat device as blacklisted\n"
429 " --whitelisted threat device as whitelisted\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");
438 if (strcmp(optarg, "0x80") == 0) {
439 default_page_code = PAGE_80;
440 } else if (strcmp(optarg, "0x83") == 0) {
441 default_page_code = PAGE_83;
442 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
443 default_page_code = PAGE_83_PRE_SPC3;
445 err("Unknown page code '%s'\n", optarg);
451 sg_version = atoi(optarg);
452 if (sg_version < 3 || sg_version > 4) {
453 err("Unknown SG version '%s'\n", optarg);
471 printf("%s\n", SCSI_ID_VERSION);
479 if (optind < argc && !dev_specified) {
481 strncpy(maj_min_dev, argv[optind], MAX_PATH_LEN);
482 maj_min_dev[MAX_PATH_LEN-1] = '\0';
487 static int per_dev_options(struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
491 char **newargv = NULL;
494 *good_bad = all_good;
495 *page_code = default_page_code;
497 retval = get_file_options(vendor_str, model_str, &newargc, &newargv);
499 optind = 1; /* reset this global extern */
500 while (retval == 0) {
501 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
506 dbg("option '%c' arg '%s'\n", option, optarg);
508 dbg("option '%c'\n", option);
520 if (strcmp(optarg, "0x80") == 0) {
521 *page_code = PAGE_80;
522 } else if (strcmp(optarg, "0x83") == 0) {
523 *page_code = PAGE_83;
524 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
525 *page_code = PAGE_83_PRE_SPC3;
527 err("Unknown page code '%s'\n", optarg);
533 err("Unknown or bad option '%c' (0x%x)\n", option, option);
546 static int set_inq_values(struct scsi_id_device *dev_scsi, const char *path)
550 dev_scsi->use_sg = sg_version;
552 retval = scsi_std_inquiry(dev_scsi, path);
556 set_str(vendor_str, dev_scsi->vendor, sizeof(vendor_str));
557 set_str(model_str, dev_scsi->model, sizeof(model_str));
558 set_type(type_str, dev_scsi->type, sizeof(type_str));
559 set_str(revision_str, dev_scsi->revision, sizeof(revision_str));
565 * format_serial: replace to whitespaces by underscores for calling
566 * programs that use the serial for device naming (multipath, Suse
569 static void format_serial(char *serial)
571 char *p = serial, *q;
576 if (q > serial && q[-1] != '_') {
590 * scsi_id: try to get an id, if one is found, printf it to stdout.
591 * returns a value passed to exit() - 0 if printed an id, else 1. This
592 * could be expanded, for example, if we want to report a failure like no
593 * memory etc. return 2, and return 1 for expected cases (like broken
594 * device found) that do not print an id.
596 static int scsi_id(char *maj_min_dev)
599 struct scsi_id_device dev_scsi;
602 char serial_short[MAX_SERIAL_LEN] = "";
604 set_inq_values(&dev_scsi, maj_min_dev);
606 /* get per device (vendor + model) options from the config file */
607 retval = per_dev_options(&dev_scsi, &good_dev, &page_code);
608 dbg("per dev options: good %d; page code 0x%x\n", good_dev, page_code);
612 } else if (scsi_get_serial(&dev_scsi, maj_min_dev, page_code,
613 serial_short, MAX_SERIAL_LEN)) {
620 char serial_str[MAX_SERIAL_LEN];
622 printf("ID_VENDOR=%s\n", vendor_str);
623 printf("ID_MODEL=%s\n", model_str);
624 printf("ID_REVISION=%s\n", revision_str);
625 set_str(serial_str, dev_scsi.serial, sizeof(serial_str));
626 printf("ID_SERIAL=%s\n", serial_str);
627 set_str(serial_str, serial_short, sizeof(serial_str));
628 printf("ID_SERIAL_SHORT=%s\n", serial_str);
629 printf("ID_TYPE=%s\n", type_str);
632 format_serial(dev_scsi.serial);
633 printf("%s\n", dev_scsi.serial);
635 dbg("%s\n", dev_scsi.serial);
642 int main(int argc, char **argv)
645 char maj_min_dev[MAX_PATH_LEN];
649 logging_init("scsi_id");
650 dbg("argc is %d\n", argc);
653 * Get config file options.
656 retval = get_file_options(NULL, NULL, &newargc, &newargv);
661 if (newargv && (retval == 0)) {
662 if (set_options(newargc, newargv, short_options, maj_min_dev) < 0) {
670 * Get command line options (overriding any config file settings).
672 if (set_options(argc, argv, short_options, maj_min_dev) < 0)
675 if (!dev_specified) {
676 err("no device specified\n");
681 retval = scsi_id(maj_min_dev);