* file named AUTHORS, in the root directory of this distribution.
*/
/*
- * $Id: scsi-bsd.c,v 1.1.2.10.4.2.2.4 2003/06/05 20:44:23 martinea Exp $
+ * $Id: scsi-bsd.c,v 1.18 2006/05/25 01:47:07 johnfranks Exp $
*
* Interface to execute SCSI commands on an BSD System (FreeBSD)
*
*/
-#include <amanda.h>
-
-#ifdef HAVE_BSD_LIKE_SCSI
-
-/*
-#ifdef HAVE_STDIO_H
-*/
-#include <stdio.h>
-/*
-#endif
-*/
-#ifdef HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#endif
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
+#include "amanda.h"
#include <sys/scsiio.h>
#include <sys/mtio.h>
void SCSI_OS_Version()
{
#ifndef lint
- static char rcsid[] = "$Id: scsi-bsd.c,v 1.1.2.10.4.2.2.4 2003/06/05 20:44:23 martinea Exp $";
+ static char rcsid[] = "$Id: scsi-bsd.c,v 1.18 2006/05/25 01:47:07 johnfranks Exp $";
DebugPrint(DEBUG_INFO, SECTION_INFO, "scsi-os-layer: %s\n",rcsid);
#endif
}
* Return:
* 0 -> device not opened
* 1 -> sucess , device open
- * -1 -> fatal error
*/
int SCSI_OpenDevice(int ip)
{
pDev[ip].SCSI = 0; /* This will only be set if the inquiry works */
pDev[ip].inquiry = (SCSIInquiry_T *)malloc(INQUIRY_SIZE);
- if (( pDev[ip].fd = open(pDev[ip].dev, O_RDWR)) > 0) /* We need the device in read/write mode */
+ if (( pDev[ip].fd = open(pDev[ip].dev, O_RDWR)) >= 0) /* We need the device in read/write mode */
{
pDev[ip].devopen = 1; /* The device is open for use */
pDev[ip].avail = 1; /* And it is available, it could be opened */
} /* open() */
return(0); /* Open failed .... */
} else { /* pDev[ip].inqdone */ /* OK this is the way we go if the device */
- if (( pDev[ip].fd = open(pDev[ip].dev, O_RDWR)) > 0) /* was opened successfull before */
+ if (( pDev[ip].fd = open(pDev[ip].dev, O_RDWR)) >= 0) /* was opened successfull before */
{
pDev[ip].devopen = 1;
return(1);
int SCSI_ExecuteCommand(int DeviceFD,
Direction_T Direction,
CDB_T CDB,
- int CDB_Length,
+ size_t CDB_Length,
void *DataBuffer,
- int DataBufferLength,
- char *pRequestSense,
- int RequestSenseLength)
+ size_t DataBufferLength,
+ RequestSense_T *pRequestSense,
+ size_t RequestSenseLength)
{
extern OpenFiles_T *pDev;
ExtendedRequestSense_T ExtendedRequestSense;
int retries = 5;
extern int errno;
+ /* Basic sanity checks */
+ assert(CDB_Length <= UCHAR_MAX);
+ assert(RequestSenseLength <= UCHAR_MAX);
+
+ /* Clear buffer for cases where sense is not returned */
+ memset(pRequestSense, 0, RequestSenseLength);
+
if (pDev[DeviceFD].avail == 0)
{
return(SCSI_ERROR);
}
- memset(&ds, 0, sizeof(scsireq_t));
+ memset(&ds, 0, SIZEOF(scsireq_t));
memset(pRequestSense, 0, RequestSenseLength);
- memset(&ExtendedRequestSense, 0 , sizeof(ExtendedRequestSense_T));
+ memset(&ExtendedRequestSense, 0 , SIZEOF(ExtendedRequestSense_T));
ds.flags = SCCMD_ESCAPE;
/* Timeout */
while (--retries > 0) {
if (pDev[DeviceFD].devopen == 0)
- {
- SCSI_OpenDevice(DeviceFD);
- }
+ if (SCSI_OpenDevice(DeviceFD) == 0)
+ return(SCSI_ERROR);
Result = ioctl(pDev[DeviceFD].fd, SCIOCCOMMAND, &ds);
SCSI_CloseDevice(DeviceFD);
memcpy(pRequestSense, ds.sense, RequestSenseLength);
if (Result < 0)
{
- dbprintf(("errno : %d\n",errno));
+ dbprintf("errno : %s\n",strerror(errno));
return (SCSI_ERROR);
}
- dbprintf(("SCSI_ExecuteCommand(BSD) %02X STATUS(%02X) \n", CDB[0], ds.retsts));
+ dbprintf("SCSI_ExecuteCommand(BSD) %02X STATUS(%02X) \n", CDB[0], ds.retsts);
switch (ds.retsts)
{
case SCCMD_BUSY: /* BUSY */
break;
+
case SCCMD_OK: /* GOOD */
return(SCSI_OK);
- break;
+
case SCCMD_SENSE: /* CHECK CONDITION */
return(SCSI_SENSE);
- break;
+
default:
continue;
}
int ret = 0;
if (pDev[DeviceFD].devopen == 0)
- {
- SCSI_OpenDevice(DeviceFD);
- }
+ if (SCSI_OpenDevice(DeviceFD) == 0)
+ return(-1);
switch (command)
{
if (ioctl(pDev[DeviceFD].fd , MTIOCTOP, &mtop) != 0)
{
- dbprintf(("Tape_Ioctl error ioctl %d\n",errno));
+ dbprintf(_("Tape_Ioctl error ioctl %s\n"),strerror(errno));
SCSI_CloseDevice(DeviceFD);
return(-1);
}
return(-1);
}
-#endif
/*
* Local variables:
* indent-tabs-mode: nil