chiark / gitweb /
fix size_t compiler warning on 32 bit platforms
[elogind.git] / udev / udev-rules.c
index b5fe2f468badcbe7c54c7c5d0e506b2217cbcea8..4713352826d524daaff101ee72dcd31d85af19fa 100644 (file)
 #define PREALLOC_TOKEN                 2048
 #define PREALLOC_STRBUF                        32 * 1024
 
+struct uid_gid {
+       unsigned int name_off;
+       union {
+               uid_t   uid;
+               gid_t   gid;
+       };
+};
+
 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
+struct udev_rules {
+       struct udev *udev;
+       int resolve_names;
+
+       /* every key in the rules file becomes a token */
+       struct token *tokens;
+       unsigned int token_cur;
+       unsigned int token_max;
+
+       /* all key strings are copied to a single string buffer */
+       char *buf;
+       size_t buf_cur;
+       size_t buf_max;
+       unsigned int buf_count;
+
+       /* during rule parsing, we cache uid/gid lookup results */
+       struct uid_gid *uids;
+       unsigned int uids_cur;
+       unsigned int uids_max;
+       struct uid_gid *gids;
+       unsigned int gids_cur;
+       unsigned int gids_max;
+};
+
 enum operation_type {
        OP_UNSET,
 
@@ -44,17 +76,6 @@ enum operation_type {
        OP_ASSIGN_FINAL,
 };
 
-static const char *operation_str[] = {
-       [OP_UNSET] =            "UNSET",
-       [OP_MATCH] =            "match",
-       [OP_NOMATCH] =          "nomatch",
-       [OP_MATCH_MAX] =        "MATCH_MAX",
-
-       [OP_ADD] =              "add",
-       [OP_ASSIGN] =           "assign",
-       [OP_ASSIGN_FINAL] =     "assign-final",
-};
-
 enum string_glob_type {
        GL_UNSET,
        GL_PLAIN,                       /* no special chars */
@@ -65,18 +86,6 @@ enum string_glob_type {
        GL_FORMAT,
 };
 
-#ifdef DEBUG
-static const char *string_glob_str[] = {
-       [GL_UNSET] =            "UNSET",
-       [GL_PLAIN] =            "plain",
-       [GL_GLOB] =             "glob",
-       [GL_SPLIT] =            "split",
-       [GL_SPLIT_GLOB] =       "split-glob",
-       [GL_SOMETHING] =        "split-glob",
-       [GL_FORMAT] =           "format",
-};
-#endif
-
 /* tokens of a rule are sorted/handled in this order */
 enum token_type {
        TK_UNSET,
@@ -132,6 +141,70 @@ enum token_type {
        TK_END,
 };
 
+/* we try to pack stuff in a way that we take only 12 bytes per token */
+struct token {
+       union {
+               unsigned char type;             /* same as in rule and key */
+               struct {
+                       unsigned char type;
+                       unsigned char flags;
+                       unsigned short token_count;
+                       unsigned int label_off;
+                       unsigned short filename_off;
+                       unsigned short filename_line;
+               } rule;
+               struct {
+                       unsigned char type;
+                       unsigned char flags;
+                       unsigned char op;
+                       unsigned char glob;
+                       unsigned int value_off;
+                       union {
+                               unsigned int attr_off;
+                               int ignore_error;
+                               int i;
+                               unsigned int rule_goto;
+                               mode_t  mode;
+                               uid_t uid;
+                               gid_t gid;
+                               int num_fake_part;
+                               int devlink_prio;
+                               int event_timeout;
+                       };
+               } key;
+       };
+};
+
+#define MAX_TK         64
+struct rule_tmp {
+       struct udev_rules *rules;
+       struct token rule;
+       struct token token[MAX_TK];
+       unsigned int token_cur;
+};
+
+#ifdef DEBUG
+static const char *operation_str[] = {
+       [OP_UNSET] =            "UNSET",
+       [OP_MATCH] =            "match",
+       [OP_NOMATCH] =          "nomatch",
+       [OP_MATCH_MAX] =        "MATCH_MAX",
+
+       [OP_ADD] =              "add",
+       [OP_ASSIGN] =           "assign",
+       [OP_ASSIGN_FINAL] =     "assign-final",
+};
+
+static const char *string_glob_str[] = {
+       [GL_UNSET] =            "UNSET",
+       [GL_PLAIN] =            "plain",
+       [GL_GLOB] =             "glob",
+       [GL_SPLIT] =            "split",
+       [GL_SPLIT_GLOB] =       "split-glob",
+       [GL_SOMETHING] =        "split-glob",
+       [GL_FORMAT] =           "format",
+};
+
 static const char *token_str[] = {
        [TK_UNSET] =                    "UNSET",
        [TK_RULE] =                     "RULE",
@@ -186,79 +259,125 @@ static const char *token_str[] = {
        [TK_END] =                      "END",
 };
 
-/* we try to pack stuff in a way that we take only 16 bytes per token */
-struct token {
-       union {
-               unsigned short type;            /* same as in rule and key */
-               struct {
-                       unsigned short type;
-                       unsigned short flags;
-                       unsigned int next_rule;
-                       unsigned int label_off;
-                       unsigned short filename_off;
-                       unsigned short filename_line;
-               } rule;
-               struct {
-                       unsigned short type;
-                       unsigned short flags;
-                       unsigned short op;
-                       unsigned short glob;
-                       unsigned int value_off;
-                       union {
-                               unsigned int attr_off;
-                               int ignore_error;
-                               int i;
-                               unsigned int rule_goto;
-                               mode_t  mode;
-                               uid_t uid;
-                               gid_t gid;
-                               int num_fake_part;
-                               int devlink_prio;
-                               int event_timeout;
-                       };
-               } key;
-       };
-};
-
-#define MAX_TK         64
-struct rule_tmp {
-       struct udev_rules *rules;
-       struct token rule;
-       struct token token[MAX_TK];
-       unsigned int token_cur;
-};
-
-struct uid_gid {
-       unsigned int name_off;
-       union {
-               uid_t   uid;
-               gid_t   gid;
-       };
-};
+static void dump_token(struct udev_rules *rules, struct token *token)
+{
+       enum token_type type = token->type;
+       enum operation_type op = token->key.op;
+       enum string_glob_type glob = token->key.glob;
+       const char *value = &rules->buf[token->key.value_off];
+       const char *attr = &rules->buf[token->key.attr_off];
 
-struct udev_rules {
-       struct udev *udev;
-       int resolve_names;
+       switch (type) {
+       case TK_RULE:
+               {
+                       const char *tks_ptr = (char *)rules->tokens;
+                       const char *tk_ptr = (char *)token;
+                       unsigned int off = tk_ptr - tks_ptr;
 
-       /* every key in the rules file becomes a token */
-       struct token *tokens;
-       unsigned int token_cur;
-       unsigned int token_max;
+                       dbg(rules->udev, "* RULE %s:%u, off: %u(%u), token_count: %u(%u), label: '%s', flags: 0x%02x\n",
+                           &rules->buf[token->rule.filename_off], token->rule.filename_line,
+                           off / (unsigned int) sizeof(struct token), off,
+                           token->rule.token_count,
+                           token->rule.token_count * (unsigned int) sizeof(struct token),
+                           &rules->buf[token->rule.label_off],
+                           token->rule.flags);
+                       break;
+               }
+       case TK_M_ACTION:
+       case TK_M_DEVPATH:
+       case TK_M_KERNEL:
+       case TK_M_SUBSYSTEM:
+       case TK_M_DRIVER:
+       case TK_M_WAITFOR:
+       case TK_M_DEVLINK:
+       case TK_M_NAME:
+       case TK_M_KERNELS:
+       case TK_M_SUBSYSTEMS:
+       case TK_M_DRIVERS:
+       case TK_M_PROGRAM:
+       case TK_M_IMPORT_FILE:
+       case TK_M_IMPORT_PROG:
+       case TK_M_IMPORT_PARENT:
+       case TK_M_RESULT:
+       case TK_A_NAME:
+       case TK_A_DEVLINK:
+       case TK_A_OWNER:
+       case TK_A_GROUP:
+       case TK_A_MODE:
+       case TK_A_RUN:
+               dbg(rules->udev, "%s %s '%s'(%s)\n",
+                   token_str[type], operation_str[op], value, string_glob_str[glob]);
+               break;
+       case TK_M_ATTR:
+       case TK_M_ATTRS:
+       case TK_M_ENV:
+       case TK_A_ATTR:
+       case TK_A_ENV:
+               dbg(rules->udev, "%s %s '%s' '%s'(%s)\n",
+                   token_str[type], operation_str[op], attr, value, string_glob_str[glob]);
+               break;
+       case TK_A_IGNORE_DEVICE:
+       case TK_A_STRING_ESCAPE_NONE:
+       case TK_A_STRING_ESCAPE_REPLACE:
+       case TK_A_LAST_RULE:
+       case TK_A_IGNORE_REMOVE:
+               dbg(rules->udev, "%s\n", token_str[type]);
+               break;
+       case TK_M_TEST:
+               dbg(rules->udev, "%s %s '%s'(%s) %#o\n",
+                   token_str[type], operation_str[op], value, string_glob_str[glob], token->key.mode);
+               break;
+       case TK_A_NUM_FAKE_PART:
+               dbg(rules->udev, "%s %u\n", token_str[type], token->key.num_fake_part);
+               break;
+       case TK_A_DEVLINK_PRIO:
+               dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.devlink_prio);
+               break;
+       case TK_A_OWNER_ID:
+               dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.uid);
+               break;
+       case TK_A_GROUP_ID:
+               dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.gid);
+               break;
+       case TK_A_MODE_ID:
+               dbg(rules->udev, "%s %s %#o\n", token_str[type], operation_str[op], token->key.mode);
+               break;
+       case TK_A_EVENT_TIMEOUT:
+               dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.event_timeout);
+               break;
+       case TK_A_GOTO:
+               dbg(rules->udev, "%s '%s' %u\n", token_str[type], value, token->key.rule_goto);
+               break;
+       case TK_END:
+               dbg(rules->udev, "* %s\n", token_str[type]);
+               break;
+       case TK_M_PARENTS_MIN:
+       case TK_M_PARENTS_MAX:
+       case TK_M_MAX:
+       case TK_UNSET:
+               dbg(rules->udev, "unknown type %u\n", type);
+               break;
+       }
+}
 
-       /* all key strings are copied to a single string buffer */
-       char *buf;
-       size_t buf_cur;
-       size_t buf_max;
-       unsigned int buf_count;
+static void dump_rules(struct udev_rules *rules)
+{
+       unsigned int i;
 
-       /* during rule parsing, we cache uid/gid lookup results */
-       struct uid_gid *uids;
-       unsigned int uids_cur;
-       unsigned int uids_max;
-       struct uid_gid *gids;
-       unsigned int gids_cur;
-       unsigned int gids_max;
-};
+       dbg(rules->udev, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
+           rules->token_cur,
+           rules->token_cur * sizeof(struct token),
+           rules->buf_count,
+           rules->buf_cur);
+       for(i = 0; i < rules->token_cur; i++)
+               dump_token(rules, &rules->tokens[i]);
+}
+#else
+static const char **operation_str;
+static const char **token_str;
+static inline void dump_token(struct udev_rules *rules, struct token *token) {}
+static inline void dump_rules(struct udev_rules *rules) {}
+#endif /* DEBUG */
 
 /* we could lookup and return existing strings, or tails of strings */
 static int add_string(struct udev_rules *rules, const char *str)
