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>
45 #ifndef SCSI_ID_VERSION
47 #define SCSI_ID_VERSION "unknown"
51 * temporary names for mknod.
53 #define TMP_DIR "/tmp"
54 #define TMP_PREFIX "scsi"
56 #define CONFIG_FILE "/etc/scsi_id.config"
58 static const char short_options[] = "bc:d:ef:gip:s:vV";
60 * Just duplicate per dev options.
62 static const char dev_short_options[] = "bc:gp:";
64 char sysfs_mnt_path[SYSFS_PATH_MAX];
67 static char *default_callout;
68 static int dev_specified;
69 static int sys_specified;
70 static char config_file[MAX_NAME_LEN] = CONFIG_FILE;
71 static int display_bus_id;
72 static int default_page_code;
73 static int use_stderr;
75 static int hotplug_mode;
77 void log_message (int level, const char *format, ...)
81 if (!debug && level == LOG_DEBUG)
84 va_start (args, format);
85 if (!hotplug_mode || use_stderr) {
86 vfprintf(stderr, format, args);
88 static int logging_init = 0;
89 static unsigned char logname[32];
92 * klibc does not have LOG_PID.
94 snprintf(logname, 32, "scsi_id[%d]", getpid());
95 openlog (logname, 0, LOG_DAEMON);
99 vsyslog(level, format, args);
105 static int sysfs_get_actual_dev(const char *sysfs_path, char *dev, int len)
107 dprintf("%s\n", sysfs_path);
108 strncpy(dev, sysfs_path, len);
109 strncat(dev, "/device", len);
110 if (sysfs_get_link(dev, dev, len)) {
112 log_message(LOG_WARNING, "%s: %s\n", dev,
120 * sysfs_is_bus: Given the sysfs_path to a device, return 1 if sysfs_path
121 * is on bus, 0 if not on bus, and < 0 on error
123 static int sysfs_is_bus(const char *sysfs_path, const char *bus)
125 char bus_dev_name[SYSFS_PATH_MAX];
126 char bus_id[SYSFS_NAME_LEN];
127 struct stat stat_buf;
130 dprintf("%s\n", sysfs_path);
132 if (sysfs_get_name_from_path(sysfs_path, bus_id, SYSFS_NAME_LEN))
135 snprintf(bus_dev_name, MAX_NAME_LEN, "%s/%s/%s/%s/%s", sysfs_mnt_path,
136 SYSFS_BUS_NAME, bus, SYSFS_DEVICES_NAME, bus_id);
138 if (stat(sysfs_path, &stat_buf))
140 dev_inode = stat_buf.st_ino;
142 if (stat(bus_dev_name, &stat_buf)) {
148 if (dev_inode == stat_buf.st_ino)
154 static int get_major_minor(const char *devpath, int *major, int *minor)
156 struct sysfs_class_device *class_dev;
157 char dev_value[SYSFS_NAME_LEN];
160 dprintf("%s\n", devpath);
161 class_dev = sysfs_open_class_device_path(devpath);
163 log_message(LOG_WARNING, "open class %s failed: %s\n", devpath,
168 dev = sysfs_get_attr(class_dev, "dev");
170 strncpy(dev_value, dev, SYSFS_NAME_LEN);
171 sysfs_close_class_device(class_dev);
174 * XXX This happens a lot, since sg has no dev attr.
175 * Someday change this back to a LOG_WARNING.
177 log_message(LOG_DEBUG, "%s could not get dev attribute: %s\n",
178 devpath, strerror(errno));
183 dprintf("dev %s", dev_value); /* dev_value has a trailing \n */
184 if (sscanf(dev_value, "%u:%u", major, minor) != 2) {
185 log_message(LOG_WARNING, "%s: invalid dev major/minor\n",
193 static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
197 dprintf("(%s)\n", devpath);
199 if (get_major_minor(devpath, &major, &minor))
201 snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
202 TMP_DIR, TMP_PREFIX, major, minor, getpid());
204 dprintf("tmpdev '%s'\n", tmpdev);
206 if (mknod(tmpdev, 0600 | dev_type, makedev(major, minor))) {
207 log_message(LOG_WARNING, "mknod failed: %s\n", strerror(errno));
213 static int has_sysfs_prefix(const char *path, const char *prefix)
215 char match[MAX_NAME_LEN];
217 strncpy(match, sysfs_mnt_path, MAX_NAME_LEN);
218 strncat(match, prefix, MAX_NAME_LEN);
219 if (strncmp(path, match, strlen(match)) == 0)
228 * buf points to an '=' followed by a quoted string ("foo") or a string ending
229 * with a space or ','.
231 * Return a pointer to the NUL terminated string, returns NULL if no
234 static char *get_value(char **buffer)
236 static char *quote_string = "\"\n";
237 static char *comma_string = ",\n";
241 if (**buffer == '"') {
243 * skip leading quote, terminate when quote seen
250 val = strsep(buffer, end);
251 if (val && end == quote_string)
253 * skip trailing quote
257 while (isspace(**buffer))
263 static int argc_count(char *opts)
266 while (*opts != '\0')
275 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
276 * if vendor and model are set find the first OPTIONS line in the config
277 * file that matches. Set argc and argv to match the OPTIONS string.
279 * vendor and model can end in '\n'.
281 static int get_file_options(char *vendor, char *model, int *argc,
288 char *vendor_in, *model_in, *options_in; /* read in from file */
293 dprintf("vendor='%s'; model='%s'\n", vendor, model);
294 fd = fopen(config_file, "r");
296 dprintf("can't open %s\n", config_file);
297 if (errno == ENOENT) {
300 log_message(LOG_WARNING, "can't open %s: %s\n",
301 config_file, strerror(errno));
307 * Allocate a buffer rather than put it on the stack so we can
308 * keep it around to parse any options (any allocated newargv
309 * points into this buffer for its strings).
311 buffer = malloc(MAX_BUFFER_LEN);
313 log_message(LOG_WARNING, "Can't allocate memory.\n");
320 vendor_in = model_in = options_in = NULL;
322 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
326 if (buf[strlen(buffer) - 1] != '\n') {
327 log_message(LOG_WARNING,
328 "Config file line %d too long.\n", lineno);
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 strcpy(buffer, options_in);
423 c = argc_count(buffer) + 2;
424 *newargv = calloc(c, sizeof(**newargv));
426 log_message(LOG_WARNING,
427 "Can't allocate memory.\n");
433 * argv[0] at 0 is skipped by getopt, but
434 * store the buffer address there for
437 (*newargv)[c] = buffer;
438 for (c = 1; c < *argc; c++)
439 (*newargv)[c] = strsep(&buffer, " ");
454 static int set_options(int argc, char **argv, const char *short_opts,
455 char *target, char *maj_min_dev)
460 * optind is a global extern used by getopt. Since we can call
461 * set_options twice (once for command line, and once for config
462 * file) we have to reset this back to 1. [Note glibc handles
463 * setting this to 0, but klibc does not.]
467 option = getopt(argc, argv, short_options);
472 dprintf("option '%c' arg '%s'\n", option, optarg);
474 dprintf("option '%c'\n", option);
482 default_callout = optarg;
487 strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
495 strncpy(config_file, optarg, MAX_NAME_LEN);
507 if (strcmp(optarg, "0x80") == 0) {
508 default_page_code = 0x80;
509 } else if (strcmp(optarg, "0x83") == 0) {
510 default_page_code = 0x83;
512 log_message(LOG_WARNING,
513 "Unknown page code '%s'\n", optarg);
520 strncpy(target, sysfs_mnt_path, MAX_NAME_LEN);
521 strncat(target, optarg, MAX_NAME_LEN);
529 log_message(LOG_WARNING, "scsi_id version: %s\n",
535 log_message(LOG_WARNING,
536 "Unknown or bad option '%c' (0x%x)\n",
544 static int per_dev_options(struct sysfs_class_device *scsi_dev, int *good_bad,
545 int *page_code, char *callout)
549 char **newargv = NULL;
554 *good_bad = all_good;
555 *page_code = default_page_code;
556 if (default_callout && (callout != default_callout))
557 strncpy(callout, default_callout, MAX_NAME_LEN);
561 vendor = sysfs_get_attr(scsi_dev, "vendor");
563 log_message(LOG_WARNING, "%s: no vendor attribute\n",
568 model = sysfs_get_attr(scsi_dev, "model");
570 log_message(LOG_WARNING, "%s: no model attribute\n",
575 retval = get_file_options(vendor, model, &newargc, &newargv);
577 optind = 1; /* reset this global extern */
578 while (retval == 0) {
579 option = getopt(newargc, newargv, dev_short_options);
584 dprintf("option '%c' arg '%s'\n", option, optarg);
586 dprintf("option '%c'\n", option);
594 strncpy(callout, default_callout, MAX_NAME_LEN);
602 if (strcmp(optarg, "0x80") == 0) {
604 } else if (strcmp(optarg, "0x83") == 0) {
607 log_message(LOG_WARNING,
608 "Unknown page code '%s'\n", optarg);
614 log_message(LOG_WARNING,
615 "Unknown or bad option '%c' (0x%x)\n",
630 * scsi_id: try to get an id, if one is found, printf it to stdout.
631 * returns a value passed to exit() - 0 if printed an id, else 1. This
632 * could be expanded, for example, if we want to report a failure like no
633 * memory etc. return 2, and return 1 for expected cases (like broken
634 * device found) that do not print an id.
636 static int scsi_id(const char *target_path, char *maj_min_dev)
640 char full_dev_path[MAX_NAME_LEN];
641 char serial[MAX_SERIAL_LEN];
642 struct sysfs_class_device *scsi_dev; /* of scsi_device full_dev_path */
645 char callout[MAX_NAME_LEN];
647 dprintf("target_path %s\n", target_path);
650 * Ugly: depend on the sysfs path to tell us whether this is a
651 * block or char device. This should probably be encoded in the
652 * "dev" along with the major/minor.
654 if (has_sysfs_prefix(target_path, "/block")) {
656 } else if (has_sysfs_prefix(target_path, "/class")) {
660 log_message(LOG_WARNING,
661 "Non block or class device '%s'\n",
666 * Expected in some cases.
668 dprintf("Non block or class device\n");
673 if (sysfs_get_actual_dev(target_path, full_dev_path, MAX_NAME_LEN))
676 dprintf("full_dev_path %s\n", full_dev_path);
679 * Allow only scsi devices (those that have a matching device
680 * under /bus/scsi/devices).
682 * Other block devices can support SG IO, but only ide-cd does, so
683 * for now, don't bother with anything else.
685 retval = sysfs_is_bus(full_dev_path, "scsi");
689 * Expected in some cases.
691 dprintf("%s is not a scsi device\n", target_path);
693 log_message(LOG_WARNING, "%s is not a scsi device\n",
696 } else if (retval < 0) {
697 log_message(LOG_WARNING, "sysfs_is_bus failed: %s\n",
703 * mknod a temp dev to communicate with the device.
705 if (!dev_specified && create_tmp_dev(target_path, maj_min_dev,
707 dprintf("create_tmp_dev failed\n");
711 scsi_dev = sysfs_open_class_device_path(full_dev_path);
713 log_message(LOG_WARNING, "open class %s failed: %s\n",
714 full_dev_path, strerror(errno));
719 * Get any per device (vendor + model) options from the config
722 retval = per_dev_options(scsi_dev, &good_dev, &page_code, callout);
723 dprintf("per dev options: good %d; page code 0x%x; callout '%s'\n",
724 good_dev, page_code, callout);
728 } else if (callout[0] != '\0') {
730 * exec vendor callout, pass it only the "name" to be used
731 * for error messages, and the dev to open.
733 * This won't work if we need to pass on the original
734 * command line (when not hotplug mode) since the option
735 * parsing and per dev parsing modify the argv's.
737 * XXX Not implemented yet. And not fully tested ;-)
740 } else if (scsi_get_serial(scsi_dev, maj_min_dev, page_code,
741 serial, MAX_SERIAL_LEN)) {
748 printf("%s ", scsi_dev->name);
749 printf("%s", serial);
752 dprintf("%s\n", serial);
755 sysfs_close_class_device(scsi_dev);
763 int main(int argc, char **argv)
767 char target_path[MAX_NAME_LEN];
768 char maj_min_dev[MAX_NAME_LEN];
775 if ((argc == 2) && (argv[1][0] != '-')) {
777 dprintf("hotplug assumed\n");
780 dprintf("argc is %d\n", argc);
781 if (sysfs_get_mnt_path(sysfs_mnt_path, MAX_NAME_LEN)) {
782 log_message(LOG_WARNING, "sysfs_get_mnt_path failed: %s\n",
789 * There is a kernel race creating attributes, if called
790 * directly, uncomment the sleep.
794 devpath = getenv("DEVPATH");
796 log_message(LOG_WARNING, "DEVPATH is not set\n");
801 strncpy(target_path, sysfs_mnt_path, MAX_NAME_LEN);
802 strncat(target_path, devpath, MAX_NAME_LEN);
804 if (set_options(argc, argv, short_options, target_path,
810 * Override any command line options set via the config file. This
811 * is the only way to set options when in hotplug mode.
814 retval = get_file_options(NULL, NULL, &newargc, &newargv);
817 } else if (newargv && (retval == 0)) {
818 if (set_options(newargc, newargv, short_options, target_path,
824 if (!sys_specified) {
825 log_message(LOG_WARNING, "-s must be specified\n");
829 retval = scsi_id(target_path, maj_min_dev);