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"
35 #include "udev-util.h"
37 static const struct option options[] = {
38 { "device", required_argument, NULL, 'd' },
39 { "config", required_argument, NULL, 'f' },
40 { "page", required_argument, NULL, 'p' },
41 { "blacklisted", no_argument, NULL, 'b' },
42 { "whitelisted", no_argument, NULL, 'g' },
43 { "replace-whitespace", no_argument, NULL, 'u' },
44 { "sg-version", required_argument, NULL, 's' },
45 { "verbose", no_argument, NULL, 'v' },
46 { "version", no_argument, NULL, 'V' }, /* don't advertise -V */
47 { "export", no_argument, NULL, 'x' },
48 { "help", no_argument, NULL, 'h' },
52 static bool all_good = false;
53 static bool dev_specified = false;
54 static char config_file[MAX_PATH_LEN] = "/etc/scsi_id.config";
55 static enum page_code default_page_code = PAGE_UNSPECIFIED;
56 static int sg_version = 4;
57 static bool reformat_serial = false;
58 static bool export = false;
59 static char vendor_str[64];
60 static char model_str[64];
61 static char vendor_enc_str[256];
62 static char model_enc_str[256];
63 static char revision_str[16];
64 static char type_str[16];
66 static void set_type(const char *from, char *to, size_t len)
70 const char *type = "generic";
72 type_num = strtoul(from, &eptr, 0);
100 strscpy(to, len, type);
106 * buf points to an '=' followed by a quoted string ("foo") or a string ending
107 * with a space or ','.
109 * Return a pointer to the NUL terminated string, returns NULL if no
112 static char *get_value(char **buffer)
114 static const char *quote_string = "\"\n";
115 static const char *comma_string = ",\n";
119 if (**buffer == '"') {
121 * skip leading quote, terminate when quote seen
128 val = strsep(buffer, end);
129 if (val && end == quote_string)
131 * skip trailing quote
135 while (isspace(**buffer))
141 static int argc_count(char *opts)
144 while (*opts != '\0')
153 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
154 * if vendor and model are set find the first OPTIONS line in the config
155 * file that matches. Set argc and argv to match the OPTIONS string.
157 * vendor and model can end in '\n'.
159 static int get_file_options(struct udev *udev,
160 const char *vendor, const char *model,
161 int *argc, char ***newargv)
164 _cleanup_fclose_ FILE *f;
167 char *vendor_in, *model_in, *options_in; /* read in from file */
172 f = fopen(config_file, "re");
177 log_error("can't open %s: %m", config_file);
183 * Allocate a buffer rather than put it on the stack so we can
184 * keep it around to parse any options (any allocated newargv
185 * points into this buffer for its strings).
187 buffer = malloc(MAX_BUFFER_LEN);
194 vendor_in = model_in = options_in = NULL;
196 buf = fgets(buffer, MAX_BUFFER_LEN, f);
200 if (buf[strlen(buffer) - 1] != '\n') {
201 log_error("Config file line %d too long", lineno);
205 while (isspace(*buf))
208 /* blank or all whitespace line */
216 str1 = strsep(&buf, "=");
217 if (str1 && strcaseeq(str1, "VENDOR")) {
218 str1 = get_value(&buf);
225 str1 = strsep(&buf, "=");
226 if (str1 && strcaseeq(str1, "MODEL")) {
227 str1 = get_value(&buf);
233 str1 = strsep(&buf, "=");
237 if (str1 && strcaseeq(str1, "OPTIONS")) {
238 str1 = get_value(&buf);
247 * Only allow: [vendor=foo[,model=bar]]options=stuff
249 if (!options_in || (!vendor_in && model_in)) {
250 log_error("Error parsing config file line %d '%s'", lineno, buffer);
254 if (vendor == NULL) {
255 if (vendor_in == NULL)
257 } else if (vendor_in &&
258 strneq(vendor, vendor_in, strlen(vendor_in)) &&
260 (strneq(model, model_in, strlen(model_in))))) {
262 * Matched vendor and optionally model.
264 * Note: a short vendor_in or model_in can
265 * give a partial match (that is FOO
273 if (vendor_in != NULL || model_in != NULL ||
274 options_in != NULL) {
276 * Something matched. Allocate newargv, and store
277 * values found in options_in.
279 strcpy(buffer, options_in);
280 c = argc_count(buffer) + 2;
281 *newargv = calloc(c, sizeof(**newargv));
288 * argv[0] at 0 is skipped by getopt, but
289 * store the buffer address there for
292 (*newargv)[c] = buffer;
293 for (c = 1; c < *argc; c++)
294 (*newargv)[c] = strsep(&buffer, " \t");
306 static void help(void) {
307 printf("Usage: scsi_id [OPTION...] DEVICE\n"
308 " -d,--device= device node for SG_IO commands\n"
309 " -f,--config= location of config file\n"
310 " -p,--page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
311 " -s,--sg-version=3|4 use SGv3 or SGv4\n"
312 " -b,--blacklisted treat device as blacklisted\n"
313 " -g,--whitelisted treat device as whitelisted\n"
314 " -u,--replace-whitespace replace all whitespace by underscores\n"
315 " -v,--verbose verbose logging\n"
316 " --version print version\n"
317 " -x,--export print values as environment keys\n"
318 " -h,--help print this help text\n\n");
322 static int set_options(struct udev *udev,
323 int argc, char **argv,
329 * optind is a global extern used by getopt. Since we can call
330 * set_options twice (once for command line, and once for config
331 * file) we have to reset this back to 1.
334 while ((option = getopt_long(argc, argv, "d:f:gp:uvVxh", options, NULL)) >= 0)
341 dev_specified = true;
342 strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
346 strscpy(config_file, MAX_PATH_LEN, optarg);
358 if (streq(optarg, "0x80"))
359 default_page_code = PAGE_80;
360 else if (streq(optarg, "0x83"))
361 default_page_code = PAGE_83;
362 else if (streq(optarg, "pre-spc3-83"))
363 default_page_code = PAGE_83_PRE_SPC3;
365 log_error("Unknown page code '%s'", optarg);
371 sg_version = atoi(optarg);
372 if (sg_version < 3 || sg_version > 4) {
373 log_error("Unknown SG version '%s'", optarg);
379 reformat_serial = true;
383 log_set_target(LOG_TARGET_CONSOLE);
384 log_set_max_level(LOG_DEBUG);
389 printf("%s\n", VERSION);
400 assert_not_reached("Unknown option");
403 if (optind < argc && !dev_specified) {
404 dev_specified = true;
405 strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
411 static int per_dev_options(struct udev *udev,
412 struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
416 char **newargv = NULL;
419 *good_bad = all_good;
420 *page_code = default_page_code;
422 retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
424 optind = 1; /* reset this global extern */
425 while (retval == 0) {
426 option = getopt_long(newargc, newargv, "bgp:", options, NULL);
440 if (streq(optarg, "0x80")) {
441 *page_code = PAGE_80;
442 } else if (streq(optarg, "0x83")) {
443 *page_code = PAGE_83;
444 } else if (streq(optarg, "pre-spc3-83")) {
445 *page_code = PAGE_83_PRE_SPC3;
447 log_error("Unknown page code '%s'", optarg);
453 log_error("Unknown or bad option '%c' (0x%x)", option, option);
466 static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
470 dev_scsi->use_sg = sg_version;
472 retval = scsi_std_inquiry(udev, dev_scsi, path);
476 udev_util_encode_string(dev_scsi->vendor, vendor_enc_str, sizeof(vendor_enc_str));
477 udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
479 util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
480 util_replace_chars(vendor_str, NULL);
481 util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
482 util_replace_chars(model_str, NULL);
483 set_type(dev_scsi->type, type_str, sizeof(type_str));
484 util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
485 util_replace_chars(revision_str, NULL);
490 * scsi_id: try to get an id, if one is found, printf it to stdout.
491 * returns a value passed to exit() - 0 if printed an id, else 1.
493 static int scsi_id(struct udev *udev, char *maj_min_dev)
495 struct scsi_id_device dev_scsi = {};
500 if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
505 /* get per device (vendor + model) options from the config file */
506 per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
512 /* read serial number from mode pages (no values for optical drives) */
513 scsi_get_serial(udev, &dev_scsi, maj_min_dev, page_code, MAX_SERIAL_LEN);
516 char serial_str[MAX_SERIAL_LEN];
518 printf("ID_SCSI=1\n");
519 printf("ID_VENDOR=%s\n", vendor_str);
520 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str);
521 printf("ID_MODEL=%s\n", model_str);
522 printf("ID_MODEL_ENC=%s\n", model_enc_str);
523 printf("ID_REVISION=%s\n", revision_str);
524 printf("ID_TYPE=%s\n", type_str);
525 if (dev_scsi.serial[0] != '\0') {
526 util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
527 util_replace_chars(serial_str, NULL);
528 printf("ID_SERIAL=%s\n", serial_str);
529 util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
530 util_replace_chars(serial_str, NULL);
531 printf("ID_SERIAL_SHORT=%s\n", serial_str);
533 if (dev_scsi.wwn[0] != '\0') {
534 printf("ID_WWN=0x%s\n", dev_scsi.wwn);
535 if (dev_scsi.wwn_vendor_extension[0] != '\0') {
536 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi.wwn_vendor_extension);
537 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi.wwn, dev_scsi.wwn_vendor_extension);
539 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi.wwn);
542 if (dev_scsi.tgpt_group[0] != '\0') {
543 printf("ID_TARGET_PORT=%s\n", dev_scsi.tgpt_group);
545 if (dev_scsi.unit_serial_number[0] != '\0') {
546 printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
551 if (dev_scsi.serial[0] == '\0') {
556 if (reformat_serial) {
557 char serial_str[MAX_SERIAL_LEN];
559 util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
560 util_replace_chars(serial_str, NULL);
561 printf("%s\n", serial_str);
565 printf("%s\n", dev_scsi.serial);
570 int main(int argc, char **argv)
572 _cleanup_udev_unref_ struct udev *udev;
574 char maj_min_dev[MAX_PATH_LEN];
576 char **newargv = NULL;
578 log_parse_environment();
586 * Get config file options.
588 retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
596 if (set_options(udev, newargc, newargv, maj_min_dev) < 0) {
603 * Get command line options (overriding any config file settings).
605 if (set_options(udev, argc, argv, maj_min_dev) < 0)
608 if (!dev_specified) {
609 log_error("no device specified");
614 retval = scsi_id(udev, maj_min_dev);