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>
37 #include "scsi_id_version.h"
40 #ifndef SCSI_ID_VERSION
42 #define SCSI_ID_VERSION "unknown"
46 * temporary names for mknod.
48 #define TMP_DIR "/tmp"
49 #define TMP_PREFIX "scsi"
51 static const char short_options[] = "bc:d:ef:gip:s:vV";
53 * Just duplicate per dev options.
55 static const char dev_short_options[] = "bc:gp:";
57 char sysfs_mnt_path[SYSFS_PATH_MAX];
60 static char *default_callout;
61 static int dev_specified;
62 static int sys_specified;
63 static char config_file[MAX_NAME_LEN] = SCSI_ID_CONFIG_FILE;
64 static int display_bus_id;
65 static int default_page_code;
66 static int use_stderr;
68 static int hotplug_mode;
70 void log_message (int level, const char *format, ...)
74 if (!debug && level == LOG_DEBUG)
77 va_start (args, format);
78 if (!hotplug_mode || use_stderr) {
79 vfprintf(stderr, format, args);
81 static int logging_init = 0;
82 static unsigned char logname[32];
85 * klibc does not have LOG_PID.
87 snprintf(logname, 32, "scsi_id[%d]", getpid());
88 openlog (logname, 0, LOG_DAEMON);
92 vsyslog(level, format, args);
98 int sysfs_get_attr(const char *devpath, const char *attr, char *value,
101 char attr_path[SYSFS_PATH_MAX];
103 strncpy(attr_path, devpath, SYSFS_PATH_MAX);
104 strncat(attr_path, "/", SYSFS_PATH_MAX);
105 strncat(attr_path, attr, SYSFS_PATH_MAX);
106 dprintf("%s\n", attr_path);
107 return sysfs_read_attribute_value(attr_path, value, SYSFS_NAME_LEN);
110 static int sysfs_get_actual_dev(const char *sysfs_path, char *dev, int len)
112 dprintf("%s\n", sysfs_path);
113 strncpy(dev, sysfs_path, len);
114 strncat(dev, "/device", len);
115 if (sysfs_get_link(dev, dev, len)) {
117 log_message(LOG_WARNING, "%s: %s\n", dev,
125 * sysfs_is_bus: Given the sysfs_path to a device, return 1 if sysfs_path
126 * is on bus, 0 if not on bus, and < 0 on error
128 static int sysfs_is_bus(const char *sysfs_path, const char *bus)
130 char bus_dev_name[SYSFS_PATH_MAX];
131 char bus_id[SYSFS_NAME_LEN];
132 struct stat stat_buf;
135 dprintf("%s\n", sysfs_path);
137 if (sysfs_get_name_from_path(sysfs_path, bus_id, SYSFS_NAME_LEN))
140 snprintf(bus_dev_name, MAX_NAME_LEN, "%s/%s/%s/%s/%s", sysfs_mnt_path,
141 SYSFS_BUS_NAME, bus, SYSFS_DEVICES_NAME, bus_id);
143 if (stat(sysfs_path, &stat_buf))
145 dev_inode = stat_buf.st_ino;
147 if (stat(bus_dev_name, &stat_buf)) {
153 if (dev_inode == stat_buf.st_ino)
159 static int get_major_minor(const char *devpath, int *major, int *minor)
161 char dev_value[MAX_ATTR_LEN];
163 if (sysfs_get_attr(devpath, "dev", dev_value, MAX_ATTR_LEN)) {
165 * XXX This happens a lot, since sg has no dev attr.
166 * And now sysfsutils does not set a meaningful errno
167 * value. Someday change this back to a LOG_WARNING.
168 * And if sysfsutils changes, check for ENOENT and handle
171 log_message(LOG_DEBUG, "%s could not get dev attribute: %s\n",
172 devpath, strerror(errno));
176 dprintf("dev value %s", dev_value); /* dev_value has a trailing \n */
177 if (sscanf(dev_value, "%u:%u", major, minor) != 2) {
178 log_message(LOG_WARNING, "%s: invalid dev major/minor\n",
186 static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
190 dprintf("(%s)\n", devpath);
192 if (get_major_minor(devpath, &major, &minor))
194 snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
195 TMP_DIR, TMP_PREFIX, major, minor, getpid());
197 dprintf("tmpdev '%s'\n", tmpdev);
199 if (mknod(tmpdev, 0600 | dev_type, makedev(major, minor))) {
200 log_message(LOG_WARNING, "mknod failed: %s\n", strerror(errno));
206 static int has_sysfs_prefix(const char *path, const char *prefix)
208 char match[MAX_NAME_LEN];
210 strncpy(match, sysfs_mnt_path, MAX_NAME_LEN);
211 strncat(match, prefix, MAX_NAME_LEN);
212 if (strncmp(path, match, strlen(match)) == 0)
221 * buf points to an '=' followed by a quoted string ("foo") or a string ending
222 * with a space or ','.
224 * Return a pointer to the NUL terminated string, returns NULL if no
227 static char *get_value(char **buffer)
229 static char *quote_string = "\"\n";
230 static char *comma_string = ",\n";
234 if (**buffer == '"') {
236 * skip leading quote, terminate when quote seen
243 val = strsep(buffer, end);
244 if (val && end == quote_string)
246 * skip trailing quote
250 while (isspace(**buffer))
256 static int argc_count(char *opts)
259 while (*opts != '\0')
268 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
269 * if vendor and model are set find the first OPTIONS line in the config
270 * file that matches. Set argc and argv to match the OPTIONS string.
272 * vendor and model can end in '\n'.
274 static int get_file_options(char *vendor, char *model, int *argc,
281 char *vendor_in, *model_in, *options_in; /* read in from file */
286 dprintf("vendor='%s'; model='%s'\n", vendor, model);
287 fd = fopen(config_file, "r");
289 dprintf("can't open %s\n", config_file);
290 if (errno == ENOENT) {
293 log_message(LOG_WARNING, "can't open %s: %s\n",
294 config_file, strerror(errno));
300 * Allocate a buffer rather than put it on the stack so we can
301 * keep it around to parse any options (any allocated newargv
302 * points into this buffer for its strings).
304 buffer = malloc(MAX_BUFFER_LEN);
306 log_message(LOG_WARNING, "Can't allocate memory.\n");
313 vendor_in = model_in = options_in = NULL;
315 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
319 if (buf[strlen(buffer) - 1] != '\n') {
320 log_message(LOG_WARNING,
321 "Config file line %d too long.\n", lineno);
325 while (isspace(*buf))
330 * blank or all whitespace line
341 dprintf("lineno %d: '%s'\n", lineno, buf);
343 str1 = strsep(&buf, "=");
344 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
345 str1 = get_value(&buf);
352 str1 = strsep(&buf, "=");
353 if (str1 && strcasecmp(str1, "MODEL") == 0) {
354 str1 = get_value(&buf);
360 str1 = strsep(&buf, "=");
364 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
365 str1 = get_value(&buf);
372 dprintf("config file line %d:"
373 " vendor '%s'; model '%s'; options '%s'\n",
374 lineno, vendor_in, model_in, options_in);
376 * Only allow: [vendor=foo[,model=bar]]options=stuff
378 if (!options_in || (!vendor_in && model_in)) {
379 log_message(LOG_WARNING,
380 "Error parsing config file line %d '%s'\n",
385 if (vendor == NULL) {
386 if (vendor_in == NULL) {
387 dprintf("matched global option\n");
390 } else if ((vendor_in && strncmp(vendor, vendor_in,
391 strlen(vendor_in)) == 0) &&
392 (!model_in || (strncmp(model, model_in,
393 strlen(model_in)) == 0))) {
395 * Matched vendor and optionally model.
397 * Note: a short vendor_in or model_in can
398 * give a partial match (that is FOO
401 dprintf("matched vendor/model\n");
404 dprintf("no match\n");
409 if (vendor_in != NULL || model_in != NULL ||
410 options_in != NULL) {
412 * Something matched. Allocate newargv, and store
413 * values found in options_in.
415 strcpy(buffer, options_in);
416 c = argc_count(buffer) + 2;
417 *newargv = calloc(c, sizeof(**newargv));
419 log_message(LOG_WARNING,
420 "Can't allocate memory.\n");
426 * argv[0] at 0 is skipped by getopt, but
427 * store the buffer address there for
430 (*newargv)[c] = buffer;
431 for (c = 1; c < *argc; c++)
432 (*newargv)[c] = strsep(&buffer, " ");
447 static int set_options(int argc, char **argv, const char *short_opts,
448 char *target, char *maj_min_dev)
453 * optind is a global extern used by getopt. Since we can call
454 * set_options twice (once for command line, and once for config
455 * file) we have to reset this back to 1. [Note glibc handles
456 * setting this to 0, but klibc does not.]
460 option = getopt(argc, argv, short_options);
465 dprintf("option '%c' arg '%s'\n", option, optarg);
467 dprintf("option '%c'\n", option);
475 default_callout = optarg;
480 strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
488 strncpy(config_file, optarg, MAX_NAME_LEN);
500 if (strcmp(optarg, "0x80") == 0) {
501 default_page_code = 0x80;
502 } else if (strcmp(optarg, "0x83") == 0) {
503 default_page_code = 0x83;
505 log_message(LOG_WARNING,
506 "Unknown page code '%s'\n", optarg);
513 strncpy(target, sysfs_mnt_path, MAX_NAME_LEN);
514 strncat(target, optarg, MAX_NAME_LEN);
522 log_message(LOG_WARNING, "scsi_id version: %s\n",
528 log_message(LOG_WARNING,
529 "Unknown or bad option '%c' (0x%x)\n",
537 static int per_dev_options(struct sysfs_class_device *scsi_dev, int *good_bad,
538 int *page_code, char *callout)
542 char **newargv = NULL;
543 char vendor[MAX_ATTR_LEN];
544 char model[MAX_ATTR_LEN];
547 *good_bad = all_good;
548 *page_code = default_page_code;
549 if (default_callout && (callout != default_callout))
550 strncpy(callout, default_callout, MAX_NAME_LEN);
554 if (sysfs_get_attr(scsi_dev->path, "vendor", vendor, MAX_ATTR_LEN)) {
555 log_message(LOG_WARNING, "%s: cannot get vendor attribute\n",
560 if (sysfs_get_attr(scsi_dev->path, "model", model, MAX_ATTR_LEN)) {
561 log_message(LOG_WARNING, "%s: cannot get model attribute\n",
566 retval = get_file_options(vendor, model, &newargc, &newargv);
568 optind = 1; /* reset this global extern */
569 while (retval == 0) {
570 option = getopt(newargc, newargv, dev_short_options);
575 dprintf("option '%c' arg '%s'\n", option, optarg);
577 dprintf("option '%c'\n", option);
585 strncpy(callout, default_callout, MAX_NAME_LEN);
593 if (strcmp(optarg, "0x80") == 0) {
595 } else if (strcmp(optarg, "0x83") == 0) {
598 log_message(LOG_WARNING,
599 "Unknown page code '%s'\n", optarg);
605 log_message(LOG_WARNING,
606 "Unknown or bad option '%c' (0x%x)\n",
621 * scsi_id: try to get an id, if one is found, printf it to stdout.
622 * returns a value passed to exit() - 0 if printed an id, else 1. This
623 * could be expanded, for example, if we want to report a failure like no
624 * memory etc. return 2, and return 1 for expected cases (like broken
625 * device found) that do not print an id.
627 static int scsi_id(const char *target_path, char *maj_min_dev)
631 char full_dev_path[MAX_NAME_LEN];
632 char serial[MAX_SERIAL_LEN];
633 struct sysfs_class_device *scsi_dev; /* of scsi_device full_dev_path */
636 char callout[MAX_NAME_LEN];
638 dprintf("target_path %s\n", target_path);
641 * Ugly: depend on the sysfs path to tell us whether this is a
642 * block or char device. This should probably be encoded in the
643 * "dev" along with the major/minor.
645 if (has_sysfs_prefix(target_path, "/block")) {
647 } else if (has_sysfs_prefix(target_path, "/class")) {
651 log_message(LOG_WARNING,
652 "Non block or class device '%s'\n",
657 * Expected in some cases.
659 dprintf("Non block or class device\n");
664 if (sysfs_get_actual_dev(target_path, full_dev_path, MAX_NAME_LEN))
667 dprintf("full_dev_path %s\n", full_dev_path);
670 * Allow only scsi devices (those that have a matching device
671 * under /bus/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 retval = sysfs_is_bus(full_dev_path, "scsi");
680 * Expected in some cases.
682 dprintf("%s is not a scsi device\n", target_path);
684 log_message(LOG_WARNING, "%s is not a scsi device\n",
687 } else if (retval < 0) {
688 log_message(LOG_WARNING, "sysfs_is_bus failed: %s\n",
694 * mknod a temp dev to communicate with the device.
696 if (!dev_specified && create_tmp_dev(target_path, maj_min_dev,
698 dprintf("create_tmp_dev failed\n");
702 scsi_dev = sysfs_open_class_device_path(full_dev_path);
704 log_message(LOG_WARNING, "open class %s failed: %s\n",
705 full_dev_path, strerror(errno));
710 * Get any per device (vendor + model) options from the config
713 retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout);
714 dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n",
715 good_dev, page_code, callout);
719 } else if (callout[0] != '\0') {
721 * exec vendor callout, pass it only the "name" to be used
722 * for error messages, and the dev to open.
724 * This won't work if we need to pass on the original
725 * command line (when not hotplug mode) since the option
726 * parsing and per dev parsing modify the argv's.
728 * XXX Not implemented yet. And not fully tested ;-)
731 } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code,
732 serial, MAX_SERIAL_LEN)) {
739 printf("%s ", scsi_dev->name);
740 printf("%s", serial);
743 dprintf("%s\n", serial);
746 sysfs_close_class_device(scsi_dev);
754 int main(int argc, char **argv)
758 char target_path[MAX_NAME_LEN];
759 char maj_min_dev[MAX_NAME_LEN];
766 if ((argc == 2) && (argv[1][0] != '-')) {
768 dprintf("hotplug assumed\n");
771 dprintf("argc is %d\n", argc);
772 if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
773 log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n",
780 * There is a kernel race creating attributes, if called
781 * directly, uncomment the sleep.
785 devpath = getenv("DEVPATH");
787 log_message(LOG_WARNING, "DEVPATH is not set\n");
792 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
793 strncat(target_path, devpath, MAX_NAME_LEN);
795 if (set_options(argc, argv, short_options, target_path,
801 * Override any command line options set via the config file. This
802 * is the only way to set options when in hotplug mode.
805 retval = get_file_options(NULL, NULL, &newargc, &newargv);
808 } else if (newargv && (retval == 0)) {
809 if (set_options(newargc, newargv, short_options, target_path,
815 if (!sys_specified) {
816 log_message(LOG_WARNING, "-s must be specified\n");
820 retval = scsi_id(target_path, maj_min_dev);