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[] = "abd: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 int always_info;
65 static char *default_callout;
66 static int dev_specified;
67 static int sys_specified;
68 static char config_file[MAX_NAME_LEN] = SCSI_ID_CONFIG_FILE;
69 static int display_bus_id;
70 static int default_page_code;
71 static int use_stderr;
73 static int hotplug_mode;
74 static int reformat_serial;
76 static char vendor_str[64];
77 static char model_str[64];
78 static char revision_str[16];
79 static char type_str[16];
81 void log_message (int level, const char *format, ...)
85 if (!debug && level == LOG_DEBUG)
88 va_start (args, format);
89 if (!hotplug_mode || use_stderr) {
90 vfprintf(stderr, format, args);
92 static int logging_init = 0;
94 openlog ("scsi_id", LOG_PID, LOG_DAEMON);
98 vsyslog(level, format, args);
104 static void set_str(char *to, const unsigned char *from, int count)
109 /* strip trailing whitespace */
110 len = strnlen(from, count);
111 while (isspace(from[len-1]))
114 /* strip leading whitespace */
116 while (isspace(from[i]) && (i < len))
121 /* substitute multiple whitespace */
122 if (isspace(from[i])) {
123 while (isspace(from[i]))
128 if (from[i] == '/') {
137 static void set_type(char *to, const char *from, int count)
142 type_num = strtoul(from, &eptr, 0);
152 sprintf(to, "optical");
158 sprintf(to, "optical");
164 sprintf(to, "optical");
167 sprintf(to, "generic");
171 sprintf(to, "generic");
175 static int get_major_minor(struct sysfs_class_device *class_dev, int *maj,
178 struct sysfs_attribute *dev_attr;
180 dev_attr = sysfs_get_classdev_attr(class_dev, "dev");
183 * XXX This happens a lot, since sg has no dev attr.
184 * And now sysfsutils does not set a meaningful errno
185 * value. Someday change this back to a LOG_WARNING.
186 * And if sysfsutils changes, check for ENOENT and handle
189 log_message(LOG_DEBUG, "%s: could not get dev attribute: %s\n",
190 class_dev->name, strerror(errno));
194 dprintf("dev value %s", dev_attr->value); /* value has a trailing \n */
195 if (sscanf(dev_attr->value, "%u:%u", maj, min) != 2) {
196 log_message(LOG_WARNING, "%s: invalid dev major/minor\n",
204 static int create_tmp_dev(struct sysfs_class_device *class_dev, char *tmpdev,
209 dprintf("(%s)\n", class_dev->name);
211 if (get_major_minor(class_dev, &maj, &min))
213 snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
214 TMP_DIR, TMP_PREFIX, maj, min, getpid());
216 dprintf("tmpdev '%s'\n", tmpdev);
218 if (mknod(tmpdev, 0600 | dev_type, makedev(maj, min))) {
219 log_message(LOG_WARNING, "mknod failed: %s\n", strerror(errno));
225 static int has_sysfs_prefix(const char *path, const char *prefix)
227 char match[MAX_NAME_LEN];
229 strncpy(match, sysfs_mnt_path, MAX_NAME_LEN);
230 strncat(match, prefix, MAX_NAME_LEN);
231 if (strncmp(path, match, strlen(match)) == 0)
240 * buf points to an '=' followed by a quoted string ("foo") or a string ending
241 * with a space or ','.
243 * Return a pointer to the NUL terminated string, returns NULL if no
246 static char *get_value(char **buffer)
248 static char *quote_string = "\"\n";
249 static char *comma_string = ",\n";
253 if (**buffer == '"') {
255 * skip leading quote, terminate when quote seen
262 val = strsep(buffer, end);
263 if (val && end == quote_string)
265 * skip trailing quote
269 while (isspace(**buffer))
275 static int argc_count(char *opts)
278 while (*opts != '\0')
287 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
288 * if vendor and model are set find the first OPTIONS line in the config
289 * file that matches. Set argc and argv to match the OPTIONS string.
291 * vendor and model can end in '\n'.
293 static int get_file_options(char *vendor, char *model, int *argc,
300 char *vendor_in, *model_in, *options_in; /* read in from file */
305 dprintf("vendor='%s'; model='%s'\n", vendor, model);
306 fd = fopen(config_file, "r");
308 dprintf("can't open %s\n", config_file);
309 if (errno == ENOENT) {
312 log_message(LOG_WARNING, "can't open %s: %s\n",
313 config_file, strerror(errno));
319 * Allocate a buffer rather than put it on the stack so we can
320 * keep it around to parse any options (any allocated newargv
321 * points into this buffer for its strings).
323 buffer = malloc(MAX_BUFFER_LEN);
325 log_message(LOG_WARNING, "Can't allocate memory.\n");
332 vendor_in = model_in = options_in = NULL;
334 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
338 if (buf[strlen(buffer) - 1] != '\n') {
339 log_message(LOG_WARNING,
340 "Config file line %d too long.\n", lineno);
344 while (isspace(*buf))
349 * blank or all whitespace line
360 dprintf("lineno %d: '%s'\n", lineno, buf);
362 str1 = strsep(&buf, "=");
363 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
364 str1 = get_value(&buf);
371 str1 = strsep(&buf, "=");
372 if (str1 && strcasecmp(str1, "MODEL") == 0) {
373 str1 = get_value(&buf);
379 str1 = strsep(&buf, "=");
383 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
384 str1 = get_value(&buf);
391 dprintf("config file line %d:"
392 " vendor '%s'; model '%s'; options '%s'\n",
393 lineno, vendor_in, model_in, options_in);
395 * Only allow: [vendor=foo[,model=bar]]options=stuff
397 if (!options_in || (!vendor_in && model_in)) {
398 log_message(LOG_WARNING,
399 "Error parsing config file line %d '%s'\n",
404 if (vendor == NULL) {
405 if (vendor_in == NULL) {
406 dprintf("matched global option\n");
409 } else if ((vendor_in && strncmp(vendor, vendor_in,
410 strlen(vendor_in)) == 0) &&
411 (!model_in || (strncmp(model, model_in,
412 strlen(model_in)) == 0))) {
414 * Matched vendor and optionally model.
416 * Note: a short vendor_in or model_in can
417 * give a partial match (that is FOO
420 dprintf("matched vendor/model\n");
423 dprintf("no match\n");
428 if (vendor_in != NULL || model_in != NULL ||
429 options_in != NULL) {
431 * Something matched. Allocate newargv, and store
432 * values found in options_in.
434 strcpy(buffer, options_in);
435 c = argc_count(buffer) + 2;
436 *newargv = calloc(c, sizeof(**newargv));
438 log_message(LOG_WARNING,
439 "Can't allocate memory.\n");
445 * argv[0] at 0 is skipped by getopt, but
446 * store the buffer address there for
449 (*newargv)[c] = buffer;
450 for (c = 1; c < *argc; c++)
451 (*newargv)[c] = strsep(&buffer, " ");
466 static int set_options(int argc, char **argv, const char *short_opts,
467 char *target, char *maj_min_dev)
472 * optind is a global extern used by getopt. Since we can call
473 * set_options twice (once for command line, and once for config
474 * file) we have to reset this back to 1. [Note glibc handles
475 * setting this to 0, but klibc does not.]
479 option = getopt(argc, argv, short_options);
484 dprintf("option '%c' arg '%s'\n", option, optarg);
486 dprintf("option '%c'\n", option);
497 default_callout = optarg;
502 strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
510 strncpy(config_file, optarg, MAX_NAME_LEN);
522 if (strcmp(optarg, "0x80") == 0) {
523 default_page_code = 0x80;
524 } else if (strcmp(optarg, "0x83") == 0) {
525 default_page_code = 0x83;
527 log_message(LOG_WARNING,
528 "Unknown page code '%s'\n", optarg);
535 strncpy(target, sysfs_mnt_path, MAX_NAME_LEN);
536 strncat(target, optarg, MAX_NAME_LEN);
552 log_message(LOG_WARNING, "scsi_id version: %s\n",
558 log_message(LOG_WARNING,
559 "Unknown or bad option '%c' (0x%x)\n",
567 static int per_dev_options(struct sysfs_device *scsi_dev, int *good_bad,
568 int *page_code, char *callout)
572 char **newargv = NULL;
573 struct sysfs_attribute *vendor, *model, *type;
576 *good_bad = all_good;
577 *page_code = default_page_code;
578 if (default_callout && (callout != default_callout))
579 strncpy(callout, default_callout, MAX_NAME_LEN);
583 vendor = sysfs_get_device_attr(scsi_dev, "vendor");
585 log_message(LOG_WARNING, "%s: cannot get vendor attribute\n",
589 set_str(vendor_str, vendor->value, sizeof(vendor_str)-1);
591 model = sysfs_get_device_attr(scsi_dev, "model");
593 log_message(LOG_WARNING, "%s: cannot get model attribute\n",
597 set_str(model_str, model->value, sizeof(model_str)-1);
599 type = sysfs_get_device_attr(scsi_dev, "type");
601 log_message(LOG_WARNING, "%s: cannot get type attribute\n",
605 set_type(type_str, type->value, sizeof(type_str)-1);
607 type = sysfs_get_device_attr(scsi_dev, "rev");
609 log_message(LOG_WARNING, "%s: cannot get type attribute\n",
613 set_str(revision_str, type->value, sizeof(revision_str)-1);
615 retval = get_file_options(vendor->value, model->value, &newargc,
618 optind = 1; /* reset this global extern */
619 while (retval == 0) {
620 option = getopt(newargc, newargv, dev_short_options);
625 dprintf("option '%c' arg '%s'\n", option, optarg);
627 dprintf("option '%c'\n", option);
635 strncpy(callout, default_callout, MAX_NAME_LEN);
643 if (strcmp(optarg, "0x80") == 0) {
645 } else if (strcmp(optarg, "0x83") == 0) {
648 log_message(LOG_WARNING,
649 "Unknown page code '%s'\n", optarg);
655 log_message(LOG_WARNING,
656 "Unknown or bad option '%c' (0x%x)\n",
671 * format_serial: replace to whitespaces by underscores for calling
672 * programs that use the serial for device naming (multipath, Suse
675 static void format_serial(char *serial)
687 * scsi_id: try to get an id, if one is found, printf it to stdout.
688 * returns a value passed to exit() - 0 if printed an id, else 1. This
689 * could be expanded, for example, if we want to report a failure like no
690 * memory etc. return 2, and return 1 for expected cases (like broken
691 * device found) that do not print an id.
693 static int scsi_id(const char *target_path, char *maj_min_dev)
697 char *serial, *unaligned_buf;
698 struct sysfs_class_device *class_dev; /* of target_path */
699 struct sysfs_class_device *class_dev_parent; /* for partitions */
700 struct sysfs_device *scsi_dev; /* the scsi_device */
703 char callout[MAX_NAME_LEN];
705 dprintf("target_path %s\n", target_path);
708 * Ugly: depend on the sysfs path to tell us whether this is a
709 * block or char device. This should probably be encoded in the
710 * "dev" along with the major/minor.
712 if (has_sysfs_prefix(target_path, "/block")) {
714 } else if (has_sysfs_prefix(target_path, "/class")) {
718 log_message(LOG_WARNING,
719 "Non block or class device '%s'\n",
724 * Expected in some cases.
726 dprintf("Non block or class device\n");
731 class_dev = sysfs_open_class_device_path(target_path);
733 log_message(LOG_WARNING, "open class %s failed: %s\n",
734 target_path, strerror(errno));
737 class_dev_parent = sysfs_get_classdev_parent(class_dev);
738 dprintf("class_dev 0x%p; class_dev_parent 0x%p\n", class_dev,
740 if (class_dev_parent) {
741 scsi_dev = sysfs_get_classdev_device(class_dev_parent);
743 scsi_dev = sysfs_get_classdev_device(class_dev);
747 * The close of scsi_dev will close class_dev or class_dev_parent.
751 * We assume we are called after the device is completely ready,
752 * so we don't have to loop here like udev. (And we are usually
757 * errno is not set if we can't find the device link, so
758 * don't print it out here.
760 log_message(LOG_WARNING, "Cannot find sysfs device associated with %s\n",
767 * Allow only scsi devices.
769 * Other block devices can support SG IO, but only ide-cd does, so
770 * for now, don't bother with anything else.
772 if (strcmp(scsi_dev->bus, "scsi") != 0) {
775 * Expected in some cases.
777 dprintf("%s is not a scsi device\n", target_path);
779 log_message(LOG_WARNING, "%s is not a scsi device\n",
785 * mknod a temp dev to communicate with the device.
787 if (!dev_specified && create_tmp_dev(class_dev, maj_min_dev,
789 dprintf("create_tmp_dev failed\n");
794 * Get any per device (vendor + model) options from the config
797 retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout);
798 dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n",
799 good_dev, page_code, callout);
802 unaligned_buf = malloc(MAX_SERIAL_LEN + ALIGN);
803 serial = (char*) (((unsigned long) unaligned_buf + (ALIGN - 1))
805 dprintf("buffer unaligned 0x%p; aligned 0x%p\n", unaligned_buf, serial);
810 } else if (callout[0] != '\0') {
812 * XXX Disabled for now ('c' is not in any options[]).
815 } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code,
816 serial, MAX_SERIAL_LEN)) {
817 retval = always_info?0:1;
823 static char serial_str[64];
824 printf("ID_VENDOR=%s\n", vendor_str);
825 printf("ID_MODEL=%s\n", model_str);
826 printf("ID_REVISION=%s\n", revision_str);
827 set_str(serial_str, serial, sizeof(serial_str));
828 printf("ID_SERIAL=%s\n", serial_str);
829 printf("ID_TYPE=%s\n", type_str);
830 printf("ID_BUS=scsi\n");
833 format_serial(serial);
835 printf("%s: ", scsi_dev->name);
836 printf("%s\n", serial);
838 dprintf("%s\n", serial);
841 sysfs_close_device(scsi_dev);
849 int main(int argc, char **argv)
853 char target_path[MAX_NAME_LEN];
854 char maj_min_dev[MAX_NAME_LEN];
861 dprintf("argc is %d\n", argc);
862 if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
863 log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n",
868 devpath = getenv("DEVPATH");
871 * This implies that we were invoked via udev or hotplug.
875 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
876 strncat(target_path, devpath, MAX_NAME_LEN);
880 * Get config file options.
883 retval = get_file_options(NULL, NULL, &newargc, &newargv);
886 } else if (newargv && (retval == 0)) {
887 if (set_options(newargc, newargv, short_options, target_path,
893 * Get command line options (overriding any config file or DEVPATH
896 if (set_options(argc, argv, short_options, target_path,
900 if (!sys_specified) {
901 log_message(LOG_WARNING, "-s must be specified\n");
905 retval = scsi_id(target_path, maj_min_dev);