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