~ubuntu-branches/ubuntu/intrepid/sg3-utils/intrepid

« back to all changes in this revision

Viewing changes to sg_ident.c

  • Committer: Bazaar Package Importer
  • Author(s): Luk Claes
  • Date: 2006-11-05 17:23:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.2 edgy)
  • Revision ID: james.westby@ubuntu.com-20061105172329-l4loha00sk36qz6k
* Non-maintainer upload.
* Fix FTBFS due to old syscall usage (Closes: #395512).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2006 Douglas Gilbert.
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 * 1. Redistributions of source code must retain the above copyright
 
9
 *    notice, this list of conditions and the following disclaimer.
 
10
 * 2. Redistributions in binary form must reproduce the above copyright
 
11
 *    notice, this list of conditions and the following disclaimer in the
 
12
 *    documentation and/or other materials provided with the distribution.
 
13
 * 3. The name of the author may not be used to endorse or promote products
 
14
 *    derived from this software without specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
26
 * SUCH DAMAGE.
 
27
 *
 
28
 */
 
29
 
 
30
#include <unistd.h>
 
31
#include <fcntl.h>
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <string.h>
 
35
#include <getopt.h>
 
36
 
 
37
#include "sg_lib.h"
 
38
#include "sg_cmds.h"
 
39
 
 
40
/* A utility program for the Linux OS SCSI subsystem.
 
41
 *
 
42
 *
 
43
 * This program issues these SCSI commands: REPORT DEVICE IDENTIFIER,
 
44
 * SET DEVICE IDENTIFIER and/or INQUIRY (VPD=0x83 [device identifier]).
 
45
 */
 
46
 
 
47
static char * version_str = "1.02 20060623";
 
48
 
 
49
#define ME "sg_ident: "
 
50
 
 
51
#define REPORT_DEV_ID_SANITY_LEN 512
 
52
 
 
53
 
 
54
static struct option long_options[] = {
 
55
        {"ascii", 0, 0, 'A'},
 
56
        {"clear", 0, 0, 'C'},
 
57
        {"help", 0, 0, 'h'},
 
58
        {"raw", 0, 0, 'r'},
 
59
        {"set", 0, 0, 'S'},
 
60
        {"verbose", 0, 0, 'v'},
 
61
        {"version", 0, 0, 'V'},
 
62
        {0, 0, 0, 0},
 
63
};
 
64
 
 
65
static void usage()
 
66
{
 
67
    fprintf(stderr, "Usage: "
 
68
          "sg_ident   [--ascii] [--clear] [--help] [--raw] [--set] "
 
69
          "[--verbose]\n"
 
70
          "                  [--version] <scsi_device>\n"
 
71
          "  where: --ascii|-A      report device identifier as ASCII "
 
72
          "string\n"
 
73
          "         --clear|-C      clear (set to zero length) device "
 
74
          "identifier\n"
 
75
          "         --help|-h       print out usage message\n"
 
76
          "         --raw|-r        output device identifier to stdout\n"
 
77
          "                         fetch from stdin (when '--set')\n"
 
78
          "         --set|-S        invoke set device identifier with data "
 
79
          "from stdin\n"
 
80
          "         --verbose|-v    set device identifier\n"
 
81
          "         --version|-V    print version string and exit\n\n"
 
82
          "Performs a REPORT or SET DEVICE IDENTIFIER SCSI command\n"
 
83
          );
 
84
}
 
85
 
 
86
int main(int argc, char * argv[])
 
87
{
 
88
    int sg_fd, res, c, di_len, n;
 
89
    unsigned char rdi_buff[REPORT_DEV_ID_SANITY_LEN + 4];
 
90
    unsigned char * ucp = NULL;
 
91
    int ascii = 0;
 
92
    int do_clear = 0;
 
93
    int raw = 0;
 
94
    int do_set = 0;
 
95
    int verbose = 0;
 
96
    char device_name[512];
 
97
    int ret = 0;
 
98
 
 
99
    memset(device_name, 0, sizeof device_name);
 
100
    while (1) {
 
101
        int option_index = 0;
 
102
 
 
103
        c = getopt_long(argc, argv, "AChrSvV", long_options,
 
104
                        &option_index);
 
105
        if (c == -1)
 
106
            break;
 
107
 
 
108
        switch (c) {
 
109
        case 'A':
 
110
            ascii = 1;
 
111
            break;
 
112
        case 'C':
 
113
            do_clear = 1;
 
114
            break;
 
115
        case 'h':
 
116
        case '?':
 
117
            usage();
 
118
            return 0;
 
119
        case 'r':
 
120
            raw = 1;
 
121
            break;
 
122
        case 'S':
 
123
            do_set = 1;
 
124
            break;
 
125
        case 'v':
 
126
            ++verbose;
 
127
            break;
 
128
        case 'V':
 
129
            fprintf(stderr, ME "version: %s\n", version_str);
 
130
            return 0;
 
131
        default:
 
132
            fprintf(stderr, "unrecognised switch code 0x%x ??\n", c);
 
133
            usage();
 
134
            return SG_LIB_SYNTAX_ERROR;
 
135
        }
 
136
    }
 
137
    if (optind < argc) {
 
138
        if ('\0' == device_name[0]) {
 
139
            strncpy(device_name, argv[optind], sizeof(device_name) - 1);
 
140
            device_name[sizeof(device_name) - 1] = '\0';
 
141
            ++optind;
 
142
        }
 
143
        if (optind < argc) {
 
144
            for (; optind < argc; ++optind)
 
145
                fprintf(stderr, "Unexpected extra argument: %s\n",
 
146
                        argv[optind]);
 
147
            usage();
 
148
            return SG_LIB_SYNTAX_ERROR;
 
149
        }
 
150
    }
 
151
 
 
152
    if (0 == device_name[0]) {
 
153
        fprintf(stderr, "missing device name!\n");
 
154
        usage();
 
155
        return SG_LIB_SYNTAX_ERROR;
 
156
    }
 
157
    if (do_set && do_clear) {
 
158
        fprintf(stderr, "only one of '--clear' and '--set' can be given\n");
 
159
        usage();
 
160
        return SG_LIB_SYNTAX_ERROR;
 
161
    }
 
162
    if (ascii && raw) {
 
163
        fprintf(stderr, "only one of '--ascii' and '--raw' can be given\n");
 
164
        usage();
 
165
        return SG_LIB_SYNTAX_ERROR;
 
166
    }
 
167
    if ((do_set || do_clear) && (raw || ascii)) {
 
168
        fprintf(stderr, "'--set' cannot be used with either '--ascii' or "
 
169
                "'--raw'\n");
 
170
        usage();
 
171
        return SG_LIB_SYNTAX_ERROR;
 
172
    }
 
173
    sg_fd = sg_cmds_open_device(device_name, 0 /* rw */, verbose);
 
174
    if (sg_fd < 0) {
 
175
        fprintf(stderr, ME "open error: %s: %s\n", device_name,
 
176
                safe_strerror(-sg_fd));
 
177
        return SG_LIB_FILE_ERROR;
 
178
    }
 
179
 
 
180
    memset(rdi_buff, 0x0, sizeof(rdi_buff));
 
181
    if (do_set || do_clear) {
 
182
        if (do_set) {
 
183
            res = fread(rdi_buff, 1, REPORT_DEV_ID_SANITY_LEN + 2, stdin); 
 
184
            if (res <= 0) {
 
185
                fprintf(stderr, "no data read from stdin; to clear "
 
186
                        "identifier use '--clear' instead\n");
 
187
                goto err_out;
 
188
            } else if (res > REPORT_DEV_ID_SANITY_LEN) {
 
189
                fprintf(stderr, "SPC-3 limits identifier length to 512 "
 
190
                        "bytes\n");
 
191
                goto err_out;
 
192
            }
 
193
            di_len = res;
 
194
            res = sg_ll_set_dev_id(sg_fd, rdi_buff, di_len, 1, verbose);
 
195
        } else    /* do_clear */
 
196
            res = sg_ll_set_dev_id(sg_fd, rdi_buff, 0, 1, verbose);
 
197
        if (res) {
 
198
            ret = res;
 
199
            if (SG_LIB_CAT_NOT_READY == res)
 
200
                fprintf(stderr, "Set Device Identifier command, device "
 
201
                        "not ready\n");
 
202
            else if (SG_LIB_CAT_INVALID_OP == res)
 
203
                fprintf(stderr, "Set Device Identifier command not "
 
204
                        "supported\n");
 
205
            else if (SG_LIB_CAT_UNIT_ATTENTION == res)
 
206
                fprintf(stderr, "Set Device Identifier, unit attention\n");
 
207
            else if (SG_LIB_CAT_ILLEGAL_REQ == res)
 
208
                fprintf(stderr, "bad field in Set Device Identifier "
 
209
                        "cdb\n");
 
210
            else {
 
211
                fprintf(stderr, "Set Device Identifier command failed\n");
 
212
                if (0 == verbose)
 
213
                    fprintf(stderr, "    try '-v' for more information\n");
 
214
            }
 
215
        }
 
216
    } else {    /* do report device identifier */
 
217
        res = sg_ll_report_dev_id(sg_fd, rdi_buff, 4, 1, verbose);
 
218
        if (0 == res) {
 
219
            di_len = (rdi_buff[0] << 24) + (rdi_buff[1] << 16) + 
 
220
                         (rdi_buff[2] << 8) + rdi_buff[3];
 
221
            if (! raw)
 
222
                printf("Reported device identifier length = %d\n", di_len);
 
223
            if (0 == di_len) {
 
224
                fprintf(stderr, "    This implies the device has an empty "
 
225
                        "identifier\n");
 
226
                goto err_out;
 
227
            }
 
228
            if (di_len > REPORT_DEV_ID_SANITY_LEN) {
 
229
                fprintf(stderr, "    That length (%d) seems too long for a "
 
230
                        "device identifier\n", di_len);
 
231
                goto err_out;
 
232
            }
 
233
            ucp = rdi_buff;
 
234
            res = sg_ll_report_dev_id(sg_fd, ucp, di_len + 4, 1, verbose);
 
235
            if (0 == res) {
 
236
                di_len = (ucp[0] << 24) + (ucp[1] << 16) + (ucp[2] << 8) +
 
237
                         ucp[3];
 
238
                if (raw) {
 
239
                    if (di_len > 0)
 
240
                        n = fwrite(ucp + 4, 1, di_len, stdout);
 
241
                } else {
 
242
                    printf("Device identifier:\n");
 
243
                    if (di_len > 0) {
 
244
                        if (ascii)
 
245
                            printf("%.*s\n", di_len, (const char *)ucp + 4);
 
246
                        else
 
247
                            dStrHex((const char *)ucp + 4, di_len, 0); 
 
248
                    }
 
249
                }
 
250
            } else {
 
251
                ret = res;
 
252
                if (SG_LIB_CAT_NOT_READY == res)
 
253
                    fprintf(stderr, "Report Device Identifier command, "
 
254
                            "device not ready\n");
 
255
                else if (SG_LIB_CAT_UNIT_ATTENTION == res)
 
256
                    fprintf(stderr, "Report Device Identifier, unit "
 
257
                            "attention\n");
 
258
                else if (SG_LIB_CAT_INVALID_OP == res)
 
259
                    fprintf(stderr, "Report Device Identifier command not "
 
260
                            "supported\n");
 
261
                else if (SG_LIB_CAT_ILLEGAL_REQ == res)
 
262
                    fprintf(stderr, "bad field in Report Device Identifier "
 
263
                            "cdb\n");
 
264
                else {
 
265
                    fprintf(stderr, "Report Device Identifier command "
 
266
                            "failed\n");
 
267
                    if (0 == verbose)
 
268
                        fprintf(stderr, "    try '-v' for more "
 
269
                                "information\n");
 
270
                }
 
271
            }
 
272
        }
 
273
    }
 
274
 
 
275
err_out:
 
276
    res = sg_cmds_close_device(sg_fd);
 
277
    if (res < 0) {
 
278
        fprintf(stderr, "close error: %s\n", safe_strerror(-res));
 
279
        if (0 == ret)
 
280
            return SG_LIB_FILE_ERROR;
 
281
    }
 
282
    return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
 
283
}