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:uvVx";
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 static char vendor_str[64];
76 static char model_str[64];
78 void log_message (int level, const char *format, ...)
82 if (!debug && level == LOG_DEBUG)
85 va_start (args, format);
86 if (!hotplug_mode || use_stderr) {
87 vfprintf(stderr, format, args);
89 static int logging_init = 0;
91 openlog ("scsi_id", LOG_PID, LOG_DAEMON);
95 vsyslog(level, format, args);
101 static void set_str(char *to, const unsigned char *from, int count)
106 len = strnlen(from, count);
107 while (isspace(from[len-1]))
111 while (isspace(from[i]) && (i < len))
129 static int get_major_minor(struct sysfs_class_device *class_dev, int *maj,
132 struct sysfs_attribute *dev_attr;
134 dev_attr = sysfs_get_classdev_attr(class_dev, "dev");
137 * XXX This happens a lot, since sg has no dev attr.
138 * And now sysfsutils does not set a meaningful errno
139 * value. Someday change this back to a LOG_WARNING.
140 * And if sysfsutils changes, check for ENOENT and handle
143 log_message(LOG_DEBUG, "%s: could not get dev attribute: %s\n",
144 class_dev->name, strerror(errno));
148 dprintf("dev value %s", dev_attr->value); /* value has a trailing \n */
149 if (sscanf(dev_attr->value, "%u:%u", maj, min) != 2) {
150 log_message(LOG_WARNING, "%s: invalid dev major/minor\n",
158 static int create_tmp_dev(struct sysfs_class_device *class_dev, char *tmpdev,
163 dprintf("(%s)\n", class_dev->name);
165 if (get_major_minor(class_dev, &maj, &min))
167 snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
168 TMP_DIR, TMP_PREFIX, maj, min, getpid());
170 dprintf("tmpdev '%s'\n", tmpdev);
172 if (mknod(tmpdev, 0600 | dev_type, makedev(maj, min))) {
173 log_message(LOG_WARNING, "mknod failed: %s\n", strerror(errno));
179 static int has_sysfs_prefix(const char *path, const char *prefix)
181 char match[MAX_NAME_LEN];
183 strncpy(match, sysfs_mnt_path, MAX_NAME_LEN);
184 strncat(match, prefix, MAX_NAME_LEN);
185 if (strncmp(path, match, strlen(match)) == 0)
194 * buf points to an '=' followed by a quoted string ("foo") or a string ending
195 * with a space or ','.
197 * Return a pointer to the NUL terminated string, returns NULL if no
200 static char *get_value(char **buffer)
202 static char *quote_string = "\"\n";
203 static char *comma_string = ",\n";
207 if (**buffer == '"') {
209 * skip leading quote, terminate when quote seen
216 val = strsep(buffer, end);
217 if (val && end == quote_string)
219 * skip trailing quote
223 while (isspace(**buffer))
229 static int argc_count(char *opts)
232 while (*opts != '\0')
241 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
242 * if vendor and model are set find the first OPTIONS line in the config
243 * file that matches. Set argc and argv to match the OPTIONS string.
245 * vendor and model can end in '\n'.
247 static int get_file_options(char *vendor, char *model, int *argc,
254 char *vendor_in, *model_in, *options_in; /* read in from file */
259 dprintf("vendor='%s'; model='%s'\n", vendor, model);
260 fd = fopen(config_file, "r");
262 dprintf("can't open %s\n", config_file);
263 if (errno == ENOENT) {
266 log_message(LOG_WARNING, "can't open %s: %s\n",
267 config_file, strerror(errno));
273 * Allocate a buffer rather than put it on the stack so we can
274 * keep it around to parse any options (any allocated newargv
275 * points into this buffer for its strings).
277 buffer = malloc(MAX_BUFFER_LEN);
279 log_message(LOG_WARNING, "Can't allocate memory.\n");
286 vendor_in = model_in = options_in = NULL;
288 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
292 if (buf[strlen(buffer) - 1] != '\n') {
293 log_message(LOG_WARNING,
294 "Config file line %d too long.\n", lineno);
298 while (isspace(*buf))
303 * blank or all whitespace line
314 dprintf("lineno %d: '%s'\n", lineno, buf);
316 str1 = strsep(&buf, "=");
317 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
318 str1 = get_value(&buf);
325 str1 = strsep(&buf, "=");
326 if (str1 && strcasecmp(str1, "MODEL") == 0) {
327 str1 = get_value(&buf);
333 str1 = strsep(&buf, "=");
337 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
338 str1 = get_value(&buf);
345 dprintf("config file line %d:"
346 " vendor '%s'; model '%s'; options '%s'\n",
347 lineno, vendor_in, model_in, options_in);
349 * Only allow: [vendor=foo[,model=bar]]options=stuff
351 if (!options_in || (!vendor_in && model_in)) {
352 log_message(LOG_WARNING,
353 "Error parsing config file line %d '%s'\n",
358 if (vendor == NULL) {
359 if (vendor_in == NULL) {
360 dprintf("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 dprintf("matched vendor/model\n");
377 dprintf("no match\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 log_message(LOG_WARNING,
393 "Can't allocate memory.\n");
399 * argv[0] at 0 is skipped by getopt, but
400 * store the buffer address there for
403 (*newargv)[c] = buffer;
404 for (c = 1; c < *argc; c++)
405 (*newargv)[c] = strsep(&buffer, " ");
420 static int set_options(int argc, char **argv, const char *short_opts,
421 char *target, char *maj_min_dev)
426 * optind is a global extern used by getopt. Since we can call
427 * set_options twice (once for command line, and once for config
428 * file) we have to reset this back to 1. [Note glibc handles
429 * setting this to 0, but klibc does not.]
433 option = getopt(argc, argv, short_options);
438 dprintf("option '%c' arg '%s'\n", option, optarg);
440 dprintf("option '%c'\n", option);
448 default_callout = optarg;
453 strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
461 strncpy(config_file, optarg, MAX_NAME_LEN);
473 if (strcmp(optarg, "0x80") == 0) {
474 default_page_code = 0x80;
475 } else if (strcmp(optarg, "0x83") == 0) {
476 default_page_code = 0x83;
478 log_message(LOG_WARNING,
479 "Unknown page code '%s'\n", optarg);
486 strncpy(target, sysfs_mnt_path, MAX_NAME_LEN);
487 strncat(target, optarg, MAX_NAME_LEN);
503 log_message(LOG_WARNING, "scsi_id version: %s\n",
509 log_message(LOG_WARNING,
510 "Unknown or bad option '%c' (0x%x)\n",
518 static int per_dev_options(struct sysfs_device *scsi_dev, int *good_bad,
519 int *page_code, char *callout)
523 char **newargv = NULL;
524 struct sysfs_attribute *vendor, *model;
527 *good_bad = all_good;
528 *page_code = default_page_code;
529 if (default_callout && (callout != default_callout))
530 strncpy(callout, default_callout, MAX_NAME_LEN);
534 vendor = sysfs_get_device_attr(scsi_dev, "vendor");
536 log_message(LOG_WARNING, "%s: cannot get vendor attribute\n",
540 set_str(vendor_str, vendor->value, sizeof(vendor_str)-1);
542 model = sysfs_get_device_attr(scsi_dev, "model");
544 log_message(LOG_WARNING, "%s: cannot get model attribute\n",
548 set_str(model_str, model->value, sizeof(model_str)-1);
550 retval = get_file_options(vendor->value, model->value, &newargc,
553 optind = 1; /* reset this global extern */
554 while (retval == 0) {
555 option = getopt(newargc, newargv, dev_short_options);
560 dprintf("option '%c' arg '%s'\n", option, optarg);
562 dprintf("option '%c'\n", option);
570 strncpy(callout, default_callout, MAX_NAME_LEN);
578 if (strcmp(optarg, "0x80") == 0) {
580 } else if (strcmp(optarg, "0x83") == 0) {
583 log_message(LOG_WARNING,
584 "Unknown page code '%s'\n", optarg);
590 log_message(LOG_WARNING,
591 "Unknown or bad option '%c' (0x%x)\n",
606 * format_serial: replace to whitespaces by underscores for calling
607 * programs that use the serial for device naming (multipath, Suse
610 static void format_serial(char *serial)
623 * scsi_id: try to get an id, if one is found, printf it to stdout.
624 * returns a value passed to exit() - 0 if printed an id, else 1. This
625 * could be expanded, for example, if we want to report a failure like no
626 * memory etc. return 2, and return 1 for expected cases (like broken
627 * device found) that do not print an id.
629 static int scsi_id(const char *target_path, char *maj_min_dev)
633 char *serial, *unaligned_buf;
634 struct sysfs_class_device *class_dev; /* of target_path */
635 struct sysfs_class_device *class_dev_parent; /* for partitions */
636 struct sysfs_device *scsi_dev; /* the scsi_device */
639 char callout[MAX_NAME_LEN];
641 dprintf("target_path %s\n", target_path);
644 * Ugly: depend on the sysfs path to tell us whether this is a
645 * block or char device. This should probably be encoded in the
646 * "dev" along with the major/minor.
648 if (has_sysfs_prefix(target_path, "/block")) {
650 } else if (has_sysfs_prefix(target_path, "/class")) {
654 log_message(LOG_WARNING,
655 "Non block or class device '%s'\n",
660 * Expected in some cases.
662 dprintf("Non block or class device\n");
667 class_dev = sysfs_open_class_device_path(target_path);
669 log_message(LOG_WARNING, "open class %s failed: %s\n",
670 target_path, strerror(errno));
673 class_dev_parent = sysfs_get_classdev_parent(class_dev);
674 dprintf("class_dev 0x%p; class_dev_parent 0x%p\n", class_dev,
676 if (class_dev_parent) {
677 scsi_dev = sysfs_get_classdev_device(class_dev_parent);
679 scsi_dev = sysfs_get_classdev_device(class_dev);
683 * The close of scsi_dev will close class_dev or class_dev_parent.
687 * We assume we are called after the device is completely ready,
688 * so we don't have to loop here like udev. (And we are usually
693 * errno is not set if we can't find the device link, so
694 * don't print it out here.
696 log_message(LOG_WARNING, "Cannot find sysfs device associated with %s\n",
703 * Allow only scsi devices.
705 * Other block devices can support SG IO, but only ide-cd does, so
706 * for now, don't bother with anything else.
708 if (strcmp(scsi_dev->bus, "scsi") != 0) {
711 * Expected in some cases.
713 dprintf("%s is not a scsi device\n", target_path);
715 log_message(LOG_WARNING, "%s is not a scsi device\n",
721 * mknod a temp dev to communicate with the device.
723 if (!dev_specified && create_tmp_dev(class_dev, maj_min_dev,
725 dprintf("create_tmp_dev failed\n");
730 * Get any per device (vendor + model) options from the config
733 retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout);
734 dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n",
735 good_dev, page_code, callout);
738 unaligned_buf = malloc(MAX_SERIAL_LEN + ALIGN);
739 serial = (char*) (((unsigned long) unaligned_buf + (ALIGN - 1))
741 dprintf("buffer unaligned 0x%p; aligned 0x%p\n", unaligned_buf, serial);
746 } else if (callout[0] != '\0') {
748 * XXX Disabled for now ('c' is not in any options[]).
751 } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code,
752 serial, MAX_SERIAL_LEN)) {
759 static char serial_str[64];
760 printf("ID_VENDOR=%s\n", vendor_str);
761 printf("ID_MODEL=%s\n", model_str);
762 set_str(serial_str, serial, sizeof(serial_str));
763 printf("ID_SERIAL=%s\n", serial_str);
766 format_serial(serial);
768 printf("%s: ", scsi_dev->name);
769 printf("%s\n", serial);
771 dprintf("%s\n", serial);
774 sysfs_close_device(scsi_dev);
782 int main(int argc, char **argv)
786 char target_path[MAX_NAME_LEN];
787 char maj_min_dev[MAX_NAME_LEN];
794 dprintf("argc is %d\n", argc);
795 if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
796 log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n",
801 devpath = getenv("DEVPATH");
804 * This implies that we were invoked via udev or hotplug.
808 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
809 strncat(target_path, devpath, MAX_NAME_LEN);
813 * Get config file options.
816 retval = get_file_options(NULL, NULL, &newargc, &newargv);
819 } else if (newargv && (retval == 0)) {
820 if (set_options(newargc, newargv, short_options, target_path,
826 * Get command line options (overriding any config file or DEVPATH
829 if (set_options(argc, argv, short_options, target_path,
833 if (!sys_specified) {
834 log_message(LOG_WARNING, "-s must be specified\n");
838 retval = scsi_id(target_path, maj_min_dev);