1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <sys/ioctl.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/mman.h>
11 #include "sg_lib.h"
12 #include "sg_io_linux.h"
13
14 /* This is a simple program executing a SCSI INQUIRY command and a
15 TEST UNIT READY command using the SCSI generic (sg) driver
16 This variant shows mmap-ed IO being used to read the data returned
17 by the INQUIRY command.
18
19 * Copyright (C) 2001-2016 D. Gilbert
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2, or (at your option)
23 * any later version.
24
25 Invocation: sg_simple4 [-x] <sg_device>
26
27 Version 1.02 (20160528)
28
29 6 byte INQUIRY command:
30 [0x12][ |lu][pg cde][res ][al len][cntrl ]
31
32 6 byte TEST UNIT READY command:
33 [0x00][ |lu][res ][res ][res ][res ]
34
35 */
36
37 #ifndef SG_FLAG_MMAP_IO
38 #define SG_FLAG_MMAP_IO 4
39 #endif /* since /usr/include/scsi/sg.h doesn't know about this yet */
40
41 #define INQ_REPLY_LEN 96
42 #define INQ_CMD_LEN 6
43 #define TUR_CMD_LEN 6
44
45 #define EBUFF_SZ 256
46
main(int argc,char * argv[])47 int main(int argc, char * argv[])
48 {
49 int sg_fd, k, ok;
50 unsigned char inq_cdb[INQ_CMD_LEN] =
51 {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
52 unsigned char tur_cdb[TUR_CMD_LEN] =
53 {0x00, 0, 0, 0, 0, 0};
54 unsigned char * inqBuff;
55 unsigned char * inqBuff2;
56 sg_io_hdr_t io_hdr;
57 char * file_name = 0;
58 char ebuff[EBUFF_SZ];
59 unsigned char sense_buffer[32];
60 int do_extra = 0;
61
62 for (k = 1; k < argc; ++k) {
63 if (0 == memcmp("-x", argv[k], 2))
64 do_extra = 1;
65 else if (*argv[k] == '-') {
66 printf("Unrecognized switch: %s\n", argv[k]);
67 file_name = 0;
68 break;
69 }
70 else if (0 == file_name)
71 file_name = argv[k];
72 else {
73 printf("too many arguments\n");
74 file_name = 0;
75 break;
76 }
77 }
78 if (0 == file_name) {
79 printf("Usage: 'sg_simple4 [-x] <sg_device>'\n");
80 return 1;
81 }
82
83 /* N.B. An access mode of O_RDWR is required for some SCSI commands */
84 if ((sg_fd = open(file_name, O_RDWR)) < 0) {
85 snprintf(ebuff, EBUFF_SZ,
86 "sg_simple4: error opening file: %s", file_name);
87 perror(ebuff);
88 return 1;
89 }
90 /* Just to be safe, check we have a new sg device by trying an ioctl */
91 if ((ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30122)) {
92 printf("sg_simple4: %s needs sg driver version >= 3.1.22\n",
93 file_name);
94 close(sg_fd);
95 return 1;
96 }
97
98 /* since I know this program will only read from inqBuff then I use
99 PROT_READ rather than PROT_READ | PROT_WRITE */
100 inqBuff = (unsigned char *)mmap(NULL, 8000, PROT_READ | PROT_WRITE,
101 MAP_SHARED, sg_fd, 0);
102 if (MAP_FAILED == inqBuff) {
103 snprintf(ebuff, EBUFF_SZ, "sg_simple4: error using mmap() on "
104 "file: %s", file_name);
105 perror(ebuff);
106 return 1;
107 }
108 if (inqBuff[0])
109 printf("non-null char at inqBuff[0]\n");
110 if (inqBuff[5000])
111 printf("non-null char at inqBuff[5000]\n");
112
113 /* Prepare INQUIRY command */
114 memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
115 io_hdr.interface_id = 'S';
116 io_hdr.cmd_len = sizeof(inq_cdb);
117 /* io_hdr.iovec_count = 0; */ /* memset takes care of this */
118 io_hdr.mx_sb_len = sizeof(sense_buffer);
119 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
120 io_hdr.dxfer_len = INQ_REPLY_LEN;
121 /* io_hdr.dxferp = inqBuff; // ignored in mmap-ed IO */
122 io_hdr.cmdp = inq_cdb;
123 io_hdr.sbp = sense_buffer;
124 io_hdr.timeout = 20000; /* 20000 millisecs == 20 seconds */
125 io_hdr.flags = SG_FLAG_MMAP_IO;
126 /* io_hdr.pack_id = 0; */
127 /* io_hdr.usr_ptr = NULL; */
128
129 if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
130 perror("sg_simple4: Inquiry SG_IO ioctl error");
131 close(sg_fd);
132 return 1;
133 }
134
135 /* now for the error processing */
136 ok = 0;
137 switch (sg_err_category3(&io_hdr)) {
138 case SG_LIB_CAT_CLEAN:
139 ok = 1;
140 break;
141 case SG_LIB_CAT_RECOVERED:
142 printf("Recovered error on INQUIRY, continuing\n");
143 ok = 1;
144 break;
145 default: /* won't bother decoding other categories */
146 sg_chk_n_print3("INQUIRY command error", &io_hdr, 1);
147 break;
148 }
149
150 if (ok) { /* output result if it is available */
151 char * p = (char *)inqBuff;
152 int f = (int)*(p + 7);
153 printf("Some of the INQUIRY command's results:\n");
154 printf(" %.8s %.16s %.4s ", p + 8, p + 16, p + 32);
155 printf("[wide=%d sync=%d cmdque=%d sftre=%d]\n",
156 !!(f & 0x20), !!(f & 0x10), !!(f & 2), !!(f & 1));
157 /* Extra info, not necessary to look at */
158 if (do_extra)
159 printf("INQUIRY duration=%u millisecs, resid=%d, msg_status=%d\n",
160 io_hdr.duration, io_hdr.resid, (int)io_hdr.msg_status);
161 }
162
163
164 /* Prepare TEST UNIT READY command */
165 memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
166 io_hdr.interface_id = 'S';
167 io_hdr.cmd_len = sizeof(tur_cdb);
168 io_hdr.mx_sb_len = sizeof(sense_buffer);
169 io_hdr.dxfer_direction = SG_DXFER_NONE;
170 io_hdr.cmdp = tur_cdb;
171 io_hdr.sbp = sense_buffer;
172 io_hdr.timeout = 20000; /* 20000 millisecs == 20 seconds */
173
174 if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
175 perror("sg_simple4: Test Unit Ready SG_IO ioctl error");
176 close(sg_fd);
177 return 1;
178 }
179
180 /* now for the error processing */
181 ok = 0;
182 switch (sg_err_category3(&io_hdr)) {
183 case SG_LIB_CAT_CLEAN:
184 ok = 1;
185 break;
186 case SG_LIB_CAT_RECOVERED:
187 printf("Recovered error on Test Unit Ready, continuing\n");
188 ok = 1;
189 break;
190 default: /* won't bother decoding other categories */
191 sg_chk_n_print3("Test Unit Ready command error", &io_hdr, 1);
192 break;
193 }
194
195 if (ok)
196 printf("Test Unit Ready successful so unit is ready!\n");
197 else
198 printf("Test Unit Ready failed so unit may _not_ be ready!\n");
199
200 if (do_extra)
201 printf("TEST UNIT READY duration=%u millisecs, resid=%d, "
202 "msg_status=%d\n",
203 io_hdr.duration, io_hdr.resid, (int)io_hdr.msg_status);
204
205 /* munmap(inqBuff, 8000); */
206 /* could call munmap(inqBuff, INQ_REPLY_LEN) here but following close()
207 causes this too happen anyway */
208 #if 1
209 inqBuff2 = (unsigned char *)mmap(NULL, 8000, PROT_READ | PROT_WRITE,
210 MAP_SHARED, sg_fd, 0);
211 if (MAP_FAILED == inqBuff2) {
212 snprintf(ebuff, EBUFF_SZ, "sg_simple4: error using mmap() 2 on "
213 "file: %s", file_name);
214 perror(ebuff);
215 return 1;
216 }
217 if (inqBuff2[0])
218 printf("non-null char at inqBuff2[0]\n");
219 if (inqBuff2[5000])
220 printf("non-null char at inqBuff2[5000]\n");
221 {
222 pid_t pid;
223 pid = fork();
224 if (pid) {
225 inqBuff2[5000] = 33;
226 munmap(inqBuff, 8000);
227 sleep(3);
228 }
229 else {
230 inqBuff[5000] = 0xaa;
231 munmap(inqBuff, 8000);
232 sleep(1);
233 }
234 }
235 #endif
236 close(sg_fd);
237 return 0;
238 }
239