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 int sysfs_get_attr(const char *devpath, const char *attr, char *value,
106 char attr_path[SYSFS_PATH_MAX];
108 strncpy(attr_path, devpath, SYSFS_PATH_MAX);
109 strncat(attr_path, "/", SYSFS_PATH_MAX);
110 strncat(attr_path, attr, SYSFS_PATH_MAX);
111 dprintf("%s\n", attr_path);
112 return sysfs_read_attribute_value(attr_path, value, SYSFS_NAME_LEN);
115 static int get_major_minor(const char *devpath, int *maj, int *min)
117 char dev_value[MAX_ATTR_LEN];
119 if (sysfs_get_attr(devpath, "dev", dev_value, MAX_ATTR_LEN)) {
121 * XXX This happens a lot, since sg has no dev attr.
122 * And now sysfsutils does not set a meaningful errno
123 * value. Someday change this back to a LOG_WARNING.
124 * And if sysfsutils changes, check for ENOENT and handle
127 log_message(LOG_DEBUG, "%s: could not get dev attribute: %s\n",
128 devpath, strerror(errno));
132 dprintf("dev value %s", dev_value); /* dev_value has a trailing \n */
133 if (sscanf(dev_value, "%u:%u", maj, min) != 2) {
134 log_message(LOG_WARNING, "%s: invalid dev major/minor\n",
142 static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
146 dprintf("(%s)\n", devpath);
148 if (get_major_minor(devpath, &maj, &min))
150 snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
151 TMP_DIR, TMP_PREFIX, maj, min, getpid());
153 dprintf("tmpdev '%s'\n", tmpdev);
155 if (mknod(tmpdev, 0600 | dev_type, makedev(maj, min))) {
156 log_message(LOG_WARNING, "mknod failed: %s\n", strerror(errno));
162 static int has_sysfs_prefix(const char *path, const char *prefix)
164 char match[MAX_NAME_LEN];
166 strncpy(match, sysfs_mnt_path, MAX_NAME_LEN);
167 strncat(match, prefix, MAX_NAME_LEN);
168 if (strncmp(path, match, strlen(match)) == 0)
177 * buf points to an '=' followed by a quoted string ("foo") or a string ending
178 * with a space or ','.
180 * Return a pointer to the NUL terminated string, returns NULL if no
183 static char *get_value(char **buffer)
185 static char *quote_string = "\"\n";
186 static char *comma_string = ",\n";
190 if (**buffer == '"') {
192 * skip leading quote, terminate when quote seen
199 val = strsep(buffer, end);
200 if (val && end == quote_string)
202 * skip trailing quote
206 while (isspace(**buffer))
212 static int argc_count(char *opts)
215 while (*opts != '\0')
224 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
225 * if vendor and model are set find the first OPTIONS line in the config
226 * file that matches. Set argc and argv to match the OPTIONS string.
228 * vendor and model can end in '\n'.
230 static int get_file_options(char *vendor, char *model, int *argc,
237 char *vendor_in, *model_in, *options_in; /* read in from file */
242 dprintf("vendor='%s'; model='%s'\n", vendor, model);
243 fd = fopen(config_file, "r");
245 dprintf("can't open %s\n", config_file);
246 if (errno == ENOENT) {
249 log_message(LOG_WARNING, "can't open %s: %s\n",
250 config_file, strerror(errno));
256 * Allocate a buffer rather than put it on the stack so we can
257 * keep it around to parse any options (any allocated newargv
258 * points into this buffer for its strings).
260 buffer = malloc(MAX_BUFFER_LEN);
262 log_message(LOG_WARNING, "Can't allocate memory.\n");
269 vendor_in = model_in = options_in = NULL;
271 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
275 if (buf[strlen(buffer) - 1] != '\n') {
276 log_message(LOG_WARNING,
277 "Config file line %d too long.\n", lineno);
281 while (isspace(*buf))
286 * blank or all whitespace line
297 dprintf("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 dprintf("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 log_message(LOG_WARNING,
336 "Error parsing config file line %d '%s'\n",
341 if (vendor == NULL) {
342 if (vendor_in == NULL) {
343 dprintf("matched global option\n");
346 } else if ((vendor_in && strncmp(vendor, vendor_in,
347 strlen(vendor_in)) == 0) &&
348 (!model_in || (strncmp(model, model_in,
349 strlen(model_in)) == 0))) {
351 * Matched vendor and optionally model.
353 * Note: a short vendor_in or model_in can
354 * give a partial match (that is FOO
357 dprintf("matched vendor/model\n");
360 dprintf("no match\n");
365 if (vendor_in != NULL || model_in != NULL ||
366 options_in != NULL) {
368 * Something matched. Allocate newargv, and store
369 * values found in options_in.
371 strcpy(buffer, options_in);
372 c = argc_count(buffer) + 2;
373 *newargv = calloc(c, sizeof(**newargv));
375 log_message(LOG_WARNING,
376 "Can't allocate memory.\n");
382 * argv[0] at 0 is skipped by getopt, but
383 * store the buffer address there for
386 (*newargv)[c] = buffer;
387 for (c = 1; c < *argc; c++)
388 (*newargv)[c] = strsep(&buffer, " ");
403 static int set_options(int argc, char **argv, const char *short_opts,
404 char *target, char *maj_min_dev)
409 * optind is a global extern used by getopt. Since we can call
410 * set_options twice (once for command line, and once for config
411 * file) we have to reset this back to 1. [Note glibc handles
412 * setting this to 0, but klibc does not.]
416 option = getopt(argc, argv, short_options);
421 dprintf("option '%c' arg '%s'\n", option, optarg);
423 dprintf("option '%c'\n", option);
431 default_callout = optarg;
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 = 0x80;
458 } else if (strcmp(optarg, "0x83") == 0) {
459 default_page_code = 0x83;
461 log_message(LOG_WARNING,
462 "Unknown page code '%s'\n", optarg);
469 strncpy(target, sysfs_mnt_path, MAX_NAME_LEN);
470 strncat(target, optarg, MAX_NAME_LEN);
482 log_message(LOG_WARNING, "scsi_id version: %s\n",
488 log_message(LOG_WARNING,
489 "Unknown or bad option '%c' (0x%x)\n",
497 static int per_dev_options(struct sysfs_device *scsi_dev, int *good_bad,
498 int *page_code, char *callout)
502 char **newargv = NULL;
503 char vendor[MAX_ATTR_LEN];
504 char model[MAX_ATTR_LEN];
507 *good_bad = all_good;
508 *page_code = default_page_code;
509 if (default_callout && (callout != default_callout))
510 strncpy(callout, default_callout, MAX_NAME_LEN);
514 if (sysfs_get_attr(scsi_dev->path, "vendor", vendor, MAX_ATTR_LEN)) {
515 log_message(LOG_WARNING, "%s: cannot get vendor attribute\n",
520 if (sysfs_get_attr(scsi_dev->path, "model", model, MAX_ATTR_LEN)) {
521 log_message(LOG_WARNING, "%s: cannot get model attribute\n",
526 retval = get_file_options(vendor, model, &newargc, &newargv);
528 optind = 1; /* reset this global extern */
529 while (retval == 0) {
530 option = getopt(newargc, newargv, dev_short_options);
535 dprintf("option '%c' arg '%s'\n", option, optarg);
537 dprintf("option '%c'\n", option);
545 strncpy(callout, default_callout, MAX_NAME_LEN);
553 if (strcmp(optarg, "0x80") == 0) {
555 } else if (strcmp(optarg, "0x83") == 0) {
558 log_message(LOG_WARNING,
559 "Unknown page code '%s'\n", optarg);
565 log_message(LOG_WARNING,
566 "Unknown or bad option '%c' (0x%x)\n",
581 * format_serial: replace to whitespaces by underscores for calling
582 * programs that use the serial for device naming (multipath, Suse
585 static void format_serial(char *serial)
598 * scsi_id: try to get an id, if one is found, printf it to stdout.
599 * returns a value passed to exit() - 0 if printed an id, else 1. This
600 * could be expanded, for example, if we want to report a failure like no
601 * memory etc. return 2, and return 1 for expected cases (like broken
602 * device found) that do not print an id.
604 static int scsi_id(const char *target_path, char *maj_min_dev)
608 char *serial, *unaligned_buf;
609 struct sysfs_class_device *class_dev; /* of target_path */
610 struct sysfs_class_device *class_dev_parent; /* for partitions */
611 struct sysfs_device *scsi_dev; /* the scsi_device */
614 char callout[MAX_NAME_LEN];
616 dprintf("target_path %s\n", target_path);
619 * Ugly: depend on the sysfs path to tell us whether this is a
620 * block or char device. This should probably be encoded in the
621 * "dev" along with the major/minor.
623 if (has_sysfs_prefix(target_path, "/block")) {
625 } else if (has_sysfs_prefix(target_path, "/class")) {
629 log_message(LOG_WARNING,
630 "Non block or class device '%s'\n",
635 * Expected in some cases.
637 dprintf("Non block or class device\n");
642 class_dev = sysfs_open_class_device_path(target_path);
644 log_message(LOG_WARNING, "open class %s failed: %s\n",
645 target_path, strerror(errno));
648 class_dev_parent = sysfs_get_classdev_parent(class_dev);
649 dprintf("class_dev 0x%p; class_dev_parent 0x%p\n", class_dev,
651 if (class_dev_parent) {
652 scsi_dev = sysfs_get_classdev_device(class_dev_parent);
654 scsi_dev = sysfs_get_classdev_device(class_dev);
658 * The close of scsi_dev will close class_dev or class_dev_parent.
662 * We assume we are called after the device is completely ready,
663 * so we don't have to loop here like udev. (And we are usually
668 * errno is not set if we can't find the device link, so
669 * don't print it out here.
671 log_message(LOG_WARNING, "Cannot find sysfs device associated with %s\n",
678 * Allow only scsi devices.
680 * Other block devices can support SG IO, but only ide-cd does, so
681 * for now, don't bother with anything else.
683 if (strcmp(scsi_dev->bus, "scsi") != 0) {
686 * Expected in some cases.
688 dprintf("%s is not a scsi device\n", target_path);
690 log_message(LOG_WARNING, "%s is not a scsi device\n",
696 * mknod a temp dev to communicate with the device.
698 * XXX pass down class_dev or class_dev_parent.
700 if (!dev_specified && create_tmp_dev(target_path, maj_min_dev,
702 dprintf("create_tmp_dev failed\n");
707 * Get any per device (vendor + model) options from the config
710 retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout);
711 dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n",
712 good_dev, page_code, callout);
715 unaligned_buf = malloc(MAX_SERIAL_LEN + ALIGN);
716 serial = (char*) (((int) unaligned_buf + (ALIGN - 1)) & ~(ALIGN - 1));
717 dprintf("buffer unaligned 0x%p; aligned 0x%p\n", unaligned_buf, serial);
722 } else if (callout[0] != '\0') {
724 * XXX Disabled for now ('c' is not in any options[]).
727 } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code,
728 serial, MAX_SERIAL_LEN)) {
735 format_serial(serial);
737 printf("%s: ", scsi_dev->name);
738 printf("%s", serial);
741 dprintf("%s\n", serial);
744 sysfs_close_device(scsi_dev);
752 int main(int argc, char **argv)
756 char target_path[MAX_NAME_LEN];
757 char maj_min_dev[MAX_NAME_LEN];
764 if ((argc == 2) && (argv[1][0] != '-')) {
766 dprintf("hotplug assumed\n");
769 dprintf("argc is %d\n", argc);
770 if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
771 log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n",
778 * There is a kernel race creating attributes, if called
779 * directly, uncomment the sleep.
783 devpath = getenv("DEVPATH");
785 log_message(LOG_WARNING, "DEVPATH is not set\n");
790 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
791 strncat(target_path, devpath, MAX_NAME_LEN);
795 * Override any command line options set via the config file. This
796 * is the only way to set options when in hotplug mode.
799 retval = get_file_options(NULL, NULL, &newargc, &newargv);
802 } else if (newargv && (retval == 0)) {
803 if (set_options(newargc, newargv, short_options, target_path,
809 if (set_options(argc, argv, short_options, target_path,
814 if (!sys_specified) {
815 log_message(LOG_WARNING, "-s must be specified\n");
819 retval = scsi_id(target_path, maj_min_dev);