chiark / gitweb /
scsi_id: do not fail if no serial is found like for optical drives
authorKay Sievers <kay.sievers@vrfy.org>
Tue, 2 Dec 2008 18:23:38 +0000 (19:23 +0100)
committerKay Sievers <kay.sievers@vrfy.org>
Tue, 2 Dec 2008 18:23:38 +0000 (19:23 +0100)
extras/scsi_id/scsi_id.c
extras/scsi_id/scsi_id.h
extras/scsi_id/scsi_serial.c

index 943cbb2..21b7cbe 100644 (file)
@@ -538,58 +538,59 @@ static void format_serial(char *serial)
 
 /*
  * scsi_id: try to get an id, if one is found, printf it to stdout.
- * returns a value passed to exit() - 0 if printed an id, else 1. This
- * could be expanded, for example, if we want to report a failure like no
- * memory etc. return 2, and return 1 for expected cases (like broken
- * device found) that do not print an id.
+ * returns a value passed to exit() - 0 if printed an id, else 1.
  */
 static int scsi_id(struct udev *udev, char *maj_min_dev)
 {
-       int retval;
        struct scsi_id_device dev_scsi;
        int good_dev;
        int page_code;
-       char serial_short[MAX_SERIAL_LEN];
+       int retval = 0;
 
-       serial_short[0] = '\0';
-       set_inq_values(udev, &dev_scsi, maj_min_dev);
+       memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
+
+       if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
+               retval = 1;
+               goto out;
+       }
 
        /* get per device (vendor + model) options from the config file */
-       retval = per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
+       per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
        dbg(udev, "per dev options: good %d; page code 0x%x\n", good_dev, page_code);
-
        if (!good_dev) {
                retval = 1;
-       } else if (scsi_get_serial(udev,
-                                  &dev_scsi, maj_min_dev, page_code,
-                                  serial_short, MAX_SERIAL_LEN)) {
-               retval = 1;
-       } else {
-               retval = 0;
+               goto out;
        }
-       if (!retval) {
-               if (export) {
-                       char serial_str[MAX_SERIAL_LEN];
 
-                       printf("ID_VENDOR=%s\n", vendor_str);
-                       printf("ID_MODEL=%s\n", model_str);
-                       printf("ID_REVISION=%s\n", revision_str);
+       /* read serial number from mode pages (no values for optical drives) */
+       scsi_get_serial(udev, &dev_scsi, maj_min_dev, page_code, MAX_SERIAL_LEN);
+
+       if (export) {
+               char serial_str[MAX_SERIAL_LEN];
+
+               printf("ID_VENDOR=%s\n", vendor_str);
+               printf("ID_MODEL=%s\n", model_str);
+               printf("ID_REVISION=%s\n", revision_str);
+               printf("ID_TYPE=%s\n", type_str);
+               if (dev_scsi.serial[0] != '\0') {
                        udev_util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
                        udev_util_replace_chars(serial_str, NULL);
                        printf("ID_SERIAL=%s\n", serial_str);
-                       udev_util_replace_whitespace(serial_short, serial_str, sizeof(serial_str));
+                       udev_util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
                        udev_util_replace_chars(serial_str, NULL);
                        printf("ID_SERIAL_SHORT=%s\n", serial_str);
-                       printf("ID_TYPE=%s\n", type_str);
-               } else {
-                       if (reformat_serial)
-                               format_serial(dev_scsi.serial);
-                       printf("%s\n", dev_scsi.serial);
                }
-               dbg(udev, "%s\n", dev_scsi.serial);
-               retval = 0;
+               goto out;
        }
 
+       if (dev_scsi.serial[0] == '\0') {
+               retval = 1;
+               goto out;
+       }
+       if (reformat_serial)
+               format_serial(dev_scsi.serial);
+       printf("%s\n", dev_scsi.serial);
+out:
        return retval;
 }
 
index 48e4a58..9fcadec 100644 (file)
@@ -37,12 +37,13 @@ struct scsi_id_device {
        char type[33];
        char kernel[64];
        char serial[MAX_SERIAL_LEN];
+       char serial_short[MAX_SERIAL_LEN];
        int use_sg;
 };
 
 extern int scsi_std_inquiry(struct udev *udev, struct scsi_id_device *dev_scsi, const char *devname);
 extern int scsi_get_serial (struct udev *udev, struct scsi_id_device *dev_scsi, const char *devname,
-                           int page_code, char *serial_short, int len);
+                           int page_code, int len);
 
 /*
  * Page code values.
index 20d30cf..cbacf37 100644 (file)
@@ -837,7 +837,7 @@ out:
 
 int scsi_get_serial(struct udev *udev,
                    struct scsi_id_device *dev_scsi, const char *devname,
-                   int page_code, char *serial_short, int len)
+                   int page_code, int len)
 {
        unsigned char page0[SCSI_INQ_BUFF_LEN];
        int fd = -1;
@@ -860,7 +860,7 @@ int scsi_get_serial(struct udev *udev,
                return 1;
 
        if (page_code == PAGE_80) {
-               if (do_scsi_page80_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
+               if (do_scsi_page80_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len)) {
                        retval = 1;
                        goto completed;
                } else  {
@@ -868,7 +868,7 @@ int scsi_get_serial(struct udev *udev,
                        goto completed;
                }
        } else if (page_code == PAGE_83) {
-               if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
+               if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len)) {
                        retval = 1;
                        goto completed;
                } else  {
@@ -876,7 +876,7 @@ int scsi_get_serial(struct udev *udev,
                        goto completed;
                }
        } else if (page_code == PAGE_83_PRE_SPC3) {
-               retval = do_scsi_page83_prespc3_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len);
+               retval = do_scsi_page83_prespc3_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len);
                if (retval) {
                        /*
                         * Fallback to servicing a SPC-2/3 compliant page 83
@@ -884,7 +884,7 @@ int scsi_get_serial(struct udev *udev,
                         * conform to pre-SPC3 expectations.
                         */
                        if (retval == 2) {
-                               if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
+                               if (do_scsi_page83_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len)) {
                                        retval = 1;
                                        goto completed;
                                } else  {
@@ -924,7 +924,7 @@ int scsi_get_serial(struct udev *udev,
        for (ind = 4; ind <= page0[3] + 3; ind++)
                if (page0[ind] == PAGE_83)
                        if (!do_scsi_page83_inquiry(udev, dev_scsi, fd,
-                                                   dev_scsi->serial, serial_short, len)) {
+                                                   dev_scsi->serial, dev_scsi->serial_short, len)) {
                                /*
                                 * Success
                                 */
@@ -935,7 +935,7 @@ int scsi_get_serial(struct udev *udev,
        for (ind = 4; ind <= page0[3] + 3; ind++)
                if (page0[ind] == PAGE_80)
                        if (!do_scsi_page80_inquiry(udev, dev_scsi, fd,
-                                                   dev_scsi->serial, serial_short, len)) {
+                                                   dev_scsi->serial, dev_scsi->serial_short, len)) {
                                /*
                                 * Success
                                 */