aboutsummaryrefslogtreecommitdiff
path: root/archive/scsi_devfs_scan.c
blob: 24c99384cf6ad871345d4642480374334477f88d (plain)
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
#define _XOPEN_SOURCE 500
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include "sg_include.h"
#include "sg_err.h"

/* Code for scanning for SCSI devices within a Linux device pseudo file
   system.
   *  Copyright (C) 2001 D. Gilbert
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; either version 2, or (at your option)
   *  any later version.

      This program scans the /dev directory structure looking for the
      devfs "primary" scsi (and optionally IDE) device names.

   Version 0.13 20030430
*/

void usage()
{
    printf("Usage: 'scsi_devfs_scan [-d <dir>] [-i] [-ide] [-l [-x]] "
           "[-q]'\n");
    printf("    where: -d <dir> location of devfs [default: /dev ]\n");
    printf("           -i   show INQUIRY data for each SCSI device\n");
    printf("           -ide show scan of IDE devices after SCSI devices\n");
    printf("           -l   show device file names in leaf directory\n");
    printf("           -q   just output host, bus, target, lun numbers\n");
    printf("           -x   add (major,minor) information to '-l'\n");
}

#define NAME_LEN_MAX 256
#define LEVELS 4

#define SENSE_BUFF_LEN 32       /* Arbitrary, could be larger */
#define DEF_TIMEOUT 60000       /* 60,000 millisecs == 60 seconds */
#define INQUIRY_CMD     0x12
#define INQUIRY_CMDLEN  6

static const char * level_arr[LEVELS] = {"host", "bus", "target", "lun"};

static int do_ide = 0;
static int do_inq = 0;
static int do_leaf = 0;
static int do_extra = 0;
static int do_quiet = 0;
static int checked_sg = 0;

static void ddStrHex(const char* str, int len)
{
    const char* p = str;
    unsigned char c;
    char buff[82];
    int a = 0;
    const int bpstart = 5;
    const int cpstart = 60;
    int cpos = cpstart;
    int bpos = bpstart;
    int i, k;

    if (len <= 0) return;
    memset(buff,' ',80);
    buff[80]='\0';
    k = sprintf(buff + 1, "%.2x", a);
    buff[k + 1] = ' ';
    if (bpos >= ((bpstart + (9 * 3))))
        bpos++;

    for(i = 0; i < len; i++)
    {
        c = *p++;
        bpos += 3;
        if (bpos == (bpstart + (9 * 3)))
            bpos++;
        sprintf(&buff[bpos], "%.2x", (int)(unsigned char)c);
        buff[bpos + 2] = ' ';
        if ((c < ' ') || (c >= 0x7f))
            c='.';
        buff[cpos++] = c;
        if (cpos > (cpstart+15))
        {
            printf("%s\n", buff);
            bpos = bpstart;
            cpos = cpstart;
            a += 16;
            memset(buff,' ',80);
            k = sprintf(buff + 1, "%.2x", a);
            buff[k + 1] = ' ';
        }
    }
    if (cpos > cpstart)
    {
        printf("%s\n", buff);
    }
}

static int do_inquiry(int sg_fd, void * resp, int mx_resp_len)
{
    int res;
    unsigned char inqCmdBlk[INQUIRY_CMDLEN] = {INQUIRY_CMD, 0, 0, 0, 0, 0};
    unsigned char sense_b[SENSE_BUFF_LEN];
    sg_io_hdr_t io_hdr;

    inqCmdBlk[4] = (unsigned char)mx_resp_len;
    memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
    io_hdr.interface_id = 'S';
    io_hdr.cmd_len = sizeof(inqCmdBlk);
    io_hdr.mx_sb_len = sizeof(sense_b);
    io_hdr.dxfer_direction = SG_DXFER_TO_FROM_DEV;
    io_hdr.dxfer_len = mx_resp_len;
    io_hdr.dxferp = resp;
    io_hdr.cmdp = inqCmdBlk;
    io_hdr.timeout = DEF_TIMEOUT;

    if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
        perror("SG_IO (inquiry) error");
        return -1;
    }
    res = sg_err_category3(&io_hdr);
    switch (res) {
    case SG_ERR_CAT_CLEAN:
    case SG_ERR_CAT_RECOVERED:
        return 0;
    default:
        sg_chk_n_print3("Failed INQUIRY", &io_hdr);
        return -1;
    }
}

