chiark / gitweb /
[PATCH] start working on label support, and fix some segfaults for block devices.
[elogind.git] / namedev.c
index ee122cf075e52aa620d19e48c7879ae6aebb8b75..b8243682a71d05ee2c3e5cd13271d22958ed64ee 100644 (file)
--- a/namedev.c
+++ b/namedev.c
 #include "udev.h"
 #include "udev_version.h"
 #include "namedev.h"
+#include "libsysfs/libsysfs.h"
 
 #define TYPE_LABEL     "LABEL"
 #define TYPE_NUMBER    "NUMBER"
 #define TYPE_TOPOLOGY  "TOPOLOGY"
 #define TYPE_REPLACE   "REPLACE"
 
-enum config_type {
-       KERNEL_NAME     = 0,    /* must be 0 to let memset() default to this value */
-       LABEL           = 1,
-       NUMBER          = 2,
-       TOPOLOGY        = 3,
-       REPLACE         = 4,
-};
-
-#define BUS_SIZE       30
-#define FILE_SIZE      50
-#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 {
-       struct list_head node;
-
-       enum config_type type;
-
-       char bus[BUS_SIZE];
-       char sysfs_file[FILE_SIZE];
-       char sysfs_value[VALUE_SIZE];
-       char id[ID_SIZE];
-       char place[PLACE_SIZE];
-       char kernel_name[NAME_SIZE];
-       
-       /* what to set the device to */
-       int mode;
-       char name[NAME_SIZE];
-       char owner[OWNER_SIZE];
-       char group[GROUP_SIZE];
-};
-
-
 static LIST_HEAD(config_device_list);
 
+static void dump_dev(struct config_device *dev)
+{
+       switch (dev->type) {
+       case KERNEL_NAME:
+               dbg("KERNEL name ='%s'"
+                       " owner = '%s', group = '%s', mode = '%#o'",
+                       dev->attr.name, 
+                       dev->attr.owner, dev->attr.group, dev->attr.mode);
+               break;
+       case LABEL:
+               dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
+                       " owner = '%s', group = '%s', mode = '%#o'",
+                       dev->attr.name, dev->bus, dev->sysfs_file, dev->sysfs_value,
+                       dev->attr.owner, dev->attr.group, dev->attr.mode);
+               break;
+       case NUMBER:
+               dbg("NUMBER name = '%s', bus = '%s', id = '%s'"
+                       " owner = '%s', group = '%s', mode = '%#o'",
+                       dev->attr.name, dev->bus, dev->id,
+                       dev->attr.owner, dev->attr.group, dev->attr.mode);
+               break;
+       case TOPOLOGY:
+               dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'"
+                       " owner = '%s', group = '%s', mode = '%#o'",
+                       dev->attr.name, dev->bus, dev->place,
+                       dev->attr.owner, dev->attr.group, dev->attr.mode);
+               break;
+       case REPLACE:
+               dbg("REPLACE name = %s, kernel_name = %s"
+                       " owner = '%s', group = '%s', mode = '%#o'",
+                       dev->attr.name, dev->kernel_name,
+                       dev->attr.owner, dev->attr.group, dev->attr.mode);
+               break;
+       default:
+               dbg("Unknown type of device!");
+       }
+}
+
 #define copy_var(a, b, var)            \
        if (b->var)                     \
-               b->var = a->var;
+               a->var = b->var;
 
 #define copy_string(a, b, var)         \
        if (strlen(b->var))             \