@@ -283,7 +402,7 @@ static int add_string(struct udev_rules *rules, const char *str)
                buf = realloc(rules->buf, rules->buf_max + add);
                if (buf == NULL)
                        return -1;
-               info(rules->udev, "extend buffer from %zu to %zu\n", rules->buf_max, rules->buf_max + add);
+               dbg(rules->udev, "extend buffer from %zu to %zu\n", rules->buf_max, rules->buf_max + add);
                rules->buf = buf;
                rules->buf_max += add;
        }
@@ -310,7 +429,7 @@ static int add_token(struct udev_rules *rules, struct token *token)
                tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
                if (tokens == NULL)
                        return -1;
-               info(rules->udev, "extend tokens from %u to %u\n", rules->token_max, rules->token_max + add);
+               dbg(rules->udev, "extend tokens from %u to %u\n", rules->token_max, rules->token_max + add);
                rules->tokens = tokens;
                rules->token_max += add;
        }
@@ -349,7 +468,7 @@ static uid_t add_uid(struct udev_rules *rules, const char *owner)
                uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
                if (uids == NULL)
                        return uid;
-               info(rules->udev, "extend uids from %u to %u\n", rules->uids_max, rules->uids_max + add);
+               dbg(rules->udev, "extend uids from %u to %u\n", rules->uids_max, rules->uids_max + add);
                rules->uids = uids;
                rules->uids_max += add;
        }
