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
35 #include <sysfs/libsysfs.h>
36 #include "scsi_id_version.h"
39 #ifndef SCSI_ID_VERSION
41 #define SCSI_ID_VERSION "unknown"
45 * temporary names for mknod.
47 #define TMP_DIR "/tmp"
48 #define TMP_PREFIX "scsi"
51 * XXX Note the 'e' (send output to stderr in all cases), and 'c' (callout)
52 * options are not supported, but other code is still left in place for
55 static const char short_options[] = "bd:f:gip:s:uvV";
57 * Just duplicate per dev options.
59 static const char dev_short_options[] = "bgp:";
61 char sysfs_mnt_path[SYSFS_PATH_MAX];
64 static char *default_callout;
65 static int dev_specified;
66 static int sys_specified;
67 static char config_file[MAX_NAME_LEN] = SCSI_ID_CONFIG_FILE;
68 static int display_bus_id;
69 static int default_page_code;
70 static int use_stderr;
72 static int hotplug_mode;
73 static int reformat_serial;
75 void log_message (int level, const char *format, ...)
79 if (!debug && level == LOG_DEBUG)
82 va_start (args, format);
83 if (!hotplug_mode || use_stderr) {
84 vfprintf(stderr, format, args);
86 static int logging_init = 0;
87 static unsigned char logname[32];
90 * klibc does not have LOG_PID.
92 snprintf(logname, 32, "scsi_id[%d]", getpid());
93 openlog (logname, 0, LOG_DAEMON);
97 vsyslog(level, format, args);
103 static int get_major_minor(struct sysfs_class_device *class_dev, int *maj,
106 struct sysfs_attribute *dev_attr;
108 dev_attr = sysfs_get_classdev_attr(class_dev, "dev");
111 * XXX This happens a lot, since sg has no dev attr.
112 * And now sysfsutils does not set a meaningful errno
113 * value. Someday change this back to a LOG_WARNING.
114 * And if sysfsutils changes, check for ENOENT and handle
117 log_message(LOG_DEBUG, "%s: could not get dev attribute: %s\n",
118 class_dev->name, strerror(errno));
122 dprintf("dev value %s", dev_attr->value); /* value has a trailing \n */
123 if (sscanf(dev_attr->value, "%u:%u", maj, min) != 2) {
124 log_message(LOG_WARNING, "%s: invalid dev major/minor\n",
132 static int create_tmp_dev(struct sysfs_class_device *class_dev, char *tmpdev,
137 dprintf("(%s)\n", class_dev->name);
139 if (get_major_minor(class_dev, &maj, &min))
141 snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
142 TMP_DIR, TMP_PREFIX, maj, min, getpid());
144 dprintf("tmpdev '%s'\n", tmpdev);
146 if (mknod(tmpdev, 0600 | dev_type, makedev(maj, min))) {
147 log_message(LOG_WARNING, "mknod failed: %s\n", strerror(errno));
153 static int has_sysfs_prefix(const char *path, const char *prefix)
155 char match[MAX_NAME_LEN];
157 strncpy(match, sysfs_mnt_path, MAX_NAME_LEN);
158 strncat(match, prefix, MAX_NAME_LEN);
159 if (strncmp(path, match, strlen(match)) == 0)
168 * buf points to an '=' followed by a quoted string ("foo") or a string ending
169 * with a space or ','.
171 * Return a pointer to the NUL terminated string, returns NULL if no
174 static char *get_value(char **buffer)
176 static char *quote_string = "\"\n";
177 static char *comma_string = ",\n";
181 if (**buffer == '"') {
183 * skip leading quote, terminate when quote seen
190 val = strsep(buffer, end);
191 if (val && end == quote_string)
193 * skip trailing quote
197 while (isspace(**buffer))
203 static int argc_count(char *opts)
206 while (*opts != '\0')
215 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
216 * if vendor and model are set find the first OPTIONS line in the config
217 * file that matches. Set argc and argv to match the OPTIONS string.
219 * vendor and model can end in '\n'.
221 static int get_file_options(char *vendor, char *model, int *argc,
228 char *vendor_in, *model_in, *options_in; /* read in from file */
233 dprintf("vendor='%s'; model='%s'\n", vendor, model);
234 fd = fopen(config_file, "r");
236 dprintf("can't open %s\n", config_file);
237 if (errno == ENOENT) {
240 log_message(LOG_WARNING, "can't open %s: %s\n",
241 config_file, strerror(errno));
247 * Allocate a buffer rather than put it on the stack so we can
248 * keep it around to parse any options (any allocated newargv
249 * points into this buffer for its strings).
251 buffer = malloc(MAX_BUFFER_LEN);
253 log_message(LOG_WARNING, "Can't allocate memory.\n");
260 vendor_in = model_in = options_in = NULL;
262 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
266 if (buf[strlen(buffer) - 1] != '\n') {
267 log_message(LOG_WARNING,
268 "Config file line %d too long.\n", lineno);
272 while (isspace(*buf))
277 * blank or all whitespace line
288 dprintf("lineno %d: '%s'\n", lineno, buf);
290 str1 = strsep(&buf, "=");
291 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
292 str1 = get_value(&buf);
299 str1 = strsep(&buf, "=");
300 if (str1 && strcasecmp(str1, "MODEL") == 0) {
301 str1 = get_value(&buf);
307 str1 = strsep(&buf, "=");
311 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
312 str1 = get_value(&buf);
319 dprintf("config file line %d:"
320 " vendor '%s'; model '%s'; options '%s'\n",
321 lineno, vendor_in, model_in, options_in);
323 * Only allow: [vendor=foo[,model=bar]]options=stuff
325 if (!options_in || (!vendor_in && model_in)) {
326 log_message(LOG_WARNING,
327 "Error parsing config file line %d '%s'\n",
332 if (vendor == NULL) {
333 if (vendor_in == NULL) {
334 dprintf("matched global option\n");
337 } else if ((vendor_in && strncmp(vendor, vendor_in,
338 strlen(vendor_in)) == 0) &&
339 (!model_in || (strncmp(model, model_in,
340 strlen(model_in)) == 0))) {
342 * Matched vendor and optionally model.
344 * Note: a short vendor_in or model_in can
345 * give a partial match (that is FOO
348 dprintf("matched vendor/model\n");
351 dprintf("no match\n");
356 if (vendor_in != NULL || model_in != NULL ||
357 options_in != NULL) {
359 * Something matched. Allocate newargv, and store
360 * values found in options_in.
362 strcpy(buffer, options_in);
363 c = argc_count(buffer) + 2;
364 *newargv = calloc(c, sizeof(**newargv));
366 log_message(LOG_WARNING,
367 "Can't allocate memory.\n");
373 * argv[0] at 0 is skipped by getopt, but
374 * store the buffer address there for
377 (*newargv)[c] = buffer;
378 for (c = 1; c < *argc; c++)
379 (*newargv)[c] = strsep(&buffer, " ");
394 static int set_options(int argc, char **argv, const char *short_opts,
395 char *target, char *maj_min_dev)
400 * optind is a global extern used by getopt. Since we can call
401 * set_options twice (once for command line, and once for config
402 * file) we have to reset this back to 1. [Note glibc handles
403 * setting this to 0, but klibc does not.]
407 option = getopt(argc, argv, short_options);
412 dprintf("option '%c' arg '%s'\n", option, optarg);
414 dprintf("option '%c'\n", option);
422 default_callout = optarg;
427 strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
435 strncpy(config_file, optarg, MAX_NAME_LEN);
447 if (strcmp(optarg, "0x80") == 0) {
448 default_page_code = 0x80;
449 } else if (strcmp(optarg, "0x83") == 0) {
450 default_page_code = 0x83;
452 log_message(LOG_WARNING,
453 "Unknown page code '%s'\n", optarg);
460 strncpy(target, sysfs_mnt_path, MAX_NAME_LEN);
461 strncat(target, optarg, MAX_NAME_LEN);
473 log_message(LOG_WARNING, "scsi_id version: %s\n",
479 log_message(LOG_WARNING,
480 "Unknown or bad option '%c' (0x%x)\n",
488 static int per_dev_options(struct sysfs_device *scsi_dev, int *good_bad,
489 int *page_code, char *callout)
493 char **newargv = NULL;
494 struct sysfs_attribute *vendor, *model;
497 *good_bad = all_good;
498 *page_code = default_page_code;
499 if (default_callout && (callout != default_callout))
500 strncpy(callout, default_callout, MAX_NAME_LEN);
504 vendor = sysfs_get_device_attr(scsi_dev, "vendor");
506 log_message(LOG_WARNING, "%s: cannot get vendor attribute\n",
511 model = sysfs_get_device_attr(scsi_dev, "model");
513 log_message(LOG_WARNING, "%s: cannot get model attribute\n",
518 retval = get_file_options(vendor->value, model->value, &newargc,
521 optind = 1; /* reset this global extern */
522 while (retval == 0) {
523 option = getopt(newargc, newargv, dev_short_options);
528 dprintf("option '%c' arg '%s'\n", option, optarg);
530 dprintf("option '%c'\n", option);
538 strncpy(callout, default_callout, MAX_NAME_LEN);
546 if (strcmp(optarg, "0x80") == 0) {
548 } else if (strcmp(optarg, "0x83") == 0) {
551 log_message(LOG_WARNING,
552 "Unknown page code '%s'\n", optarg);
558 log_message(LOG_WARNING,
559 "Unknown or bad option '%c' (0x%x)\n",
574 * format_serial: replace to whitespaces by underscores for calling
575 * programs that use the serial for device naming (multipath, Suse
578 static void format_serial(char *serial)
591 * scsi_id: try to get an id, if one is found, printf it to stdout.
592 * returns a value passed to exit() - 0 if printed an id, else 1. This
593 * could be expanded, for example, if we want to report a failure like no
594 * memory etc. return 2, and return 1 for expected cases (like broken
595 * device found) that do not print an id.
597 static int scsi_id(const char *target_path, char *maj_min_dev)
601 char *serial, *unaligned_buf;
602 struct sysfs_class_device *class_dev; /* of target_path */
603 struct sysfs_class_device *class_dev_parent; /* for partitions */
604 struct sysfs_device *scsi_dev; /* the scsi_device */
607 char callout[MAX_NAME_LEN];
609 dprintf("target_path %s\n", target_path);
612 * Ugly: depend on the sysfs path to tell us whether this is a
613 * block or char device. This should probably be encoded in the
614 * "dev" along with the major/minor.
616 if (has_sysfs_prefix(target_path, "/block")) {
618 } else if (has_sysfs_prefix(target_path, "/class")) {
622 log_message(LOG_WARNING,
623 "Non block or class device '%s'\n",
628 * Expected in some cases.
630 dprintf("Non block or class device\n");
635 class_dev = sysfs_open_class_device_path(target_path);
637 log_message(LOG_WARNING, "open class %s failed: %s\n",
638 target_path, strerror(errno));
641 class_dev_parent = sysfs_get_classdev_parent(class_dev);
642 dprintf("class_dev 0x%p; class_dev_parent 0x%p\n", class_dev,
644 if (class_dev_parent) {
645 scsi_dev = sysfs_get_classdev_device(class_dev_parent);
647 scsi_dev = sysfs_get_classdev_device(class_dev);
651 * The close of scsi_dev will close class_dev or class_dev_parent.
655 * We assume we are called after the device is completely ready,
656 * so we don't have to loop here like udev. (And we are usually
661 * errno is not set if we can't find the device link, so
662 * don't print it out here.
664 log_message(LOG_WARNING, "Cannot find sysfs device associated with %s\n",
671 * Allow only scsi devices.
673 * Other block devices can support SG IO, but only ide-cd does, so
674 * for now, don't bother with anything else.
676 if (strcmp(scsi_dev->bus, "scsi") != 0) {
679 * Expected in some cases.
681 dprintf("%s is not a scsi device\n", target_path);
683 log_message(LOG_WARNING, "%s is not a scsi device\n",
689 * mknod a temp dev to communicate with the device.
691 if (!dev_specified && create_tmp_dev(class_dev, maj_min_dev,
693 dprintf("create_tmp_dev failed\n");
698 * Get any per device (vendor + model) options from the config
701 retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout);
702 dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n",
703 good_dev, page_code, callout);
706 unaligned_buf = malloc(MAX_SERIAL_LEN + ALIGN);
707 serial = (char*) (((unsigned long) unaligned_buf + (ALIGN - 1))
709 dprintf("buffer unaligned 0x%p; aligned 0x%p\n", unaligned_buf, serial);
714 } else if (callout[0] != '\0') {
716 * XXX Disabled for now ('c' is not in any options[]).
719 } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code,
720 serial, MAX_SERIAL_LEN)) {
727 format_serial(serial);
729 printf("%s: ", scsi_dev->name);
730 printf("%s\n", serial);
731 dprintf("%s\n", serial);
734 sysfs_close_device(scsi_dev);
742 int main(int argc, char **argv)
746 char target_path[MAX_NAME_LEN];
747 char maj_min_dev[MAX_NAME_LEN];
754 dprintf("argc is %d\n", argc);
755 if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
756 log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n",
761 devpath = getenv("DEVPATH");
764 * This implies that we were invoked via udev or hotplug.
768 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
769 strncat(target_path, devpath, MAX_NAME_LEN);
773 * Get config file options.
776 retval = get_file_options(NULL, NULL, &newargc, &newargv);
779 } else if (newargv && (retval == 0)) {
780 if (set_options(newargc, newargv, short_options, target_path,
786 * Get command line options (overriding any config file or DEVPATH
789 if (set_options(argc, argv, short_options, target_path,
793 if (!sys_specified) {
794 log_message(LOG_WARNING, "-s must be specified\n");
798 retval = scsi_id(target_path, maj_min_dev);