-               strcpy(b->var, a->var);
+               strcpy(a->var, b->var);
 
 static int add_dev(struct config_device *new_dev)
 {
@@ -97,18 +98,18 @@ static int add_dev(struct config_device *new_dev)
         * this one... */
        list_for_each(tmp, &config_device_list) {
                struct config_device *dev = list_entry(tmp, struct config_device, node);
-               if (strcmp(dev->name, new_dev->name) == 0) {
+               if (strcmp(dev->attr.name, new_dev->attr.name) == 0) {
                        /* the same, copy the new info into this structure */
-                       copy_var(new_dev, dev, type);
-                       copy_var(new_dev, dev, mode);
-                       copy_string(new_dev, dev, bus);
-                       copy_string(new_dev, dev, sysfs_file);
-                       copy_string(new_dev, dev, sysfs_value);
-                       copy_string(new_dev, dev, id);
-                       copy_string(new_dev, dev, place);
-                       copy_string(new_dev, dev, kernel_name);
-                       copy_string(new_dev, dev, owner);
-                       copy_string(new_dev, dev, group);
+                       copy_var(dev, new_dev, type);
+                       copy_var(dev, new_dev, attr.mode);
+                       copy_string(dev, new_dev, bus);
+                       copy_string(dev, new_dev, sysfs_file);
+                       copy_string(dev, new_dev, sysfs_value);
+                       copy_string(dev, new_dev, id);
+                       copy_string(dev, new_dev, place);
+                       copy_string(dev, new_dev, kernel_name);
+                       copy_string(dev, new_dev, attr.owner);
+                       copy_string(dev, new_dev, attr.group);
                        return 0;
                }
        }
@@ -119,9 +120,20 @@ static int add_dev(struct config_device *new_dev)
                return -ENOMEM;
        memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
        list_add(&tmp_dev->node, &config_device_list);
+       //dump_dev(tmp_dev);
        return 0;
 }
 
+static void dump_dev_list(void)
+{
+       struct list_head *tmp;
+
+       list_for_each(tmp, &config_device_list) {
+               struct config_device *dev = list_entry(tmp, struct config_device, node);
+               dump_dev(dev);
+       }
+}
+
 static int get_value(const char *left, char **orig_string, char **ret_string)
 {
        char *temp;
@@ -147,6 +159,30 @@ static int get_value(const char *left, char **orig_string, char **ret_string)
        return -ENODEV;
 }
        
+static int get_pair(char **orig_string, char **left, char **right)
+{
+       char *temp;
+       char *string = *orig_string;
+
+       /* eat any whitespace */
+       while (isspace(*string))
+               ++string;
+
+       /* split based on '=' */
+       temp = strsep(&string, "=");
+       *left = temp;
+
+       /* take the right side and strip off the '"' */
+       while (isspace(*string))
+               ++string;
+       if (*string == '"')
+               ++string;
+       temp = strsep(&string, "\"");
+       *right = temp;
+       *orig_string = string;
+       
+       return 0;
+}
 
 static int namedev_init_config(void)
 {
@@ -188,7 +224,7 @@ static int namedev_init_config(void)
                if (*temp == COMMENT_CHARACTER)
                        continue;
 
-               memset(&dev, 0x00, sizeof(dev));
+               memset(&dev, 0x00, sizeof(struct config_device));
 
                /* parse the line */
                temp2 = strsep(&temp, ",");
@@ -201,7 +237,23 @@ static int namedev_init_config(void)
                        if (retval)
                                continue;
                        strcpy(dev.bus, temp3);
-                       dbg("LABEL name = %s, bus = %s", dev.name, dev.bus);
+
+                       /* file="value" */
+                       temp2 = strsep(&temp, ",");
+                       retval = get_pair(&temp, &temp2, &temp3);
+                       if (retval)
+                               continue;
+                       strcpy(dev.sysfs_file, temp2);
+                       strcpy(dev.sysfs_value, temp3);
+
+                       /* NAME="new_name" */
+                       temp2 = strsep(&temp, ",");
+                       retval = get_value("NAME", &temp, &temp3);
+                       if (retval)
+                               continue;
+                       strcpy(dev.attr.name, temp3);
+
+                       dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'", dev.attr.name, dev.bus, dev.sysfs_file, dev.sysfs_value);
                }
 
                if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
@@ -213,7 +265,22 @@ static int namedev_init_config(void)
                        if (retval)
                                continue;
                        strcpy(dev.bus, temp3);
-                       dbg("NUMBER name = %s, bus = %s", dev.name, dev.bus);
+
+                       /* ID="id" */
+                       temp2 = strsep(&temp, ",");
+                       retval = get_value("id", &temp, &temp3);
+                       if (retval)
+                               continue;
+                       strcpy(dev.id, temp3);
+
+                       /* NAME="new_name" */
+                       temp2 = strsep(&temp, ",");
+                       retval = get_value("NAME", &temp, &temp3);
+                       if (retval)
+                               continue;
+                       strcpy(dev.attr.name, temp3);
+
+                       dbg("NUMBER name = '%s', bus = '%s', id = '%s'", dev.attr.name, dev.bus, dev.id);
                }
 
                if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