void leaf_dir(const char * lf, unsigned int * larr)
{
    char name[NAME_LEN_MAX * 2];
    int res;

    if (do_quiet) {
        printf("%u\t%u\t%u\t%u\n", larr[0], larr[1], larr[2], larr[3]);
        return;
    }
    printf("%u\t%u\t%u\t%u\t%s\n", larr[0], larr[1], larr[2], larr[3], lf);
    if (do_leaf) {
        struct dirent * de_entry;
        struct dirent * de_result;
        DIR * sdir;
        int outpos;

        if (NULL == (sdir = opendir(lf))) {
            fprintf(stderr, "leaf_dir: opendir of %s: failed\n", lf);
            return;
        }
        de_entry = (struct dirent *)malloc(sizeof(struct dirent) + 
                                           NAME_LEN_MAX);
        if (NULL == de_entry)
            return;
        res = 0;
        printf("\t");
        outpos = 8;
        while (1) {
            res = readdir_r(sdir, de_entry, &de_result);
            if (0 != res) {
                fprintf(stderr, "leaf_dir: readdir_r of %s: %s\n", 
                        lf, strerror(res));
                res = -2;
                break;
            }
            if (de_result == NULL) 
                break;
            strncpy(name, de_entry->d_name, NAME_LEN_MAX * 2);
            if ((0 == strcmp("..", name)) ||(0 == strcmp(".", name))) 
                continue;
            if (do_extra) {
                struct stat st;
                char devname[NAME_LEN_MAX * 2];

                strncpy(devname, lf, NAME_LEN_MAX * 2);
                strcat(devname, "/");
                strcat(devname, name);
                if (stat(devname, &st) < 0)
                    return;
                if (S_ISCHR(st.st_mode)) {
                    strcat(name, "(c ");
                    sprintf(name + strlen(name), "%d %d)", major(st.st_rdev),
                            minor(st.st_rdev));
                }
                else if (S_ISBLK(st.st_mode)) {
                    strcat(name, "(b ");
                    sprintf(name + strlen(name), "%d %d)", major(st.st_rdev),
                            minor(st.st_rdev));
                }
            }
            res = strlen(name);
            if ((outpos + res + 2) > 80) {
                printf("\n\t");
                outpos = 8;
            }
            printf("%s  ", name);
            outpos += res + 2;
        }
        printf("\n");
    }
    if (do_inq) {
        int sg_fd;
        char buff[64];

        memset(buff, 0, sizeof(buff));
        strncpy(name, lf, NAME_LEN_MAX * 2);
        strcat(name, "/generic");
        if ((sg_fd = open(name, O_RDONLY)) < 0) {
            if (! checked_sg) {
                checked_sg = 1;
                if ((sg_fd = open("/dev/sg0", O_RDONLY)) >= 0)
                    close(sg_fd);  /* try and get sg module loaded */
                sg_fd = open(name, O_RDONLY);
            }
            if (sg_fd < 0) {
                printf("Unable to open sg device: %s, %s\n", name, 
                       strerror(errno));
                return;
            }
        }
        if (0 != do_inquiry(sg_fd, buff, 64))
            return;
        close(sg_fd);
        ddStrHex(buff, 64);
    }
}

/* Return 0 -> ok, -1 -> opendir() error, -2 -> readdir_r error, 
         -3 -> malloc error */
