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 "../../udev.h"
38 #include "scsi_id_version.h"
40 /* temporary names for mknod */
41 #define TMP_DIR "/dev"
42 #define TMP_PREFIX "tmp-scsi"
44 static const char short_options[] = "abd:f:gip:s:uvVx";
45 static const char dev_short_options[] = "bgp:";
48 static int always_info;
49 static int dev_specified;
50 static int sys_specified;
51 static char config_file[MAX_PATH_LEN] = SCSI_ID_CONFIG_FILE;
52 static int display_bus_id;
53 static enum page_code default_page_code;
54 static int use_stderr;
56 static int hotplug_mode;
57 static int reformat_serial;
59 static char vendor_str[64];
60 static char model_str[64];
61 static char revision_str[16];
62 static char type_str[16];
65 void log_message(int priority, const char *format, ...)
68 static int udev_log = -1;
73 value = getenv("UDEV_LOG");
75 udev_log = log_priority(value);
80 if (priority > udev_log)
83 va_start(args, format);
84 vsyslog(priority, format, args);
89 static void set_str(char *to, const char *from, size_t count)
93 /* strip trailing whitespace */
94 len = strnlen(from, count);
95 while (len && isspace(from[len-1]))
98 /* strip leading whitespace */
100 while (isspace(from[i]) && (i < len))
105 /* substitute multiple whitespace */
106 if (isspace(from[i])) {
107 while (isspace(from[i]))
112 if (from[i] == '/') {
121 static void set_type(char *to, const char *from, size_t len)
125 char *type = "generic";
127 type_num = strtoul(from, &eptr, 0);
155 strncpy(to, type, len);
159 static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
161 unsigned int maj, min;
165 attr = sysfs_attr_get_value(devpath, "dev");
167 dbg("%s: could not get dev attribute: %s", devpath, strerror(errno));
171 dbg("dev value %s", attr);
172 if (sscanf(attr, "%u:%u", &maj, &min) != 2) {
173 err("%s: invalid dev major/minor", devpath);
177 snprintf(tmpdev, MAX_PATH_LEN, "%s/%s-maj%d-min%d-%u",
178 TMP_DIR, TMP_PREFIX, maj, min, getpid());
180 dbg("tmpdev '%s'", tmpdev);
181 if (mknod(tmpdev, 0600 | dev_type, makedev(maj, min))) {
182 err("mknod failed: %s", strerror(errno));
191 * buf points to an '=' followed by a quoted string ("foo") or a string ending
192 * with a space or ','.
194 * Return a pointer to the NUL terminated string, returns NULL if no
197 static char *get_value(char **buffer)
199 static char *quote_string = "\"\n";
200 static char *comma_string = ",\n";
204 if (**buffer == '"') {
206 * skip leading quote, terminate when quote seen
213 val = strsep(buffer, end);
214 if (val && end == quote_string)
216 * skip trailing quote
220 while (isspace(**buffer))
226 static int argc_count(char *opts)
229 while (*opts != '\0')
238 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
239 * if vendor and model are set find the first OPTIONS line in the config
240 * file that matches. Set argc and argv to match the OPTIONS string.
242 * vendor and model can end in '\n'.
244 static int get_file_options(const char *vendor, const char *model,
245 int *argc, char ***newargv)
251 char *vendor_in, *model_in, *options_in; /* read in from file */
256 dbg("vendor='%s'; model='%s'\n", vendor, model);
257 fd = fopen(config_file, "r");
259 dbg("can't open %s\n", config_file);
260 if (errno == ENOENT) {
263 err("can't open %s: %s", config_file, strerror(errno));
269 * Allocate a buffer rather than put it on the stack so we can
270 * keep it around to parse any options (any allocated newargv
271 * points into this buffer for its strings).
273 buffer = malloc(MAX_BUFFER_LEN);
275 err("Can't allocate memory.");
282 vendor_in = model_in = options_in = NULL;
284 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
288 if (buf[strlen(buffer) - 1] != '\n') {
289 info("Config file line %d too long.\n", lineno);
293 while (isspace(*buf))
296 /* blank or all whitespace line */
304 dbg("lineno %d: '%s'\n", lineno, buf);
305 str1 = strsep(&buf, "=");
306 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
307 str1 = get_value(&buf);
314 str1 = strsep(&buf, "=");
315 if (str1 && strcasecmp(str1, "MODEL") == 0) {
316 str1 = get_value(&buf);
322 str1 = strsep(&buf, "=");
326 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
327 str1 = get_value(&buf);
334 dbg("config file line %d:"
335 " vendor '%s'; model '%s'; options '%s'\n",
336 lineno, vendor_in, model_in, options_in);
338 * Only allow: [vendor=foo[,model=bar]]options=stuff
340 if (!options_in || (!vendor_in && model_in)) {
341 info("Error parsing config file line %d '%s'", lineno, buffer);
345 if (vendor == NULL) {
346 if (vendor_in == NULL) {
347 dbg("matched global option\n");
350 } else if ((vendor_in && strncmp(vendor, vendor_in,
351 strlen(vendor_in)) == 0) &&
352 (!model_in || (strncmp(model, model_in,
353 strlen(model_in)) == 0))) {
355 * Matched vendor and optionally model.
357 * Note: a short vendor_in or model_in can
358 * give a partial match (that is FOO
361 dbg("matched vendor/model\n");
369 if (vendor_in != NULL || model_in != NULL ||
370 options_in != NULL) {
372 * Something matched. Allocate newargv, and store
373 * values found in options_in.
375 strcpy(buffer, options_in);
376 c = argc_count(buffer) + 2;
377 *newargv = calloc(c, sizeof(**newargv));
379 err("Can't allocate memory.");
385 * argv[0] at 0 is skipped by getopt, but
386 * store the buffer address there for
389 (*newargv)[c] = buffer;
390 for (c = 1; c < *argc; c++)
391 (*newargv)[c] = strsep(&buffer, " ");
404 static int set_options(int argc, char **argv, const char *short_opts,
405 char *target, char *maj_min_dev)
410 * optind is a global extern used by getopt. Since we can call
411 * set_options twice (once for command line, and once for config
412 * file) we have to reset this back to 1. [Note glibc handles
413 * setting this to 0, but klibc does not.]
417 option = getopt(argc, argv, short_opts);
422 dbg("option '%c' arg '%s'\n", option, optarg);
424 dbg("option '%c'\n", option);
436 strncpy(maj_min_dev, optarg, MAX_PATH_LEN);
437 maj_min_dev[MAX_PATH_LEN-1] = '\0';
445 strncpy(config_file, optarg, MAX_PATH_LEN);
446 config_file[MAX_PATH_LEN-1] = '\0';
458 if (strcmp(optarg, "0x80") == 0) {
459 default_page_code = PAGE_80;
460 } else if (strcmp(optarg, "0x83") == 0) {
461 default_page_code = PAGE_83;
462 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
463 default_page_code = PAGE_83_PRE_SPC3;
465 info("Unknown page code '%s'", optarg);
472 strncpy(target, optarg, MAX_PATH_LEN);
473 target[MAX_PATH_LEN-1] = '\0';
489 info("scsi_id version: %s\n", SCSI_ID_VERSION);
494 info("Unknown or bad option '%c' (0x%x)", option, option);
501 static int per_dev_options(struct sysfs_device *dev_scsi, int *good_bad, int *page_code)
505 char **newargv = NULL;
506 const char *vendor, *model, *type;
509 *good_bad = all_good;
510 *page_code = default_page_code;
512 vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor");
514 info("%s: cannot get vendor attribute", dev_scsi->devpath);
517 set_str(vendor_str, vendor, sizeof(vendor_str)-1);
519 model = sysfs_attr_get_value(dev_scsi->devpath, "model");
521 info("%s: cannot get model attribute\n", dev_scsi->devpath);
524 set_str(model_str, model, sizeof(model_str)-1);
526 type = sysfs_attr_get_value(dev_scsi->devpath, "type");
528 info("%s: cannot get type attribute", dev_scsi->devpath);
531 set_type(type_str, type, sizeof(type_str));
533 type = sysfs_attr_get_value(dev_scsi->devpath, "rev");
535 info("%s: cannot get type attribute\n", dev_scsi->devpath);
538 set_str(revision_str, type, sizeof(revision_str)-1);
540 retval = get_file_options(vendor, model, &newargc, &newargv);
542 optind = 1; /* reset this global extern */
543 while (retval == 0) {
544 option = getopt(newargc, newargv, dev_short_options);
549 dbg("option '%c' arg '%s'\n", option, optarg);
551 dbg("option '%c'\n", option);
563 if (strcmp(optarg, "0x80") == 0) {
564 *page_code = PAGE_80;
565 } else if (strcmp(optarg, "0x83") == 0) {
566 *page_code = PAGE_83;
567 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
568 *page_code = PAGE_83_PRE_SPC3;
570 info("Unknown page code '%s'", optarg);
576 info("Unknown or bad option '%c' (0x%x)", option, option);
590 * format_serial: replace to whitespaces by underscores for calling
591 * programs that use the serial for device naming (multipath, Suse
594 static void format_serial(char *serial)
596 char *p = serial, *q;
601 if (q > serial && q[-1] != '_') {
615 * scsi_id: try to get an id, if one is found, printf it to stdout.
616 * returns a value passed to exit() - 0 if printed an id, else 1. This
617 * could be expanded, for example, if we want to report a failure like no
618 * memory etc. return 2, and return 1 for expected cases (like broken
619 * device found) that do not print an id.
621 static int scsi_id(const char *devpath, char *maj_min_dev)
625 char *serial, *unaligned_buf;
626 struct sysfs_device *dev;
627 struct sysfs_device *dev_scsi;
631 dbg("devpath %s\n", devpath);
633 dev = sysfs_device_get(devpath);
635 err("unable to access '%s'", devpath);
639 if (strcmp(dev->subsystem, "block") == 0)
644 /* get scsi parent device */
645 dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
646 if (dev_scsi == NULL) {
647 err("unable to access parent device of '%s'", devpath);
651 /* mknod a temp dev to communicate with the device */
652 if (!dev_specified && create_tmp_dev(dev->devpath, maj_min_dev, dev_type)) {
653 dbg("create_tmp_dev failed\n");
657 /* get per device (vendor + model) options from the config file */
658 retval = per_dev_options(dev_scsi, &good_dev, &page_code);
659 dbg("per dev options: good %d; page code 0x%x", good_dev, page_code);
662 unaligned_buf = malloc(MAX_SERIAL_LEN + ALIGN);
663 serial = (char*) (((unsigned long) unaligned_buf + (ALIGN - 1))
665 dbg("buffer unaligned 0x%p; aligned 0x%p\n", unaligned_buf, serial);
670 } else if (scsi_get_serial(dev_scsi, maj_min_dev, page_code,
671 serial, MAX_SERIAL_LEN)) {
672 retval = always_info?0:1;
678 static char serial_str[64];
679 printf("ID_VENDOR=%s\n", vendor_str);
680 printf("ID_MODEL=%s\n", model_str);
681 printf("ID_REVISION=%s\n", revision_str);
682 set_str(serial_str, serial, sizeof(serial_str));
683 printf("ID_SERIAL=%s\n", serial_str);
684 printf("ID_TYPE=%s\n", type_str);
685 printf("ID_BUS=scsi\n");
688 format_serial(serial);
690 printf("%s: ", dev_scsi->kernel_name);
691 printf("%s\n", serial);
703 int main(int argc, char **argv)
706 char devpath[MAX_PATH_LEN];
707 char maj_min_dev[MAX_PATH_LEN];
712 logging_init("scsi_id");
714 dbg("argc is %d\n", argc);
716 /* sysfs path can be overridden for testing */
717 env = getenv("SYSFS_PATH");
719 strncpy(sysfs_path, env, sizeof(sysfs_path));
720 sysfs_path[sizeof(sysfs_path)-1] = '\0';
722 strcpy(sysfs_path, "/sys");
724 env = getenv("DEVPATH");
728 strncpy(devpath, env, MAX_PATH_LEN);
729 devpath[sizeof(devpath)-1] = '\0';
733 * Get config file options.
736 retval = get_file_options(NULL, NULL, &newargc, &newargv);
741 if (newargv && (retval == 0)) {
742 if (set_options(newargc, newargv, short_options, devpath,
751 * Get command line options (overriding any config file or DEVPATH
754 if (set_options(argc, argv, short_options, devpath, maj_min_dev) < 0)
757 if (!sys_specified) {
758 info("-s must be specified\n");
763 retval = scsi_id(devpath, maj_min_dev);