]> git.neil.brown.name Git - mdadm.git/blob - sg_io.c
Release mdadm-4.0
[mdadm.git] / sg_io.c
1 /*
2  * Copyright (C) 2007-2008 Intel Corporation
3  *
4  *      Retrieve drive serial numbers for scsi disks
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 #include <string.h>
20 #include <scsi/scsi.h>
21 #include <scsi/sg.h>
22 #include <sys/ioctl.h>
23
24 int scsi_get_serial(int fd, void *buf, size_t buf_len)
25 {
26         unsigned char rsp_buf[255];
27         unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, sizeof(rsp_buf), 0};
28         unsigned char sense[32];
29         struct sg_io_hdr io_hdr;
30         int rv;
31         unsigned int rsp_len;
32
33         memset(&io_hdr, 0, sizeof(io_hdr));
34         io_hdr.interface_id = 'S';
35         io_hdr.cmdp = inq_cmd;
36         io_hdr.cmd_len = sizeof(inq_cmd);
37         io_hdr.dxferp = rsp_buf;
38         io_hdr.dxfer_len = sizeof(rsp_buf);
39         io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
40         io_hdr.sbp = sense;
41         io_hdr.mx_sb_len = sizeof(sense);
42         io_hdr.timeout = 5000;
43
44         rv = ioctl(fd, SG_IO, &io_hdr);
45
46         if (rv)
47                 return rv;
48
49         rsp_len = rsp_buf[3];
50
51         if (!rsp_len || buf_len < rsp_len)
52                 return -1;
53
54         memcpy(buf, &rsp_buf[4], rsp_len);
55
56         return 0;
57 }