scsi_id.c revision 4b0060e68bab7bddbe8968082af8e042b8c11316
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering/*
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Copyright (C) IBM Corp. 2003
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Copyright (C) SUSE Linux Products GmbH, 2006
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering *
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 *
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 *
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 */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <stdio.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <stdlib.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <unistd.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <signal.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <fcntl.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <errno.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <string.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <syslog.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <stdarg.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <ctype.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <getopt.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include <sys/stat.h>
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include "libudev.h"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include "libudev-private.h"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering#include "scsi_id.h"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic const struct option options[] = {
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 Poettering { "verbose", no_argument, NULL, 'v' },
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering { "version", no_argument, NULL, 'V' },
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering { "export", no_argument, NULL, 'x' },
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering { "help", no_argument, NULL, 'h' },
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering {}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering};
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic const char short_options[] = "d:f:ghip:uvVx";
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic const char dev_short_options[] = "bgp:";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int all_good;
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int dev_specified;
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic char config_file[MAX_PATH_LEN] = SYSCONFDIR "/scsi_id.config";
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic enum page_code default_page_code;
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int sg_version = 4;
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int use_stderr;
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int debug;
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int reformat_serial;
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int export;
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic char vendor_str[64];
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic char model_str[64];
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic char vendor_enc_str[256];
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic char model_enc_str[256];
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic char revision_str[16];
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic char type_str[16];
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poetteringstatic void log_fn(struct udev *udev, int priority,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering const char *file, int line, const char *fn,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering const char *format, va_list args)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering{
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering vsyslog(priority, format, args);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic void set_type(const char *from, char *to, size_t len)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering{
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int type_num;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering char *eptr;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering const char *type = "generic";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering type_num = strtoul(from, &eptr, 0);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (eptr != from) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering switch (type_num) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 0:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering type = "disk";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 1:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering type = "tape";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 4:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering type = "optical";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 5:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering type = "cd";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 7:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering type = "optical";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 0xe:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering type = "disk";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 0xf:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering type = "optical";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering default:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering util_strscpy(to, len, type);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering/*
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * get_value:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering *
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * buf points to an '=' followed by a quoted string ("foo") or a string ending
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * with a space or ','.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering *
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Return a pointer to the NUL terminated string, returns NULL if no
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering * matches.
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering */
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poetteringstatic char *get_value(char **buffer)
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering{
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering static const char *quote_string = "\"\n";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering static const char *comma_string = ",\n";
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering char *val;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering const char *end;
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (**buffer == '"') {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /*
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * skip leading quote, terminate when quote seen
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering (*buffer)++;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering end = quote_string;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering end = comma_string;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering val = strsep(buffer, end);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (val && end == quote_string)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /*
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * skip trailing quote
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering (*buffer)++;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering while (isspace(**buffer))
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering (*buffer)++;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering return val;
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering}
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poetteringstatic int argc_count(char *opts)
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering{
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering int i = 0;
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering while (*opts != '\0')
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering if (*opts++ == ' ')
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering i++;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return i;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering/*
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * get_file_options:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering *
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 *
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * vendor and model can end in '\n'.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering */
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int get_file_options(struct udev *udev,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering const char *vendor, const char *model,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int *argc, char ***newargv)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering{
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering char *buffer;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering FILE *fd;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering char *buf;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering char *str1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering char *vendor_in, *model_in, *options_in; /* read in from file */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int lineno;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int c;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int retval = 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "vendor='%s'; model='%s'\n", vendor, model);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering fd = fopen(config_file, "r");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (fd == NULL) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "can't open %s\n", config_file);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (errno == ENOENT) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return 1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering err(udev, "can't open %s: %s\n", config_file, strerror(errno));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return -1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /*
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 */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering buffer = malloc(MAX_BUFFER_LEN);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (!buffer) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering fclose(fd);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering err(udev, "can't allocate memory\n");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return -1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering *newargv = NULL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering lineno = 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering while (1) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering vendor_in = model_in = options_in = NULL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering buf = fgets(buffer, MAX_BUFFER_LEN, fd);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (buf == NULL)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering lineno++;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (buf[strlen(buffer) - 1] != '\n') {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering err(udev, "Config file line %d too long\n", lineno);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering while (isspace(*buf))
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering buf++;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* blank or all whitespace line */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (*buf == '\0')
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering continue;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* comment line */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (*buf == '#')
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering continue;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "lineno %d: '%s'\n", lineno, buf);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering str1 = strsep(&buf, "=");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (str1 && strcasecmp(str1, "VENDOR") == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering str1 = get_value(&buf);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (!str1) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering retval = -1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering vendor_in = str1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering str1 = strsep(&buf, "=");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (str1 && strcasecmp(str1, "MODEL") == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering str1 = get_value(&buf);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (!str1) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering retval = -1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering model_in = str1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering str1 = strsep(&buf, "=");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering str1 = get_value(&buf);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (!str1) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering retval = -1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering options_in = str1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "config file line %d:\n"
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering " vendor '%s'; model '%s'; options '%s'\n",
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering lineno, vendor_in, model_in, options_in);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /*
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Only allow: [vendor=foo[,model=bar]]options=stuff
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering */
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 retval = -1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (vendor == NULL) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (vendor_in == NULL) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "matched global option\n");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else if ((vendor_in && strncmp(vendor, vendor_in,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering strlen(vendor_in)) == 0) &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering (!model_in || (strncmp(model, model_in,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering strlen(model_in)) == 0))) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /*
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Matched vendor and optionally model.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering *
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 */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "matched vendor/model\n");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "no match\n");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (retval == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (vendor_in != NULL || model_in != NULL ||
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering options_in != NULL) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /*
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * Something matched. Allocate newargv, and store
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * values found in options_in.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering strcpy(buffer, options_in);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering c = argc_count(buffer) + 2;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering *newargv = calloc(c, sizeof(**newargv));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (!*newargv) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering err(udev, "can't allocate memory\n");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering retval = -1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering *argc = c;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering c = 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /*
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 */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering (*newargv)[c] = buffer;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering for (c = 1; c < *argc; c++)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering (*newargv)[c] = strsep(&buffer, " \t");
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* No matches */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering retval = 1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (retval != 0)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering free(buffer);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering fclose(fd);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return retval;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int set_options(struct udev *udev,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int argc, char **argv, const char *short_opts,
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering char *maj_min_dev)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering{
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int option;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /*
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 */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering optind = 1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering while (1) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering option = getopt_long(argc, argv, short_opts, options, NULL);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (option == -1)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (optarg)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "option '%c' arg '%s'\n", option, optarg);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering else
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dbg(udev, "option '%c'\n", option);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering switch (option) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 'b':
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering all_good = 0;
5034c7bcdfc4493ed3c30722e9d897c8da78fedeLennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 'd':
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dev_specified = 1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 'e':
5034c7bcdfc4493ed3c30722e9d897c8da78fedeLennart Poettering use_stderr = 1;
5034c7bcdfc4493ed3c30722e9d897c8da78fedeLennart Poettering break;
5034c7bcdfc4493ed3c30722e9d897c8da78fedeLennart Poettering
5034c7bcdfc4493ed3c30722e9d897c8da78fedeLennart Poettering case 'f':
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_strscpy(config_file, MAX_PATH_LEN, optarg);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 'g':
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering all_good = 1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 'h':
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");
5034c7bcdfc4493ed3c30722e9d897c8da78fedeLennart Poettering exit(0);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 'p':
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (strcmp(optarg, "0x80") == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering default_page_code = PAGE_80;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else if (strcmp(optarg, "0x83") == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering default_page_code = PAGE_83;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else if (strcmp(optarg, "pre-spc3-83") == 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering default_page_code = PAGE_83_PRE_SPC3;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering } else {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering err(udev, "Unknown page code '%s'\n", optarg);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return -1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 's':
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering sg_version = atoi(optarg);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (sg_version < 3 || sg_version > 4) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering err(udev, "Unknown SG version '%s'\n", optarg);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return -1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 'u':
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering reformat_serial = 1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 'x':
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering export = 1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 'v':
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering debug++;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case 'V':
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering printf("%s\n", VERSION);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering exit(0);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering default:
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering exit(1);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (optind < argc && !dev_specified) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dev_specified = 1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
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{
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int retval;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int newargc;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering char **newargv = NULL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int option;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering *good_bad = all_good;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering *page_code = default_page_code;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
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);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (option == -1)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering break;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering if (optarg)
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering dbg(udev, "option '%c' arg '%s'\n", option, optarg);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering else
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering dbg(udev, "option '%c'\n", option);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering switch (option) {
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering case 'b':
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering *good_bad = 0;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering break;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering case 'g':
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering *good_bad = 1;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering break;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering case 'p':
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering if (strcmp(optarg, "0x80") == 0) {
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering *page_code = PAGE_80;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering } else if (strcmp(optarg, "0x83") == 0) {
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering *page_code = PAGE_83;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering } else if (strcmp(optarg, "pre-spc3-83") == 0) {
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering *page_code = PAGE_83_PRE_SPC3;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering } else {
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering err(udev, "Unknown page code '%s'\n", optarg);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering retval = -1;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering }
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering break;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering default:
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering err(udev, "Unknown or bad option '%c' (0x%x)\n", option, option);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering retval = -1;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering break;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering }
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering }
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering if (newargv) {
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering free(newargv[0]);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering free(newargv);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering }
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering return retval;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering}
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poetteringstatic int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering{
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering int retval;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dev_scsi->use_sg = sg_version;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering retval = scsi_std_inquiry(udev, dev_scsi, path);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (retval)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return retval;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
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
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_replace_chars(vendor_str, NULL);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering util_replace_chars(model_str, NULL);
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 util_replace_chars(revision_str, NULL);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering}
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering/*
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 Poettering */
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int scsi_id(struct udev *udev, char *maj_min_dev)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering{
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering struct scsi_id_device dev_scsi;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int good_dev;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int page_code;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering int retval = 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering retval = 1;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering goto out;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering }
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
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);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (!good_dev) {
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering retval = 1;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering goto out;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering }
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering
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);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering
if (export) {
char serial_str[MAX_SERIAL_LEN];
printf("ID_SCSI=1\n");
printf("ID_VENDOR=%s\n", vendor_str);
printf("ID_VENDOR_ENC=%s\n", vendor_enc_str);
printf("ID_MODEL=%s\n", model_str);
printf("ID_MODEL_ENC=%s\n", model_enc_str);
printf("ID_REVISION=%s\n", revision_str);
printf("ID_TYPE=%s\n", type_str);
if (dev_scsi.serial[0] != '\0') {
util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
util_replace_chars(serial_str, NULL);
printf("ID_SERIAL=%s\n", serial_str);
util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
util_replace_chars(serial_str, NULL);
printf("ID_SERIAL_SHORT=%s\n", serial_str);
}
if (dev_scsi.wwn[0] != '\0') {
printf("ID_WWN=0x%s\n", dev_scsi.wwn);
if (dev_scsi.wwn_vendor_extension[0] != '\0') {
printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi.wwn_vendor_extension);
printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi.wwn, dev_scsi.wwn_vendor_extension);
} else {
printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi.wwn);
}
}
if (dev_scsi.tgpt_group[0] != '\0') {
printf("ID_TARGET_PORT=%s\n", dev_scsi.tgpt_group);
}
if (dev_scsi.unit_serial_number[0] != '\0') {
printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
}
goto out;
}
if (dev_scsi.serial[0] == '\0') {
retval = 1;
goto out;
}
if (reformat_serial) {
char serial_str[MAX_SERIAL_LEN];
util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
util_replace_chars(serial_str, NULL);
printf("%s\n", serial_str);
goto out;
}
printf("%s\n", dev_scsi.serial);
out:
return retval;
}
int main(int argc, char **argv)
{
struct udev *udev;
int retval = 0;
char maj_min_dev[MAX_PATH_LEN];
int newargc;
char **newargv;
udev = udev_new();
if (udev == NULL)
goto exit;
udev_log_init("scsi_id");
udev_set_log_fn(udev, log_fn);
/*
* Get config file options.
*/
newargv = NULL;
retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
if (retval < 0) {
retval = 1;
goto exit;
}
if (newargv && (retval == 0)) {
if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
retval = 2;
goto exit;
}
free(newargv);
}
/*
* Get command line options (overriding any config file settings).
*/
if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
exit(1);
if (!dev_specified) {
err(udev, "no device specified\n");
retval = 1;
goto exit;
}
retval = scsi_id(udev, maj_min_dev);
exit:
udev_unref(udev);
udev_log_close();
return retval;
}