aboutsummaryrefslogtreecommitdiff
path: root/sg_debug.c
blob: fb044febb715bd3fea811d50feca06659698fe49 (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include "sg_include.h"


/* Test code for D. Gilbert's extensions to the Linux OS SCSI generic ("sg")
   device driver.
*  Copyright (C) 1999 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 outputs debug information to the console/log for _all_
   active sg devices.

   Version 3.55 (20020115)
*/

#define EBUFF_SZ 256


int main(int argc, char * argv[])
{
    int fd, res, debug, t;
    char ebuff[EBUFF_SZ];
    
    if ((argc != 2) || ('-' == *argv[1])) {
        printf("Usage: sg_debug <sg_device>\n");
        return 1;
    }
    fd = open(argv[1], O_RDONLY | O_NONBLOCK);
    if (fd < 0) {
        if (EBUSY == errno)
            printf("Failed trying to open %s because it is busy\n", argv[1]);
        else {
            snprintf(ebuff, EBUFF_SZ, "sg_debug: Error trying to open %s ", 
	    	     argv[1]);
            perror(ebuff);
        }
        return 1;
    }
    res = ioctl(fd, SG_GET_VERSION_NUM, &t);
    if ((res >= 0) || (t >= 30000)) {
        printf("System is using sg version 3 driver. Hence the user can");
        printf(" execute:\n  'cat /proc/scsi/sg/debug' themselves. ");
        printf("Here is an example:\n");
	system("cat /proc/scsi/sg/debug");
        return 0;
    }
    debug = 10;
    res = ioctl(fd, SG_SET_DEBUG, &debug);
    if (res < 0) {
        perror("sg_debug: ioctl error on SG_SET_DEBUG");
        return 1;
    }
    
    res = close(fd);
    if (res < 0) {
        snprintf(ebuff, EBUFF_SZ, "sg_debug: trying to close %s ", argv[1]);
        perror(ebuff);
        return 1;
    }
    return 0;
}