aboutsummaryrefslogtreecommitdiff
path: root/lib/sg_pt_linux.c
diff options
context:
space:
mode:
authorDouglas Gilbert <dgilbert@interlog.com>2015-05-12 04:21:05 +0000
committerDouglas Gilbert <dgilbert@interlog.com>2015-05-12 04:21:05 +0000
commit77c837a07783a4e63b7f4a85852f55a112b994e0 (patch)
tree59ea0b252047bccea1e04c5ceeed3ccec04465c5 /lib/sg_pt_linux.c
parentf54b2fbd8e1cc79cf1bafe9516a4771f88be29a7 (diff)
downloadsg3_utils-77c837a07783a4e63b7f4a85852f55a112b994e0.tar.gz
sg_lib: Linux: ENXIO errno --> SG_LIB_CAT_NOT_READY; asc/ascq codes --> T10 20150423
git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@644 6180dd3e-e324-4e3e-922d-17de1ae2f315
Diffstat (limited to 'lib/sg_pt_linux.c')
-rw-r--r--lib/sg_pt_linux.c90
1 files changed, 49 insertions, 41 deletions
diff --git a/lib/sg_pt_linux.c b/lib/sg_pt_linux.c
index bcb58712..1cfaeab3 100644
--- a/lib/sg_pt_linux.c
+++ b/lib/sg_pt_linux.c
@@ -1,15 +1,16 @@
/*
- * Copyright (c) 2005-2014 Douglas Gilbert.
+ * Copyright (c) 2005-2015 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.
*/
-/* sg_pt_linux version 1.23 20141224 */
+/* sg_pt_linux version 1.24 20150511 */
#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
@@ -81,6 +82,26 @@ static const char * linux_driver_suggests[] = {
#define SG_LIB_DRIVER_SENSE DRIVER_SENSE
+#ifdef __GNUC__
+static int pr2ws(const char * fmt, ...)
+ __attribute__ ((format (printf, 1, 2)));
+#else
+static int pr2ws(const char * fmt, ...);
+#endif
+
+
+static int
+pr2ws(const char * fmt, ...)
+{
+ va_list args;
+ int n;
+
+ va_start(args, fmt);
+ n = vfprintf(sg_warnings_strm ? sg_warnings_strm : stderr, fmt, args);
+ va_end(args);
+ return n;
+}
+
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#if defined(IGNORE_LINUX_BSG) || ! defined(HAVE_LINUX_BSG_H)
@@ -122,10 +143,7 @@ scsi_pt_open_flags(const char * device_name, int flags, int verbose)
int fd;
if (verbose > 1) {
- if (NULL == sg_warnings_strm)
- sg_warnings_strm = stderr;
- fprintf(sg_warnings_strm, "open %s with flags=0x%x\n", device_name,
- flags);
+ pr2ws("open %s with flags=0x%x\n", device_name, flags);
}
fd = open(device_name, flags);
if (fd < 0)
@@ -151,6 +169,12 @@ construct_scsi_pt_obj()
{
struct sg_pt_linux_scsi * ptp;
+ /* The following 2 lines are temporary. It is to avoid a NULL pointer
+ * crash when an old utility is used with a newer library built after
+ * the sg_warnings_strm cleanup */
+ if (NULL == sg_warnings_strm)
+ sg_warnings_strm = stderr;
+
ptp = (struct sg_pt_linux_scsi *)
calloc(1, sizeof(struct sg_pt_linux_scsi));
if (ptp) {
@@ -306,18 +330,15 @@ do_scsi_pt(struct sg_pt_base * vp, int fd, int time_secs, int verbose)
{
struct sg_pt_linux_scsi * ptp = &vp->impl;
- if (NULL == sg_warnings_strm)
- sg_warnings_strm = stderr;
ptp->os_err = 0;
if (ptp->in_err) {
if (verbose)
- fprintf(sg_warnings_strm, "Replicated or unused set_scsi_pt... "
- "functions\n");
+ pr2ws("Replicated or unused set_scsi_pt... functions\n");
return SCSI_PT_DO_BAD_PARAMS;
}
if (NULL == ptp->io_hdr.cmdp) {
if (verbose)
- fprintf(sg_warnings_strm, "No SCSI command (cdb) given\n");
+ pr2ws("No SCSI command (cdb) given\n");
return SCSI_PT_DO_BAD_PARAMS;
}
/* io_hdr.timeout is in milliseconds */
@@ -328,8 +349,8 @@ do_scsi_pt(struct sg_pt_base * vp, int fd, int time_secs, int verbose)
if (ioctl(fd, SG_IO, &ptp->io_hdr) < 0) {
ptp->os_err = errno;
if (verbose > 1)
- fprintf(sg_warnings_strm, "ioctl(SG_IO) failed: %s (errno=%d)\n",
- strerror(ptp->os_err), ptp->os_err);
+ pr2ws("ioctl(SG_IO) failed: %s (errno=%d)\n",
+ strerror(ptp->os_err), ptp->os_err);
return -ptp->os_err;
}
return 0;
@@ -531,11 +552,8 @@ find_bsg_major(int verbose)
int n;
if (NULL == (fp = fopen(proc_devices, "r"))) {
- if (NULL == sg_warnings_strm)
- sg_warnings_strm = stderr;
if (verbose)
- fprintf(sg_warnings_strm, "fopen %s failed: %s\n", proc_devices,
- strerror(errno));
+ pr2ws("fopen %s failed: %s\n", proc_devices, strerror(errno));
return;
}
while ((cp = fgets(b, sizeof(b), fp))) {
@@ -553,13 +571,10 @@ find_bsg_major(int verbose)
break;
}
if (verbose > 3) {
- if (NULL == sg_warnings_strm)
- sg_warnings_strm = stderr;
if (cp)
- fprintf(sg_warnings_strm, "found bsg_major=%d\n", bsg_major);
+ pr2ws("found bsg_major=%d\n", bsg_major);
else
- fprintf(sg_warnings_strm, "found no bsg char device in %s\n",
- proc_devices);
+ pr2ws("found no bsg char device in %s\n", proc_devices);
}
fclose(fp);
}
@@ -587,12 +602,8 @@ scsi_pt_open_flags(const char * device_name, int flags, int verbose)
bsg_major_checked = 1;
find_bsg_major(verbose);
}
- if (verbose > 1) {
- if (NULL == sg_warnings_strm)
- sg_warnings_strm = stderr;
- fprintf(sg_warnings_strm, "open %s with flags=0x%x\n", device_name,
- flags);
- }
+ if (verbose > 1)
+ pr2ws("open %s with flags=0x%x\n", device_name, flags);
fd = open(device_name, flags);
if (fd < 0)
fd = -errno;
@@ -929,7 +940,7 @@ do_scsi_pt_v3(struct sg_pt_linux_scsi * ptp, int fd, int time_secs,
if (ptp->io_hdr.din_xfer_len > 0) {
if (ptp->io_hdr.dout_xfer_len > 0) {
if (verbose)
- fprintf(sg_warnings_strm, "sgv3 doesn't support bidi\n");
+ pr2ws("sgv3 doesn't support bidi\n");
return SCSI_PT_DO_BAD_PARAMS;
}
v3_hdr.dxferp = (void *)(long)ptp->io_hdr.din_xferp;
@@ -952,7 +963,7 @@ do_scsi_pt_v3(struct sg_pt_linux_scsi * ptp, int fd, int time_secs,
if (NULL == v3_hdr.cmdp) {
if (verbose)
- fprintf(sg_warnings_strm, "No SCSI command (cdb) given\n");
+ pr2ws("No SCSI command (cdb) given\n");
return SCSI_PT_DO_BAD_PARAMS;
}
/* io_hdr.timeout is in milliseconds, if greater than zero */
@@ -961,8 +972,8 @@ do_scsi_pt_v3(struct sg_pt_linux_scsi * ptp, int fd, int time_secs,
if (ioctl(fd, SG_IO, &v3_hdr) < 0) {
ptp->os_err = errno;
if (verbose > 1)
- fprintf(sg_warnings_strm, "ioctl(SG_IO v3) failed: %s "
- "(errno=%d)\n", strerror(ptp->os_err), ptp->os_err);
+ pr2ws("ioctl(SG_IO v3) failed: %s (errno=%d)\n",
+ strerror(ptp->os_err), ptp->os_err);
return -ptp->os_err;
}
ptp->io_hdr.device_status = (__u32)v3_hdr.status;
@@ -987,13 +998,10 @@ do_scsi_pt(struct sg_pt_base * vp, int fd, int time_secs, int verbose)
bsg_major_checked = 1;
find_bsg_major(verbose);
}
- if (NULL == sg_warnings_strm)
- sg_warnings_strm = stderr;
ptp->os_err = 0;
if (ptp->in_err) {
if (verbose)
- fprintf(sg_warnings_strm, "Replicated or unused set_scsi_pt... "
- "functions\n");
+ pr2ws("Replicated or unused set_scsi_pt... functions\n");
return SCSI_PT_DO_BAD_PARAMS;
}
if (bsg_major <= 0)
@@ -1004,8 +1012,8 @@ do_scsi_pt(struct sg_pt_base * vp, int fd, int time_secs, int verbose)
if (fstat(fd, &a_stat) < 0) {
ptp->os_err = errno;
if (verbose > 1)
- fprintf(sg_warnings_strm, "fstat() failed: %s (errno=%d)\n",
- strerror(ptp->os_err), ptp->os_err);
+ pr2ws("fstat() failed: %s (errno=%d)\n",
+ strerror(ptp->os_err), ptp->os_err);
return -ptp->os_err;
}
if (! S_ISCHR(a_stat.st_mode) ||
@@ -1015,7 +1023,7 @@ do_scsi_pt(struct sg_pt_base * vp, int fd, int time_secs, int verbose)
if (! ptp->io_hdr.request) {
if (verbose)
- fprintf(sg_warnings_strm, "No SCSI command (cdb) given (v4)\n");
+ pr2ws("No SCSI command (cdb) given (v4)\n");
return SCSI_PT_DO_BAD_PARAMS;
}
/* io_hdr.timeout is in milliseconds */
@@ -1033,8 +1041,8 @@ do_scsi_pt(struct sg_pt_base * vp, int fd, int time_secs, int verbose)
if (ioctl(fd, SG_IO, &ptp->io_hdr) < 0) {
ptp->os_err = errno;
if (verbose > 1)
- fprintf(sg_warnings_strm, "ioctl(SG_IO v4) failed: %s "
- "(errno=%d)\n", strerror(ptp->os_err), ptp->os_err);
+ pr2ws("ioctl(SG_IO v4) failed: %s (errno=%d)\n",
+ strerror(ptp->os_err), ptp->os_err);
return -ptp->os_err;
}
return 0;