#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 <syslog.h>
-#include <stdarg.h>
#include <ctype.h>
#include <getopt.h>
#include <sys/stat.h>
#include "libudev.h"
#include "libudev-private.h"
#include "scsi_id.h"
+#include "udev-util.h"
static const struct option options[] = {
- { "device", required_argument, NULL, 'd' },
- { "config", required_argument, NULL, 'f' },
- { "page", required_argument, NULL, 'p' },
- { "blacklisted", no_argument, NULL, 'b' },
- { "whitelisted", no_argument, NULL, 'g' },
- { "replace-whitespace", no_argument, NULL, 'u' },
- { "sg-version", required_argument, NULL, 's' },
- { "verbose", no_argument, NULL, 'v' },
- { "version", no_argument, NULL, 'V' },
- { "export", no_argument, NULL, 'x' },
- { "help", no_argument, NULL, 'h' },
+ { "device", required_argument, NULL, 'd' },
+ { "config", required_argument, NULL, 'f' },
+ { "page", required_argument, NULL, 'p' },
+ { "blacklisted", no_argument, NULL, 'b' },
+ { "whitelisted", no_argument, NULL, 'g' },
+ { "replace-whitespace", no_argument, NULL, 'u' },
+ { "sg-version", required_argument, NULL, 's' },
+ { "verbose", no_argument, NULL, 'v' },
+ { "version", no_argument, NULL, 'V' }, /* don't advertise -V */
+ { "export", no_argument, NULL, 'x' },
+ { "help", no_argument, NULL, 'h' },
{}
};
-static const char short_options[] = "d:f:ghip:uvVx";
-static const char dev_short_options[] = "bgp:";
-
-static int all_good;
-static int dev_specified;
-static char config_file[MAX_PATH_LEN] = SYSCONFDIR "/scsi_id.config";
-static enum page_code default_page_code;
+static bool all_good = false;
+static bool dev_specified = false;
+static char config_file[MAX_PATH_LEN] = "/etc/scsi_id.config";
+static enum page_code default_page_code = PAGE_UNSPECIFIED;
static int sg_version = 4;
-static int use_stderr;
-static int debug;
-static int reformat_serial;
-static int export;
+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 revision_str[16];
static char type_str[16];
+_printf_(6,0)
static void log_fn(struct udev *udev, int priority,
const char *file, int line, const char *fn,
const char *format, va_list args)
{
- vsyslog(priority, format, args);
+ log_metav(priority, file, line, fn, format, args);
}
static void set_type(const char *from, char *to, size_t len)
break;
}
}
- util_strscpy(to, len, type);
+ strscpy(to, len, type);
}
/*
int *argc, char ***newargv)
{
char *buffer;
- FILE *fd;
+ _cleanup_fclose_ FILE *f;
char *buf;
char *str1;
char *vendor_in, *model_in, *options_in; /* read in from file */
int c;
int retval = 0;
- fd = fopen(config_file, "re");
- if (fd == NULL) {
- if (errno == ENOENT) {
+ f = fopen(config_file, "re");
+ if (f == NULL) {
+ if (errno == ENOENT)
return 1;
- } else {
- log_error("can't open %s: %s\n", config_file, strerror(errno));
+ else {
+ log_error("can't open %s: %m", config_file);
return -1;
}
}
* points into this buffer for its strings).
*/
buffer = malloc(MAX_BUFFER_LEN);
- if (!buffer) {
- fclose(fd);
- log_error("can't allocate memory\n");
- return -1;
- }
+ if (!buffer)
+ return log_oom();
*newargv = NULL;
lineno = 0;
while (1) {
vendor_in = model_in = options_in = NULL;
- buf = fgets(buffer, MAX_BUFFER_LEN, fd);
+ buf = fgets(buffer, MAX_BUFFER_LEN, f);
if (buf == NULL)
break;
lineno++;
if (buf[strlen(buffer) - 1] != '\n') {
- log_error("Config file line %d too long\n", lineno);
+ log_error("Config file line %d too long", lineno);
break;
}
continue;
str1 = strsep(&buf, "=");
- if (str1 && strcasecmp(str1, "VENDOR") == 0) {
+ if (str1 && strcaseeq(str1, "VENDOR")) {
str1 = get_value(&buf);
if (!str1) {
- retval = -1;
+ retval = log_oom();
break;
}
vendor_in = str1;
str1 = strsep(&buf, "=");
- if (str1 && strcasecmp(str1, "MODEL") == 0) {
+ if (str1 && strcaseeq(str1, "MODEL")) {
str1 = get_value(&buf);
if (!str1) {
- retval = -1;
+ retval = log_oom();
break;
}
model_in = str1;
}
}
- if (str1 && strcasecmp(str1, "OPTIONS") == 0) {
+ if (str1 && strcaseeq(str1, "OPTIONS")) {
str1 = get_value(&buf);
if (!str1) {
- retval = -1;
+ retval = log_oom();
break;
}
options_in = str1;
* Only allow: [vendor=foo[,model=bar]]options=stuff
*/
if (!options_in || (!vendor_in && model_in)) {
- log_error("Error parsing config file line %d '%s'\n", lineno, buffer);
+ log_error("Error parsing config file line %d '%s'", lineno, buffer);
retval = -1;
break;
}
if (vendor == NULL) {
if (vendor_in == NULL)
break;
- } else if ((vendor_in && strncmp(vendor, vendor_in,
- strlen(vendor_in)) == 0) &&
- (!model_in || (strncmp(model, model_in,
- strlen(model_in)) == 0))) {
+ } else if (vendor_in &&
+ strneq(vendor, vendor_in, strlen(vendor_in)) &&
+ (!model_in ||
+ (strneq(model, model_in, strlen(model_in))))) {
/*
* Matched vendor and optionally model.
*
c = argc_count(buffer) + 2;
*newargv = calloc(c, sizeof(**newargv));
if (!*newargv) {
- log_error("can't allocate memory\n");
- retval = -1;
+ retval = log_oom();
} else {
*argc = c;
c = 0;
}
if (retval != 0)
free(buffer);
- fclose(fd);
return retval;
}
+static void help(void) {
+ printf("Usage: scsi_id [OPTION...] DEVICE\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 threat device as blacklisted\n"
+ " -g,--whitelisted threat device as whitelisted\n"
+ " -u,--replace-whitespace replace all whitespace by underscores\n"
+ " -v,--verbose verbose logging\n"
+ " --version print version\n"
+ " -x,--export print values as environment keys\n"
+ " -h,--help print this help text\n\n");
+
+}
+
static int set_options(struct udev *udev,
- int argc, char **argv, const char *short_opts,
+ int argc, char **argv,
char *maj_min_dev)
{
int option;
* file) we have to reset this back to 1.
*/
optind = 1;
- while (1) {
- option = getopt_long(argc, argv, short_opts, options, NULL);
- if (option == -1)
- break;
-
+ while ((option = getopt_long(argc, argv, "d:f:gp:uvVxh", options, NULL)) >= 0)
switch (option) {
case 'b':
- all_good = 0;
+ all_good = false;
break;
case 'd':
- dev_specified = 1;
- util_strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
- break;
-
- case 'e':
- use_stderr = 1;
+ dev_specified = true;
+ strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
break;
case 'f':
- util_strscpy(config_file, MAX_PATH_LEN, optarg);
+ strscpy(config_file, MAX_PATH_LEN, optarg);
break;
case 'g':
- all_good = 1;
+ all_good = true;
break;
case 'h':
- printf("Usage: scsi_id OPTIONS <device>\n"
- " --device= device node for SG_IO commands\n"
- " --config= location of config file\n"
- " --page=0x80|0x83|pre-spc3-83 SCSI page (0x80, 0x83, pre-spc3-83)\n"
- " --sg-version=3|4 use SGv3 or SGv4\n"
- " --blacklisted threat device as blacklisted\n"
- " --whitelisted threat device as whitelisted\n"
- " --replace-whitespace replace all whitespaces by underscores\n"
- " --verbose verbose logging\n"
- " --version print version\n"
- " --export print values as environment keys\n"
- " --help print this help text\n\n");
+ help();
exit(0);
case 'p':
- if (strcmp(optarg, "0x80") == 0) {
+ if (streq(optarg, "0x80"))
default_page_code = PAGE_80;
- } else if (strcmp(optarg, "0x83") == 0) {
+ else if (streq(optarg, "0x83"))
default_page_code = PAGE_83;
- } else if (strcmp(optarg, "pre-spc3-83") == 0) {
+ else if (streq(optarg, "pre-spc3-83"))
default_page_code = PAGE_83_PRE_SPC3;
- } else {
- log_error("Unknown page code '%s'\n", optarg);
+ else {
+ log_error("Unknown page code '%s'", optarg);
return -1;
}
break;
case 's':
sg_version = atoi(optarg);
if (sg_version < 3 || sg_version > 4) {
- log_error("Unknown SG version '%s'\n", optarg);
+ log_error("Unknown SG version '%s'", optarg);
return -1;
}
break;
case 'u':
- reformat_serial = 1;
- break;
-
- case 'x':
- export = 1;
+ reformat_serial = true;
break;
case 'v':
- debug++;
+ log_set_target(LOG_TARGET_CONSOLE);
+ log_set_max_level(LOG_DEBUG);
+ udev_set_log_priority(udev, LOG_DEBUG);
+ log_open();
break;
case 'V':
printf("%s\n", VERSION);
exit(0);
+
+ case 'x':
+ export = true;
break;
+ case '?':
+ return -1;
+
default:
- exit(1);
+ assert_not_reached("Unknown option");
}
- }
+
if (optind < argc && !dev_specified) {
- dev_specified = 1;
- util_strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
+ dev_specified = true;
+ strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
}
+
return 0;
}
optind = 1; /* reset this global extern */
while (retval == 0) {
- option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
+ option = getopt_long(newargc, newargv, "bgp:", options, NULL);
if (option == -1)
break;
break;
case 'p':
- if (strcmp(optarg, "0x80") == 0) {
+ if (streq(optarg, "0x80")) {
*page_code = PAGE_80;
- } else if (strcmp(optarg, "0x83") == 0) {
+ } else if (streq(optarg, "0x83")) {
*page_code = PAGE_83;
- } else if (strcmp(optarg, "pre-spc3-83") == 0) {
+ } else if (streq(optarg, "pre-spc3-83")) {
*page_code = PAGE_83_PRE_SPC3;
} else {
- log_error("Unknown page code '%s'\n", optarg);
+ log_error("Unknown page code '%s'", optarg);
retval = -1;
}
break;
default:
- log_error("Unknown or bad option '%c' (0x%x)\n", option, option);
+ log_error("Unknown or bad option '%c' (0x%x)", option, option);
retval = -1;
break;
}
*/
static int scsi_id(struct udev *udev, char *maj_min_dev)
{
- struct scsi_id_device dev_scsi;
+ struct scsi_id_device dev_scsi = {};
int good_dev;
int page_code;
int retval = 0;
- memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
-
if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
retval = 1;
goto out;
int main(int argc, char **argv)
{
- struct udev *udev;
+ _cleanup_udev_unref_ struct udev *udev;
int retval = 0;
char maj_min_dev[MAX_PATH_LEN];
int newargc;
- char **newargv;
+ char **newargv = NULL;
+
+ log_parse_environment();
+ log_open();
udev = udev_new();
if (udev == NULL)
goto exit;
- log_open();
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) {
+ if (retval == 0) {
+ assert(newargv);
+
+ if (set_options(udev, newargc, newargv, 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)
+ if (set_options(udev, argc, argv, maj_min_dev) < 0)
exit(1);
if (!dev_specified) {
- log_error("no device specified\n");
+ log_error("no device specified");
retval = 1;
goto exit;
}
retval = scsi_id(udev, maj_min_dev);
exit:
- udev_unref(udev);
+ if (newargv) {
+ free(newargv[0]);
+ free(newargv);
+ }
log_close();
return retval;
}