@@ -225,7 +292,22 @@ static int namedev_init_config(void)
                        if (retval)
                                continue;
                        strcpy(dev.bus, temp3);
-                       dbg("TOPOLOGY name = %s, bus = %s", dev.name, dev.bus);
+
+                       /* PLACE="place" */
+                       temp2 = strsep(&temp, ",");
+                       retval = get_value("place", &temp, &temp3);
+                       if (retval)
+                               continue;
+                       strcpy(dev.place, temp3);
+
+                       /* NAME="new_name" */
+                       temp2 = strsep(&temp, ",");
+                       retval = get_value("NAME", &temp, &temp3);
+                       if (retval)
+                               continue;
+                       strcpy(dev.attr.name, temp3);
+
+                       dbg("TOPOLOGY name = '%s', bus = '%s', place = '%s'", dev.attr.name, dev.bus, dev.place);
                }
 
                if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
@@ -243,8 +325,8 @@ static int namedev_init_config(void)
                        retval = get_value("NAME", &temp, &temp3);
                        if (retval)
                                continue;
-                       strcpy(dev.name, temp3);
-                       dbg("REPLACE name = %s, kernel_name = %s", dev.name, dev.kernel_name);
+                       strcpy(dev.attr.name, temp3);
+                       dbg("REPLACE name = %s, kernel_name = %s", dev.attr.name, dev.kernel_name);
                }
 
                retval = add_dev(&dev);
@@ -303,17 +385,17 @@ static int namedev_init_permissions(void)
 
                /* parse the line */
                temp2 = strsep(&temp, ":");
-               strncpy(dev.name, temp2, sizeof(dev.name));
+               strncpy(dev.attr.name, temp2, sizeof(dev.attr.name));
 
                temp2 = strsep(&temp, ":");
-               strncpy(dev.owner, temp2, sizeof(dev.owner));
+               strncpy(dev.attr.owner, temp2, sizeof(dev.attr.owner));
 
                temp2 = strsep(&temp, ":");
-               strncpy(dev.group, temp2, sizeof(dev.owner));
+               strncpy(dev.attr.group, temp2, sizeof(dev.attr.owner));
 
-               dev.mode = strtol(temp, NULL, 8);
+               dev.attr.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.attr.name, dev.attr.owner, dev.attr.group, dev.attr.mode);
                retval = add_dev(&dev);
                if (retval) {
                        dbg("add_dev returned with error %d", retval);
@@ -326,12 +408,215 @@ exit:
        return retval;
 }      
 
