-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsgio.cpp
629 lines (483 loc) · 14.8 KB
/
sgio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/**
** sgio.cpp
** Saturday. September 21, 2002
**
** Talk thru a Linux ioctl SG_IO connection to a device, since Linux
** kernel 2.4 i.e. since <scsi/sg.h> version 3.
**/
/**
** Link with standard C libraries.
**/
#include <errno.h> /* errno ... */
#include <limits.h> /* INT_MAX ... */
#include <stddef.h> /* offsetof ... */
#include <stdio.h> /* FILE ... */
#include <stdlib.h> /* calloc ... */
#include <string.h> /* memset ... */
/**
** Link with the *.c* of ../plscsi/.
**/
#include "plscsi.h"
/**
** Define some units.
**/
#define K 1000
#define M (K * K)
#define G (M * K)
#define Ki 1024 /* x1024 = x400 = 1 << 10 */
/**
** Link with Linux.
**
** Also compile in Microsoft Windows if "winsg.h" is available there.
**/
#ifdef SGIO
#ifdef _WIN32
#include "winsg.h"
#else
#include <unistd.h> /* close ... */
#include <fcntl.h> /* open O_RDWR ... */
#include <sys/ioctl.h> /* ioctl ... */
#include <scsi/sg.h> /* SG_... sg_... */
#endif
#define MAX_DEV_HD 20 /* hda -> hdt */
#define MAX_DEV_SG 17 /* sg0 -> sg16 */
#define MAX_DEV_SCD 16 /* scd0 -> scd15 */
/**
** Describe a Linux ioctl SG_IO connection to a device.
**/
#define MAX_CDB_LENGTH 0xFF
#define MAX_SENSE_LENGTH 0xFF
typedef struct SgioCommand SgioCommand;
struct SgioCommand
{
sg_io_hdr_t theSgioHdr;
char theCdbChars[MAX_CDB_LENGTH];
char theSenseChars[MAX_SENSE_LENGTH];
};
struct Sgio /* connection */
{
SgioCommand theSgioCommand;
FILE * theErrFile;
int theOpenMode;
int theFileDescriptor;
int theSgVersionNum;
int theMaxSenseLength;
int theMinMilliSeconds;
int theExitInt;
int theLength;
char const * theSenseChars;
int theSenseLength;
int theReadNameInt;
};
/**
** Construct a new Sgio connection.
**
** Associate the given Scsi connection with the new Sgio connection.
**
** Arrange for sgioSay to fail until sgioLimitSense and sgioLimitTime
** are both called.
**
** Return null to fail, else don't. Do fail if scsi is null.
**/
Sgio * newSgio(void)
{
/* Alloc and zero a new struct. */
Sgio * sgio = (Sgio *) calloc(1, sizeof (Sgio));
if (sgio == NULL) return NULL;
/* Choose defaults, say closed. */
sgio->theErrFile = stderr;
// sgio->theOpenMode = O_RDWR; /* O_RDONLY often works */
// sgio->theOpenMode = O_RDONLY|O_NONBLOCK;
sgio->theOpenMode = O_NONBLOCK;
sgio->theFileDescriptor = -1;
sgio->theSgVersionNum = 0; /* 0 = unknown */
sgio->theMaxSenseLength = -1;
sgio->theMinMilliSeconds = -1;
sgio->theExitInt = -1;
sgio->theLength = -1;
sgio->theSenseChars = NULL;
sgio->theSenseLength = -1;
sgio->theReadNameInt = 0;
/* Succeed. */
return sgio;
}
/**
** Redirect the stderr of this Sgio connection.
**/
void sgioSetErr(Sgio * sgio, FILE * fi)
{
if (sgio == NULL) return;
if (fi == NULL) fi = stderr;
sgio->theErrFile = fi;
}
/**
** Close this Sgio connection.
**
** Gracefully do nothing if sgio is null.
**/
void sgioClose(Sgio * sgio)
{
if (sgio == NULL) return;
int fd = sgio->theFileDescriptor;
if (0 <= fd)
{
int closeInt = close(fd);
if (closeInt != 0)
{
FILE * fi = sgio->theErrFile;
(void) fprintf(fi, "// sgioClose.close: %s\n", strerror(errno));
}
sgio->theFileDescriptor = fd = -1;
}
}
/**
** Open this Sgio connection with a new name.
**
** Return zero to succeed, else don't.
**/
int sgioOpen(Sgio * sgio, char const * name)
{
if (sgio == NULL) return -1;
if (name == NULL) return -1;
/* Close always. */
sgioClose(sgio);
/* Open a "/dev/sg$number" name else die. */
int mode = sgio->theOpenMode;
int fd = open(name, mode);
sgio->theFileDescriptor = fd;
if (fd < 0)
{
FILE * fi = sgio->theErrFile;
(void) fprintf(fi, "// sgioOpen.open: %s\n", strerror(errno));
return -1;
}
/* Get hints else die. */
int versionNum = 0;
int ioctlInt = ioctl(fd, SG_GET_VERSION_NUM, &versionNum);
if (ioctlInt < 0)
{
FILE * fi = sgio->theErrFile;
(void) fprintf(fi, "// sgioOpen.ioctl: %s\n", strerror(errno));
return -1;
}
/* Get a compatible version else die. */
if (!(30000 <= versionNum)) return -1;
/* Otherwise succeed. */
return 0;
}
/**
** Limit the count of sense bytes copied In by sgioSay.
**
** Return zero if ok, else negative. Do succeed if closed.
**/
int sgioLimitSense(Sgio * sgio, int maxSenseLength)
{
if (sgio == NULL) return -1;
if ((0 <= maxSenseLength) && (maxSenseLength <= MAX_SENSE_LENGTH))
{
sgio->theMaxSenseLength = maxSenseLength;
return 0;
}
return -1;
}
/**
** Convert to ms from (s, ns) else return negative.
**/
static int toMilliSeconds(int s, int ns)
{
if ((0 <= ns) && (ns < G))
{
int ms = ((ns + M - 1) / M);
if ((0 <= s) && (s <= ((INT_MAX - ms) / K)))
{
int ms = (s * K);
return ms;
}
}
return -1;
}
/**
** Specify the min time to wait before cancelling sgioSay.
**
** Let negative time mean allow the max possible time.
**
** Return zero if ok, else negative.
**/
int sgioLimitSeconds(Sgio * sgio, int s, int ns)
{
if (sgio == NULL) return -1;
/* Round up to whole seconds, let negative mean max. */
int ms = toMilliSeconds(s, ns);
if (ms < 0) ms = ((28 * 60 * 60) * 1000); /* 28 * 60 * 60 s = 28 hours = 1.125 days */
/* Set a positive limit and succeed, else fail. */
if (0 < ms)
{
sgio->theMinMilliSeconds = ms;
return 0;
}
return -1;
}
/**
** Remember what to say via ioctl SG_IO.
**
** Return zero if ok, else negative.
**/
static int sgioSetCommand(Sgio * sgio,
char const * cdbChars, int cdbLength, char * dataChars, int maxLength, int direction)
{
if (sgio == NULL) return -1;
/* Fetch implicit args. */
int maxSenseLength = sgio->theMaxSenseLength;
/* Constrain args. */
if ((cdbChars == NULL) && (cdbLength != 0)) return -1;
if ((cdbLength < 0) || (MAX_CDB_LENGTH < cdbLength)) return -1;
if ((dataChars == NULL) && (maxLength != 0)) return -1;
if ((maxSenseLength < 0) || (MAX_SENSE_LENGTH < maxSenseLength)) return -1;
if (UCHAR_MAX < cdbLength) return -1; /* UCHAR_MAX = max cmd_len */
if (UCHAR_MAX < maxSenseLength) return -1; /* UCHAR_MAX = max mx_sb_len */
/* Erase the entire speech. */
SgioCommand * sc = &sgio->theSgioCommand;
(void) memset(sc, '\0', sizeof *sc);
sg_io_hdr_t * sih = &sc->theSgioHdr;
sih->interface_id = 'S';
/* Ask to copy all the Cdb Out. */
sih->cmdp = (unsigned char *) &sc->theCdbChars[0];
(void) memmove(sih->cmdp, cdbChars, cdbLength);
sih->cmd_len = ((unsigned char) cdbLength);
/* Ask to copy all the Data In or Out. */
switch (direction)
{
case X2_DATA_OUT:
sih->dxferp = dataChars;
sih->dxfer_len = maxLength;
sih->dxfer_direction = SG_DXFER_TO_DEV; /* Out to device */
break;
case X1_DATA_IN:
sih->dxferp = dataChars;
sih->dxfer_len = maxLength;
sih->dxfer_direction = SG_DXFER_FROM_DEV; /* In from device */
break;
/* Ask to copy No Data in the last of many possible ways. */
case X0_DATA_NOT:
default:
sih->dxferp = NULL;
sih->dxferp = dataChars; /* maybe NULL */
sih->dxfer_len = 0;
sih->dxfer_len = maxLength; /* often zero */
sih->dxfer_direction = SG_DXFER_NONE;
break;
}
/* Ask to copy some Sense data In, if available. */
sih->sbp = (unsigned char *) &sc->theSenseChars[0];
sih->mx_sb_len = ((unsigned char) maxSenseLength);
/* Leave much zeroed. */
; /* sih->iovec_count sih->flags sih->pack_id sih->usr_ptr */
; /* sih->status sih->masked_status sih->msg_status */
; /* sih->sb_len_wr sih->host_status sih->drive_status */
; /* sih->resid sih->duration sih->info */
return 0;
}
/**
** Call ioctl SG_IO but wait at least ms before asking to cancel.
**
** Return the int that the ioctl returned.
**/
static int sgioSayForMilliSeconds(Sgio * sgio, int ms)
{
if (sgio == NULL) return -1;
if (ms < 0) return -1;
SgioCommand * sc = &sgio->theSgioCommand;
sg_io_hdr_t * sih = &sc->theSgioHdr;
sih->timeout = ms;
int fd = sgio->theFileDescriptor;
int ioctlInt = ioctl(fd, SG_IO, sih);
return ioctlInt;
}
/**
** Speak a sentence of Scsi through an Sgio connection.
**
** That is, copy the cdbLength of the cdbChars Out, copy between zero
** and the maxLength of the dataChars In or Out, copy status In, and
** copy sense bytes In if available.
**
** Return zero if all was ok, positive to say specifically how many
** less than all data bytes were copied, else negative.
**
** Also return zero if all was ok except that the number of data bytes
** copied is unknown.
**
** Do succeed if closed.
**/
int sgioSay(Sgio * sgio,
char const * cdbChars, int cdbLength, char * dataChars, int maxLength, int direction)
{
if (sgio == NULL) return -1;
if (sgio->theFileDescriptor < 0) return 0;
/* Fail by default. */
/* And say the data length and sense length are unknown. */
sgio->theExitInt = -1;
sgio->theLength = -1;
sgio->theSenseChars = NULL;
sgio->theSenseLength = -1;
/* Remember what to say. */
int maxSenseLength = sgio->theMaxSenseLength;
int exitInt = sgioSetCommand(sgio,
cdbChars, cdbLength, dataChars, maxLength, direction);
if (exitInt != 0) return exitInt;
/* Say it. */
int ms = sgio->theMinMilliSeconds;
if (ms < 0) return -1;
int ioctlInt = sgioSayForMilliSeconds(sgio, ms);
/* Fetch more results. */
SgioCommand * sc = &sgio->theSgioCommand;
sg_io_hdr_t * sih = &sc->theSgioHdr;
int sihInfo = sih->info;
int sihResid = sih->resid;
int sihSbLenWr = sih->sb_len_wr;
/* Complain of utter failure. */
exitInt = -1;
if (ioctlInt < 0)
{
FILE * fi = sgio->theErrFile;
(void) fprintf(fi, "// sgioSay.ioctl: %s\n", strerror(errno));
}
else
{
/* Count and complain of data bytes Not copied. */
exitInt = -1;
int residue = sihResid;
if ((0 <= residue) && (residue <= maxLength))
{
sgio->theLength = (maxLength - residue);
exitInt = residue;
}
/* Complain of sense bytes copied. */
if ((sihInfo & SG_INFO_OK_MASK) != SG_INFO_OK)
{
int senseLength = sihSbLenWr;
if (0 < exitInt) exitInt = -X0100_RESIDUE;
exitInt = -((-exitInt) | X0002_SENSE);
/* Count sense bytes copied. */
if ((0 <= senseLength) && (senseLength <= maxSenseLength))
{
char const * senseChars = &sc->theSenseChars[0];
sgio->theSenseChars = senseChars;
sgio->theSenseLength = senseLength;
}
else
{
exitInt = -1; /* sense not intelligible */
}
}
}
/* Proceed. */
sgio->theExitInt = exitInt;
if (exitInt == -1)
{
FILE * fi = sgio->theErrFile;
(void) fprintf(fi,
"// sgioSay x %lX %lX %lX %lX (ioctl sihInfo sihResid sihSbLenWr)\n",
(long) ioctlInt, (long) sihInfo, (long) sihResid, (long) sihSbLenWr);
}
return exitInt;
}
/**
** Count the data bytes copied In or Out by sgioSay.
**
** Guess the elseLength if the actual length of data bytes copied is
** unknown. Also if the sgio is null.
**/
int sgioGetLength(Sgio * sgio, int elseLength)
{
if (sgio == NULL) return elseLength;
int length = sgio->theLength;
if (length < 0) length = elseLength;
return length;
}
/**
** Copy some or all of the sense bytes copied by sgioSay.
**
** Guess the elseLength were copied if the length copied is unknown,
** except guess 0 if sgio is null.
**
** Copy the min of the charsLength with the actual or guessed copy
** length.
**
** Return the count of bytes copied.
**/
int sgioGetSense(Sgio * sgio, char * chars, int charsLength, int elseLength)
{
if (sgio == NULL) return 0;
/* Guess the elseLength if the length of sense bytes copied is unknown. */
int length = sgio->theSenseLength;
if (length < 0) length = elseLength;
/* Copy the min of the charsLength with the actual or guessed copy length. */
int copyLength = length;
if (charsLength < copyLength) copyLength = charsLength;
if ((chars != NULL) && (0 < copyLength))
{
(void) memmove(chars, sgio->theSenseChars, copyLength);
}
/* Return the count of bytes copied. */
return copyLength;
}
/**
** Read the first else the next well known device path name.
**
** UPDATED: Recent Linux kernel handle standard devices as SGIO ones
**
** Copy a name of up to charsLength to the chars.
**
** Return the positive length of chars assigned, else zero to say the
** name was too long (or if chars is null), else negative if no more
** names exist (or if sgio is null).
**/
int sgioReadName(Sgio * sgio, char * chars, int charsLength)
{
if (sgio == NULL) return -1;
/* Close always. */
sgioClose(sgio);
/* Return negative after the last name. */
int readNameInt = sgio->theReadNameInt;
if ((MAX_DEV_HD+MAX_DEV_SG+MAX_DEV_SCD) <= readNameInt) return -1;
/* Read a "/dev/sg$number" name. */
sgio->theReadNameInt = (readNameInt + 1);
/* Print the name. */
char name[] = "/dev/hda876543210";
if (readNameInt < MAX_DEV_HD)
{
name[7] += readNameInt;
name[8] = 0;
}
else if ((readNameInt - MAX_DEV_HD) < MAX_DEV_SG)
(void) sprintf(&name[0], "/dev/sg%ld", (long) (readNameInt - MAX_DEV_HD));
else
(void) sprintf(&name[0], "/dev/scd%ld", (long) (readNameInt - MAX_DEV_HD - MAX_DEV_SG));
/* Copy the name if it fits. */
int copyLength = (strlen(name) + 1);
if (chars != NULL)
if (copyLength <= charsLength)
{
(void) memmove(chars, &name[0], copyLength);
return copyLength;
}
/* Else copy nothing and return zero. */
return 0;
}
/**
** Accept a -X option of a Scsi command line.
**
** Return zero if ok, else negative.
**/
int sgioSwallowArg(Sgio * sgio, char const * arg)
{
if (sgio == NULL) return -1;
if (arg == NULL) return -1;
if (strcmp(arg, "SGIO") == 0)
{
return 0;
}
return -1;
}
#endif /* SGIO */
/* end of file */