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.
29 #include "../../udev.h"
31 #include "scsi_id_version.h"
33 /* temporary names for mknod */
34 #define TMP_DIR "/dev"
35 #define TMP_PREFIX "tmp-scsi"
37 static const char short_options[] = "abd:f:ginp:s:uvVx";
38 static const char dev_short_options[] = "bgp:";
41 static int always_info;
42 static int dev_specified;
43 static int sys_specified;
44 static char config_file[MAX_PATH_LEN] = SCSI_ID_CONFIG_FILE;
45 static int display_bus_id;
46 static enum page_code default_page_code;
47 static int use_stderr;
49 static int hotplug_mode;
50 static int reformat_serial;
51 static int ignore_sysfs;
53 static char vendor_str[64];
54 static char model_str[64];
55 static char revision_str[16];
56 static char type_str[16];
59 void log_message(int priority, const char *format, ...)
62 static int udev_log = -1;
67 value = getenv("UDEV_LOG");
69 udev_log = log_priority(value);
74 if (priority > udev_log)
77 va_start(args, format);
78 vsyslog(priority, format, args);
83 static void set_str(char *to, const char *from, size_t count)
87 /* strip trailing whitespace */
88 len = strnlen(from, count);
89 while (len && isspace(from[len-1]))
92 /* strip leading whitespace */
94 while (isspace(from[i]) && (i < len))
99 /* substitute multiple whitespace */
100 if (isspace(from[i])) {
101 while (isspace(from[i]))
106 if (from[i] == '/') {
115 static void set_type(char *to, const char *from, size_t len)
119 char *type = "generic";
121 type_num = strtoul(from, &eptr, 0);
149 strncpy(to, type, len);
153 static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
155 unsigned int maj, min;
159 attr = sysfs_attr_get_value(devpath, "dev");
161 dbg("%s: could not get dev attribute: %s", devpath, strerror(errno));
165 dbg("dev value %s", attr);
166 if (sscanf(attr, "%u:%u", &maj, &min) != 2) {
167 err("%s: invalid dev major/minor", devpath);
171 snprintf(tmpdev, MAX_PATH_LEN, "%s/%s-maj%d-min%d-%u",
172 TMP_DIR, TMP_PREFIX, maj, min, getpid());
174 dbg("tmpdev '%s'", tmpdev);
175 if (mknod(tmpdev, 0600 | dev_type, makedev(maj, min))) {
176 err("mknod failed: %s", strerror(errno));
185 * buf points to an '=' followed by a quoted string ("foo") or a string ending
186 * with a space or ','.
188 * Return a pointer to the NUL terminated string, returns NULL if no
191 static char *get_value(char **buffer)
193 static char *quote_string = "\"\n";
194 static char *comma_string = ",\n";
198 if (**buffer == '"') {
200 * skip leading quote, terminate when quote seen
207 val = strsep(buffer, end);
208 if (val && end == quote_string)
210 * skip trailing quote
214 while (isspace(**buffer))
220 static int argc_count(char *opts)
223 while (*opts != '\0')
232 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
233 * if vendor and model are set find the first OPTIONS line in the config
234 * file that matches. Set argc and argv to match the OPTIONS string.
236 * vendor and model can end in '\n'.
238 static int get_file_options(const char *vendor, const char *model,
239 int *argc, char ***newargv)
245 char *vendor_in, *model_in, *options_in; /* read in from file */
250 dbg("vendor='%s'; model='%s'\n", vendor, model);
251 fd = fopen(config_file, "r");
253 dbg("can't open %s\n", config_file);
254 if (errno == ENOENT) {
257 err("can't open %s: %s", config_file, strerror(errno));
263 * Allocate a buffer rather than put it on the stack so we can
264 * keep it around to parse any options (any allocated newargv
265 * points into this buffer for its strings).
267 buffer = malloc(MAX_BUFFER_LEN);
269 err("Can't allocate memory.");
276 vendor_in = model_in = options_in = NULL;
278 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
282 if (buf[strlen(buffer) - 1] != '\n') {
283 info("Config file line %d too long.\n", lineno);
287 while (isspace(*buf))
290 /* blank or all whitespace line */
298 dbg("lineno %d: '%s'\n", lineno, buf);
299 str1 = strsep(&buf, "=");
300 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
301 str1 = get_value(&buf);
308 str1 = strsep(&buf, "=");
309 if (str1 && strcasecmp(str1, "MODEL") == 0) {
310 str1 = get_value(&buf);
316 str1 = strsep(&buf, "=");
320 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
321 str1 = get_value(&buf);
328 dbg("config file line %d:"
329 " vendor '%s'; model '%s'; options '%s'\n",
330 lineno, vendor_in, model_in, options_in);
332 * Only allow: [vendor=foo[,model=bar]]options=stuff
334 if (!options_in || (!vendor_in && model_in)) {
335 info("Error parsing config file line %d '%s'", lineno, buffer);
339 if (vendor == NULL) {
340 if (vendor_in == NULL) {
341 dbg("matched global option\n");
344 } else if ((vendor_in && strncmp(vendor, vendor_in,
345 strlen(vendor_in)) == 0) &&
346 (!model_in || (strncmp(model, model_in,
347 strlen(model_in)) == 0))) {
349 * Matched vendor and optionally model.
351 * Note: a short vendor_in or model_in can
352 * give a partial match (that is FOO
355 dbg("matched vendor/model\n");
363 if (vendor_in != NULL || model_in != NULL ||
364 options_in != NULL) {
366 * Something matched. Allocate newargv, and store
367 * values found in options_in.
369 strcpy(buffer, options_in);
370 c = argc_count(buffer) + 2;
371 *newargv = calloc(c, sizeof(**newargv));
373 err("Can't allocate memory.");
379 * argv[0] at 0 is skipped by getopt, but
380 * store the buffer address there for
383 (*newargv)[c] = buffer;
384 for (c = 1; c < *argc; c++)
385 (*newargv)[c] = strsep(&buffer, " \t");
398 static int set_options(int argc, char **argv, const char *short_opts,
399 char *target, char *maj_min_dev)
404 * optind is a global extern used by getopt. Since we can call
405 * set_options twice (once for command line, and once for config
406 * file) we have to reset this back to 1.
410 option = getopt(argc, argv, short_opts);
415 dbg("option '%c' arg '%s'\n", option, optarg);
417 dbg("option '%c'\n", option);
429 strncpy(maj_min_dev, optarg, MAX_PATH_LEN);
430 maj_min_dev[MAX_PATH_LEN-1] = '\0';
438 strncpy(config_file, optarg, MAX_PATH_LEN);
439 config_file[MAX_PATH_LEN-1] = '\0';
451 if (strcmp(optarg, "0x80") == 0) {
452 default_page_code = PAGE_80;
453 } else if (strcmp(optarg, "0x83") == 0) {
454 default_page_code = PAGE_83;
455 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
456 default_page_code = PAGE_83_PRE_SPC3;
458 info("Unknown page code '%s'", optarg);
469 strncpy(target, optarg, MAX_PATH_LEN);
470 target[MAX_PATH_LEN-1] = '\0';
486 info("scsi_id version: %s\n", SCSI_ID_VERSION);
491 info("Unknown or bad option '%c' (0x%x)", option, option);
498 static int per_dev_options(struct sysfs_device *dev_scsi, int *good_bad, int *page_code)
502 char **newargv = NULL;
505 *good_bad = all_good;
506 *page_code = default_page_code;
508 retval = get_file_options(vendor_str, model_str, &newargc, &newargv);
510 optind = 1; /* reset this global extern */
511 while (retval == 0) {
512 option = getopt(newargc, newargv, dev_short_options);
517 dbg("option '%c' arg '%s'\n", option, optarg);
519 dbg("option '%c'\n", option);
531 if (strcmp(optarg, "0x80") == 0) {
532 *page_code = PAGE_80;
533 } else if (strcmp(optarg, "0x83") == 0) {
534 *page_code = PAGE_83;
535 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
536 *page_code = PAGE_83_PRE_SPC3;
538 info("Unknown page code '%s'", optarg);
544 info("Unknown or bad option '%c' (0x%x)", option, option);
557 static int set_sysfs_values(struct sysfs_device *dev_scsi)
559 const char *vendor, *model, *type;
561 vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
563 info("%s: cannot get vendor attribute", dev_scsi->devpath);
566 set_str(vendor_str, vendor, sizeof(vendor_str)-1);
568 model = sysfs_attr_get_value(dev_scsi->devpath, "model");
570 info("%s: cannot get model attribute\n", dev_scsi->devpath);
573 set_str(model_str, model, sizeof(model_str)-1);
575 type = sysfs_attr_get_value(dev_scsi->devpath, "type");
577 info("%s: cannot get type attribute", dev_scsi->devpath);
580 set_type(type_str, type, sizeof(type_str));
582 type = sysfs_attr_get_value(dev_scsi->devpath, "rev");
584 info("%s: cannot get type attribute\n", dev_scsi->devpath);
587 set_str(revision_str, type, sizeof(revision_str)-1);
592 static int set_inq_values(struct sysfs_device *dev_scsi, const char *path)
595 char vendor[8], model[16], type[4], rev[4];
597 retval = scsi_std_inquiry(dev_scsi, path, vendor, model, rev, type);
601 set_str(vendor_str, vendor, 8);
602 set_str(model_str, model, 16);
603 set_type(type_str, type, sizeof(type_str) - 1);
604 set_str(revision_str, rev, sizeof(revision_str) -1);
610 * format_serial: replace to whitespaces by underscores for calling
611 * programs that use the serial for device naming (multipath, Suse
614 static void format_serial(char *serial)
616 char *p = serial, *q;
621 if (q > serial && q[-1] != '_') {
635 * scsi_id: try to get an id, if one is found, printf it to stdout.
636 * returns a value passed to exit() - 0 if printed an id, else 1. This
637 * could be expanded, for example, if we want to report a failure like no
638 * memory etc. return 2, and return 1 for expected cases (like broken
639 * device found) that do not print an id.
641 static int scsi_id(const char *devpath, char *maj_min_dev)
645 struct sysfs_device *dev;
646 struct sysfs_device *dev_scsi = NULL;
649 char serial[MAX_SERIAL_LEN];
650 char serial_short[MAX_SERIAL_LEN];
653 dbg("devpath %s\n", devpath);
655 dev = sysfs_device_get(devpath);
657 err("unable to access '%s'", devpath);
661 if (strcmp(dev->subsystem, "block") == 0)
667 /* get scsi parent device */
668 dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
669 if (dev_scsi == NULL) {
670 err("unable to access parent device of '%s'", devpath);
675 /* mknod a temp dev to communicate with the device */
676 if (!dev_specified && create_tmp_dev(dev->devpath, maj_min_dev, dev_type)) {
677 dbg("create_tmp_dev failed\n");
682 set_sysfs_values(dev_scsi);
683 strcpy(bus_str,"scsi");
686 set_inq_values(dev_scsi, maj_min_dev);
687 strcpy(bus_str,"cciss");
690 /* get per device (vendor + model) options from the config file */
691 retval = per_dev_options(dev_scsi, &good_dev, &page_code);
692 dbg("per dev options: good %d; page code 0x%x", good_dev, page_code);
696 } else if (scsi_get_serial(dev_scsi, maj_min_dev, page_code,
697 serial, serial_short, MAX_SERIAL_LEN)) {
698 retval = always_info?0:1;
704 char serial_str[MAX_SERIAL_LEN];
706 printf("ID_VENDOR=%s\n", vendor_str);
707 printf("ID_MODEL=%s\n", model_str);
708 printf("ID_REVISION=%s\n", revision_str);
709 set_str(serial_str, serial, sizeof(serial_str));
710 printf("ID_SERIAL=%s\n", serial_str);
711 set_str(serial_str, serial_short, sizeof(serial_str));
712 printf("ID_SERIAL_SHORT=%s\n", serial_str);
713 printf("ID_TYPE=%s\n", type_str);
714 printf("ID_BUS=%s\n", bus_str);
717 format_serial(serial);
719 printf("%s: ", dev_scsi->kernel);
720 printf("%s\n", serial);
732 int main(int argc, char **argv)
735 char devpath[MAX_PATH_LEN];
736 char maj_min_dev[MAX_PATH_LEN];
741 logging_init("scsi_id");
743 dbg("argc is %d\n", argc);
745 /* sysfs path can be overridden for testing */
746 env = getenv("SYSFS_PATH");
748 strncpy(sysfs_path, env, sizeof(sysfs_path));
749 sysfs_path[sizeof(sysfs_path)-1] = '\0';
751 strcpy(sysfs_path, "/sys");
753 env = getenv("DEVPATH");
757 strncpy(devpath, env, MAX_PATH_LEN);
758 devpath[sizeof(devpath)-1] = '\0';
762 * Get config file options.
765 retval = get_file_options(NULL, NULL, &newargc, &newargv);
770 if (newargv && (retval == 0)) {
771 if (set_options(newargc, newargv, short_options, devpath,
780 * Get command line options (overriding any config file or DEVPATH
783 if (set_options(argc, argv, short_options, devpath, maj_min_dev) < 0)
786 if (!sys_specified) {
787 info("-s must be specified\n");
792 retval = scsi_id(devpath, maj_min_dev);