chiark / gitweb /
rules_generator: add KERNEL=="<netifname>*" to generated rules
[elogind.git] / udev_rules.c
index 35db958c8df4959fbb68664b1aed7afb0124a3aa..e464404b5947e14a9eb4a97a6edbb0266c0cc2ab 100644 (file)
@@ -26,6 +26,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <syslog.h>
+#include <dirent.h>
 #include <fnmatch.h>
 #include <sys/socket.h>
 #include <sys/un.h>
@@ -349,11 +350,6 @@ static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bu
                cur += count+1;
                lineno++;
 
-               if (count >= sizeof(line)) {
-                       err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
-                       continue;
-               }
-
                /* eat the whitespace */
                while ((count > 0) && isspace(bufline[0])) {
                        bufline++;
@@ -366,6 +362,11 @@ static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bu
                if (bufline[0] == COMMENT_CHARACTER)
                        continue;
 
+               if (count >= sizeof(line)) {
+                       err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
+                       continue;
+               }
+
                memcpy(line, bufline, count);
                line[count] = '\0';
 
@@ -606,6 +607,7 @@ void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
                SUBST_ATTR,
                SUBST_PARENT,
                SUBST_TEMP_NODE,
+               SUBST_NAME,
                SUBST_ROOT,
                SUBST_SYS,
                SUBST_ENV,
@@ -627,6 +629,7 @@ void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize)
                { .name = "sysfs",      .fmt = 's',     .type = SUBST_ATTR },
                { .name = "parent",     .fmt = 'P',     .type = SUBST_PARENT },
                { .name = "tempnode",   .fmt = 'N',     .type = SUBST_TEMP_NODE },
+               { .name = "name",       .fmt = 'D',     .type = SUBST_NAME },
                { .name = "root",       .fmt = 'r',     .type = SUBST_ROOT },
                { .name = "sys",        .fmt = 'S',     .type = SUBST_SYS },
                { .name = "env",        .fmt = 'E',     .type = SUBST_ENV },
@@ -845,6 +848,10 @@ found:
                        strlcat(string, udev->tmp_node, maxsize);
                        dbg("substitute temporary device node name '%s'", udev->tmp_node);
                        break;
+               case SUBST_NAME:
+                       strlcat(string, udev->name, maxsize);
+                       dbg("substitute udev->name '%s'", udev->name);
+                       break;
                case SUBST_ROOT:
                        strlcat(string, udev_root, maxsize);
                        dbg("substitute udev_root '%s'", udev_root);
@@ -988,7 +995,8 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule)
                }
        }
 
-       if (rule->test.operation != KEY_OP_UNSET) {
+       if (rule->test.operation == KEY_OP_MATCH ||
+           rule->test.operation == KEY_OP_NOMATCH) {
                char filename[PATH_SIZE];
                char devpath[PATH_SIZE];
                char *attr;
@@ -1033,7 +1041,7 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule)
        if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
                int found;
 
-               found = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
+               found = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 10) == 0);
                if (!found && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH))
                        goto nomatch;
        }
@@ -1236,6 +1244,7 @@ try_parent:
                if (pair->key.operation == KEY_OP_ASSIGN) {
                        const char *key_name = key_pair_name(rule, pair);
                        char devpath[PATH_SIZE];
+                       char *pos;
                        char *attrib;
                        char attr[PATH_SIZE] = "";
                        char value[NAME_SIZE];
@@ -1257,6 +1266,28 @@ try_parent:
                                strlcat(attr, key_name, sizeof(attr));
                        }
 
+                       pos = strstr(attr, "/*/");
+                       if (pos != NULL) {
+                               char str[PATH_SIZE];
+                               DIR *dir;
+
+                               pos[1] = '\0';
+                               strlcpy(str, &pos[2], sizeof(str));
+                               dir = opendir(attr);
+                               if (dir != NULL) {
+                                       struct dirent *dent;
+
+                                       for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
+                                               if (dent->d_name[0] == '.')
+                                                       continue;
+                                               strlcat(attr, dent->d_name, sizeof(attr));
+                                               break;
+                                       }
+                                       closedir(dir);
+                               }
+                               strlcat(attr, str, sizeof(attr));
+                       }
+
                        strlcpy(value, key_val(rule, &pair->key), sizeof(value));
                        udev_rules_apply_format(udev, value, sizeof(value));
                        info("writing '%s' to sysfs file '%s'", value, attr);
@@ -1439,7 +1470,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev)
        }
 
        if (!name_set) {
-               info("no node name set, will use kernel name '%s'", udev->name);
+               info("no node name set, will use kernel name '%s'", udev->dev->kernel);
                strlcpy(udev->name, udev->dev->kernel, sizeof(udev->name));
        }
 
@@ -1466,9 +1497,14 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev)
                        break;
 
                dbg("process rule");
-               if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET ||
-                   rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET ||
-                   rule->group.operation != KEY_OP_UNSET) {
+               if (rule->name.operation == KEY_OP_ASSIGN ||
+                   rule->name.operation == KEY_OP_ASSIGN_FINAL ||
+                   rule->name.operation == KEY_OP_ADD ||
+                   rule->symlink.operation == KEY_OP_ASSIGN ||
+                   rule->symlink.operation == KEY_OP_ASSIGN_FINAL ||
+                   rule->symlink.operation == KEY_OP_ADD ||
+                   rule->mode_operation != KEY_OP_UNSET ||
+                   rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
                        dbg("skip rule that names a device");
                        continue;
                }
@@ -1479,6 +1515,10 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev)
                                udev->ignore_device = 1;
                                return 0;
                        }
+                       if (rule->ignore_remove) {
+                               udev->ignore_remove = 1;
+                               dbg("remove event should be ignored");
+                       }
 
                        if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
                                struct name_entry *entry;