]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - sg_io.c
sysfs: Use the presence of /sys/block/<dev>/md as indicator of valid device
[thirdparty/mdadm.git] / sg_io.c
diff --git a/sg_io.c b/sg_io.c
index f9682be39fc3e9f8f9d42528386c3748499d6b36..42c91e1e9c6be4deb7a96d7a78c0c29dd9aaaa01 100644 (file)
--- a/sg_io.c
+++ b/sg_io.c
@@ -1,7 +1,7 @@
 /*
  * Copyright (C) 2007-2008 Intel Corporation
  *
- *     Retrieve drive serial numbers for scsi disks
+ *     Retrieve drive serial numbers for scsi disks
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms and conditions of the GNU General Public License,
 
 int scsi_get_serial(int fd, void *buf, size_t buf_len)
 {
-       unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, buf_len, 0};
+       unsigned char rsp_buf[255];
+       unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, sizeof(rsp_buf), 0};
        unsigned char sense[32];
        struct sg_io_hdr io_hdr;
+       int rv;
+       unsigned int rsp_len;
 
        memset(&io_hdr, 0, sizeof(io_hdr));
        io_hdr.interface_id = 'S';
        io_hdr.cmdp = inq_cmd;
        io_hdr.cmd_len = sizeof(inq_cmd);
-       io_hdr.dxferp = buf;
-       io_hdr.dxfer_len = buf_len;
+       io_hdr.dxferp = rsp_buf;
+       io_hdr.dxfer_len = sizeof(rsp_buf);
        io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
        io_hdr.sbp = sense;
        io_hdr.mx_sb_len = sizeof(sense);
        io_hdr.timeout = 5000;
 
-       return ioctl(fd, SG_IO, &io_hdr);
+       rv = ioctl(fd, SG_IO, &io_hdr);
+
+       if (rv)
+               return rv;
+
+       rsp_len = rsp_buf[3];
+
+       if (!rsp_len || buf_len < rsp_len)
+               return -1;
+
+       memcpy(buf, &rsp_buf[4], rsp_len);
+
+       return 0;
 }