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:vV";
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;
74 void log_message (int level, const char *format, ...)
78 if (!debug && level == LOG_DEBUG)
81 va_start (args, format);
82 if (!hotplug_mode || use_stderr) {
83 vfprintf(stderr, format, args);
85 static int logging_init = 0;
86 static unsigned char logname[32];
89 * klibc does not have LOG_PID.
91 snprintf(logname, 32, "scsi_id[%d]", getpid());
92 openlog (logname, 0, LOG_DAEMON);
96 vsyslog(level, format, args);
102 int sysfs_get_attr(const char *devpath, const char *attr, char *value,
105 char attr_path[SYSFS_PATH_MAX];
107 strncpy(attr_path, devpath, SYSFS_PATH_MAX);
108 strncat(attr_path, "/", SYSFS_PATH_MAX);
109 strncat(attr_path, attr, SYSFS_PATH_MAX);
110 dprintf("%s\n", attr_path);
111 return sysfs_read_attribute_value(attr_path, value, SYSFS_NAME_LEN);
114 static int get_major_minor(const char *devpath, int *major, int *minor)
116 char dev_value[MAX_ATTR_LEN];
118 if (sysfs_get_attr(devpath, "dev", dev_value, MAX_ATTR_LEN)) {
120 * XXX This happens a lot, since sg has no dev attr.
121 * And now sysfsutils does not set a meaningful errno
122 * value. Someday change this back to a LOG_WARNING.
123 * And if sysfsutils changes, check for ENOENT and handle
126 log_message(LOG_DEBUG, "%s: could not get dev attribute: %s\n",
127 devpath, strerror(errno));
131 dprintf("dev value %s", dev_value); /* dev_value has a trailing \n */
132 if (sscanf(dev_value, "%u:%u", major, minor) != 2) {
133 log_message(LOG_WARNING, "%s: invalid dev major/minor\n",
141 static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
145 dprintf("(%s)\n", devpath);
147 if (get_major_minor(devpath, &major, &minor))
149 snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
150 TMP_DIR, TMP_PREFIX, major, minor, getpid());
152 dprintf("tmpdev '%s'\n", tmpdev);
154 if (mknod(tmpdev, 0600 | dev_type, makedev(major, minor))) {
155 log_message(LOG_WARNING, "mknod failed: %s\n", strerror(errno));
161 static int has_sysfs_prefix(const char *path, const char *prefix)
163 char match[MAX_NAME_LEN];
165 strncpy(match, sysfs_mnt_path, MAX_NAME_LEN);
166 strncat(match, prefix, MAX_NAME_LEN);
167 if (strncmp(path, match, strlen(match)) == 0)
176 * buf points to an '=' followed by a quoted string ("foo") or a string ending
177 * with a space or ','.
179 * Return a pointer to the NUL terminated string, returns NULL if no
182 static char *get_value(char **buffer)
184 static char *quote_string = "\"\n";
185 static char *comma_string = ",\n";
189 if (**buffer == '"') {
191 * skip leading quote, terminate when quote seen
198 val = strsep(buffer, end);
199 if (val && end == quote_string)
201 * skip trailing quote
205 while (isspace(**buffer))
211 static int argc_count(char *opts)
214 while (*opts != '\0')
223 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
224 * if vendor and model are set find the first OPTIONS line in the config
225 * file that matches. Set argc and argv to match the OPTIONS string.
227 * vendor and model can end in '\n'.
229 static int get_file_options(char *vendor, char *model, int *argc,
236 char *vendor_in, *model_in, *options_in; /* read in from file */
241 dprintf("vendor='%s'; model='%s'\n", vendor, model);
242 fd = fopen(config_file, "r");
244 dprintf("can't open %s\n", config_file);
245 if (errno == ENOENT) {
248 log_message(LOG_WARNING, "can't open %s: %s\n",
249 config_file, strerror(errno));
255 * Allocate a buffer rather than put it on the stack so we can
256 * keep it around to parse any options (any allocated newargv
257 * points into this buffer for its strings).
259 buffer = malloc(MAX_BUFFER_LEN);
261 log_message(LOG_WARNING, "Can't allocate memory.\n");
268 vendor_in = model_in = options_in = NULL;
270 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
274 if (buf[strlen(buffer) - 1] != '\n') {
275 log_message(LOG_WARNING,
276 "Config file line %d too long.\n", lineno);
280 while (isspace(*buf))
285 * blank or all whitespace line
296 dprintf("lineno %d: '%s'\n", lineno, buf);
298 str1 = strsep(&buf, "=");
299 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
300 str1 = get_value(&buf);
307 str1 = strsep(&buf, "=");
308 if (str1 && strcasecmp(str1, "MODEL") == 0) {
309 str1 = get_value(&buf);
315 str1 = strsep(&buf, "=");
319 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
320 str1 = get_value(&buf);
327 dprintf("config file line %d:"
328 " vendor '%s'; model '%s'; options '%s'\n",
329 lineno, vendor_in, model_in, options_in);
331 * Only allow: [vendor=foo[,model=bar]]options=stuff
333 if (!options_in || (!vendor_in && model_in)) {
334 log_message(LOG_WARNING,
335 "Error parsing config file line %d '%s'\n",
340 if (vendor == NULL) {
341 if (vendor_in == NULL) {
342 dprintf("matched global option\n");
345 } else if ((vendor_in && strncmp(vendor, vendor_in,
346 strlen(vendor_in)) == 0) &&
347 (!model_in || (strncmp(model, model_in,
348 strlen(model_in)) == 0))) {
350 * Matched vendor and optionally model.
352 * Note: a short vendor_in or model_in can
353 * give a partial match (that is FOO
356 dprintf("matched vendor/model\n");
359 dprintf("no match\n");
364 if (vendor_in != NULL || model_in != NULL ||
365 options_in != NULL) {
367 * Something matched. Allocate newargv, and store
368 * values found in options_in.
370 strcpy(buffer, options_in);
371 c = argc_count(buffer) + 2;
372 *newargv = calloc(c, sizeof(**newargv));
374 log_message(LOG_WARNING,
375 "Can't allocate memory.\n");
381 * argv[0] at 0 is skipped by getopt, but
382 * store the buffer address there for
385 (*newargv)[c] = buffer;
386 for (c = 1; c < *argc; c++)
387 (*newargv)[c] = strsep(&buffer, " ");
402 static int set_options(int argc, char **argv, const char *short_opts,
403 char *target, char *maj_min_dev)
408 * optind is a global extern used by getopt. Since we can call
409 * set_options twice (once for command line, and once for config
410 * file) we have to reset this back to 1. [Note glibc handles
411 * setting this to 0, but klibc does not.]
415 option = getopt(argc, argv, short_options);
420 dprintf("option '%c' arg '%s'\n", option, optarg);
422 dprintf("option '%c'\n", option);
430 default_callout = optarg;
435 strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
443 strncpy(config_file, optarg, MAX_NAME_LEN);
455 if (strcmp(optarg, "0x80") == 0) {
456 default_page_code = 0x80;
457 } else if (strcmp(optarg, "0x83") == 0) {
458 default_page_code = 0x83;
460 log_message(LOG_WARNING,
461 "Unknown page code '%s'\n", optarg);
468 strncpy(target, sysfs_mnt_path, MAX_NAME_LEN);
469 strncat(target, optarg, MAX_NAME_LEN);
477 log_message(LOG_WARNING, "scsi_id version: %s\n",
483 log_message(LOG_WARNING,
484 "Unknown or bad option '%c' (0x%x)\n",
492 static int per_dev_options(struct sysfs_device *scsi_dev, int *good_bad,
493 int *page_code, char *callout)
497 char **newargv = NULL;
498 char vendor[MAX_ATTR_LEN];
499 char model[MAX_ATTR_LEN];
502 *good_bad = all_good;
503 *page_code = default_page_code;
504 if (default_callout && (callout != default_callout))
505 strncpy(callout, default_callout, MAX_NAME_LEN);
509 if (sysfs_get_attr(scsi_dev->path, "vendor", vendor, MAX_ATTR_LEN)) {
510 log_message(LOG_WARNING, "%s: cannot get vendor attribute\n",
515 if (sysfs_get_attr(scsi_dev->path, "model", model, MAX_ATTR_LEN)) {
516 log_message(LOG_WARNING, "%s: cannot get model attribute\n",
521 retval = get_file_options(vendor, model, &newargc, &newargv);
523 optind = 1; /* reset this global extern */
524 while (retval == 0) {
525 option = getopt(newargc, newargv, dev_short_options);
530 dprintf("option '%c' arg '%s'\n", option, optarg);
532 dprintf("option '%c'\n", option);
540 strncpy(callout, default_callout, MAX_NAME_LEN);
548 if (strcmp(optarg, "0x80") == 0) {
550 } else if (strcmp(optarg, "0x83") == 0) {
553 log_message(LOG_WARNING,
554 "Unknown page code '%s'\n", optarg);
560 log_message(LOG_WARNING,
561 "Unknown or bad option '%c' (0x%x)\n",
576 * scsi_id: try to get an id, if one is found, printf it to stdout.
577 * returns a value passed to exit() - 0 if printed an id, else 1. This
578 * could be expanded, for example, if we want to report a failure like no
579 * memory etc. return 2, and return 1 for expected cases (like broken
580 * device found) that do not print an id.
582 static int scsi_id(const char *target_path, char *maj_min_dev)
586 char serial[MAX_SERIAL_LEN];
587 struct sysfs_class_device *class_dev; /* of target_path */
588 struct sysfs_class_device *class_dev_parent; /* for partitions */
589 struct sysfs_device *scsi_dev; /* the scsi_device */
592 char callout[MAX_NAME_LEN];
594 dprintf("target_path %s\n", target_path);
597 * Ugly: depend on the sysfs path to tell us whether this is a
598 * block or char device. This should probably be encoded in the
599 * "dev" along with the major/minor.
601 if (has_sysfs_prefix(target_path, "/block")) {
603 } else if (has_sysfs_prefix(target_path, "/class")) {
607 log_message(LOG_WARNING,
608 "Non block or class device '%s'\n",
613 * Expected in some cases.
615 dprintf("Non block or class device\n");
620 class_dev = sysfs_open_class_device_path(target_path);
622 log_message(LOG_WARNING, "open class %s failed: %s\n",
623 target_path, strerror(errno));
626 class_dev_parent = sysfs_get_classdev_parent(class_dev);
627 dprintf("class_dev 0x%p; class_dev_parent 0x%p\n", class_dev,
629 if (class_dev_parent) {
630 scsi_dev = sysfs_get_classdev_device(class_dev_parent);
632 scsi_dev = sysfs_get_classdev_device(class_dev);
636 * The close of scsi_dev will close class_dev or class_dev_parent.
640 * We assume we are called after the device is completely ready,
641 * so we don't have to loop here like udev. (And we are usually
646 * errno is not set if we can't find the device link, so
647 * don't print it out here.
649 log_message(LOG_WARNING, "Cannot find sysfs device associated with %s\n",
656 * Allow only scsi devices.
658 * Other block devices can support SG IO, but only ide-cd does, so
659 * for now, don't bother with anything else.
661 if (strcmp(scsi_dev->bus, "scsi") != 0) {
664 * Expected in some cases.
666 dprintf("%s is not a scsi device\n", target_path);
668 log_message(LOG_WARNING, "%s is not a scsi device\n",
674 * mknod a temp dev to communicate with the device.
676 * XXX pass down class_dev or class_dev_parent.
678 if (!dev_specified && create_tmp_dev(target_path, maj_min_dev,
680 dprintf("create_tmp_dev failed\n");
685 * Get any per device (vendor + model) options from the config
688 retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout);
689 dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n",
690 good_dev, page_code, callout);
694 } else if (callout[0] != '\0') {
696 * XXX Disabled for now ('c' is not in any options[]).
699 } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code,
700 serial, MAX_SERIAL_LEN)) {
707 printf("%s: ", scsi_dev->name);
708 printf("%s", serial);
711 dprintf("%s\n", serial);
714 sysfs_close_device(scsi_dev);
722 int main(int argc, char **argv)
726 char target_path[MAX_NAME_LEN];
727 char maj_min_dev[MAX_NAME_LEN];
734 if ((argc == 2) && (argv[1][0] != '-')) {
736 dprintf("hotplug assumed\n");
739 dprintf("argc is %d\n", argc);
740 if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
741 log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n",
748 * There is a kernel race creating attributes, if called
749 * directly, uncomment the sleep.
753 devpath = getenv("DEVPATH");
755 log_message(LOG_WARNING, "DEVPATH is not set\n");
760 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
761 strncat(target_path, devpath, MAX_NAME_LEN);
763 if (set_options(argc, argv, short_options, target_path,
769 * Override any command line options set via the config file. This
770 * is the only way to set options when in hotplug mode.
773 retval = get_file_options(NULL, NULL, &newargc, &newargv);
776 } else if (newargv && (retval == 0)) {
777 if (set_options(newargc, newargv, short_options, target_path,
783 if (!sys_specified) {
784 log_message(LOG_WARNING, "-s must be specified\n");
788 retval = scsi_id(target_path, maj_min_dev);