diff options
author | Anthony G. Basile <blueness@gentoo.org> | 2014-01-14 12:20:25 -0500 |
---|---|---|
committer | Anthony G. Basile <blueness@gentoo.org> | 2014-01-14 12:20:25 -0500 |
commit | b8ed8b124aaff754e244dff6930b36c192fa69e0 (patch) | |
tree | c6d7caf75d42584eaf0227332d6c43ecc76d7565 /src/scsi_id | |
parent | fd98dfdcb889c74c84a00e54e8094f9c91387c6a (diff) |
src/libudev: bring in line with upstream
Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
Diffstat (limited to 'src/scsi_id')
-rw-r--r-- | src/scsi_id/scsi_id.c | 183 | ||||
-rw-r--r-- | src/scsi_id/scsi_id.h | 8 | ||||
-rw-r--r-- | src/scsi_id/scsi_serial.c | 62 |
3 files changed, 124 insertions, 129 deletions
diff --git a/src/scsi_id/scsi_id.c b/src/scsi_id/scsi_id.c index 58a54d4a63..849453f8eb 100644 --- a/src/scsi_id/scsi_id.c +++ b/src/scsi_id/scsi_id.c @@ -18,13 +18,14 @@ #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> @@ -32,34 +33,31 @@ #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 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; +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 int debug = 0; +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]; @@ -173,7 +171,7 @@ static int get_file_options(struct udev *udev, 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 */ @@ -181,12 +179,12 @@ static int get_file_options(struct udev *udev, 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; } } @@ -197,22 +195,20 @@ static int get_file_options(struct udev *udev, * points into this buffer for its strings). */ buffer = malloc(MAX_BUFFER_LEN); - if (!buffer) { - fclose(fd); + 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; } @@ -261,17 +257,17 @@ static int get_file_options(struct udev *udev, * 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 && strneq(vendor, vendor_in, - strlen(vendor_in))) && - (!model_in || (strneq(model, model_in, - strlen(model_in))))) { + } 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. * @@ -314,12 +310,27 @@ static int get_file_options(struct udev *udev, } 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; @@ -330,57 +341,38 @@ static int set_options(struct udev *udev, * 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; + dev_specified = true; strscpy(maj_min_dev, MAX_PATH_LEN, optarg); break; - case 'e': - use_stderr = 1; - break; - case 'f': 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 (streq(optarg, "0x80")) { + if (streq(optarg, "0x80")) default_page_code = PAGE_80; - } else if (streq(optarg, "0x83")) { + else if (streq(optarg, "0x83")) default_page_code = PAGE_83; - } else if (streq(optarg, "pre-spc3-83")) { + 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; @@ -388,17 +380,13 @@ static int set_options(struct udev *udev, 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': @@ -410,14 +398,22 @@ static int set_options(struct udev *udev, exit(0); break; + case 'x': + export = true; + break; + + case '?': + return -1; + default: - exit(1); + assert_not_reached("Unknown option"); } - } + if (optind < argc && !dev_specified) { - dev_specified = 1; + dev_specified = true; strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]); } + return 0; } @@ -436,7 +432,7 @@ static int per_dev_options(struct udev *udev, 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; @@ -457,13 +453,13 @@ static int per_dev_options(struct udev *udev, } 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; } @@ -505,13 +501,11 @@ static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, co */ 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; @@ -584,11 +578,11 @@ 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; udev = udev_new(); if (udev == NULL) @@ -600,28 +594,28 @@ int main(int argc, char **argv) /* * 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; } @@ -629,7 +623,10 @@ int main(int argc, char **argv) retval = scsi_id(udev, maj_min_dev); exit: - udev_unref(udev); + if (newargv) { + free(newargv[0]); + free(newargv); + } log_close(); return retval; } diff --git a/src/scsi_id/scsi_id.h b/src/scsi_id/scsi_id.h index 103e443d07..648b5ce42a 100644 --- a/src/scsi_id/scsi_id.h +++ b/src/scsi_id/scsi_id.h @@ -66,8 +66,8 @@ int scsi_get_serial(struct udev *udev, struct scsi_id_device *dev_scsi, const ch * Page code values. */ enum page_code { - PAGE_83_PRE_SPC3 = -0x83, - PAGE_UNSPECIFIED = 0x00, - PAGE_80 = 0x80, - PAGE_83 = 0x83, + PAGE_83_PRE_SPC3 = -0x83, + PAGE_UNSPECIFIED = 0x00, + PAGE_80 = 0x80, + PAGE_83 = 0x83, }; diff --git a/src/scsi_id/scsi_serial.c b/src/scsi_id/scsi_serial.c index d522a23a08..66e752e426 100644 --- a/src/scsi_id/scsi_serial.c +++ b/src/scsi_id/scsi_serial.c @@ -197,7 +197,7 @@ static int scsi_dump_sense(struct udev *udev, */ if (sb_len < 1) { - log_debug("%s: sense buffer empty\n", dev_scsi->kernel); + log_debug("%s: sense buffer empty", dev_scsi->kernel); return -1; } @@ -210,7 +210,7 @@ static int scsi_dump_sense(struct udev *udev, */ s = sense_buffer[7] + 8; if (sb_len < s) { - log_debug("%s: sense buffer too small %d bytes, %d bytes too short\n", + log_debug("%s: sense buffer too small %d bytes, %d bytes too short", dev_scsi->kernel, sb_len, s - sb_len); return -1; } @@ -220,7 +220,7 @@ static int scsi_dump_sense(struct udev *udev, /* * Possible? */ - log_debug("%s: sense result too" " small %d bytes\n", + log_debug("%s: sense result too" " small %d bytes", dev_scsi->kernel, s); return -1; } @@ -231,25 +231,25 @@ static int scsi_dump_sense(struct udev *udev, asc = sense_buffer[2]; ascq = sense_buffer[3]; } else { - log_debug("%s: invalid sense code 0x%x\n", + log_debug("%s: invalid sense code 0x%x", dev_scsi->kernel, code); return -1; } - log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x\n", + log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x", dev_scsi->kernel, sense_key, asc, ascq); } else { if (sb_len < 4) { - log_debug("%s: sense buffer too small %d bytes, %d bytes too short\n", + log_debug("%s: sense buffer too small %d bytes, %d bytes too short", dev_scsi->kernel, sb_len, 4 - sb_len); return -1; } if (sense_buffer[0] < 15) - log_debug("%s: old sense key: 0x%x\n", dev_scsi->kernel, sense_buffer[0] & 0x0f); + log_debug("%s: old sense key: 0x%x", dev_scsi->kernel, sense_buffer[0] & 0x0f); else - log_debug("%s: sense = %2x %2x\n", + log_debug("%s: sense = %2x %2x", dev_scsi->kernel, sense_buffer[0], sense_buffer[2]); - log_debug("%s: non-extended sense class %d code 0x%0x\n", + log_debug("%s: non-extended sense class %d code 0x%0x", dev_scsi->kernel, sense_class, code); } @@ -261,8 +261,8 @@ static int scsi_dump_sense(struct udev *udev, out_buffer[j++] = ' '; } out_buffer[j] = '\0'; - log_debug("%s: sense dump:\n", dev_scsi->kernel); - log_debug("%s: %s\n", dev_scsi->kernel, out_buffer); + log_debug("%s: sense dump:", dev_scsi->kernel); + log_debug("%s: %s", dev_scsi->kernel, out_buffer); #endif return -1; @@ -276,11 +276,11 @@ static int scsi_dump(struct udev *udev, /* * Impossible, should not be called. */ - log_debug("%s: called with no error\n", __FUNCTION__); + log_debug("%s: called with no error", __FUNCTION__); return -1; } - log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x\n", + log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x", dev_scsi->kernel, io->driver_status, io->host_status, io->msg_status, io->status); if (io->status == SCSI_CHECK_CONDITION) return scsi_dump_sense(udev, dev_scsi, io->sbp, io->sb_len_wr); @@ -296,11 +296,11 @@ static int scsi_dump_v4(struct udev *udev, /* * Impossible, should not be called. */ - log_debug("%s: called with no error\n", __FUNCTION__); + log_debug("%s: called with no error", __FUNCTION__); return -1; } - log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x\n", + log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x", dev_scsi->kernel, io->driver_status, io->transport_status, io->device_status); if (io->device_status == SCSI_CHECK_CONDITION) @@ -325,7 +325,7 @@ static int scsi_inquiry(struct udev *udev, int retval; if (buflen > SCSI_INQ_BUFF_LEN) { - log_debug("buflen %d too long\n", buflen); + log_debug("buflen %d too long", buflen); return -1; } @@ -362,7 +362,7 @@ resend: dev_scsi->use_sg = 3; goto resend; } - log_debug("%s: ioctl failed: %s\n", dev_scsi->kernel, strerror(errno)); + log_debug("%s: ioctl failed: %m", dev_scsi->kernel); goto error; } @@ -397,7 +397,7 @@ resend: error: if (retval < 0) - log_debug("%s: Unable to get INQUIRY vpd %d page 0x%x.\n", + log_debug("%s: Unable to get INQUIRY vpd %d page 0x%x.", dev_scsi->kernel, evpd, page); return retval; @@ -416,11 +416,11 @@ static int do_scsi_page0_inquiry(struct udev *udev, return 1; if (buffer[1] != 0) { - log_debug("%s: page 0 not available.\n", dev_scsi->kernel); + log_debug("%s: page 0 not available.", dev_scsi->kernel); return 1; } if (buffer[3] > len) { - log_debug("%s: page 0 buffer too long %d\n", dev_scsi->kernel, buffer[3]); + log_debug("%s: page 0 buffer too long %d", dev_scsi->kernel, buffer[3]); return 1; } @@ -437,7 +437,7 @@ static int do_scsi_page0_inquiry(struct udev *udev, * invalid. */ if (strneq((char *)&buffer[VENDOR_LENGTH], dev_scsi->vendor, VENDOR_LENGTH)) { - log_debug("%s: invalid page0 data\n", dev_scsi->kernel); + log_debug("%s: invalid page0 data", dev_scsi->kernel); return 1; } } @@ -462,7 +462,7 @@ static int prepend_vendor_model(struct udev *udev, * above, ind will never be too large. */ if (ind != (VENDOR_LENGTH + MODEL_LENGTH)) { - log_debug("%s: expected length %d, got length %d\n", + log_debug("%s: expected length %d, got length %d", dev_scsi->kernel, (VENDOR_LENGTH + MODEL_LENGTH), ind); return -1; } @@ -528,7 +528,7 @@ static int check_fill_0x83_id(struct udev *udev, len += VENDOR_LENGTH + MODEL_LENGTH; if (max_len < len) { - log_debug("%s: length %d too short - need %d\n", + log_debug("%s: length %d too short - need %d", dev_scsi->kernel, max_len, len); return 1; } @@ -629,7 +629,7 @@ static int do_scsi_page83_inquiry(struct udev *udev, return 1; if (page_83[1] != PAGE_83) { - log_debug("%s: Invalid page 0x83\n", dev_scsi->kernel); + log_debug("%s: Invalid page 0x83", dev_scsi->kernel); return 1; } @@ -715,7 +715,7 @@ static int do_scsi_page83_prespc3_inquiry(struct udev *udev, return 1; if (page_83[1] != PAGE_83) { - log_debug("%s: Invalid page 0x83\n", dev_scsi->kernel); + log_debug("%s: Invalid page 0x83", dev_scsi->kernel); return 1; } /* @@ -779,13 +779,13 @@ static int do_scsi_page80_inquiry(struct udev *udev, return retval; if (buf[1] != PAGE_80) { - log_debug("%s: Invalid page 0x80\n", dev_scsi->kernel); + log_debug("%s: Invalid page 0x80", dev_scsi->kernel); return 1; } len = 1 + VENDOR_LENGTH + MODEL_LENGTH + buf[3]; if (max_len < len) { - log_debug("%s: length %d too short - need %d\n", + log_debug("%s: length %d too short - need %d", dev_scsi->kernel, max_len, len); return 1; } @@ -820,14 +820,12 @@ int scsi_std_inquiry(struct udev *udev, fd = open(devname, O_RDONLY | O_NONBLOCK); if (fd < 0) { - log_debug("scsi_id: cannot open %s: %s\n", - devname, strerror(errno)); + log_debug("scsi_id: cannot open %s: %m", devname); return 1; } if (fstat(fd, &statbuf) < 0) { - log_debug("scsi_id: cannot stat %s: %s\n", - devname, strerror(errno)); + log_debug("scsi_id: cannot stat %s: %m", devname); err = 2; goto out; } @@ -920,7 +918,7 @@ int scsi_get_serial(struct udev *udev, goto completed; } } else if (page_code != 0x00) { - log_debug("%s: unsupported page code 0x%d\n", dev_scsi->kernel, page_code); + log_debug("%s: unsupported page code 0x%d", dev_scsi->kernel, page_code); retval = 1; goto completed; } |