2 * Copyright (C) IBM Corp. 2003
3 * Copyright (C) SUSE Linux Products GmbH, 2006
6 * Patrick Mansfield<patmans@us.ibm.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation version 2 of the License.
27 #include "libudev-private.h"
30 static const struct option options[] = {
31 { "device", required_argument, NULL, 'd' },
32 { "config", required_argument, NULL, 'f' },
33 { "page", required_argument, NULL, 'p' },
34 { "blacklisted", no_argument, NULL, 'b' },
35 { "whitelisted", no_argument, NULL, 'g' },
36 { "replace-whitespace", no_argument, NULL, 'u' },
37 { "sg-version", required_argument, NULL, 's' },
38 { "verbose", no_argument, NULL, 'v' },
39 { "version", no_argument, NULL, 'V' },
40 { "export", no_argument, NULL, 'x' },
41 { "help", no_argument, NULL, 'h' },
45 static const char short_options[] = "d:f:ghip:uvVx";
46 static const char dev_short_options[] = "bgp:";
49 static int dev_specified;
50 static char config_file[MAX_PATH_LEN] = SYSCONFDIR "/scsi_id.config";
51 static enum page_code default_page_code;
52 static int sg_version = 4;
53 static int use_stderr;
55 static int reformat_serial;
57 static char vendor_str[64];
58 static char model_str[64];
59 static char vendor_enc_str[256];
60 static char model_enc_str[256];
61 static char revision_str[16];
62 static char type_str[16];
64 static void log_fn(struct udev *udev, int priority,
65 const char *file, int line, const char *fn,
66 const char *format, va_list args)
68 vsyslog(priority, format, args);
71 static void set_type(const char *from, char *to, size_t len)
75 char *type = "generic";
77 type_num = strtoul(from, &eptr, 0);
105 util_strscpy(to, len, type);
111 * buf points to an '=' followed by a quoted string ("foo") or a string ending
112 * with a space or ','.
114 * Return a pointer to the NUL terminated string, returns NULL if no
117 static char *get_value(char **buffer)
119 static char *quote_string = "\"\n";
120 static char *comma_string = ",\n";
124 if (**buffer == '"') {
126 * skip leading quote, terminate when quote seen
133 val = strsep(buffer, end);
134 if (val && end == quote_string)
136 * skip trailing quote
140 while (isspace(**buffer))
146 static int argc_count(char *opts)
149 while (*opts != '\0')
158 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
159 * if vendor and model are set find the first OPTIONS line in the config
160 * file that matches. Set argc and argv to match the OPTIONS string.
162 * vendor and model can end in '\n'.
164 static int get_file_options(struct udev *udev,
165 const char *vendor, const char *model,
166 int *argc, char ***newargv)
172 char *vendor_in, *model_in, *options_in; /* read in from file */
177 dbg(udev, "vendor='%s'; model='%s'\n", vendor, model);
178 fd = fopen(config_file, "r");
180 dbg(udev, "can't open %s\n", config_file);
181 if (errno == ENOENT) {
184 err(udev, "can't open %s: %s\n", config_file, strerror(errno));
190 * Allocate a buffer rather than put it on the stack so we can
191 * keep it around to parse any options (any allocated newargv
192 * points into this buffer for its strings).
194 buffer = malloc(MAX_BUFFER_LEN);
196 err(udev, "can't allocate memory\n");
203 vendor_in = model_in = options_in = NULL;
205 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
209 if (buf[strlen(buffer) - 1] != '\n') {
210 err(udev, "Config file line %d too long\n", lineno);
214 while (isspace(*buf))
217 /* blank or all whitespace line */
225 dbg(udev, "lineno %d: '%s'\n", lineno, buf);
226 str1 = strsep(&buf, "=");
227 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
228 str1 = get_value(&buf);
235 str1 = strsep(&buf, "=");
236 if (str1 && strcasecmp(str1, "MODEL") == 0) {
237 str1 = get_value(&buf);
243 str1 = strsep(&buf, "=");
247 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
248 str1 = get_value(&buf);
255 dbg(udev, "config file line %d:\n"
256 " vendor '%s'; model '%s'; options '%s'\n",
257 lineno, vendor_in, model_in, options_in);
259 * Only allow: [vendor=foo[,model=bar]]options=stuff
261 if (!options_in || (!vendor_in && model_in)) {
262 err(udev, "Error parsing config file line %d '%s'\n", lineno, buffer);
266 if (vendor == NULL) {
267 if (vendor_in == NULL) {
268 dbg(udev, "matched global option\n");
271 } else if ((vendor_in && strncmp(vendor, vendor_in,
272 strlen(vendor_in)) == 0) &&
273 (!model_in || (strncmp(model, model_in,
274 strlen(model_in)) == 0))) {
276 * Matched vendor and optionally model.
278 * Note: a short vendor_in or model_in can
279 * give a partial match (that is FOO
282 dbg(udev, "matched vendor/model\n");
285 dbg(udev, "no match\n");
290 if (vendor_in != NULL || model_in != NULL ||
291 options_in != NULL) {
293 * Something matched. Allocate newargv, and store
294 * values found in options_in.
296 strcpy(buffer, options_in);
297 c = argc_count(buffer) + 2;
298 *newargv = calloc(c, sizeof(**newargv));
300 err(udev, "can't allocate memory\n");
306 * argv[0] at 0 is skipped by getopt, but
307 * store the buffer address there for
310 (*newargv)[c] = buffer;
311 for (c = 1; c < *argc; c++)
312 (*newargv)[c] = strsep(&buffer, " \t");
325 static int set_options(struct udev *udev,
326 int argc, char **argv, const char *short_opts,
332 * optind is a global extern used by getopt. Since we can call
333 * set_options twice (once for command line, and once for config
334 * file) we have to reset this back to 1.
338 option = getopt_long(argc, argv, short_opts, options, NULL);
343 dbg(udev, "option '%c' arg '%s'\n", option, optarg);
345 dbg(udev, "option '%c'\n", option);
354 util_strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
362 util_strscpy(config_file, MAX_PATH_LEN, optarg);
370 printf("Usage: scsi_id OPTIONS <device>\n"
371 " --device= device node for SG_IO commands\n"
372 " --config= location of config file\n"
373 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
374 " --sg-version=3|4 use SGv3 or SGv4\n"
375 " --blacklisted threat device as blacklisted\n"
376 " --whitelisted threat device as whitelisted\n"
377 " --replace-whitespace replace all whitespaces by underscores\n"
378 " --verbose verbose logging\n"
379 " --version print version\n"
380 " --export print values as environment keys\n"
381 " --help print this help text\n\n");
385 if (strcmp(optarg, "0x80") == 0) {
386 default_page_code = PAGE_80;
387 } else if (strcmp(optarg, "0x83") == 0) {
388 default_page_code = PAGE_83;
389 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
390 default_page_code = PAGE_83_PRE_SPC3;
392 err(udev, "Unknown page code '%s'\n", optarg);
398 sg_version = atoi(optarg);
399 if (sg_version < 3 || sg_version > 4) {
400 err(udev, "Unknown SG version '%s'\n", optarg);
418 printf("%s\n", VERSION);
426 if (optind < argc && !dev_specified) {
428 util_strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
433 static int per_dev_options(struct udev *udev,
434 struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
438 char **newargv = NULL;
441 *good_bad = all_good;
442 *page_code = default_page_code;
444 retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
446 optind = 1; /* reset this global extern */
447 while (retval == 0) {
448 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
453 dbg(udev, "option '%c' arg '%s'\n", option, optarg);
455 dbg(udev, "option '%c'\n", option);
467 if (strcmp(optarg, "0x80") == 0) {
468 *page_code = PAGE_80;
469 } else if (strcmp(optarg, "0x83") == 0) {
470 *page_code = PAGE_83;
471 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
472 *page_code = PAGE_83_PRE_SPC3;
474 err(udev, "Unknown page code '%s'\n", optarg);
480 err(udev, "Unknown or bad option '%c' (0x%x)\n", option, option);
493 static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
497 dev_scsi->use_sg = sg_version;
499 retval = scsi_std_inquiry(udev, dev_scsi, path);
503 udev_util_encode_string(dev_scsi->vendor, vendor_enc_str, sizeof(vendor_enc_str));
504 udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
506 udev_util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
507 udev_util_replace_chars(vendor_str, NULL);
508 udev_util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
509 udev_util_replace_chars(model_str, NULL);
510 set_type(dev_scsi->type, type_str, sizeof(type_str));
511 udev_util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
512 udev_util_replace_chars(revision_str, NULL);
517 * scsi_id: try to get an id, if one is found, printf it to stdout.
518 * returns a value passed to exit() - 0 if printed an id, else 1.
520 static int scsi_id(struct udev *udev, char *maj_min_dev)
522 struct scsi_id_device dev_scsi;
527 memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
529 if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
534 /* get per device (vendor + model) options from the config file */
535 per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
536 dbg(udev, "per dev options: good %d; page code 0x%x\n", good_dev, page_code);
542 /* read serial number from mode pages (no values for optical drives) */
543 scsi_get_serial(udev, &dev_scsi, maj_min_dev, page_code, MAX_SERIAL_LEN);
546 char serial_str[MAX_SERIAL_LEN];
548 printf("ID_SCSI=1\n");
549 printf("ID_VENDOR=%s\n", vendor_str);
550 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str);
551 printf("ID_MODEL=%s\n", model_str);
552 printf("ID_MODEL_ENC=%s\n", model_enc_str);
553 printf("ID_REVISION=%s\n", revision_str);
554 printf("ID_TYPE=%s\n", type_str);
555 if (dev_scsi.serial[0] != '\0') {
556 udev_util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
557 udev_util_replace_chars(serial_str, NULL);
558 printf("ID_SERIAL=%s\n", serial_str);
559 udev_util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
560 udev_util_replace_chars(serial_str, NULL);
561 printf("ID_SERIAL_SHORT=%s\n", serial_str);
563 if (dev_scsi.wwn[0] != '\0') {
564 printf("ID_WWN=0x%s\n", dev_scsi.wwn);
565 if (dev_scsi.wwn_vendor_extension[0] != '\0') {
566 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi.wwn_vendor_extension);
567 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi.wwn, dev_scsi.wwn_vendor_extension);
569 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi.wwn);
572 if (dev_scsi.unit_serial_number[0] != '\0') {
573 printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
578 if (dev_scsi.serial[0] == '\0') {
583 if (reformat_serial) {
584 char serial_str[MAX_SERIAL_LEN];
586 udev_util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
587 udev_util_replace_chars(serial_str, NULL);
588 printf("%s\n", serial_str);
592 printf("%s\n", dev_scsi.serial);
597 int main(int argc, char **argv)
601 char maj_min_dev[MAX_PATH_LEN];
609 udev_log_init("scsi_id");
610 udev_set_log_fn(udev, log_fn);
613 * Get config file options.
616 retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
621 if (newargv && (retval == 0)) {
622 if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
630 * Get command line options (overriding any config file settings).
632 if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
635 if (!dev_specified) {
636 err(udev, "no device specified\n");
641 retval = scsi_id(udev, maj_min_dev);