1*44704f69SBart Van Assche /*
2*44704f69SBart Van Assche * Copyright (c) 2013-2022 Douglas Gilbert.
3*44704f69SBart Van Assche * All rights reserved.
4*44704f69SBart Van Assche *
5*44704f69SBart Van Assche * Redistribution and use in source and binary forms, with or without
6*44704f69SBart Van Assche * modification, are permitted provided that the following conditions
7*44704f69SBart Van Assche * are met:
8*44704f69SBart Van Assche * 1. Redistributions of source code must retain the above copyright
9*44704f69SBart Van Assche * notice, this list of conditions and the following disclaimer.
10*44704f69SBart Van Assche * 2. Redistributions in binary form must reproduce the above copyright
11*44704f69SBart Van Assche * notice, this list of conditions and the following disclaimer in the
12*44704f69SBart Van Assche * documentation and/or other materials provided with the distribution.
13*44704f69SBart Van Assche *
14*44704f69SBart Van Assche * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*44704f69SBart Van Assche * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*44704f69SBart Van Assche * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*44704f69SBart Van Assche * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*44704f69SBart Van Assche * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*44704f69SBart Van Assche * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*44704f69SBart Van Assche * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*44704f69SBart Van Assche * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*44704f69SBart Van Assche * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*44704f69SBart Van Assche * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*44704f69SBart Van Assche * SUCH DAMAGE.
25*44704f69SBart Van Assche *
26*44704f69SBart Van Assche * SPDX-License-Identifier: BSD-2-Clause
27*44704f69SBart Van Assche */
28*44704f69SBart Van Assche
29*44704f69SBart Van Assche #include <iostream>
30*44704f69SBart Van Assche #include <vector>
31*44704f69SBart Van Assche #include <system_error>
32*44704f69SBart Van Assche #include <thread>
33*44704f69SBart Van Assche #include <mutex>
34*44704f69SBart Van Assche #include <chrono>
35*44704f69SBart Van Assche
36*44704f69SBart Van Assche #include <unistd.h>
37*44704f69SBart Van Assche #include <fcntl.h>
38*44704f69SBart Van Assche #include <stdio.h>
39*44704f69SBart Van Assche #include <stdlib.h>
40*44704f69SBart Van Assche #include <string.h>
41*44704f69SBart Van Assche #include <errno.h>
42*44704f69SBart Van Assche #include <ctype.h>
43*44704f69SBart Van Assche #include <sys/ioctl.h>
44*44704f69SBart Van Assche #include <sys/types.h>
45*44704f69SBart Van Assche #include <sys/stat.h>
46*44704f69SBart Van Assche
47*44704f69SBart Van Assche #include "sg_lib.h"
48*44704f69SBart Van Assche #include "sg_pt.h"
49*44704f69SBart Van Assche #include "sg_unaligned.h"
50*44704f69SBart Van Assche
51*44704f69SBart Van Assche static const char * version_str = "1.11 20220425";
52*44704f69SBart Van Assche static const char * util_name = "sg_tst_excl3";
53*44704f69SBart Van Assche
54*44704f69SBart Van Assche /* This is a test program for checking O_EXCL on open() works. It uses
55*44704f69SBart Van Assche * multiple threads and can be run as multiple processes and attempts
56*44704f69SBart Van Assche * to "break" O_EXCL. The strategy is to open a device O_EXCL|O_NONBLOCK
57*44704f69SBart Van Assche * and do a double increment on a LB then close it from a single thread.
58*44704f69SBart Van Assche * the remaining threads open that device O_NONBLOCK and do a read and
59*44704f69SBart Van Assche * note if the number is odd. Assuming the count starts as an even
60*44704f69SBart Van Assche * (typically 0) then it should remain even. Odd instances
61*44704f69SBart Van Assche * are counted and reported at the end of the program, after all threads
62*44704f69SBart Van Assche * have completed.
63*44704f69SBart Van Assche *
64*44704f69SBart Van Assche * This is C++ code with some things from C++11 (e.g. threads) and was
65*44704f69SBart Van Assche * only just able to compile (when some things were reverted) with gcc/g++
66*44704f69SBart Van Assche * version 4.7.3 found in Ubuntu 13.04 . C++11 "feature complete" support
67*44704f69SBart Van Assche * was not available until g++ version 4.8.1 and that is found in Fedora
68*44704f69SBart Van Assche * 19 and Ubuntu 13.10 .
69*44704f69SBart Van Assche *
70*44704f69SBart Van Assche * The build uses various object files from the <sg3_utils>/lib directory
71*44704f69SBart Van Assche * which is assumed to be a sibling of this examples directory. Those
72*44704f69SBart Van Assche * object files in the lib directory can be built with:
73*44704f69SBart Van Assche * cd <sg3_utils> ; ./configure ; cd lib; make
74*44704f69SBart Van Assche * Then:
75*44704f69SBart Van Assche * cd ../testing
76*44704f69SBart Van Assche * make sg_tst_excl3
77*44704f69SBart Van Assche *
78*44704f69SBart Van Assche * BEWARE: this utility modifies a logical block (default LBA 1000) on the
79*44704f69SBart Van Assche * given device.
80*44704f69SBart Van Assche *
81*44704f69SBart Van Assche */
82*44704f69SBart Van Assche
83*44704f69SBart Van Assche using namespace std;
84*44704f69SBart Van Assche using namespace std::chrono;
85*44704f69SBart Van Assche
86*44704f69SBart Van Assche #define DEF_NUM_PER_THREAD 200
87*44704f69SBart Van Assche #define DEF_NUM_THREADS 4
88*44704f69SBart Van Assche #define DEF_WAIT_MS 0 /* 0: yield; -1: don't wait; -2: sleep(0) */
89*44704f69SBart Van Assche
90*44704f69SBart Van Assche #define DEF_LBA 1000
91*44704f69SBart Van Assche
92*44704f69SBart Van Assche #define EBUFF_SZ 256
93*44704f69SBart Van Assche
94*44704f69SBart Van Assche
95*44704f69SBart Van Assche static mutex odd_count_mutex;
96*44704f69SBart Van Assche static mutex console_mutex;
97*44704f69SBart Van Assche static unsigned int odd_count;
98*44704f69SBart Van Assche static unsigned int ebusy_count;
99*44704f69SBart Van Assche
100*44704f69SBart Van Assche
101*44704f69SBart Van Assche static void
usage(void)102*44704f69SBart Van Assche usage(void)
103*44704f69SBart Van Assche {
104*44704f69SBart Van Assche printf("Usage: %s [-b] [-f] [-h] [-l <lba>] [-n <n_per_thr>]\n"
105*44704f69SBart Van Assche " [-R] [-t <num_thrs>] [-V] [-w <wait_ms>] "
106*44704f69SBart Van Assche "[-x]\n"
107*44704f69SBart Van Assche " <disk_device>\n", util_name);
108*44704f69SBart Van Assche printf(" where\n");
109*44704f69SBart Van Assche printf(" -b block on open (def: O_NONBLOCK)\n");
110*44704f69SBart Van Assche printf(" -f force: any SCSI disk (def: only "
111*44704f69SBart Van Assche "scsi_debug)\n");
112*44704f69SBart Van Assche printf(" WARNING: <lba> written to\n");
113*44704f69SBart Van Assche printf(" -h print this usage message then exit\n");
114*44704f69SBart Van Assche printf(" -l <lba> logical block to increment (def: %u)\n",
115*44704f69SBart Van Assche DEF_LBA);
116*44704f69SBart Van Assche printf(" -n <n_per_thr> number of loops per thread "
117*44704f69SBart Van Assche "(def: %d)\n", DEF_NUM_PER_THREAD);
118*44704f69SBart Van Assche printf(" -R all readers; so first thread (id=0) "
119*44704f69SBart Van Assche "just reads\n");
120*44704f69SBart Van Assche printf(" -t <num_thrs> number of threads (def: %d)\n",
121*44704f69SBart Van Assche DEF_NUM_THREADS);
122*44704f69SBart Van Assche printf(" -V print version number then exit\n");
123*44704f69SBart Van Assche printf(" -w <wait_ms> >0: sleep_for(<wait_ms>); =0: "
124*44704f69SBart Van Assche "yield(); -1: no\n"
125*44704f69SBart Van Assche " wait; -2: sleep(0) (def: %d)\n",
126*44704f69SBart Van Assche DEF_WAIT_MS);
127*44704f69SBart Van Assche printf(" -x don't use O_EXCL on first thread "
128*44704f69SBart Van Assche "(def: use\n"
129*44704f69SBart Van Assche " O_EXCL on first thread)\n\n");
130*44704f69SBart Van Assche printf("Test O_EXCL open flag with pass-through drivers. First thread "
131*44704f69SBart Van Assche "(id=0) does\nopen/close cycle with the O_EXCL flag then does a "
132*44704f69SBart Van Assche "double increment on\nlba (using its first 4 bytes). Remaining "
133*44704f69SBart Van Assche "theads read (without\nO_EXCL flag on open) and check the "
134*44704f69SBart Van Assche "value is even.\n");
135*44704f69SBart Van Assche }
136*44704f69SBart Van Assche
137*44704f69SBart Van Assche /* Assumed a lock (mutex) held when pt_err() is called */
138*44704f69SBart Van Assche static int
pt_err(int res)139*44704f69SBart Van Assche pt_err(int res)
140*44704f69SBart Van Assche {
141*44704f69SBart Van Assche if (res < 0)
142*44704f69SBart Van Assche fprintf(stderr, " pass through os error: %s\n", safe_strerror(-res));
143*44704f69SBart Van Assche else if (SCSI_PT_DO_BAD_PARAMS == res)
144*44704f69SBart Van Assche fprintf(stderr, " bad pass through setup\n");
145*44704f69SBart Van Assche else if (SCSI_PT_DO_TIMEOUT == res)
146*44704f69SBart Van Assche fprintf(stderr, " pass through timeout\n");
147*44704f69SBart Van Assche else
148*44704f69SBart Van Assche fprintf(stderr, " do_scsi_pt error=%d\n", res);
149*44704f69SBart Van Assche return -1;
150*44704f69SBart Van Assche }
151*44704f69SBart Van Assche
152*44704f69SBart Van Assche /* Assumed a lock (mutex) held when pt_cat_no_good() is called */
153*44704f69SBart Van Assche static int
pt_cat_no_good(int cat,struct sg_pt_base * ptp,const unsigned char * sbp)154*44704f69SBart Van Assche pt_cat_no_good(int cat, struct sg_pt_base * ptp, const unsigned char * sbp)
155*44704f69SBart Van Assche {
156*44704f69SBart Van Assche int slen;
157*44704f69SBart Van Assche char b[256];
158*44704f69SBart Van Assche const int bl = (int)sizeof(b);
159*44704f69SBart Van Assche
160*44704f69SBart Van Assche switch (cat) {
161*44704f69SBart Van Assche case SCSI_PT_RESULT_STATUS: /* other than GOOD and CHECK CONDITION */
162*44704f69SBart Van Assche sg_get_scsi_status_str(get_scsi_pt_status_response(ptp), bl, b);
163*44704f69SBart Van Assche fprintf(stderr, " scsi status: %s\n", b);
164*44704f69SBart Van Assche break;
165*44704f69SBart Van Assche case SCSI_PT_RESULT_SENSE:
166*44704f69SBart Van Assche slen = get_scsi_pt_sense_len(ptp);
167*44704f69SBart Van Assche sg_get_sense_str("", sbp, slen, 1, bl, b);
168*44704f69SBart Van Assche fprintf(stderr, "%s", b);
169*44704f69SBart Van Assche break;
170*44704f69SBart Van Assche case SCSI_PT_RESULT_TRANSPORT_ERR:
171*44704f69SBart Van Assche get_scsi_pt_transport_err_str(ptp, bl, b);
172*44704f69SBart Van Assche fprintf(stderr, " transport: %s", b);
173*44704f69SBart Van Assche break;
174*44704f69SBart Van Assche case SCSI_PT_RESULT_OS_ERR:
175*44704f69SBart Van Assche get_scsi_pt_os_err_str(ptp, bl, b);
176*44704f69SBart Van Assche fprintf(stderr, " os: %s", b);
177*44704f69SBart Van Assche break;
178*44704f69SBart Van Assche default:
179*44704f69SBart Van Assche fprintf(stderr, " unknown pt result category (%d)\n", cat);
180*44704f69SBart Van Assche break;
181*44704f69SBart Van Assche }
182*44704f69SBart Van Assche return -1;
183*44704f69SBart Van Assche }
184*44704f69SBart Van Assche
185*44704f69SBart Van Assche #define READ16_REPLY_LEN 512
186*44704f69SBart Van Assche #define READ16_CMD_LEN 16
187*44704f69SBart Van Assche #define WRITE16_REPLY_LEN 512
188*44704f69SBart Van Assche #define WRITE16_CMD_LEN 16
189*44704f69SBart Van Assche
190*44704f69SBart Van Assche /* Opens dev_name and spins if busy (i.e. gets EBUSY), sleeping for
191*44704f69SBart Van Assche * wait_ms milliseconds if wait_ms is positive. Reads lba and treats the
192*44704f69SBart Van Assche * first 4 bytes as an int (SCSI endian), increments it and writes it back.
193*44704f69SBart Van Assche * Repeats so that happens twice. Then closes dev_name. If an error occurs
194*44704f69SBart Van Assche * returns -1 else returns 0 if first int read is even otherwise returns 1. */
195*44704f69SBart Van Assche static int
do_rd_inc_wr_twice(const char * dev_name,int read_only,unsigned int lba,int block,int excl,int wait_ms,unsigned int & ebusys)196*44704f69SBart Van Assche do_rd_inc_wr_twice(const char * dev_name, int read_only, unsigned int lba,
197*44704f69SBart Van Assche int block, int excl, int wait_ms, unsigned int & ebusys)
198*44704f69SBart Van Assche {
199*44704f69SBart Van Assche int k, sg_fd, res, cat;
200*44704f69SBart Van Assche int odd = 0;
201*44704f69SBart Van Assche unsigned int u = 0;
202*44704f69SBart Van Assche struct sg_pt_base * ptp = NULL;
203*44704f69SBart Van Assche unsigned char r16CmdBlk [READ16_CMD_LEN] =
204*44704f69SBart Van Assche {0x88, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
205*44704f69SBart Van Assche unsigned char w16CmdBlk [WRITE16_CMD_LEN] =
206*44704f69SBart Van Assche {0x8a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
207*44704f69SBart Van Assche unsigned char sense_buffer[64] SG_C_CPP_ZERO_INIT;
208*44704f69SBart Van Assche unsigned char lb[READ16_REPLY_LEN];
209*44704f69SBart Van Assche char ebuff[EBUFF_SZ];
210*44704f69SBart Van Assche int open_flags = O_RDWR;
211*44704f69SBart Van Assche
212*44704f69SBart Van Assche sg_put_unaligned_be64(lba, r16CmdBlk + 2);
213*44704f69SBart Van Assche sg_put_unaligned_be64(lba, w16CmdBlk + 2);
214*44704f69SBart Van Assche if (! block)
215*44704f69SBart Van Assche open_flags |= O_NONBLOCK;
216*44704f69SBart Van Assche if (excl)
217*44704f69SBart Van Assche open_flags |= O_EXCL;
218*44704f69SBart Van Assche
219*44704f69SBart Van Assche while (((sg_fd = scsi_pt_open_flags(dev_name, open_flags, 0)) < 0) &&
220*44704f69SBart Van Assche (-EBUSY == sg_fd)) {
221*44704f69SBart Van Assche ++ebusys;
222*44704f69SBart Van Assche if (wait_ms > 0)
223*44704f69SBart Van Assche this_thread::sleep_for(milliseconds{wait_ms});
224*44704f69SBart Van Assche else if (0 == wait_ms)
225*44704f69SBart Van Assche this_thread::yield(); // thread yield
226*44704f69SBart Van Assche else if (-2 == wait_ms)
227*44704f69SBart Van Assche sleep(0); // process yield ??
228*44704f69SBart Van Assche }
229*44704f69SBart Van Assche if (sg_fd < 0) {
230*44704f69SBart Van Assche snprintf(ebuff, EBUFF_SZ,
231*44704f69SBart Van Assche "do_rd_inc_wr_twice: error opening file: %s", dev_name);
232*44704f69SBart Van Assche {
233*44704f69SBart Van Assche lock_guard<mutex> lg(console_mutex);
234*44704f69SBart Van Assche
235*44704f69SBart Van Assche perror(ebuff);
236*44704f69SBart Van Assche }
237*44704f69SBart Van Assche return -1;
238*44704f69SBart Van Assche }
239*44704f69SBart Van Assche
240*44704f69SBart Van Assche ptp = construct_scsi_pt_obj();
241*44704f69SBart Van Assche for (k = 0; k < 2; ++k) {
242*44704f69SBart Van Assche /* Prepare READ_16 command */
243*44704f69SBart Van Assche clear_scsi_pt_obj(ptp);
244*44704f69SBart Van Assche set_scsi_pt_cdb(ptp, r16CmdBlk, sizeof(r16CmdBlk));
245*44704f69SBart Van Assche set_scsi_pt_sense(ptp, sense_buffer, sizeof(sense_buffer));
246*44704f69SBart Van Assche set_scsi_pt_data_in(ptp, lb, READ16_REPLY_LEN);
247*44704f69SBart Van Assche res = do_scsi_pt(ptp, sg_fd, 20 /* secs timeout */, 1);
248*44704f69SBart Van Assche if (res) {
249*44704f69SBart Van Assche {
250*44704f69SBart Van Assche lock_guard<mutex> lg(console_mutex);
251*44704f69SBart Van Assche
252*44704f69SBart Van Assche fprintf(stderr, "READ_16 do_scsi_pt() submission error\n");
253*44704f69SBart Van Assche res = pt_err(res);
254*44704f69SBart Van Assche }
255*44704f69SBart Van Assche goto err;
256*44704f69SBart Van Assche }
257*44704f69SBart Van Assche cat = get_scsi_pt_result_category(ptp);
258*44704f69SBart Van Assche if (SCSI_PT_RESULT_GOOD != cat) {
259*44704f69SBart Van Assche {
260*44704f69SBart Van Assche lock_guard<mutex> lg(console_mutex);
261*44704f69SBart Van Assche
262*44704f69SBart Van Assche fprintf(stderr, "READ_16 do_scsi_pt() category problem\n");
263*44704f69SBart Van Assche res = pt_cat_no_good(cat, ptp, sense_buffer);
264*44704f69SBart Van Assche }
265*44704f69SBart Van Assche goto err;
266*44704f69SBart Van Assche }
267*44704f69SBart Van Assche
268*44704f69SBart Van Assche u = sg_get_unaligned_be32(lb);
269*44704f69SBart Van Assche // Assuming u starts test as even (probably 0), expect it to stay even
270*44704f69SBart Van Assche if (0 == k)
271*44704f69SBart Van Assche odd = (1 == (u % 2));
272*44704f69SBart Van Assche
273*44704f69SBart Van Assche if (wait_ms > 0) /* allow daylight for bad things ... */
274*44704f69SBart Van Assche this_thread::sleep_for(milliseconds{wait_ms});
275*44704f69SBart Van Assche else if (0 == wait_ms)
276*44704f69SBart Van Assche this_thread::yield(); // thread yield
277*44704f69SBart Van Assche else if (-2 == wait_ms)
278*44704f69SBart Van Assche sleep(0); // process yield ??
279*44704f69SBart Van Assche
280*44704f69SBart Van Assche if (read_only)
281*44704f69SBart Van Assche break;
282*44704f69SBart Van Assche ++u;
283*44704f69SBart Van Assche sg_put_unaligned_be32(u, lb);
284*44704f69SBart Van Assche
285*44704f69SBart Van Assche /* Prepare WRITE_16 command */
286*44704f69SBart Van Assche clear_scsi_pt_obj(ptp);
287*44704f69SBart Van Assche set_scsi_pt_cdb(ptp, w16CmdBlk, sizeof(w16CmdBlk));
288*44704f69SBart Van Assche set_scsi_pt_sense(ptp, sense_buffer, sizeof(sense_buffer));
289*44704f69SBart Van Assche set_scsi_pt_data_out(ptp, lb, WRITE16_REPLY_LEN);
290*44704f69SBart Van Assche res = do_scsi_pt(ptp, sg_fd, 20 /* secs timeout */, 1);
291*44704f69SBart Van Assche if (res) {
292*44704f69SBart Van Assche {
293*44704f69SBart Van Assche lock_guard<mutex> lg(console_mutex);
294*44704f69SBart Van Assche
295*44704f69SBart Van Assche fprintf(stderr, "WRITE_16 do_scsi_pt() submission error\n");
296*44704f69SBart Van Assche res = pt_err(res);
297*44704f69SBart Van Assche }
298*44704f69SBart Van Assche goto err;
299*44704f69SBart Van Assche }
300*44704f69SBart Van Assche cat = get_scsi_pt_result_category(ptp);
301*44704f69SBart Van Assche if (SCSI_PT_RESULT_GOOD != cat) {
302*44704f69SBart Van Assche {
303*44704f69SBart Van Assche lock_guard<mutex> lg(console_mutex);
304*44704f69SBart Van Assche
305*44704f69SBart Van Assche fprintf(stderr, "WRITE_16 do_scsi_pt() category problem\n");
306*44704f69SBart Van Assche res = pt_cat_no_good(cat, ptp, sense_buffer);
307*44704f69SBart Van Assche }
308*44704f69SBart Van Assche goto err;
309*44704f69SBart Van Assche }
310*44704f69SBart Van Assche }
311*44704f69SBart Van Assche err:
312*44704f69SBart Van Assche if (ptp)
313*44704f69SBart Van Assche destruct_scsi_pt_obj(ptp);
314*44704f69SBart Van Assche scsi_pt_close_device(sg_fd);
315*44704f69SBart Van Assche return odd;
316*44704f69SBart Van Assche }
317*44704f69SBart Van Assche
318*44704f69SBart Van Assche
319*44704f69SBart Van Assche #define INQ_REPLY_LEN 96
320*44704f69SBart Van Assche #define INQ_CMD_LEN 6
321*44704f69SBart Van Assche
322*44704f69SBart Van Assche /* Send INQUIRY and fetches response. If okay puts PRODUCT ID field
323*44704f69SBart Van Assche * in b (up to m_blen bytes). Does not use O_EXCL flag. Returns 0 on success,
324*44704f69SBart Van Assche * else -1 . */
325*44704f69SBart Van Assche static int
do_inquiry_prod_id(const char * dev_name,int block,int wait_ms,unsigned int & ebusys,char * b,int b_mlen)326*44704f69SBart Van Assche do_inquiry_prod_id(const char * dev_name, int block, int wait_ms,
327*44704f69SBart Van Assche unsigned int & ebusys, char * b, int b_mlen)
328*44704f69SBart Van Assche {
329*44704f69SBart Van Assche int sg_fd, res, cat;
330*44704f69SBart Van Assche struct sg_pt_base * ptp = NULL;
331*44704f69SBart Van Assche unsigned char inqCmdBlk [INQ_CMD_LEN] =
332*44704f69SBart Van Assche {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
333*44704f69SBart Van Assche unsigned char inqBuff[INQ_REPLY_LEN];
334*44704f69SBart Van Assche unsigned char sense_buffer[64] SG_C_CPP_ZERO_INIT;
335*44704f69SBart Van Assche char ebuff[EBUFF_SZ];
336*44704f69SBart Van Assche int open_flags = O_RDWR; /* since O_EXCL | O_RDONLY gives EPERM */
337*44704f69SBart Van Assche
338*44704f69SBart Van Assche if (! block)
339*44704f69SBart Van Assche open_flags |= O_NONBLOCK;
340*44704f69SBart Van Assche while (((sg_fd = scsi_pt_open_flags(dev_name, open_flags, 0)) < 0) &&
341*44704f69SBart Van Assche (-EBUSY == sg_fd)) {
342*44704f69SBart Van Assche ++ebusys;
343*44704f69SBart Van Assche if (wait_ms > 0)
344*44704f69SBart Van Assche this_thread::sleep_for(milliseconds{wait_ms});
345*44704f69SBart Van Assche else if (0 == wait_ms)
346*44704f69SBart Van Assche this_thread::yield(); // thread yield
347*44704f69SBart Van Assche else if (-2 == wait_ms)
348*44704f69SBart Van Assche sleep(0); // process yield ??
349*44704f69SBart Van Assche }
350*44704f69SBart Van Assche if (sg_fd < 0) {
351*44704f69SBart Van Assche snprintf(ebuff, EBUFF_SZ,
352*44704f69SBart Van Assche "do_inquiry_prod_id: error opening file: %s", dev_name);
353*44704f69SBart Van Assche perror(ebuff);
354*44704f69SBart Van Assche return -1;
355*44704f69SBart Van Assche }
356*44704f69SBart Van Assche /* Prepare INQUIRY command */
357*44704f69SBart Van Assche ptp = construct_scsi_pt_obj();
358*44704f69SBart Van Assche clear_scsi_pt_obj(ptp);
359*44704f69SBart Van Assche set_scsi_pt_cdb(ptp, inqCmdBlk, sizeof(inqCmdBlk));
360*44704f69SBart Van Assche set_scsi_pt_sense(ptp, sense_buffer, sizeof(sense_buffer));
361*44704f69SBart Van Assche set_scsi_pt_data_in(ptp, inqBuff, INQ_REPLY_LEN);
362*44704f69SBart Van Assche res = do_scsi_pt(ptp, sg_fd, 20 /* secs timeout */, 1);
363*44704f69SBart Van Assche if (res) {
364*44704f69SBart Van Assche fprintf(stderr, "INQUIRY do_scsi_pt() submission error\n");
365*44704f69SBart Van Assche res = pt_err(res);
366*44704f69SBart Van Assche goto err;
367*44704f69SBart Van Assche }
368*44704f69SBart Van Assche cat = get_scsi_pt_result_category(ptp);
369*44704f69SBart Van Assche if (SCSI_PT_RESULT_GOOD != cat) {
370*44704f69SBart Van Assche fprintf(stderr, "INQUIRY do_scsi_pt() category problem\n");
371*44704f69SBart Van Assche res = pt_cat_no_good(cat, ptp, sense_buffer);
372*44704f69SBart Van Assche goto err;
373*44704f69SBart Van Assche }
374*44704f69SBart Van Assche
375*44704f69SBart Van Assche /* Good, so fetch Product ID from response, copy to 'b' */
376*44704f69SBart Van Assche if (b_mlen > 0) {
377*44704f69SBart Van Assche if (b_mlen > 16) {
378*44704f69SBart Van Assche memcpy(b, inqBuff + 16, 16);
379*44704f69SBart Van Assche b[16] = '\0';
380*44704f69SBart Van Assche } else {
381*44704f69SBart Van Assche memcpy(b, inqBuff + 16, b_mlen - 1);
382*44704f69SBart Van Assche b[b_mlen - 1] = '\0';
383*44704f69SBart Van Assche }
384*44704f69SBart Van Assche }
385*44704f69SBart Van Assche err:
386*44704f69SBart Van Assche if (ptp)
387*44704f69SBart Van Assche destruct_scsi_pt_obj(ptp);
388*44704f69SBart Van Assche close(sg_fd);
389*44704f69SBart Van Assche return res;
390*44704f69SBart Van Assche }
391*44704f69SBart Van Assche
392*44704f69SBart Van Assche static void
work_thread(const char * dev_name,unsigned int lba,int id,int block,int excl,bool all_readers,int num,int wait_ms)393*44704f69SBart Van Assche work_thread(const char * dev_name, unsigned int lba, int id, int block,
394*44704f69SBart Van Assche int excl, bool all_readers, int num, int wait_ms)
395*44704f69SBart Van Assche {
396*44704f69SBart Van Assche unsigned int thr_odd_count = 0;
397*44704f69SBart Van Assche unsigned int thr_ebusy_count = 0;
398*44704f69SBart Van Assche int k, res;
399*44704f69SBart Van Assche int reader = ((id > 0) || (all_readers));
400*44704f69SBart Van Assche
401*44704f69SBart Van Assche {
402*44704f69SBart Van Assche lock_guard<mutex> lg(console_mutex);
403*44704f69SBart Van Assche
404*44704f69SBart Van Assche cerr << "Enter work_thread id=" << id << " excl=" << excl << " block="
405*44704f69SBart Van Assche << block << " reader=" << reader << endl;
406*44704f69SBart Van Assche }
407*44704f69SBart Van Assche for (k = 0; k < num; ++k) {
408*44704f69SBart Van Assche res = do_rd_inc_wr_twice(dev_name, reader, lba, block, excl,
409*44704f69SBart Van Assche wait_ms, thr_ebusy_count);
410*44704f69SBart Van Assche if (res < 0)
411*44704f69SBart Van Assche break;
412*44704f69SBart Van Assche if (res)
413*44704f69SBart Van Assche ++thr_odd_count;
414*44704f69SBart Van Assche }
415*44704f69SBart Van Assche {
416*44704f69SBart Van Assche lock_guard<mutex> lg(console_mutex);
417*44704f69SBart Van Assche
418*44704f69SBart Van Assche if (k < num)
419*44704f69SBart Van Assche cerr << "thread id=" << id << " FAILed at iteration: " << k
420*44704f69SBart Van Assche << '\n';
421*44704f69SBart Van Assche else
422*44704f69SBart Van Assche cerr << "thread id=" << id << " normal exit" << '\n';
423*44704f69SBart Van Assche }
424*44704f69SBart Van Assche
425*44704f69SBart Van Assche {
426*44704f69SBart Van Assche lock_guard<mutex> lg(odd_count_mutex);
427*44704f69SBart Van Assche
428*44704f69SBart Van Assche odd_count += thr_odd_count;
429*44704f69SBart Van Assche ebusy_count += thr_ebusy_count;
430*44704f69SBart Van Assche }
431*44704f69SBart Van Assche }
432*44704f69SBart Van Assche
433*44704f69SBart Van Assche
434*44704f69SBart Van Assche int
main(int argc,char * argv[])435*44704f69SBart Van Assche main(int argc, char * argv[])
436*44704f69SBart Van Assche {
437*44704f69SBart Van Assche int k, res;
438*44704f69SBart Van Assche int block = 0;
439*44704f69SBart Van Assche int force = 0;
440*44704f69SBart Van Assche unsigned int lba = DEF_LBA;
441*44704f69SBart Van Assche int num_per_thread = DEF_NUM_PER_THREAD;
442*44704f69SBart Van Assche bool all_readers = false;
443*44704f69SBart Van Assche int num_threads = DEF_NUM_THREADS;
444*44704f69SBart Van Assche int wait_ms = DEF_WAIT_MS;
445*44704f69SBart Van Assche int exclude_o_excl = 0;
446*44704f69SBart Van Assche char * dev_name = NULL;
447*44704f69SBart Van Assche char b[64];
448*44704f69SBart Van Assche
449*44704f69SBart Van Assche for (k = 1; k < argc; ++k) {
450*44704f69SBart Van Assche if (0 == memcmp("-b", argv[k], 2))
451*44704f69SBart Van Assche ++block;
452*44704f69SBart Van Assche else if (0 == memcmp("-f", argv[k], 2))
453*44704f69SBart Van Assche ++force;
454*44704f69SBart Van Assche else if (0 == memcmp("-h", argv[k], 2)) {
455*44704f69SBart Van Assche usage();
456*44704f69SBart Van Assche return 0;
457*44704f69SBart Van Assche } else if (0 == memcmp("-l", argv[k], 2)) {
458*44704f69SBart Van Assche ++k;
459*44704f69SBart Van Assche if ((k < argc) && isdigit(*argv[k]))
460*44704f69SBart Van Assche lba = (unsigned int)atoi(argv[k]);
461*44704f69SBart Van Assche else
462*44704f69SBart Van Assche break;
463*44704f69SBart Van Assche } else if (0 == memcmp("-n", argv[k], 2)) {
464*44704f69SBart Van Assche ++k;
465*44704f69SBart Van Assche if ((k < argc) && isdigit(*argv[k]))
466*44704f69SBart Van Assche num_per_thread = atoi(argv[k]);
467*44704f69SBart Van Assche else
468*44704f69SBart Van Assche break;
469*44704f69SBart Van Assche } else if (0 == memcmp("-t", argv[k], 2)) {
470*44704f69SBart Van Assche ++k;
471*44704f69SBart Van Assche if ((k < argc) && isdigit(*argv[k]))
472*44704f69SBart Van Assche num_threads = atoi(argv[k]);
473*44704f69SBart Van Assche else
474*44704f69SBart Van Assche break;
475*44704f69SBart Van Assche } else if (0 == memcmp("-R", argv[k], 2))
476*44704f69SBart Van Assche all_readers = true;
477*44704f69SBart Van Assche else if (0 == memcmp("-V", argv[k], 2)) {
478*44704f69SBart Van Assche printf("%s version: %s\n", util_name, version_str);
479*44704f69SBart Van Assche return 0;
480*44704f69SBart Van Assche } else if (0 == memcmp("-w", argv[k], 2)) {
481*44704f69SBart Van Assche ++k;
482*44704f69SBart Van Assche if ((k < argc) && (isdigit(*argv[k]) || ('-' == *argv[k]))) {
483*44704f69SBart Van Assche if ('-' == *argv[k])
484*44704f69SBart Van Assche wait_ms = - atoi(argv[k] + 1);
485*44704f69SBart Van Assche else
486*44704f69SBart Van Assche wait_ms = atoi(argv[k]);
487*44704f69SBart Van Assche } else
488*44704f69SBart Van Assche break;
489*44704f69SBart Van Assche } else if (0 == memcmp("-x", argv[k], 2))
490*44704f69SBart Van Assche ++exclude_o_excl;
491*44704f69SBart Van Assche else if (*argv[k] == '-') {
492*44704f69SBart Van Assche printf("Unrecognized switch: %s\n", argv[k]);
493*44704f69SBart Van Assche dev_name = NULL;
494*44704f69SBart Van Assche break;
495*44704f69SBart Van Assche }
496*44704f69SBart Van Assche else if (! dev_name)
497*44704f69SBart Van Assche dev_name = argv[k];
498*44704f69SBart Van Assche else {
499*44704f69SBart Van Assche printf("too many arguments\n");
500*44704f69SBart Van Assche dev_name = 0;
501*44704f69SBart Van Assche break;
502*44704f69SBart Van Assche }
503*44704f69SBart Van Assche }
504*44704f69SBart Van Assche if (0 == dev_name) {
505*44704f69SBart Van Assche usage();
506*44704f69SBart Van Assche return 1;
507*44704f69SBart Van Assche }
508*44704f69SBart Van Assche try {
509*44704f69SBart Van Assche
510*44704f69SBart Van Assche if (! force) {
511*44704f69SBart Van Assche res = do_inquiry_prod_id(dev_name, block, wait_ms, ebusy_count,
512*44704f69SBart Van Assche b, sizeof(b));
513*44704f69SBart Van Assche if (res) {
514*44704f69SBart Van Assche fprintf(stderr, "INQUIRY failed on %s\n", dev_name);
515*44704f69SBart Van Assche return 1;
516*44704f69SBart Van Assche }
517*44704f69SBart Van Assche // For safety, since <lba> written to, only permit scsi_debug
518*44704f69SBart Van Assche // devices. Bypass this with '-f' option.
519*44704f69SBart Van Assche if (0 != memcmp("scsi_debug", b, 10)) {
520*44704f69SBart Van Assche fprintf(stderr, "Since this utility writes to LBA %d, only "
521*44704f69SBart Van Assche "devices with scsi_debug\nproduct ID accepted.\n",
522*44704f69SBart Van Assche lba);
523*44704f69SBart Van Assche return 2;
524*44704f69SBart Van Assche }
525*44704f69SBart Van Assche }
526*44704f69SBart Van Assche
527*44704f69SBart Van Assche vector<thread *> vt;
528*44704f69SBart Van Assche
529*44704f69SBart Van Assche for (k = 0; k < num_threads; ++k) {
530*44704f69SBart Van Assche int excl = ((0 == k) && (! exclude_o_excl)) ? 1 : 0;
531*44704f69SBart Van Assche
532*44704f69SBart Van Assche thread * tp = new thread {work_thread, dev_name, lba, k, block,
533*44704f69SBart Van Assche excl, all_readers, num_per_thread,
534*44704f69SBart Van Assche wait_ms};
535*44704f69SBart Van Assche vt.push_back(tp);
536*44704f69SBart Van Assche }
537*44704f69SBart Van Assche
538*44704f69SBart Van Assche for (k = 0; k < (int)vt.size(); ++k)
539*44704f69SBart Van Assche vt[k]->join();
540*44704f69SBart Van Assche
541*44704f69SBart Van Assche for (k = 0; k < (int)vt.size(); ++k)
542*44704f69SBart Van Assche delete vt[k];
543*44704f69SBart Van Assche
544*44704f69SBart Van Assche cout << "Expecting odd count of 0, got " << odd_count << endl;
545*44704f69SBart Van Assche cout << "Number of EBUSYs: " << ebusy_count << endl;
546*44704f69SBart Van Assche
547*44704f69SBart Van Assche }
548*44704f69SBart Van Assche catch(system_error& e) {
549*44704f69SBart Van Assche cerr << "got a system_error exception: " << e.what() << '\n';
550*44704f69SBart Van Assche auto ec = e.code();
551*44704f69SBart Van Assche cerr << "category: " << ec.category().name() << '\n';
552*44704f69SBart Van Assche cerr << "value: " << ec.value() << '\n';
553*44704f69SBart Van Assche cerr << "message: " << ec.message() << '\n';
554*44704f69SBart Van Assche cerr << "\nNote: if g++ may need '-pthread' or similar in "
555*44704f69SBart Van Assche "compile/link line" << '\n';
556*44704f69SBart Van Assche }
557*44704f69SBart Van Assche catch(...) {
558*44704f69SBart Van Assche cerr << "got another exception: " << '\n';
559*44704f69SBart Van Assche }
560*44704f69SBart Van Assche return 0;
561*44704f69SBart Van Assche }
562