chiark / gitweb /
[PATCH] Make udev/udevstart be one binary
[elogind.git] / extras / scsi_id / scsi_serial.c
index 80a1d5163516785f92ada6515b876234efa7f821..e0074b662d3a9692b1c17f253c753eebfbc825b8 100644 (file)
 #include <stdlib.h>
 #include <unistd.h>
 #include <syslog.h>
+#include <linux/compiler.h> /* need __user when built via klibc */
 #include <scsi/sg.h>
-#ifdef __KLIBC__
-/*
- * Assume built under udev with KLIBC
- */
-#include <libsysfs.h>
-#else
 #include <sysfs/libsysfs.h>
-#endif
 #include "scsi_id.h"
 #include "scsi.h"
 
@@ -76,12 +70,6 @@ static const struct scsi_id_search_values id_search_list[] = {
 
 static const char hex_str[]="0123456789abcdef";
 
-/*
- * XXX maybe move all these to an sg_io.c file.
- *
- * From here ...
- */
-
 /*
  * Values returned in the result/status, only the ones used by the code
  * are used here.
@@ -166,8 +154,7 @@ static int sg_err_category3(struct sg_io_hdr *hp)
                                   hp->sbp, hp->sb_len_wr);
 }
 
-static int scsi_dump_sense(struct sysfs_class_device *scsi_dev,
-                          struct sg_io_hdr *io)
+static int scsi_dump_sense(struct sysfs_device *scsi_dev, struct sg_io_hdr *io)
 {
        unsigned char *sense_buffer;
        int s;
@@ -263,7 +250,7 @@ static int scsi_dump_sense(struct sysfs_class_device *scsi_dev,
                                    scsi_dev->name,  sense_buffer[0],
                                    sense_buffer[2]);
                log_message(LOG_WARNING,
-                           "%s: non-extended sense class %d code 0x%0x ",
+                           "%s: non-extended sense class %d code 0x%0x\n",
                            scsi_dev->name, sense_class, code);
 
        }
@@ -283,7 +270,7 @@ static int scsi_dump_sense(struct sysfs_class_device *scsi_dev,
        return -1;
 }
 
-static int scsi_dump(struct sysfs_class_device *scsi_dev, struct sg_io_hdr *io)
+static int scsi_dump(struct sysfs_device *scsi_dev, struct sg_io_hdr *io)
 {
        if (!io->status && !io->host_status && !io->msg_status &&
            !io->driver_status) {
@@ -304,9 +291,9 @@ static int scsi_dump(struct sysfs_class_device *scsi_dev, struct sg_io_hdr *io)
                return -1;
 }
 
-static int scsi_inquiry(struct sysfs_class_device *scsi_dev, int fd,
-                       unsigned char evpd, unsigned char page, unsigned
-                       char *buf, unsigned int buflen)
+static int scsi_inquiry(struct sysfs_device *scsi_dev, int fd, unsigned
+                       char evpd, unsigned char page, unsigned char *buf,
+                       unsigned int buflen)
 {
        unsigned char inq_cmd[INQUIRY_CMDLEN] =
                { INQUIRY_CMD, evpd, page, 0, buflen, 0 };
@@ -326,6 +313,8 @@ static int scsi_inquiry(struct sysfs_class_device *scsi_dev, int fd,
        buffer = inq + OFFSET;
 
 resend:
+       dprintf("%s evpd %d, page 0x%x\n", scsi_dev->name, evpd, page);
+
        memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
        io_hdr.interface_id = 'S';
        io_hdr.cmd_len = sizeof(inq_cmd);
@@ -338,7 +327,7 @@ resend:
        io_hdr.timeout = DEF_TIMEOUT;
 
        if (ioctl(fd, SG_IO, &io_hdr) < 0) {
-               log_message(LOG_WARNING, "%s ioctl failed: %s\n",
+               log_message(LOG_WARNING, "%s: ioctl failed: %s\n",
                            scsi_dev->name, strerror(errno));
                return -1;
        }
@@ -355,11 +344,6 @@ resend:
                        retval = scsi_dump(scsi_dev, &io_hdr);
        }
 
-       /*
-        * XXX where is the length checked? That is, was our request
-        * buffer long enough?
-        */
-
        if (!retval) {
                retval = buflen;
                memcpy(buf, buffer, retval);
@@ -371,17 +355,16 @@ resend:
                retval = -1;
        }
 
+       if (retval < 0)
+               log_message(LOG_WARNING,
+                           "%s: Unable to get INQUIRY vpd %d page 0x%x.\n",
+                           scsi_dev->name, evpd, page);
+
        free(inq);
        return retval;
 }
 
