chiark / gitweb /
[PATCH] update volume_id
[elogind.git] / extras / multipath-tools / multipathd / devinfo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <sys/ioctl.h>
7 #include "devinfo.h"
8 #include "sg_include.h"
9
10 #define FILE_NAME_SIZE 255
11
12 static int
13 do_inq(int sg_fd, int cmddt, int evpd, unsigned int pg_op,
14        void *resp, int mx_resp_len, int noisy)
15 {
16         unsigned char inqCmdBlk[INQUIRY_CMDLEN] =
17             { INQUIRY_CMD, 0, 0, 0, 0, 0 };
18         unsigned char sense_b[SENSE_BUFF_LEN];
19         struct sg_io_hdr io_hdr;
20                                                                                                                  
21         if (cmddt)
22                 inqCmdBlk[1] |= 2;
23         if (evpd)
24                 inqCmdBlk[1] |= 1;
25         inqCmdBlk[2] = (unsigned char) pg_op;
26         inqCmdBlk[4] = (unsigned char) mx_resp_len;
27         memset(&io_hdr, 0, sizeof (struct sg_io_hdr));
28         io_hdr.interface_id = 'S';
29         io_hdr.cmd_len = sizeof (inqCmdBlk);
30         io_hdr.mx_sb_len = sizeof (sense_b);
31         io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
32         io_hdr.dxfer_len = mx_resp_len;
33         io_hdr.dxferp = resp;
34         io_hdr.cmdp = inqCmdBlk;
35         io_hdr.sbp = sense_b;
36         io_hdr.timeout = DEF_TIMEOUT;
37  
38         if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
39                 perror("SG_IO (inquiry) error");
40                 return -1;
41         }
42  
43         /* treat SG_ERR here to get rid of sg_err.[ch] */
44         io_hdr.status &= 0x7e;
45         if ((0 == io_hdr.status) && (0 == io_hdr.host_status) &&
46             (0 == io_hdr.driver_status))
47                 return 0;
48         if ((SCSI_CHECK_CONDITION == io_hdr.status) ||
49             (SCSI_COMMAND_TERMINATED == io_hdr.status) ||
50             (SG_ERR_DRIVER_SENSE == (0xf & io_hdr.driver_status))) {
51                 if (io_hdr.sbp && (io_hdr.sb_len_wr > 2)) {
52                         int sense_key;
53                         unsigned char * sense_buffer = io_hdr.sbp;
54                         if (sense_buffer[0] & 0x2)
55                                 sense_key = sense_buffer[1] & 0xf;
56                         else
57                                 sense_key = sense_buffer[2] & 0xf;
58                         if(RECOVERED_ERROR == sense_key)
59                                 return 0;
60                 }
61         }
62         return -1;
63 }
64
65 int
66 get_lun_strings(char * vendor_id, char * product_id, char * rev, char * devname)
67 {
68         int fd;
69         char buff[36];
70                                                                                                                  
71         /* ioctl style */
72         if ((fd = open(devname, O_RDONLY)) < 0)
73                 return 1;
74         if (0 != do_inq(fd, 0, 0, 0, buff, 36, 1))
75                 return 1;
76         memcpy(vendor_id, &buff[8], 8);
77         memcpy(product_id, &buff[16], 16);
78         memcpy(rev, &buff[32], 4);
79         close(fd);
80
81         return 0;
82 }