chiark / gitweb /
add flag for reading of precompiled rules
[elogind.git] / udev_rules.c
index 48397ac1c7bf3b9a6a74069b46ebff8683629f35..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.
@@ -31,6 +28,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <sys/wait.h>
+#include <sys/stat.h>
 
 #include "libsysfs/sysfs/libsysfs.h"
 #include "list.h"
 #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)
 {
@@ -242,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) {
@@ -280,6 +226,46 @@ static int import_program_into_env(struct udevice *udev, const char *program)
        return import_keys_into_env(udev, result, reslen);
 }
 
+static int import_parent_into_env(struct udevice *udev, struct sysfs_class_device *class_dev, const char *filter)
+{
+       struct sysfs_class_device *parent = sysfs_get_classdev_parent(class_dev);
+       int rc = -1;
+
+       if (parent != NULL) {
+               struct udevice udev_parent;
+               struct name_entry *name_loop;
+
+               dbg("found parent '%s', get the node name", parent->path);
+               udev_init_device(&udev_parent, NULL, NULL, NULL);
+               /* import the udev_db of the parent */
+               if (udev_db_get_device(&udev_parent, &parent->path[strlen(sysfs_path)]) == 0) {
+                       dbg("import stored parent env '%s'", udev_parent.name);
+                       list_for_each_entry(name_loop, &udev_parent.env_list, node) {
+                               char name[NAME_SIZE];
+                               char *pos;
+
+                               strlcpy(name, name_loop->name, sizeof(name));
+                               pos = strchr(name, '=');
+                               if (pos) {
+                                       pos[0] = '\0';
+                                       pos++;
+                                       if (strcmp_pattern(filter, name) == 0) {
+                                               dbg("import key '%s'", name_loop->name);
+                                               name_list_add(&udev->env_list, name_loop->name, 0);
+                                               setenv(name, pos, 1);
+                                       } else
+                                               dbg("skip key '%s'", name_loop->name);
+                               }
+                       }
+                       rc = 0;
+               } else
+                       dbg("parent not found in database");
+               udev_cleanup_device(&udev_parent);
+       }
+
+       return rc;
+}
+
 /* finds the lowest positive N such that <name>N isn't present in the udevdb
  * if <name> doesn't exist, 0 is returned, N otherwise
  */
@@ -310,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);
@@ -318,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);
@@ -335,6 +328,28 @@ attr_found:
        return 0;
 }
 