@@ -392,7 +511,7 @@ static gid_t add_gid(struct udev_rules *rules, const char *group)
                gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
                if (gids == NULL)
                        return gid;
-               info(rules->udev, "extend gids from %u to %u\n", rules->gids_max, rules->gids_max + add);
+               dbg(rules->udev, "extend gids from %u to %u\n", rules->gids_max, rules->gids_max + add);
                rules->gids = gids;
                rules->gids_max += add;
        }
@@ -461,7 +580,7 @@ static int import_property_from_string(struct udev_device *dev, char *line)
                val++;
        }
 
-       info(udev, "adding '%s'='%s'\n", key, val);
+       dbg(udev, "adding '%s'='%s'\n", key, val);
 
        /* handle device, renamed by external tool, returning new path */
        if (strcmp(key, "DEVPATH") == 0) {
@@ -861,123 +980,6 @@ static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
        return 0;
 }
 
-#ifdef DEBUG
-static void dump_token(struct udev_rules *rules, struct token *token)
-{
-       enum token_type type = token->type;
-       enum operation_type op = token->key.op;
-       enum string_glob_type glob = token->key.glob;
-       const char *value = &rules->buf[token->key.value_off];
-       const char *attr = &rules->buf[token->key.attr_off];
-
-       switch (type) {
-       case TK_RULE:
-               {
-                       const char *tks_ptr = (char *)rules->tokens;
-                       const char *tk_ptr = (char *)token;
-                       unsigned int off = tk_ptr - tks_ptr;
-
-                       dbg(rules->udev, "* RULE %s:%u, off: %u(%u), next: %u, label: '%s'\n",
-                           &rules->buf[token->rule.filename_off], token->rule.filename_line,
-                           off / (unsigned int) sizeof(struct token), off,
-                           token->rule.next_rule,
-                           &rules->buf[token->rule.label_off]);
-                       break;
-               }
-       case TK_M_ACTION:
-       case TK_M_DEVPATH:
-       case TK_M_KERNEL:
-       case TK_M_SUBSYSTEM:
-       case TK_M_DRIVER:
-       case TK_M_WAITFOR:
-       case TK_M_DEVLINK:
-       case TK_M_NAME:
-       case TK_M_KERNELS:
-       case TK_M_SUBSYSTEMS:
-       case TK_M_DRIVERS:
-       case TK_M_PROGRAM:
-       case TK_M_IMPORT_FILE:
-       case TK_M_IMPORT_PROG:
-       case TK_M_IMPORT_PARENT:
-       case TK_M_RESULT:
-       case TK_A_NAME:
-       case TK_A_DEVLINK:
-       case TK_A_OWNER:
-       case TK_A_GROUP:
-       case TK_A_MODE:
-       case TK_A_RUN:
-               dbg(rules->udev, "%s %s '%s'(%s)\n",
-                   token_str[type], operation_str[op], value, string_glob_str[glob]);
-               break;
-       case TK_M_ATTR:
-       case TK_M_ATTRS:
-       case TK_M_ENV:
-       case TK_A_ATTR:
-       case TK_A_ENV:
-               dbg(rules->udev, "%s %s '%s' '%s'(%s)\n",
-                   token_str[type], operation_str[op], attr, value, string_glob_str[glob]);
-               break;
-       case TK_A_IGNORE_DEVICE:
-       case TK_A_STRING_ESCAPE_NONE:
-       case TK_A_STRING_ESCAPE_REPLACE:
-       case TK_A_LAST_RULE:
-       case TK_A_IGNORE_REMOVE:
-               dbg(rules->udev, "%s\n", token_str[type]);
-               break;
-       case TK_M_TEST:
-               dbg(rules->udev, "%s %s '%s'(%s) %#o\n",
-                   token_str[type], operation_str[op], value, string_glob_str[glob], token->key.mode);
-               break;
-       case TK_A_NUM_FAKE_PART:
-               dbg(rules->udev, "%s %u\n", token_str[type], token->key.num_fake_part);
-               break;
-       case TK_A_DEVLINK_PRIO:
-               dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.devlink_prio);
-               break;
-       case TK_A_OWNER_ID:
-               dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.uid);
-               break;
-       case TK_A_GROUP_ID:
-               dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.gid);
-               break;
-       case TK_A_MODE_ID:
-               dbg(rules->udev, "%s %s %#o\n", token_str[type], operation_str[op], token->key.mode);
-               break;
-       case TK_A_EVENT_TIMEOUT:
-               dbg(rules->udev, "%s %s %u\n", token_str[type], operation_str[op], token->key.event_timeout);
-               break;
-       case TK_A_GOTO:
-               dbg(rules->udev, "%s '%s' %u\n", token_str[type], value, token->key.rule_goto);
-               break;
-       case TK_END:
-               dbg(rules->udev, "* %s\n", token_str[type]);
-               break;
-       case TK_M_PARENTS_MIN:
-       case TK_M_PARENTS_MAX:
-       case TK_M_MAX:
-       case TK_UNSET:
-               dbg(rules->udev, "unknown type %u\n", type);
-               break;
-       }
-}
-
-static void dump_rules(struct udev_rules *rules)
-{
-       unsigned int i;
-
-       dbg(rules->udev, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
-           rules->token_cur,
-           rules->token_cur * sizeof(struct token),
-           rules->buf_count,
-           rules->buf_cur);
-       for(i = 0; i < rules->token_cur; i++)
-               dump_token(rules, &rules->tokens[i]);
-}
-#else
-static inline void dump_token(struct udev_rules *rules, struct token *token) {}
-static inline void dump_rules(struct udev_rules *rules) {}
-#endif /* DEBUG */
-
 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
 {
        unsigned int i;
@@ -1316,6 +1318,7 @@ static int add_rule(struct udev_rules *rules, char *line,
                                        }
                                }
                        }
