1 /*
2 * Copyright (c) 2018-2022 Douglas Gilbert.
3 * All rights reserved.
4 * Use of this source code is governed by a BSD-style
5 * license that can be found in the BSD_LICENSE file.
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 */
9
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdbool.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <getopt.h>
18 #include <errno.h>
19 #define __STDC_FORMAT_MACROS 1
20 #include <inttypes.h>
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "sg_lib.h"
27 #include "sg_lib_data.h"
28 #include "sg_pt.h"
29 #include "sg_cmds_basic.h"
30 #include "sg_unaligned.h"
31 #include "sg_pr2serr.h"
32
33 /*
34 * This program issues the SCSI STREAM CONTROL or GET STREAM STATUS command
35 * to the given SCSI device. Based on sbc4r15.pdf .
36 */
37
38 static const char * version_str = "1.13 20221028";
39
40 #define STREAM_CONTROL_SA 0x14
41 #define GET_STREAM_STATUS_SA 0x16
42
43 #define STREAM_CONTROL_OPEN 0x1
44 #define STREAM_CONTROL_CLOSE 0x2
45
46 #define SENSE_BUFF_LEN 64 /* Arbitrary, could be larger */
47 #define DEF_PT_TIMEOUT 60 /* 60 seconds */
48
49
50 static struct option long_options[] = {
51 {"brief", no_argument, 0, 'b'},
52 {"close", no_argument, 0, 'c'},
53 {"ctl", required_argument, 0, 'C'},
54 {"get", no_argument, 0, 'g'},
55 {"help", no_argument, 0, 'h'},
56 {"id", required_argument, 0, 'i'},
57 {"maxlen", required_argument, 0, 'm'},
58 {"open", no_argument, 0, 'o'},
59 {"readonly", no_argument, 0, 'r'},
60 {"verbose", no_argument, 0, 'v'},
61 {"version", no_argument, 0, 'V'},
62 {0, 0, 0, 0},
63 };
64
65
66 static void
usage()67 usage()
68 {
69 pr2serr("Usage: "
70 "sg_stream_ctl [-brief] [--close] [--ctl=CTL] [-get] [--help]\n"
71 " [--id=SID] [--maxlen=LEN] [--open] "
72 "[--readonly]\n"
73 " [--verbose] [--version] DEVICE\n");
74 pr2serr(" where:\n"
75 " --brief|-b for open, output assigned stream id to "
76 "stdout, or\n"
77 " -1 if error; for close, output 0, or "
78 "-1; for get\n"
79 " output list of stream id, 1 per line\n"
80 " --close|-c close stream given by --id=SID\n"
81 " --ctl=CTL|-C CTL CTL is stream control value, "
82 "(STR_CTL field)\n"
83 " 1 -> open; 2 -> close\n"
84 " --get|-g do GET STREAM STATUS command (default "
85 "if no other)\n"
86 " --help|-h print out usage message\n"
87 " --id=SID|-i SID for close, SID is stream_id to close; "
88 "for get,\n"
89 " list from and include this stream id\n"
90 " --maxlen=LEN|-m LEN length in bytes of buffer to "
91 "receive data-in\n"
92 " (def: 8 (for open and close); 252 "
93 "(for get,\n"
94 " but increase if needed)\n"
95 " --open|-o open a new stream, return assigned "
96 "stream id\n"
97 " --readonly|-r open DEVICE read-only (if supported)\n"
98 " --verbose|-v increase verbosity\n"
99 " --version|-V print version string and exit\n\n"
100 "Performs a SCSI STREAM CONTROL or GET STREAM STATUS command. "
101 "If --open,\n--close or --ctl=CTL given (only one) then "
102 "performs STREAM CONTROL\ncommand. If --get or no other "
103 "selecting option given then performs a\nGET STREAM STATUS "
104 "command. A successful --open will output the assigned\nstream "
105 "id to stdout (and ignore --id=SID , if given).\n"
106 );
107 }
108
109 /* Invokes a SCSI GET STREAM STATUS command (SBC-4). Return of 0 -> success,
110 * various SG_LIB_CAT_* positive values or -1 -> other errors */
111 static int
sg_ll_get_stream_status(int sg_fd,uint16_t s_str_id,uint8_t * resp,uint32_t alloc_len,int * residp,bool noisy,int verbose)112 sg_ll_get_stream_status(int sg_fd, uint16_t s_str_id, uint8_t * resp,
113 uint32_t alloc_len, int * residp, bool noisy,
114 int verbose)
115 {
116 int k, ret, res, sense_cat;
117 uint8_t gssCdb[16] = {SG_SERVICE_ACTION_IN_16,
118 GET_STREAM_STATUS_SA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
119 uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
120 struct sg_pt_base * ptvp;
121 static const char * const cmd_name = "Get stream status";
122
123 if (s_str_id) /* starting stream id, fetch from and including */
124 sg_put_unaligned_be16(s_str_id, gssCdb + 4);
125 sg_put_unaligned_be32(alloc_len, gssCdb + 10);
126 if (verbose) {
127 char b[128];
128
129 pr2serr(" %s cdb: %s\n", cmd_name,
130 sg_get_command_str(gssCdb, (int)sizeof(gssCdb), false,
131 sizeof(b), b));
132 }
133
134 ptvp = construct_scsi_pt_obj_with_fd(sg_fd, verbose);
135 if (NULL == ptvp) {
136 pr2serr("%s: out of memory\n", cmd_name);
137 return -1;
138 }
139 set_scsi_pt_cdb(ptvp, gssCdb, sizeof(gssCdb));
140 set_scsi_pt_data_in(ptvp, resp, alloc_len);
141 set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
142 res = do_scsi_pt(ptvp, -1, DEF_PT_TIMEOUT, verbose);
143 ret = sg_cmds_process_resp(ptvp, cmd_name, res, noisy, verbose,
144 &sense_cat);
145 if (-1 == ret) {
146 if (get_scsi_pt_transport_err(ptvp))
147 ret = SG_LIB_TRANSPORT_ERROR;
148 else
149 ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
150 } else if (-2 == ret) {
151 switch (sense_cat) {
152 case SG_LIB_CAT_RECOVERED:
153 case SG_LIB_CAT_NO_SENSE:
154 ret = 0;
155 break;
156 default:
157 ret = sense_cat;
158 break;
159 }
160 } else
161 ret = 0;
162 k = ret ? (int)alloc_len : get_scsi_pt_resid(ptvp);
163 if (residp)
164 *residp = k;
165 if ((verbose > 2) && ((alloc_len - k) > 0)) {
166 pr2serr("%s: parameter data returned:\n", cmd_name);
167 hex2stderr((const uint8_t *)resp, alloc_len - k,
168 ((verbose > 3) ? -1 : 1));
169 }
170 destruct_scsi_pt_obj(ptvp);
171 return ret;
172 }
173
174 /* Invokes a SCSI STREAM CONTROL command (SBC-4). Return of 0 -> success,
175 * various SG_LIB_CAT_* positive values or -1 -> other errors.
176 * N.B. The is a device modifying command that is SERVICE ACTION IN(16)
177 * command since it has data-in buffer that for open returns the
178 * ASSIGNED_STR_ID field . */
179 static int
sg_ll_stream_control(int sg_fd,uint32_t str_ctl,uint16_t str_id,uint8_t * resp,uint32_t alloc_len,int * residp,bool noisy,int verbose)180 sg_ll_stream_control(int sg_fd, uint32_t str_ctl, uint16_t str_id,
181 uint8_t * resp, uint32_t alloc_len, int * residp,
182 bool noisy, int verbose)
183 {
184 int k, ret, res, sense_cat;
185 uint8_t scCdb[16] = {SG_SERVICE_ACTION_IN_16,
186 STREAM_CONTROL_SA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
187 uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
188 struct sg_pt_base * ptvp;
189 static const char * const cmd_name = "Stream control";
190
191 if (str_ctl)
192 scCdb[1] |= (str_ctl & 0x3) << 5;
193 if (str_id) /* Only used for close, stream id to close */
194 sg_put_unaligned_be16(str_id, scCdb + 4);
195 sg_put_unaligned_be32(alloc_len, scCdb + 10);
196 if (verbose) {
197 char b[128];
198
199 pr2serr(" %s cdb: %s\n", cmd_name,
200 sg_get_command_str(scCdb, (int)sizeof(scCdb), false,
201 sizeof(b), b));
202 }
203
204 ptvp = construct_scsi_pt_obj_with_fd(sg_fd, verbose);
205 if (NULL == ptvp) {
206 pr2serr("%s: out of memory\n", cmd_name);
207 return -1;
208 }
209 set_scsi_pt_cdb(ptvp, scCdb, sizeof(scCdb));
210 set_scsi_pt_data_in(ptvp, resp, alloc_len);
211 set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
212 res = do_scsi_pt(ptvp, -1, DEF_PT_TIMEOUT, verbose);
213 ret = sg_cmds_process_resp(ptvp, cmd_name, res, noisy, verbose,
214 &sense_cat);
215 if (-1 == ret) {
216 if (get_scsi_pt_transport_err(ptvp))
217 ret = SG_LIB_TRANSPORT_ERROR;
218 else
219 ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
220 } else if (-2 == ret) {
221 switch (sense_cat) {
222 case SG_LIB_CAT_RECOVERED:
223 case SG_LIB_CAT_NO_SENSE:
224 ret = 0;
225 break;
226 default:
227 ret = sense_cat;
228 break;
229 }
230 } else
231 ret = 0;
232 k = ret ? (int)alloc_len : get_scsi_pt_resid(ptvp);
233 if (residp)
234 *residp = k;
235 if ((verbose > 2) && ((alloc_len - k) > 0)) {
236 pr2serr("%s: parameter data returned:\n", cmd_name);
237 hex2stderr((const uint8_t *)resp, alloc_len - k,
238 ((verbose > 3) ? -1 : 1));
239 }
240 destruct_scsi_pt_obj(ptvp);
241 return ret;
242 }
243
244
245 int
main(int argc,char * argv[])246 main(int argc, char * argv[])
247 {
248 bool do_brief = false;
249 bool do_close = false;
250 bool do_get = false;
251 bool do_open = false;
252 bool ctl_given = false;
253 bool maxlen_given = false;
254 bool read_only = false;
255 bool verbose_given = false;
256 bool version_given = false;
257 int c, k, res, resid;
258 int sg_fd = -1;
259 int maxlen = 0;
260 int ret = 0;
261 int verbose = 0;
262 uint16_t stream_id = 0;
263 uint16_t num_streams = 0;
264 uint32_t ctl = 0;
265 uint32_t pg_sz = sg_get_page_size();
266 uint32_t param_dl;
267 const char * device_name = NULL;
268 const char * cmd_name = NULL;
269 uint8_t * arr = NULL;
270 uint8_t * free_arr = NULL;
271
272 while (1) {
273 int option_index = 0;
274
275 c = getopt_long(argc, argv, "bcC:ghi:m:orvV", long_options,
276 &option_index);
277 if (c == -1)
278 break;
279
280 switch (c) {
281 case 'b':
282 do_brief = true;
283 break;
284 case 'c':
285 do_close = true;
286 break;
287 case 'C':
288 if ((1 != sscanf(optarg, "%4u", &ctl)) || (ctl > 3)) {
289 pr2serr("--ctl= expects a number from 0 to 3\n");
290 return SG_LIB_SYNTAX_ERROR;
291 }
292 ctl_given = true;
293 break;
294 case 'g':
295 do_get = true;
296 break;
297 case 'h':
298 case '?':
299 usage();
300 return 0;
301 case 'i':
302 k = sg_get_num(optarg);
303 if ((k < 0) || (k > (int)UINT16_MAX)) {
304 pr2serr("--id= expects a number from 0 to 65535\n");
305 return SG_LIB_SYNTAX_ERROR;
306 }
307 stream_id = (uint16_t)k;
308 break;
309 case 'm':
310 k = sg_get_num(optarg);
311 if (k < 0) {
312 pr2serr("--maxlen= unable to decode argument\n");
313 return SG_LIB_SYNTAX_ERROR;
314 }
315 maxlen_given = true;
316 if (k > 0)
317 maxlen = k;
318 break;
319 case 'o':
320 do_open = true;
321 break;
322 case 'r':
323 read_only = true;
324 break;
325 case 'v':
326 verbose_given = true;
327 ++verbose;
328 break;
329 case 'V':
330 version_given = true;
331 break;
332 default:
333 pr2serr("unrecognised option code 0x%x ??\n", c);
334 usage();
335 return SG_LIB_SYNTAX_ERROR;
336 }
337 }
338 if (optind < argc) {
339 if (NULL == device_name) {
340 device_name = argv[optind];
341 ++optind;
342 }
343 if (optind < argc) {
344 for (; optind < argc; ++optind)
345 pr2serr("Unexpected extra argument: %s\n",
346 argv[optind]);
347 usage();
348 return SG_LIB_SYNTAX_ERROR;
349 }
350 }
351 #ifdef DEBUG
352 pr2serr("In DEBUG mode, ");
353 if (verbose_given && version_given) {
354 pr2serr("but override: '-vV' given, zero verbose and continue\n");
355 verbose_given = false;
356 version_given = false;
357 verbose = 0;
358 } else if (! verbose_given) {
359 pr2serr("set '-vv'\n");
360 verbose = 2;
361 } else
362 pr2serr("keep verbose=%d\n", verbose);
363 #else
364 if (verbose_given && version_given)
365 pr2serr("Not in DEBUG mode, so '-vV' has no special action\n");
366 #endif
367 if (version_given) {
368 pr2serr("version: %s\n", version_str);
369 return 0;
370 }
371 if (NULL == device_name) {
372 pr2serr("missing device name!\n\n");
373 usage();
374 return SG_LIB_SYNTAX_ERROR;
375 }
376
377 k = (int)do_close + (int)do_get + (int)do_open + (int)ctl_given;
378 if (k > 1) {
379 pr2serr("Can only have one of: --close, --ctl==, --get, or --open\n");
380 return SG_LIB_CONTRADICT;
381 } else if (0 == k)
382 do_get = true;
383 if (do_close)
384 ctl = STREAM_CONTROL_CLOSE;
385 else if (do_open)
386 ctl = STREAM_CONTROL_OPEN;
387
388 if (maxlen_given) {
389 if (0 == maxlen)
390 maxlen = do_get ? 248 : 8;
391 } else
392 maxlen = do_get ? 248 : 8;
393
394 if (verbose) {
395 if (read_only && (! do_get))
396 pr2serr("Probably need to open %s read-write\n", device_name);
397 if (do_open && (stream_id > 0))
398 pr2serr("With --open the --id-SID option is ignored\n");
399 }
400
401 sg_fd = sg_cmds_open_device(device_name, read_only, verbose);
402 if (sg_fd < 0) {
403 if (verbose)
404 pr2serr("open error: %s: %s\n", device_name,
405 safe_strerror(-sg_fd));
406 ret = sg_convert_errno(-sg_fd);
407 goto fini;
408 }
409
410 if (maxlen > (int)pg_sz)
411 arr = sg_memalign(maxlen, pg_sz, &free_arr, verbose > 3);
412 else
413 arr = sg_memalign(pg_sz, pg_sz, &free_arr, verbose > 3);
414 if (NULL == arr) {
415 pr2serr("Unable to allocate space for response\n");
416 ret = sg_convert_errno(ENOMEM);
417 goto fini;
418 }
419
420 resid = 0;
421 if (do_get) { /* Get stream status */
422 cmd_name = "Get stream status";
423 ret = sg_ll_get_stream_status(sg_fd, stream_id, arr, maxlen,
424 &resid, false, verbose);
425 if (ret) {
426 if (SG_LIB_CAT_INVALID_OP == ret)
427 pr2serr("%s command not supported\n", cmd_name);
428 else {
429 char b[80];
430
431 sg_get_category_sense_str(ret, sizeof(b), b, verbose);
432 pr2serr("%s command: %s\n", cmd_name, b);
433 }
434 goto fini;
435 }
436 if ((maxlen - resid) < 8) {
437 pr2serr("Response too short (%d bytes) assigned stream id\n",
438 k);
439 printf("-1\n");
440 ret = SG_LIB_CAT_MALFORMED;
441 goto fini;
442 } else
443 maxlen -= resid;
444 param_dl = sg_get_unaligned_be32(arr + 0) + 8;
445 if (param_dl > (uint32_t)maxlen) {
446 pr2serr("Response truncated, need to set --maxlen=%u\n",
447 param_dl);
448 if (maxlen < (8 /* header */ + 4 /* enough of first */)) {
449 pr2serr("Response too short to continue\n");
450 goto fini;
451 }
452 }
453 num_streams = sg_get_unaligned_be16(arr + 6);
454 if (! do_brief) {
455 if (stream_id > 0)
456 printf("Starting at stream id: %u\n", stream_id);
457 printf("Number of open streams: %u\n", num_streams);
458 }
459 maxlen = ((uint32_t)maxlen < param_dl) ? maxlen : (int)param_dl;
460 for (k = 8; k < maxlen; k += 8) {
461 stream_id = sg_get_unaligned_be16(arr + k + 2);
462 if (do_brief)
463 printf("%u\n", stream_id);
464 else
465 printf("Open stream id: %u\n", stream_id);
466 }
467 } else { /* Stream control */
468 cmd_name = "Stream control";
469 ret = sg_ll_stream_control(sg_fd, ctl, stream_id, arr, maxlen,
470 &resid, false, verbose);
471 if (ret) {
472 if (SG_LIB_CAT_INVALID_OP == ret)
473 pr2serr("%s command not supported\n", cmd_name);
474 else {
475 char b[80];
476
477 sg_get_category_sense_str(ret, sizeof(b), b, verbose);
478 pr2serr("%s command: %s\n", cmd_name, b);
479 }
480 goto fini;
481 }
482 if (do_open) {
483 k = arr[0] + 1;
484 k = (k < (maxlen - resid)) ? k : (maxlen - resid);
485 if (k < 5) {
486 pr2serr("Response too short (%d bytes) assigned stream id\n",
487 k);
488 printf("-1\n");
489 ret = SG_LIB_CAT_MALFORMED;
490 } else {
491 stream_id = sg_get_unaligned_be16(arr + 4);
492 if (do_brief)
493 printf("%u\n", stream_id);
494 else
495 printf("Assigned stream id: %u\n", stream_id);
496 }
497 }
498 }
499
500 fini:
501 if (free_arr)
502 free(free_arr);
503 if (sg_fd >= 0) {
504 res = sg_cmds_close_device(sg_fd);
505 if (res < 0) {
506 pr2serr("close error: %s\n", safe_strerror(-res));
507 if (0 == ret)
508 ret = sg_convert_errno(-res);
509 }
510 }
511 if (0 == verbose) {
512 if (! sg_if_can2stderr("sg_stream_ctl failed: ", ret))
513 pr2serr("Some error occurred, try again with '-v' "
514 "or '-vv' for more information\n");
515 }
516 return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
517 }
518