diff options
author | Douglas Gilbert <dgilbert@interlog.com> | 2018-05-15 20:44:45 +0000 |
---|---|---|
committer | Douglas Gilbert <dgilbert@interlog.com> | 2018-05-15 20:44:45 +0000 |
commit | 856fb91355bddbe7a15a9d11334c354d8ee7b6d3 (patch) | |
tree | 9446f14be2d17b1f0449a466e0762a9a3361ca7b /src | |
parent | 71e56cc16387a76971a04fda4ff9306ec704daf9 (diff) | |
download | sg3_utils-856fb91355bddbe7a15a9d11334c354d8ee7b6d3.tar.gz |
cleanup of error messages around sg_cmds_open_device(); add flock error msg for ddpt
git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@772 6180dd3e-e324-4e3e-922d-17de1ae2f315
Diffstat (limited to 'src')
45 files changed, 748 insertions, 410 deletions
diff --git a/src/sg_bg_ctl.c b/src/sg_bg_ctl.c index b36ef03e..f0ea7ff6 100644 --- a/src/sg_bg_ctl.c +++ b/src/sg_bg_ctl.c @@ -33,7 +33,7 @@ * device. Based on sbc4r10.pdf . */ -static const char * version_str = "1.07 20180509"; +static const char * version_str = "1.08 20180512"; #define BACKGROUND_CONTROL_SA 0x15 @@ -235,7 +235,7 @@ fini: if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) - return sg_convert_errno(-res); + ret = sg_convert_errno(-res); } } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; diff --git a/src/sg_compare_and_write.c b/src/sg_compare_and_write.c index 24a66b5d..2db81001 100644 --- a/src/sg_compare_and_write.c +++ b/src/sg_compare_and_write.c @@ -54,7 +54,7 @@ #include "sg_unaligned.h" #include "sg_pr2serr.h" -static const char * version_str = "1.23 20180510"; +static const char * version_str = "1.24 20180512"; #define DEF_BLOCK_SIZE 512 #define DEF_NUM_BLOCKS (1) @@ -444,11 +444,9 @@ open_dev(const char * outf, int verbose) { int sg_fd = sg_cmds_open_device(outf, false /* rw */, verbose); - if (sg_fd < 0) { + if ((sg_fd < 0) && verbose) pr2serr(ME "open error: %s: %s\n", outf, safe_strerror(-sg_fd)); - return -sg_convert_errno(-sg_fd); - } return sg_fd; } @@ -505,7 +503,7 @@ main(int argc, char * argv[]) devfd = open_dev(op->device_name, vb); if (devfd < 0) { - res = -devfd; + res = sg_convert_errno(-devfd); goto out; } @@ -551,8 +549,6 @@ main(int argc, char * argv[]) res = sg_ll_compare_and_write(devfd, wrkBuff, op->numblocks, op->lba, op->xfer_len, op->flags, ! op->quiet, vb); - -out: if (0 != res) { char b[80]; @@ -567,7 +563,7 @@ out: break; } } - +out: if (free_wrkBuff) free(free_wrkBuff); if ((infd >= 0) && (! ifn_stdin)) diff --git a/src/sg_copy_results.c b/src/sg_copy_results.c index 0c64cf61..09989791 100644 --- a/src/sg_copy_results.c +++ b/src/sg_copy_results.c @@ -38,7 +38,7 @@ and the optional list identifier passed as the list_id argument. */ -static const char * version_str = "1.21 20180510"; +static const char * version_str = "1.22 20180512"; #define MAX_XFER_LEN 10000 @@ -411,9 +411,10 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, o_readonly, verbose); if (sg_fd < 0) { - pr2serr(ME "open error: %s: %s\n", device_name, - safe_strerror(-sg_fd)); - ret = SG_LIB_FILE_ERROR; + if (verbose) + pr2serr(ME "open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); goto finish; } diff --git a/src/sg_format.c b/src/sg_format.c index 563bc307..eb0ad798 100644 --- a/src/sg_format.c +++ b/src/sg_format.c @@ -37,7 +37,7 @@ #include "sg_pr2serr.h" #include "sg_pt.h" -static const char * version_str = "1.46 20180510"; +static const char * version_str = "1.47 20180512"; #define RW_ERROR_RECOVERY_PAGE 1 /* can give alternate with --mode=MP */ diff --git a/src/sg_modes.c b/src/sg_modes.c index e680a153..ebcaabc9 100644 --- a/src/sg_modes.c +++ b/src/sg_modes.c @@ -30,7 +30,7 @@ #include "sg_unaligned.h" #include "sg_pr2serr.h" -static const char * version_str = "1.60 20180414"; +static const char * version_str = "1.62 20180511"; #define DEF_ALLOC_LEN (1024 * 4) #define DEF_6_ALLOC_LEN 252 @@ -62,6 +62,7 @@ static struct option long_options[] = { {"raw", no_argument, 0, 'r'}, {"read-write", no_argument, 0, 'w'}, {"read_write", no_argument, 0, 'w'}, + {"readwrite", no_argument, 0, 'w'}, {"six", no_argument, 0, '6'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, diff --git a/src/sg_persist.c b/src/sg_persist.c index 81f81117..8ff8bf0b 100644 --- a/src/sg_persist.c +++ b/src/sg_persist.c @@ -33,7 +33,7 @@ #include "sg_unaligned.h" #include "sg_pr2serr.h" -static const char * version_str = "0.63 20180326"; +static const char * version_str = "0.64 20180511"; #define PRIN_RKEY_SA 0x0 @@ -1282,7 +1282,7 @@ main(int argc, char * argv[]) fini: if (ret && (0 == op->verbose) && (! flagged)) { - if (! sg_if_can2stderr("", ret)) + if (! sg_if_can2stderr("sg_persist failed: ", ret)) pr2serr("Some error occurred [%d]\n", ret); } if (sg_fd >= 0) { diff --git a/src/sg_prevent.c b/src/sg_prevent.c index 7c2e6683..ba262ce6 100644 --- a/src/sg_prevent.c +++ b/src/sg_prevent.c @@ -134,9 +134,11 @@ int main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, false /* rw */, verbose); if (sg_fd < 0) { - pr2serr(ME "open error: %s: %s\n", device_name, - safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr(ME "open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto fini; } res = sg_ll_prevent_allow(sg_fd, prevent, true, verbose); ret = res; @@ -150,7 +152,13 @@ int main(int argc, char * argv[]) if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) - return SG_LIB_FILE_ERROR; + ret = sg_convert_errno(-res); + } +fini: + if (0 == verbose) { + if (! sg_if_can2stderr("sg_prevent failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_rdac.c b/src/sg_rdac.c index f979c759..73f4684a 100644 --- a/src/sg_rdac.c +++ b/src/sg_rdac.c @@ -30,7 +30,7 @@ #include "sg_pr2serr.h" -static const char * version_str = "1.16 20180219"; +static const char * version_str = "1.17 20180512"; uint8_t mode6_hdr[] = { 0x75, /* Length */ @@ -440,7 +440,8 @@ int main(int argc, char * argv[]) if (fd < 0) { pr2serr("open error: %s: %s\n", file_name, safe_strerror(-fd)); usage(); - return SG_LIB_FILE_ERROR; + ret = sg_convert_errno(-fd); + goto fini; } if (fail_all) { @@ -501,7 +502,13 @@ int main(int argc, char * argv[]) if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) - return SG_LIB_FILE_ERROR; + ret = sg_convert_errno(res); + } +fini: + if (0 == do_verbose) { + if (! sg_if_can2stderr("sg_rdac failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_read_attr.c b/src/sg_read_attr.c index ff10af92..32f19ec3 100644 --- a/src/sg_read_attr.c +++ b/src/sg_read_attr.c @@ -37,7 +37,7 @@ * and decodes the response. Based on spc5r08.pdf */ -static const char * version_str = "1.09 20180425"; +static const char * version_str = "1.10 20180512"; #define MAX_RATTR_BUFF_LEN (1024 * 1024) #define DEF_RATTR_BUFF_LEN (1024 * 8) @@ -1096,7 +1096,7 @@ main(int argc, char * argv[]) if (sg_fd < 0) { pr2serr("open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - ret = SG_LIB_FILE_ERROR; + ret = sg_convert_errno(-sg_fd); goto clean_up; } @@ -1141,10 +1141,15 @@ close_then_end: if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) - ret = SG_LIB_FILE_ERROR; + ret = sg_convert_errno(-res); } clean_up: if (free_rabp) free(free_rabp); + if (0 == op->verbose) { + if (! sg_if_can2stderr("sg_read_attr failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' or '-vv' for " + "more information\n"); + } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_read_block_limits.c b/src/sg_read_block_limits.c index 5dbc8478..607627ca 100644 --- a/src/sg_read_block_limits.c +++ b/src/sg_read_block_limits.c @@ -147,8 +147,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, readonly, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto the_end2; } memset(readBlkLmtBuff, 0x0, 6); @@ -195,7 +198,13 @@ the_end: if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) - return SG_LIB_FILE_ERROR; + ret = sg_convert_errno(-res); + } +the_end2: + if (0 == verbose) { + if (! sg_if_can2stderr("sg_read_block_limits failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' or '-vv' for " + "more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_read_buffer.c b/src/sg_read_buffer.c index 2137ba6a..d643a272 100644 --- a/src/sg_read_buffer.c +++ b/src/sg_read_buffer.c @@ -33,7 +33,7 @@ * device. */ -static const char * version_str = "1.23 20180219"; +static const char * version_str = "1.24 20180513"; #ifndef SG_READ_BUFFER_10_CMD @@ -448,8 +448,10 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, o_readonly, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - ret = SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); goto fini; } @@ -512,8 +514,13 @@ fini: if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) - return SG_LIB_FILE_ERROR; + ret = sg_convert_errno(-res); } } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_read_buffer failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); + } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_read_long.c b/src/sg_read_long.c index 1c278801..d9f24829 100644 --- a/src/sg_read_long.c +++ b/src/sg_read_long.c @@ -19,6 +19,7 @@ #include <stdarg.h> #include <stdbool.h> #include <string.h> +#include <errno.h> #include <getopt.h> #include <errno.h> #define __STDC_FORMAT_MACROS 1 @@ -127,14 +128,16 @@ main(int argc, char * argv[]) bool pblock = false; bool readonly = false; bool got_stdout; - int sg_fd, outfd, res, c; + int outfd, res, c; + int sg_fd = -1; int ret = 0; int xfer_len = 520; int verbose = 0; uint64_t llba = 0; int64_t ll; uint8_t * readLongBuff = NULL; - void * rawp = NULL; + uint8_t * rawp = NULL; + uint8_t * free_rawp = NULL; const char * device_name = NULL; char out_fname[256]; char ebuff[EBUFF_SZ]; @@ -221,14 +224,19 @@ main(int argc, char * argv[]) } sg_fd = sg_cmds_open_device(device_name, readonly, verbose); if (sg_fd < 0) { - pr2serr(ME "open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr(ME "open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto err_out; } - if (NULL == (rawp = malloc(MAX_XFER_LEN))) { - pr2serr(ME "out of memory\n"); - sg_cmds_close_device(sg_fd); - return SG_LIB_SYNTAX_ERROR; + if (NULL == (rawp = (uint8_t *)sg_memalign(MAX_XFER_LEN, 0, &free_rawp, + false))) { + if (verbose) + pr2serr(ME "out of memory\n"); + ret = sg_convert_errno(ENOMEM); + goto err_out; } readLongBuff = (uint8_t *)rawp; memset(rawp, 0x0, MAX_XFER_LEN); @@ -272,12 +280,20 @@ main(int argc, char * argv[]) } err_out: - if (rawp) free(rawp); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (free_rawp) + free(free_rawp); + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_read_long failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_readcap.c b/src/sg_readcap.c index 9b5a6850..ac0b96e1 100644 --- a/src/sg_readcap.c +++ b/src/sg_readcap.c @@ -71,7 +71,7 @@ struct opts_t { int do_help; int do_hex; int do_lba; - int do_verbose; + int verbose; uint64_t llba; const char * device_name; }; @@ -214,7 +214,7 @@ new_parse_cmd_line(struct opts_t * op, int argc, char * argv[]) op->o_readonly = true; break; case 'v': - ++op->do_verbose; + ++op->verbose; break; case 'V': op->do_version = true; @@ -299,7 +299,7 @@ old_parse_cmd_line(struct opts_t * op, int argc, char * argv[]) op->o_readonly = true; break; case 'v': - ++op->do_verbose; + ++op->verbose; break; case 'V': op->do_version = true; @@ -457,7 +457,7 @@ main(int argc, char * argv[]) else rw_0_flag = true; /* RCAP(10) has opened RO in past, so leave */ if ((sg_fd = sg_cmds_open_device(op->device_name, rw_0_flag, - op->do_verbose)) < 0) { + op->verbose)) < 0) { pr2serr(ME "error opening file: %s: %s\n", op->device_name, safe_strerror(-sg_fd)); ret = sg_convert_errno(-sg_fd); @@ -467,7 +467,7 @@ main(int argc, char * argv[]) if (! op->do_long) { res = sg_ll_readcap_10(sg_fd, op->do_pmi, (unsigned int)op->llba, resp_buff, RCAP_REPLY_LEN, true, - op->do_verbose); + op->verbose); ret = res; if (0 == res) { if (op->do_hex || op->do_raw) { @@ -525,23 +525,23 @@ main(int argc, char * argv[]) op->do_long = true; sg_cmds_close_device(sg_fd); if ((sg_fd = sg_cmds_open_device(op->device_name, op->o_readonly, - op->do_verbose)) < 0) { + op->verbose)) < 0) { pr2serr(ME "error re-opening file: %s (rw): %s\n", op->device_name, safe_strerror(-sg_fd)); ret = sg_convert_errno(-sg_fd); goto fini; } - if (op->do_verbose) + if (op->verbose) pr2serr("READ CAPACITY (10) not supported, trying READ " "CAPACITY (16)\n"); } else if (res) { - sg_get_category_sense_str(res, sizeof(b), b, op->do_verbose); + sg_get_category_sense_str(res, sizeof(b), b, op->verbose); pr2serr("READ CAPACITY (10) failed: %s\n", b); } } if (op->do_long) { res = sg_ll_readcap_16(sg_fd, op->do_pmi, op->llba, resp_buff, - RCAP16_REPLY_LEN, true, op->do_verbose); + RCAP16_REPLY_LEN, true, op->verbose); ret = res; if (0 == res) { if (op->do_hex || op->do_raw) { @@ -619,7 +619,7 @@ main(int argc, char * argv[]) pr2serr("bad field in READ CAPACITY (16) cdb including " "unsupported service action\n"); else if (res) { - sg_get_category_sense_str(res, sizeof(b), b, op->do_verbose); + sg_get_category_sense_str(res, sizeof(b), b, op->verbose); pr2serr("READ CAPACITY (16) failed: %s\n", b); } } @@ -633,8 +633,13 @@ fini: if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) - return SG_LIB_FILE_ERROR; + ret = sg_convert_errno(-res); } } + if (0 == op->verbose) { + if (! sg_if_can2stderr("sg_readcap failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); + } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_reassign.c b/src/sg_reassign.c index 79e3ffd7..0cba9582 100644 --- a/src/sg_reassign.c +++ b/src/sg_reassign.c @@ -37,7 +37,7 @@ * vendor specific data is written. */ -static const char * version_str = "1.24 20180219"; +static const char * version_str = "1.25 20180513"; #define DEF_DEFECT_LIST_FORMAT 4 /* bytes from index */ @@ -225,7 +225,8 @@ main(int argc, char * argv[]) bool longlist = false; bool primary = false; bool grown = false; - int sg_fd, res, c, num, k, j; + int res, c, num, k, j; + int sg_fd = -1; int addr_arr_len = 0; int do_hex = 0; int verbose = 0; @@ -365,8 +366,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, false /* rw */, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto err_out; } if (got_addr) { @@ -457,11 +461,18 @@ main(int argc, char * argv[]) } err_out: - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_reassign failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_referrals.c b/src/sg_referrals.c index 83ab89e7..f2036149 100644 --- a/src/sg_referrals.c +++ b/src/sg_referrals.c @@ -35,7 +35,7 @@ * SCSI device. */ -static const char * version_str = "1.11 20180425"; /* sbc4r10 */ +static const char * version_str = "1.12 20180513"; /* sbc4r10 */ #define MAX_REFER_BUFF_LEN (1024 * 1024) #define DEF_REFER_BUFF_LEN 256 @@ -173,7 +173,8 @@ main(int argc, char * argv[]) bool do_one_segment = false; bool o_readonly = false; bool do_raw = false; - int sg_fd, k, res, c, rlen; + int k, res, c, rlen; + int sg_fd = -1; int do_hex = 0; int maxlen = DEF_REFER_BUFF_LEN; int verbose = 0; @@ -276,8 +277,10 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, o_readonly, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - ret = SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); goto free_buff; } @@ -345,10 +348,15 @@ the_end: if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) - ret = SG_LIB_FILE_ERROR; + ret = sg_convert_errno(-res); } free_buff: if (free_referralBuffp) free(free_referralBuffp); + if (0 == verbose) { + if (! sg_if_can2stderr("sg_referrals failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); + } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_rep_zones.c b/src/sg_rep_zones.c index ce4078ad..ea1e8226 100644 --- a/src/sg_rep_zones.c +++ b/src/sg_rep_zones.c @@ -36,7 +36,7 @@ * and decodes the response. Based on zbc-r02.pdf */ -static const char * version_str = "1.16 20180425"; +static const char * version_str = "1.16 20180513"; #define MAX_RZONES_BUFF_LEN (1024 * 1024) #define DEF_RZONES_BUFF_LEN (1024 * 8) @@ -277,7 +277,8 @@ main(int argc, char * argv[]) bool do_partial = false; bool do_raw = false; bool o_readonly = false; - int sg_fd, k, res, c, zl_len, len, zones, resid, rlen, zt, zc, same; + int k, res, c, zl_len, len, zones, resid, rlen, zt, zc, same; + int sg_fd = -1; int do_help = 0; int do_hex = 0; int maxlen = 0; @@ -385,9 +386,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, o_readonly, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", device_name, - safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto the_end; } if (0 == maxlen) @@ -472,11 +475,18 @@ main(int argc, char * argv[]) the_end: if (free_rzbp) free(free_rzbp); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_rep_zones failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_requests.c b/src/sg_requests.c index fd85c054..6153c847 100644 --- a/src/sg_requests.c +++ b/src/sg_requests.c @@ -29,7 +29,7 @@ * This program issues the SCSI command REQUEST SENSE to the given SCSI device. */ -static const char * version_str = "1.30 20180219"; +static const char * version_str = "1.31 20180513"; #define MAX_REQS_RESP_LEN 255 #define DEF_REQS_RESP_LEN 252 @@ -110,7 +110,8 @@ dStrRaw(const uint8_t * str, int len) int main(int argc, char * argv[]) { - int sg_fd, res, c, resp_len, k, progress; + int res, c, resp_len, k, progress; + int sg_fd = -1; uint8_t requestSenseBuff[MAX_REQS_RESP_LEN + 1]; bool desc = false; bool do_progress = false; @@ -217,8 +218,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, true /* ro */, verbose); if (sg_fd < 0) { - pr2serr(ME "open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr(ME "open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto finish; } if (do_progress) { for (k = 0; k < num_rs; ++k) { @@ -344,11 +348,18 @@ main(int argc, char * argv[]) #endif finish: - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_requests failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_reset.c b/src/sg_reset.c index 36f14470..9f5648e8 100644 --- a/src/sg_reset.c +++ b/src/sg_reset.c @@ -1,12 +1,13 @@ /* A utility program originally written for the Linux OS SCSI subsystem. - * Copyright (C) 1999-2017 D. Gilbert + * Copyright (C) 1999-2018 D. Gilbert * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program send either device, bus or host resets to device, - * or bus or host associated with the given sg device. + * or bus or host associated with the given sg device. This is a Linux + * only utility (perhaps Android as well). */ #include <unistd.h> @@ -30,7 +31,7 @@ #define ME "sg_reset: " -static const char * version_str = "0.65 20171021"; +static const char * version_str = "0.66 20180515"; #ifndef SG_SCSI_RESET #define SG_SCSI_RESET 0x2284 @@ -103,8 +104,8 @@ usage(int compat_mode) " --host|-H host (bus adapter: HBA) reset\n"); } pr2serr(" --no-esc|-N overrides default action and only does " - " --no-escalate The same as --no-esc|-N" "reset requested\n" + " --no-escalate The same as --no-esc|-N" " --target|-t target reset. The target holds the DEVICE " "and perhaps\n" " other LUs\n" diff --git a/src/sg_reset_wp.c b/src/sg_reset_wp.c index 5542283b..8af91d4b 100644 --- a/src/sg_reset_wp.c +++ b/src/sg_reset_wp.c @@ -35,7 +35,7 @@ * device. Based on zbc-r04c.pdf . */ -static const char * version_str = "1.10 20180504"; +static const char * version_str = "1.11 20180513"; #define SG_ZONING_OUT_CMDLEN 16 #define RESET_WRITE_POINTER_SA 0x4 @@ -135,7 +135,8 @@ main(int argc, char * argv[]) { bool all = false; bool zid_given = false; - int sg_fd, res, c, n; + int res, c, n; + int sg_fd = -1; int ret = 0; int verbose = 0; uint16_t zc = 0; @@ -219,9 +220,11 @@ main(int argc, char * argv[]) if (sg_fd < 0) { int err = -sg_fd; - pr2serr("open error: %s: %s\n", device_name, - safe_strerror(err)); - return sg_convert_errno(err); + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(err)); + ret = sg_convert_errno(err); + goto fini; } res = sg_ll_reset_write_pointer(sg_fd, zid, zc, all, true, verbose); @@ -237,16 +240,19 @@ main(int argc, char * argv[]) } } +fini: + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } if (0 == verbose) { if (! sg_if_can2stderr("sg_reset_wp failed: ", ret)) pr2serr("Some error occurred, try again with '-v' or '-vv' for " "more information\n"); } - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - ret = sg_convert_errno(-res); - } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_rmsn.c b/src/sg_rmsn.c index cfbc29e2..41bf7ee7 100644 --- a/src/sg_rmsn.c +++ b/src/sg_rmsn.c @@ -31,7 +31,7 @@ * to the given SCSI device. */ -static const char * version_str = "1.16 20180219"; +static const char * version_str = "1.17 20180513"; #define SERIAL_NUM_SANITY_LEN (16 * 1024) @@ -65,7 +65,8 @@ int main(int argc, char * argv[]) { bool raw = false; bool readonly = false; - int sg_fd, res, c, sn_len, n; + int res, c, sn_len, n; + int sg_fd = -1; int ret = 0; int verbose = 0; uint8_t rmsn_buff[4]; @@ -130,8 +131,11 @@ int main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, readonly, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto err_out; } memset(rmsn_buff, 0x0, sizeof(rmsn_buff)); @@ -185,11 +189,18 @@ int main(int argc, char * argv[]) err_out: if (bp) free(bp); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_rmsn failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_rtpg.c b/src/sg_rtpg.c index 9a366583..2203c015 100644 --- a/src/sg_rtpg.c +++ b/src/sg_rtpg.c @@ -31,7 +31,7 @@ * to the given SCSI device. */ -static const char * version_str = "1.25 20180219"; +static const char * version_str = "1.26 20180513"; #define REPORT_TGT_GRP_BUFF_LEN 1024 @@ -143,7 +143,8 @@ int main(int argc, char * argv[]) bool raw = false; bool o_readonly = false; bool extended = false; - int sg_fd, k, j, off, res, c, report_len, tgt_port_count; + int k, j, off, res, c, report_len, tgt_port_count; + int sg_fd = -1; int ret = 0; int verbose = 0; uint8_t reportTgtGrpBuff[REPORT_TGT_GRP_BUFF_LEN]; @@ -217,8 +218,11 @@ int main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, o_readonly, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto err_out; } memset(reportTgtGrpBuff, 0x0, sizeof(reportTgtGrpBuff)); @@ -312,11 +316,18 @@ int main(int argc, char * argv[]) } err_out: - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_rtpg failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_safte.c b/src/sg_safte.c index 9edb9083..0a257851 100644 --- a/src/sg_safte.c +++ b/src/sg_safte.c @@ -514,7 +514,8 @@ main(int argc, char * argv[]) { bool do_insertions = false; bool no_hex_raw; - int sg_fd, c, ret, peri_type; + int c, ret, peri_type; + int sg_fd = -1; int res = SG_LIB_CAT_OTHER; const char * device_name = NULL; char ebuff[EBUFF_SZ]; @@ -610,10 +611,13 @@ main(int argc, char * argv[]) if ((sg_fd = sg_cmds_open_device(device_name, false /* rw */, verbose)) < 0) { - snprintf(ebuff, EBUFF_SZ, "sg_safte: error opening file: %s (rw)", - device_name); - perror(ebuff); - return SG_LIB_FILE_ERROR; + if (verbose) { + snprintf(ebuff, EBUFF_SZ, "sg_safte: error opening file: %s (rw)", + device_name); + perror(ebuff); + } + ret = sg_convert_errno(-sg_fd); + goto fini; } no_hex_raw = ((0 == do_hex) && (0 == do_raw)); @@ -728,11 +732,19 @@ err_out: break; } ret = res; - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; +fini: + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_safte failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_sanitize.c b/src/sg_sanitize.c index 38e5030b..aaa98c04 100644 --- a/src/sg_sanitize.c +++ b/src/sg_sanitize.c @@ -31,7 +31,7 @@ #include "sg_unaligned.h" #include "sg_pr2serr.h" -static const char * version_str = "1.07 20180219"; +static const char * version_str = "1.08 20180515"; /* Not all environments support the Unix sleep() */ #if defined(MSC_VER) || defined(__MINGW32__) @@ -432,7 +432,8 @@ int main(int argc, char * argv[]) { bool got_stdin = false; - int sg_fd, k, res, c, infd, progress, vb, n, resp_len; + int k, res, c, infd, progress, vb, n, resp_len, err; + int sg_fd = -1; int param_lst_len = 0; int ret = -1; const char * device_name = NULL; @@ -582,9 +583,11 @@ main(int argc, char * argv[]) if (! got_stdin) { memset(&a_stat, 0, sizeof(a_stat)); if (stat(op->pattern_fn, &a_stat) < 0) { + err = errno; pr2serr("pattern file: unable to stat(%s): %s\n", - op->pattern_fn, safe_strerror(errno)); - return SG_LIB_FILE_ERROR; + op->pattern_fn, safe_strerror(err)); + ret = sg_convert_errno(err); + goto err_out; } if (op->ipl <= 0) { op->ipl = (int)a_stat.st_size; @@ -605,9 +608,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, false /* rw */, vb); if (sg_fd < 0) { - pr2serr(ME "open error: %s: %s\n", device_name, - safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (op->verbose) + pr2serr(ME "open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto err_out; } ret = print_dev_id(sg_fd, inq_resp, sizeof(inq_resp), op->verbose); @@ -741,11 +746,18 @@ main(int argc, char * argv[]) err_out: if (wBuff) free(wBuff); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == op->verbose) { + if (! sg_if_can2stderr("sg_sanitize failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_sat_identify.c b/src/sg_sat_identify.c index 2fbe53e8..454b8c06 100644 --- a/src/sg_sat_identify.c +++ b/src/sg_sat_identify.c @@ -52,9 +52,10 @@ #define EBUFF_SZ 256 -static const char * version_str = "1.16 20180219"; +static const char * version_str = "1.17 20180515"; static struct option long_options[] = { + {"ck-cond", no_argument, 0, 'c'}, {"ck_cond", no_argument, 0, 'c'}, {"extend", no_argument, 0, 'e'}, {"help", no_argument, 0, 'h'}, @@ -392,7 +393,8 @@ int main(int argc, char * argv[]) bool o_readonly = false; bool ck_cond = false; /* set to true to read register(s) back */ bool extend = false; /* set to true to send 48 bit LBA with command */ - int sg_fd, c, res; + int c, res; + int sg_fd = -1; int cdb_len = SAT_ATA_PASS_THROUGH16_LEN; int do_hex = 0; int verbose = 0; @@ -483,19 +485,29 @@ int main(int argc, char * argv[]) } if ((sg_fd = sg_cmds_open_device(device_name, o_readonly, verbose)) < 0) { - pr2serr("error opening file: %s: %s\n", device_name, - safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("error opening file: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto fini; } ret = do_identify_dev(sg_fd, do_packet, cdb_len, ck_cond, extend, do_ident, do_hex, do_raw, verbose); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; +fini: + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_sat_identify failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_sat_phy_event.c b/src/sg_sat_phy_event.c index 0325c6f7..87b7ce54 100644 --- a/src/sg_sat_phy_event.c +++ b/src/sg_sat_phy_event.c @@ -25,7 +25,7 @@ #include "sg_cmds_extra.h" #include "sg_pr2serr.h" -static const char * version_str = "1.11 20180219"; +static const char * version_str = "1.12 20180515"; /* This program uses a ATA PASS-THROUGH SCSI command. This usage is * defined in the SCSI to ATA Translation (SAT) drafts and standards. @@ -62,6 +62,7 @@ static const char * version_str = "1.11 20180219"; static struct option long_options[] = { {"ck_cond", no_argument, 0, 'c'}, + {"ck-cond", no_argument, 0, 'c'}, {"extend", no_argument, 0, 'e'}, {"hex", no_argument, 0, 'H'}, {"ignore", no_argument, 0, 'i'}, diff --git a/src/sg_sat_read_gplog.c b/src/sg_sat_read_gplog.c index 0dbaa0fa..17885479 100644 --- a/src/sg_sat_read_gplog.c +++ b/src/sg_sat_read_gplog.c @@ -52,7 +52,7 @@ #define DEF_TIMEOUT 20 -static const char * version_str = "1.18 20180219"; +static const char * version_str = "1.19 20180513"; struct opts_t { bool ck_cond; @@ -69,6 +69,7 @@ struct opts_t { static struct option long_options[] = { {"count", required_argument, 0, 'c'}, {"ck_cond", no_argument, 0, 'C'}, + {"ck-cond", no_argument, 0, 'C'}, {"dma", no_argument, 0, 'd'}, {"help", no_argument, 0, 'h'}, {"hex", no_argument, 0, 'H'}, @@ -328,9 +329,11 @@ do_read_gplog(int sg_fd, int ata_cmd, uint8_t *inbuff, int main(int argc, char * argv[]) { - int sg_fd, c, ret, res, n; + int c, ret, res, n; + int sg_fd = -1; int ata_cmd = ATA_READ_LOG_EXT; - uint8_t *inbuff; + uint8_t *inbuff = NULL; + uint8_t *free_inbuff = NULL; struct opts_t opts; struct opts_t * op; @@ -431,7 +434,7 @@ main(int argc, char * argv[]) } n = op->count * 512; - inbuff = (uint8_t *)malloc(n); + inbuff = (uint8_t *)sg_memalign(n, 0, &free_inbuff, op->verbose > 3); if (!inbuff) { pr2serr("Cannot allocate output buffer of size %d\n", n); return SG_LIB_CAT_OTHER; @@ -439,21 +442,30 @@ main(int argc, char * argv[]) if ((sg_fd = sg_cmds_open_device(op->device_name, op->rdonly, op->verbose)) < 0) { - pr2serr("error opening file: %s: %s\n", op->device_name, - safe_strerror(-sg_fd)); - ret = SG_LIB_FILE_ERROR; + if (op->verbose) + pr2serr("error opening file: %s: %s\n", op->device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); goto fini; } ret = do_read_gplog(sg_fd, ata_cmd, inbuff, op); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - ret = SG_LIB_FILE_ERROR; - } fini: - free(inbuff); + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == op->verbose) { + if (! sg_if_can2stderr("sg_sat_read_gplog failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); + } + if (free_inbuff) + free(free_inbuff); return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_sat_set_features.c b/src/sg_sat_set_features.c index 523e38a4..dbcd8353 100644 --- a/src/sg_sat_set_features.c +++ b/src/sg_sat_set_features.c @@ -50,11 +50,12 @@ #define DEF_TIMEOUT 20 -static const char * version_str = "1.16 20180219"; +static const char * version_str = "1.17 20180513"; static struct option long_options[] = { {"count", required_argument, 0, 'c'}, {"ck_cond", no_argument, 0, 'C'}, + {"ck-cond", no_argument, 0, 'C'}, {"extended", no_argument, 0, 'e'}, {"feature", required_argument, 0, 'f'}, {"help", no_argument, 0, 'h'}, @@ -304,7 +305,8 @@ main(int argc, char * argv[]) bool ck_cond = false; bool extend = false; bool rdonly = false; - int sg_fd, c, ret, res; + int c, ret, res; + int sg_fd = -1; int count = 0; int feature = 0; int verbose = 0; @@ -409,19 +411,29 @@ main(int argc, char * argv[]) } if ((sg_fd = sg_cmds_open_device(device_name, rdonly, verbose)) < 0) { - pr2serr("error opening file: %s: %s\n", device_name, - safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("error opening file: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto fini; } ret = do_set_features(sg_fd, feature, count, lba, cdb_len, ck_cond, extend, verbose); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; +fini: + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_sat_set_feature failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_seek.c b/src/sg_seek.c index 55ef5f02..784770c7 100644 --- a/src/sg_seek.c +++ b/src/sg_seek.c @@ -47,7 +47,7 @@ * to that LBA ... */ -static const char * version_str = "1.03 20180304"; +static const char * version_str = "1.05 20180513"; #define BACKGROUND_CONTROL_SA 0x15 @@ -136,7 +136,8 @@ main(int argc, char * argv[]) bool prefetch = false; bool readonly = false; bool start_tm_valid = false; - int sg_fd, res, c; + int res, c; + int sg_fd = -1; int first_err = 0; int last_err = 0; int ret = 0; @@ -283,9 +284,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, readonly, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s %s\n", device_name, cdb_name, - safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s %s\n", device_name, cdb_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto fini; } #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) if (do_time) { @@ -375,11 +378,19 @@ main(int argc, char * argv[]) printf(" code: %d\n", last_err); } } - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; +fini: + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_seek failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_senddiag.c b/src/sg_senddiag.c index 98f3e23f..410e1409 100644 --- a/src/sg_senddiag.c +++ b/src/sg_senddiag.c @@ -670,7 +670,8 @@ list_page_codes() int main(int argc, char * argv[]) { - int sg_fd, k, num, rsp_len, res, rsp_buff_size, pg, bd_len, resid, vb; + int k, num, rsp_len, res, rsp_buff_size, pg, bd_len, resid, vb; + int sg_fd = -1; int read_in_len = 0; int ret = 0; struct opts_t opts; @@ -791,8 +792,9 @@ main(int argc, char * argv[]) if ((sg_fd = sg_cmds_open_device(op->device_name, false /* rw */, vb)) < 0) { - pr2serr(ME "error opening file: %s: %s\n", op->device_name, - safe_strerror(-sg_fd)); + if (vb) + pr2serr(ME "error opening file: %s: %s\n", op->device_name, + safe_strerror(-sg_fd)); ret = sg_convert_errno(-sg_fd); goto fini; } @@ -920,13 +922,20 @@ err_out9: if (vb < 2) pr2serr(" try again with '-vv' for more information\n"); close_fini: - res = sg_cmds_close_device(sg_fd); - if ((res < 0) && (0 == ret)) - ret = SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (0 == ret) + ret = sg_convert_errno(-res); + } fini: if (free_read_in) free(free_read_in); if (free_rsp_buff) free(free_rsp_buff); + if (0 == vb) { + if (! sg_if_can2stderr("sg_senddiag failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); + } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_ses.c b/src/sg_ses.c index 15d76295..cd75bf58 100644 --- a/src/sg_ses.c +++ b/src/sg_ses.c @@ -36,7 +36,7 @@ * commands tailored for SES (enclosure) devices. */ -static const char * version_str = "2.37 20180502"; /* ses4r02 */ +static const char * version_str = "2.38 20180511"; /* ses4r02 */ #define MX_ALLOC_LEN ((64 * 1024) - 4) /* max allowable for big enclosures */ #define MX_ELEM_HDR 1024 @@ -558,7 +558,7 @@ static struct acronym2tuple ecs_a2t_arr[] = { {"hotspare", ARRAY_DEV_ETC, 1, 5, 1, NULL}, {"hotswap", COOLING_ETC, 3, 7, 1, NULL}, {"hotswap", ENC_SCELECTR_ETC, 3, 7, 1, NULL}, /* status only */ - {"hw_reset", ENC_SCELECTR_ETC, 1, 2, 1, "hardware reset"}, /* 18-047r0 */ + {"hw_reset", ENC_SCELECTR_ETC, 1, 2, 1, "hardware reset"}, /* 18-047r1 */ {"ident", DEVICE_ETC, 2, 1, 1, "flash LED"}, {"ident", ARRAY_DEV_ETC, 2, 1, 1, "flash LED"}, {"ident", POWER_SUPPLY_ETC, 1, 7, 1, "flash LED"}, @@ -670,7 +670,7 @@ static struct acronym2tuple ecs_a2t_arr[] = { "0: leave; 1: lowest... 7: highest"}, {"size_mult", NV_CACHE_ETC, 1, 1, 2, NULL}, {"swap", -1, 0, 4, 1, NULL}, /* Reset swap */ - {"sw_reset", ENC_SCELECTR_ETC, 1, 3, 1, "software reset"},/* 18-047r0 */ + {"sw_reset", ENC_SCELECTR_ETC, 1, 3, 1, "software reset"},/* 18-047r1 */ {"temp", TEMPERATURE_ETC, 2, 7, 8, "(Requested) temperature"}, {"unlock", DOOR_ETC, 3, 0, 1, NULL}, {"undertemp_fail", TEMPERATURE_ETC, 3, 1, 1, "Undertemperature failure"}, @@ -5617,10 +5617,11 @@ main(int argc, char * argv[]) if (op->dev_name) { sg_fd = sg_cmds_open_device(op->dev_name, op->o_readonly, vb); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", op->dev_name, - safe_strerror(-sg_fd)); + if (vb) + pr2serr("open error: %s: %s\n", op->dev_name, + safe_strerror(-sg_fd)); ret = sg_convert_errno(-sg_fd); - goto err_out; + goto early_out; } ptvp = construct_scsi_pt_obj_with_fd(sg_fd, vb); if (NULL == ptvp) { @@ -5830,10 +5831,6 @@ err_out: sg_get_category_sense_str(ret, sizeof(b), b, vb); pr2serr(" %s\n", b); } - if (sg_fd >= 0) - res = sg_cmds_close_device(sg_fd); - else - res = 0; if (free_enc_stat_rsp) free(free_enc_stat_rsp); if (free_elem_desc_rsp) @@ -5842,12 +5839,16 @@ err_out: free(free_add_elem_rsp); if (free_threshold_rsp) free(free_threshold_rsp); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - ret = SG_LIB_FILE_ERROR; - } + early_out: + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } if (ptvp) destruct_scsi_pt_obj(ptvp); if ((0 == vb) && (! op->quiet)) { diff --git a/src/sg_ses_microcode.c b/src/sg_ses_microcode.c index f503180e..f8ffccd2 100644 --- a/src/sg_ses_microcode.c +++ b/src/sg_ses_microcode.c @@ -41,7 +41,7 @@ * RESULTS commands in order to send microcode to the given SES device. */ -static const char * version_str = "1.13 20180219"; /* ses4r02 */ +static const char * version_str = "1.14 20180513"; /* ses4r02 */ #define ME "sg_ses_microcode: " #define MAX_XFER_LEN (128 * 1024 * 1024) @@ -480,7 +480,8 @@ main(int argc, char * argv[]) { bool last, got_stdin, is_reg; bool want_file = false; - int sg_fd, res, c, len, k, n, rsp_len, resid, act_len, din_len, verb; + int res, c, len, k, n, rsp_len, resid, act_len, din_len, verb; + int sg_fd = -1; int infd = -1; int do_help = 0; int ret = 0; @@ -686,9 +687,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, false /* rw */, op->verbose); if (sg_fd < 0) { - pr2serr(ME "open error: %s: %s\n", device_name, - safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (op->verbose) + pr2serr(ME "open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto fini; } if (file_name && (! want_file)) @@ -896,21 +899,18 @@ fini: free(dout.free_doutp); if (free_dip) free(free_dip); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } } - if (ret && (0 == op->verbose)) { - if (SG_LIB_CAT_INVALID_OP == ret) - pr2serr("%sRECEIVE DIAGNOSTIC RESULTS command not supported\n", - ((MODE_DNLD_STATUS == op->mc_mode) ? - "" : "SEND DIAGNOSTIC or ")); - else if (ret > 0) - pr2serr("Failed, exit status %d\n", ret); - else if (ret < 0) - pr2serr("Some error occurred\n"); + if (0 == op->verbose) { + if (! sg_if_can2stderr("sg_ses_mocrocode failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_start.c b/src/sg_start.c index 8fee166e..28f22f20 100644 --- a/src/sg_start.c +++ b/src/sg_start.c @@ -34,7 +34,7 @@ #include "sg_pr2serr.h" -static const char * version_str = "0.64 20180302"; /* sbc3r14; mmc6r01a */ +static const char * version_str = "0.65 20180513"; /* sbc3r14; mmc6r01a */ static struct option long_options[] = { {"eject", no_argument, 0, 'e'}, @@ -71,7 +71,7 @@ struct opts_t { int do_help; int do_mod; int do_pc; - int do_verbose; + int verbose; const char * device_name; }; @@ -247,7 +247,7 @@ news_parse_cmd_line(struct opts_t * op, int argc, char * argv[]) op->do_stop = true; break; case 'v': - ++op->do_verbose; + ++op->verbose; break; case 'V': op->do_version = true; @@ -315,7 +315,7 @@ old_parse_cmd_line(struct opts_t * op, int argc, char * argv[]) op->do_readonly = true; break; case 'v': - ++op->do_verbose; + ++op->verbose; break; case 'V': op->do_version = true; @@ -482,7 +482,8 @@ parse_cmd_line(struct opts_t * op, int argc, char * argv[]) int main(int argc, char * argv[]) { - int fd, res; + int res; + int sg_fd = -1; int ret = 0; struct opts_t opts; struct opts_t * op; @@ -545,39 +546,49 @@ main(int argc, char * argv[]) } } - fd = sg_cmds_open_device(op->device_name, op->do_readonly, - op->do_verbose); - if (fd < 0) { - pr2serr("Error trying to open %s: %s\n", op->device_name, - safe_strerror(-fd)); - return SG_LIB_FILE_ERROR; + sg_fd = sg_cmds_open_device(op->device_name, op->do_readonly, + op->verbose); + if (sg_fd < 0) { + if (op->verbose) + pr2serr("Error trying to open %s: %s\n", op->device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto fini; } if (op->do_fl >= 0) - res = sg_ll_start_stop_unit(fd, op->do_immed, op->do_fl, 0 /* pc */, + res = sg_ll_start_stop_unit(sg_fd, op->do_immed, op->do_fl, 0 /* pc */, true /* fl */, true /* loej */, true /*start */, true /* noisy */, - op->do_verbose); + op->verbose); else if (op->do_pc > 0) - res = sg_ll_start_stop_unit(fd, op->do_immed, op->do_mod, + res = sg_ll_start_stop_unit(sg_fd, op->do_immed, op->do_mod, op->do_pc, op->do_noflush, false, false, - true, op->do_verbose); + true, op->verbose); else - res = sg_ll_start_stop_unit(fd, op->do_immed, 0, false, + res = sg_ll_start_stop_unit(sg_fd, op->do_immed, 0, false, op->do_noflush, op->do_loej, - op->do_start, true, op->do_verbose); + op->do_start, true, op->verbose); ret = res; if (res) { - if (op->do_verbose < 2) { + if (op->verbose < 2) { char b[80]; - sg_get_category_sense_str(res, sizeof(b), b, op->do_verbose); + sg_get_category_sense_str(res, sizeof(b), b, op->verbose); pr2serr("%s\n", b); } pr2serr("START STOP UNIT command failed\n"); } - res = sg_cmds_close_device(fd); - if ((res < 0) && (0 == ret)) - return SG_LIB_FILE_ERROR; +fini: + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (0 == ret) + ret = sg_convert_errno(-res); + } + if (0 == op->verbose) { + if (! sg_if_can2stderr("sg_start failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); + } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_stpg.c b/src/sg_stpg.c index b25339eb..c5e6b466 100644 --- a/src/sg_stpg.c +++ b/src/sg_stpg.c @@ -32,7 +32,7 @@ * to the given SCSI device. */ -static const char * version_str = "1.16 20180219"; +static const char * version_str = "1.17 20180513"; #define TGT_GRP_BUFF_LEN 1024 #define MX_ALLOC_LEN (0xc000 + 0x80) @@ -415,7 +415,8 @@ main(int argc, char * argv[]) { bool hex = false; bool raw = false; - int sg_fd, k, off, res, c, report_len, tgt_port_count; + int k, off, res, c, report_len, tgt_port_count; + int sg_fd = -1; int port_arr_len = 0; int verbose = 0; uint8_t reportTgtGrpBuff[TGT_GRP_BUFF_LEN]; @@ -561,8 +562,11 @@ main(int argc, char * argv[]) } sg_fd = sg_cmds_open_device(device_name, false /* rw */, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto err_out; } if (0 == port_arr_len) { @@ -683,11 +687,18 @@ main(int argc, char * argv[]) } err_out: - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_stpg failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_stream_ctl.c b/src/sg_stream_ctl.c index f2372ed7..794bf254 100644 --- a/src/sg_stream_ctl.c +++ b/src/sg_stream_ctl.c @@ -33,7 +33,7 @@ * to the given SCSI device. Based on sbc4r15.pdf . */ -static const char * version_str = "1.03 20180427"; +static const char * version_str = "1.04 20180513"; #define STREAM_CONTROL_SA 0x14 #define GET_STREAM_STATUS_SA 0x16 @@ -242,7 +242,8 @@ main(int argc, char * argv[]) bool ctl_given = false; bool maxlen_given = false; bool read_only = false; - int c, k, sg_fd, res, resid; + int c, k, res, resid; + int sg_fd = -1; int maxlen = 0; int ret = 0; int verbose = 0; @@ -253,8 +254,8 @@ main(int argc, char * argv[]) uint32_t param_dl; const char * device_name = NULL; const char * cmd_name = NULL; - uint8_t * arr; - uint8_t * free_arr; + uint8_t * arr = NULL; + uint8_t * free_arr = NULL; while (1) { int option_index = 0; @@ -367,9 +368,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, read_only, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", device_name, - safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto fini; } if (maxlen > (int)pg_sz) @@ -464,11 +467,18 @@ main(int argc, char * argv[]) fini: if (free_arr) free(free_arr); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_stream_ctl failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_sync.c b/src/sg_sync.c index 59cf8276..23a26d5b 100644 --- a/src/sg_sync.c +++ b/src/sg_sync.c @@ -35,7 +35,7 @@ * (e.g. disks). */ -static const char * version_str = "1.20 20180219"; +static const char * version_str = "1.21 20180514"; #define SYNCHRONIZE_CACHE16_CMD 0x91 #define SYNCHRONIZE_CACHE16_CMDLEN 16 @@ -152,7 +152,8 @@ int main(int argc, char * argv[]) bool do_16 = false; bool immed = false; bool sync_nv = false; - int sg_fd, res, c; + int res, c; + int sg_fd = -1; int group = 0; int ret = 0; int to_secs = DEF_PT_TIMEOUT; @@ -245,9 +246,11 @@ int main(int argc, char * argv[]) } sg_fd = sg_cmds_open_device(device_name, false /* rw */, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", device_name, - safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto fini; } if (do_16) @@ -264,11 +267,19 @@ int main(int argc, char * argv[]) pr2serr("Synchronize cache failed: %s\n", b); } - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; +fini: + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_sync failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_timestamp.c b/src/sg_timestamp.c index 976bca56..2049052c 100644 --- a/src/sg_timestamp.c +++ b/src/sg_timestamp.c @@ -316,7 +316,8 @@ main(int argc, char * argv[]) bool no_timestamp = false; bool readonly = false; bool secs_given = false; - int sg_fd, res, c; + int res, c; + int sg_fd = 1; int elapsed = 0; int do_origin = 0; int do_help = 0; @@ -427,9 +428,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, readonly, verbose); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", device_name, - safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto fini; } memset(d_buff, 0, 12); @@ -495,11 +498,19 @@ main(int argc, char * argv[]) } } - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; +fini: + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_timestamp failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_unmap.c b/src/sg_unmap.c index 7b1bf424..32295e0a 100644 --- a/src/sg_unmap.c +++ b/src/sg_unmap.c @@ -44,7 +44,7 @@ * logical blocks. Note that DATA MAY BE LOST. */ -static const char * version_str = "1.14 20180330"; +static const char * version_str = "1.15 20180514"; #define DEF_TIMEOUT_SECS 60 @@ -771,18 +771,18 @@ retry: } err_out: - if ((0 == vb) && (! err_printed)) { - if (! sg_if_can2stderr("sg_unmap failed: ", ret)) - pr2serr("Some error occurred, try again with '-v' or '-vv' for " - "more information\n"); - } if (sg_fd >= 0) { res = sg_cmds_close_device(sg_fd); if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) - return SG_LIB_FILE_ERROR; + ret = sg_convert_errno(-res); } } + if ((0 == vb) && (! err_printed)) { + if (! sg_if_can2stderr("sg_unmap failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' or '-vv' for " + "more information\n"); + } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_verify.c b/src/sg_verify.c index 5defc175..34954d65 100644 --- a/src/sg_verify.c +++ b/src/sg_verify.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004-2017 Douglas Gilbert. + * Copyright (c) 2004-2018 Douglas Gilbert. * All rights reserved. * Use of this source code is governed by a BSD-style * license that can be found in the BSD_LICENSE file. @@ -11,6 +11,7 @@ #include <stdlib.h> #include <stdarg.h> #include <stdbool.h> +#include <errno.h> #include <string.h> #include <getopt.h> #define __STDC_FORMAT_MACROS 1 @@ -36,7 +37,7 @@ * the possibility of protection data (DIF). */ -static const char * version_str = "1.23 20171012"; /* sbc4r01 */ +static const char * version_str = "1.23 20180515"; /* sbc4r01 */ #define ME "sg_verify: " @@ -131,7 +132,8 @@ main(int argc, char * argv[]) bool quiet = false; bool readonly = false; bool verify16 = false; - int sg_fd, res, c, num, nread, infd; + int res, c, num, nread, infd; + int sg_fd = -1; int bpc = 128; int group = 0; int bytchk = 0; @@ -146,7 +148,8 @@ main(int argc, char * argv[]) uint64_t info64 = 0; uint64_t lba = 0; uint64_t orig_lba; - char *ref_data = NULL; + uint8_t * ref_data = NULL; + uint8_t * free_ref_data = NULL; const char * device_name = NULL; const char * file_name = NULL; const char * vc; @@ -299,10 +302,11 @@ main(int argc, char * argv[]) orig_lba = lba; if (ndo > 0) { - ref_data = (char *)malloc(ndo); + ref_data = (uint8_t *)sg_memalign(ndo, 0, &free_ref_data, verbose > 4); if (NULL == ref_data) { pr2serr("failed to allocate %d byte buffer\n", ndo); - return SG_LIB_FILE_ERROR; + ret = sg_convert_errno(ENOMEM); + goto err_out; } if ((NULL == file_name) || (0 == strcmp(file_name, "-"))) { got_stdin = true; @@ -342,8 +346,10 @@ main(int argc, char * argv[]) } sg_fd = sg_cmds_open_device(device_name, readonly, verbose); if (sg_fd < 0) { - pr2serr(ME "open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - ret = SG_LIB_FILE_ERROR; + if (verbose) + pr2serr(ME "open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); goto err_out; } @@ -399,15 +405,21 @@ main(int argc, char * argv[]) " [0x%" PRIx64 "]\n without error\n", orig_count, (uint64_t)orig_count, orig_lba, orig_lba); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - ret = SG_LIB_FILE_ERROR; - } - err_out: - if (ref_data) - free(ref_data); + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (free_ref_data) + free(free_ref_data); + if (0 == verbose) { + if (! sg_if_can2stderr("sg_verify failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); + } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_wr_mode.c b/src/sg_wr_mode.c index c6ebf13a..425b3a41 100644 --- a/src/sg_wr_mode.c +++ b/src/sg_wr_mode.c @@ -320,8 +320,9 @@ int main(int argc, char * argv[]) bool mode_6 = false; /* so default is mode_10 */ bool rtd = false; /* added in spc5r11 */ bool save = false; - int sg_fd, res, c, num, alloc_len, off, pdt; - int k, md_len, hdr_len, bd_len, mask_in_len; + int res, c, num, alloc_len, off, pdt, k, md_len, hdr_len, bd_len; + int mask_in_len; + int sg_fd = -1; int pg_code = -1; int sub_pg_code = 0; int verbose = 0; @@ -457,8 +458,11 @@ int main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, false /* rw */, verbose); if (sg_fd < 0) { - pr2serr(ME "open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr(ME "open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto fini; } if (rtd) goto revert_to_defaults; @@ -595,11 +599,13 @@ revert_to_defaults: save, NULL, 0, true, verbose); ret = res; fini: - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - ret = SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = SG_LIB_FILE_ERROR; + } } if (0 == verbose) { if (! sg_if_can2stderr("sg_wr_mode failed: ", ret)) diff --git a/src/sg_write_buffer.c b/src/sg_write_buffer.c index e50d7a4d..4591c1a4 100644 --- a/src/sg_write_buffer.c +++ b/src/sg_write_buffer.c @@ -13,6 +13,7 @@ #include <stdint.h> #include <stdbool.h> #include <ctype.h> +#include <errno.h> #include <string.h> #include <getopt.h> #define __STDC_FORMAT_MACROS 1 @@ -37,7 +38,7 @@ * This utility issues the SCSI WRITE BUFFER command to the given device. */ -static const char * version_str = "1.25 20180217"; /* spc5r19 */ +static const char * version_str = "1.26 20180515"; /* spc5r19 */ #define ME "sg_write_buffer: " #define DEF_XFER_LEN (8 * 1024 * 1024) @@ -193,7 +194,8 @@ main(int argc, char * argv[]) bool dry_run = false; bool got_stdin = false; bool wb_len_given = false; - int sg_fd, infd, res, c, len, k, n; + int infd, res, c, len, k, n; + int sg_fd = -1; int bpw = 0; int do_help = 0; int ret = 0; @@ -369,9 +371,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, false /* rw */, verbose); if (sg_fd < 0) { - pr2serr(ME "open error: %s: %s\n", device_name, - safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr(ME "open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto err_out; } if (file_name || (wb_len > 0)) { if (0 == wb_len) @@ -379,7 +383,7 @@ main(int argc, char * argv[]) dop = sg_memalign(wb_len, 0, &free_dop, false); if (NULL == dop) { pr2serr(ME "out of memory\n"); - ret = SG_LIB_SYNTAX_ERROR; + ret = sg_convert_errno(ENOMEM); goto err_out; } memset(dop, 0xff, wb_len); @@ -509,11 +513,18 @@ main(int argc, char * argv[]) err_out: if (free_dop) free(free_dop); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_write_buffer failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_write_long.c b/src/sg_write_long.c index b6f9e694..0080343b 100644 --- a/src/sg_write_long.c +++ b/src/sg_write_long.c @@ -35,7 +35,7 @@ #include "sg_cmds_extra.h" #include "sg_pr2serr.h" -static const char * version_str = "1.17 20180502"; +static const char * version_str = "1.18 20180515"; #define MAX_XFER_LEN (15 * 1024) @@ -105,7 +105,8 @@ main(int argc, char * argv[]) bool got_stdin; bool pblock = false; bool wr_uncor = false; - int sg_fd, res, c, infd, offset; + int res, c, infd, offset; + int sg_fd = -1; int xfer_len = 520; int ret = 1; int verbose = 0; @@ -204,8 +205,11 @@ main(int argc, char * argv[]) } sg_fd = sg_cmds_open_device(device_name, false /* rw */, verbose); if (sg_fd < 0) { - pr2serr(ME "open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (verbose) + pr2serr(ME "open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto err_out; } if (wr_uncor) { @@ -285,11 +289,18 @@ main(int argc, char * argv[]) err_out: if (free_rawp) free(free_rawp); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == verbose) { + if (! sg_if_can2stderr("sg_write_long failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_write_same.c b/src/sg_write_same.c index 2d6d241a..a8060de0 100644 --- a/src/sg_write_same.c +++ b/src/sg_write_same.c @@ -31,7 +31,7 @@ #include "sg_unaligned.h" #include "sg_pr2serr.h" -static const char * version_str = "1.22 20180425"; +static const char * version_str = "1.23 20180515"; #define ME "sg_write_same: " @@ -150,7 +150,8 @@ usage() "specified blocks\nwill be filled with zeros or the " "'provisioning initialization pattern'\nas indicated by the " "LBPRZ field. As a precaution one of the '--in=',\n'--lba=' or " - "'--num=' options is required.\n" + "'--num=' options is required.\nAnother implementation of WRITE " + "SAME is found in the sg_write_x utility.\n" ); } @@ -310,7 +311,8 @@ main(int argc, char * argv[]) bool lba_given = false; bool num_given = false; bool prot_en; - int sg_fd, res, c, infd, act_cdb_len, vb; + int res, c, infd, act_cdb_len, vb; + int sg_fd = -1; int ret = -1; uint32_t block_size; int64_t ll; @@ -491,8 +493,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, false /* rw */, vb); if (sg_fd < 0) { - pr2serr(ME "open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); - return SG_LIB_FILE_ERROR; + if (op->verbose) + pr2serr(ME "open error: %s: %s\n", device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto err_out; } if (! op->ndob) { @@ -613,11 +618,18 @@ main(int argc, char * argv[]) err_out: if (free_wBuff) free(free_wBuff); - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - return SG_LIB_FILE_ERROR; + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } + if (0 == op->verbose) { + if (! sg_if_can2stderr("sg_write_same failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' " + "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_write_x.c b/src/sg_write_x.c index 3311ad0d..6467319b 100644 --- a/src/sg_write_x.c +++ b/src/sg_write_x.c @@ -36,7 +36,7 @@ #include "sg_unaligned.h" #include "sg_pr2serr.h" -static const char * version_str = "1.16 20180425"; +static const char * version_str = "1.17 20180515"; /* Protection Information refers to 8 bytes of extra information usually * associated with each logical block and is often abbreviated to PI while @@ -2370,9 +2370,11 @@ main(int argc, char * argv[]) /* Open device file, do READ CAPACITY(16, maybe 10) if no BS */ sg_fd = sg_cmds_open_device(op->device_name, false /* rw */, vb); if (sg_fd < 0) { - pr2serr("open error: %s: %s\n", op->device_name, - safe_strerror(-sg_fd)); - goto file_err_out; + if (op->verbose) + pr2serr("open error: %s: %s\n", op->device_name, + safe_strerror(-sg_fd)); + ret = sg_convert_errno(-sg_fd); + goto fini; } if (0 == op->bs) { /* ask DEVICE about logical/actual block size */ ret = do_read_capacity(sg_fd, op); diff --git a/src/sg_xcopy.c b/src/sg_xcopy.c index c114e860..08602809 100644 --- a/src/sg_xcopy.c +++ b/src/sg_xcopy.c @@ -338,13 +338,13 @@ open_sg(struct xcopy_fp_t * fp, int vb) fp->sg_type & FT_BLOCK ? "block" : "char", devmajor, devminor); perror(ebuff); - return -1; + return -sg_convert_errno(-fp->sg_fd); } if (sg_simple_inquiry(fp->sg_fd, &sir, false, vb)) { pr2serr("INQUIRY failed on %s\n", ebuff); sg_cmds_close_device(fp->sg_fd); fp->sg_fd = -1; - return -2; + return -1; } fp->pdt = sir.peripheral_type; @@ -1716,22 +1716,25 @@ main(int argc, char * argv[]) pr2serr("Unit attention (%s), continuing\n", rec_copy_op_params_str); res = scsi_operating_parameter(&ixcf, 0); - } else { - if (-res == SG_LIB_CAT_INVALID_OP) { - pr2serr("%s command not supported on %s\n", - rec_copy_op_params_str, ixcf.fname); - return EINVAL; - } else if (-res == SG_LIB_CAT_NOT_READY) - pr2serr("%s failed on %s - not ready\n", - rec_copy_op_params_str, ixcf.fname); - else { - pr2serr("Unable to %s on %s\n", rec_copy_op_params_str, - ixcf.fname); - return -res; - } } - } else if (res == 0) - return SG_LIB_CAT_INVALID_OP; + if (-res == SG_LIB_CAT_INVALID_OP) { + pr2serr("%s command not supported on %s\n", + rec_copy_op_params_str, ixcf.fname); + ret = sg_convert_errno(EINVAL); + goto fini; + } else if (-res == SG_LIB_CAT_NOT_READY) + pr2serr("%s failed on %s - not ready\n", + rec_copy_op_params_str, ixcf.fname); + else { + pr2serr("Unable to %s on %s\n", rec_copy_op_params_str, + ixcf.fname); + ret = -res; + goto fini; + } + } else if (res == 0) { + ret = SG_LIB_CAT_INVALID_OP; + goto fini; + } if (res & TD_VPD) { if (verbose) @@ -1741,10 +1744,12 @@ main(int argc, char * argv[]) sizeof(src_desc), ixcf.sect_sz, ixcf.pad); if (src_desc_len > (int)sizeof(src_desc)) { pr2serr("source descriptor too large (%d bytes)\n", res); - return SG_LIB_CAT_MALFORMED; + ret = SG_LIB_CAT_MALFORMED; + goto fini; } } else { - return SG_LIB_CAT_INVALID_OP; + ret = SG_LIB_CAT_INVALID_OP; + goto fini; } res = scsi_operating_parameter(&oxcf, 1); @@ -1753,22 +1758,25 @@ main(int argc, char * argv[]) pr2serr("Unit attention (%s), continuing\n", rec_copy_op_params_str); res = scsi_operating_parameter(&oxcf, 1); - } else { - if (-res == SG_LIB_CAT_INVALID_OP) { - pr2serr("%s command not supported on %s\n", - rec_copy_op_params_str, oxcf.fname); - return EINVAL; - } else if (-res == SG_LIB_CAT_NOT_READY) - pr2serr("%s failed on %s - not ready\n", - rec_copy_op_params_str, oxcf.fname); - else { - pr2serr("Unable to %s on %s\n", rec_copy_op_params_str, - oxcf.fname); - return -res; - } } - } else if (res == 0) - return SG_LIB_CAT_INVALID_OP; + if (-res == SG_LIB_CAT_INVALID_OP) { + pr2serr("%s command not supported on %s\n", + rec_copy_op_params_str, oxcf.fname); + ret = sg_convert_errno(EINVAL); + goto fini; + } else if (-res == SG_LIB_CAT_NOT_READY) + pr2serr("%s failed on %s - not ready\n", + rec_copy_op_params_str, oxcf.fname); + else { + pr2serr("Unable to %s on %s\n", rec_copy_op_params_str, + oxcf.fname); + ret = -res; + goto fini; + } + } else if (res == 0) { + ret = SG_LIB_CAT_INVALID_OP; + goto fini; + } if (res & TD_VPD) { if (verbose) @@ -1778,10 +1786,12 @@ main(int argc, char * argv[]) sizeof(dst_desc), oxcf.sect_sz, oxcf.pad); if (dst_desc_len > (int)sizeof(dst_desc)) { pr2serr("destination descriptor too large (%d bytes)\n", res); - return SG_LIB_CAT_MALFORMED; + ret = SG_LIB_CAT_MALFORMED; + goto fini; } } else { - return SG_LIB_CAT_INVALID_OP; + ret = SG_LIB_CAT_INVALID_OP; + goto fini; } if (dd_count < 0) { @@ -1865,6 +1875,14 @@ main(int argc, char * argv[]) else pr2serr("sg_xcopy: %" PRId64 " blocks, %d command%s\n", in_full, num_xcopy, ((num_xcopy > 1) ? "s" : "")); + ret = res; - return res; +fini: + /* file handles not explicity closed; let process cleanup do that */ + if (0 == verbose) { + if (! sg_if_can2stderr("sg_xcopy failed: ", ret)) + pr2serr("Some error occurred, try again with '-v' or '-vv' for " + "more information\n"); + } + return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } diff --git a/src/sg_zone.c b/src/sg_zone.c index 7b2c616a..77c409c0 100644 --- a/src/sg_zone.c +++ b/src/sg_zone.c @@ -35,7 +35,7 @@ * to the given SCSI device. Based on zbc-r04c.pdf . */ -static const char * version_str = "1.08 20180504"; +static const char * version_str = "1.09 20180515"; #define SG_ZONING_OUT_CMDLEN 16 #define CLOSE_ZONE_SA 0x1 @@ -169,7 +169,8 @@ main(int argc, char * argv[]) bool finish = false; bool open = false; bool sequentialize = false; - int sg_fd, res, c, n; + int res, c, n; + int sg_fd = -1; int verbose = 0; int ret = 0; int sa = 0; @@ -272,9 +273,11 @@ main(int argc, char * argv[]) sg_fd = sg_cmds_open_device(device_name, false /* rw */, verbose); if (sg_fd < 0) { int err = -sg_fd; - pr2serr("open error: %s: %s\n", device_name, - safe_strerror(err)); - return sg_convert_errno(err); + if (verbose) + pr2serr("open error: %s: %s\n", device_name, + safe_strerror(err)); + ret = sg_convert_errno(err); + goto fini; } res = sg_ll_zone_out(sg_fd, sa, zid, zc, all, true, verbose); @@ -290,16 +293,19 @@ main(int argc, char * argv[]) } } +fini: + if (sg_fd >= 0) { + res = sg_cmds_close_device(sg_fd); + if (res < 0) { + pr2serr("close error: %s\n", safe_strerror(-res)); + if (0 == ret) + ret = sg_convert_errno(-res); + } + } if (0 == verbose) { if (! sg_if_can2stderr("sg_zone failed: ", ret)) pr2serr("Some error occurred, try again with '-v' or '-vv' for " "more information\n"); } - res = sg_cmds_close_device(sg_fd); - if (res < 0) { - pr2serr("close error: %s\n", safe_strerror(-res)); - if (0 == ret) - ret = sg_convert_errno(-res); - } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; } |