xref: /aosp_15_r20/external/sg3_utils/examples/sg_excl.c (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1*44704f69SBart Van Assche /*
2*44704f69SBart Van Assche  * Copyright (C) 2003-2018 D. Gilbert
3*44704f69SBart Van Assche  * This program is free software; you can redistribute it and/or modify
4*44704f69SBart Van Assche  * it under the terms of the GNU General Public License as published by
5*44704f69SBart Van Assche  * the Free Software Foundation; either version 2, or (at your option)
6*44704f69SBart Van Assche  * any later version.
7*44704f69SBart Van Assche  *
8*44704f69SBart Van Assche  * SPDX-License-Identifier: GPL-2.0-or-later
9*44704f69SBart Van Assche  *
10*44704f69SBart Van Assche  * This is a simple program that tests the O_EXCL flag in sg while
11*44704f69SBart Van Assche  * executing a SCSI INQUIRY command and a
12*44704f69SBart Van Assche  * TEST UNIT READY command using the SCSI generic (sg) driver
13*44704f69SBart Van Assche  *
14*44704f69SBart Van Assche  * Invocation: sg_excl [-x] <sg_device>
15*44704f69SBart Van Assche  *
16*44704f69SBart Van Assche  * Version 3.62 (20181227)
17*44704f69SBart Van Assche  *
18*44704f69SBart Van Assche  * 6 byte INQUIRY command:
19*44704f69SBart Van Assche  * [0x12][   |lu][pg cde][res   ][al len][cntrl ]
20*44704f69SBart Van Assche  *
21*44704f69SBart Van Assche  * 6 byte TEST UNIT READY command:
22*44704f69SBart Van Assche  * [0x00][   |lu][res   ][res   ][res   ][res   ]
23*44704f69SBart Van Assche  *
24*44704f69SBart Van Assche  */
25*44704f69SBart Van Assche 
26*44704f69SBart Van Assche #include <unistd.h>
27*44704f69SBart Van Assche #include <fcntl.h>
28*44704f69SBart Van Assche #include <stdio.h>
29*44704f69SBart Van Assche #include <stdlib.h>
30*44704f69SBart Van Assche #include <stdint.h>
31*44704f69SBart Van Assche #include <string.h>
32*44704f69SBart Van Assche #include <errno.h>
33*44704f69SBart Van Assche #include <sys/ioctl.h>
34*44704f69SBart Van Assche #include <sys/types.h>
35*44704f69SBart Van Assche #include <sys/stat.h>
36*44704f69SBart Van Assche #include "sg_lib.h"
37*44704f69SBart Van Assche #include "sg_io_linux.h"
38*44704f69SBart Van Assche 
39*44704f69SBart Van Assche 
40*44704f69SBart Van Assche #define INQ_REPLY_LEN 96
41*44704f69SBart Van Assche #define INQ_CMD_LEN 6
42*44704f69SBart Van Assche #define TUR_CMD_LEN 6
43*44704f69SBart Van Assche 
44*44704f69SBart Van Assche #define EBUFF_SZ 256
45*44704f69SBart Van Assche 
46*44704f69SBart Van Assche #define ME "sg_excl: "
47*44704f69SBart Van Assche 
main(int argc,char * argv[])48*44704f69SBart Van Assche int main(int argc, char * argv[])
49*44704f69SBart Van Assche {
50*44704f69SBart Van Assche     int sg_fd, k, ok /*, sg_fd2 */;
51*44704f69SBart Van Assche     uint8_t inq_cdb [INQ_CMD_LEN] = {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
52*44704f69SBart Van Assche     uint8_t tur_cdb [TUR_CMD_LEN] = {0x00, 0, 0, 0, 0, 0};
53*44704f69SBart Van Assche     uint8_t inqBuff[INQ_REPLY_LEN];
54*44704f69SBart Van Assche     sg_io_hdr_t io_hdr;
55*44704f69SBart Van Assche     char * file_name = 0;
56*44704f69SBart Van Assche     char ebuff[EBUFF_SZ];
57*44704f69SBart Van Assche     uint8_t sense_buffer[32];
58*44704f69SBart Van Assche     int do_extra = 0;
59*44704f69SBart Van Assche 
60*44704f69SBart Van Assche     for (k = 1; k < argc; ++k) {
61*44704f69SBart Van Assche         if (0 == memcmp("-x", argv[k], 2))
62*44704f69SBart Van Assche             do_extra = 1;
63*44704f69SBart Van Assche         else if (*argv[k] == '-') {
64*44704f69SBart Van Assche             printf("Unrecognized switch: %s\n", argv[k]);
65*44704f69SBart Van Assche             file_name = 0;
66*44704f69SBart Van Assche             break;
67*44704f69SBart Van Assche         }
68*44704f69SBart Van Assche         else if (0 == file_name)
69*44704f69SBart Van Assche             file_name = argv[k];
70*44704f69SBart Van Assche         else {
71*44704f69SBart Van Assche             printf("too many arguments\n");
72*44704f69SBart Van Assche             file_name = 0;
73*44704f69SBart Van Assche             break;
74*44704f69SBart Van Assche         }
75*44704f69SBart Van Assche     }
76*44704f69SBart Van Assche     if (0 == file_name) {
77*44704f69SBart Van Assche         printf("Usage: 'sg_excl [-x] <sg_device>'\n");
78*44704f69SBart Van Assche         return 1;
79*44704f69SBart Van Assche     }
80*44704f69SBart Van Assche 
81*44704f69SBart Van Assche     /* N.B. An access mode of O_RDWR is required for some SCSI commands */
82*44704f69SBart Van Assche     if ((sg_fd = open(file_name, O_RDWR | O_EXCL | O_NONBLOCK)) < 0) {
83*44704f69SBart Van Assche         snprintf(ebuff, EBUFF_SZ, ME "error opening file: %s", file_name);
84*44704f69SBart Van Assche         perror(ebuff);
85*44704f69SBart Van Assche         return 1;
86*44704f69SBart Van Assche     }
87*44704f69SBart Van Assche     /* Just to be safe, check we have a new sg device by trying an ioctl */
88*44704f69SBart Van Assche     if ((ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
89*44704f69SBart Van Assche         printf(ME "%s doesn't seem to be an new sg device\n",
90*44704f69SBart Van Assche                file_name);
91*44704f69SBart Van Assche         close(sg_fd);
92*44704f69SBart Van Assche         return 1;
93*44704f69SBart Van Assche     }
94*44704f69SBart Van Assche #if 0
95*44704f69SBart Van Assche     if ((sg_fd2 = open(file_name, O_RDWR | O_EXCL)) < 0) {
96*44704f69SBart Van Assche         snprintf(ebuff, EBUFF_SZ,
97*44704f69SBart Van Assche                  ME "error opening file: %s a second time", file_name);
98*44704f69SBart Van Assche         perror(ebuff);
99*44704f69SBart Van Assche         return 1;
100*44704f69SBart Van Assche     } else {
101*44704f69SBart Van Assche         printf(ME "second open of %s in violation of O_EXCL\n", file_name);
102*44704f69SBart Van Assche         close(sg_fd2);
103*44704f69SBart Van Assche     }
104*44704f69SBart Van Assche #endif
105*44704f69SBart Van Assche 
106*44704f69SBart Van Assche     /* Prepare INQUIRY command */
107*44704f69SBart Van Assche     memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
108*44704f69SBart Van Assche     io_hdr.interface_id = 'S';
109*44704f69SBart Van Assche     io_hdr.cmd_len = sizeof(inq_cdb);
110*44704f69SBart Van Assche     /* io_hdr.iovec_count = 0; */  /* memset takes care of this */
111*44704f69SBart Van Assche     io_hdr.mx_sb_len = sizeof(sense_buffer);
112*44704f69SBart Van Assche     io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
113*44704f69SBart Van Assche     io_hdr.dxfer_len = INQ_REPLY_LEN;
114*44704f69SBart Van Assche     io_hdr.dxferp = inqBuff;
115*44704f69SBart Van Assche     io_hdr.cmdp = inq_cdb;
116*44704f69SBart Van Assche     io_hdr.sbp = sense_buffer;
117*44704f69SBart Van Assche     io_hdr.timeout = 20000;     /* 20000 millisecs == 20 seconds */
118*44704f69SBart Van Assche     /* io_hdr.flags = 0; */     /* take defaults: indirect IO, etc */
119*44704f69SBart Van Assche     /* io_hdr.pack_id = 0; */
120*44704f69SBart Van Assche     /* io_hdr.usr_ptr = NULL; */
121*44704f69SBart Van Assche 
122*44704f69SBart Van Assche     if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
123*44704f69SBart Van Assche         perror(ME "Inquiry SG_IO ioctl error");
124*44704f69SBart Van Assche         close(sg_fd);
125*44704f69SBart Van Assche         return 1;
126*44704f69SBart Van Assche     }
127*44704f69SBart Van Assche 
128*44704f69SBart Van Assche     /* now for the error processing */
129*44704f69SBart Van Assche     ok = 0;
130*44704f69SBart Van Assche     switch (sg_err_category3(&io_hdr)) {
131*44704f69SBart Van Assche     case SG_LIB_CAT_CLEAN:
132*44704f69SBart Van Assche         ok = 1;
133*44704f69SBart Van Assche         break;
134*44704f69SBart Van Assche     case SG_LIB_CAT_RECOVERED:
135*44704f69SBart Van Assche         printf("Recovered error on INQUIRY, continuing\n");
136*44704f69SBart Van Assche         ok = 1;
137*44704f69SBart Van Assche         break;
138*44704f69SBart Van Assche     default: /* won't bother decoding other categories */
139*44704f69SBart Van Assche         sg_chk_n_print3("INQUIRY command error", &io_hdr, 1);
140*44704f69SBart Van Assche         break;
141*44704f69SBart Van Assche     }
142*44704f69SBart Van Assche 
143*44704f69SBart Van Assche     if (ok) { /* output result if it is available */
144*44704f69SBart Van Assche         char * p = (char *)inqBuff;
145*44704f69SBart Van Assche         int f = (int)*(p + 7);
146*44704f69SBart Van Assche         printf("Some of the INQUIRY command's results:\n");
147*44704f69SBart Van Assche         printf("    %.8s  %.16s  %.4s  ", p + 8, p + 16, p + 32);
148*44704f69SBart Van Assche         printf("[wide=%d sync=%d cmdque=%d sftre=%d]\n",
149*44704f69SBart Van Assche                !!(f & 0x20), !!(f & 0x10), !!(f & 2), !!(f & 1));
150*44704f69SBart Van Assche         /* Extra info, not necessary to look at */
151*44704f69SBart Van Assche         if (do_extra)
152*44704f69SBart Van Assche             printf("INQUIRY duration=%u millisecs, resid=%d, msg_status=%d\n",
153*44704f69SBart Van Assche                    io_hdr.duration, io_hdr.resid, (int)io_hdr.msg_status);
154*44704f69SBart Van Assche     }
155*44704f69SBart Van Assche 
156*44704f69SBart Van Assche 
157*44704f69SBart Van Assche     /* Prepare TEST UNIT READY command */
158*44704f69SBart Van Assche     memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
159*44704f69SBart Van Assche     io_hdr.interface_id = 'S';
160*44704f69SBart Van Assche     io_hdr.cmd_len = sizeof(tur_cdb);
161*44704f69SBart Van Assche     io_hdr.mx_sb_len = sizeof(sense_buffer);
162*44704f69SBart Van Assche     io_hdr.dxfer_direction = SG_DXFER_NONE;
163*44704f69SBart Van Assche     io_hdr.cmdp = tur_cdb;
164*44704f69SBart Van Assche     io_hdr.sbp = sense_buffer;
165*44704f69SBart Van Assche     io_hdr.timeout = 20000;     /* 20000 millisecs == 20 seconds */
166*44704f69SBart Van Assche 
167*44704f69SBart Van Assche     if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
168*44704f69SBart Van Assche         perror(ME "Test Unit Ready SG_IO ioctl error");
169*44704f69SBart Van Assche         close(sg_fd);
170*44704f69SBart Van Assche         return 1;
171*44704f69SBart Van Assche     }
172*44704f69SBart Van Assche 
173*44704f69SBart Van Assche     /* now for the error processing */
174*44704f69SBart Van Assche     ok = 0;
175*44704f69SBart Van Assche     switch (sg_err_category3(&io_hdr)) {
176*44704f69SBart Van Assche     case SG_LIB_CAT_CLEAN:
177*44704f69SBart Van Assche         ok = 1;
178*44704f69SBart Van Assche         break;
179*44704f69SBart Van Assche     case SG_LIB_CAT_RECOVERED:
180*44704f69SBart Van Assche         printf("Recovered error on Test Unit Ready, continuing\n");
181*44704f69SBart Van Assche         ok = 1;
182*44704f69SBart Van Assche         break;
183*44704f69SBart Van Assche     default: /* won't bother decoding other categories */
184*44704f69SBart Van Assche         sg_chk_n_print3("Test Unit Ready command error", &io_hdr, 1);
185*44704f69SBart Van Assche         break;
186*44704f69SBart Van Assche     }
187*44704f69SBart Van Assche 
188*44704f69SBart Van Assche     if (ok)
189*44704f69SBart Van Assche         printf("Test Unit Ready successful so unit is ready!\n");
190*44704f69SBart Van Assche     else
191*44704f69SBart Van Assche         printf("Test Unit Ready failed so unit may _not_ be ready!\n");
192*44704f69SBart Van Assche 
193*44704f69SBart Van Assche     if (do_extra)
194*44704f69SBart Van Assche         printf("TEST UNIT READY duration=%u millisecs, resid=%d, "
195*44704f69SBart Van Assche                "msg_status=%d\n", io_hdr.duration, io_hdr.resid,
196*44704f69SBart Van Assche                 (int)io_hdr.msg_status);
197*44704f69SBart Van Assche 
198*44704f69SBart Van Assche     printf("Wait for 60 seconds with O_EXCL help on %s\n", file_name);
199*44704f69SBart Van Assche     sleep(60);
200*44704f69SBart Van Assche     close(sg_fd);
201*44704f69SBart Van Assche     return 0;
202*44704f69SBart Van Assche }
203