-/*
- * XXX maybe move all these to an sg_io.c file.
- *
- * Ending here.
- */
-
-static int do_scsi_page0_inquiry(struct sysfs_class_device *scsi_dev, int fd,
+static int do_scsi_page0_inquiry(struct sysfs_device *scsi_dev, int fd,
                                 char *buffer, int len)
 {
        int retval;
@@ -398,7 +381,7 @@ static int do_scsi_page0_inquiry(struct sysfs_class_device *scsi_dev, int fd,
                return 1;
        }
        if (buffer[3] > len) {
-               log_message(LOG_WARNING, "%s: page 0 buffer too long %d",
+               log_message(LOG_WARNING, "%s: page 0 buffer too long %d\n",
                           scsi_dev->name,  buffer[3]);
                return 1;
        }
@@ -423,7 +406,7 @@ static int do_scsi_page0_inquiry(struct sysfs_class_device *scsi_dev, int fd,
                        return 1;
                }
                if (!strncmp(&buffer[VENDOR_LENGTH], vendor, VENDOR_LENGTH)) {
-                       log_message(LOG_WARNING, "%s invalid page0 data\n",
+                       log_message(LOG_WARNING, "%s: invalid page0 data\n",
                                    scsi_dev->name);
                        return 1;
                }
@@ -435,8 +418,7 @@ static int do_scsi_page0_inquiry(struct sysfs_class_device *scsi_dev, int fd,
  * The caller checks that serial is long enough to include the vendor +
  * model.
  */
-static int prepend_vendor_model(struct sysfs_class_device *scsi_dev,
-                               char *serial)
+static int prepend_vendor_model(struct sysfs_device *scsi_dev, char *serial)
 {
        char attr[MAX_ATTR_LEN];
        int ind;
@@ -483,10 +465,9 @@ static int prepend_vendor_model(struct sysfs_class_device *scsi_dev,
  * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill
  * serial number.
  **/
-static int check_fill_0x83_id(struct sysfs_class_device *scsi_dev,
-                             char *page_83,
-                             const struct scsi_id_search_values *id_search,
-                             char *serial, int max_len)
+static int check_fill_0x83_id(struct sysfs_device *scsi_dev, char
+                             *page_83, const struct scsi_id_search_values
+                             *id_search, char *serial, int max_len)
 {
        int i, j, len;
 
@@ -571,7 +552,7 @@ static int check_fill_0x83_id(struct sysfs_class_device *scsi_dev,
        return 0;
 }
 
-static int do_scsi_page83_inquiry(struct sysfs_class_device *scsi_dev, int fd,
+static int do_scsi_page83_inquiry(struct sysfs_device *scsi_dev, int fd,
                                  char *serial, int len)
 {
        int retval;
@@ -628,7 +609,7 @@ static int do_scsi_page83_inquiry(struct sysfs_class_device *scsi_dev, int fd,
        return 1;
 }
 
-static int do_scsi_page80_inquiry(struct sysfs_class_device *scsi_dev, int fd,
+static int do_scsi_page80_inquiry(struct sysfs_device *scsi_dev, int fd,
                                  char *serial, int max_len)
 {
        int retval;
@@ -668,7 +649,7 @@ static int do_scsi_page80_inquiry(struct sysfs_class_device *scsi_dev, int fd,
        return 0;
 }
 
-int scsi_get_serial (struct sysfs_class_device *scsi_dev, const char *devname,
+int scsi_get_serial (struct sysfs_device *scsi_dev, const char *devname,
                     int page_code, char *serial, int len)
 {
        unsigned char page0[256];
@@ -680,9 +661,9 @@ int scsi_get_serial (struct sysfs_class_device *scsi_dev, const char *devname,
        }
        memset(serial, 0, len);
        dprintf("opening %s\n", devname);
-       fd = open(devname, O_RDONLY);
+       fd = open(devname, O_RDONLY | O_NONBLOCK);
        if (fd < 0) {
-               log_message(LOG_WARNING, "%s cannot open %s: %s\n",
+               log_message(LOG_WARNING, "%s: cannot open %s: %s\n",
                            scsi_dev->name, devname, strerror(errno));
                return 1;
        }
@@ -704,7 +685,7 @@ int scsi_get_serial (struct sysfs_class_device *scsi_dev, const char *devname,
                        goto completed;
                }
        } else if (page_code != 0x00) {
-               log_message(LOG_WARNING, "%s unsupported page code 0x%d\n",
+               log_message(LOG_WARNING, "%s: unsupported page code 0x%d\n",
                            scsi_dev->name, page_code);
                return 1;
        }
@@ -749,6 +730,7 @@ int scsi_get_serial (struct sysfs_class_device *scsi_dev, const char *devname,
        retval = 1;
 completed:
        if (close(fd) < 0)
-               log_message(LOG_WARNING, "close failed: %s", strerror(errno));
+               log_message(LOG_WARNING, "%s: close failed: %s\n", 
+                           scsi_dev->name, strerror(errno));
        return retval;
 }