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];
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_str(char *to, const char *from, size_t count)
78 /* strip trailing whitespace */
79 len = strnlen(from, count);
80 while (len && isspace(from[len-1]))
83 /* strip leading whitespace */
85 while (isspace(from[i]) && (i < len))
90 /* substitute multiple whitespace */
91 if (isspace(from[i])) {
92 while (isspace(from[i]))
106 static void set_type(char *to, const char *from, size_t len)
110 char *type = "generic";
112 type_num = strtoul(from, &eptr, 0);
140 strncpy(to, type, len);
147 * buf points to an '=' followed by a quoted string ("foo") or a string ending
148 * with a space or ','.
150 * Return a pointer to the NUL terminated string, returns NULL if no
153 static char *get_value(char **buffer)
155 static char *quote_string = "\"\n";
156 static char *comma_string = ",\n";
160 if (**buffer == '"') {
162 * skip leading quote, terminate when quote seen
169 val = strsep(buffer, end);
170 if (val && end == quote_string)
172 * skip trailing quote
176 while (isspace(**buffer))
182 static int argc_count(char *opts)
185 while (*opts != '\0')
194 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
195 * if vendor and model are set find the first OPTIONS line in the config
196 * file that matches. Set argc and argv to match the OPTIONS string.
198 * vendor and model can end in '\n'.
200 static int get_file_options(struct udev *udev,
201 const char *vendor, const char *model,
202 int *argc, char ***newargv)
208 char *vendor_in, *model_in, *options_in; /* read in from file */
213 dbg(udev, "vendor='%s'; model='%s'\n", vendor, model);
214 fd = fopen(config_file, "r");
216 dbg(udev, "can't open %s\n", config_file);
217 if (errno == ENOENT) {
220 err(udev, "can't open %s: %s\n", config_file, strerror(errno));
226 * Allocate a buffer rather than put it on the stack so we can
227 * keep it around to parse any options (any allocated newargv
228 * points into this buffer for its strings).
230 buffer = malloc(MAX_BUFFER_LEN);
232 err(udev, "can't allocate memory\n");
239 vendor_in = model_in = options_in = NULL;
241 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
245 if (buf[strlen(buffer) - 1] != '\n') {
246 err(udev, "Config file line %d too long\n", lineno);
250 while (isspace(*buf))
253 /* blank or all whitespace line */
261 dbg("lineno %d: '%s'\n", lineno, buf);
262 str1 = strsep(&buf, "=");
263 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
264 str1 = get_value(&buf);
271 str1 = strsep(&buf, "=");
272 if (str1 && strcasecmp(str1, "MODEL") == 0) {
273 str1 = get_value(&buf);
279 str1 = strsep(&buf, "=");
283 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
284 str1 = get_value(&buf);
291 dbg("config file line %d:\n"
292 " vendor '%s'; model '%s'; options '%s'\n",
293 lineno, vendor_in, model_in, options_in);
295 * Only allow: [vendor=foo[,model=bar]]options=stuff
297 if (!options_in || (!vendor_in && model_in)) {
298 err(udev, "Error parsing config file line %d '%s'\n", lineno, buffer);
302 if (vendor == NULL) {
303 if (vendor_in == NULL) {
304 dbg("matched global option\n");
307 } else if ((vendor_in && strncmp(vendor, vendor_in,
308 strlen(vendor_in)) == 0) &&
309 (!model_in || (strncmp(model, model_in,
310 strlen(model_in)) == 0))) {
312 * Matched vendor and optionally model.
314 * Note: a short vendor_in or model_in can
315 * give a partial match (that is FOO
318 dbg("matched vendor/model\n");
326 if (vendor_in != NULL || model_in != NULL ||
327 options_in != NULL) {
329 * Something matched. Allocate newargv, and store
330 * values found in options_in.
332 strcpy(buffer, options_in);
333 c = argc_count(buffer) + 2;
334 *newargv = calloc(c, sizeof(**newargv));
336 err(udev, "can't allocate memory\n");
342 * argv[0] at 0 is skipped by getopt, but
343 * store the buffer address there for
346 (*newargv)[c] = buffer;
347 for (c = 1; c < *argc; c++)
348 (*newargv)[c] = strsep(&buffer, " \t");
361 static int set_options(struct udev *udev,
362 int argc, char **argv, const char *short_opts,
368 * optind is a global extern used by getopt. Since we can call
369 * set_options twice (once for command line, and once for config
370 * file) we have to reset this back to 1.
374 option = getopt_long(argc, argv, short_opts, options, NULL);
379 dbg(udev, "option '%c' arg '%s'\n", option, optarg);
381 dbg(udev, "option '%c'\n", option);
390 strncpy(maj_min_dev, optarg, MAX_PATH_LEN);
391 maj_min_dev[MAX_PATH_LEN-1] = '\0';
399 strncpy(config_file, optarg, MAX_PATH_LEN);
400 config_file[MAX_PATH_LEN-1] = '\0';
408 printf("Usage: scsi_id OPTIONS <device>\n"
409 " --device= device node for SG_IO commands\n"
410 " --config= location of config file\n"
411 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
412 " --sg-version=3|4 use SGv3 or SGv4\n"
413 " --blacklisted threat device as blacklisted\n"
414 " --whitelisted threat device as whitelisted\n"
415 " --replace-whitespace replace all whitespaces by underscores\n"
416 " --verbose verbose logging\n"
417 " --version print version\n"
418 " --export print values as environment keys\n"
419 " --help print this help text\n\n");
423 if (strcmp(optarg, "0x80") == 0) {
424 default_page_code = PAGE_80;
425 } else if (strcmp(optarg, "0x83") == 0) {
426 default_page_code = PAGE_83;
427 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
428 default_page_code = PAGE_83_PRE_SPC3;
430 err(udev, "Unknown page code '%s'\n", optarg);
436 sg_version = atoi(optarg);
437 if (sg_version < 3 || sg_version > 4) {
438 err(udev, "Unknown SG version '%s'\n", optarg);
456 printf("%s\n", VERSION);
464 if (optind < argc && !dev_specified) {
466 strncpy(maj_min_dev, argv[optind], MAX_PATH_LEN);
467 maj_min_dev[MAX_PATH_LEN-1] = '\0';
472 static int per_dev_options(struct udev *udev,
473 struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
477 char **newargv = NULL;
480 *good_bad = all_good;
481 *page_code = default_page_code;
483 retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
485 optind = 1; /* reset this global extern */
486 while (retval == 0) {
487 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
492 dbg(udev, "option '%c' arg '%s'\n", option, optarg);
494 dbg(udev, "option '%c'\n", option);
506 if (strcmp(optarg, "0x80") == 0) {
507 *page_code = PAGE_80;
508 } else if (strcmp(optarg, "0x83") == 0) {
509 *page_code = PAGE_83;
510 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
511 *page_code = PAGE_83_PRE_SPC3;
513 err(udev, "Unknown page code '%s'\n", optarg);
519 err(udev, "Unknown or bad option '%c' (0x%x)\n", option, option);
532 static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
536 dev_scsi->use_sg = sg_version;
538 retval = scsi_std_inquiry(udev, dev_scsi, path);
542 set_str(vendor_str, dev_scsi->vendor, sizeof(vendor_str));
543 set_str(model_str, dev_scsi->model, sizeof(model_str));
544 set_type(type_str, dev_scsi->type, sizeof(type_str));
545 set_str(revision_str, dev_scsi->revision, sizeof(revision_str));
551 * format_serial: replace to whitespaces by underscores for calling
552 * programs that use the serial for device naming (multipath, Suse
555 static void format_serial(char *serial)
557 char *p = serial, *q;
562 if (q > serial && q[-1] != '_') {
576 * scsi_id: try to get an id, if one is found, printf it to stdout.
577 * returns a value passed to exit() - 0 if printed an id, else 1. This
578 * could be expanded, for example, if we want to report a failure like no
579 * memory etc. return 2, and return 1 for expected cases (like broken
580 * device found) that do not print an id.
582 static int scsi_id(struct udev *udev, char *maj_min_dev)
585 struct scsi_id_device dev_scsi;
588 char serial_short[MAX_SERIAL_LEN] = "";
590 set_inq_values(udev, &dev_scsi, maj_min_dev);
592 /* get per device (vendor + model) options from the config file */
593 retval = per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
594 dbg("per dev options: good %d; page code 0x%x\n", good_dev, page_code);
598 } else if (scsi_get_serial(udev,
599 &dev_scsi, maj_min_dev, page_code,
600 serial_short, MAX_SERIAL_LEN)) {
607 char serial_str[MAX_SERIAL_LEN];
609 printf("ID_VENDOR=%s\n", vendor_str);
610 printf("ID_MODEL=%s\n", model_str);
611 printf("ID_REVISION=%s\n", revision_str);
612 set_str(serial_str, dev_scsi.serial, sizeof(serial_str));
613 printf("ID_SERIAL=%s\n", serial_str);
614 set_str(serial_str, serial_short, sizeof(serial_str));
615 printf("ID_SERIAL_SHORT=%s\n", serial_str);
616 printf("ID_TYPE=%s\n", type_str);
619 format_serial(dev_scsi.serial);
620 printf("%s\n", dev_scsi.serial);
622 dbg("%s\n", dev_scsi.serial);
629 int main(int argc, char **argv)
633 char maj_min_dev[MAX_PATH_LEN];
641 logging_init("scsi_id");
642 udev_set_log_fn(udev, log_fn);
645 * Get config file options.
648 retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
653 if (newargv && (retval == 0)) {
654 if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
662 * Get command line options (overriding any config file settings).
664 if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
667 if (!dev_specified) {
668 err(udev, "no device specified\n");
673 retval = scsi_id(udev, maj_min_dev);