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 /* temporary names for mknod */
35 #define TMP_DIR "/dev"
36 #define TMP_PREFIX "tmp-scsi"
38 static const struct option options[] = {
39 { "device", 1, NULL, 'd' },
40 { "config", 1, NULL, 'f' },
41 { "page", 1, NULL, 'p' },
42 { "devpath", 1, NULL, 's' },
43 { "fallback-to-sysfs", 0, NULL, 'a' },
44 { "blacklisted", 0, NULL, 'b' },
45 { "whitelisted", 0, NULL, 'g' },
46 { "prefix-bus-id", 0, NULL, 'i' },
47 { "replace-whitespace", 0, NULL, 'u' },
48 { "ignore-sysfs", 0, NULL, 'n' },
49 { "verbose", 0, NULL, 'v' },
50 { "version", 0, NULL, 'V' },
51 { "export", 0, NULL, 'x' },
52 { "help", 0, NULL, 'h' },
56 static const char short_options[] = "abd:f:ghinp:s:uvVx";
57 static const char dev_short_options[] = "bgp:";
60 static int always_info;
61 static int dev_specified;
62 static int sys_specified;
63 static char config_file[MAX_PATH_LEN] = SCSI_ID_CONFIG_FILE;
64 static int display_bus_id;
65 static enum page_code default_page_code;
66 static int use_stderr;
68 static int hotplug_mode;
69 static int reformat_serial;
70 static int ignore_sysfs;
72 static char vendor_str[64];
73 static char model_str[64];
74 static char revision_str[16];
75 static char type_str[16];
78 void log_message(int priority, const char *format, ...)
81 static int udev_log = -1;
86 value = getenv("UDEV_LOG");
88 udev_log = log_priority(value);
93 if (priority > udev_log)
96 va_start(args, format);
97 vsyslog(priority, format, args);
102 static void set_str(char *to, const char *from, size_t count)
106 /* strip trailing whitespace */
107 len = strnlen(from, count);
108 while (len && isspace(from[len-1]))
111 /* strip leading whitespace */
113 while (isspace(from[i]) && (i < len))
118 /* substitute multiple whitespace */
119 if (isspace(from[i])) {
120 while (isspace(from[i]))
125 if (from[i] == '/') {
134 static void set_type(char *to, const char *from, size_t len)
138 char *type = "generic";
140 type_num = strtoul(from, &eptr, 0);
168 strncpy(to, type, len);
172 static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
174 unsigned int maj, min;
178 attr = sysfs_attr_get_value(devpath, "dev");
180 dbg("%s: could not get dev attribute: %s", devpath, strerror(errno));
184 dbg("dev value %s", attr);
185 if (sscanf(attr, "%u:%u", &maj, &min) != 2) {
186 err("%s: invalid dev major/minor", devpath);
190 snprintf(tmpdev, MAX_PATH_LEN, "%s/%s-maj%d-min%d-%u",
191 TMP_DIR, TMP_PREFIX, maj, min, getpid());
193 dbg("tmpdev '%s'", tmpdev);
194 if (mknod(tmpdev, 0600 | dev_type, makedev(maj, min))) {
195 err("mknod failed: %s", strerror(errno));
204 * buf points to an '=' followed by a quoted string ("foo") or a string ending
205 * with a space or ','.
207 * Return a pointer to the NUL terminated string, returns NULL if no
210 static char *get_value(char **buffer)
212 static char *quote_string = "\"\n";
213 static char *comma_string = ",\n";
217 if (**buffer == '"') {
219 * skip leading quote, terminate when quote seen
226 val = strsep(buffer, end);
227 if (val && end == quote_string)
229 * skip trailing quote
233 while (isspace(**buffer))
239 static int argc_count(char *opts)
242 while (*opts != '\0')
251 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
252 * if vendor and model are set find the first OPTIONS line in the config
253 * file that matches. Set argc and argv to match the OPTIONS string.
255 * vendor and model can end in '\n'.
257 static int get_file_options(const char *vendor, const char *model,
258 int *argc, char ***newargv)
264 char *vendor_in, *model_in, *options_in; /* read in from file */
269 dbg("vendor='%s'; model='%s'\n", vendor, model);
270 fd = fopen(config_file, "r");
272 dbg("can't open %s\n", config_file);
273 if (errno == ENOENT) {
276 err("can't open %s: %s", config_file, strerror(errno));
282 * Allocate a buffer rather than put it on the stack so we can
283 * keep it around to parse any options (any allocated newargv
284 * points into this buffer for its strings).
286 buffer = malloc(MAX_BUFFER_LEN);
288 err("Can't allocate memory.");
295 vendor_in = model_in = options_in = NULL;
297 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
301 if (buf[strlen(buffer) - 1] != '\n') {
302 info("Config file line %d too long.\n", lineno);
306 while (isspace(*buf))
309 /* blank or all whitespace line */
317 dbg("lineno %d: '%s'\n", lineno, buf);
318 str1 = strsep(&buf, "=");
319 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
320 str1 = get_value(&buf);
327 str1 = strsep(&buf, "=");
328 if (str1 && strcasecmp(str1, "MODEL") == 0) {
329 str1 = get_value(&buf);
335 str1 = strsep(&buf, "=");
339 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
340 str1 = get_value(&buf);
347 dbg("config file line %d:"
348 " vendor '%s'; model '%s'; options '%s'\n",
349 lineno, vendor_in, model_in, options_in);
351 * Only allow: [vendor=foo[,model=bar]]options=stuff
353 if (!options_in || (!vendor_in && model_in)) {
354 info("Error parsing config file line %d '%s'", lineno, buffer);
358 if (vendor == NULL) {
359 if (vendor_in == NULL) {
360 dbg("matched global option\n");
363 } else if ((vendor_in && strncmp(vendor, vendor_in,
364 strlen(vendor_in)) == 0) &&
365 (!model_in || (strncmp(model, model_in,
366 strlen(model_in)) == 0))) {
368 * Matched vendor and optionally model.
370 * Note: a short vendor_in or model_in can
371 * give a partial match (that is FOO
374 dbg("matched vendor/model\n");
382 if (vendor_in != NULL || model_in != NULL ||
383 options_in != NULL) {
385 * Something matched. Allocate newargv, and store
386 * values found in options_in.
388 strcpy(buffer, options_in);
389 c = argc_count(buffer) + 2;
390 *newargv = calloc(c, sizeof(**newargv));
392 err("Can't allocate memory.");
398 * argv[0] at 0 is skipped by getopt, but
399 * store the buffer address there for
402 (*newargv)[c] = buffer;
403 for (c = 1; c < *argc; c++)
404 (*newargv)[c] = strsep(&buffer, " \t");
417 static int set_options(int argc, char **argv, const char *short_opts,
418 char *target, char *maj_min_dev)
423 * optind is a global extern used by getopt. Since we can call
424 * set_options twice (once for command line, and once for config
425 * file) we have to reset this back to 1.
429 option = getopt_long(argc, argv, short_opts, options, NULL);
434 dbg("option '%c' arg '%s'\n", option, optarg);
436 dbg("option '%c'\n", option);
448 strncpy(maj_min_dev, optarg, MAX_PATH_LEN);
449 maj_min_dev[MAX_PATH_LEN-1] = '\0';
457 strncpy(config_file, optarg, MAX_PATH_LEN);
458 config_file[MAX_PATH_LEN-1] = '\0';
466 printf("Usage: scsi_id OPTIONS <device>\n"
467 " --device device node for SG_IO commands\n"
468 " --devpath sysfs devpath\n"
469 " --config location of config file\n"
470 " --page SCSI page (0x80, 0x83, pre-spc3-83)\n"
471 " --fallback-to-sysfs print sysfs values if inquiry fails\n"
472 " --ignore-sysfs ignore sysfs entries\n"
473 " --blacklisted threat device as blacklisted\n"
474 " --whitelisted threat device as whitelisted\n"
475 " --prefix-bus-id prefix SCSI bus id\n"
476 " --replace-whitespace replace all whitespaces by underscores\n"
477 " --verbose verbose logging\n"
478 " --version print version\n"
479 " --export print values as environment keys\n"
480 " --help print this help text\n\n");
488 if (strcmp(optarg, "0x80") == 0) {
489 default_page_code = PAGE_80;
490 } else if (strcmp(optarg, "0x83") == 0) {
491 default_page_code = PAGE_83;
492 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
493 default_page_code = PAGE_83_PRE_SPC3;
495 info("Unknown page code '%s'", optarg);
506 strncpy(target, optarg, MAX_PATH_LEN);
507 target[MAX_PATH_LEN-1] = '\0';
523 printf("%s\n", SCSI_ID_VERSION);
534 static int per_dev_options(struct sysfs_device *dev_scsi, int *good_bad, int *page_code)
538 char **newargv = NULL;
541 *good_bad = all_good;
542 *page_code = default_page_code;
544 retval = get_file_options(vendor_str, model_str, &newargc, &newargv);
546 optind = 1; /* reset this global extern */
547 while (retval == 0) {
548 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
553 dbg("option '%c' arg '%s'\n", option, optarg);
555 dbg("option '%c'\n", option);
567 if (strcmp(optarg, "0x80") == 0) {
568 *page_code = PAGE_80;
569 } else if (strcmp(optarg, "0x83") == 0) {
570 *page_code = PAGE_83;
571 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
572 *page_code = PAGE_83_PRE_SPC3;
574 info("Unknown page code '%s'", optarg);
580 info("Unknown or bad option '%c' (0x%x)", option, option);
593 static int set_sysfs_values(struct sysfs_device *dev_scsi)
595 const char *vendor, *model, *type;
597 vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
599 info("%s: cannot get vendor attribute", dev_scsi->devpath);
602 set_str(vendor_str, vendor, sizeof(vendor_str)-1);
604 model = sysfs_attr_get_value(dev_scsi->devpath, "model");
606 info("%s: cannot get model attribute\n", dev_scsi->devpath);
609 set_str(model_str, model, sizeof(model_str)-1);
611 type = sysfs_attr_get_value(dev_scsi->devpath, "type");
613 info("%s: cannot get type attribute", dev_scsi->devpath);
616 set_type(type_str, type, sizeof(type_str));
618 type = sysfs_attr_get_value(dev_scsi->devpath, "rev");
620 info("%s: cannot get type attribute\n", dev_scsi->devpath);
623 set_str(revision_str, type, sizeof(revision_str)-1);
628 static int set_inq_values(struct sysfs_device *dev_scsi, const char *path)
631 char vendor[8], model[16], type[4], rev[4];
633 retval = scsi_std_inquiry(dev_scsi, path, vendor, model, rev, type);
637 set_str(vendor_str, vendor, 8);
638 set_str(model_str, model, 16);
639 set_type(type_str, type, sizeof(type_str) - 1);
640 set_str(revision_str, rev, sizeof(revision_str) -1);
646 * format_serial: replace to whitespaces by underscores for calling
647 * programs that use the serial for device naming (multipath, Suse
650 static void format_serial(char *serial)
652 char *p = serial, *q;
657 if (q > serial && q[-1] != '_') {
671 * scsi_id: try to get an id, if one is found, printf it to stdout.
672 * returns a value passed to exit() - 0 if printed an id, else 1. This
673 * could be expanded, for example, if we want to report a failure like no
674 * memory etc. return 2, and return 1 for expected cases (like broken
675 * device found) that do not print an id.
677 static int scsi_id(const char *devpath, char *maj_min_dev)
681 struct sysfs_device *dev;
682 struct sysfs_device *dev_scsi = NULL;
685 char serial[MAX_SERIAL_LEN];
686 char serial_short[MAX_SERIAL_LEN];
687 const char *bus_str = NULL;
689 dbg("devpath %s\n", devpath);
691 dev = sysfs_device_get(devpath);
693 err("unable to access '%s'", devpath);
697 if (strcmp(dev->subsystem, "block") == 0)
702 /* mknod a temp dev to communicate with the device */
703 if (!dev_specified && create_tmp_dev(dev->devpath, maj_min_dev, dev_type)) {
704 dbg("create_tmp_dev failed\n");
709 /* get scsi parent device */
710 dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
711 if (dev_scsi == NULL) {
712 err("unable to access parent device of '%s'", devpath);
715 set_sysfs_values(dev_scsi);
719 set_inq_values(dev_scsi, maj_min_dev);
722 /* get per device (vendor + model) options from the config file */
723 retval = per_dev_options(dev_scsi, &good_dev, &page_code);
724 dbg("per dev options: good %d; page code 0x%x", good_dev, page_code);
728 } else if (scsi_get_serial(dev_scsi, maj_min_dev, page_code,
729 serial, serial_short, MAX_SERIAL_LEN)) {
730 retval = always_info?0:1;
736 char serial_str[MAX_SERIAL_LEN];
738 printf("ID_VENDOR=%s\n", vendor_str);
739 printf("ID_MODEL=%s\n", model_str);
740 printf("ID_REVISION=%s\n", revision_str);
741 set_str(serial_str, serial, sizeof(serial_str));
742 printf("ID_SERIAL=%s\n", serial_str);
743 set_str(serial_str, serial_short, sizeof(serial_str));
744 printf("ID_SERIAL_SHORT=%s\n", serial_str);
745 printf("ID_TYPE=%s\n", type_str);
747 printf("ID_BUS=%s\n", bus_str);
750 format_serial(serial);
752 printf("%s: ", dev_scsi->kernel);
753 printf("%s\n", serial);
765 int main(int argc, char **argv)
768 char devpath[MAX_PATH_LEN];
769 char maj_min_dev[MAX_PATH_LEN];
774 logging_init("scsi_id");
776 dbg("argc is %d\n", argc);
778 /* sysfs path can be overridden for testing */
779 env = getenv("SYSFS_PATH");
781 strncpy(sysfs_path, env, sizeof(sysfs_path));
782 sysfs_path[sizeof(sysfs_path)-1] = '\0';
784 strcpy(sysfs_path, "/sys");
786 env = getenv("DEVPATH");
790 strncpy(devpath, env, MAX_PATH_LEN);
791 devpath[sizeof(devpath)-1] = '\0';
795 * Get config file options.
798 retval = get_file_options(NULL, NULL, &newargc, &newargv);
803 if (newargv && (retval == 0)) {
804 if (set_options(newargc, newargv, short_options, devpath,
813 * Get command line options (overriding any config file or DEVPATH
816 if (set_options(argc, argv, short_options, devpath, maj_min_dev) < 0)
819 if (!sys_specified) {
820 info("--devpath=<path> must be specified\n");
825 retval = scsi_id(devpath, maj_min_dev);