+static int get_default_mode(struct sysfs_class_device *class_dev)
+{
+       /* just default everyone to rw for the world! */
+       return 0666;
+}
 
 
-int namedev_init(void)
+static int get_attr(struct sysfs_class_device *class_dev, struct device_attr *attr)
+{
+       struct list_head *tmp;
+       int retval = 0;
+       int found;
+
+       if (class_dev->sysdevice) {
+               dbg("class_dev->sysdevice->directory->path = '%s'", class_dev->sysdevice->directory->path);
+               dbg("class_dev->sysdevice->bus_id = '%s'", class_dev->sysdevice->bus_id);
+       } else {
+               dbg("class_dev->name = '%s'", class_dev->name);
+       }
+       list_for_each(tmp, &config_device_list) {
+               struct config_device *dev = list_entry(tmp, struct config_device, node);
+               switch (dev->type) {
+               case LABEL:
+                       {
+                       char *temp;
+
+                       dbg("LABEL: match file '%s' with value '%s'", dev->sysfs_file, dev->sysfs_value);
+                       /* try to find the attribute in the class device directory */
+                       temp = sysfs_get_value_from_attributes(class_dev->directory->attributes, dev->sysfs_file);
+                       if (temp)
+                               goto label_found;
+
+                       /* look in the class device device directory if present */
+                       if (class_dev->sysdevice) {
+                               temp = sysfs_get_value_from_attributes(class_dev->sysdevice->directory->attributes, dev->sysfs_file);
+                               if (temp)
+                                       goto label_found;
+                       }
+
+                       /* bah, let's go backwards up a level to see if the device is there,
+                        * as block partitions don't point to the physical device.  Need to fix that
+                        * up in the kernel...
+                        */
+                       if (strstr(class_dev->directory->path, "block")) {
+                               dbg("looking at block device...");
+                               if (isdigit(class_dev->directory->path[strlen(class_dev->directory->path)-1])) {
+                                       char path[SYSFS_PATH_MAX];
+                                       struct sysfs_class_device *class_dev_parent;
+
+                                       dbg("really is a partition...");
+                                       strcpy(path, class_dev->directory->path);
+                                       temp = strrchr(path, '/');
+                                       *temp = 0x00;
+                                       dbg("looking for a class device at '%s'", path);
+                                       class_dev_parent = sysfs_open_class_device(path);
+                                       if (class_dev_parent == NULL) {
+                                               dbg ("sysfs_open_class_device failed");
+                                               continue;
+                                       }
+                                       dbg("class_dev_parent->name = %s", class_dev_parent->name);
+
+                                       /* try to find the attribute in the class device directory */
+                                       temp = sysfs_get_value_from_attributes(class_dev_parent->directory->attributes, dev->sysfs_file);
+                                       if (temp) {
+                                               //sysfs_close_class_device(class_dev_parent);
+                                               goto label_found;
+                                       }
+
+                                       /* look in the class device device directory if present */
+                                       if (class_dev_parent->sysdevice) {
+                                               temp = sysfs_get_value_from_attributes(class_dev_parent->sysdevice->directory->attributes, dev->sysfs_file);
+                                               if (temp) {
+                                                       // sysfs_close_class_device(class_dev_parent);
+                                                       goto label_found;
+                                               }
+                                       }
+                                       
+                               }
+                       }
+                       continue;
+                                               
+label_found:
+                       dbg("file '%s' found with value '%s'", dev->sysfs_file, temp);
+                       if (strcmp(dev->sysfs_value, temp) != 0)
+                               continue;
+
+                       strcpy(attr->name, dev->attr.name);
+                       attr->mode = dev->attr.mode;
+                       strcpy(attr->owner, dev->attr.owner);
+                       strcpy(attr->group, dev->attr.group);
+                       dbg("file '%s' with value '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
+                               dev->sysfs_file, dev->sysfs_value, attr->name, 
+                               dev->attr.owner, dev->attr.group, dev->attr.mode);
+                       return retval;
+dbg("LABEL name = '%s', bus = '%s', sysfs_file = '%s', sysfs_value = '%s'"
+                               " owner = '%s', group = '%s', mode = '%#o'",
+                               dev->attr.name, dev->bus, dev->sysfs_file, dev->sysfs_value,
+                               dev->attr.owner, dev->attr.group, dev->attr.mode);
+                       break;
+                       }
+               case NUMBER:
+                       {
+                       char path[SYSFS_PATH_MAX];
+                       char *temp;
+
+                       found = 0;
+                       if (!class_dev->sysdevice)
+                               continue;
+                       strcpy(path, class_dev->sysdevice->directory->path);
+                       temp = strrchr(path, '/');
+                       dbg("NUMBER path = '%s'", path);
+                       dbg("NUMBER temp = '%s' id = '%s'", temp, dev->id);
+                       if (strstr(temp, dev->id) != NULL) {
+                               found = 1;
+                       } else {
+                               *temp = 0x00;
+                               temp = strrchr(path, '/');
+                               dbg("TOPOLOGY temp = '%s' id = '%s'", temp, dev->id);
+                               if (strstr(temp, dev->id) != NULL)
+                                       found = 1;
+                       }
+                       if (!found)
+                               continue;
+
+                       strcpy(attr->name, dev->attr.name);
+                       attr->mode = dev->attr.mode;
+                       strcpy(attr->owner, dev->attr.owner);
+                       strcpy(attr->group, dev->attr.group);
+                       dbg("device id '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
+                               dev->id, attr->name, 
+                               dev->attr.owner, dev->attr.group, dev->attr.mode);
+                       return retval;
+                       break;
+                       }
+               case TOPOLOGY:
+                       {
+                       char path[SYSFS_PATH_MAX];
+                       char *temp;
+
+                       if (!class_dev->sysdevice)
+                               continue;
+                       found = 0;      
+                       strcpy(path, class_dev->sysdevice->directory->path);
+                       temp = strrchr(path, '/');
+                       dbg("TOPOLOGY path = '%s'", path);
+                       dbg("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
+                       if (strstr(temp, dev->place) != NULL) {
+                               found = 1;
+                       } else {
+                               *temp = 0x00;
+                               temp = strrchr(path, '/');
+                               dbg("TOPOLOGY temp = '%s' place = '%s'", temp, dev->place);
+                               if (strstr(temp, dev->place) != NULL)
+                                       found = 1;
+                       }
+                       if (!found)
+                               continue;
+
+                       strcpy(attr->name, dev->attr.name);
+                       attr->mode = dev->attr.mode;
+                       strcpy(attr->owner, dev->attr.owner);
+                       strcpy(attr->group, dev->attr.group);
+                       dbg("device at '%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
+                               dev->place, attr->name, 
+                               dev->attr.owner, dev->attr.group, dev->attr.mode);
+                       return retval;
+                       break;
+                       }
+               case REPLACE:
+                       if (strcmp(dev->kernel_name, class_dev->name) != 0)
+                               continue;
+                       strcpy(attr->name, dev->attr.name);
+                       attr->mode = dev->attr.mode;
+                       strcpy(attr->owner, dev->attr.owner);
+                       strcpy(attr->group, dev->attr.group);
+                       dbg("'%s' becomes '%s' - owner = %s, group = %s, mode = %#o",
+                               dev->kernel_name, attr->name, 
+                               dev->attr.owner, dev->attr.group, dev->attr.mode);
+                       return retval;
+                       break;
+               case KERNEL_NAME:
+                       break;
+               default:
+                       dbg("Unknown type of device '%d'", dev->type);
+                       break;
+               }       
+       }
+       attr->mode = get_default_mode(class_dev);
+       attr->owner[0] = 0x00;
+       attr->group[0] = 0x00;
+       strcpy(attr->name, class_dev->name);
+       return retval;
+}
+
+int namedev_name_device(struct sysfs_class_device *class_dev, struct device_attr *attr)
 {
        int retval;
 
+       retval = get_attr(class_dev, attr);
+       if (retval)
+               dbg("get_attr failed");
+
+       return retval;
+}
+
+int namedev_init(void)
+{
+       int retval;
+       
        retval = namedev_init_config();
        if (retval)
                return retval;
@@ -340,6 +625,8 @@ int namedev_init(void)
        if (retval)
                return retval;
 
+       dump_dev_list();
        return retval;
 }
 
+