chiark / gitweb /
[PATCH] v007 Changelog
[elogind.git] / namedev.c
index 3b19aaf9099c8de0a848adb557d17e097f415f18..e640b93c3dc94ec29b0a284be38796020b60ae22 100644 (file)
--- a/namedev.c
+++ b/namedev.c
 
 static LIST_HEAD(config_device_list);
 
+/* s2 may end with '*' to match everything */
+static int strncmp_wildcard(char *s1, char *s2, int max)
+{
+       int len = strlen(s2);
+       if (len > max)
+               len = max;
+       if (s2[len-1] == '*')
+               len--;
+       else
+               len = max;
+       return strncmp(s1, s2, len);
+}
+
 static void dump_dev(struct config_device *dev)
 {
        switch (dev->type) {
@@ -83,9 +96,9 @@ static void dump_dev(struct config_device *dev)
                          dev->owner, dev->group, dev->mode);
                break;
        case CALLOUT:
-               dbg_parse("CALLOUT name='%s', program='%s', bus='%s', id='%s', "
+               dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s', "
                          "owner='%s', group='%s', mode=%#o",
-                         dev->name, dev->exec_program, dev->bus, dev->id,
+                         dev->name, dev->bus, dev->exec_program, dev->id,
                          dev->owner, dev->group, dev->mode);
                break;
        default:
@@ -109,16 +122,8 @@ static int add_dev(struct config_device *new_dev)
        /* update the values if we already have the device */
        list_for_each(tmp, &config_device_list) {
                struct config_device *dev = list_entry(tmp, struct config_device, node);
-               int len = strlen(new_dev->name);
-               if (new_dev->name[len-1] == '*') {
-                       len--;
-                       if (strncmp(dev->name, new_dev->name, len))
-                               continue;
-               } else {
-                       if (strcmp(dev->name, new_dev->name))
-                               continue;
-               }
-               /* the same, copy the new info into this structure */
+               if (strncmp_wildcard(dev->name, new_dev->name, sizeof(dev->name)))
+                       continue;
                copy_var(dev, new_dev, type);
                copy_var(dev, new_dev, mode);
                copy_string(dev, new_dev, bus);
@@ -271,8 +276,8 @@ static int namedev_init_config(void)
                        strfieldcpy(dev.name, temp3);
 
                        dbg_parse("LABEL name='%s', bus='%s', "
-                                 "sysfs_file='%s', sysfs_value='%s'", 
-                                 dev.name, dev.bus, dev.sysfs_file, 
+                                 "sysfs_file='%s', sysfs_value='%s'",
+                                 dev.name, dev.bus, dev.sysfs_file,
                                  dev.sysfs_value);
                }
 
@@ -417,7 +422,6 @@ static int namedev_init_permissions(void)
 
        /* loop through the whole file */
        while (1) {
-               /* get a line */
                temp = fgets(line, sizeof(line), fd);
                if (temp == NULL)
                        break;
@@ -483,6 +487,66 @@ static mode_t get_default_mode(struct sysfs_class_device *class_dev)
        return 0666;
 }
 
+static void build_kernel_number(struct sysfs_class_device *class_dev, struct udevice *udev)
+{
+       char *dig;
+
+       /* FIXME, figure out how to handle stuff like sdaj which will not work right now. */
+       dig = class_dev->name + strlen(class_dev->name);
+       while (isdigit(*(dig-1)))
+               dig--;
+       strfieldcpy(udev->kernel_number, dig);
+       dbg("kernel_number = %s", udev->kernel_number);
+}
+
+static void apply_format(struct udevice *udev, unsigned char *string)
+{
+       char name[NAME_SIZE];
+       char *pos;
+
+       while (1) {
+               pos = strchr(string, '%');
+
+               if (pos) {
+                       strfieldcpy(name, pos+2);
+                       *pos = 0x00;
+                       switch (pos[1]) {
+                       case 'b':
+                               if (strlen(udev->bus_id) == 0)
+                                       break;
+                               strcat(string, udev->bus_id);
+                               dbg("substitute bus_id '%s'", udev->bus_id);
+                               break;
+                       case 'n':
+                               if (strlen(udev->kernel_number) == 0)
+                                       break;
+                               strcat(pos, udev->kernel_number);
+                               dbg("substitute kernel number '%s'", udev->kernel_number);
+                               break;
+                       case 'm':
+                               sprintf(pos, "%u", udev->minor);
+                               dbg("substitute minor number '%u'", udev->minor);
+                               break;
+                       case 'M':
+                               sprintf(pos, "%u", udev->major);
+                               dbg("substitute major number '%u'", udev->major);
+                               break;
+                       case 'c':
+                               if (strlen(udev->callout_value) == 0)
+                                       break;
+                               strcat(pos, udev->callout_value);
+                               dbg("substitute callout output '%s'", udev->callout_value);
+                               break;
+                       default:
+                               dbg("unknown substitution type '%%%c'", pos[1]);
+                               break;
+                       }
+                       strcat(string, name);
+               } else
+                       break;
+       }
+}
+
 
 static int exec_callout(struct config_device *dev, char *value, int len)
 {
@@ -522,7 +586,7 @@ static int exec_callout(struct config_device *dev, char *value, int len)
                                        break;
                        }
                        if (args[i]) {
-                               dbg("to many args - %d", i);
+                               dbg("too many args - %d", i);
                                args[i] = NULL;
                        }
                        retval = execve(args[0], args, main_envp);
@@ -577,16 +641,17 @@ static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev
 {
        struct config_device *dev;
        struct list_head *tmp;
-       char value[ID_SIZE];
 
        list_for_each(tmp, &config_device_list) {
                dev = list_entry(tmp, struct config_device, node);
                if (dev->type != CALLOUT)
                        continue;
 
-               if (exec_callout(dev, value, sizeof(value)))
+               /* substitute anything that needs to be in the program name */
+               apply_format(udev, dev->exec_program);
+               if (exec_callout(dev, udev->callout_value, NAME_SIZE))
                        continue;
-               if (strncmp(value, dev->id, sizeof(value)) != 0)
+               if (strncmp_wildcard(udev->callout_value, dev->id, NAME_SIZE) != 0)
                        continue;
                strfieldcpy(udev->name, dev->name);
                if (dev->mode != 0) {
@@ -785,17 +850,12 @@ static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *
        int len;
 
        strfieldcpy(udev->name, class_dev->name);
+       /* look for permissions */
        list_for_each(tmp, &config_device_list) {
                dev = list_entry(tmp, struct config_device, node);
                len = strlen(dev->name);
-               if (dev->name[len-1] == '*') {
-                       len--;
-                       if (strncmp(dev->name, class_dev->name, len))
-                               continue;
-               } else {
-                       if (strcmp(dev->name, class_dev->name))
-                               continue;
-               }
+               if (strncmp_wildcard(class_dev->name, dev->name, sizeof(dev->name)))
+                       continue;
                if (dev->mode != 0) {
                        dbg_parse("found permissions for '%s'", class_dev->name);
                        udev->mode = dev->mode;
@@ -848,10 +908,13 @@ static int get_attr(struct sysfs_class_device *class_dev, struct udevice *udev)
        if (sysfs_device) {
                dbg_parse("sysfs_device->path='%s'", sysfs_device->path);
                dbg_parse("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
+               strfieldcpy(udev->bus_id, sysfs_device->bus_id);
        } else {
                dbg_parse("class_dev->name = '%s'", class_dev->name);
        }
 
+       build_kernel_number(class_dev, udev);
+
        /* rules are looked at in priority order */
        retval = do_callout(class_dev, udev);
        if (retval == 0)
@@ -878,44 +941,7 @@ static int get_attr(struct sysfs_class_device *class_dev, struct udevice *udev)
 
 found:
        /* substitute placeholder in NAME  */
-       while (1) {
-               char *pos = strchr(udev->name, '%');
-               char *dig;
-               char name[NAME_SIZE];
-               if (pos) {
-                       strfieldcpy(name, pos+2);
-                       *pos = 0x00;
-                       switch (pos[1]) {
-                       case 'b':
-                               if (!sysfs_device)
-                                       break;
-                               strcat(udev->name, sysfs_device->bus_id);
-                               dbg("bus_id inserted: %s", 
-                                               sysfs_device->bus_id);
-                               break;
-                       case 'n':
-                               dig = class_dev->name + strlen(class_dev->name);
-                               while (isdigit(*(dig-1)))
-                                       dig--;
-                               strcat(udev->name, dig);
-                               dbg("substitute kernel number '%s'", dig);
-                               break;
-                       case 'm':
-                               sprintf(pos, "%u", udev->minor);
-                               dbg("substitute minor number '%u'", udev->minor);
-                               break;
-                       case 'M':
-                               sprintf(pos, "%u", udev->major);
-                               dbg("substitute major number '%u'", udev->major);
-                               break;
-                       default:
-                               dbg("unknown substitution type '%%%c'", pos[1]);
-                               break;
-                       }
-                       strcat(udev->name, name);
-               } else
-                       break;
-       }
+       apply_format(udev, udev->name);
 
 done:
        /* mode was never set above */
@@ -957,5 +983,3 @@ int namedev_init(void)
        dump_dev_list();
        return retval;
 }
-
-