1
0
mirror of https://git.FreeBSD.org/src.git synced 2025-01-20 15:43:16 +00:00

Allocate a target mode instance to handle our target mode session, clean

it up on exit.  The address for attaching the emulator (path, target id,
lun) is now specified on the command line.  Some attempt at cathing
signals and cleaning up target mode instances is now made.
This commit is contained in:
Justin T. Gibbs 1999-03-05 23:12:02 +00:00
parent b2f2e3bbdb
commit b3fae35ce8
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=44498

View File

@ -26,13 +26,15 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: scsi_target.c,v 1.1 1998/09/15 06:46:32 gibbs Exp $ * $Id: scsi_target.c,v 1.2 1998/12/10 04:00:03 gibbs Exp $
*/ */
#include <sys/types.h> #include <sys/types.h>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <poll.h> #include <poll.h>
#include <signal.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -43,18 +45,26 @@
#include <cam/scsi/scsi_message.h> #include <cam/scsi/scsi_message.h>
#include <cam/scsi/scsi_targetio.h> #include <cam/scsi/scsi_targetio.h>
char *appname; char *appname;
int ifd; int ifd;
char *ifilename; char *ifilename;
int ofd; int ofd;
char *ofilename; char *ofilename;
size_t bufsize = 64 * 1024; size_t bufsize = 64 * 1024;
void *buf; void *buf;
char *targdevname; char targdevname[80];
int targfd; int targctlfd;
int targfd;
int quit;
struct ioc_alloc_unit alloc_unit = {
CAM_BUS_WILDCARD,
CAM_TARGET_WILDCARD,
CAM_LUN_WILDCARD
};
static void pump_events(); static void pump_events();
static void handle_exception(); static void handle_exception();
static void quit_handler();
static void usage(); static void usage();
int int
@ -63,7 +73,7 @@ main(int argc, char *argv[])
int ch; int ch;
appname = *argv; appname = *argv;
while ((ch = getopt(argc, argv, "i:o:")) != -1) { while ((ch = getopt(argc, argv, "i:o:p:t:l:")) != -1) {
switch(ch) { switch(ch) {
case 'i': case 'i':
if ((ifd = open(optarg, O_RDONLY)) == -1) { if ((ifd = open(optarg, O_RDONLY)) == -1) {
@ -80,6 +90,15 @@ main(int argc, char *argv[])
} }
ofilename = optarg; ofilename = optarg;
break; break;
case 'p':
alloc_unit.path_id = atoi(optarg);
break;
case 't':
alloc_unit.target_id = atoi(optarg);
break;
case 'l':
alloc_unit.lun_id = atoi(optarg);
break;
case '?': case '?':
default: default:
usage(); usage();
@ -89,15 +108,38 @@ main(int argc, char *argv[])
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (argc != 1) { if (alloc_unit.path_id == CAM_BUS_WILDCARD
fprintf(stderr, "%s: No target device specifiled\n", appname); || alloc_unit.target_id == CAM_TARGET_WILDCARD
|| alloc_unit.lun_id == CAM_LUN_WILDCARD) {
fprintf(stderr, "%s: Incomplete device path specifiled\n",
appname);
usage(); usage();
/* NOTREACHED */ /* NOTREACHED */
} }
targdevname = *argv; if (argc != 0) {
fprintf(stderr, "%s: Too many arguments\n", appname);
usage();
/* NOTREACHED */
}
/* Allocate a new instance */
if ((targctlfd = open("/dev/targ.ctl", O_RDWR)) == -1) {
perror("/dev/targ.ctl");
exit(EX_UNAVAILABLE);
}
if (ioctl(targctlfd, TARGCTLIOALLOCUNIT, &alloc_unit) == -1) {
perror("TARGCTLIOALLOCUNIT");
exit(EX_SOFTWARE);
}
snprintf(targdevname, sizeof(targdevname), "/dev/targ%d",
alloc_unit.unit);
if ((targfd = open(targdevname, O_RDWR)) == -1) { if ((targfd = open(targdevname, O_RDWR)) == -1) {
perror(targdevname); perror(targdevname);
ioctl(targctlfd, TARGCTLIOFREEUNIT, &alloc_unit);
exit(EX_NOINPUT); exit(EX_NOINPUT);
} }
@ -108,8 +150,20 @@ main(int argc, char *argv[])
exit(EX_OSERR); exit(EX_OSERR);
} }
signal(SIGHUP, quit_handler);
signal(SIGINT, quit_handler);
signal(SIGTERM, quit_handler);
pump_events(); pump_events();
close(targfd);
if (ioctl(targctlfd, TARGCTLIOFREEUNIT, &alloc_unit) == -1) {
perror("TARGCTLIOFREEUNIT");
exit(EX_SOFTWARE);
}
close(targctlfd);
return (0); return (0);
} }
@ -121,12 +175,14 @@ pump_events()
targpoll.fd = targfd; targpoll.fd = targfd;
targpoll.events = POLLRDNORM|POLLWRNORM; targpoll.events = POLLRDNORM|POLLWRNORM;
while (1) { while (quit == 0) {
int retval; int retval;
retval = poll(&targpoll, 1, INFTIM); retval = poll(&targpoll, 1, INFTIM);
if (retval == -1) { if (retval == -1) {
if (errno == EINTR)
continue;
perror("Poll Failed"); perror("Poll Failed");
exit(EX_SOFTWARE); exit(EX_SOFTWARE);
} }
@ -259,12 +315,19 @@ handle_exception()
} }
} }
static void
quit_handler(int signum)
{
quit = 1;
}
static void static void
usage() usage()
{ {
(void)fprintf(stderr, (void)fprintf(stderr,
"usage: %-16s [-o output_file] [-i input_file] /dev/targ?\n", appname); "usage: %-16s [-o output_file] [-i input_file] -p path -t target -l lun\n",
appname);
exit(EX_USAGE); exit(EX_USAGE);
} }