*/
#include <stddef.h>
+#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define PREALLOC_TOKEN 2048
#define PREALLOC_STRBUF 32 * 1024
+#define PREALLOC_TRIE 256
+
+struct uid_gid {
+ unsigned int name_off;
+ union {
+ uid_t uid;
+ gid_t gid;
+ };
+};
+
+#define TRIE_CHILD_MAX 10
+
+struct trie_node {
+ unsigned int value_off;
+ size_t value_len;
+ unsigned char child_cur;
+ unsigned char child_key[TRIE_CHILD_MAX];
+ unsigned short child[TRIE_CHILD_MAX];
+};
+
+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;
+
+ /* key strings are indexed to avoid wasting space with duplicates */
+ struct trie_node *trie;
+ unsigned short trie_cur;
+ unsigned short trie_max;
+ unsigned short *trie_root;
+
+ /* 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;
+};
/* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
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 */
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,
TK_END,
};
-static const char *token_str[] = {
- [TK_UNSET] = "UNSET",
- [TK_RULE] = "RULE",
-
- [TK_M_ACTION] = "M ACTION",
- [TK_M_DEVPATH] = "M DEVPATH",
- [TK_M_KERNEL] = "M KERNEL",
- [TK_M_DEVLINK] = "M DEVLINK",
- [TK_M_NAME] = "M NAME",
- [TK_M_ENV] = "M ENV",
- [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
- [TK_M_DRIVER] = "M DRIVER",
- [TK_M_WAITFOR] = "M WAITFOR",
- [TK_M_ATTR] = "M ATTR",
-
- [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
- [TK_M_KERNELS] = "M KERNELS",
- [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
- [TK_M_DRIVERS] = "M DRIVERS",
- [TK_M_ATTRS] = "M ATTRS",
- [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
-
- [TK_M_TEST] = "M TEST",
- [TK_M_PROGRAM] = "M PROGRAM",
- [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
- [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
- [TK_M_IMPORT_PARENT] = "M MPORT_PARENT",
- [TK_M_RESULT] = "M RESULT",
- [TK_M_MAX] = "M MAX",
-
- [TK_A_IGNORE_DEVICE] = "A IGNORE_DEVICE",
- [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
- [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
- [TK_A_NUM_FAKE_PART] = "A NUM_FAKE_PART",
- [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
- [TK_A_OWNER] = "A OWNER",
- [TK_A_GROUP] = "A GROUP",
- [TK_A_MODE] = "A MODE",
- [TK_A_OWNER_ID] = "A OWNER_ID",
- [TK_A_GROUP_ID] = "A GROUP_ID",
- [TK_A_MODE_ID] = "A MODE_ID",
- [TK_A_ENV] = "A ENV",
- [TK_A_NAME] = "A NAME",
- [TK_A_DEVLINK] = "A DEVLINK",
- [TK_A_EVENT_TIMEOUT] = "A EVENT_TIMEOUT",
- [TK_A_IGNORE_REMOVE] = "A IGNORE_REMOVE",
- [TK_A_ATTR] = "A ATTR",
- [TK_A_RUN] = "A RUN",
- [TK_A_GOTO] = "A GOTO",
- [TK_A_LAST_RULE] = "A LAST_RULE",
-
- [TK_END] = "END",
-};
-
-/* we try to pack stuff in a way that we take only 16 bytes per token */
+/* we try to pack stuff in a way that we take only 12 bytes per token */
struct token {
union {
- unsigned short type; /* same as in rule and key */
+ unsigned char type; /* same as in rule and key */
struct {
- unsigned short type;
- unsigned short flags;
- unsigned int next_rule;
+ 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 short type;
- unsigned short flags;
- unsigned short op;
- unsigned short glob;
+ unsigned char type;
+ unsigned char flags;
+ unsigned char op;
+ unsigned char glob;
unsigned int value_off;
union {
unsigned int attr_off;
unsigned int token_cur;
};
-struct uid_gid {
- unsigned int name_off;
- union {
- uid_t uid;
- gid_t gid;
+#ifdef DEBUG
+static const char *operation_str(enum operation_type type)
+{
+ static const char *operation_strs[] = {
+ [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",
+} ;
+
+ return operation_strs[type];
+}
+
+static const char *string_glob_str(enum string_glob_type type)
+{
+ static const char *string_glob_strs[] = {
+ [GL_UNSET] = "UNSET",
+ [GL_PLAIN] = "plain",
+ [GL_GLOB] = "glob",
+ [GL_SPLIT] = "split",
+ [GL_SPLIT_GLOB] = "split-glob",
+ [GL_SOMETHING] = "split-glob",
+ [GL_FORMAT] = "format",
};
-};
-struct udev_rules {
- struct udev *udev;
- int resolve_names;
+ return string_glob_strs[type];
+}
- /* every key in the rules file becomes a token */
- struct token *tokens;
- unsigned int token_cur;
- unsigned int token_max;
+static const char *token_str(enum token_type type)
+{
+ static const char *token_strs[] = {
+ [TK_UNSET] = "UNSET",
+ [TK_RULE] = "RULE",
+
+ [TK_M_ACTION] = "M ACTION",
+ [TK_M_DEVPATH] = "M DEVPATH",
+ [TK_M_KERNEL] = "M KERNEL",
+ [TK_M_DEVLINK] = "M DEVLINK",
+ [TK_M_NAME] = "M NAME",
+ [TK_M_ENV] = "M ENV",
+ [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
+ [TK_M_DRIVER] = "M DRIVER",
+ [TK_M_WAITFOR] = "M WAITFOR",
+ [TK_M_ATTR] = "M ATTR",
+
+ [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
+ [TK_M_KERNELS] = "M KERNELS",
+ [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
+ [TK_M_DRIVERS] = "M DRIVERS",
+ [TK_M_ATTRS] = "M ATTRS",
+ [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
+
+ [TK_M_TEST] = "M TEST",
+ [TK_M_PROGRAM] = "M PROGRAM",
+ [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
+ [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
+ [TK_M_IMPORT_PARENT] = "M MPORT_PARENT",
+ [TK_M_RESULT] = "M RESULT",
+ [TK_M_MAX] = "M MAX",
+
+ [TK_A_IGNORE_DEVICE] = "A IGNORE_DEVICE",
+ [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
+ [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
+ [TK_A_NUM_FAKE_PART] = "A NUM_FAKE_PART",
+ [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
+ [TK_A_OWNER] = "A OWNER",
+ [TK_A_GROUP] = "A GROUP",
+ [TK_A_MODE] = "A MODE",
+ [TK_A_OWNER_ID] = "A OWNER_ID",
+ [TK_A_GROUP_ID] = "A GROUP_ID",
+ [TK_A_MODE_ID] = "A MODE_ID",
+ [TK_A_ENV] = "A ENV",
+ [TK_A_NAME] = "A NAME",
+ [TK_A_DEVLINK] = "A DEVLINK",
+ [TK_A_EVENT_TIMEOUT] = "A EVENT_TIMEOUT",
+ [TK_A_IGNORE_REMOVE] = "A IGNORE_REMOVE",
+ [TK_A_ATTR] = "A ATTR",
+ [TK_A_RUN] = "A RUN",
+ [TK_A_GOTO] = "A GOTO",
+ [TK_A_LAST_RULE] = "A LAST_RULE",
+
+ [TK_END] = "END",
+ };
- /* all key strings are copied to a single string buffer */
- char *buf;
- size_t buf_cur;
- size_t buf_max;
- unsigned int buf_count;
+ return token_strs[type];
+}
- /* 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;
-};
+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];
-/* we could lookup and return existing strings, or tails of strings */
-static int add_string(struct udev_rules *rules, const char *str)
+ switch (type) {
+ case TK_RULE:
+ {
+ const char *tks_ptr = (char *)rules->tokens;
+ const char *tk_ptr = (char *)token;
+ unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
+
+ dbg(rules->udev, "* RULE %s:%u, token: %u, count: %u, label: '%s', flags: 0x%02x\n",
+ &rules->buf[token->rule.filename_off], token->rule.filename_line,
+ idx, token->rule.token_count,
+ &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;
+ }
+}
+
+static void dump_rules(struct udev_rules *rules)
{
- size_t len = strlen(str)+1;
- int off;
+ unsigned int i;
- /* offset 0 is always '\0' */
- if (str[0] == '\0')
- return 0;
+ 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 const char *operation_str(enum operation_type type) { return NULL; }
+static inline const char *token_str(enum token_type type) { return NULL; }
+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 add_new_string(struct udev_rules *rules, const char *str, size_t bytes)
+{
+ int off;
/* grow buffer if needed */
- if (rules->buf_cur + len+1 >= rules->buf_max) {
+ if (rules->buf_cur + bytes+1 >= rules->buf_max) {
char *buf;
unsigned int add;
/* double the buffer size */
add = rules->buf_max;
- if (add < len * 8)
- add = len * 8;
+ if (add < bytes * 8)
+ add = bytes * 8;
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;
}
off = rules->buf_cur;
- memcpy(&rules->buf[rules->buf_cur], str, len);
- rules->buf_cur += len;
+ memcpy(&rules->buf[rules->buf_cur], str, bytes);
+ rules->buf_cur += bytes;
rules->buf_count++;
return off;
}
-static int add_token(struct udev_rules *rules, struct token *token)
+static unsigned char trie_child_slot(struct trie_node *node, char key)
+{
+ unsigned char child_slot;
+
+ for (child_slot = 0; child_slot < node->child_cur; child_slot++) {
+ if (node->child_key[child_slot] == key)
+ break;
+ }
+
+ return child_slot;
+}
+
+static int add_string(struct udev_rules *rules, const char *str)
{
+ struct trie_node *child;
+ unsigned short child_off;
+ unsigned short node_off;
+ unsigned char key;
+ size_t len;
+ int depth;
+ unsigned int off;
+
+ len = strlen(str);
+
+ /* offset 0 is always '\0' */
+ if (len == 0)
+ return 0;
+
+ /* strings with spaces are probably commands e.g. modprobe,
+ with unique arguments. */
+ if (strchr(str, ' ') != NULL)
+ return add_new_string(rules, str, len + 1);
+
+ /* descend root - start from last character of str */
+ key = str[len - 1];
+ node_off = rules->trie_root[key];
+ depth = 0;
+
+ /* descend suffix trie */
+ if (node_off != 0) {
+ while (1) {
+ struct trie_node *node = &rules->trie[node_off];
+ unsigned char child_slot;
+
+ depth++;
+ off = node->value_off + node->value_len - len;
+
+ /* match against current node */
+ if (depth == len ||
+ (node->value_len >= len &&
+ memcmp(&rules->buf[off], str, len) == 0))
+ {
+ return off;
+ }
+
+ /* lookup child node */
+ key = str[len - 1 - depth];
+ child_slot = trie_child_slot(node, key);
+
+ if(child_slot == node->child_cur)
+ break;
+
+ node_off = node->child[child_slot];
+ }
+ }
+
+ /* string not found, add it */
+ off = add_new_string(rules, str, len + 1);
+
+ /* grow trie storage if needed */
+ if (rules->trie_cur >= rules->trie_max) {
+ struct trie_node *trie;
+ unsigned short add;
+
+ /* double the buffer size */
+ add = rules->trie_max;
+ if (add < 8)
+ add = 8;
+
+ trie = realloc(rules->trie, (rules->trie_max + add) * sizeof(struct trie_node));
+ if (trie == NULL)
+ return -1;
+ dbg(rules->udev, "extend string index nodes from %u to %u\n", rules->trie_max, rules->trie_max + add);
+ rules->trie = trie;
+ rules->trie_max += add;
+ }
+
+ /* insert new child node */
+ child_off = rules->trie_cur;
+ if (depth == 0) {
+ rules->trie_root[key] = child_off;
+ } else {
+ struct trie_node *parent = &rules->trie[node_off];
+ unsigned char child_slot = parent->child_cur;
+
+ /* no space in parent, we can't index this string, nevermind */
+ if (child_slot == TRIE_CHILD_MAX)
+ return off;
+
+ parent->child[child_slot] = child_off;
+ parent->child_key[child_slot] = key;
+ parent->child_cur = child_slot + 1;
+ }
+
+ /* allocate and construct the child node */
+ rules->trie_cur++;
+ child = &rules->trie[child_off];
+ memset(child, 0x00, sizeof(struct trie_node));
+ child->value_off = off;
+ child->value_len = len;
+
+ return off;
+}
+static int add_token(struct udev_rules *rules, struct token *token)
+{
/* grow buffer if needed */
if (rules->token_cur+1 >= rules->token_max) {
struct token *tokens;
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;
}
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;
}
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;
}
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) {
return -1;
temp[0] = '\0';
temp++;
- dbg(udev, "%s '%s'-'%s'\n", operation_str[*op], *key, *value);
+ dbg(udev, "%s '%s'-'%s'\n", operation_str(*op), *key, *value);
/* move line to next key */
*line = temp;
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', flags: 0x%02x\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],
- 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;
- }
-}
-
-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;
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);
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];
}
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);
}
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)
if (rules->buf == NULL)
return NULL;
rules->buf_max = PREALLOC_STRBUF;
+ rules->trie = malloc(PREALLOC_TRIE * sizeof(struct trie_node));
+ if (rules->trie == NULL)
+ return NULL;
+ rules->trie_max = PREALLOC_TRIE;
+ rules->trie_root = calloc(UCHAR_MAX + 1, sizeof(unsigned short));
/* 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);
+ /* offset 0 is reserved for the null trie node */
+ rules->trie_cur = 1;
+ 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 */
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, '/');
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);
}
}
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;
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);
+ info(udev, "used %zu bytes of string index nodes (%hu * %zu bytes)\n",
+ rules->trie_cur * sizeof(struct trie_node), rules->trie_cur, sizeof(struct trie_node));
+
+ /* cleanup trie */
+ free(rules->trie);
+ rules->trie = NULL;
+ rules->trie_cur = 0;
+ rules->trie_max = 0;
+ free(rules->trie_root);
+ rules->trie_root = NULL;
/* cleanup uid/gid cache */
free(rules->uids);
return;
free(rules->tokens);
free(rules->buf);
+ free(rules->trie);
+ free(rules->trie_root);
free(rules->uids);
free(rules->gids);
free(rules);
pos[0] = '\0';
pos = &pos[1];
}
- dbg(rules->udev, "match %s '%s' <-> '%s'\n", token_str[token->type], key_value, val);
+ dbg(rules->udev, "match %s '%s' <-> '%s'\n", token_str(token->type), key_value, val);
match = (fnmatch(key_value, val, 0) == 0);
if (match)
break;
}
if (match && (token->key.op == OP_MATCH)) {
- dbg(rules->udev, "%s is true (matching value)\n", token_str[token->type]);
+ dbg(rules->udev, "%s is true (matching value)\n", token_str(token->type));
return 0;
}
if (!match && (token->key.op == OP_NOMATCH)) {
- dbg(rules->udev, "%s is true (non-matching value)\n", token_str[token->type]);
+ dbg(rules->udev, "%s is true (non-matching value)\n", token_str(token->type));
return 0;
}
- dbg(rules->udev, "%s is not true\n", token_str[token->type]);
+ dbg(rules->udev, "%s is not true\n", token_str(token->type));
return -1;
}
cur = &rules->tokens[0];
rule = cur;
while (1) {
- unsigned int idx;
-
dump_token(rules, cur);
switch (cur->type) {
case TK_RULE:
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;
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;
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);
}
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;
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);
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);
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)
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));
}
}