scsi_id.c revision 4b0060e68bab7bddbe8968082af8e042b8c11316
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Copyright (C) IBM Corp. 2003
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Copyright (C) SUSE Linux Products GmbH, 2006
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * This program is free software: you can redistribute it and/or modify
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * it under the terms of the GNU General Public License as published by
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * (at your option) any later version.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * This program is distributed in the hope that it will be useful,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * GNU General Public License for more details.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * You should have received a copy of the GNU General Public License
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering { "device", required_argument, NULL, 'd' },
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering { "config", required_argument, NULL, 'f' },
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering { "page", required_argument, NULL, 'p' },
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering { "blacklisted", no_argument, NULL, 'b' },
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering { "whitelisted", no_argument, NULL, 'g' },
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering { "replace-whitespace", no_argument, NULL, 'u' },
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering { "sg-version", required_argument, NULL, 's' },
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic const char short_options[] = "d:f:ghip:uvVx";
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic const char dev_short_options[] = "bgp:";
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic char config_file[MAX_PATH_LEN] = SYSCONFDIR "/scsi_id.config";
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poetteringstatic void log_fn(struct udev *udev, int priority,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering const char *file, int line, const char *fn,
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic void set_type(const char *from, char *to, size_t len)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * buf points to an '=' followed by a quoted string ("foo") or a string ending
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * with a space or ','.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Return a pointer to the NUL terminated string, returns NULL if no
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering static const char *quote_string = "\"\n";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * skip leading quote, terminate when quote seen
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * skip trailing quote
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * get_file_options:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * If vendor == NULL, find a line in the config file with only "OPTIONS=";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * if vendor and model are set find the first OPTIONS line in the config
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * file that matches. Set argc and argv to match the OPTIONS string.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * vendor and model can end in '\n'.
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int get_file_options(struct udev *udev,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering char *vendor_in, *model_in, *options_in; /* read in from file */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "vendor='%s'; model='%s'\n", vendor, model);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "can't open %s\n", config_file);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering err(udev, "can't open %s: %s\n", config_file, strerror(errno));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Allocate a buffer rather than put it on the stack so we can
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * keep it around to parse any options (any allocated newargv
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * points into this buffer for its strings).
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering vendor_in = model_in = options_in = NULL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering err(udev, "Config file line %d too long\n", lineno);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* blank or all whitespace line */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* comment line */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "lineno %d: '%s'\n", lineno, buf);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (str1 && strcasecmp(str1, "VENDOR") == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (str1 && strcasecmp(str1, "MODEL") == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " vendor '%s'; model '%s'; options '%s'\n",
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering lineno, vendor_in, model_in, options_in);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Only allow: [vendor=foo[,model=bar]]options=stuff
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (!options_in || (!vendor_in && model_in)) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering err(udev, "Error parsing config file line %d '%s'\n", lineno, buffer);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else if ((vendor_in && strncmp(vendor, vendor_in,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Matched vendor and optionally model.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Note: a short vendor_in or model_in can
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * give a partial match (that is FOO
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * matches FOOBAR).
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (vendor_in != NULL || model_in != NULL ||
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Something matched. Allocate newargv, and store
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * values found in options_in.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * argv[0] at 0 is skipped by getopt, but
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * store the buffer address there for
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * later freeing
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* No matches */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int argc, char **argv, const char *short_opts,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * optind is a global extern used by getopt. Since we can call
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * set_options twice (once for command line, and once for config
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * file) we have to reset this back to 1.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering option = getopt_long(argc, argv, short_opts, options, NULL);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "option '%c' arg '%s'\n", option, optarg);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_strscpy(config_file, MAX_PATH_LEN, optarg);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering printf("Usage: scsi_id OPTIONS <device>\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " --device= device node for SG_IO commands\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " --config= location of config file\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " --sg-version=3|4 use SGv3 or SGv4\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " --blacklisted threat device as blacklisted\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " --whitelisted threat device as whitelisted\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " --replace-whitespace replace all whitespaces by underscores\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " --verbose verbose logging\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " --version print version\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " --export print values as environment keys\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " --help print this help text\n\n");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else if (strcmp(optarg, "0x83") == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else if (strcmp(optarg, "pre-spc3-83") == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering err(udev, "Unknown page code '%s'\n", optarg);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering err(udev, "Unknown SG version '%s'\n", optarg);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int per_dev_options(struct udev *udev,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering optind = 1; /* reset this global extern */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering while (retval == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering dbg(udev, "option '%c' arg '%s'\n", option, optarg);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering } else if (strcmp(optarg, "0x83") == 0) {
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering } else if (strcmp(optarg, "pre-spc3-83") == 0) {
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering err(udev, "Unknown page code '%s'\n", optarg);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering err(udev, "Unknown or bad option '%c' (0x%x)\n", option, option);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poetteringstatic int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering retval = scsi_std_inquiry(udev, dev_scsi, path);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering udev_util_encode_string(dev_scsi->vendor, vendor_enc_str, sizeof(vendor_enc_str));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering set_type(dev_scsi->type, type_str, sizeof(type_str));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * scsi_id: try to get an id, if one is found, printf it to stdout.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * returns a value passed to exit() - 0 if printed an id, else 1.
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int scsi_id(struct udev *udev, char *maj_min_dev)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* get per device (vendor + model) options from the config file */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "per dev options: good %d; page code 0x%x\n", good_dev, page_code);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering /* read serial number from mode pages (no values for optical drives) */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering scsi_get_serial(udev, &dev_scsi, maj_min_dev, page_code, MAX_SERIAL_LEN);
if (export) {
goto out;
goto out;
if (reformat_serial) {
goto out;
out:
return retval;
int retval = 0;
int newargc;
char **newargv;
goto exit;
if (retval < 0) {
goto exit;
goto exit;
if (!dev_specified) {
goto exit;
exit:
return retval;