scsi_id.c revision 57255510c95ca3369d241502f26d7bbd8fac30a6
/*
* Copyright (C) IBM Corp. 2003
* Copyright (C) SUSE Linux Products GmbH, 2006
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include "libudev.h"
#include "libudev-private.h"
#include "scsi_id.h"
#include "udev-util.h"
{}
};
static bool all_good = false;
static bool dev_specified = false;
static int sg_version = 4;
static bool reformat_serial = false;
static bool export = false;
static char vendor_str[64];
static char model_str[64];
static char vendor_enc_str[256];
static char model_enc_str[256];
static char revision_str[16];
static char type_str[16];
{
int type_num;
char *eptr;
const char *type = "generic";
switch (type_num) {
case 0:
type = "disk";
break;
case 1:
type = "tape";
break;
case 4:
type = "optical";
break;
case 5:
type = "cd";
break;
case 7:
type = "optical";
break;
case 0xe:
type = "disk";
break;
case 0xf:
type = "optical";
break;
default:
break;
}
}
}
/*
* get_value:
*
* buf points to an '=' followed by a quoted string ("foo") or a string ending
* with a space or ','.
*
* Return a pointer to the NUL terminated string, returns NULL if no
* matches.
*/
{
static const char *quote_string = "\"\n";
static const char *comma_string = ",\n";
char *val;
const char *end;
if (**buffer == '"') {
/*
* skip leading quote, terminate when quote seen
*/
(*buffer)++;
end = quote_string;
} else {
end = comma_string;
}
/*
* skip trailing quote
*/
(*buffer)++;
(*buffer)++;
return val;
}
static int argc_count(char *opts)
{
int i = 0;
while (*opts != '\0')
if (*opts++ == ' ')
i++;
return i;
}
/*
* get_file_options:
*
* If vendor == NULL, find a line in the config file with only "OPTIONS=";
* if vendor and model are set find the first OPTIONS line in the config
* file that matches. Set argc and argv to match the OPTIONS string.
*
* vendor and model can end in '\n'.
*/
{
char *buffer;
_cleanup_fclose_ FILE *f;
char *buf;
char *str1;
int lineno;
int c;
int retval = 0;
if (f == NULL) {
return 1;
else {
return -1;
}
}
/*
* Allocate a buffer rather than put it on the stack so we can
* keep it around to parse any options (any allocated newargv
* points into this buffer for its strings).
*/
if (!buffer)
return log_oom();
lineno = 0;
for (;;) {
break;
lineno++;
break;
}
buf++;
/* blank or all whitespace line */
if (*buf == '\0')
continue;
/* comment line */
if (*buf == '#')
continue;
if (!str1) {
break;
}
if (!str1) {
break;
}
}
}
if (!str1) {
break;
}
options_in = str1;
}
/*
* Only allow: [vendor=foo[,model=bar]]options=stuff
*/
retval = -1;
break;
}
break;
} else if (vendor_in &&
(!model_in ||
/*
* Matched vendor and optionally model.
*
* Note: a short vendor_in or model_in can
* give a partial match (that is FOO
* matches FOOBAR).
*/
break;
}
}
if (retval == 0) {
options_in != NULL) {
/*
* Something matched. Allocate newargv, and store
* values found in options_in.
*/
if (!*newargv)
else {
*argc = c;
c = 0;
/*
* argv[0] at 0 is skipped by getopt, but
* store the buffer address there for
* later freeing
*/
for (c = 1; c < *argc; c++)
}
} else {
/* No matches */
retval = 1;
}
}
if (retval != 0)
return retval;
}
static void help(void) {
printf("Usage: %s [OPTION...] DEVICE\n\n"
"SCSI device identification.\n\n"
" -h --help Print this message\n"
" --version Print version of the program\n\n"
" -d --device= Device node for SG_IO commands\n"
" -f --config= Location of config file\n"
" -p --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
" -s --sg-version=3|4 Use SGv3 or SGv4\n"
" -b --blacklisted Treat device as blacklisted\n"
" -g --whitelisted Treat device as whitelisted\n"
" -u --replace-whitespace Replace all whitespace by underscores\n"
" -v --verbose Verbose logging\n"
" -x --export Print values as environment keys\n"
}
char *maj_min_dev)
{
int option;
/*
* optind is a global extern used by getopt. Since we can call
* set_options twice (once for command line, and once for config
* file) we have to reset this back to 1.
*/
optind = 1;
switch (option) {
case 'b':
all_good = false;
break;
case 'd':
dev_specified = true;
break;
case 'f':
break;
case 'g':
all_good = true;
break;
case 'h':
help();
exit(0);
case 'p':
else {
return -1;
}
break;
case 's':
return -1;
}
break;
case 'u':
reformat_serial = true;
break;
case 'v':
log_open();
break;
case 'V':
exit(0);
case 'x':
export = true;
break;
case '?':
return -1;
default:
assert_not_reached("Unknown option");
}
dev_specified = true;
}
return 0;
}
{
int retval;
int newargc;
int option;
while (retval == 0) {
if (option == -1)
break;
switch (option) {
case 'b':
*good_bad = 0;
break;
case 'g':
*good_bad = 1;
break;
case 'p':
} else {
retval = -1;
}
break;
default:
retval = -1;
break;
}
}
if (newargv) {
}
return retval;
}
{
int retval;
if (retval)
return retval;
return 0;
}
/*
* scsi_id: try to get an id, if one is found, printf it to stdout.
* returns a value passed to exit() - 0 if printed an id, else 1.
*/
{
struct scsi_id_device dev_scsi = {};
int good_dev;
int page_code;
int retval = 0;
retval = 1;
goto out;
}
/* get per device (vendor + model) options from the config file */
if (!good_dev) {
retval = 1;
goto out;
}
/* read serial number from mode pages (no values for optical drives) */
if (export) {
char serial_str[MAX_SERIAL_LEN];
printf("ID_SCSI=1\n");
}
} else
}
goto out;
}
retval = 1;
goto out;
}
if (reformat_serial) {
char serial_str[MAX_SERIAL_LEN];
goto out;
}
out:
return retval;
}
{
int retval = 0;
char maj_min_dev[MAX_PATH_LEN];
int newargc;
log_open();
goto exit;
/*
* Get config file options.
*/
if (retval < 0) {
retval = 1;
goto exit;
}
if (retval == 0) {
retval = 2;
goto exit;
}
}
/*
* Get command line options (overriding any config file settings).
*/
exit(1);
if (!dev_specified) {
log_error("No device specified.");
retval = 1;
goto exit;
}
exit:
if (newargv) {
}
log_close();
return retval;
}