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
37 * Assume built under udev with KLIBC
41 #include <sysfs/libsysfs.h>
43 #include "scsi_id_version.h"
46 #ifndef SCSI_ID_VERSION
48 #define SCSI_ID_VERSION "unknown"
52 * temporary names for mknod.
54 #define TMP_DIR "/tmp"
55 #define TMP_PREFIX "scsi"
57 static const char short_options[] = "bc:d:ef:gip:s:vV";
59 * Just duplicate per dev options.
61 static const char dev_short_options[] = "bc:gp:";
63 char sysfs_mnt_path[SYSFS_PATH_MAX];
66 static char *default_callout;
67 static int dev_specified;
68 static int sys_specified;
69 static char config_file[MAX_NAME_LEN] = SCSI_ID_CONFIG_FILE;
70 static int display_bus_id;
71 static int default_page_code;
72 static int use_stderr;
74 static int hotplug_mode;
76 void log_message (int level, const char *format, ...)
80 if (!debug && level == LOG_DEBUG)
83 va_start (args, format);
84 if (!hotplug_mode || use_stderr) {
85 vfprintf(stderr, format, args);
87 static int logging_init = 0;
88 static unsigned char logname[32];
91 * klibc does not have LOG_PID.
93 snprintf(logname, 32, "scsi_id[%d]", getpid());
94 openlog (logname, 0, LOG_DAEMON);
98 vsyslog(level, format, args);
104 int sysfs_get_attr(const char *devpath, const char *attr, char *value,
107 char attr_path[SYSFS_PATH_MAX];
109 strncpy(attr_path, devpath, SYSFS_PATH_MAX);
110 strncat(attr_path, "/", SYSFS_PATH_MAX);
111 strncat(attr_path, attr, SYSFS_PATH_MAX);
112 dprintf("%s\n", attr_path);
113 return sysfs_read_attribute_value(attr_path, value, SYSFS_NAME_LEN);
116 static int sysfs_get_actual_dev(const char *sysfs_path, char *dev, int len)
118 dprintf("%s\n", sysfs_path);
119 strncpy(dev, sysfs_path, len);
120 strncat(dev, "/device", len);
121 if (sysfs_get_link(dev, dev, len)) {
123 log_message(LOG_WARNING, "%s: %s\n", dev,
131 * sysfs_is_bus: Given the sysfs_path to a device, return 1 if sysfs_path
132 * is on bus, 0 if not on bus, and < 0 on error
134 static int sysfs_is_bus(const char *sysfs_path, const char *bus)
136 char bus_dev_name[SYSFS_PATH_MAX];
137 char bus_id[SYSFS_NAME_LEN];
138 struct stat stat_buf;
141 dprintf("%s\n", sysfs_path);
143 if (sysfs_get_name_from_path(sysfs_path, bus_id, SYSFS_NAME_LEN))
146 snprintf(bus_dev_name, MAX_NAME_LEN, "%s/%s/%s/%s/%s", sysfs_mnt_path,
147 SYSFS_BUS_NAME, bus, SYSFS_DEVICES_NAME, bus_id);
149 if (stat(sysfs_path, &stat_buf))
151 dev_inode = stat_buf.st_ino;
153 if (stat(bus_dev_name, &stat_buf)) {
159 if (dev_inode == stat_buf.st_ino)
165 static int get_major_minor(const char *devpath, int *major, int *minor)
167 char dev_value[MAX_ATTR_LEN];
169 if (sysfs_get_attr(devpath, "dev", dev_value, MAX_ATTR_LEN)) {
171 * XXX This happens a lot, since sg has no dev attr.
172 * And now sysfsutils does not set a meaningful errno
173 * value. Someday change this back to a LOG_WARNING.
174 * And if sysfsutils changes, check for ENOENT and handle
177 log_message(LOG_DEBUG, "%s could not get dev attribute: %s\n",
178 devpath, strerror(errno));
182 dprintf("dev value %s", dev_value); /* dev_value has a trailing \n */
183 if (sscanf(dev_value, "%u:%u", major, minor) != 2) {
184 log_message(LOG_WARNING, "%s: invalid dev major/minor\n",
192 static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
196 dprintf("(%s)\n", devpath);
198 if (get_major_minor(devpath, &major, &minor))
200 snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
201 TMP_DIR, TMP_PREFIX, major, minor, getpid());
203 dprintf("tmpdev '%s'\n", tmpdev);
205 if (mknod(tmpdev, 0600 | dev_type, makedev(major, minor))) {
206 log_message(LOG_WARNING, "mknod failed: %s\n", strerror(errno));
212 static int has_sysfs_prefix(const char *path, const char *prefix)
214 char match[MAX_NAME_LEN];
216 strncpy(match, sysfs_mnt_path, MAX_NAME_LEN);
217 strncat(match, prefix, MAX_NAME_LEN);
218 if (strncmp(path, match, strlen(match)) == 0)
227 * buf points to an '=' followed by a quoted string ("foo") or a string ending
228 * with a space or ','.
230 * Return a pointer to the NUL terminated string, returns NULL if no
233 static char *get_value(char **buffer)
235 static char *quote_string = "\"\n";
236 static char *comma_string = ",\n";
240 if (**buffer == '"') {
242 * skip leading quote, terminate when quote seen
249 val = strsep(buffer, end);
250 if (val && end == quote_string)
252 * skip trailing quote
256 while (isspace(**buffer))
262 static int argc_count(char *opts)
265 while (*opts != '\0')
274 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
275 * if vendor and model are set find the first OPTIONS line in the config
276 * file that matches. Set argc and argv to match the OPTIONS string.
278 * vendor and model can end in '\n'.
280 static int get_file_options(char *vendor, char *model, int *argc,
287 char *vendor_in, *model_in, *options_in; /* read in from file */
292 dprintf("vendor='%s'; model='%s'\n", vendor, model);
293 fd = fopen(config_file, "r");
295 dprintf("can't open %s\n", config_file);
296 if (errno == ENOENT) {
299 log_message(LOG_WARNING, "can't open %s: %s\n",
300 config_file, strerror(errno));
306 * Allocate a buffer rather than put it on the stack so we can
307 * keep it around to parse any options (any allocated newargv
308 * points into this buffer for its strings).
310 buffer = malloc(MAX_BUFFER_LEN);
312 log_message(LOG_WARNING, "Can't allocate memory.\n");
319 vendor_in = model_in = options_in = NULL;
321 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
325 if (buf[strlen(buffer) - 1] != '\n') {
326 log_message(LOG_WARNING,
327 "Config file line %d too long.\n", lineno);
331 while (isspace(*buf))
336 * blank or all whitespace line
347 dprintf("lineno %d: '%s'\n", lineno, buf);
349 str1 = strsep(&buf, "=");
350 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
351 str1 = get_value(&buf);
358 str1 = strsep(&buf, "=");
359 if (str1 && strcasecmp(str1, "MODEL") == 0) {
360 str1 = get_value(&buf);
366 str1 = strsep(&buf, "=");
370 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
371 str1 = get_value(&buf);
378 dprintf("config file line %d:"
379 " vendor '%s'; model '%s'; options '%s'\n",
380 lineno, vendor_in, model_in, options_in);
382 * Only allow: [vendor=foo[,model=bar]]options=stuff
384 if (!options_in || (!vendor_in && model_in)) {
385 log_message(LOG_WARNING,
386 "Error parsing config file line %d '%s'\n",
391 if (vendor == NULL) {
392 if (vendor_in == NULL) {
393 dprintf("matched global option\n");
396 } else if ((vendor_in && strncmp(vendor, vendor_in,
397 strlen(vendor_in)) == 0) &&
398 (!model_in || (strncmp(model, model_in,
399 strlen(model_in)) == 0))) {
401 * Matched vendor and optionally model.
403 * Note: a short vendor_in or model_in can
404 * give a partial match (that is FOO
407 dprintf("matched vendor/model\n");
410 dprintf("no match\n");
415 if (vendor_in != NULL || model_in != NULL ||
416 options_in != NULL) {
418 * Something matched. Allocate newargv, and store
419 * values found in options_in.
421 strcpy(buffer, options_in);
422 c = argc_count(buffer) + 2;
423 *newargv = calloc(c, sizeof(**newargv));
425 log_message(LOG_WARNING,
426 "Can't allocate memory.\n");
432 * argv[0] at 0 is skipped by getopt, but
433 * store the buffer address there for
436 (*newargv)[c] = buffer;
437 for (c = 1; c < *argc; c++)
438 (*newargv)[c] = strsep(&buffer, " ");
453 static int set_options(int argc, char **argv, const char *short_opts,
454 char *target, char *maj_min_dev)
459 * optind is a global extern used by getopt. Since we can call
460 * set_options twice (once for command line, and once for config
461 * file) we have to reset this back to 1. [Note glibc handles
462 * setting this to 0, but klibc does not.]
466 option = getopt(argc, argv, short_options);
471 dprintf("option '%c' arg '%s'\n", option, optarg);
473 dprintf("option '%c'\n", option);
481 default_callout = optarg;
486 strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
494 strncpy(config_file, optarg, MAX_NAME_LEN);
506 if (strcmp(optarg, "0x80") == 0) {
507 default_page_code = 0x80;
508 } else if (strcmp(optarg, "0x83") == 0) {
509 default_page_code = 0x83;
511 log_message(LOG_WARNING,
512 "Unknown page code '%s'\n", optarg);
519 strncpy(target, sysfs_mnt_path, MAX_NAME_LEN);
520 strncat(target, optarg, MAX_NAME_LEN);
528 log_message(LOG_WARNING, "scsi_id version: %s\n",
534 log_message(LOG_WARNING,
535 "Unknown or bad option '%c' (0x%x)\n",
543 static int per_dev_options(struct sysfs_class_device *scsi_dev, int *good_bad,
544 int *page_code, char *callout)
548 char **newargv = NULL;
549 char vendor[MAX_ATTR_LEN];
550 char model[MAX_ATTR_LEN];
553 *good_bad = all_good;
554 *page_code = default_page_code;
555 if (default_callout && (callout != default_callout))
556 strncpy(callout, default_callout, MAX_NAME_LEN);
560 if (sysfs_get_attr(scsi_dev->path, "vendor", vendor, MAX_ATTR_LEN)) {
561 log_message(LOG_WARNING, "%s: cannot get vendor attribute\n",
566 if (sysfs_get_attr(scsi_dev->path, "model", model, MAX_ATTR_LEN)) {
567 log_message(LOG_WARNING, "%s: cannot get model attribute\n",
572 retval = get_file_options(vendor, model, &newargc, &newargv);
574 optind = 1; /* reset this global extern */
575 while (retval == 0) {
576 option = getopt(newargc, newargv, dev_short_options);
581 dprintf("option '%c' arg '%s'\n", option, optarg);
583 dprintf("option '%c'\n", option);
591 strncpy(callout, default_callout, MAX_NAME_LEN);
599 if (strcmp(optarg, "0x80") == 0) {
601 } else if (strcmp(optarg, "0x83") == 0) {
604 log_message(LOG_WARNING,
605 "Unknown page code '%s'\n", optarg);
611 log_message(LOG_WARNING,
612 "Unknown or bad option '%c' (0x%x)\n",
627 * scsi_id: try to get an id, if one is found, printf it to stdout.
628 * returns a value passed to exit() - 0 if printed an id, else 1. This
629 * could be expanded, for example, if we want to report a failure like no
630 * memory etc. return 2, and return 1 for expected cases (like broken
631 * device found) that do not print an id.
633 static int scsi_id(const char *target_path, char *maj_min_dev)
637 char full_dev_path[MAX_NAME_LEN];
638 char serial[MAX_SERIAL_LEN];
639 struct sysfs_class_device *scsi_dev; /* of scsi_device full_dev_path */
642 char callout[MAX_NAME_LEN];
644 dprintf("target_path %s\n", target_path);
647 * Ugly: depend on the sysfs path to tell us whether this is a
648 * block or char device. This should probably be encoded in the
649 * "dev" along with the major/minor.
651 if (has_sysfs_prefix(target_path, "/block")) {
653 } else if (has_sysfs_prefix(target_path, "/class")) {
657 log_message(LOG_WARNING,
658 "Non block or class device '%s'\n",
663 * Expected in some cases.
665 dprintf("Non block or class device\n");
670 if (sysfs_get_actual_dev(target_path, full_dev_path, MAX_NAME_LEN))
673 dprintf("full_dev_path %s\n", full_dev_path);
676 * Allow only scsi devices (those that have a matching device
677 * under /bus/scsi/devices).
679 * Other block devices can support SG IO, but only ide-cd does, so
680 * for now, don't bother with anything else.
682 retval = sysfs_is_bus(full_dev_path, "scsi");
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",
693 } else if (retval < 0) {
694 log_message(LOG_WARNING, "sysfs_is_bus failed: %s\n",
700 * mknod a temp dev to communicate with the device.
702 if (!dev_specified && create_tmp_dev(target_path, maj_min_dev,
704 dprintf("create_tmp_dev failed\n");
708 scsi_dev = sysfs_open_class_device_path(full_dev_path);
710 log_message(LOG_WARNING, "open class %s failed: %s\n",
711 full_dev_path, strerror(errno));
716 * Get any per device (vendor + model) options from the config
719 retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout);
720 dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n",
721 good_dev, page_code, callout);
725 } else if (callout[0] != '\0') {
727 * exec vendor callout, pass it only the "name" to be used
728 * for error messages, and the dev to open.
730 * This won't work if we need to pass on the original
731 * command line (when not hotplug mode) since the option
732 * parsing and per dev parsing modify the argv's.
734 * XXX Not implemented yet. And not fully tested ;-)
737 } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code,
738 serial, MAX_SERIAL_LEN)) {
745 printf("%s ", scsi_dev->name);
746 printf("%s", serial);
749 dprintf("%s\n", serial);
752 sysfs_close_class_device(scsi_dev);
760 int main(int argc, char **argv)
764 char target_path[MAX_NAME_LEN];
765 char maj_min_dev[MAX_NAME_LEN];
772 if ((argc == 2) && (argv[1][0] != '-')) {
774 dprintf("hotplug assumed\n");
777 dprintf("argc is %d\n", argc);
778 if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
779 log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n",
786 * There is a kernel race creating attributes, if called
787 * directly, uncomment the sleep.
791 devpath = getenv("DEVPATH");
793 log_message(LOG_WARNING, "DEVPATH is not set\n");
798 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
799 strncat(target_path, devpath, MAX_NAME_LEN);
801 if (set_options(argc, argv, short_options, target_path,
807 * Override any command line options set via the config file. This
808 * is the only way to set options when in hotplug mode.
811 retval = get_file_options(NULL, NULL, &newargc, &newargv);
814 } else if (newargv && (retval == 0)) {
815 if (set_options(newargc, newargv, short_options, target_path,
821 if (!sys_specified) {
822 log_message(LOG_WARNING, "-s must be specified\n");
826 retval = scsi_id(target_path, maj_min_dev);