chiark / gitweb /
scsi_id: export ID_SERIAL_SHORT without vendor/product
[elogind.git] / extras / scsi_id / scsi_id.c
index 3a4a374d3e20e807531ac69cb20d8b690e0a3f6d..7fd6066c0174fc484d3b3d76104935f95069d361 100644 (file)
@@ -4,21 +4,14 @@
  * Main section of the scsi_id program
  *
  * Copyright (C) IBM Corp. 2003
+ * Copyright (C) SUSE Linux Products GmbH, 2006
  *
- *  This library is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU Lesser General Public License as
- *  published by the Free Software Foundation; either version 2.1 of the
- *  License, or (at your option) any later version.
+ * Author:
+ *     Patrick Mansfield<patmans@us.ibm.com>
  *
- *  This library is distributed in the hope that it will be useful, but
- *  WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- *  Lesser General Public License for more details.
- *
- *  You should have received a copy of the GNU Lesser General Public
- *  License along with this library; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- *  USA
+ *     This program is free software; you can redistribute it and/or modify it
+ *     under the terms of the GNU General Public License as published by the
+ *     Free Software Foundation version 2 of the License.
  */
 
 #include <stdio.h>
@@ -48,7 +41,7 @@ static int all_good;
 static int always_info;
 static int dev_specified;
 static int sys_specified;
-static char config_file[MAX_NAME_LEN] = SCSI_ID_CONFIG_FILE;
+static char config_file[MAX_PATH_LEN] = SCSI_ID_CONFIG_FILE;
 static int display_bus_id;
 static enum page_code default_page_code;
 static int use_stderr;
@@ -174,7 +167,7 @@ static int create_tmp_dev(const char *devpath, char *tmpdev, int dev_type)
                return -1;
        }
 
-       snprintf(tmpdev, MAX_NAME_LEN, "%s/%s-maj%d-min%d-%u",
+       snprintf(tmpdev, MAX_PATH_LEN, "%s/%s-maj%d-min%d-%u",
                 TMP_DIR, TMP_PREFIX, maj, min, getpid());
 
        dbg("tmpdev '%s'", tmpdev);
@@ -409,8 +402,7 @@ static int set_options(int argc, char **argv, const char *short_opts,
        /*
         * optind is a global extern used by getopt. Since we can call
         * set_options twice (once for command line, and once for config
-        * file) we have to reset this back to 1. [Note glibc handles
-        * setting this to 0, but klibc does not.]
+        * file) we have to reset this back to 1.
         */
        optind = 1;
        while (1) {
@@ -433,7 +425,8 @@ static int set_options(int argc, char **argv, const char *short_opts,
 
                case 'd':
                        dev_specified = 1;
-                       strncpy(maj_min_dev, optarg, MAX_NAME_LEN);
+                       strncpy(maj_min_dev, optarg, MAX_PATH_LEN);
+                       maj_min_dev[MAX_PATH_LEN-1] = '\0';
                        break;
 
                case 'e':
@@ -441,7 +434,8 @@ static int set_options(int argc, char **argv, const char *short_opts,
                        break;
 
                case 'f':
-                       strncpy(config_file, optarg, MAX_NAME_LEN);
+                       strncpy(config_file, optarg, MAX_PATH_LEN);
+                       config_file[MAX_PATH_LEN-1] = '\0';
                        break;
 
                case 'g':
@@ -467,8 +461,8 @@ static int set_options(int argc, char **argv, const char *short_opts,
 
                case 's':
                        sys_specified = 1;
-                       strncpy(target, optarg, MAX_NAME_LEN);
-                       target[MAX_NAME_LEN-1] = '\0';
+                       strncpy(target, optarg, MAX_PATH_LEN);
+                       target[MAX_PATH_LEN-1] = '\0';
                        break;
 
                case 'u':
@@ -620,11 +614,12 @@ static int scsi_id(const char *devpath, char *maj_min_dev)
 {
        int retval;
        int dev_type = 0;
-       char *serial, *unaligned_buf;
        struct sysfs_device *dev;
        struct sysfs_device *dev_scsi;
        int good_dev;
        int page_code;
+       char serial[MAX_SERIAL_LEN];
+       char serial_short[MAX_SERIAL_LEN];
 
        dbg("devpath %s\n", devpath);
 
@@ -640,18 +635,12 @@ static int scsi_id(const char *devpath, char *maj_min_dev)
                dev_type = S_IFCHR;
 
        /* get scsi parent device */
-       dev_scsi = sysfs_device_get_parent(dev);
+       dev_scsi = sysfs_device_get_parent_with_subsystem(dev, "scsi");
        if (dev_scsi == NULL) {
                err("unable to access parent device of '%s'", devpath);
                return 1;
        }
 
-       /* allow only scsi devices */
-       if (strcmp(dev_scsi->subsystem, "scsi") != 0) {
-               info("%s is not a scsi device", devpath);
-               return 1;
-       }
-
        /* mknod a temp dev to communicate with the device */
        if (!dev_specified && create_tmp_dev(dev->devpath, maj_min_dev, dev_type)) {
                dbg("create_tmp_dev failed\n");
@@ -662,36 +651,32 @@ static int scsi_id(const char *devpath, char *maj_min_dev)
        retval = per_dev_options(dev_scsi, &good_dev, &page_code);
        dbg("per dev options: good %d; page code 0x%x", good_dev, page_code);
 
-#define ALIGN   512
-       unaligned_buf = malloc(MAX_SERIAL_LEN + ALIGN);
-       serial = (char*) (((unsigned long) unaligned_buf + (ALIGN - 1))
-                         & ~(ALIGN - 1));
-       dbg("buffer unaligned 0x%p; aligned 0x%p\n", unaligned_buf, serial);
-#undef ALIGN
-
        if (!good_dev) {
                retval = 1;
        } else if (scsi_get_serial(dev_scsi, maj_min_dev, page_code,
-                                  serial, MAX_SERIAL_LEN)) {
+                                  serial, serial_short, MAX_SERIAL_LEN)) {
                retval = always_info?0:1;
        } else {
                retval = 0;
        }
        if (!retval) {
                if (export) {
-                       static char serial_str[64];
+                       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);
                        set_str(serial_str, serial, sizeof(serial_str));
                        printf("ID_SERIAL=%s\n", serial_str);
+                       set_str(serial_str, serial_short, sizeof(serial_str));
+                       printf("ID_SERIAL_SHORT=%s\n", serial_str);
                        printf("ID_TYPE=%s\n", type_str);
                        printf("ID_BUS=scsi\n");
                } else {
                        if (reformat_serial)
                                format_serial(serial);
                        if (display_bus_id)
-                               printf("%s: ", dev_scsi->kernel_name);
+                               printf("%s: ", dev_scsi->kernel);
                        printf("%s\n", serial);
                }
                dbg("%s\n", serial);
@@ -707,8 +692,8 @@ static int scsi_id(const char *devpath, char *maj_min_dev)
 int main(int argc, char **argv)
 {
        int retval = 0;
-       char devpath[MAX_NAME_LEN];
-       char maj_min_dev[MAX_NAME_LEN];
+       char devpath[MAX_PATH_LEN];
+       char maj_min_dev[MAX_PATH_LEN];
        int newargc;
        const char *env;
        char **newargv;
@@ -729,7 +714,7 @@ int main(int argc, char **argv)
        if (env) {
                hotplug_mode = 1;
                sys_specified = 1;
-               strncpy(devpath, env, MAX_NAME_LEN);
+               strncpy(devpath, env, MAX_PATH_LEN);
                devpath[sizeof(devpath)-1] = '\0';
        }