scsi_id.c revision 1aa1e24848903d11780db1ade355be73ad61a937
/*
*
* Main section of the scsi_id program
*
* Copyright (C) IBM Corp. 2003
*
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <syslog.h>
#include <stdarg.h>
#include <ctype.h>
#include "../../udev.h"
#include "scsi_id.h"
#include "scsi_id_version.h"
/* temporary names for mknod */
#define TMP_DIR "/dev"
#define TMP_PREFIX "tmp-scsi"
static const char short_options[] = "abd:f:gip:s:uvVx";
static const char dev_short_options[] = "bgp:";
static int all_good;
static int always_info;
static int dev_specified;
static int sys_specified;
static int display_bus_id;
static enum page_code default_page_code;
static int use_stderr;
static int debug;
static int hotplug_mode;
static int reformat_serial;
static int export;
static char vendor_str[64];
static char model_str[64];
static char revision_str[16];
static char type_str[16];
#ifdef USE_LOG
{
static int udev_log = -1;
if (udev_log == -1) {
const char *value;
if (value)
else
}
return;
}
#endif
{
/* strip trailing whitespace */
len--;
/* strip leading whitespace */
i = 0;
i++;
j = 0;
while (i < len) {
/* substitute multiple whitespace */
i++;
to[j++] = '_';
}
/* skip chars */
if (from[i] == '/') {
i++;
continue;
}
}
to[j] = '\0';
}
{
int type_num;
char *eptr;
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;
}
}
}
{
const char *attr;
return -1;
}
return -1;
}
return -1;
}
return 0;
}
/*
* 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 char *quote_string = "\"\n";
static char *comma_string = ",\n";
char *val;
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;
char *buf;
char *str1;
int lineno;
int c;
int retval = 0;
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) {
err("Can't allocate memory.");
return -1;
}
lineno = 0;
while (1) {
break;
lineno++;
break;
}
buf++;
/* blank or all whitespace line */
if (*buf == '\0')
continue;
/* comment line */
if (*buf == '#')
continue;
if (!str1) {
retval = -1;
break;
}
if (!str1) {
retval = -1;
break;
}
}
}
if (!str1) {
retval = -1;
break;
}
options_in = str1;
}
dbg("config file line %d:"
" vendor '%s'; model '%s'; options '%s'\n",
/*
* Only allow: [vendor=foo[,model=bar]]options=stuff
*/
retval = -1;
break;
}
dbg("matched global option\n");
break;
}
/*
* Matched vendor and optionally model.
*
* Note: a short vendor_in or model_in can
* give a partial match (that is FOO
* matches FOOBAR).
*/
break;
} else {
dbg("no match\n");
}
}
if (retval == 0) {
options_in != NULL) {
/*
* Something matched. Allocate newargv, and store
* values found in options_in.
*/
if (!*newargv) {
err("Can't allocate memory.");
retval = -1;
} else {
*argc = c;
c = 0;
/*
* argv[0] at 0 is skipped by getopt, but
* store the buffer address there for
* alter freeing.
*/
for (c = 1; c < *argc; c++)
}
} else {
/* No matches */
retval = 1;
}
}
if (retval != 0)
return retval;
}
char *target, 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. [Note glibc handles
* setting this to 0, but klibc does not.]
*/
optind = 1;
while (1) {
if (option == -1)
break;
if (optarg)
else
switch (option) {
case 'a':
always_info = 1;
break;
case 'b':
all_good = 0;
break;
case 'd':
dev_specified = 1;
break;
case 'e':
use_stderr = 1;
break;
case 'f':
break;
case 'g':
all_good = 1;
break;
case 'i':
display_bus_id = 1;
break;
case 'p':
} else {
return -1;
}
break;
case 's':
sys_specified = 1;
break;
case 'u':
reformat_serial = 1;
break;
case 'x':
export = 1;
break;
case 'v':
debug++;
break;
case 'V':
exit(0);
break;
default:
return -1;
}
}
return 0;
}
{
int retval;
int newargc;
int option;
if (!vendor) {
return -1;
}
if (!model) {
return -1;
}
if (!type) {
return -1;
}
if (!type) {
return -1;
}
while (retval == 0) {
if (option == -1)
break;
if (optarg)
else
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;
}
/*
* format_serial: replace to whitespaces by underscores for calling
* programs that use the serial for device naming (multipath, Suse
* naming, etc...)
*/
static void format_serial(char *serial)
{
char *p = serial, *q;
q = p;
while (*p != '\0') {
if (isspace(*p)) {
*q = '_';
q++;
}
} else {
*q = *p;
q++;
}
p++;
}
*q = '\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. This
* could be expanded, for example, if we want to report a failure like no
* memory etc. return 2, and return 1 for expected cases (like broken
* device found) that do not print an id.
*/
{
int retval;
int dev_type = 0;
char *serial, *unaligned_buf;
struct sysfs_device *dev;
struct sysfs_device *dev_scsi;
int good_dev;
int page_code;
return 1;
}
else
/* get scsi parent device */
return 1;
}
/* allow only scsi devices */
return 1;
}
/* mknod a temp dev to communicate with the device */
dbg("create_tmp_dev failed\n");
return 1;
}
/* get per device (vendor + model) options from the config file */
#define ALIGN 512
& ~(ALIGN - 1));
if (!good_dev) {
retval = 1;
serial, MAX_SERIAL_LEN)) {
} else {
retval = 0;
}
if (!retval) {
if (export) {
static char serial_str[64];
printf("ID_BUS=scsi\n");
} else {
if (reformat_serial)
if (display_bus_id)
}
retval = 0;
}
if (!dev_specified)
return retval;
}
{
int retval = 0;
char devpath[MAX_NAME_LEN];
char maj_min_dev[MAX_NAME_LEN];
int newargc;
const char *env;
char **newargv;
logging_init("scsi_id");
sysfs_init();
/* sysfs path can be overridden for testing */
if (env) {
} else
if (env) {
hotplug_mode = 1;
sys_specified = 1;
}
/*
* Get config file options.
*/
if (retval < 0) {
retval = 1;
goto exit;
}
maj_min_dev) < 0) {
retval = 2;
goto exit;
}
}
/*
* Get command line options (overriding any config file or DEVPATH
* settings).
*/
exit(1);
if (!sys_specified) {
info("-s must be specified\n");
retval = 1;
goto exit;
}
exit:
return retval;
}