+                       rule_tmp.rule.rule.flags = 1;
                        continue;
                }
 
@@ -1324,6 +1327,7 @@ static int add_rule(struct udev_rules *rules, char *line,
                                rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
                        else
                                rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
+                       rule_tmp.rule.rule.flags = 1;
                        valid = 1;
                        continue;
                }
@@ -1341,6 +1345,7 @@ static int add_rule(struct udev_rules *rules, char *line,
                        } else {
                                rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
                        }
+                       rule_tmp.rule.rule.flags = 1;
                        valid = 1;
                        continue;
                }
@@ -1358,6 +1363,7 @@ static int add_rule(struct udev_rules *rules, char *line,
                        } else {
                                rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
                        }
+                       rule_tmp.rule.rule.flags = 1;
                        valid = 1;
                        continue;
                }
@@ -1371,6 +1377,7 @@ static int add_rule(struct udev_rules *rules, char *line,
                                rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
                        else
                                rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
+                       rule_tmp.rule.rule.flags = 1;
                        valid = 1;
                        continue;
                }
@@ -1433,12 +1440,14 @@ static int add_rule(struct udev_rules *rules, char *line,
                goto invalid;
 
        /* add rule token */
+       rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
        if (add_token(rules, &rule_tmp.rule) != 0)
                goto invalid;
 
        /* add tokens to list, sorted by type */
        if (sort_token(rules, &rule_tmp) != 0)
                goto invalid;
+
        return 0;
 invalid:
        err(rules->udev, "invalid rule '%s:%u'\n", filename, lineno);
@@ -1519,7 +1528,6 @@ static int parse_file(struct udev_rules *rules, const char *filename, unsigned s
 
 static int add_matching_files(struct udev *udev, struct udev_list_node *file_list, const char *dirname, const char *suffix)
 {
-       struct dirent *ent;
        DIR *dir;
        char filename[UTIL_PATH_SIZE];
 
@@ -1531,26 +1539,28 @@ static int add_matching_files(struct udev *udev, struct udev_list_node *file_lis
        }
 
        while (1) {
-               ent = readdir(dir);
-               if (ent == NULL || ent->d_name[0] == '\0')
+               struct dirent *dent;
+
+               dent = readdir(dir);
+               if (dent == NULL || dent->d_name[0] == '\0')
                        break;
 
-               if ((ent->d_name[0] == '.') || (ent->d_name[0] == '#'))
+               if (dent->d_name[0] == '.')
                        continue;
 
                /* look for file matching with specified suffix */
                if (suffix != NULL) {
                        const char *ext;
 
-                       ext = strrchr(ent->d_name, '.');
+                       ext = strrchr(dent->d_name, '.');
                        if (ext == NULL)
                                continue;
                        if (strcmp(ext, suffix) != 0)
                                continue;
                }
-               dbg(udev, "put file '%s/%s' into list\n", dirname, ent->d_name);
+               dbg(udev, "put file '%s/%s' into list\n", dirname, dent->d_name);
 
-               snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
+               snprintf(filename, sizeof(filename), "%s/%s", dirname, dent->d_name);
                filename[sizeof(filename)-1] = '\0';
                udev_list_entry_add(udev, file_list, filename, NULL, 1, 1);
        }
@@ -1565,9 +1575,7 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
        struct stat statbuf;
        struct udev_list_node file_list;
        struct udev_list_entry *file_loop, *file_tmp;
-       unsigned int prev_rule;
        struct token end_token;
-       unsigned int i;
 
        rules = malloc(sizeof(struct udev_rules));
        if (rules == NULL)
@@ -1589,8 +1597,8 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
        /* offset 0 is always '\0' */
        rules->buf[0] = '\0';
        rules->buf_cur = 1;
-       info(udev, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
-            rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
+       dbg(udev, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
+           rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
 
        if (udev_get_rules_path(udev) != NULL) {
                /* custom rules location for testing */
@@ -1625,7 +1633,7 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
 
                        if (sort_base == NULL)
                                continue;
-
+                       /* sort entry into existing list */
                        udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) {
                                const char *file_name = udev_list_entry_get_name(file_loop);
                                const char *file_base = strrchr(file_name, '/');
@@ -1635,15 +1643,24 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
                                if (strcmp(file_base, sort_base) == 0) {
                                        info(udev, "rule file basename '%s' already added, ignoring '%s'\n",
                                             file_name, sort_name);
-                                       udev_list_entry_remove(sort_loop);
+                                       udev_list_entry_delete(sort_loop);
                                        sort_loop = NULL;
                                        break;
                                }
-                               if (strcmp(file_base, sort_base) > 0)
+                               if (strcmp(file_base, sort_base) > 0) {
+                                       /* found later file, insert before */
+                                       udev_list_entry_remove(sort_loop);
+                                       udev_list_entry_insert_before(sort_loop, file_loop);
+                                       sort_loop = NULL;
                                        break;
+                               }
                        }
-                       if (sort_loop != NULL)
-                               udev_list_entry_move_before(sort_loop, file_loop);
+                       /* current file already handled */
+                       if (sort_loop == NULL)
+                               continue;
+                       /* no later file, append to end of list */
+                       udev_list_entry_remove(sort_loop);
+                       udev_list_entry_append(sort_loop, &file_list);
                }
        }
 
@@ -1667,22 +1684,13 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
                        parse_file(rules, filename, filename_off);
                else
                        info(udev, "can not read '%s'\n", filename);
-               udev_list_entry_remove(file_loop);
+               udev_list_entry_delete(file_loop);
        }
 
        memset(&end_token, 0x00, sizeof(struct token));
        end_token.type = TK_END;
        add_token(rules, &end_token);
 
-       /* link all TK_RULE tokens to be able to fast-forward to next TK_RULE */
-       prev_rule = 0;
-       for (i = 1; i < rules->token_cur; i++) {
-               if (rules->tokens[i].type == TK_RULE) {
-                       rules->tokens[prev_rule].rule.next_rule = i;
-                       prev_rule = i;
-               }
-       }
-
        /* shrink allocated token and string buffer */
        if (rules->token_cur < rules->token_max) {
                struct token *tokens;
@@ -1702,7 +1710,7 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
                        rules->buf_max = rules->buf_cur;
                }
        }
-       info(udev, "shrunk to %lu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
+       info(udev, "shrunk to %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
             rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
 
        /* cleanup uid/gid cache */
@@ -1856,21 +1864,28 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
        struct token *cur;
        struct token *rule;
        enum escape_type esc = ESCAPE_UNSET;
+       int can_set_name;
 
        if (rules->tokens == NULL)
                return -1;
 
+       can_set_name = ((strcmp(udev_device_get_action(event->dev), "add") == 0 ||
+                        strcmp(udev_device_get_action(event->dev), "change") == 0) &&
+                       (major(udev_device_get_devnum(event->dev)) > 0 ||
+                        strcmp(udev_device_get_subsystem(event->dev), "net") == 0));
+
        /* loop through token list, match, run actions or forward to next rule */
        cur = &rules->tokens[0];
        rule = cur;
        while (1) {
-               unsigned int idx;
-
                dump_token(rules, cur);
                switch (cur->type) {
                case TK_RULE:
                        /* current rule */
                        rule = cur;
+                       /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
+                       if (!can_set_name && rule->rule.flags)
+                               ;//goto nomatch;
                        esc = ESCAPE_UNSET;
                        break;
                case TK_M_ACTION:
@@ -2026,11 +2041,11 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
                                attr_subst_subdir(filename, sizeof(filename));
 
                                match = (stat(filename, &statbuf) == 0);
-                               info(event->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n");
+                               dbg(event->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n");
                                if (match && cur->key.mode > 0) {
                                        match = ((statbuf.st_mode & cur->key.mode) > 0);
-                                       info(event->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
-                                            match ? "matches" : "does not match", cur->key.mode);
+                                       dbg(event->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
+                                           match ? "matches" : "does not match", cur->key.mode);
                                }
                                if (match && cur->key.op == OP_NOMATCH)
                                        goto nomatch;
@@ -2049,6 +2064,10 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
                                util_strlcpy(program, &rules->buf[cur->key.value_off], sizeof(program));
                                udev_event_apply_format(event, program, sizeof(program));
                                envp = udev_device_get_properties_envp(event->dev);
+                               info(event->udev, "PROGRAM '%s' %s:%u\n",
+                                    program,
+                                    &rules->buf[rule->rule.filename_off],
+                                    rule->rule.filename_line);
                                if (util_run_program(event->udev, program, envp, result, sizeof(result), NULL) != 0) {
                                        if (cur->key.op != OP_NOMATCH)
                                                goto nomatch;
@@ -2057,7 +2076,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
 
                                        util_remove_trailing_chars(result, '\n');
                                        if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
-                                               count = util_replace_chars(result, ALLOWED_CHARS_INPUT);
+                                               count = udev_util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
                                                if (count > 0)
                                                        info(event->udev, "%i character(s) replaced\n" , count);
                                        }
@@ -2085,6 +2104,10 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
 
                                util_strlcpy(import, &rules->buf[cur->key.value_off], sizeof(import));
                                udev_event_apply_format(event, import, sizeof(import));
+                               info(event->udev, "IMPORT '%s' %s:%u\n",
+                                    import,
+                                    &rules->buf[rule->rule.filename_off],
+                                    rule->rule.filename_line);
                                if (import_program_into_properties(event->dev, import) != 0)
                                        if (cur->key.op != OP_NOMATCH)
                                                goto nomatch;
@@ -2252,7 +2275,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
                                util_strlcpy(name_str, name, sizeof(name_str));
                                udev_event_apply_format(event, name_str, sizeof(name_str));
                                if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
-                                       count = util_replace_chars(name_str, ALLOWED_CHARS_FILE);
+                                       count = udev_util_replace_chars(name_str, "/");
                                        if (count > 0)
                                                info(event->udev, "%i character(s) replaced\n", count);
                                        free(event->name);
@@ -2284,9 +2307,9 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
                                util_strlcpy(temp, &rules->buf[cur->key.value_off], sizeof(temp));
                                udev_event_apply_format(event, temp, sizeof(temp));
                                if (esc == ESCAPE_UNSET)
-                                       count = util_replace_chars(temp, ALLOWED_CHARS_FILE " ");
+                                       count = udev_util_replace_chars(temp, "/ ");
                                else if (esc == ESCAPE_REPLACE)
-                                       count = util_replace_chars(temp, ALLOWED_CHARS_FILE);
+                                       count = udev_util_replace_chars(temp, "/");
                                if (count > 0)
                                        info(event->udev, "%i character(s) replaced\n" , count);
                                dbg(event->udev, "rule applied, added symlink(s) '%s'\n", temp);
@@ -2345,7 +2368,9 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
 
                                util_strlcpy(value, &rules->buf[cur->key.value_off], sizeof(value));
                                udev_event_apply_format(event, value, sizeof(value));
-                               info(event->udev, "writing '%s' to sysfs file '%s'\n", value, attr);
+                               info(event->udev, "ATTR '%s' writing '%s' %s:%u\n", attr, value,
+                                    &rules->buf[rule->rule.filename_off],
+                                    rule->rule.filename_line);
                                f = fopen(attr, "w");
                                if (f != NULL) {
                                        if (!event->test)
@@ -2392,10 +2417,8 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
                continue;
        nomatch:
                /* fast-forward to next rule */
-               idx = rule->rule.next_rule;
-               if (idx == 0)
-                       return 0;
-               dbg(rules->udev, "forward to rule: %u\n", idx);
-               cur = &rules->tokens[idx];
+               cur = rule + rule->rule.token_count;
+               dbg(rules->udev, "forward to rule: %u\n",
+                                (unsigned int) (cur - rules->tokens));
        }
 }