1 /*
2 * Copyright (C) 1999-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 * Test code for D. Gilbert's extensions to the Linux OS SCSI generic ("sg")
11 * device driver.
12 * This program does a SCSI inquiry command on the given device and
13 * outputs some of the result. This program highlights the use of the
14 * SCSI_IOCTL_SEND_COMMAND ioctl. This should be able to be applied to
15 * any SCSI device file descriptor (not just one related to sg). [Whether
16 * this is a good idea on a disk while it is mounted is debatable.
17 * No detrimental effects when this was tested ...]
18 *
19 * Version 0.16 20181207
20 */
21
22 #include <unistd.h>
23 #include <signal.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <scsi/scsi.h>
33 /* #include <scsi/scsi_ioctl.h> */ /* glibc hides this file sometimes */
34
35 typedef struct my_scsi_ioctl_command {
36 unsigned int inlen; /* _excluding_ scsi command length */
37 unsigned int outlen;
38 unsigned char data[1]; /* was 0 but that's not ISO C!! */
39 /* on input, scsi command starts here then opt. data */
40 } My_Scsi_Ioctl_Command;
41
42 #define OFF (2 * sizeof(unsigned int))
43
44 #ifndef SCSI_IOCTL_SEND_COMMAND
45 #define SCSI_IOCTL_SEND_COMMAND 1
46 #endif
47
48 #define INQUIRY_CMD 0x12
49 #define INQUIRY_CMDLEN 6
50 #define INQUIRY_REPLY_LEN 96
51
52
main(int argc,char * argv[])53 int main(int argc, char * argv[])
54 {
55 int s_fd, res, k, to;
56 unsigned char inq_cdb [INQUIRY_CMDLEN] = {INQUIRY_CMD, 0, 0, 0,
57 INQUIRY_REPLY_LEN, 0};
58 unsigned char * inqBuff = (unsigned char *)
59 malloc(OFF + sizeof(inq_cdb) + 512);
60 unsigned char * buffp = inqBuff + OFF;
61 My_Scsi_Ioctl_Command * ishp = (My_Scsi_Ioctl_Command *)inqBuff;
62 char * file_name = 0;
63 int do_nonblock = 0;
64 int oflags = 0;
65
66 for (k = 1; k < argc; ++k) {
67 if (0 == strcmp(argv[k], "-n"))
68 do_nonblock = 1;
69 else if (*argv[k] != '-')
70 file_name = argv[k];
71 else {
72 printf("Unrecognized argument '%s'\n", argv[k]);
73 file_name = 0;
74 break;
75 }
76 }
77 if (0 == file_name) {
78 printf("Usage: 'scsi_inquiry [-n] <scsi_device>'\n");
79 printf(" where: -n open device in non-blocking mode\n");
80 printf(" Examples: scsi_inquiry /dev/sda\n");
81 printf(" scsi_inquiry /dev/sg0\n");
82 printf(" scsi_inquiry -n /dev/scd0\n");
83 return 1;
84 }
85
86 if (do_nonblock)
87 oflags = O_NONBLOCK;
88 s_fd = open(file_name, oflags | O_RDWR);
89 if (s_fd < 0) {
90 if ((EROFS == errno) || (EACCES == errno)) {
91 s_fd = open(file_name, oflags | O_RDONLY);
92 if (s_fd < 0) {
93 perror("scsi_inquiry: open error");
94 return 1;
95 }
96 }
97 else {
98 perror("scsi_inquiry: open error");
99 return 1;
100 }
101 }
102 /* Don't worry, being very careful not to write to a none-scsi file ... */
103 res = ioctl(s_fd, SCSI_IOCTL_GET_BUS_NUMBER, &to);
104 if (res < 0) {
105 /* perror("ioctl on scsi device, error"); */
106 printf("scsi_inquiry: not a scsi device\n");
107 return 1;
108 }
109
110 ishp->inlen = 0;
111 ishp->outlen = INQUIRY_REPLY_LEN;
112 memcpy(buffp, inq_cdb, INQUIRY_CMDLEN);
113 res = ioctl(s_fd, SCSI_IOCTL_SEND_COMMAND, inqBuff);
114 if (0 == res) {
115 to = (int)*(buffp + 7);
116 printf(" %.8s %.16s %.4s, byte_7=0x%x\n", buffp + 8,
117 buffp + 16, buffp + 32, to);
118 }
119 else if (res < 0)
120 perror("scsi_inquiry: SCSI_IOCTL_SEND_COMMAND err");
121 else
122 printf("scsi_inquiry: SCSI_IOCTL_SEND_COMMAND status=0x%x\n", res);
123
124 res = close(s_fd);
125 if (res < 0) {
126 perror("scsi_inquiry: close error");
127 return 1;
128 }
129 return 0;
130 }
131