+#define WAIT_LOOP_PER_SECOND                   20
+static int wait_for_sysfs(struct udevice *udev, const char *file, int timeout)
+{
+       char filename[PATH_SIZE];
+       struct stat stats;
+       int loop = timeout * WAIT_LOOP_PER_SECOND;
+
+       snprintf(filename, sizeof(filename), "%s%s/%s", sysfs_path, udev->devpath, file);
+       filename[sizeof(filename)-1] = '\0';
+       dbg("wait %i sec for '%s'", timeout, filename);
+
+       while (--loop) {
+               if (stat(filename, &stats) == 0) {
+                       dbg("file appeared after %i loops", (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
+                       return 0;
+               }
+               usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
+       }
+       dbg("waiting for '%s' failed", filename);
+       return -1;
+}
+
 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
                         struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
 {
@@ -581,10 +596,12 @@ found:
                                break;
                        }
                        pos = getenv(attr);
-                       if (pos == NULL)
+                       if (pos == NULL) {
+                               dbg("env '%s' not avialable", attr);
                                break;
-                       strlcat(string, pos, maxsize);
+                       }
                        dbg("substitute env '%s=%s'", attr, pos);
+                       strlcat(string, pos, maxsize);
                        break;
                default:
                        err("unknown substitution type=%i", type);
@@ -612,25 +629,36 @@ static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
 static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
 {
        int match;
+       char value[PATH_SIZE];
        char *key_value;
+       char *pos;
 
        if (key->operation == KEY_OP_UNSET)
                return 0;
 
-       key_value = rule->buf + key->val_off;
+       strlcpy(value, rule->buf + key->val_off, sizeof(value));
+       key_value = value;
 
-       dbg("check for %s '%s' <-> '%s'", key_name, key_value, val);
-       match = (strcmp_pattern(key_value, val) == 0);
-       if (match && (key->operation != KEY_OP_NOMATCH)) {
-               dbg("%s key is matching (matching value)", key_name);
-               return 0;
-       }
-       if (!match && (key->operation == KEY_OP_NOMATCH)) {
-               dbg("%s key is matching, (non matching value)", key_name);
-               return 0;
+       dbg("key %s value='%s'", key_name, key_value);
+       while (key_value) {
+               pos = strchr(key_value, '|');
+               if (pos) {
+                       pos[0] = '\0';
+                       pos++;
+               }
+               dbg("match %s '%s' <-> '%s'", key_name, key_value, val);
+               match = (strcmp_pattern(key_value, val) == 0);
+               if (match && (key->operation != KEY_OP_NOMATCH)) {
+                       dbg("%s is true (matching value)", key_name);
+                       return 0;
+               }
+               if (!match && (key->operation == KEY_OP_NOMATCH)) {
+                       dbg("%s is true (non-matching value)", key_name);
+                       return 0;
+               }
+               key_value = pos;
        }
-
-       dbg("%s key is not matching", key_name);
+       dbg("%s is false", key_name);
        return -1;
 }
 
@@ -672,8 +700,8 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                        const char *value = getenv(key_name);
 
                        if (!value) {
-                               dbg("ENV{'%s'} is not found", key_name);
-                               goto exit;
+                               dbg("ENV{'%s'} is not set", key_name);
+                               value = "";
                        }
                        if (match_key("ENV", rule, &pair->key, value))
                                goto exit;
@@ -681,6 +709,22 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                dbg("all %i ENV keys matched", rule->env.count);
        }
 
+       if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
+               int match;
+
+               match = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
+               if (match && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH)) {
+                       dbg("WAIT_FOR_SYSFS is true (matching value)");
+                       return 0;
+               }
+               if (!match && (rule->wait_for_sysfs.operation == KEY_OP_NOMATCH)) {
+                       dbg("WAIT_FOR_SYSFS is true, (non matching value)");
+                       return 0;
+               }
+               dbg("WAIT_FOR_SYSFS is false");
+               return -1;
+       }
+
        /* walk up the chain of physical devices and find a match */
        while (1) {
                /* check for matching driver */
@@ -762,12 +806,15 @@ try_parent:
                strlcpy(import, key_val(rule, &rule->import), sizeof(import));
                apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
                dbg("check for IMPORT import='%s'", import);
-               if (rule->import_exec) {
+               if (rule->import_type == IMPORT_PROGRAM) {
                        dbg("run executable file import='%s'", import);
                        rc = import_program_into_env(udev, import);
-               } else {
+               } else if (rule->import_type == IMPORT_FILE) {
                        dbg("import file import='%s'", import);
                        rc = import_file_into_env(udev, import);
+               } else if (rule->import_type == IMPORT_PARENT && class_dev) {
+                       dbg("import parent import='%s'", import);
+                       rc = import_parent_into_env(udev, class_dev, import);
                }
                if (rc) {
                        dbg("IMPORT failed");
@@ -787,7 +834,7 @@ try_parent:
                apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
                dbg("check for PROGRAM program='%s", program);
                if (execute_program(program, udev->subsystem, result, sizeof(result), NULL) != 0) {
-                       dbg("PROGRAM is not matching");
+                       dbg("PROGRAM is false");
                        if (rule->program.operation != KEY_OP_NOMATCH)
                                goto exit;
                } else {
@@ -819,6 +866,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct s
        struct sysfs_class_device *class_dev_parent;
        struct sysfs_device *sysfs_device = NULL;
        struct udev_rule *rule;
+       int name_set = 0;
 
        dbg("class_dev->name='%s'", class_dev->name);
 
@@ -850,7 +898,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct s
                if (rule == NULL)
                        break;
 
-               if (udev->name_set && rule->name.operation != KEY_OP_UNSET) {
+               if (name_set && rule->name.operation != KEY_OP_UNSET) {
                        dbg("node name already set, rule ignored");
                        continue;
                }
@@ -891,7 +939,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct s
                                if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
                                        udev->group_final = 1;
                                strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
-                               apply_format(udev, key_val(rule, &rule->group), sizeof(udev->group), class_dev, sysfs_device);
+                               apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
                                dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
                        }
 
@@ -918,21 +966,27 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct s
 
                                /* add multiple symlinks separated by spaces */
                                pos = temp;
-                               next = strchr(temp, ' ');
+                               while (isspace(pos[0]))
+                                       pos++;
+                               next = strchr(pos, ' ');
                                while (next) {
                                        next[0] = '\0';
                                        info("add symlink '%s'", pos);
                                        name_list_add(&udev->symlink_list, pos, 0);
+                                       while (isspace(next[1]))
+                                               next++;
                                        pos = &next[1];
                                        next = strchr(pos, ' ');
                                }
-                               info("add symlink '%s'", pos);
-                               name_list_add(&udev->symlink_list, pos, 0);
+                               if (pos[0] != '\0') {
+                                       info("add symlink '%s'", pos);
+                                       name_list_add(&udev->symlink_list, pos, 0);
+                               }
                        }
 
                        /* set name, later rules with name set will be ignored */
                        if (rule->name.operation != KEY_OP_UNSET) {
-                               udev->name_set = 1;
+                               name_set = 1;
                                strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
                                apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
 
@@ -967,10 +1021,15 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct s
                                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));
+                       }
                }
        }
 
-       if (udev->name[0] == '\0') {
+       if (!name_set) {
                strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
                info("no rule found, will use kernel name '%s'", udev->name);
        }
@@ -984,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) {
@@ -1002,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;
@@ -1023,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)
@@ -1034,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));
+                       }
                }
        }