1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
5 * Main section of the scsi_id program
7 * Copyright (C) IBM Corp. 2003
8 * Copyright (C) SUSE Linux Products GmbH, 2006
11 * Patrick Mansfield<patmans@us.ibm.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation version 2 of the License.
32 #include "libudev-private.h"
35 static const struct option options[] = {
36 { "device", required_argument, NULL, 'd' },
37 { "config", required_argument, NULL, 'f' },
38 { "page", required_argument, NULL, 'p' },
39 { "blacklisted", no_argument, NULL, 'b' },
40 { "whitelisted", no_argument, NULL, 'g' },
41 { "replace-whitespace", no_argument, NULL, 'u' },
42 { "sg-version", required_argument, NULL, 's' },
43 { "verbose", no_argument, NULL, 'v' },
44 { "version", no_argument, NULL, 'V' },
45 { "export", no_argument, NULL, 'x' },
46 { "help", no_argument, NULL, 'h' },
50 static const char short_options[] = "d:f:ghip:uvVx";
51 static const char dev_short_options[] = "bgp:";
54 static int dev_specified;
55 static char config_file[MAX_PATH_LEN] = SYSCONFDIR "/scsi_id.config";
56 static enum page_code default_page_code;
57 static int sg_version = 4;
58 static int use_stderr;
60 static int reformat_serial;
62 static char vendor_str[64];
63 static char model_str[64];
64 static char vendor_enc_str[256];
65 static char model_enc_str[256];
66 static char revision_str[16];
67 static char type_str[16];
69 static void log_fn(struct udev *udev, int priority,
70 const char *file, int line, const char *fn,
71 const char *format, va_list args)
73 vsyslog(priority, format, args);
76 static void set_type(const char *from, char *to, size_t len)
80 char *type = "generic";
82 type_num = strtoul(from, &eptr, 0);
110 util_strscpy(to, len, type);
116 * buf points to an '=' followed by a quoted string ("foo") or a string ending
117 * with a space or ','.
119 * Return a pointer to the NUL terminated string, returns NULL if no
122 static char *get_value(char **buffer)
124 static char *quote_string = "\"\n";
125 static char *comma_string = ",\n";
129 if (**buffer == '"') {
131 * skip leading quote, terminate when quote seen
138 val = strsep(buffer, end);
139 if (val && end == quote_string)
141 * skip trailing quote
145 while (isspace(**buffer))
151 static int argc_count(char *opts)
154 while (*opts != '\0')
163 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
164 * if vendor and model are set find the first OPTIONS line in the config
165 * file that matches. Set argc and argv to match the OPTIONS string.
167 * vendor and model can end in '\n'.
169 static int get_file_options(struct udev *udev,
170 const char *vendor, const char *model,
171 int *argc, char ***newargv)
177 char *vendor_in, *model_in, *options_in; /* read in from file */
182 dbg(udev, "vendor='%s'; model='%s'\n", vendor, model);
183 fd = fopen(config_file, "r");
185 dbg(udev, "can't open %s\n", config_file);
186 if (errno == ENOENT) {
189 err(udev, "can't open %s: %s\n", config_file, strerror(errno));
195 * Allocate a buffer rather than put it on the stack so we can
196 * keep it around to parse any options (any allocated newargv
197 * points into this buffer for its strings).
199 buffer = malloc(MAX_BUFFER_LEN);
201 err(udev, "can't allocate memory\n");
208 vendor_in = model_in = options_in = NULL;
210 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
214 if (buf[strlen(buffer) - 1] != '\n') {
215 err(udev, "Config file line %d too long\n", lineno);
219 while (isspace(*buf))
222 /* blank or all whitespace line */
230 dbg(udev, "lineno %d: '%s'\n", lineno, buf);
231 str1 = strsep(&buf, "=");
232 if (str1 && strcasecmp(str1, "VENDOR") == 0) {
233 str1 = get_value(&buf);
240 str1 = strsep(&buf, "=");
241 if (str1 && strcasecmp(str1, "MODEL") == 0) {
242 str1 = get_value(&buf);
248 str1 = strsep(&buf, "=");
252 if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
253 str1 = get_value(&buf);
260 dbg(udev, "config file line %d:\n"
261 " vendor '%s'; model '%s'; options '%s'\n",
262 lineno, vendor_in, model_in, options_in);
264 * Only allow: [vendor=foo[,model=bar]]options=stuff
266 if (!options_in || (!vendor_in && model_in)) {
267 err(udev, "Error parsing config file line %d '%s'\n", lineno, buffer);
271 if (vendor == NULL) {
272 if (vendor_in == NULL) {
273 dbg(udev, "matched global option\n");
276 } else if ((vendor_in && strncmp(vendor, vendor_in,
277 strlen(vendor_in)) == 0) &&
278 (!model_in || (strncmp(model, model_in,
279 strlen(model_in)) == 0))) {
281 * Matched vendor and optionally model.
283 * Note: a short vendor_in or model_in can
284 * give a partial match (that is FOO
287 dbg(udev, "matched vendor/model\n");
290 dbg(udev, "no match\n");
295 if (vendor_in != NULL || model_in != NULL ||
296 options_in != NULL) {
298 * Something matched. Allocate newargv, and store
299 * values found in options_in.
301 strcpy(buffer, options_in);
302 c = argc_count(buffer) + 2;
303 *newargv = calloc(c, sizeof(**newargv));
305 err(udev, "can't allocate memory\n");
311 * argv[0] at 0 is skipped by getopt, but
312 * store the buffer address there for
315 (*newargv)[c] = buffer;
316 for (c = 1; c < *argc; c++)
317 (*newargv)[c] = strsep(&buffer, " \t");
330 static int set_options(struct udev *udev,
331 int argc, char **argv, const char *short_opts,
337 * optind is a global extern used by getopt. Since we can call
338 * set_options twice (once for command line, and once for config
339 * file) we have to reset this back to 1.
343 option = getopt_long(argc, argv, short_opts, options, NULL);
348 dbg(udev, "option '%c' arg '%s'\n", option, optarg);
350 dbg(udev, "option '%c'\n", option);
359 util_strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
367 util_strscpy(config_file, MAX_PATH_LEN, optarg);
375 printf("Usage: scsi_id OPTIONS <device>\n"
376 " --device= device node for SG_IO commands\n"
377 " --config= location of config file\n"
378 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
379 " --sg-version=3|4 use SGv3 or SGv4\n"
380 " --blacklisted threat device as blacklisted\n"
381 " --whitelisted threat device as whitelisted\n"
382 " --replace-whitespace replace all whitespaces by underscores\n"
383 " --verbose verbose logging\n"
384 " --version print version\n"
385 " --export print values as environment keys\n"
386 " --help print this help text\n\n");
390 if (strcmp(optarg, "0x80") == 0) {
391 default_page_code = PAGE_80;
392 } else if (strcmp(optarg, "0x83") == 0) {
393 default_page_code = PAGE_83;
394 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
395 default_page_code = PAGE_83_PRE_SPC3;
397 err(udev, "Unknown page code '%s'\n", optarg);
403 sg_version = atoi(optarg);
404 if (sg_version < 3 || sg_version > 4) {
405 err(udev, "Unknown SG version '%s'\n", optarg);
423 printf("%s\n", VERSION);
431 if (optind < argc && !dev_specified) {
433 util_strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
438 static int per_dev_options(struct udev *udev,
439 struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
443 char **newargv = NULL;
446 *good_bad = all_good;
447 *page_code = default_page_code;
449 retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
451 optind = 1; /* reset this global extern */
452 while (retval == 0) {
453 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
458 dbg(udev, "option '%c' arg '%s'\n", option, optarg);
460 dbg(udev, "option '%c'\n", option);
472 if (strcmp(optarg, "0x80") == 0) {
473 *page_code = PAGE_80;
474 } else if (strcmp(optarg, "0x83") == 0) {
475 *page_code = PAGE_83;
476 } else if (strcmp(optarg, "pre-spc3-83") == 0) {
477 *page_code = PAGE_83_PRE_SPC3;
479 err(udev, "Unknown page code '%s'\n", optarg);
485 err(udev, "Unknown or bad option '%c' (0x%x)\n", option, option);
498 static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
502 dev_scsi->use_sg = sg_version;
504 retval = scsi_std_inquiry(udev, dev_scsi, path);
508 udev_util_encode_string(dev_scsi->vendor, vendor_enc_str, sizeof(vendor_enc_str));
509 udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
511 udev_util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
512 udev_util_replace_chars(vendor_str, NULL);
513 udev_util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
514 udev_util_replace_chars(model_str, NULL);
515 set_type(dev_scsi->type, type_str, sizeof(type_str));
516 udev_util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
517 udev_util_replace_chars(revision_str, NULL);
522 * scsi_id: try to get an id, if one is found, printf it to stdout.
523 * returns a value passed to exit() - 0 if printed an id, else 1.
525 static int scsi_id(struct udev *udev, char *maj_min_dev)
527 struct scsi_id_device dev_scsi;
532 memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
534 if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
539 /* get per device (vendor + model) options from the config file */
540 per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
541 dbg(udev, "per dev options: good %d; page code 0x%x\n", good_dev, page_code);
547 /* read serial number from mode pages (no values for optical drives) */
548 scsi_get_serial(udev, &dev_scsi, maj_min_dev, page_code, MAX_SERIAL_LEN);
551 char serial_str[MAX_SERIAL_LEN];
553 printf("ID_SCSI=1\n");
554 printf("ID_VENDOR=%s\n", vendor_str);
555 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str);
556 printf("ID_MODEL=%s\n", model_str);
557 printf("ID_MODEL_ENC=%s\n", model_enc_str);
558 printf("ID_REVISION=%s\n", revision_str);
559 printf("ID_TYPE=%s\n", type_str);
560 if (dev_scsi.serial[0] != '\0') {
561 udev_util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
562 udev_util_replace_chars(serial_str, NULL);
563 printf("ID_SERIAL=%s\n", serial_str);
564 udev_util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
565 udev_util_replace_chars(serial_str, NULL);
566 printf("ID_SERIAL_SHORT=%s\n", serial_str);
568 if (dev_scsi.wwn[0] != '\0') {
569 printf("ID_WWN=0x%s\n", dev_scsi.wwn);
570 if (dev_scsi.wwn_vendor_extension[0] != '\0') {
571 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi.wwn_vendor_extension);
572 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi.wwn, dev_scsi.wwn_vendor_extension);
574 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi.wwn);
577 if (dev_scsi.unit_serial_number[0] != '\0') {
578 printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
583 if (dev_scsi.serial[0] == '\0') {
588 if (reformat_serial) {
589 char serial_str[MAX_SERIAL_LEN];
591 udev_util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
592 udev_util_replace_chars(serial_str, NULL);
593 printf("%s\n", serial_str);
597 printf("%s\n", dev_scsi.serial);
602 int main(int argc, char **argv)
606 char maj_min_dev[MAX_PATH_LEN];
614 udev_log_init("scsi_id");
615 udev_set_log_fn(udev, log_fn);
618 * Get config file options.
621 retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
626 if (newargv && (retval == 0)) {
627 if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
635 * Get command line options (overriding any config file settings).
637 if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
640 if (!dev_specified) {
641 err(udev, "no device specified\n");
646 retval = scsi_id(udev, maj_min_dev);