chiark / gitweb /
scsi_id, usb_id: request device parent by subsystem
authorKay Sievers <kay.sievers@suse.de>
Wed, 18 Jan 2006 03:24:48 +0000 (04:24 +0100)
committerKay Sievers <kay.sievers@suse.de>
Wed, 18 Jan 2006 03:24:48 +0000 (04:24 +0100)
Request specific parents identified by subsystem and don't rely on
a predefined sequence.
Also let the devpath be longer than 72 chars, tsss ...

Signed-off-by: Kay Sievers <kay.sievers@suse.de>
extras/scsi_id/scsi_id.c
extras/scsi_id/scsi_id.h
extras/usb_id/usb_id.c
udev.h
udev_sysfs.c

index 3a4a374d3e20e807531ac69cb20d8b690e0a3f6d..6458f2b7ac1ce553c89f243f431b108ed2baeaa0 100644 (file)
@@ -48,7 +48,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 +174,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);
@@ -433,7 +433,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 +442,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 +469,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':
@@ -640,18 +642,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");
@@ -707,8 +703,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 +725,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';
        }
 
index b5312b89888d02ef78fbff275b056f45da23bdbb..758706ba25e036238bc763a911622adfcdac8ae9 100644 (file)
@@ -21,7 +21,7 @@
  *  USA
  */
 
-#define        MAX_NAME_LEN    72
+#define        MAX_PATH_LEN    512
 
 /*
  * MAX_ATTR_LEN: maximum length of the result of reading a sysfs
index 0bcd267f67aff5d385c25283aa82b11eacace2b5..62aa1506d7b4d3d6d1261b61cef43bae5754541a 100644 (file)
@@ -23,7 +23,7 @@
 
 #include "../../udev.h"
 
-#define MAX_NAME_LEN                   72
+#define MAX_PATH_LEN                   512
 #define MAX_SERIAL_LEN                 256
 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
 
@@ -247,7 +247,9 @@ static int usb_id(const char *devpath)
        struct sysfs_device *dev;
        struct sysfs_device *dev_scsi;
        struct sysfs_device *dev_target;
-       struct sysfs_device *dev_host, *dev_interface, *dev_usb;
+       struct sysfs_device *dev_host;
+       struct sysfs_device *dev_interface;
+       struct sysfs_device *dev_usb;
        const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
        const char *usb_model = NULL, *usb_vendor = NULL, *usb_rev, *usb_serial;
        const char *if_class, *if_subclass;
@@ -263,14 +265,9 @@ static int usb_id(const char *devpath)
        }
 
        /* 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);
+               err("unable to find parent 'scsi' device of '%s'", devpath);
                return 1;
        }
 
@@ -289,20 +286,16 @@ static int usb_id(const char *devpath)
        }
 
        /* usb interface directory */
-       dev_interface = sysfs_device_get_parent(dev_host);
+       dev_interface = sysfs_device_get_parent_with_subsystem(dev_host, "usb");
        if (dev_interface == NULL) {
                err("unable to access parent device of '%s'", devpath);
                return 1;
        }
 
        /* usb device directory */
-       dev_usb = sysfs_device_get_parent(dev_interface);
+       dev_usb = sysfs_device_get_parent_with_subsystem(dev_interface, "usb");
        if (dev_usb == NULL) {
-               err("unable to access parent device of '%s'", devpath);
-               return 1;
-       }
-       if (strcmp(dev_interface->subsystem, "usb") != 0) {
-               info("%s is not an usb device", devpath);
+               err("unable to find parent 'usb' device of '%s'", devpath);
                return 1;
        }
 
@@ -397,7 +390,7 @@ int main(int argc, char **argv)
 {
        int retval = 0;
        const char *env;
-       char devpath[MAX_NAME_LEN];
+       char devpath[MAX_PATH_LEN];
        int option;
 
        logging_init("usb_id");
diff --git a/udev.h b/udev.h
index 5d54b5443248c894f142169de404bad78d8f4086..0c7ca12f10def122a55a8359cc2968df491e2910 100644 (file)
--- a/udev.h
+++ b/udev.h
@@ -113,6 +113,7 @@ extern void sysfs_cleanup(void);
 extern void sysfs_device_set_values(struct sysfs_device *dev, const char *devpath, const char *subsystem);
 extern struct sysfs_device *sysfs_device_get(const char *devpath);
 extern struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev);
+extern struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem);
 extern char *sysfs_attr_get_value(const char *devpath, const char *attr_name);
 
 /* udev_add.c */
index 983837980338e8de0a219b991063689188f08413..172f0ce1fdd4b3e95999c4f9f5c1ee0043c9bdd4 100644 (file)
@@ -306,6 +306,19 @@ device_link:
        return sysfs_device_get(parent_devpath);
 }
 
+struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem)
+{
+       struct sysfs_device *dev_parent;
+
+       dev_parent = sysfs_device_get_parent(dev);
+       while (dev_parent != NULL) {
+               if (strcmp(dev_parent->subsystem, subsystem) == 0)
+                       return dev_parent;
+               dev_parent = sysfs_device_get_parent(dev_parent);
+       }
+       return NULL;
+}
+
 char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
 {
        char path_full[PATH_SIZE];