4 * Main section of the scsi_id program
6 * Copyright (C) IBM Corp. 2003
7 * Copyright (C) SUSE Linux Products GmbH, 2006
10 * Patrick Mansfield<patmans@us.ibm.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation version 2 of the License.
30 #include "../../udev/udev.h"
33 static const struct option options[] = {
34 { "device", required_argument, NULL, 'd' },
35 { "config", required_argument, NULL, 'f' },
36 { "page", required_argument, NULL, 'p' },
37 { "blacklisted", no_argument, NULL, 'b' },
38 { "whitelisted", no_argument, NULL, 'g' },
39 { "replace-whitespace", no_argument, NULL, 'u' },
40 { "sg-version", required_argument, NULL, 's' },
41 { "verbose", no_argument, NULL, 'v' },
42 { "version", no_argument, NULL, 'V' },
43 { "export", no_argument, NULL, 'x' },
44 { "help", no_argument, NULL, 'h' },
48 static const char short_options[] = "d:f:ghip:uvVx";
49 static const char dev_short_options[] = "bgp:";
52 static int dev_specified;
53 static char config_file[MAX_PATH_LEN] = SYSCONFDIR "/scsi_id.config";
54 static enum page_code default_page_code;
55 static int sg_version = 4;
56 static int use_stderr;
58 static int reformat_serial;
60 static char vendor_str[64];
61 static char model_str[64];
62 static char revision_str[16];
63 static char type_str[16];
65 static void log_fn(struct udev *udev, int priority,
66 const char *file, int line, const char *fn,
67 const char *format, va_list args)
69 vsyslog(priority, format, args);
72 static void set_str(char *to, const char *from, size_t count)
76 /* strip trailing whitespace */
77 len = strnlen(from, count);
78 while (len && isspace(from[len-1]))
81 /* strip leading whitespace */
83 while (isspace(from[i]) && (i < len))
88 /* substitute multiple whitespace */
89 if (isspace(from[i])) {
90 while (isspace(from[i]))
104 static void set_type(char *to, const char *from, size_t len)
108 char *type = "generic";
110 type_num = strtoul(from, &eptr, 0);
138 strncpy(to, type, len);
145 * buf points to an '=' followed by a quoted string ("foo") or a string ending
146 * with a space or ','.
148 * Return a pointer to the NUL terminated string, returns NULL if no
151 static char *get_value(char **buffer)
153 static char *quote_string = "\"\n";
154 static char *comma_string = ",\n";
158 if (**buffer == '"') {
160 * skip leading quote, terminate when quote seen
167 val = strsep(buffer, end);
168 if (val && end == quote_string)
170 * skip trailing quote
174 while (isspace(**buffer))
180 static int argc_count(char *opts)
183 while (*opts != '\0')
192 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
193 * if vendor and model are set find the first OPTIONS line in the config
194 * file that matches. Set argc and argv to match the OPTIONS string.
196 * vendor and model can end in '\n'.
198 static int get_file_options(struct udev *udev,
199 const char *vendor, const char *model,
200 int *argc, char ***newargv)
206 char *vendor_in, *model_in, *options_in; /* read in from file */
211 dbg(udev, "vendor='%s'; model='%s'\n", vendor, model);
212 fd = fopen(config_file, "r");
214 dbg(udev, "can't open %s\n", config_file);
215 if (errno == ENOENT) {
218 err(udev, "can't open %s: %s\n", config_file, strerror(errno));
224 * Allocate a buffer rather than put it on the stack so we can
225 * keep it around to parse any options (any allocated newargv
226 * points into this buffer for its strings).
228 buffer = malloc(MAX_BUFFER_LEN);
230 err(udev, "can't allocate memory\n");
237 vendor_in = model_in = options_in = NULL;
239 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
243 if (buf[strlen(buffer) - 1] != '\n') {
244 err(udev, "Config file line %d too long\n", lineno);
248 while (isspace(*buf))
251 /* blank or all whitespace line */
259 dbg(udev, "lineno %d: '%s'\n", lineno, buf);
260 str1 = strsep(&buf, "=");
261 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
262 str1 = get_value(&buf);
269 str1 = strsep(&buf, "=");
270 if (str1 && strcasecmp(str1, "MODEL") == 0) {
271 str1 = get_value(&buf);
277 str1 = strsep(&buf, "=");
281 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
282 str1 = get_value(&buf);
289 dbg(udev, "config file line %d:\n"
290 " vendor '%s'; model '%s'; options '%s'\n",
291 lineno, vendor_in, model_in, options_in);
293 * Only allow: [vendor=foo[,model=bar]]options=stuff
295 if (!options_in || (!vendor_in && model_in)) {
296 err(udev, "Error parsing config file line %d '%s'\n", lineno, buffer);
300 if (vendor == NULL) {
301 if (vendor_in == NULL) {
302 dbg(udev, "matched global option\n");
305 } else if ((vendor_in && strncmp(vendor, vendor_in,
306 strlen(vendor_in)) == 0) &&
307 (!model_in || (strncmp(model, model_in,
308 strlen(model_in)) == 0))) {
310 * Matched vendor and optionally model.
312 * Note: a short vendor_in or model_in can
313 * give a partial match (that is FOO
316 dbg(udev, "matched vendor/model\n");
319 dbg(udev, "no match\n");
324 if (vendor_in != NULL || model_in != NULL ||
325 options_in != NULL) {
327 * Something matched. Allocate newargv, and store
328 * values found in options_in.
330 strcpy(buffer, options_in);
331 c = argc_count(buffer) + 2;
332 *newargv = calloc(c, sizeof(**newargv));
334 err(udev, "can't allocate memory\n");
340 * argv[0] at 0 is skipped by getopt, but
341 * store the buffer address there for
344 (*newargv)[c] = buffer;
345 for (c = 1; c < *argc; c++)
346 (*newargv)[c] = strsep(&buffer, " \t");
359 static int set_options(struct udev *udev,
360 int argc, char **argv, const char *short_opts,
366 * optind is a global extern used by getopt. Since we can call
367 * set_options twice (once for command line, and once for config
368 * file) we have to reset this back to 1.
372 option = getopt_long(argc, argv, short_opts, options, NULL);
377 dbg(udev, "option '%c' arg '%s'\n", option, optarg);
379 dbg(udev, "option '%c'\n", option);
388 strncpy(maj_min_dev, optarg, MAX_PATH_LEN);
389 maj_min_dev[MAX_PATH_LEN-1] = '\0';
397 strncpy(config_file, optarg, MAX_PATH_LEN);
398 config_file[MAX_PATH_LEN-1] = '\0';
406 printf("Usage: scsi_id OPTIONS <device>\n"
407 " --device= device node for SG_IO commands\n"
408 " --config= location of config file\n"
409 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
410 " --sg-version=3|4 use SGv3 or SGv4\n"
411 " --blacklisted threat device as blacklisted\n"
412 " --whitelisted threat device as whitelisted\n"
413 " --replace-whitespace replace all whitespaces by underscores\n"
414 " --verbose verbose logging\n"
415 " --version print version\n"
416 " --export print values as environment keys\n"
417 " --help print this help text\n\n");
421 if (strcmp(optarg, "0x80") == 0) {
422 default_page_code = PAGE_80;
423 } else if (strcmp(optarg, "0x83") == 0) {
424 default_page_code = PAGE_83;
425 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
426 default_page_code = PAGE_83_PRE_SPC3;
428 err(udev, "Unknown page code '%s'\n", optarg);
434 sg_version = atoi(optarg);
435 if (sg_version < 3 || sg_version > 4) {
436 err(udev, "Unknown SG version '%s'\n", optarg);
454 printf("%s\n", VERSION);
462 if (optind < argc && !dev_specified) {
464 strncpy(maj_min_dev, argv[optind], MAX_PATH_LEN);
465 maj_min_dev[MAX_PATH_LEN-1] = '\0';
470 static int per_dev_options(struct udev *udev,
471 struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
475 char **newargv = NULL;
478 *good_bad = all_good;
479 *page_code = default_page_code;
481 retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
483 optind = 1; /* reset this global extern */
484 while (retval == 0) {
485 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
490 dbg(udev, "option '%c' arg '%s'\n", option, optarg);
492 dbg(udev, "option '%c'\n", option);
504 if (strcmp(optarg, "0x80") == 0) {
505 *page_code = PAGE_80;
506 } else if (strcmp(optarg, "0x83") == 0) {
507 *page_code = PAGE_83;
508 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
509 *page_code = PAGE_83_PRE_SPC3;
511 err(udev, "Unknown page code '%s'\n", optarg);
517 err(udev, "Unknown or bad option '%c' (0x%x)\n", option, option);
530 static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
534 dev_scsi->use_sg = sg_version;
536 retval = scsi_std_inquiry(udev, dev_scsi, path);
540 set_str(vendor_str, dev_scsi->vendor, sizeof(vendor_str));
541 set_str(model_str, dev_scsi->model, sizeof(model_str));
542 set_type(type_str, dev_scsi->type, sizeof(type_str));
543 set_str(revision_str, dev_scsi->revision, sizeof(revision_str));
549 * format_serial: replace to whitespaces by underscores for calling
550 * programs that use the serial for device naming (multipath, Suse
553 static void format_serial(char *serial)
555 char *p = serial, *q;
560 if (q > serial && q[-1] != '_') {
574 * scsi_id: try to get an id, if one is found, printf it to stdout.
575 * returns a value passed to exit() - 0 if printed an id, else 1. This
576 * could be expanded, for example, if we want to report a failure like no
577 * memory etc. return 2, and return 1 for expected cases (like broken
578 * device found) that do not print an id.
580 static int scsi_id(struct udev *udev, char *maj_min_dev)
583 struct scsi_id_device dev_scsi;
586 char serial_short[MAX_SERIAL_LEN] = "";
588 set_inq_values(udev, &dev_scsi, maj_min_dev);
590 /* get per device (vendor + model) options from the config file */
591 retval = per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
592 dbg(udev, "per dev options: good %d; page code 0x%x\n", good_dev, page_code);
596 } else if (scsi_get_serial(udev,
597 &dev_scsi, maj_min_dev, page_code,
598 serial_short, MAX_SERIAL_LEN)) {
605 char serial_str[MAX_SERIAL_LEN];
607 printf("ID_VENDOR=%s\n", vendor_str);
608 printf("ID_MODEL=%s\n", model_str);
609 printf("ID_REVISION=%s\n", revision_str);
610 set_str(serial_str, dev_scsi.serial, sizeof(serial_str));
611 printf("ID_SERIAL=%s\n", serial_str);
612 set_str(serial_str, serial_short, sizeof(serial_str));
613 printf("ID_SERIAL_SHORT=%s\n", serial_str);
614 printf("ID_TYPE=%s\n", type_str);
617 format_serial(dev_scsi.serial);
618 printf("%s\n", dev_scsi.serial);
620 dbg(udev, "%s\n", dev_scsi.serial);
627 int main(int argc, char **argv)
631 char maj_min_dev[MAX_PATH_LEN];
639 logging_init("scsi_id");
640 udev_set_log_fn(udev, log_fn);
643 * Get config file options.
646 retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
651 if (newargv && (retval == 0)) {
652 if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
660 * Get command line options (overriding any config file settings).
662 if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
665 if (!dev_specified) {
666 err(udev, "no device specified\n");
671 retval = scsi_id(udev, maj_min_dev);