chiark / gitweb /
add flag for reading of precompiled rules
[elogind.git] / udev_rules.c
index 939febe8f69e20986a5b4629f69cd5f5caddbccc..d42b219d7d4697bedac9032e81167700c778a707 100644 (file)
@@ -1,12 +1,9 @@
 /*
  * udev_rules.c
  *
- * Userspace devfs
- *
  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
  * Copyright (C) 2003-2005 Kay Sievers <kay.sievers@vrfy.org>
  *
- *
  *     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 "udev_db.h"
 
 
-/* compare string with pattern (supports * ? [0-9] [!A-Z]) */
-static int strcmp_pattern(const char *p, const char *s)
-{
-       if (s[0] == '\0') {
-               while (p[0] == '*')
-                       p++;
-               return (p[0] != '\0');
-       }
-       switch (p[0]) {
-       case '[':
-               {
-                       int not = 0;
-                       p++;
-                       if (p[0] == '!') {
-                               not = 1;
-                               p++;
-                       }
-                       while ((p[0] != '\0') && (p[0] != ']')) {
-                               int match = 0;
-                               if (p[1] == '-') {
-                                       if ((s[0] >= p[0]) && (s[0] <= p[2]))
-                                               match = 1;
-                                       p += 3;
-                               } else {
-                                       match = (p[0] == s[0]);
-                                       p++;
-                               }
-                               if (match ^ not) {
-                                       while ((p[0] != '\0') && (p[0] != ']'))
-                                               p++;
-                                       if (p[0] == ']')
-                                               return strcmp_pattern(p+1, s+1);
-                               }
-                       }
-               }
-               break;
-       case '*':
-               if (strcmp_pattern(p, s+1))
-                       return strcmp_pattern(p+1, s);
-               return 0;
-       case '\0':
-               if (s[0] == '\0') {
-                       return 0;
-               }
-               break;
-       default:
-               if ((p[0] == s[0]) || (p[0] == '?'))
-                       return strcmp_pattern(p+1, s+1);
-               break;
-       }
-       return 1;
-}
-
 /* extract possible {attr} and move str behind it */
 static char *get_format_attribute(char **str)
 {
@@ -243,7 +187,8 @@ static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bu
                if (bufline[0] == COMMENT_CHARACTER)
                        continue;
 
-               strlcpy(line, bufline, count+1);
+               memcpy(line, bufline, count);
+               line[count] = '\0';
 
                linepos = line;
                if (get_key(&linepos, &variable, &value) == 0) {
@@ -351,6 +296,7 @@ static int find_free_number(struct udevice *udev, const char *name)
 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
                                const char *name, char *value, size_t len)
 {
+       struct sysfs_class_device *class_dev_parent;
        struct sysfs_attribute *tmpattr;
 
        dbg("look for device attribute '%s'", name);
@@ -359,6 +305,12 @@ static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sys
                tmpattr = sysfs_get_classdev_attr(class_dev, name);
                if (tmpattr)
                        goto attr_found;
+               class_dev_parent = sysfs_get_classdev_parent(class_dev);
+               if (class_dev_parent) {
+                       tmpattr = sysfs_get_classdev_attr(class_dev_parent, name);
+                       if (tmpattr)
+                               goto attr_found;
+               }
        }
        if (sysfs_device) {
                dbg("look for devices attribute '%s/%s'", sysfs_device->path, name);
@@ -1091,10 +1043,21 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct s
        return 0;
 }
 
-int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev, struct sysfs_device *sysfs_device)
+int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev,
+                      struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_dev)
 {
        struct udev_rule *rule;
 
+       if (class_dev && !sysfs_dev)
+               sysfs_dev = sysfs_get_classdev_device(class_dev);
+       if (sysfs_dev) {
+               dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
+                   sysfs_dev->path, sysfs_dev->bus_id, sysfs_dev->bus);
+               strlcpy(udev->bus_id, sysfs_dev->bus_id, sizeof(udev->bus_id));
+       }
+
+       dbg("udev->kernel_name='%s'", udev->kernel_name);
+
        /* look for a matching rule to apply */
        udev_rules_iter_init(rules);
        while (1) {
@@ -1109,7 +1072,7 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev, struct sy
                        continue;
                }
 
-               if (match_rule(udev, rule, NULL, sysfs_device) == 0) {
+               if (match_rule(udev, rule, class_dev, sysfs_dev) == 0) {
                        if (rule->ignore_device) {
                                info("rule applied, '%s' is ignored", udev->kernel_name);
                                udev->ignore_device = 1;
@@ -1130,7 +1093,7 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev, struct sy
                                        }
                                }
                                strlcpy(program, key_val(rule, &rule->run), sizeof(program));
-                               apply_format(udev, program, sizeof(program), NULL, sysfs_device);
+                               apply_format(udev, program, sizeof(program), class_dev, sysfs_dev);
                                dbg("add run '%s'", program);
                                name_list_add(&udev->run_list, program, 0);
                                if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
@@ -1141,6 +1104,11 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev, struct sy
                                dbg("last rule to be applied");
                                break;
                        }
+
+                       if (rule->goto_label.operation != KEY_OP_UNSET) {
+                               dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
+                               udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
+                       }
                }
        }