chiark / gitweb /
[PATCH] add initial libsysfs support...
authorgreg@kroah.com <greg@kroah.com>
Sat, 19 Jul 2003 05:48:28 +0000 (22:48 -0700)
committerGreg KH <gregkh@suse.de>
Wed, 27 Apr 2005 04:01:39 +0000 (21:01 -0700)
needs lots more cleanup, but is much nicer than doing this by hand...

namedev.c
namedev.h
udev.c
udev.h

index 0271ba55a2ad4091f382c506c87a559aa456dd57..b8a21a25ae5f48bb4b866f5316901a86c209920a 100644 (file)
--- a/namedev.c
+++ b/namedev.c
@@ -34,6 +34,7 @@
 #include "udev.h"
 #include "udev_version.h"
 #include "namedev.h"
+#include "libsysfs/libsysfs.h"
 
 #define TYPE_LABEL     "LABEL"
 #define TYPE_NUMBER    "NUMBER"
@@ -53,9 +54,6 @@ enum config_type {
 #define VALUE_SIZE     100
 #define ID_SIZE                50
 #define PLACE_SIZE     50
-#define NAME_SIZE      100
-#define OWNER_SIZE     30
-#define GROUP_SIZE     30
 
 
 struct config_device {
@@ -80,36 +78,38 @@ struct config_device {
 
 static LIST_HEAD(config_device_list);
 
+static char sysfs_path[SYSFS_PATH_MAX];
+
 static void dump_dev(struct config_device *dev)
 {
        switch (dev->type) {
        case KERNEL_NAME:
                dbg("KERNEL name ='%s'"
-                       " owner = '%s', group = '%s', mode = '%d'", 
+                       " owner = '%s', group = '%s', mode = '%#o'",
                        dev->name, 
                        dev->owner, dev->group, dev->mode);
                break;
        case LABEL:
                dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
-                       " owner = '%s', group = '%s', mode = '%d'",
+                       " owner = '%s', group = '%s', mode = '%#o'",
                        dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value,
                        dev->owner, dev->group, dev->mode);
                break;
        case NUMBER:
                dbg("NUMBER name = '%s', bus = '%s', id = '%s'"
-                       " owner = '%s', group = '%s', mode = '%d'",
+                       " owner = '%s', group = '%s', mode = '%#o'",
                        dev->name, dev->bus, dev->id,
                        dev->owner, dev->group, dev->mode);
                break;
        case TOPOLOGY:
                dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
-                       " owner = '%s', group = '%s', mode = '%d'",
+                       " owner = '%s', group = '%s', mode = '%#o'",
                        dev->name, dev->bus, dev->place,
                        dev->owner, dev->group, dev->mode);
                break;
        case REPLACE:
                dbg("REPLACE name = %s, kernel_name = %s"
-                       " owner = '%s', group = '%s', mode = '%d'",
+                       " owner = '%s', group = '%s', mode = '%#o'",
                        dev->name, dev->kernel_name,
                        dev->owner, dev->group, dev->mode);
                break;
@@ -432,7 +432,7 @@ static int namedev_init_permissions(void)
 
                dev.mode = strtol(temp, NULL, 8);
 
-               dbg("name = %s, owner = %s, group = %s, mode = %x", dev.name, dev.owner, dev.group, dev.mode);
+               dbg("name = %s, owner = %s, group = %s, mode = %#o", dev.name, dev.owner, dev.group, dev.mode);
                retval = add_dev(&dev);
                if (retval) {
                        dbg("add_dev returned with error %d", retval);
@@ -446,10 +446,103 @@ exit:
 }      
 
 
+static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
+{
+       char temp[3];
+       int retval = 0;
+
+       char *dev;
+
+       dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
+       if (dev == NULL)
+               return -ENODEV;
+
+       dbg("dev = %s", dev);
+
+       temp[0] = dev[0];
+       temp[1] = dev[1];
+       temp[2] = 0x00;
+       *major = (int)strtol(&temp[0], NULL, 16);
+
+       temp[0] = dev[2];
+       temp[1] = dev[3];
+       temp[2] = 0x00;
+       *minor = (int)strtol(&temp[0], NULL, 16);
+
+       dbg("found major = %d, minor = %d", *major, *minor);
+
+       retval = 0;
+       return retval;
+}
+
+static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *attr)
+{
+       struct list_head *tmp;
+       int retval = 0;
+
+       retval = get_major_minor(class_dev, &attr->major, &attr->minor);
+       if (retval) {
+               dbg ("get_major_minor failed");
+               goto exit;
+       }
+
+       list_for_each(tmp, &config_device_list) {
+               struct config_device *dev = list_entry(tmp, struct config_device, node);
+               if (strcmp(dev->name, class_dev->name) == 0) {
+                       attr->mode = dev->mode;
+                       strcpy(attr->owner, dev->owner);
+                       strcpy(attr->group, dev->group);
+                       /* FIXME  put the proper name here!!! */
+                       strcpy(attr->name, dev->name);
+                       dbg("%s - owner = %s, group = %s, mode = %#o", dev->name, dev->owner, dev->group, dev->mode);
+                       goto exit;
+               }
+       }
+       attr->mode = 0666;
+       attr->owner[0] = 0x00;
+       attr->group[0] = 0x00;
+       strcpy(attr->name, class_dev->name);
+exit:
+       return retval;
+}
+
+int namedev_name_device(char *device_name, struct device_attr *attr)
+{
+       char dev_path[SYSFS_PATH_MAX];
+       struct sysfs_class_device *class_dev;
+       int retval;
+
+       strcpy(dev_path, sysfs_path);
+       strcat(dev_path, device_name);
+
+       dbg("looking at %s", dev_path);
+
+       /* open up the sysfs class device for this thing... */
+       class_dev = sysfs_open_class_device(dev_path);
+       if (class_dev == NULL) {
+               dbg ("sysfs_open_class_device failed");
+               return -ENODEV;
+       }
+       dbg("class_dev->name = %s", class_dev->name);
+       
+       retval = get_attr(class_dev, attr);
+       if (retval) {
+               dbg ("get_attr failed");
+               goto exit;
+       }
+exit:
+       sysfs_close_class_device(class_dev);
+       return retval;
+}
 
 int namedev_init(void)
 {
        int retval;
+       
+       retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
+       if (retval)
+               return retval;
+       dbg("sysfs_path = %s", sysfs_path);
 
        retval = namedev_init_config();
        if (retval)
@@ -463,3 +556,4 @@ int namedev_init(void)
        return retval;
 }
 
+
index 0089b4d240a416157c1ab658bb95da3514ed3b3c..1fe58600156f9772d9d3f0c2d25623a179175ee3 100644 (file)
--- a/namedev.h
+++ b/namedev.h
@@ -30,5 +30,6 @@
 #define NAMEDEV_CONFIG_FILE            "namedev.config"
 
 extern int namedev_init(void);
+extern int namedev_name_device(char *device_name, struct device_attr *attr);
 
 #endif
diff --git a/udev.c b/udev.c
index bf170d1651d55c69c253d72144f8bb9b07d99bb8..dc5f5b8d8944006125b6d941d3c0e8da467df3aa 100644 (file)
--- a/udev.c
+++ b/udev.c
@@ -50,7 +50,8 @@ static char *get_device(void)
        temp = getenv("DEVPATH");
        if (temp == NULL)
                return NULL;
-       strcpy(device, SYSFS_ROOT);
+       strcpy(device, "");
+//     strcpy(device, SYSFS_ROOT);
        strcat(device, temp);
 
        return device;
@@ -198,14 +199,14 @@ static int delete_node(char *name)
        return unlink(filename);
 }
 
-static int add_device(char *device, char type)
+static int add_device(char *device, char type, struct device_attr *attr)
 {
        char *name;
        int major;
        int minor;
        int mode;
        int retval = -EINVAL;
-
+#if 0
        retval = get_major_minor(device, &major, &minor);
        if (retval) {
                dbg ("get_major_minor failed");
@@ -225,8 +226,8 @@ static int add_device(char *device, char type)
                retval = -EINVAL;
                goto exit;
        }
-
-       return create_node(name, type, major, minor, mode);
+#endif
+       return create_node(attr->name, type, attr->major, attr->minor, attr->mode);
 
 exit:
        return retval;
@@ -252,6 +253,7 @@ exit:
 
 int main(int argc, char *argv[])
 {
+       struct device_attr attr;
        char *subsystem;
        char *action;
        char *device;
@@ -290,8 +292,12 @@ int main(int argc, char *argv[])
        }
        dbg("looking at %s", device);
 
+       retval = namedev_name_device(device, &attr);
+       if (retval)
+               return retval;
+
        if (strcmp(action, "add") == 0)
-               return add_device(device, type);
+               return add_device(device, type, &attr);
 
        if (strcmp(action, "remove") == 0)
                return remove_device(device);
diff --git a/udev.h b/udev.h
index af1b6bbb8d0f2c8ad43449ec58992203bc24dfc9..c82dbebded625ce48f2d05256f7ffe907f2ee0d9 100644 (file)
--- a/udev.h
+++ b/udev.h
 extern int log_message (int level, const char *format, ...)
        __attribute__ ((format (printf, 2, 3)));
 
+#define NAME_SIZE      100
+#define OWNER_SIZE     30
+#define GROUP_SIZE     30
+
+struct device_attr {
+       int major;
+       int minor;
+       int mode;
+       char name[NAME_SIZE];
+       char owner[OWNER_SIZE];
+       char group[GROUP_SIZE];
+};
+
 
 #endif