4 * Main section of the scsi_id program
6 * Copyright (C) IBM Corp. 2003
7 * Copyright (C) SUSE Linux Products GmbH, 2006
10 * Patrick Mansfield<patmans@us.ibm.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation version 2 of the License.
29 #include "../../udev.h"
31 #include "scsi_id_version.h"
33 /* temporary names for mknod */
34 #define TMP_DIR "/dev"
35 #define TMP_PREFIX "tmp-scsi"
37 static const char short_options
[] = "abd:f:gip:s:uvVx";
38 static const char dev_short_options
[] = "bgp:";
41 static int always_info
;
42 static int dev_specified
;
43 static int sys_specified
;
44 static char config_file
[MAX_PATH_LEN
] = SCSI_ID_CONFIG_FILE
;
45 static int display_bus_id
;
46 static enum page_code default_page_code
;
47 static int use_stderr
;
49 static int hotplug_mode
;
50 static int reformat_serial
;
52 static char vendor_str
[64];
53 static char model_str
[64];
54 static char revision_str
[16];
55 static char type_str
[16];
58 void log_message(int priority
, const char *format
, ...)
61 static int udev_log
= -1;
66 value
= getenv("UDEV_LOG");
68 udev_log
= log_priority(value
);
73 if (priority
> udev_log
)
76 va_start(args
, format
);
77 vsyslog(priority
, format
, args
);
82 static void set_str(char *to
, const char *from
, size_t count
)
86 /* strip trailing whitespace */
87 len
= strnlen(from
, count
);
88 while (len
&& isspace(from
[len
-1]))
91 /* strip leading whitespace */
93 while (isspace(from
[i
]) && (i
< len
))
98 /* substitute multiple whitespace */
99 if (isspace(from
[i
])) {
100 while (isspace(from
[i
]))
105 if (from
[i
] == '/') {
114 static void set_type(char *to
, const char *from
, size_t len
)
118 char *type
= "generic";
120 type_num
= strtoul(from
, &eptr
, 0);
148 strncpy(to
, type
, len
);
152 static int create_tmp_dev(const char *devpath
, char *tmpdev
, int dev_type
)
154 unsigned int maj
, min
;
158 attr
= sysfs_attr_get_value(devpath
, "dev");
160 dbg("%s: could not get dev attribute: %s", devpath
, strerror(errno
));
164 dbg("dev value %s", attr
);
165 if (sscanf(attr
, "%u:%u", &maj
, &min
) != 2) {
166 err("%s: invalid dev major/minor", devpath
);
170 snprintf(tmpdev
, MAX_PATH_LEN
, "%s/%s-maj%d-min%d-%u",
171 TMP_DIR
, TMP_PREFIX
, maj
, min
, getpid());
173 dbg("tmpdev '%s'", tmpdev
);
174 if (mknod(tmpdev
, 0600 | dev_type
, makedev(maj
, min
))) {
175 err("mknod failed: %s", strerror(errno
));
184 * buf points to an '=' followed by a quoted string ("foo") or a string ending
185 * with a space or ','.
187 * Return a pointer to the NUL terminated string, returns NULL if no
190 static char *get_value(char **buffer
)
192 static char *quote_string
= "\"\n";
193 static char *comma_string
= ",\n";
197 if (**buffer
== '"') {
199 * skip leading quote, terminate when quote seen
206 val
= strsep(buffer
, end
);
207 if (val
&& end
== quote_string
)
209 * skip trailing quote
213 while (isspace(**buffer
))
219 static int argc_count(char *opts
)
222 while (*opts
!= '\0')
231 * If vendor == NULL, find a line in the config file with only "OPTIONS=";
232 * if vendor and model are set find the first OPTIONS line in the config
233 * file that matches. Set argc and argv to match the OPTIONS string.
235 * vendor and model can end in '\n'.
237 static int get_file_options(const char *vendor
, const char *model
,
238 int *argc
, char ***newargv
)
244 char *vendor_in
, *model_in
, *options_in
; /* read in from file */
249 dbg("vendor='%s'; model='%s'\n", vendor
, model
);
250 fd
= fopen(config_file
, "r");
252 dbg("can't open %s\n", config_file
);
253 if (errno
== ENOENT
) {
256 err("can't open %s: %s", config_file
, strerror(errno
));
262 * Allocate a buffer rather than put it on the stack so we can
263 * keep it around to parse any options (any allocated newargv
264 * points into this buffer for its strings).
266 buffer
= malloc(MAX_BUFFER_LEN
);
268 err("Can't allocate memory.");
275 vendor_in
= model_in
= options_in
= NULL
;
277 buf
= fgets(buffer
, MAX_BUFFER_LEN
, fd
);
281 if (buf
[strlen(buffer
) - 1] != '\n') {
282 info("Config file line %d too long.\n", lineno
);
286 while (isspace(*buf
))
289 /* blank or all whitespace line */
297 dbg("lineno %d: '%s'\n", lineno
, buf
);
298 str1
= strsep(&buf
, "=");
299 if (str1
&& strcasecmp(str1
, "VENDOR") == 0) {
300 str1
= get_value(&buf
);
307 str1
= strsep(&buf
, "=");
308 if (str1
&& strcasecmp(str1
, "MODEL") == 0) {
309 str1
= get_value(&buf
);
315 str1
= strsep(&buf
, "=");
319 if (str1
&& strcasecmp(str1
, "OPTIONS") == 0) {
320 str1
= get_value(&buf
);
327 dbg("config file line %d:"
328 " vendor '%s'; model '%s'; options '%s'\n",
329 lineno
, vendor_in
, model_in
, options_in
);
331 * Only allow: [vendor=foo[,model=bar]]options=stuff
333 if (!options_in
|| (!vendor_in
&& model_in
)) {
334 info("Error parsing config file line %d '%s'", lineno
, buffer
);
338 if (vendor
== NULL
) {
339 if (vendor_in
== NULL
) {
340 dbg("matched global option\n");
343 } else if ((vendor_in
&& strncmp(vendor
, vendor_in
,
344 strlen(vendor_in
)) == 0) &&
345 (!model_in
|| (strncmp(model
, model_in
,
346 strlen(model_in
)) == 0))) {
348 * Matched vendor and optionally model.
350 * Note: a short vendor_in or model_in can
351 * give a partial match (that is FOO
354 dbg("matched vendor/model\n");
362 if (vendor_in
!= NULL
|| model_in
!= NULL
||
363 options_in
!= NULL
) {
365 * Something matched. Allocate newargv, and store
366 * values found in options_in.
368 strcpy(buffer
, options_in
);
369 c
= argc_count(buffer
) + 2;
370 *newargv
= calloc(c
, sizeof(**newargv
));
372 err("Can't allocate memory.");
378 * argv[0] at 0 is skipped by getopt, but
379 * store the buffer address there for
382 (*newargv
)[c
] = buffer
;
383 for (c
= 1; c
< *argc
; c
++)
384 (*newargv
)[c
] = strsep(&buffer
, " \t");
397 static int set_options(int argc
, char **argv
, const char *short_opts
,
398 char *target
, char *maj_min_dev
)
403 * optind is a global extern used by getopt. Since we can call
404 * set_options twice (once for command line, and once for config
405 * file) we have to reset this back to 1.
409 option
= getopt(argc
, argv
, short_opts
);
414 dbg("option '%c' arg '%s'\n", option
, optarg
);
416 dbg("option '%c'\n", option
);
428 strncpy(maj_min_dev
, optarg
, MAX_PATH_LEN
);
429 maj_min_dev
[MAX_PATH_LEN
-1] = '\0';
437 strncpy(config_file
, optarg
, MAX_PATH_LEN
);
438 config_file
[MAX_PATH_LEN
-1] = '\0';
450 if (strcmp(optarg
, "0x80") == 0) {
451 default_page_code
= PAGE_80
;
452 } else if (strcmp(optarg
, "0x83") == 0) {
453 default_page_code
= PAGE_83
;
454 } else if (strcmp(optarg
, "pre-spc3-83") == 0) {
455 default_page_code
= PAGE_83_PRE_SPC3
;
457 info("Unknown page code '%s'", optarg
);
464 strncpy(target
, optarg
, MAX_PATH_LEN
);
465 target
[MAX_PATH_LEN
-1] = '\0';
481 info("scsi_id version: %s\n", SCSI_ID_VERSION
);
486 info("Unknown or bad option '%c' (0x%x)", option
, option
);
493 static int per_dev_options(struct sysfs_device
*dev_scsi
, int *good_bad
, int *page_code
)
497 char **newargv
= NULL
;
498 const char *vendor
, *model
, *type
;
501 *good_bad
= all_good
;
502 *page_code
= default_page_code
;
504 vendor
= sysfs_attr_get_value(dev_scsi
->devpath
, "vendor");
506 info("%s: cannot get vendor attribute", dev_scsi
->devpath
);
509 set_str(vendor_str
, vendor
, sizeof(vendor_str
)-1);
511 model
= sysfs_attr_get_value(dev_scsi
->devpath
, "model");
513 info("%s: cannot get model attribute\n", dev_scsi
->devpath
);
516 set_str(model_str
, model
, sizeof(model_str
)-1);
518 type
= sysfs_attr_get_value(dev_scsi
->devpath
, "type");
520 info("%s: cannot get type attribute", dev_scsi
->devpath
);
523 set_type(type_str
, type
, sizeof(type_str
));
525 type
= sysfs_attr_get_value(dev_scsi
->devpath
, "rev");
527 info("%s: cannot get type attribute\n", dev_scsi
->devpath
);
530 set_str(revision_str
, type
, sizeof(revision_str
)-1);
532 retval
= get_file_options(vendor
, model
, &newargc
, &newargv
);
534 optind
= 1; /* reset this global extern */
535 while (retval
== 0) {
536 option
= getopt(newargc
, newargv
, dev_short_options
);
541 dbg("option '%c' arg '%s'\n", option
, optarg
);
543 dbg("option '%c'\n", option
);
555 if (strcmp(optarg
, "0x80") == 0) {
556 *page_code
= PAGE_80
;
557 } else if (strcmp(optarg
, "0x83") == 0) {
558 *page_code
= PAGE_83
;
559 } else if (strcmp(optarg
, "pre-spc3-83") == 0) {
560 *page_code
= PAGE_83_PRE_SPC3
;
562 info("Unknown page code '%s'", optarg
);
568 info("Unknown or bad option '%c' (0x%x)", option
, option
);
582 * format_serial: replace to whitespaces by underscores for calling
583 * programs that use the serial for device naming (multipath, Suse
586 static void format_serial(char *serial
)
588 char *p
= serial
, *q
;
593 if (q
> serial
&& q
[-1] != '_') {
607 * scsi_id: try to get an id, if one is found, printf it to stdout.
608 * returns a value passed to exit() - 0 if printed an id, else 1. This
609 * could be expanded, for example, if we want to report a failure like no
610 * memory etc. return 2, and return 1 for expected cases (like broken
611 * device found) that do not print an id.
613 static int scsi_id(const char *devpath
, char *maj_min_dev
)
617 struct sysfs_device
*dev
;
618 struct sysfs_device
*dev_scsi
;
621 char serial
[MAX_SERIAL_LEN
];
622 char serial_short
[MAX_SERIAL_LEN
];
624 dbg("devpath %s\n", devpath
);
626 dev
= sysfs_device_get(devpath
);
628 err("unable to access '%s'", devpath
);
632 if (strcmp(dev
->subsystem
, "block") == 0)
637 /* get scsi parent device */
638 dev_scsi
= sysfs_device_get_parent_with_subsystem(dev
, "scsi");
639 if (dev_scsi
== NULL
) {
640 err("unable to access parent device of '%s'", devpath
);
644 /* mknod a temp dev to communicate with the device */
645 if (!dev_specified
&& create_tmp_dev(dev
->devpath
, maj_min_dev
, dev_type
)) {
646 dbg("create_tmp_dev failed\n");
650 /* get per device (vendor + model) options from the config file */
651 retval
= per_dev_options(dev_scsi
, &good_dev
, &page_code
);
652 dbg("per dev options: good %d; page code 0x%x", good_dev
, page_code
);
656 } else if (scsi_get_serial(dev_scsi
, maj_min_dev
, page_code
,
657 serial
, serial_short
, MAX_SERIAL_LEN
)) {
658 retval
= always_info
?0:1;
664 char serial_str
[MAX_SERIAL_LEN
];
666 printf("ID_VENDOR=%s\n", vendor_str
);
667 printf("ID_MODEL=%s\n", model_str
);
668 printf("ID_REVISION=%s\n", revision_str
);
669 set_str(serial_str
, serial
, sizeof(serial_str
));
670 printf("ID_SERIAL=%s\n", serial_str
);
671 set_str(serial_str
, serial_short
, sizeof(serial_str
));
672 printf("ID_SERIAL_SHORT=%s\n", serial_str
);
673 printf("ID_TYPE=%s\n", type_str
);
674 printf("ID_BUS=scsi\n");
677 format_serial(serial
);
679 printf("%s: ", dev_scsi
->kernel
);
680 printf("%s\n", serial
);
692 int main(int argc
, char **argv
)
695 char devpath
[MAX_PATH_LEN
];
696 char maj_min_dev
[MAX_PATH_LEN
];
701 logging_init("scsi_id");
703 dbg("argc is %d\n", argc
);
705 /* sysfs path can be overridden for testing */
706 env
= getenv("SYSFS_PATH");
708 strncpy(sysfs_path
, env
, sizeof(sysfs_path
));
709 sysfs_path
[sizeof(sysfs_path
)-1] = '\0';
711 strcpy(sysfs_path
, "/sys");
713 env
= getenv("DEVPATH");
717 strncpy(devpath
, env
, MAX_PATH_LEN
);
718 devpath
[sizeof(devpath
)-1] = '\0';
722 * Get config file options.
725 retval
= get_file_options(NULL
, NULL
, &newargc
, &newargv
);
730 if (newargv
&& (retval
== 0)) {
731 if (set_options(newargc
, newargv
, short_options
, devpath
,
740 * Get command line options (overriding any config file or DEVPATH
743 if (set_options(argc
, argv
, short_options
, devpath
, maj_min_dev
) < 0)
746 if (!sys_specified
) {
747 info("-s must be specified\n");
752 retval
= scsi_id(devpath
, maj_min_dev
);