4 * Main section of the scsi_id program
6 * Copyright (C) IBM Corp. 2003
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of the
11 * License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include "../../udev.h"
38 #include "scsi_id_version.h"
40 /* temporary names for mknod */
41 #define TMP_DIR "/dev"
42 #define TMP_PREFIX "tmp-scsi"
44 static const char short_options[] = "abd:f:gip:s:uvVx";
45 static const char dev_short_options[] = "bgp:";
48 static int always_info;
49 static int dev_specified;
50 static int sys_specified;
51 static char config_file[MAX_NAME_LEN] = SCSI_ID_CONFIG_FILE;
52 static int display_bus_id;
53 static enum page_code default_page_code;
54 static int use_stderr;
56 static int hotplug_mode;
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);
159 static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
161 unsigned int maj, min;
165 attr = sysfs_attr_get_value(devpath, "dev");
167 dbg("%s: could not get dev attribute: %s", devpath, strerror(errno));
171 dbg("dev value %s", attr);
172 if (sscanf(attr, "%u:%u", &maj, &min) != 2) {
173 err("%s: invalid dev major/minor", devpath);
177 snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
178 TMP_DIR, TMP_PREFIX, maj, min, getpid());
180 dbg("tmpdev '%s'", tmpdev);
181 if (mknod(tmpdev, 0600 | dev_type, makedev(maj, min))) {
182 err("mknod failed: %s", strerror(errno));
191 * buf points to an '=' followed by a quoted string ("foo") or a string ending
192 * with a space or ','.
194 * Return a pointer to the NUL terminated string, returns NULL if no
197 static char *get_value(char **buffer)
199 static char *quote_string = "\"\n";
200 static char *comma_string = ",\n";
204 if (**buffer == '"') {
206 * skip leading quote, terminate when quote seen
213 val = strsep(buffer, end);
214 if (val && end == quote_string)
216 * skip trailing quote
220 while (isspace(**buffer))
226 static int argc_count(char *opts)
229 while (*opts != '\0')
238 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
239 * if vendor and model are set find the first OPTIONS line in the config
240 * file that matches. Set argc and argv to match the OPTIONS string.
242 * vendor and model can end in '\n'.
244 static int get_file_options(const char *vendor, const char *model,
245 int *argc, char ***newargv)
251 char *vendor_in, *model_in, *options_in; /* read in from file */
256 dbg("vendor='%s'; model='%s'\n", vendor, model);
257 fd = fopen(config_file, "r");
259 dbg("can't open %s\n", config_file);
260 if (errno == ENOENT) {
263 err("can't open %s: %s", config_file, strerror(errno));
269 * Allocate a buffer rather than put it on the stack so we can
270 * keep it around to parse any options (any allocated newargv
271 * points into this buffer for its strings).
273 buffer = malloc(MAX_BUFFER_LEN);
275 err("Can't allocate memory.");
282 vendor_in = model_in = options_in = NULL;
284 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
288 if (buf[strlen(buffer) - 1] != '\n') {
289 info("Config file line %d too long.\n", lineno);
293 while (isspace(*buf))
296 /* blank or all whitespace line */
304 dbg("lineno %d: '%s'\n", lineno, buf);
305 str1 = strsep(&buf, "=");
306 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
307 str1 = get_value(&buf);
314 str1 = strsep(&buf, "=");
315 if (str1 && strcasecmp(str1, "MODEL") == 0) {
316 str1 = get_value(&buf);
322 str1 = strsep(&buf, "=");
326 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
327 str1 = get_value(&buf);
334 dbg("config file line %d:"
335 " vendor '%s'; model '%s'; options '%s'\n",
336 lineno, vendor_in, model_in, options_in);
338 * Only allow: [vendor=foo[,model=bar]]options=stuff
340 if (!options_in || (!vendor_in && model_in)) {
341 info("Error parsing config file line %d '%s'", lineno, buffer);
345 if (vendor == NULL) {
346 if (vendor_in == NULL) {
347 dbg("matched global option\n");
350 } else if ((vendor_in && strncmp(vendor, vendor_in,
351 strlen(vendor_in)) == 0) &&
352 (!model_in || (strncmp(model, model_in,
353 strlen(model_in)) == 0))) {
355 * Matched vendor and optionally model.
357 * Note: a short vendor_in or model_in can
358 * give a partial match (that is FOO
361 dbg("matched vendor/model\n");
369 if (vendor_in != NULL || model_in != NULL ||
370 options_in != NULL) {
372 * Something matched. Allocate newargv, and store
373 * values found in options_in.
375 strcpy(buffer, options_in);
376 c = argc_count(buffer) + 2;
377 *newargv = calloc(c, sizeof(**newargv));
379 err("Can't allocate memory.");
385 * argv[0] at 0 is skipped by getopt, but
386 * store the buffer address there for
389 (*newargv)[c] = buffer;
390 for (c = 1; c < *argc; c++)
391 (*newargv)[c] = strsep(&buffer, " ");
404 static int set_options(int argc, char **argv, const char *short_opts,
405 char *target, char *maj_min_dev)
410 * optind is a global extern used by getopt. Since we can call
411 * set_options twice (once for command line, and once for config
412 * file) we have to reset this back to 1. [Note glibc handles
413 * setting this to 0, but klibc does not.]
417 option = getopt(argc, argv, short_opts);
422 dbg("option '%c' arg '%s'\n", option, optarg);
424 dbg("option '%c'\n", option);
436 strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
444 strncpy(config_file, optarg, MAX_NAME_LEN);
456 if (strcmp(optarg, "0x80") == 0) {
457 default_page_code = PAGE_80;
458 } else if (strcmp(optarg, "0x83") == 0) {
459 default_page_code = PAGE_83;
460 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
461 default_page_code = PAGE_83_PRE_SPC3;
463 info("Unknown page code '%s'", optarg);
470 strncpy(target, optarg, MAX_NAME_LEN);
471 target[MAX_NAME_LEN-1] = '\0';
487 info("scsi_id version: %s\n", SCSI_ID_VERSION);
492 info("Unknown or bad option '%c' (0x%x)", option, option);
499 static int per_dev_options(struct sysfs_device *dev_scsi, int *good_bad, int *page_code)
503 char **newargv = NULL;
504 const char *vendor, *model, *type;
507 *good_bad = all_good;
508 *page_code = default_page_code;
510 vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
512 info("%s: cannot get vendor attribute", dev_scsi->devpath);
515 set_str(vendor_str, vendor, sizeof(vendor_str)-1);
517 model = sysfs_attr_get_value(dev_scsi->devpath, "model");
519 info("%s: cannot get model attribute\n", dev_scsi->devpath);
522 set_str(model_str, model, sizeof(model_str)-1);
524 type = sysfs_attr_get_value(dev_scsi->devpath, "type");
526 info("%s: cannot get type attribute", dev_scsi->devpath);
529 set_type(type_str, type, sizeof(type_str));
531 type = sysfs_attr_get_value(dev_scsi->devpath, "rev");
533 info("%s: cannot get type attribute\n", dev_scsi->devpath);
536 set_str(revision_str, type, sizeof(revision_str)-1);
538 retval = get_file_options(vendor, model, &newargc, &newargv);
540 optind = 1; /* reset this global extern */
541 while (retval == 0) {
542 option = getopt(newargc, newargv, dev_short_options);
547 dbg("option '%c' arg '%s'\n", option, optarg);
549 dbg("option '%c'\n", option);
561 if (strcmp(optarg, "0x80") == 0) {
562 *page_code = PAGE_80;
563 } else if (strcmp(optarg, "0x83") == 0) {
564 *page_code = PAGE_83;
565 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
566 *page_code = PAGE_83_PRE_SPC3;
568 info("Unknown page code '%s'", optarg);
574 info("Unknown or bad option '%c' (0x%x)", option, option);
588 * format_serial: replace to whitespaces by underscores for calling
589 * programs that use the serial for device naming (multipath, Suse
592 static void format_serial(char *serial)
594 char *p = serial, *q;
599 if (q > serial && q[-1] != '_') {
613 * scsi_id: try to get an id, if one is found, printf it to stdout.
614 * returns a value passed to exit() - 0 if printed an id, else 1. This
615 * could be expanded, for example, if we want to report a failure like no
616 * memory etc. return 2, and return 1 for expected cases (like broken
617 * device found) that do not print an id.
619 static int scsi_id(const char *devpath, char *maj_min_dev)
623 char *serial, *unaligned_buf;
624 struct sysfs_device *dev;
625 struct sysfs_device *dev_scsi;
629 dbg("devpath %s\n", devpath);
631 dev = sysfs_device_get(devpath);
633 err("unable to access '%s'", devpath);
637 if (strcmp(dev->subsystem, "block") == 0)
642 /* get scsi parent device */
643 dev_scsi = sysfs_device_get_parent(dev);
644 if (dev_scsi == NULL) {
645 err("unable to access parent device of '%s'", devpath);
649 /* allow only scsi devices */
650 if (strcmp(dev_scsi->subsystem, "scsi") != 0) {
651 info("%s is not a scsi device", devpath);
655 /* mknod a temp dev to communicate with the device */
656 if (!dev_specified && create_tmp_dev(dev->devpath, maj_min_dev, dev_type)) {
657 dbg("create_tmp_dev failed\n");
661 /* get per device (vendor + model) options from the config file */
662 retval = per_dev_options(dev_scsi, &good_dev, &page_code);
663 dbg("per dev options: good %d; page code 0x%x", good_dev, page_code);
666 unaligned_buf = malloc(MAX_SERIAL_LEN + ALIGN);
667 serial = (char*) (((unsigned long) unaligned_buf + (ALIGN - 1))
669 dbg("buffer unaligned 0x%p; aligned 0x%p\n", unaligned_buf, serial);
674 } else if (scsi_get_serial(dev_scsi, maj_min_dev, page_code,
675 serial, MAX_SERIAL_LEN)) {
676 retval = always_info?0:1;
682 static char serial_str[64];
683 printf("ID_VENDOR=%s\n", vendor_str);
684 printf("ID_MODEL=%s\n", model_str);
685 printf("ID_REVISION=%s\n", revision_str);
686 set_str(serial_str, serial, sizeof(serial_str));
687 printf("ID_SERIAL=%s\n", serial_str);
688 printf("ID_TYPE=%s\n", type_str);
689 printf("ID_BUS=scsi\n");
692 format_serial(serial);
694 printf("%s: ", dev_scsi->kernel_name);
695 printf("%s\n", serial);
707 int main(int argc, char **argv)
710 char devpath[MAX_NAME_LEN];
711 char maj_min_dev[MAX_NAME_LEN];
716 logging_init("scsi_id");
718 dbg("argc is %d\n", argc);
720 /* sysfs path can be overridden for testing */
721 env = getenv("SYSFS_PATH");
723 strncpy(sysfs_path, env, sizeof(sysfs_path));
724 sysfs_path[sizeof(sysfs_path)-1] = '\0';
726 strcpy(sysfs_path, "/sys");
728 env = getenv("DEVPATH");
732 strncpy(devpath, env, MAX_NAME_LEN);
733 devpath[sizeof(devpath)-1] = '\0';
737 * Get config file options.
740 retval = get_file_options(NULL, NULL, &newargc, &newargv);
745 if (newargv && (retval == 0)) {
746 if (set_options(newargc, newargv, short_options, devpath,
755 * Get command line options (overriding any config file or DEVPATH
758 if (set_options(argc, argv, short_options, devpath, maj_min_dev) < 0)
761 if (!sys_specified) {
762 info("-s must be specified\n");
767 retval = scsi_id(devpath, maj_min_dev);