2 * Copyright (C) IBM Corp. 2003
3 * Copyright (C) SUSE Linux Products GmbH, 2006
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "libudev-private.h"
36 static const struct option options[] = {
37 { "device", required_argument, NULL, 'd' },
38 { "config", required_argument, NULL, 'f' },
39 { "page", required_argument, NULL, 'p' },
40 { "blacklisted", no_argument, NULL, 'b' },
41 { "whitelisted", no_argument, NULL, 'g' },
42 { "replace-whitespace", no_argument, NULL, 'u' },
43 { "sg-version", required_argument, NULL, 's' },
44 { "verbose", no_argument, NULL, 'v' },
45 { "version", no_argument, NULL, 'V' },
46 { "export", no_argument, NULL, 'x' },
47 { "help", no_argument, NULL, 'h' },
51 static const char short_options[] = "d:f:ghip:uvVx";
52 static const char dev_short_options[] = "bgp:";
55 static int dev_specified;
56 static char config_file[MAX_PATH_LEN] = "/etc/scsi_id.config";
57 static enum page_code default_page_code;
58 static int sg_version = 4;
59 static int use_stderr;
61 static int reformat_serial;
63 static char vendor_str[64];
64 static char model_str[64];
65 static char vendor_enc_str[256];
66 static char model_enc_str[256];
67 static char revision_str[16];
68 static char type_str[16];
70 static void log_fn(struct udev *udev, int priority,
71 const char *file, int line, const char *fn,
72 const char *format, va_list args)
74 vsyslog(priority, format, args);
77 static void set_type(const char *from, char *to, size_t len)
81 const char *type = "generic";
83 type_num = strtoul(from, &eptr, 0);
111 strscpy(to, len, type);
117 * buf points to an '=' followed by a quoted string ("foo") or a string ending
118 * with a space or ','.
120 * Return a pointer to the NUL terminated string, returns NULL if no
123 static char *get_value(char **buffer)
125 static const char *quote_string = "\"\n";
126 static const char *comma_string = ",\n";
130 if (**buffer == '"') {
132 * skip leading quote, terminate when quote seen
139 val = strsep(buffer, end);
140 if (val && end == quote_string)
142 * skip trailing quote
146 while (isspace(**buffer))
152 static int argc_count(char *opts)
155 while (*opts != '\0')
164 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
165 * if vendor and model are set find the first OPTIONS line in the config
166 * file that matches. Set argc and argv to match the OPTIONS string.
168 * vendor and model can end in '\n'.
170 static int get_file_options(struct udev *udev,
171 const char *vendor, const char *model,
172 int *argc, char ***newargv)
178 char *vendor_in, *model_in, *options_in; /* read in from file */
183 fd = fopen(config_file, "re");
185 if (errno == ENOENT) {
188 log_error("can't open %s: %m\n", config_file);
194 * Allocate a buffer rather than put it on the stack so we can
195 * keep it around to parse any options (any allocated newargv
196 * points into this buffer for its strings).
198 buffer = malloc(MAX_BUFFER_LEN);
207 vendor_in = model_in = options_in = NULL;
209 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
213 if (buf[strlen(buffer) - 1] != '\n') {
214 log_error("Config file line %d too long\n", lineno);
218 while (isspace(*buf))
221 /* blank or all whitespace line */
229 str1 = strsep(&buf, "=");
230 if (str1 && strcaseeq(str1, "VENDOR")) {
231 str1 = get_value(&buf);
238 str1 = strsep(&buf, "=");
239 if (str1 && strcaseeq(str1, "MODEL")) {
240 str1 = get_value(&buf);
246 str1 = strsep(&buf, "=");
250 if (str1 && strcaseeq(str1, "OPTIONS")) {
251 str1 = get_value(&buf);
260 * Only allow: [vendor=foo[,model=bar]]options=stuff
262 if (!options_in || (!vendor_in && model_in)) {
263 log_error("Error parsing config file line %d '%s'\n", lineno, buffer);
267 if (vendor == NULL) {
268 if (vendor_in == NULL)
270 } else if ((vendor_in && strneq(vendor, vendor_in,
271 strlen(vendor_in))) &&
272 (!model_in || (strneq(model, model_in,
273 strlen(model_in))))) {
275 * Matched vendor and optionally model.
277 * Note: a short vendor_in or model_in can
278 * give a partial match (that is FOO
286 if (vendor_in != NULL || model_in != NULL ||
287 options_in != NULL) {
289 * Something matched. Allocate newargv, and store
290 * values found in options_in.
292 strcpy(buffer, options_in);
293 c = argc_count(buffer) + 2;
294 *newargv = calloc(c, sizeof(**newargv));
301 * argv[0] at 0 is skipped by getopt, but
302 * store the buffer address there for
305 (*newargv)[c] = buffer;
306 for (c = 1; c < *argc; c++)
307 (*newargv)[c] = strsep(&buffer, " \t");
320 static int set_options(struct udev *udev,
321 int argc, char **argv, const char *short_opts,
327 * optind is a global extern used by getopt. Since we can call
328 * set_options twice (once for command line, and once for config
329 * file) we have to reset this back to 1.
333 option = getopt_long(argc, argv, short_opts, options, NULL);
344 strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
352 strscpy(config_file, MAX_PATH_LEN, optarg);
360 printf("Usage: scsi_id OPTIONS <device>\n"
361 " --device= device node for SG_IO commands\n"
362 " --config= location of config file\n"
363 " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
364 " --sg-version=3|4 use SGv3 or SGv4\n"
365 " --blacklisted threat device as blacklisted\n"
366 " --whitelisted threat device as whitelisted\n"
367 " --replace-whitespace replace all whitespaces by underscores\n"
368 " --verbose verbose logging\n"
369 " --version print version\n"
370 " --export print values as environment keys\n"
371 " --help print this help text\n\n");
375 if (streq(optarg, "0x80")) {
376 default_page_code = PAGE_80;
377 } else if (streq(optarg, "0x83")) {
378 default_page_code = PAGE_83;
379 } else if (streq(optarg, "pre-spc3-83")) {
380 default_page_code = PAGE_83_PRE_SPC3;
382 log_error("Unknown page code '%s'\n", optarg);
388 sg_version = atoi(optarg);
389 if (sg_version < 3 || sg_version > 4) {
390 log_error("Unknown SG version '%s'\n", optarg);
408 printf("%s\n", VERSION);
416 if (optind < argc && !dev_specified) {
418 strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
423 static int per_dev_options(struct udev *udev,
424 struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
428 char **newargv = NULL;
431 *good_bad = all_good;
432 *page_code = default_page_code;
434 retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
436 optind = 1; /* reset this global extern */
437 while (retval == 0) {
438 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
452 if (streq(optarg, "0x80")) {
453 *page_code = PAGE_80;
454 } else if (streq(optarg, "0x83")) {
455 *page_code = PAGE_83;
456 } else if (streq(optarg, "pre-spc3-83")) {
457 *page_code = PAGE_83_PRE_SPC3;
459 log_error("Unknown page code '%s'\n", optarg);
465 log_error("Unknown or bad option '%c' (0x%x)\n", option, option);
478 static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
482 dev_scsi->use_sg = sg_version;
484 retval = scsi_std_inquiry(udev, dev_scsi, path);
488 udev_util_encode_string(dev_scsi->vendor, vendor_enc_str, sizeof(vendor_enc_str));
489 udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
491 util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
492 util_replace_chars(vendor_str, NULL);
493 util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
494 util_replace_chars(model_str, NULL);
495 set_type(dev_scsi->type, type_str, sizeof(type_str));
496 util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
497 util_replace_chars(revision_str, NULL);
502 * scsi_id: try to get an id, if one is found, printf it to stdout.
503 * returns a value passed to exit() - 0 if printed an id, else 1.
505 static int scsi_id(struct udev *udev, char *maj_min_dev)
507 struct scsi_id_device dev_scsi;
512 memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
514 if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
519 /* get per device (vendor + model) options from the config file */
520 per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
526 /* read serial number from mode pages (no values for optical drives) */
527 scsi_get_serial(udev, &dev_scsi, maj_min_dev, page_code, MAX_SERIAL_LEN);
530 char serial_str[MAX_SERIAL_LEN];
532 printf("ID_SCSI=1\n");
533 printf("ID_VENDOR=%s\n", vendor_str);
534 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str);
535 printf("ID_MODEL=%s\n", model_str);
536 printf("ID_MODEL_ENC=%s\n", model_enc_str);
537 printf("ID_REVISION=%s\n", revision_str);
538 printf("ID_TYPE=%s\n", type_str);
539 if (dev_scsi.serial[0] != '\0') {
540 util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
541 util_replace_chars(serial_str, NULL);
542 printf("ID_SERIAL=%s\n", serial_str);
543 util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
544 util_replace_chars(serial_str, NULL);
545 printf("ID_SERIAL_SHORT=%s\n", serial_str);
547 if (dev_scsi.wwn[0] != '\0') {
548 printf("ID_WWN=0x%s\n", dev_scsi.wwn);
549 if (dev_scsi.wwn_vendor_extension[0] != '\0') {
550 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi.wwn_vendor_extension);
551 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi.wwn, dev_scsi.wwn_vendor_extension);
553 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi.wwn);
556 if (dev_scsi.tgpt_group[0] != '\0') {
557 printf("ID_TARGET_PORT=%s\n", dev_scsi.tgpt_group);
559 if (dev_scsi.unit_serial_number[0] != '\0') {
560 printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
565 if (dev_scsi.serial[0] == '\0') {
570 if (reformat_serial) {
571 char serial_str[MAX_SERIAL_LEN];
573 util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
574 util_replace_chars(serial_str, NULL);
575 printf("%s\n", serial_str);
579 printf("%s\n", dev_scsi.serial);
584 int main(int argc, char **argv)
588 char maj_min_dev[MAX_PATH_LEN];
597 udev_set_log_fn(udev, log_fn);
600 * Get config file options.
603 retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
608 if (newargv && (retval == 0)) {
609 if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
617 * Get command line options (overriding any config file settings).
619 if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
622 if (!dev_specified) {
623 log_error("no device specified\n");
628 retval = scsi_id(udev, maj_min_dev);