Imported Upstream version 2.5.1
[debian/amanda] / contrib / sst / sstest.c
1 /*
2  * Copyright (c) 1992,1997 Sun Microsystems, Inc.  All Rights Reserved.
3  * Sun considers its source code as an unpublished, proprietary
4  * trade secret, and it is available only under strict license
5  * provisions.  This copyright notice is placed here only to protect
6  * Sun in the event the source is deemed a published work.
7  * Disassembly, decompilation, or other means of reducing the
8  * object code to human readable form is prohibited by the license
9  * agreement under which this code is provided to the user or
10  * company in possession of this copy.
11  *
12  * RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the
13  * Government is subject to restrictions as set forth in
14  * subparagraph (c)(1)(ii) of the Rights in Technical Data and
15  * Computer Software clause at DFARS 52.227-7013 and in similar
16  * clauses in the FAR and NASA FAR Supplement.
17  *
18  * These examples are provided with no warranties of any kind,
19  * including without limitation accuracy and usefulness, and Sun
20  * expressly disclaims all implied warranties of merchantability,
21  * fitness for a particular purpose and non-infringement. In no
22  * event shall Sun be liable for any damages, including without
23  * limitation, direct, special, indirect, or consequential damages
24  * arising out of, or relating to, use of these examples by customer
25  * or any third party. Sun is under no obligation to provide support
26  * to customer for this software.
27  *
28  * sstest.c
29  * A simple tst program for the sample SCSI target driver.
30  * To compile: cc (or acc) sstest.c -o sstest
31  * Note: the full ANSI conformance flag, -Xt, fails because of the
32  *       system header files.
33  *
34  * Usage:
35  *      sstest [device] [command] [arg]
36  * Device is the /devices entry
37  * Command can be:
38  *      open - just open and close the device
39  *      read [arg] - read a block. If arg is specifed, printf the result
40  *      write [arg] - write a block. With arg, write "arg"
41  *      rew - send the SCSI Rewind command (tests the USCSICMD ioctl)
42  *      tur - send the SCSI Test Unit Ready command (SSTIOC_READY ioctl)
43  *      errlev [lev] - set the error reporting level to <lev> (see sst_def.h)
44  */
45
46 #pragma ident   "@(#)sstest.c 1.4       97/04/07 SMI"
47
48 #include <fcntl.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <sys/scsi/impl/uscsi.h>
52 #include <sys/scsi/generic/commands.h>
53 #include "sst_def.h"
54
55 #define BUFSZ   512
56
57 static void usage(char *name);
58
59 main(int argc, char *argv[])
60 {
61         int     fd, nbytes, level, arg;
62         char    buf[BUFSZ], *progname, *devname, *command, *value;
63         struct uscsi_cmd                scmd;
64
65         if ((argc != 3) && (argc != 4)) {
66                 usage(argv[0]);
67                 /*NOTREACHED*/
68         }
69
70         progname = argv[0];     /* name of this program */
71         devname = argv[1];      /* full path of device name */
72         command = argv[2];      /* command to do */
73         value = argv[3];        /* command's argument if present */
74         arg = (argc == 4);      /* command has arg */
75
76
77
78         (void) sprintf(buf, "%s", devname);
79         if ((fd = open(buf, O_RDWR)) == -1) {
80                 perror(buf);
81                 exit(1);
82                 /*NOTREACHED*/
83         }
84
85         (void) memset((void *)buf, 0, BUFSZ);
86
87         if (strcmp(command, "open") == 0) {
88                 fprintf(stdout, "Device opened\n");
89         } else if (strcmp(command, "read") == 0) {
90                 if ((nbytes = read(fd, buf, BUFSZ)) != BUFSZ) {
91                         perror("read");
92                         (void) close(fd);
93                         exit(1);
94                         /*NOTREACHED*/
95                 }
96                 if (argc == 4) {
97                         fprintf(stdout, "Read \"%s\"\n", buf);
98                 } else {
99                         fprintf(stdout, "Read %d bytes\n", nbytes);
100                 }
101
102         } else if (strcmp(command, "write") == 0) {
103                 if (arg) {
104                         strcpy(buf, value);
105                 }
106                 if ((nbytes = write(fd, buf, BUFSZ)) != BUFSZ) {
107                         perror("write");
108                         (void) close(fd);
109                         exit(1);
110                         /*NOTREACHED*/
111                 }
112                 fprintf(stdout, "Wrote %d bytes\n", nbytes);
113
114         } else if (strcmp(command, "rew") == 0) {
115                 (void) memset((void *) &scmd, 0, sizeof (scmd));
116                 scmd.uscsi_flags = 0;
117                 scmd.uscsi_timeout = 30;
118                 buf[0] = SCMD_REWIND;
119                 scmd.uscsi_cdb = buf;
120                 scmd.uscsi_bufaddr = NULL;
121                 scmd.uscsi_buflen = 0;
122                 scmd.uscsi_cdblen = CDB_GROUP0;
123
124                 if (ioctl(fd, USCSICMD, &scmd) == -1) {
125                         perror("rewind ioctl");
126                         (void) close(fd);
127                         exit(1);
128                         /*NOTREACHED*/
129                 }
130                 fprintf(stdout, "Device rewound, status = 0x%x\n",
131                     scmd.uscsi_status);
132         } else if (strcmp(command, "errlev") == 0) {
133                 if (argc != 4) {
134                         usage(progname);
135                         /*NOTREACHED*/
136                 } else if ((level = atoi(value)) == 0) {
137                         fprintf(stderr, "Bad error level %s\n", value);
138                         exit(1);
139                 }
140
141                 if (ioctl(fd, SSTIOC_ERRLEV, &level) == -1) {
142                         perror("Set error level ioctl");
143                         (void) close(fd);
144                         exit(1);
145                         /*NOTREACHED*/
146                 }
147         } else if (strcmp(command, "tur") == 0) {
148                 if (ioctl(fd, SSTIOC_READY, NULL) == -1) {
149                         perror("Ready ioctl");
150                         (void) close(fd);
151                         exit(1);
152                         /*NOTREACHED*/
153                 }
154                 fprintf(stdout, "Device is ready\n");
155         } else {
156                 fprintf(stderr, "Unknown command: %s\n", command);
157                 usage(progname);
158                 /*NOTREACHED*/
159         }
160
161         (void) close(fd);
162         exit(0);
163 }
164
165 static void
166 usage(char *name)
167 {
168         (void) fprintf(stderr, "Usage: %s: [device] [command]\n", name);
169         (void) fprintf(stderr, "Device is the full path name of device\n");
170         (void) fprintf(stderr, "Command is one of:\n");
171         (void) fprintf(stderr, "\topen - just open & close\n");
172         (void) fprintf(stderr, "\tread - read a block\n");
173         (void) fprintf(stderr, "\twrite - write a block\n");
174         (void) fprintf(stderr, "\trew - send the SCSI 'rewind' command\n");
175         (void) fprintf(stderr, "\terrlev [lev] - Set the error "
176             "reporting level\n");
177         (void) fprintf(stderr, "\ttur - send the SCSI 'test unit ready'"
178             "command\n");
179         exit(1);
180         /*NOTREACHED*/
181 }