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
36 #include <sys/libsysfs.h>
41 #define VERSION "unknown"
45 * temporary names for mknod.
47 #define TMP_DIR "/tmp"
48 #define TMP_PREFIX "scsi"
50 #define CONFIG_FILE "/etc/scsi_id.config"
52 #define MAX_NAME_LEN 72
54 #define MAX_SERIAL_LEN 128
56 static const char short_options[] = "bc:d:ef:gip:s:vV";
57 static const struct option long_options[] = {
58 {"broken", no_argument, NULL, 'b'}, /* also per dev */
59 {"callout", required_argument, NULL, 'c'}, /* also per dev */
60 {"device", required_argument, NULL, 'd'},
61 {"stderr", no_argument, NULL, 'e'},
62 {"file", required_argument, NULL, 'f'},
63 {"good", no_argument, NULL, 'g'}, /* also per dev */
64 {"busid", no_argument, NULL, 'i'}, /* also per dev */
65 {"page", required_argument, NULL, 'p'}, /* also per dev */
66 {"devpath", required_argument, NULL, 's'},
67 {"verbose", no_argument, NULL, 'v'},
68 {"version", no_argument, NULL, 'V'},
72 * Just duplicate per dev options.
74 static const char dev_short_options[] = "bc:gp:";
75 static const struct option dev_long_options[] = {
76 {"broken", no_argument, NULL, 'b'}, /* also per dev */
77 {"callout", required_argument, NULL, 'c'}, /* also per dev */
78 {"good", no_argument, NULL, 'g'}, /* also per dev */
79 {"page", required_argument, NULL, 'p'}, /* also per dev */
83 char sysfs_mnt_path[SYSFS_PATH_MAX];
86 static char *default_callout;
87 static int dev_specified;
88 static int sys_specified;
89 static char config_file[MAX_NAME_LEN] = CONFIG_FILE;
90 static int display_bus_id;
91 static int default_page_code;
92 static int use_stderr;
94 static int hotplug_mode;
96 void log_message (int level, const char *format, ...)
100 if (!debug && level == LOG_DEBUG)
103 va_start (args, format);
104 if (!hotplug_mode || use_stderr) {
105 vfprintf(stderr, format, args);
107 static int logging_init = 0;
109 openlog ("scsi_id", LOG_PID, LOG_DAEMON);
113 vsyslog(level, format, args);
119 static int sysfs_get_actual_dev(const char *sysfs_path, char *dev, int len)
121 dprintf("%s\n", sysfs_path);
122 strncpy(dev, sysfs_path, len);
123 strncat(dev, "/device", len);
124 if (sysfs_get_link(dev, dev, len)) {
126 log_message(LOG_WARNING, "%s: %s\n", dev,
134 * sysfs_is_bus: Given the sysfs_path to a device, return 1 if sysfs_path
135 * is on bus, 0 if not on bus, and < 0 on error
137 static int sysfs_is_bus(const char *sysfs_path, const char *bus)
139 char bus_dev_name[SYSFS_PATH_MAX];
140 char bus_id[SYSFS_NAME_LEN];
141 struct stat stat_buf;
144 dprintf("%s\n", sysfs_path);
146 if (sysfs_get_name_from_path(sysfs_path, bus_id, SYSFS_NAME_LEN))
149 snprintf(bus_dev_name, MAX_NAME_LEN, "%s/%s/%s/%s/%s", sysfs_mnt_path,
150 SYSFS_BUS_DIR, bus, SYSFS_DEVICES_NAME, bus_id);
152 if (stat(sysfs_path, &stat_buf))
154 dev_inode = stat_buf.st_ino;
156 if (stat(bus_dev_name, &stat_buf)) {
162 if (dev_inode == stat_buf.st_ino)
168 static int get_major_minor(const char *devpath, int *major, int *minor)
170 struct sysfs_class_device *class_dev;
171 char dev_value[SYSFS_NAME_LEN];
174 dprintf("%s\n", devpath);
175 class_dev = sysfs_open_class_device(devpath);
177 log_message(LOG_WARNING, "open class %s failed: %s\n", devpath,
182 dev = sysfs_get_attr(class_dev, "dev");
184 strncpy(dev_value, dev, SYSFS_NAME_LEN);
185 sysfs_close_class_device(class_dev);
188 * XXX This happens a lot, since sg has no dev attr.
189 * Someday change this back to a LOG_WARNING.
191 log_message(LOG_DEBUG, "%s could not get dev attribute: %s\n",
192 devpath, strerror(errno));
197 dprintf("dev %s", dev_value); /* dev_value has a trailing \n */
198 if (sscanf(dev_value, "%u:%u", major, minor) != 2) {
199 log_message(LOG_WARNING, "%s: invalid dev major/minor\n",
207 static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
211 dprintf("(%s)\n", devpath);
213 if (get_major_minor(devpath, &major, &minor))
215 snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
216 TMP_DIR, TMP_PREFIX, major, minor, getpid());
218 dprintf("tmpdev '%s'\n", tmpdev);
220 if (mknod(tmpdev, 0600 | dev_type, makedev(major, minor))) {
221 log_message(LOG_WARNING, "mknod failed: %s\n", strerror(errno));
227 static int has_sysfs_prefix(const char *path, const char *prefix)
229 char match[MAX_NAME_LEN];
231 strncpy(match, sysfs_mnt_path, MAX_NAME_LEN);
232 strncat(match, prefix, MAX_NAME_LEN);
233 if (strncmp(path, match, strlen(match)) == 0)
242 * buf points to an '=' followed by a quoted string ("foo") or a string ending
243 * with a space or ','.
245 * Return a pointer to the NUL terminated string, returns NULL if no
248 static char *get_value(char **buffer)
250 static char *quote_string = "\"\n";
251 static char *comma_string = ",\n";
255 if (**buffer == '"') {
257 * skip leading quote, terminate when quote seen
264 val = strsep(buffer, end);
265 if (val && end == quote_string)
267 * skip trailing quote
271 while (isspace(**buffer))
277 static int argc_count(char *opts)
280 while (*opts != '\0')
289 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
290 * if vendor and model are set find the first OPTIONS line in the config
291 * file that matches. Set argc and argv to match the OPTIONS string.
293 * vendor and model can end in '\n'.
295 static int get_file_options(char *vendor, char *model, int *argc,
302 char *vendor_in, *model_in, *options_in; /* read in from file */
306 static char *prog_string = "arg0";
308 dprintf("vendor='%s'; model='%s'\n", vendor, model);
309 fd = fopen(config_file, "r");
311 dprintf("can't open %s\n", config_file);
312 if (errno == ENOENT) {
315 log_message(LOG_WARNING, "can't open %s: %s\n",
316 config_file, strerror(errno));
325 vendor_in = model_in = options_in = NULL;
327 buf = fgets(buffer, sizeof(buffer), fd);
332 while (isspace(*buf))
337 * blank or all whitespace line
348 dprintf("lineno %d: '%s'\n", lineno, buf);
350 str1 = strsep(&buf, "=");
351 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
352 str1 = get_value(&buf);
359 str1 = strsep(&buf, "=");
360 if (str1 && strcasecmp(str1, "MODEL") == 0) {
361 str1 = get_value(&buf);
367 str1 = strsep(&buf, "=");
371 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
372 str1 = get_value(&buf);
379 dprintf("config file line %d:"
380 " vendor '%s'; model '%s'; options '%s'\n",
381 lineno, vendor_in, model_in, options_in);
383 * Only allow: [vendor=foo[,model=bar]]options=stuff
385 if (!options_in || (!vendor_in && model_in)) {
386 log_message(LOG_WARNING,
387 "Error parsing config file line %d '%s'\n",
392 if (vendor == NULL) {
393 if (vendor_in == NULL) {
394 dprintf("matched global option\n");
397 } else if ((vendor_in && strncmp(vendor, vendor_in,
398 strlen(vendor_in)) == 0) &&
399 (!model_in || (strncmp(model, model_in,
400 strlen(model_in)) == 0))) {
402 * Matched vendor and optionally model.
404 * Note: a short vendor_in or model_in can
405 * give a partial match (that is FOO
408 dprintf("matched vendor/model\n");
411 dprintf("no match\n");
416 if (vendor_in != NULL || model_in != NULL ||
417 options_in != NULL) {
419 * Something matched. Allocate newargv, and store
420 * values found in options_in.
422 c = argc_count(options_in) + 2;
423 *newargv = calloc(c, sizeof(**newargv));
425 log_message(LOG_WARNING,
426 "Can't allocate memory\n");
431 (*newargv)[c] = prog_string; /* nothing */
432 for (c = 1; c < *argc; c++)
433 (*newargv)[c] = strsep(&options_in, " ");
446 static int set_options(int argc, char **argv, const char *short_opts,
447 const struct option *long_opts, char *target,
454 * optind is a global extern used by getopt_long. Since we can
455 * call set_options twice (once for command line, and once for
456 * config file) we have to reset this back to 0.
460 option = getopt_long(argc, argv, short_options, long_options,
466 dprintf("option '%c' arg '%s'\n", option, optarg);
468 dprintf("option '%c'\n", option);
476 default_callout = optarg;
481 strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
489 strncpy(config_file, optarg, MAX_NAME_LEN);
501 if (strcmp(optarg, "0x80") == 0) {
502 default_page_code = 0x80;
503 } else if (strcmp(optarg, "0x83") == 0) {
504 default_page_code = 0x83;
506 log_message(LOG_WARNING,
507 "Unknown page code '%s'\n", optarg);
514 strncpy(target, sysfs_mnt_path, MAX_NAME_LEN);
515 strncat(target, optarg, MAX_NAME_LEN);
523 log_message(LOG_WARNING, "scsi_id version: %s\n",
529 log_message(LOG_WARNING,
530 "Unknown or bad option '%c' (0x%x)\n",
538 static int per_dev_options(struct sysfs_class_device *scsi_dev, int *good_bad,
539 int *page_code, char *callout)
543 char **newargv = NULL;
550 *good_bad = all_good;
551 *page_code = default_page_code;
552 if (default_callout && (callout != default_callout))
553 strncpy(callout, default_callout, MAX_NAME_LEN);
557 vendor = sysfs_get_attr(scsi_dev, "vendor");
559 log_message(LOG_WARNING, "%s: no vendor attribute\n",
564 model = sysfs_get_attr(scsi_dev, "model");
566 log_message(LOG_WARNING, "%s: no model attribute\n",
571 retval = get_file_options(vendor, model, &newargc, &newargv);
573 optind = 0; /* global extern, reset to 0 */
574 while (retval == 0) {
575 option = getopt_long(newargc, newargv, dev_short_options,
576 dev_long_options, &option_ind);
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",
625 * scsi_id: try to get an id, if one is found, printf it to stdout.
626 * returns a value passed to exit() - 0 if printed an id, else 1. This
627 * could be expanded, for example, if we want to report a failure like no
628 * memory etc. return 2, and return 1 for expected cases (like broken
629 * device found) that do not print an id.
631 static int scsi_id(const char *target_path, char *maj_min_dev)
635 char full_dev_path[MAX_NAME_LEN];
636 char serial[MAX_SERIAL_LEN];
637 struct sysfs_class_device *scsi_dev; /* of scsi_device full_dev_path */
640 char callout[MAX_NAME_LEN];
642 dprintf("target_path %s\n", target_path);
645 * Ugly: depend on the sysfs path to tell us whether this is a
646 * block or char device. This should probably be encoded in the
647 * "dev" along with the major/minor.
649 if (has_sysfs_prefix(target_path, "/block")) {
651 } else if (has_sysfs_prefix(target_path, "/class")) {
655 log_message(LOG_WARNING,
656 "Non block or class device '%s'\n",
661 * Expected in some cases.
663 dprintf("Non block or class device\n");
668 if (sysfs_get_actual_dev(target_path, full_dev_path, MAX_NAME_LEN))
671 dprintf("full_dev_path %s\n", full_dev_path);
674 * Allow only scsi devices (those that have a matching device
675 * under /bus/scsi/devices).
677 * Other block devices can support SG IO, but only ide-cd does, so
678 * for now, don't bother with anything else.
680 retval = sysfs_is_bus(full_dev_path, "scsi");
684 * Expected in some cases.
686 dprintf("%s is not a scsi device\n", target_path);
688 log_message(LOG_WARNING, "%s is not a scsi device\n",
691 } else if (retval < 0) {
692 log_message(LOG_WARNING, "sysfs_is_bus failed: %s\n",
698 * mknod a temp dev to communicate with the device.
700 if (!dev_specified && create_tmp_dev(target_path, maj_min_dev,
702 dprintf("create_tmp_dev failed\n");
706 scsi_dev = sysfs_open_class_device(full_dev_path);
708 log_message(LOG_WARNING, "open class %s failed: %s\n",
709 full_dev_path, strerror(errno));
714 * Get any per device (vendor + model) options from the config
717 retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout);
718 dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n",
719 good_dev, page_code, callout);
723 } else if (callout[0] != '\0') {
725 * exec vendor callout, pass it only the "name" to be used
726 * for error messages, and the dev to open.
728 * This won't work if we need to pass on the original
729 * command line (when not hotplug mode) since the option
730 * parsing and per dev parsing modify the argv's.
732 * XXX Not implemented yet. And not fully tested ;-)
735 } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code,
736 serial, MAX_SERIAL_LEN)) {
743 printf("%s ", scsi_dev->name);
744 printf("%s", serial);
747 dprintf("%s\n", serial);
751 sysfs_close_class_device(scsi_dev);
759 int main(int argc, char **argv)
763 char target_path[MAX_NAME_LEN];
764 char maj_min_dev[MAX_NAME_LEN];
771 if ((argc == 2) && (argv[1][0] != '-')) {
773 dprintf("hotplug assumed\n");
776 dprintf("argc is %d\n", argc);
777 if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
778 log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n",
785 * There is a kernel race creating attributes, if called
786 * directly, uncomment the sleep.
790 devpath = getenv("DEVPATH");
792 log_message(LOG_WARNING, "DEVPATH is not set\n");
797 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
798 strncat(target_path, devpath, MAX_NAME_LEN);
800 if (set_options(argc, argv, short_options, long_options,
801 target_path, maj_min_dev) < 0)
806 * Override any command line options set via the config file. This
807 * is the only way to set options when in hotplug mode.
810 retval = get_file_options(NULL, NULL, &newargc, &newargv);
813 } else if (newargv && (retval == 0)) {
814 if (set_options(newargc, newargv, short_options, long_options,
815 target_path, maj_min_dev) < 0)
820 if (!sys_specified) {
821 log_message(LOG_WARNING, "-s must be specified\n");
825 retval = scsi_id(target_path, maj_min_dev);