int hbtl_scan(const char * path, int level, unsigned int *larr)
{
    struct dirent * de_entry;
    struct dirent * de_result;
    char new_path[NAME_LEN_MAX * 2];
    DIR * sdir;
    int res;
    size_t level_slen;

    level_slen = strlen(level_arr[level]);
    if (NULL == (sdir = opendir(path))) {
        fprintf(stderr, "hbtl_scan: opendir of %s: failed\n", path);
        return -1;
    }
    de_entry = (struct dirent *)malloc(sizeof(struct dirent) + NAME_LEN_MAX);
    if (NULL == de_entry)
        return -3;
    res = 0;
    while (1) {
        res = readdir_r(sdir, de_entry, &de_result);
        if (0 != res) {
            fprintf(stderr, "hbtl_scan: readdir_r of %s: %s\n", 
                    path, strerror(res));
            res = -2;
            break;
        }
        if (de_result == NULL) 
            break;
        if (0 == strncmp(level_arr[level], de_entry->d_name, level_slen)) {
            if (1 != sscanf(de_entry->d_name + level_slen, "%u", larr + level))
                larr[level] = UINT_MAX;
            strncpy(new_path, path, NAME_LEN_MAX * 2);
            strcat(new_path, "/");
            strcat(new_path, de_entry->d_name);
            if ((level + 1) < LEVELS) {
                res = hbtl_scan(new_path, level + 1, larr);
                if (res < 0)
                    break;
            }
            else 
                leaf_dir(new_path, larr);
        }
    }
    free(de_entry);
    closedir(sdir);
    return res;
}

#define D_ROOT_SZ 512


int main(int argc, char * argv[])
{
    int k, res;
    char ds_root[D_ROOT_SZ];
    char di_root[D_ROOT_SZ];
    unsigned int larr[LEVELS];
    struct stat st;

    strncpy(ds_root, "/dev", D_ROOT_SZ);
    for (k = 1; k < argc; ++k) {
        if (0 == strcmp("-ide", argv[k]))
            do_ide = 1;
        else if (0 == strcmp("-i", argv[k]))
            do_inq = 1;
        else if (0 == strcmp("-l", argv[k]))
            do_leaf = 1;
        else if (0 == strcmp("-x", argv[k]))
            do_extra = 1;
        else if (0 == strcmp("-q", argv[k]))
            do_quiet = 1;
        else if (0 == strncmp("-d", argv[k], 2)) {
            if (strlen(argv[k]) > 2)
                strncpy(ds_root, argv[k] + 2, D_ROOT_SZ);
            else if (++k < argc)
                strncpy(ds_root, argv[k], D_ROOT_SZ);
        }
        else if ((0 == strcmp("-?", argv[k])) ||
                 (0 == strncmp("-h", argv[k], 2))) {
            printf("Scan SCSI devices within a devfs tree\n\n");
            usage();
            return 1;
        }
        else if (*argv[k] == '-') {
            printf("Unknown switch: %s\n", argv[k]);
            usage();
            return 1;
        }
        else if (*argv[k] != '-') {
            printf("Unknown argument\n");
            usage();
            return 1;
        }
    }
    strncpy(di_root, ds_root, D_ROOT_SZ);
    strcat(di_root, "/.devfsd");
    if (stat(di_root, &st) < 0)
        printf("Didn't find %s so perhaps devfs is not present,"
                " continuing ...\n", di_root);
    strncpy(di_root, ds_root, D_ROOT_SZ);
    strcat(ds_root, "/scsi");
    strcat(di_root, "/ide");

    if (do_ide)
        printf("SCSI scan:\n");
    res = hbtl_scan(ds_root, 0, larr);
    if (res < 0)
        printf("main: scsi hbtl_scan res=%d\n", res);
    do_inq = 0;  /* won't try SCSI INQUIRY on IDE devices */
    if (do_ide) {
        printf("\nIDE scan:\n");
        res = hbtl_scan(di_root, 0, larr);
        if (res < 0)
            printf("main: ide hbtl_scan res=%d\n", res);
    }
    return 0;
}