[Midnightbsd-cvs] src [10880] trunk/lib/libcam: tag
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Wed Jun 13 19:59:34 EDT 2018
Revision: 10880
http://svnweb.midnightbsd.org/src/?rev=10880
Author: laffer1
Date: 2018-06-13 19:59:33 -0400 (Wed, 13 Jun 2018)
Log Message:
-----------
tag
Modified Paths:
--------------
trunk/lib/libcam/Makefile
trunk/lib/libcam/cam.3
trunk/lib/libcam/cam_cdbparse.3
trunk/lib/libcam/camlib.c
trunk/lib/libcam/camlib.h
trunk/lib/libcam/scsi_cmdparse.c
Added Paths:
-----------
trunk/lib/libcam/tests/
trunk/lib/libcam/tests/Makefile
trunk/lib/libcam/tests/libcam_test.c
Property Changed:
----------------
trunk/lib/libcam/cam.3
trunk/lib/libcam/cam_cdbparse.3
Modified: trunk/lib/libcam/Makefile
===================================================================
--- trunk/lib/libcam/Makefile 2018-06-13 23:57:57 UTC (rev 10879)
+++ trunk/lib/libcam/Makefile 2018-06-13 23:59:33 UTC (rev 10880)
@@ -1,4 +1,5 @@
-# $FreeBSD$
+# $MidnightBSD$
+# $FreeBSD: stable/10/lib/libcam/Makefile 315696 2017-03-22 01:11:13Z ngie $
LIB= cam
SHLIBDIR?= /lib
@@ -36,12 +37,17 @@
cam_cdbparse.3 csio_encode_visit.3 \
cam_cdbparse.3 buff_encode_visit.3
-.PATH: ${.CURDIR}/../../sys/cam/scsi ${.CURDIR}/../../sys/cam/ata \
- ${.CURDIR}/../../sys/cam
+.PATH: ${SRCTOP}/sys/cam/scsi ${SRCTOP}/sys/cam/ata \
+ ${SRCTOP}/sys/cam
-SDIR= ${.CURDIR}/../../sys
-CFLAGS+= -I${.CURDIR} -I${SDIR}
+CFLAGS+= -I${.CURDIR} -I${SRCTOP}/sys
SHLIB_MAJOR= 6
+.include <bsd.own.mk>
+
+.if ${MK_TESTS} != "no"
+SUBDIR+= tests
+.endif
+
.include <bsd.lib.mk>
Modified: trunk/lib/libcam/cam.3
===================================================================
--- trunk/lib/libcam/cam.3 2018-06-13 23:57:57 UTC (rev 10879)
+++ trunk/lib/libcam/cam.3 2018-06-13 23:59:33 UTC (rev 10880)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
.\"
.\" Copyright (c) 1998 Kenneth D. Merry.
.\" All rights reserved.
@@ -25,9 +26,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" $MidnightBSD$
+.\" $FreeBSD: stable/10/lib/libcam/cam.3 315606 2017-03-20 03:00:22Z ngie $
.\"
-.Dd October 10, 1998
+.Dd March 11, 2017
.Dt CAM 3
.Os
.Sh NAME
@@ -132,7 +133,7 @@
structure:
.Bd -literal
struct cam_device {
- char device_path[MAXPATHLEN+1];/*
+ char device_path[MAXPATHLEN+1];/*
* Pathname of the
* device given by the
* user. This may be
@@ -145,34 +146,34 @@
* Device name given by
* the user.
*/
- u_int32_t given_unit_number; /*
+ uint32_t given_unit_number; /*
* Unit number given by
* the user.
*/
char device_name[DEV_IDLEN+1];/*
* Name of the device,
- * e.g. 'pass'
+ * e.g., 'pass'
*/
- u_int32_t dev_unit_num; /* Unit number of the passthrough
+ uint32_t dev_unit_num; /* Unit number of the passthrough
* device associated with this
* particular device.
*/
char sim_name[SIM_IDLEN+1];/*
- * Controller name, e.g.'ahc'
+ * Controller name, e.g., 'ahc'
*/
- u_int32_t sim_unit_number; /* Controller unit number */
- u_int32_t bus_id; /* Controller bus number */
+ uint32_t sim_unit_number; /* Controller unit number */
+ uint32_t bus_id; /* Controller bus number */
lun_id_t target_lun; /* Logical Unit Number */
target_id_t target_id; /* Target ID */
path_id_t path_id; /* System SCSI bus number */
- u_int16_t pd_type; /* type of peripheral device */
+ uint16_t pd_type; /* type of peripheral device */
struct scsi_inquiry_data inq_data; /* SCSI Inquiry data */
- u_int8_t serial_num[252]; /* device serial number */
- u_int8_t serial_num_len; /* length of the serial number */
- u_int8_t sync_period; /* Negotiated sync period */
- u_int8_t sync_offset; /* Negotiated sync offset */
- u_int8_t bus_width; /* Negotiated bus width */
+ uint8_t serial_num[252]; /* device serial number */
+ uint8_t serial_num_len; /* length of the serial number */
+ uint8_t sync_period; /* Negotiated sync period */
+ uint8_t sync_offset; /* Negotiated sync offset */
+ uint8_t bus_width; /* Negotiated bus width */
int fd; /* file descriptor for device */
};
.Ed
@@ -208,7 +209,7 @@
If the
.Ar device
argument is
-.Va NULL ,
+.Dv NULL ,
.Fn cam_open_spec_device
will allocate space for the
.Va cam_device
@@ -258,7 +259,9 @@
.Fn cam_open_spec_device
and
.Fn cam_open_btl ,
-should be NULL if the user wants the CAM library to allocate space for the
+should be
+.Dv NULL
+if the user wants the CAM library to allocate space for the
.Va cam_device
structure.
.Fn cam_close_device
@@ -300,6 +303,11 @@
.Fn cam_freeccb
frees CCBs allocated by
.Fn cam_getccb .
+If
+.Va ccb
+is
+.Dv NULL ,
+no action is taken.
.Pp
.Fn cam_path_string
takes as arguments a
@@ -365,11 +373,14 @@
.Fn cam_open_pass
return a pointer to a
.Va cam_device
-structure, or NULL if there was an error.
+structure, or
+.Dv NULL
+if there was an error.
.Pp
.Fn cam_getccb
-returns an allocated and partially initialized CCB, or NULL if allocation
-of the CCB failed.
+returns an allocated and partially initialized CCB, or
+.Dv NULL
+if allocation of the CCB failed.
.Pp
.Fn cam_send_ccb
returns a value of -1 if an error occurred, and
@@ -386,7 +397,9 @@
.Fn cam_device_dup
returns a copy of the
.Va device
-passed in, or NULL if an error occurred.
+passed in, or
+.Dv NULL
+if an error occurred.
.Pp
.Fn cam_get_device
returns 0 for success, and -1 to indicate failure.
@@ -422,4 +435,4 @@
.Xr pass 4
device.
.Pp
-Some of the functions are possibly mis-named or poorly named.
+Some of the functions are possibly misnamed or poorly named.
Property changes on: trunk/lib/libcam/cam.3
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Modified: trunk/lib/libcam/cam_cdbparse.3
===================================================================
--- trunk/lib/libcam/cam_cdbparse.3 2018-06-13 23:57:57 UTC (rev 10879)
+++ trunk/lib/libcam/cam_cdbparse.3 2018-06-13 23:59:33 UTC (rev 10880)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
.\"
.\" Copyright (c) 1998 Kenneth D. Merry.
.\" All rights reserved.
@@ -25,7 +26,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" $MidnightBSD$
+.\" $FreeBSD: stable/10/lib/libcam/cam_cdbparse.3 315608 2017-03-20 03:01:23Z ngie $
.\"
.\" This man page borrows heavily from the old scsi(3) man page, which had
.\" the following copyright:
@@ -61,7 +62,7 @@
.\" SUCH DAMAGE.
.\"
.\"
-.Dd October 13, 1998
+.Dd March 13, 2017
.Dt CAM_CDBPARSE 3
.Os
.Sh NAME
@@ -83,9 +84,9 @@
.Ft int
.Fo csio_build
.Fa "struct ccb_scsiio *csio"
-.Fa "u_int8_t *data_ptr"
-.Fa "u_int32_t dxfer_len"
-.Fa "u_int32_t flags"
+.Fa "uint8_t *data_ptr"
+.Fa "uint32_t dxfer_len"
+.Fa "uint32_t flags"
.Fa "int retry_count"
.Fa "int timeout"
.Fa "const char *cmd_spec"
@@ -94,9 +95,9 @@
.Ft int
.Fo csio_build_visit
.Fa "struct ccb_scsiio *csio"
-.Fa "u_int8_t *data_ptr"
-.Fa "u_int32_t dxfer_len"
-.Fa "u_int32_t flags"
+.Fa "uint8_t *data_ptr"
+.Fa "uint32_t dxfer_len"
+.Fa "uint32_t flags"
.Fa "int retry_count"
.Fa "int timeout"
.Fa "const char *cmd_spec"
@@ -122,7 +123,7 @@
.Fc
.Ft int
.Fo buff_decode
-.Fa "u_int8_t *buff"
+.Fa "uint8_t *buff"
.Fa "size_t len"
.Fa "const char *fmt"
.Fa "..."
@@ -129,7 +130,7 @@
.Fc
.Ft int
.Fo buff_decode_visit
-.Fa "u_int8_t *buff"
+.Fa "uint8_t *buff"
.Fa "size_t len"
.Fa "const char *fmt"
.Fa "void (*arg_put)(void *, int, void *, int, char *)"
@@ -150,7 +151,7 @@
.Fc
.Ft int
.Fo buff_encode_visit
-.Fa "u_int8_t *buff"
+.Fa "uint8_t *buff"
.Fa "size_t len"
.Fa "const char *fmt"
.Fa "int (*arg_get)(void *hook, char *field_name)"
@@ -170,12 +171,11 @@
These functions may be used in new applications, but users may find it
easier to use the various SCSI CCB building functions included with the
.Xr cam 3
-library.
-(e.g.\&
+library, e.g., \&
.Fn cam_fill_csio ,
.Fn scsi_start_stop ,
and
-.Fn scsi_read_write )
+.Fn scsi_read_write .
.Pp
.Fn csio_build
builds up a
@@ -522,7 +522,8 @@
layer.
The encoding/decoding functions in the old
.Tn SCSI
-code were written by Peter Dufault.
+code were written by
+.An Peter Dufault Aq Mt dufault at hda.com .
.Pp
Many systems have comparable interfaces to permit a user to construct a
SCSI command in user space.
@@ -529,10 +530,10 @@
.Pp
The old
.Va scsireq
-data structure was almost identical to the SGI /dev/scsi data
-structure.
-If anyone knows the name of the authors it should
-go here; Peter Dufault first read about it in a 1989 Sun Expert magazine.
+data structure was almost identical to the SGI /dev/scsi data structure.
+If anyone knows the name of the authors it should go here;
+Peter Dufault
+first read about it in a 1989 Sun Expert magazine.
.Pp
The new CCB data structures are derived from the CAM-2 and CAM-3
specifications.
@@ -545,11 +546,14 @@
.Fx
.Tn SCSI
library and the related kernel ioctl.
-If anyone needs that for compatibility contact dufault at hda.com.
+If anyone needs that for compatibility, contact
+.Mt dufault at hda.com .
.Sh AUTHORS
-Kenneth Merry implemented the CAM versions of these encoding and decoding
-functions.
-This current work is based upon earlier work by Peter Dufault.
+.An -nosplit
+.An Kenneth Merry Aq Mt ken at FreeBSD.org
+implemented the CAM versions of these encoding and decoding functions.
+This current work is based upon earlier work by
+.An Peter Dufault Aq Mt dufault at hda.com .
.Sh BUGS
There should probably be a function that encodes both the CDB and the data
buffer portions of a
Property changes on: trunk/lib/libcam/cam_cdbparse.3
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Modified: trunk/lib/libcam/camlib.c
===================================================================
--- trunk/lib/libcam/camlib.c 2018-06-13 23:57:57 UTC (rev 10879)
+++ trunk/lib/libcam/camlib.c 2018-06-13 23:59:33 UTC (rev 10880)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/*
* Copyright (c) 1997, 1998, 1999, 2002 Kenneth D. Merry.
* All rights reserved.
@@ -24,12 +25,13 @@
*/
#include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/lib/libcam/camlib.c 319023 2017-05-28 00:47:02Z ngie $");
#include <sys/types.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
@@ -101,7 +103,7 @@
* /dev/foo0
* foo0
* nfoo0
- *
+ *
* Some peripheral drivers create separate device nodes with 'n' prefix for
* non-rewind operations. Currently only sa(4) tape driver has this feature.
* We extract pure peripheral name as device name for this special case.
@@ -113,16 +115,14 @@
int
cam_get_device(const char *path, char *dev_name, int devnamelen, int *unit)
{
- char *func_name = "cam_get_device";
char *tmpstr, *tmpstr2;
char *newpath;
int unit_offset;
int i;
-
if (path == NULL) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: device pathname was NULL", func_name);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: device pathname was NULL", __func__);
return(-1);
}
@@ -138,14 +138,14 @@
*/
if (*tmpstr == '/') {
tmpstr2 = tmpstr;
- tmpstr = (char *)rindex(tmpstr2, '/');
+ tmpstr = strrchr(tmpstr2, '/');
if ((tmpstr != NULL) && (*tmpstr != '\0'))
tmpstr++;
}
if (*tmpstr == '\0') {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: no text after slash", func_name);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: no text after slash", __func__);
free(newpath);
return(-1);
}
@@ -172,9 +172,9 @@
* If we only have 1, we don't have a valid device name.
*/
if (strlen(tmpstr) < 2) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: must have both device name and unit number",
- func_name);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: must have both device name and unit number",
+ __func__);
free(newpath);
return(-1);
}
@@ -184,9 +184,9 @@
* has probably given us all numbers. Point out the error.
*/
if (isdigit(*tmpstr)) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: device name cannot begin with a number",
- func_name);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: device name cannot begin with a number",
+ __func__);
free(newpath);
return(-1);
}
@@ -193,12 +193,12 @@
/*
* At this point, if the last character of the string isn't a
- * number, we know the user either didn't give us a device number,
+ * number, we know the user either didn't give us a device number,
* or he gave us a device name/number format we don't recognize.
*/
if (!isdigit(tmpstr[strlen(tmpstr) - 1])) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: unable to find device unit number", func_name);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: unable to find device unit number", __func__);
free(newpath);
return(-1);
}
@@ -270,13 +270,12 @@
{
union ccb ccb;
struct periph_match_pattern *match_pat;
- char *func_name = "cam_open_btl";
int fd, bufsize;
if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE,
- func_name, strerror(errno));
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
+ __func__, strerror(errno));
return(NULL);
}
@@ -291,8 +290,8 @@
ccb.cdm.match_buf_len = bufsize;
ccb.cdm.matches = (struct dev_match_result *)malloc(bufsize);
if (ccb.cdm.matches == NULL) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: couldn't malloc match buffer", func_name);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: couldn't malloc match buffer", __func__);
close(fd);
return(NULL);
}
@@ -304,14 +303,15 @@
ccb.cdm.patterns = (struct dev_match_pattern *)malloc(
sizeof(struct dev_match_pattern));
if (ccb.cdm.patterns == NULL) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: couldn't malloc pattern buffer", func_name);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: couldn't malloc pattern buffer", __func__);
free(ccb.cdm.matches);
+ ccb.cdm.matches = NULL;
close(fd);
return(NULL);
}
ccb.cdm.patterns[0].type = DEV_MATCH_PERIPH;
- match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern;
+ match_pat = &ccb.cdm.patterns[0].pattern.periph_pattern;
/*
* We're looking for the passthrough device associated with this
@@ -326,9 +326,9 @@
PERIPH_MATCH_LUN | PERIPH_MATCH_NAME;
if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: CAMIOCOMMAND ioctl failed\n"
- "%s: %s", func_name, func_name, strerror(errno));
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: CAMIOCOMMAND ioctl failed\n"
+ "%s: %s", __func__, __func__, strerror(errno));
goto btl_bailout;
}
@@ -338,26 +338,26 @@
if ((ccb.ccb_h.status != CAM_REQ_CMP)
|| ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
&& (ccb.cdm.status != CAM_DEV_MATCH_MORE))) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: CAM error %#x, CDM error %d "
- "returned from XPT_DEV_MATCH ccb", func_name,
- ccb.ccb_h.status, ccb.cdm.status);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: CAM error %#x, CDM error %d "
+ "returned from XPT_DEV_MATCH ccb", __func__,
+ ccb.ccb_h.status, ccb.cdm.status);
goto btl_bailout;
}
if (ccb.cdm.status == CAM_DEV_MATCH_MORE) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: CDM reported more than one"
- " passthrough device at %d:%d:%d!!\n",
- func_name, path_id, target_id, target_lun);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: CDM reported more than one"
+ " passthrough device at %d:%d:%jx!!\n",
+ __func__, path_id, target_id, (uintmax_t)target_lun);
goto btl_bailout;
}
if (ccb.cdm.num_matches == 0) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: no passthrough device found at"
- " %d:%d:%d", func_name, path_id, target_id,
- target_lun);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: no passthrough device found at"
+ " %d:%d:%jx", __func__, path_id, target_id,
+ (uintmax_t)target_lun);
goto btl_bailout;
}
@@ -370,7 +370,9 @@
periph_result = &ccb.cdm.matches[0].result.periph_result;
pass_unit = periph_result->unit_number;
free(ccb.cdm.matches);
+ ccb.cdm.matches = NULL;
free(ccb.cdm.patterns);
+ ccb.cdm.patterns = NULL;
close(fd);
sprintf(dev_path, "/dev/pass%d", pass_unit);
return(cam_real_open_device(dev_path, flags, device, NULL,
@@ -378,9 +380,9 @@
break; /* NOTREACHED */
}
default:
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: asked for a peripheral match, but"
- " got a bus or device match", func_name);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: asked for a peripheral match, but"
+ " got a bus or device match", __func__);
goto btl_bailout;
break; /* NOTREACHED */
}
@@ -387,7 +389,9 @@
btl_bailout:
free(ccb.cdm.matches);
+ ccb.cdm.matches = NULL;
free(ccb.cdm.patterns);
+ ccb.cdm.patterns = NULL;
close(fd);
return(NULL);
}
@@ -412,7 +416,6 @@
int fd;
union ccb ccb;
char dev_path[256];
- char *func_name = "cam_lookup_pass";
/*
* The flags argument above only applies to the actual passthrough
@@ -420,9 +423,9 @@
* passthrough device.
*/
if ((fd = open(XPT_DEVICE, O_RDWR)) < 0) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: couldn't open %s\n%s: %s", func_name, XPT_DEVICE,
- func_name, strerror(errno));
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: couldn't open %s\n%s: %s", __func__, XPT_DEVICE,
+ __func__, strerror(errno));
return(NULL);
}
@@ -434,7 +437,7 @@
ccb.cgdl.unit_number = unit;
/*
- * Attempt to get the passthrough device. This ioctl will fail if
+ * Attempt to get the passthrough device. This ioctl will fail if
* the device name is null, if the device doesn't exist, or if the
* passthrough driver isn't in the kernel.
*/
@@ -451,12 +454,12 @@
snprintf(tmpstr, sizeof(tmpstr),
"\n%s: either the pass driver isn't in "
"your kernel\n%s: or %s%d doesn't exist",
- func_name, func_name, dev_name, unit);
+ __func__, __func__, dev_name, unit);
}
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: CAMGETPASSTHRU ioctl failed\n"
- "%s: %s%s", func_name, func_name, strerror(errno),
- (errno == ENOENT) ? tmpstr : "");
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: CAMGETPASSTHRU ioctl failed\n"
+ "%s: %s%s", __func__, __func__, strerror(errno),
+ (errno == ENOENT) ? tmpstr : "");
close(fd);
return(NULL);
@@ -471,9 +474,9 @@
* the device the user gave us.
*/
if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: device %s%d does not exist!",
- func_name, dev_name, unit);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: device %s%d does not exist!",
+ __func__, dev_name, unit);
return(NULL);
}
@@ -493,7 +496,6 @@
const char *given_path, const char *given_dev_name,
int given_unit_number)
{
- char *func_name = "cam_real_open_device";
union ccb ccb;
int fd = -1, malloced_device = 0;
@@ -503,15 +505,15 @@
if (device == NULL) {
if ((device = (struct cam_device *)malloc(
sizeof(struct cam_device))) == NULL) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: device structure malloc"
- " failed\n%s: %s", func_name, func_name,
- strerror(errno));
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: device structure malloc"
+ " failed\n%s: %s", __func__, __func__,
+ strerror(errno));
return(NULL);
}
device->fd = -1;
malloced_device = 1;
- }
+ }
/*
* If the user passed in a path, save it for him.
@@ -534,10 +536,10 @@
device->given_unit_number = given_unit_number;
if ((fd = open(path, flags)) < 0) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: couldn't open passthrough device %s\n"
- "%s: %s", func_name, path, func_name,
- strerror(errno));
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: couldn't open passthrough device %s\n"
+ "%s: %s", __func__, path, __func__,
+ strerror(errno));
goto crod_bailout;
}
@@ -550,7 +552,7 @@
* we don't have to set any fields.
*/
ccb.ccb_h.func_code = XPT_GDEVLIST;
-
+
/*
* We're only doing this to get some information on the device in
* question. Otherwise, we'd have to pass in yet another
@@ -562,9 +564,9 @@
* because we just opened it above. The only way this
* ioctl can fail is if the ccb size is wrong.
*/
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: CAMGETPASSTHRU ioctl failed\n"
- "%s: %s", func_name, func_name, strerror(errno));
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: CAMGETPASSTHRU ioctl failed\n"
+ "%s: %s", __func__, __func__, strerror(errno));
goto crod_bailout;
}
@@ -575,8 +577,8 @@
* the device the user gave us.
*/
if (ccb.cgdl.status == CAM_GDEVLIST_ERROR) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: passthrough device does not exist!", func_name);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: passthrough device does not exist!", __func__);
goto crod_bailout;
}
@@ -589,9 +591,9 @@
ccb.ccb_h.func_code = XPT_PATH_INQ;
if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: Path Inquiry CCB failed\n"
- "%s: %s", func_name, func_name, strerror(errno));
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: Path Inquiry CCB failed\n"
+ "%s: %s", __func__, __func__, strerror(errno));
goto crod_bailout;
}
strlcpy(device->sim_name, ccb.cpi.dev_name, sizeof(device->sim_name));
@@ -604,13 +606,13 @@
*/
ccb.ccb_h.func_code = XPT_GDEV_TYPE;
if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: Get Device Type CCB failed\n"
- "%s: %s", func_name, func_name, strerror(errno));
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: Get Device Type CCB failed\n"
+ "%s: %s", __func__, __func__, strerror(errno));
goto crod_bailout;
}
device->pd_type = SID_TYPE(&ccb.cgd.inq_data);
- bcopy(&ccb.cgd.inq_data, &device->inq_data,
+ bcopy(&ccb.cgd.inq_data, &device->inq_data,
sizeof(struct scsi_inquiry_data));
device->serial_num_len = ccb.cgd.serial_num_len;
bcopy(&ccb.cgd.serial_num, &device->serial_num, device->serial_num_len);
@@ -618,7 +620,7 @@
/*
* Zero the payload, the kernel does look at the flags.
*/
- bzero(&(&ccb.ccb_h)[1], sizeof(struct ccb_trans_settings));
+ CCB_CLEAR_ALL_EXCEPT_HDR(&ccb.cts);
/*
* Get transfer settings for this device.
@@ -628,12 +630,12 @@
ccb.cts.type = CTS_TYPE_CURRENT_SETTINGS;
if (ioctl(fd, CAMIOCOMMAND, &ccb) == -1) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: Get Transfer Settings CCB failed\n"
- "%s: %s", func_name, func_name, strerror(errno));
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: Get Transfer Settings CCB failed\n"
+ "%s: %s", __func__, __func__, strerror(errno));
goto crod_bailout;
}
- if (ccb.cts.protocol == XPORT_SPI) {
+ if (ccb.cts.transport == XPORT_SPI) {
struct ccb_trans_settings_spi *spi =
&ccb.cts.xport_specific.spi;
device->sync_period = spi->sync_period;
@@ -675,8 +677,10 @@
if (dev == NULL)
return;
- if (dev->fd >= 0)
+ if (dev->fd >= 0) {
close(dev->fd);
+ dev->fd = -1;
+ }
}
char *
@@ -687,7 +691,7 @@
return(str);
}
- snprintf(str, len, "(%s%d:%s%d:%d:%d:%d): ",
+ snprintf(str, len, "(%s%d:%s%d:%d:%d:%jx): ",
(dev->device_name[0] != '\0') ? dev->device_name : "pass",
dev->dev_unit_num,
(dev->sim_name[0] != '\0') ? dev->sim_name : "unknown",
@@ -694,7 +698,7 @@
dev->sim_unit_number,
dev->bus_id,
dev->target_id,
- dev->target_lun);
+ (uintmax_t)dev->target_lun);
return(str);
}
@@ -705,20 +709,19 @@
struct cam_device *
cam_device_dup(struct cam_device *device)
{
- char *func_name = "cam_device_dup";
struct cam_device *newdev;
if (device == NULL) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: device is NULL", func_name);
- return(NULL);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: device is NULL", __func__);
+ return (NULL);
}
newdev = malloc(sizeof(struct cam_device));
if (newdev == NULL) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: couldn't malloc CAM device structure", func_name);
- return(NULL);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: couldn't malloc CAM device structure", __func__);
+ return (NULL);
}
bcopy(device, newdev, sizeof(struct cam_device));
@@ -732,17 +735,16 @@
void
cam_device_copy(struct cam_device *src, struct cam_device *dst)
{
- char *func_name = "cam_device_copy";
if (src == NULL) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: source device struct was NULL", func_name);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: source device struct was NULL", __func__);
return;
}
if (dst == NULL) {
- snprintf(cam_errbuf, CAM_ERRBUF_SIZE,
- "%s: destination device struct was NULL", func_name);
+ snprintf(cam_errbuf, nitems(cam_errbuf),
+ "%s: destination device struct was NULL", __func__);
return;
}
Modified: trunk/lib/libcam/camlib.h
===================================================================
--- trunk/lib/libcam/camlib.h 2018-06-13 23:57:57 UTC (rev 10879)
+++ trunk/lib/libcam/camlib.h 2018-06-13 23:59:33 UTC (rev 10880)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/*
* Copyright (c) 1997, 1998 Kenneth D. Merry.
* All rights reserved.
@@ -22,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $MidnightBSD$
+ * $FreeBSD: stable/10/lib/libcam/camlib.h 316316 2017-03-31 04:42:00Z ngie $
*/
/*
* Buffer encoding/decoding routines taken from the original FreeBSD SCSI
@@ -70,13 +71,13 @@
#include <cam/cam.h>
#include <cam/cam_ccb.h>
-#define CAM_ERRBUF_SIZE 2048 /* sizeof the CAM libarary error string */
+#define CAM_ERRBUF_SIZE 2048 /* CAM library error string size */
/*
* Right now we hard code the transport layer device, but this will change
* if we ever get more than one transport layer.
*/
-#define XPT_DEVICE "/dev/xpt0"
+#define XPT_DEVICE "/dev/xpt0"
extern char cam_errbuf[];
@@ -83,7 +84,7 @@
struct cam_device {
char device_path[MAXPATHLEN];/*
- * Pathname of the device
+ * Pathname of the device
* given by the user. This
* may be null if the
* user states the device
@@ -98,15 +99,15 @@
* Unit number given by
* the user.
*/
- char device_name[DEV_IDLEN+1];/*
- * Name of the device,
- * e.g. 'pass'
+ char device_name[DEV_IDLEN+1];/*
+ * Name of the device,
+ * e.g. 'pass'
*/
u_int32_t dev_unit_num; /* Unit number of the passthrough
* device associated with this
* particular device.
*/
-
+
char sim_name[SIM_IDLEN+1]; /* Controller name, e.g. 'ahc' */
u_int32_t sim_unit_number; /* Controller unit number */
u_int32_t bus_id; /* Controller bus number */
@@ -142,7 +143,7 @@
char * cam_path_string(struct cam_device *dev, char *str,
int len);
struct cam_device * cam_device_dup(struct cam_device *device);
-void cam_device_copy(struct cam_device *src,
+void cam_device_copy(struct cam_device *src,
struct cam_device *dst);
int cam_get_device(const char *path, char *dev_name,
int devnamelen, int *unit);
Modified: trunk/lib/libcam/scsi_cmdparse.c
===================================================================
--- trunk/lib/libcam/scsi_cmdparse.c 2018-06-13 23:57:57 UTC (rev 10879)
+++ trunk/lib/libcam/scsi_cmdparse.c 2018-06-13 23:59:33 UTC (rev 10880)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/*
* Taken from the original FreeBSD user SCSI library.
*/
@@ -35,7 +36,7 @@
*/
#include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/lib/libcam/scsi_cmdparse.c 315122 2017-03-12 04:53:27Z ngie $");
#include <sys/types.h>
@@ -100,10 +101,11 @@
*/
static int
-do_buff_decode(u_int8_t *databuf, size_t len,
+do_buff_decode(u_int8_t *buff, size_t len,
void (*arg_put)(void *, int , void *, int, char *),
- void *puthook, const char *fmt, va_list ap)
+ void *puthook, const char *fmt, va_list *ap)
{
+ int ind = 0;
int assigned = 0;
int width;
int suppress;
@@ -112,26 +114,22 @@
static u_char mask[] = {0, 0x01, 0x03, 0x07, 0x0f,
0x1f, 0x3f, 0x7f, 0xff};
int value;
- u_char *base = databuf;
char *intendp;
char letter;
char field_name[80];
-# define ARG_PUT(ARG) \
- do \
- { \
- if (!suppress) \
- { \
+#define ARG_PUT(ARG) \
+ do { \
+ if (!suppress) { \
if (arg_put) \
- (*arg_put)(puthook, (letter == 't' ? \
- 'b' : letter), \
- (void *)((long)(ARG)), width, \
- field_name); \
+ (*arg_put)(puthook, (letter == 't' ? 'b' : \
+ letter), (void *)((long)(ARG)), width, \
+ field_name); \
else \
- *(va_arg(ap, int *)) = (ARG); \
+ *(va_arg(*ap, int *)) = (ARG); \
assigned++; \
} \
- field_name[0] = 0; \
+ field_name[0] = '\0'; \
suppress = 0; \
} while (0)
@@ -138,7 +136,7 @@
u_char bits = 0; /* For bit fields */
int shift = 0; /* Bits already shifted out */
suppress = 0;
- field_name[0] = 0;
+ field_name[0] = '\0';
while (!done) {
switch(letter = *fmt) {
@@ -172,9 +170,9 @@
fmt++;
}
- if (fmt)
+ if (*fmt != '\0')
fmt++; /* Skip '}' */
- field_name[i] = 0;
+ field_name[i] = '\0';
break;
}
@@ -187,7 +185,11 @@
done = 1;
else {
if (shift <= 0) {
- bits = *databuf++;
+ if (ind >= len) {
+ done = 1;
+ break;
+ }
+ bits = buff[ind++];
shift = 8;
}
value = (bits >> (shift - width)) &
@@ -209,29 +211,31 @@
fmt++;
width = strtol(fmt, &intendp, 10);
fmt = intendp;
+ if (ind + width > len) {
+ done = 1;
+ break;
+ }
switch(width) {
case 1:
- ARG_PUT(*databuf);
- databuf++;
+ ARG_PUT(buff[ind]);
+ ind++;
break;
case 2:
- ARG_PUT((*databuf) << 8 | *(databuf + 1));
- databuf += 2;
+ ARG_PUT(buff[ind] << 8 | buff[ind + 1]);
+ ind += 2;
break;
case 3:
- ARG_PUT((*databuf) << 16 |
- (*(databuf + 1)) << 8 | *(databuf + 2));
- databuf += 3;
+ ARG_PUT(buff[ind] << 16 |
+ buff[ind + 1] << 8 | buff[ind + 2]);
+ ind += 3;
break;
case 4:
- ARG_PUT((*databuf) << 24 |
- (*(databuf + 1)) << 16 |
- (*(databuf + 2)) << 8 |
- *(databuf + 3));
- databuf += 4;
+ ARG_PUT(buff[ind] << 24 | buff[ind + 1] << 16 |
+ buff[ind + 2] << 8 | buff[ind + 3]);
+ ind += 4;
break;
default:
@@ -242,32 +246,35 @@
break;
case 'c': /* Characters (i.e., not swapped) */
- case 'z': /* Characters with zeroed trailing
- spaces */
+ case 'z': /* Characters with zeroed trailing spaces */
shift = 0;
fmt++;
width = strtol(fmt, &intendp, 10);
fmt = intendp;
+ if (ind + width > len) {
+ done = 1;
+ break;
+ }
if (!suppress) {
- if (arg_put)
+ if (arg_put != NULL)
(*arg_put)(puthook,
- (letter == 't' ? 'b' : letter),
- databuf, width, field_name);
+ (letter == 't' ? 'b' : letter),
+ &buff[ind], width, field_name);
else {
char *dest;
- dest = va_arg(ap, char *);
- bcopy(databuf, dest, width);
+ dest = va_arg(*ap, char *);
+ bcopy(&buff[ind], dest, width);
if (letter == 'z') {
char *p;
for (p = dest + width - 1;
- (p >= (char *)dest)
- && (*p == ' '); p--)
- *p = 0;
+ p >= dest && *p == ' ';
+ p--)
+ *p = '\0';
}
}
assigned++;
}
- databuf += width;
+ ind += width;
field_name[0] = 0;
suppress = 0;
break;
@@ -287,7 +294,7 @@
* can't have a variable seek when you are using
* "arg_put".
*/
- width = (arg_put) ? 0 : va_arg(ap, int);
+ width = (arg_put) ? 0 : va_arg(*ap, int);
fmt++;
} else {
width = strtol(fmt, &intendp, 10);
@@ -295,9 +302,9 @@
}
if (plus)
- databuf += width; /* Relative seek */
+ ind += width; /* Relative seek */
else
- databuf = base + width; /* Absolute seek */
+ ind = width; /* Absolute seek */
break;
@@ -373,8 +380,8 @@
field_size = 8; /* Default to byte field type... */
*fmt = 'i';
field_width = 1; /* 1 byte wide */
- if (name)
- *name = 0;
+ if (name != NULL)
+ *name = '\0';
state = BETWEEN_FIELDS;
@@ -381,7 +388,7 @@
while (state != DONE) {
switch(state) {
case BETWEEN_FIELDS:
- if (*p == 0)
+ if (*p == '\0')
state = DONE;
else if (isspace(*p))
p++;
@@ -388,7 +395,7 @@
else if (*p == '#') {
while (*p && *p != '\n')
p++;
- if (p)
+ if (*p != '\0')
p++;
} else if (*p == '{') {
int i = 0;
@@ -404,7 +411,7 @@
}
if(name && i < n_name)
- name[i] = 0;
+ name[i] = '\0';
if (*p == '}')
p++;
@@ -524,7 +531,7 @@
if (is_error) {
*error_p = 1;
- return 0;
+ return (0);
}
*error_p = 0;
@@ -539,7 +546,7 @@
static int
do_encode(u_char *buff, size_t vec_max, size_t *used,
int (*arg_get)(void *, char *), void *gethook, const char *fmt,
- va_list ap)
+ va_list *ap)
{
int ind;
int shift;
@@ -562,9 +569,9 @@
if (suppress)
value = 0;
else
- value = arg_get ?
+ value = arg_get != NULL ?
(*arg_get)(gethook, field_name) :
- va_arg(ap, int);
+ va_arg(*ap, int);
}
#if 0
@@ -653,9 +660,9 @@
*used = ind;
if (error)
- return -1;
+ return (-1);
- return encoded;
+ return (encoded);
}
int
@@ -662,11 +669,16 @@
csio_decode(struct ccb_scsiio *csio, const char *fmt, ...)
{
va_list ap;
+ int retval;
va_start(ap, fmt);
- return(do_buff_decode(csio->data_ptr, (size_t)csio->dxfer_len,
- 0, 0, fmt, ap));
+ retval = do_buff_decode(csio->data_ptr, (size_t)csio->dxfer_len,
+ NULL, NULL, fmt, &ap);
+
+ va_end(ap);
+
+ return (retval);
}
int
@@ -674,7 +686,6 @@
void (*arg_put)(void *, int, void *, int, char *),
void *puthook)
{
- va_list ap;
/*
* We need some way to output things; we can't do it without
@@ -681,12 +692,10 @@
* the arg_put function.
*/
if (arg_put == NULL)
- return(-1);
+ return (-1);
- bzero(&ap, sizeof(ap));
-
- return(do_buff_decode(csio->data_ptr, (size_t)csio->dxfer_len,
- arg_put, puthook, fmt, ap));
+ return (do_buff_decode(csio->data_ptr, (size_t)csio->dxfer_len,
+ arg_put, puthook, fmt, NULL));
}
int
@@ -693,10 +702,15 @@
buff_decode(u_int8_t *buff, size_t len, const char *fmt, ...)
{
va_list ap;
+ int retval;
va_start(ap, fmt);
- return(do_buff_decode(buff, len, 0, 0, fmt, ap));
+ retval = do_buff_decode(buff, len, NULL, NULL, fmt, &ap);
+
+ va_end(ap);
+
+ return (retval);
}
int
@@ -704,7 +718,6 @@
void (*arg_put)(void *, int, void *, int, char *),
void *puthook)
{
- va_list ap;
/*
* We need some way to output things; we can't do it without
@@ -711,11 +724,9 @@
* the arg_put function.
*/
if (arg_put == NULL)
- return(-1);
+ return (-1);
- bzero(&ap, sizeof(ap));
-
- return(do_buff_decode(buff, len, arg_put, puthook, fmt, ap));
+ return (do_buff_decode(buff, len, arg_put, puthook, fmt, NULL));
}
/*
@@ -732,7 +743,7 @@
va_list ap;
if (csio == NULL)
- return(0);
+ return (0);
bzero(csio, sizeof(struct ccb_scsiio));
@@ -739,8 +750,8 @@
va_start(ap, cmd_spec);
if ((retval = do_encode(csio->cdb_io.cdb_bytes, SCSI_MAX_CDBLEN,
- &cmdlen, NULL, NULL, cmd_spec, ap)) == -1)
- return(retval);
+ &cmdlen, NULL, NULL, cmd_spec, &ap)) == -1)
+ goto done;
cam_fill_csio(csio,
/* retries */ retry_count,
@@ -753,7 +764,10 @@
/* cdb_len */ cmdlen,
/* timeout */ timeout ? timeout : 5000);
- return(retval);
+done:
+ va_end(ap);
+
+ return (retval);
}
int
@@ -762,12 +776,11 @@
int timeout, const char *cmd_spec,
int (*arg_get)(void *hook, char *field_name), void *gethook)
{
- va_list ap;
size_t cmdlen;
int retval;
if (csio == NULL)
- return(0);
+ return (0);
/*
* We need something to encode, but we can't get it without the
@@ -774,15 +787,13 @@
* arg_get function.
*/
if (arg_get == NULL)
- return(-1);
+ return (-1);
- bzero(&ap, sizeof(ap));
-
bzero(csio, sizeof(struct ccb_scsiio));
if ((retval = do_encode(csio->cdb_io.cdb_bytes, SCSI_MAX_CDBLEN,
- &cmdlen, arg_get, gethook, cmd_spec, ap)) == -1)
- return(retval);
+ &cmdlen, arg_get, gethook, cmd_spec, NULL)) == -1)
+ return (retval);
cam_fill_csio(csio,
/* retries */ retry_count,
@@ -795,7 +806,7 @@
/* cdb_len */ cmdlen,
/* timeout */ timeout ? timeout : 5000);
- return(retval);
+ return (retval);
}
int
@@ -802,13 +813,19 @@
csio_encode(struct ccb_scsiio *csio, const char *fmt, ...)
{
va_list ap;
+ int retval;
if (csio == NULL)
- return(0);
+ return (0);
va_start(ap, fmt);
- return(do_encode(csio->data_ptr, csio->dxfer_len, 0, 0, 0, fmt, ap));
+ retval = do_encode(csio->data_ptr, csio->dxfer_len, NULL, NULL, NULL,
+ fmt, &ap);
+
+ va_end(ap);
+
+ return (retval);
}
int
@@ -815,7 +832,6 @@
buff_encode_visit(u_int8_t *buff, size_t len, const char *fmt,
int (*arg_get)(void *hook, char *field_name), void *gethook)
{
- va_list ap;
/*
* We need something to encode, but we can't get it without the
@@ -822,11 +838,9 @@
* arg_get function.
*/
if (arg_get == NULL)
- return(-1);
+ return (-1);
- bzero(&ap, sizeof(ap));
-
- return(do_encode(buff, len, 0, arg_get, gethook, fmt, ap));
+ return (do_encode(buff, len, NULL, arg_get, gethook, fmt, NULL));
}
int
@@ -833,7 +847,6 @@
csio_encode_visit(struct ccb_scsiio *csio, const char *fmt,
int (*arg_get)(void *hook, char *field_name), void *gethook)
{
- va_list ap;
/*
* We need something to encode, but we can't get it without the
@@ -840,10 +853,8 @@
* arg_get function.
*/
if (arg_get == NULL)
- return(-1);
+ return (-1);
- bzero(&ap, sizeof(ap));
-
- return(do_encode(csio->data_ptr, csio->dxfer_len, 0, arg_get,
- gethook, fmt, ap));
+ return (do_encode(csio->data_ptr, csio->dxfer_len, NULL, arg_get,
+ gethook, fmt, NULL));
}
Added: trunk/lib/libcam/tests/Makefile
===================================================================
--- trunk/lib/libcam/tests/Makefile (rev 0)
+++ trunk/lib/libcam/tests/Makefile 2018-06-13 23:59:33 UTC (rev 10880)
@@ -0,0 +1,12 @@
+# $MidnightBSD$
+# $FreeBSD: stable/10/lib/libcam/tests/Makefile 315696 2017-03-22 01:11:13Z ngie $
+
+ATF_TESTS_C+= libcam_test
+
+LDADD+= -lcam
+
+DPADD+= ${LIBCAM}
+
+WARNS?= 6
+
+.include <bsd.test.mk>
Property changes on: trunk/lib/libcam/tests/Makefile
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/lib/libcam/tests/libcam_test.c
===================================================================
--- trunk/lib/libcam/tests/libcam_test.c (rev 0)
+++ trunk/lib/libcam/tests/libcam_test.c 2018-06-13 23:59:33 UTC (rev 10880)
@@ -0,0 +1,293 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2017 Ngie Cooper <ngie at freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/lib/libcam/tests/libcam_test.c 321122 2017-07-18 08:44:38Z ngie $");
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <camlib.h>
+
+#include <atf-c.h>
+
+static const char *
+get_cam_test_device(const atf_tc_t *tc)
+{
+ const char *cam_test_device;
+
+ cam_test_device = atf_tc_get_config_var(tc, "cam_test_device");
+
+ return (cam_test_device);
+}
+
+static void
+cam_clear_error(void)
+{
+
+ strcpy(cam_errbuf, "");
+}
+
+static bool
+cam_has_error(void)
+{
+
+ return (strlen(cam_errbuf) != 0);
+}
+
+ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_NULL_path);
+ATF_TC_BODY(cam_get_device_negative_test_NULL_path, tc)
+{
+ char parsed_dev_name[DEV_IDLEN + 1];
+ int parsed_unit;
+
+ ATF_REQUIRE_MSG(cam_get_device(NULL, parsed_dev_name,
+ nitems(parsed_dev_name), &parsed_unit) == -1,
+ "cam_get_device succeeded unexpectedly");
+}
+
+ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_bad_path);
+ATF_TC_BODY(cam_get_device_negative_test_bad_path, tc)
+{
+ char parsed_dev_name[DEV_IDLEN + 1];
+ int parsed_unit;
+
+ ATF_REQUIRE_MSG(cam_get_device("1ada", parsed_dev_name,
+ nitems(parsed_dev_name), &parsed_unit) == -1,
+ "cam_get_device succeeded unexpectedly");
+}
+
+ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_nul_path);
+ATF_TC_BODY(cam_get_device_negative_test_nul_path, tc)
+{
+ char parsed_dev_name[DEV_IDLEN + 1];
+ int parsed_unit;
+
+ ATF_REQUIRE_MSG(cam_get_device("", parsed_dev_name,
+ nitems(parsed_dev_name), &parsed_unit) == -1,
+ "cam_get_device succeeded unexpectedly");
+}
+
+ATF_TC_WITHOUT_HEAD(cam_get_device_negative_test_root);
+ATF_TC_BODY(cam_get_device_negative_test_root, tc)
+{
+ char parsed_dev_name[DEV_IDLEN + 1];
+ int parsed_unit;
+
+ ATF_REQUIRE_MSG(cam_get_device("/", parsed_dev_name,
+ nitems(parsed_dev_name), &parsed_unit) == -1,
+ "cam_get_device succeeded unexpectedly");
+}
+
+ATF_TC_WITHOUT_HEAD(cam_get_device_positive_test);
+ATF_TC_BODY(cam_get_device_positive_test, tc)
+{
+ char expected_dev_name[] = "foo";
+ char parsed_dev_name[DEV_IDLEN + 1];
+ int expected_unit, parsed_unit;
+
+ expected_unit = 1;
+
+ ATF_REQUIRE_MSG(cam_get_device("/dev/foo1", parsed_dev_name,
+ nitems(parsed_dev_name), &parsed_unit) == 0,
+ "cam_get_device failed");
+ ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name);
+ ATF_REQUIRE(parsed_unit == expected_unit);
+
+ strcpy(parsed_dev_name, "");
+ parsed_unit = -1;
+
+ ATF_REQUIRE_MSG(cam_get_device("foo1", parsed_dev_name,
+ nitems(parsed_dev_name), &parsed_unit) == 0,
+ "cam_get_device failed");
+ ATF_REQUIRE_STREQ(parsed_dev_name, expected_dev_name);
+ ATF_REQUIRE(parsed_unit == expected_unit);
+}
+
+ATF_TC(cam_open_device_negative_test_O_RDONLY);
+ATF_TC_HEAD(cam_open_device_negative_test_O_RDONLY, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr",
+ "test that cam_open_device(`cam_device`, O_RDONLY) fails to open "
+ "the underlying pass(4) device (bug 217649)");
+ atf_tc_set_md_var(tc, "require.config", "cam_test_device");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(cam_open_device_negative_test_O_RDONLY, tc)
+{
+ const char *cam_test_device;
+
+ cam_test_device = get_cam_test_device(tc);
+
+ cam_clear_error();
+ ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
+ ATF_REQUIRE(cam_has_error());
+}
+
+ATF_TC(cam_open_device_negative_test_nonexistent);
+ATF_TC_HEAD(cam_open_device_negative_test_nonexistent, tc)
+{
+
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(cam_open_device_negative_test_nonexistent, tc)
+{
+
+ cam_clear_error();
+ ATF_REQUIRE(cam_open_device("/nonexistent", O_RDWR) == NULL);
+ ATF_REQUIRE(cam_has_error());
+}
+
+ATF_TC(cam_open_device_negative_test_unprivileged);
+ATF_TC_HEAD(cam_open_device_negative_test_unprivileged, tc)
+{
+
+ atf_tc_set_md_var(tc, "require.config", "cam_test_device");
+ atf_tc_set_md_var(tc, "require.user", "unprivileged");
+}
+
+ATF_TC_BODY(cam_open_device_negative_test_unprivileged, tc)
+{
+ const char *cam_test_device;
+
+ cam_test_device = get_cam_test_device(tc);
+
+ cam_clear_error();
+ ATF_CHECK(cam_open_device(cam_test_device, O_RDONLY) == NULL);
+ ATF_REQUIRE(cam_has_error());
+
+ cam_clear_error();
+ ATF_CHECK(cam_open_device(cam_test_device, O_RDWR) == NULL);
+ ATF_REQUIRE(cam_has_error());
+}
+
+ATF_TC(cam_open_device_positive_test);
+ATF_TC_HEAD(cam_open_device_positive_test, tc)
+{
+
+ atf_tc_set_md_var(tc, "require.config", "cam_test_device");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(cam_open_device_positive_test, tc)
+{
+ struct cam_device *cam_dev;
+ const char *cam_test_device;
+
+ cam_test_device = get_cam_test_device(tc);
+
+ cam_clear_error();
+ cam_dev = cam_open_device(cam_test_device, O_RDWR);
+ ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
+ cam_errbuf);
+ ATF_REQUIRE(!cam_has_error());
+ cam_close_device(cam_dev);
+}
+
+ATF_TC(cam_close_device_negative_test_NULL);
+ATF_TC_HEAD(cam_close_device_negative_test_NULL, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr",
+ "test that cam_close_device(NULL) succeeds without error");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(cam_close_device_negative_test_NULL, tc)
+{
+
+ cam_clear_error();
+ cam_close_device(NULL);
+ ATF_REQUIRE(!cam_has_error());
+}
+
+ATF_TC(cam_getccb_positive_test);
+ATF_TC_HEAD(cam_getccb_positive_test, tc)
+{
+
+ atf_tc_set_md_var(tc, "require.config", "cam_test_device");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(cam_getccb_positive_test, tc)
+{
+ union ccb *cam_ccb;
+ struct cam_device *cam_dev;
+ const char *cam_test_device;
+
+ cam_test_device = get_cam_test_device(tc);
+
+ cam_clear_error();
+ cam_dev = cam_open_device(cam_test_device, O_RDWR);
+ ATF_CHECK_MSG(cam_dev != NULL, "cam_open_device failed: %s",
+ cam_errbuf);
+ ATF_REQUIRE(!cam_has_error());
+ cam_ccb = cam_getccb(cam_dev);
+ ATF_CHECK_MSG(cam_ccb != NULL, "get_camccb failed: %s", cam_errbuf);
+ ATF_REQUIRE(!cam_has_error());
+ cam_freeccb(cam_ccb);
+ cam_close_device(cam_dev);
+}
+
+ATF_TC(cam_freeccb_negative_test_NULL);
+ATF_TC_HEAD(cam_freeccb_negative_test_NULL, tc)
+{
+
+ atf_tc_set_md_var(tc, "descr",
+ "test that cam_freeccb(NULL) succeeds without error");
+ atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(cam_freeccb_negative_test_NULL, tc)
+{
+
+ cam_clear_error();
+ cam_freeccb(NULL);
+ ATF_REQUIRE(!cam_has_error());
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, cam_get_device_negative_test_NULL_path);
+ ATF_TP_ADD_TC(tp, cam_get_device_negative_test_bad_path);
+ ATF_TP_ADD_TC(tp, cam_get_device_negative_test_nul_path);
+ ATF_TP_ADD_TC(tp, cam_get_device_negative_test_root);
+ ATF_TP_ADD_TC(tp, cam_get_device_positive_test);
+ ATF_TP_ADD_TC(tp, cam_open_device_negative_test_O_RDONLY);
+ ATF_TP_ADD_TC(tp, cam_open_device_negative_test_nonexistent);
+ ATF_TP_ADD_TC(tp, cam_open_device_negative_test_unprivileged);
+ ATF_TP_ADD_TC(tp, cam_open_device_positive_test);
+ ATF_TP_ADD_TC(tp, cam_close_device_negative_test_NULL);
+ ATF_TP_ADD_TC(tp, cam_getccb_positive_test);
+ ATF_TP_ADD_TC(tp, cam_freeccb_negative_test_NULL);
+
+ return (atf_no_error());
+}
Property changes on: trunk/lib/libcam/tests/libcam_test.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
More information about the Midnightbsd-cvs
mailing list