summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKay Sievers <kay.sievers@suse.de>2005-07-05 15:24:41 +0200
committerKay Sievers <kay.sievers@suse.de>2005-07-05 15:24:41 +0200
commit8bd41f36f793f7fc208ef6beb4b2b84e35a5e728 (patch)
treed2a8d9f3ad291cf9f341cc87d87be80f435cfc30
parent6369839195d7572151d986ddc2050162e6879585 (diff)
pack parsed rules list
This cuts down our 600 rules file to 98 kb instead of 1.9 Mb memory or file-size with precompiled rules. Signed-off-by: Kay Sievers <kay.sievers@suse.de>
-rw-r--r--udev.c13
-rw-r--r--udev_rules.c246
-rw-r--r--udev_rules.h113
-rw-r--r--udev_rules_parse.c648
-rw-r--r--udevrulescompile.c42
-rw-r--r--udevstart.c6
-rw-r--r--udevtest.c5
7 files changed, 517 insertions, 556 deletions
diff --git a/udev.c b/udev.c
index 1c55ec14b8..7a0484472a 100644
--- a/udev.c
+++ b/udev.c
@@ -68,6 +68,7 @@ static void asmlinkage sig_handler(int signum)
int main(int argc, char *argv[], char *envp[])
{
struct udevice udev;
+ struct udev_rules rules;
char path[PATH_SIZE];
const char *error;
const char *action;
@@ -118,7 +119,7 @@ int main(int argc, char *argv[], char *envp[])
}
udev_init_device(&udev, devpath, subsystem, action);
- udev_rules_init();
+ udev_rules_init(&rules, 0);
if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS || udev.type == DEV_NET) {
/* handle device node */
@@ -143,7 +144,7 @@ int main(int argc, char *argv[], char *envp[])
if (udev.type == DEV_NET || udev.devt) {
/* name device */
- udev_rules_get_name(&udev, class_dev);
+ udev_rules_get_name(&rules, &udev, class_dev);
if (udev.ignore_device) {
info("device event will be ignored");
goto cleanup;
@@ -157,7 +158,7 @@ int main(int argc, char *argv[], char *envp[])
retval = udev_add_device(&udev, class_dev);
} else {
dbg("no dev-file found");
- udev_rules_get_run(&udev, NULL);
+ udev_rules_get_run(&rules, &udev, NULL);
if (udev.ignore_device) {
info("device event will be ignored");
goto cleanup;
@@ -166,7 +167,7 @@ int main(int argc, char *argv[], char *envp[])
sysfs_close_class_device(class_dev);
} else if (strcmp(action, "remove") == 0) {
dbg("node remove");
- udev_rules_get_run(&udev, NULL);
+ udev_rules_get_run(&rules, &udev, NULL);
if (udev.ignore_device) {
dbg("device event will be ignored");
goto cleanup;
@@ -193,7 +194,7 @@ int main(int argc, char *argv[], char *envp[])
}
dbg("devices device opened '%s'", path);
wait_for_devices_device(devices_dev, &error);
- udev_rules_get_run(&udev, devices_dev);
+ udev_rules_get_run(&rules, &udev, devices_dev);
sysfs_close_device(devices_dev);
if (udev.ignore_device) {
info("device event will be ignored");
@@ -201,7 +202,7 @@ int main(int argc, char *argv[], char *envp[])
}
} else {
dbg("default handling");
- udev_rules_get_run(&udev, NULL);
+ udev_rules_get_run(&rules, &udev, NULL);
if (udev.ignore_device) {
info("device event will be ignored");
goto cleanup;
diff --git a/udev_rules.c b/udev_rules.c
index 48e22ad3e8..48397ac1c7 100644
--- a/udev_rules.c
+++ b/udev_rules.c
@@ -599,25 +599,38 @@ found:
}
}
-static int match_key(const char *key, const char *key_val, enum key_operation key_op, const char *val)
+static char *key_val(struct udev_rule *rule, struct key *key)
+{
+ return rule->buf + key->val_off;
+}
+
+static char *key_pair_name(struct udev_rule *rule, struct key_pair *pair)
+{
+ return rule->buf + pair->key_name_off;
+}
+
+static int match_key(const char *key_name, struct udev_rule *rule, struct key *key, const char *val)
{
int match;
+ char *key_value;
- if (key_op == KEY_OP_UNSET)
+ if (key->operation == KEY_OP_UNSET)
return 0;
- dbg("check for %s '%s' <-> '%s'", key, key_val, val);
- match = (strcmp_pattern(key_val, val) == 0);
- if (match && (key_op != KEY_OP_NOMATCH)) {
- dbg("%s key is matching (matching value)", key);
+ key_value = rule->buf + key->val_off;
+
+ 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_op == KEY_OP_NOMATCH)) {
- dbg("%s key is matching, (non matching value)", key);
+ if (!match && (key->operation == KEY_OP_NOMATCH)) {
+ dbg("%s key is matching, (non matching value)", key_name);
return 0;
}
- dbg("%s key is not matching", key);
+ dbg("%s key is not matching", key_name);
return -1;
}
@@ -626,108 +639,108 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
{
struct sysfs_device *parent_device = sysfs_device;
- if (match_key("ACTION", rule->action, rule->action_operation, udev->action))
+ if (match_key("ACTION", rule, &rule->action, udev->action))
goto exit;
- if (match_key("KERNEL", rule->kernel_name, rule->kernel_operation, udev->kernel_name))
+ if (match_key("KERNEL", rule, &rule->kernel_name, udev->kernel_name))
goto exit;
- if (match_key("SUBSYSTEM", rule->subsystem, rule->subsystem_operation, udev->subsystem))
+ if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->subsystem))
goto exit;
- if (match_key("DEVPATH", rule->devpath, rule->devpath_operation, udev->devpath))
+ if (match_key("DEVPATH", rule, &rule->devpath, udev->devpath))
goto exit;
- if (rule->modalias_operation != KEY_OP_UNSET) {
+ if (rule->modalias.operation != KEY_OP_UNSET) {
char value[NAME_SIZE];
if (find_sysfs_attribute(NULL, sysfs_device, "modalias", value, sizeof(value)) != 0) {
dbg("MODALIAS value not found");
goto exit;
}
- if (match_key("MODALIAS", rule->modalias, rule->modalias_operation, value))
+ if (match_key("MODALIAS", rule, &rule->modalias, value))
goto exit;
}
- if (rule->env_pair_count) {
+ if (rule->env.count) {
int i;
- for (i = 0; i < rule->env_pair_count; i++) {
- struct key_pair *pair;
- const char *value;
+ dbg("check %i ENV keys", rule->env.count);
+ for (i = 0; i < rule->env.count; i++) {
+ struct key_pair *pair = &rule->env.keys[i];
+ const char *key_name = key_pair_name(rule, pair);
+ const char *value = getenv(key_name);
- pair = &rule->env_pair[i];
- value = getenv(pair->name);
if (!value) {
- dbg("ENV{'%s'} is not found", pair->name);
+ dbg("ENV{'%s'} is not found", key_name);
goto exit;
}
- dbg("check %i ENV keys", rule->env_pair_count);
- if (match_key(pair->name, pair->value, pair->operation, value))
+ if (match_key("ENV", rule, &pair->key, value))
goto exit;
}
- dbg("all %i ENV keys matched", rule->env_pair_count);
+ dbg("all %i ENV keys matched", rule->env.count);
}
/* walk up the chain of physical devices and find a match */
while (1) {
/* check for matching driver */
- if (rule->driver_operation != KEY_OP_UNSET) {
+ if (rule->driver.operation != KEY_OP_UNSET) {
if (parent_device == NULL) {
dbg("device has no sysfs_device");
goto exit;
}
- if (match_key("DRIVER", rule->driver, rule->driver_operation, parent_device->driver_name))
+ if (match_key("DRIVER", rule, &rule->driver, parent_device->driver_name))
goto try_parent;
}
/* check for matching bus value */
- if (rule->bus_operation != KEY_OP_UNSET) {
+ if (rule->bus.operation != KEY_OP_UNSET) {
if (parent_device == NULL) {
dbg("device has no sysfs_device");
goto exit;
}
- if (match_key("BUS", rule->bus, rule->bus_operation, parent_device->bus))
+ if (match_key("BUS", rule, &rule->bus, parent_device->bus))
goto try_parent;
}
/* check for matching bus id */
- if (rule->id_operation != KEY_OP_UNSET) {
+ if (rule->id.operation != KEY_OP_UNSET) {
if (parent_device == NULL) {
dbg("device has no sysfs_device");
goto exit;
}
- if (match_key("ID", rule->id, rule->id_operation, parent_device->bus_id))
+ if (match_key("ID", rule, &rule->id, parent_device->bus_id))
goto try_parent;
}
/* check for matching sysfs pairs */
- if (rule->sysfs_pair_count) {
+ if (rule->sysfs.count) {
int i;
- for (i = 0; i < rule->sysfs_pair_count; i++) {
- struct key_pair *pair;
+ dbg("check %i SYSFS keys", rule->sysfs.count);
+ for (i = 0; i < rule->sysfs.count; i++) {
+ struct key_pair *pair = &rule->sysfs.keys[i];
+ const char *key_name = key_pair_name(rule, pair);
+ const char *key_value = key_val(rule, &pair->key);
char value[VALUE_SIZE];
size_t len;
- pair = &rule->sysfs_pair[i];
- if (find_sysfs_attribute(class_dev, parent_device, pair->name, value, sizeof(value)) != 0)
+ if (find_sysfs_attribute(class_dev, parent_device, key_name, value, sizeof(value)) != 0)
goto try_parent;
/* strip trailing whitespace of value, if not asked to match for it */
- len = strlen(pair->value);
- if (len && !isspace(pair->value[len-1])) {
+ len = strlen(key_value);
+ if (len && !isspace(key_value[len-1])) {
len = strlen(value);
while (len > 0 && isspace(value[len-1]))
value[--len] = '\0';
dbg("removed %zi trailing whitespace chars from '%s'", strlen(value)-len, value);
}
- dbg("check %i SYSFS keys", rule->sysfs_pair_count);
- if (match_key(pair->name, pair->value, pair->operation, value))
+ if (match_key("SYSFS", rule, &pair->key, value))
goto try_parent;
}
- dbg("all %i SYSFS keys matched", rule->sysfs_pair_count);
+ dbg("all %i SYSFS keys matched", rule->sysfs.count);
}
/* found matching physical device */
@@ -742,11 +755,11 @@ try_parent:
}
/* import variables from file into environment */
- if (rule->import_operation != KEY_OP_UNSET) {
+ if (rule->import.operation != KEY_OP_UNSET) {
char import[PATH_SIZE];
int rc = -1;
- strlcpy(import, rule->import, sizeof(import));
+ 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) {
@@ -758,24 +771,24 @@ try_parent:
}
if (rc) {
dbg("IMPORT failed");
- if (rule->import_operation != KEY_OP_NOMATCH)
+ if (rule->import.operation != KEY_OP_NOMATCH)
goto exit;
} else
- dbg("IMPORT '%s' imported", rule->import);
+ dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
dbg("IMPORT key is true");
}
/* execute external program */
- if (rule->program_operation != KEY_OP_UNSET) {
+ if (rule->program.operation != KEY_OP_UNSET) {
char program[PATH_SIZE];
char result[PATH_SIZE];
- strlcpy(program, rule->program, sizeof(program));
+ strlcpy(program, key_val(rule, &rule->program), sizeof(program));
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");
- if (rule->program_operation != KEY_OP_NOMATCH)
+ if (rule->program.operation != KEY_OP_NOMATCH)
goto exit;
} else {
dbg("PROGRAM matches");
@@ -784,14 +797,14 @@ try_parent:
dbg("result is '%s'", result);
strlcpy(udev->program_result, result, sizeof(udev->program_result));
dbg("PROGRAM returned successful");
- if (rule->program_operation == KEY_OP_NOMATCH)
+ if (rule->program.operation == KEY_OP_NOMATCH)
goto exit;
}
dbg("PROGRAM key is true");
}
/* check for matching result of external program */
- if (match_key("RESULT", rule->result, rule->result_operation, udev->program_result))
+ if (match_key("RESULT", rule, &rule->result, udev->program_result))
goto exit;
/* rule matches */
@@ -801,7 +814,7 @@ exit:
return -1;
}
-int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_dev)
+int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct sysfs_class_device *class_dev)
{
struct sysfs_class_device *class_dev_parent;
struct sysfs_device *sysfs_device = NULL;
@@ -831,13 +844,13 @@ int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_d
dbg("udev->kernel_name='%s'", udev->kernel_name);
/* look for a matching rule to apply */
- udev_rules_iter_init();
+ udev_rules_iter_init(rules);
while (1) {
- rule = udev_rules_iter_next();
+ rule = udev_rules_iter_next(rules);
if (rule == NULL)
break;
- if (udev->name_set && rule->name_operation != KEY_OP_UNSET) {
+ if (udev->name_set && rule->name.operation != KEY_OP_UNSET) {
dbg("node name already set, rule ignored");
continue;
}
@@ -846,8 +859,7 @@ int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_d
if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
/* apply options */
if (rule->ignore_device) {
- info("configured rule in '%s[%i]' applied, '%s' is ignored",
- rule->config_file, rule->config_line, udev->kernel_name);
+ info("rule applied, '%s' is ignored", udev->kernel_name);
udev->ignore_device = 1;
return 0;
}
@@ -866,31 +878,31 @@ int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_d
if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
udev->mode_final = 1;
udev->mode = rule->mode;
- dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
+ dbg("applied mode=%#o to '%s'", rule->mode, udev->kernel_name);
}
- if (!udev->owner_final && rule->owner[0] != '\0') {
- if (rule->owner_operation == KEY_OP_ASSIGN_FINAL)
+ if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
+ if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
udev->owner_final = 1;
- strlcpy(udev->owner, rule->owner, sizeof(udev->owner));
+ strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
}
- if (!udev->group_final && rule->group[0] != '\0') {
- if (rule->group_operation == KEY_OP_ASSIGN_FINAL)
+ if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
+ if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
udev->group_final = 1;
- strlcpy(udev->group, rule->group, sizeof(udev->group));
- apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
+ 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);
dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
}
/* collect symlinks */
- if (!udev->symlink_final && rule->symlink_operation != KEY_OP_UNSET) {
+ if (!udev->symlink_final && rule->symlink.operation != KEY_OP_UNSET) {
char temp[PATH_SIZE];
char *pos, *next;
- if (rule->symlink_operation == KEY_OP_ASSIGN_FINAL)
+ if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
udev->symlink_final = 1;
- if (rule->symlink_operation == KEY_OP_ASSIGN || rule->symlink_operation == KEY_OP_ASSIGN_FINAL) {
+ if (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
struct name_entry *name_loop;
struct name_entry *temp_loop;
@@ -900,53 +912,42 @@ int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_d
free(name_loop);
}
}
- if (rule->symlink[0] != '\0') {
- info("configured rule in '%s[%i]' applied, added symlink '%s'",
- rule->config_file, rule->config_line, rule->symlink);
- strlcpy(temp, rule->symlink, sizeof(temp));
- apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
-
- /* add multiple symlinks separated by spaces */
- pos = temp;
- next = strchr(temp, ' ');
- while (next) {
- next[0] = '\0';
- info("add symlink '%s'", pos);
- name_list_add(&udev->symlink_list, pos, 0);
- pos = &next[1];
- next = strchr(pos, ' ');
- }
+ strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
+ apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
+ info("rule applied, added symlink '%s'", temp);
+
+ /* add multiple symlinks separated by spaces */
+ pos = temp;
+ next = strchr(temp, ' ');
+ while (next) {
+ next[0] = '\0';
info("add symlink '%s'", pos);
name_list_add(&udev->symlink_list, pos, 0);
+ pos = &next[1];
+ next = strchr(pos, ' ');
}
+ 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) {
+ if (rule->name.operation != KEY_OP_UNSET) {
udev->name_set = 1;
- if (rule->name[0] == '\0') {
- info("configured rule in '%s[%i]' applied, node handling for '%s' supressed",
- rule->config_file, rule->config_line, udev->kernel_name);
- } else {
- strlcpy(udev->name, rule->name, sizeof(udev->name));
- apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
- strlcpy(udev->config_file, rule->config_file, sizeof(udev->config_file));
- udev->config_line = rule->config_line;
-
- info("configured rule in '%s:%i' applied, '%s' becomes '%s'",
- rule->config_file, rule->config_line, udev->kernel_name, rule->name);
- if (udev->type != DEV_NET)
- dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
- udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
- }
+ strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
+ apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
+
+ info("rule applied, '%s' becomes '%s'", udev->kernel_name, udev->name);
+ if (udev->type != DEV_NET)
+ dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
+ udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
}
- if (!udev->run_final && rule->run_operation != KEY_OP_UNSET) {
+ if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
char program[PATH_SIZE];
- if (rule->run_operation == KEY_OP_ASSIGN_FINAL)
+ if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
udev->run_final = 1;
- if (rule->run_operation == KEY_OP_ASSIGN || rule->run_operation == KEY_OP_ASSIGN_FINAL) {
+ if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
struct name_entry *name_loop;
struct name_entry *temp_loop;
@@ -956,12 +957,10 @@ int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_d
free(name_loop);
}
}
- if (rule->run[0] != '\0') {
- strlcpy(program, rule->run, sizeof(program));
- apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
- dbg("add run '%s'", program);
- name_list_add(&udev->run_list, program, 0);
- }
+ strlcpy(program, key_val(rule, &rule->run), sizeof(program));
+ apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
+ dbg("add run '%s'", program);
+ name_list_add(&udev->run_list, program, 0);
}
if (rule->last_rule) {
@@ -973,7 +972,7 @@ int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_d
if (udev->name[0] == '\0') {
strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
- info("no rule found, use kernel name '%s'", udev->name);
+ info("no rule found, will use kernel name '%s'", udev->name);
}
if (udev->tmp_node[0] != '\0') {
@@ -985,36 +984,35 @@ int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_d
return 0;
}
-int udev_rules_get_run(struct udevice *udev, struct sysfs_device *sysfs_device)
+int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev, struct sysfs_device *sysfs_device)
{
struct udev_rule *rule;
/* look for a matching rule to apply */
- udev_rules_iter_init();
+ udev_rules_iter_init(rules);
while (1) {
- rule = udev_rules_iter_next();
+ rule = udev_rules_iter_next(rules);
if (rule == NULL)
break;
dbg("process rule");
- if (rule->name_operation != KEY_OP_UNSET || rule->symlink_operation != KEY_OP_UNSET ||
- rule->mode != 0000 || rule->owner[0] != '\0' || rule->group[0] != '\0') {
+ 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) {
dbg("skip rule that names a device");
continue;
}
if (match_rule(udev, rule, NULL, sysfs_device) == 0) {
if (rule->ignore_device) {
- info("configured rule in '%s[%i]' applied, '%s' is ignored",
- rule->config_file, rule->config_line, udev->kernel_name);
+ info("rule applied, '%s' is ignored", udev->kernel_name);
udev->ignore_device = 1;
return 0;
}
- if (!udev->run_final && rule->run_operation != KEY_OP_UNSET) {
+ if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
char program[PATH_SIZE];
- if (rule->run_operation == KEY_OP_ASSIGN || rule->run_operation == KEY_OP_ASSIGN_FINAL) {
+ if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
struct name_entry *name_loop;
struct name_entry *temp_loop;
@@ -1024,13 +1022,11 @@ int udev_rules_get_run(struct udevice *udev, struct sysfs_device *sysfs_device)
free(name_loop);
}
}
- if (rule->run[0] != '\0') {
- strlcpy(program, rule->run, sizeof(program));
- apply_format(udev, program, sizeof(program), NULL, sysfs_device);
- dbg("add run '%s'", program);
- name_list_add(&udev->run_list, program, 0);
- }
- if (rule->run_operation == KEY_OP_ASSIGN_FINAL)
+ strlcpy(program, key_val(rule, &rule->run), sizeof(program));
+ apply_format(udev, program, sizeof(program), NULL, sysfs_device);
+ dbg("add run '%s'", program);
+ name_list_add(&udev->run_list, program, 0);
+ if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
break;
}
diff --git a/udev_rules.h b/udev_rules.h
index fb77509cce..4ecada1c03 100644
--- a/udev_rules.h
+++ b/udev_rules.h
@@ -27,9 +27,7 @@
#include "udev.h"
#include "list.h"
-#define KEY_SYSFS_PAIRS_MAX 5
-#define KEY_ENV_PAIRS_MAX 5
-
+#define PAIRS_MAX 5
#define RULEFILE_SUFFIX ".rules"
enum key_operation {
@@ -41,72 +39,69 @@ enum key_operation {
KEY_OP_ASSIGN_FINAL,
};
-struct key_pair {
- char name[NAME_SIZE];
- char value[VALUE_SIZE];
+struct key {
enum key_operation operation;
+ size_t val_off;
+};
+
+struct key_pair {
+ struct key key;
+ size_t key_name_off;
+};
+
+struct key_pairs {
+ int count;
+ struct key_pair keys[PAIRS_MAX];
};
struct udev_rule {
- struct list_head node;
-
- char kernel_name[NAME_SIZE];
- enum key_operation kernel_operation;
- char subsystem[NAME_SIZE];
- enum key_operation subsystem_operation;
- char action[NAME_SIZE];
- enum key_operation action_operation;
- char devpath[PATH_SIZE];
- enum key_operation devpath_operation;
- char bus[NAME_SIZE];
- enum key_operation bus_operation;
- char id[NAME_SIZE];
- enum key_operation id_operation;
- char driver[NAME_SIZE];
- enum key_operation driver_operation;
- char program[PATH_SIZE];
- enum key_operation program_operation;
- char result[PATH_SIZE];
- enum key_operation result_operation;
- struct key_pair sysfs_pair[KEY_SYSFS_PAIRS_MAX];
- int sysfs_pair_count;
- struct key_pair env_pair[KEY_ENV_PAIRS_MAX];
- int env_pair_count;
- char modalias[NAME_SIZE];
- enum key_operation modalias_operation;
- char import[PATH_SIZE];
- enum key_operation import_operation;
- int import_exec;
-
- char name[PATH_SIZE];
- enum key_operation name_operation;
- char symlink[PATH_SIZE];
- enum key_operation symlink_operation;
- char owner[USER_SIZE];
- enum key_operation owner_operation;
- char group[USER_SIZE];
- enum key_operation group_operation;
- mode_t mode;
+ struct key kernel_name;
+ struct key subsystem;
+ struct key action;
+ struct key devpath;
+ struct key bus;
+ struct key id;
+ struct key driver;
+ struct key program;
+ struct key result;
+ struct key modalias;
+ struct key import;
+ struct key_pairs sysfs;
+ struct key_pairs env;
+
+ struct key name;
+ struct key symlink;
+ struct key run;
+ struct key owner;
+ struct key group;
enum key_operation mode_operation;
- char run[PATH_SIZE];
- enum key_operation run_operation;
+ mode_t mode;
+
+ unsigned int partitions;
+ unsigned int last_rule:1,
+ ignore_device:1,
+ ignore_remove:1,
+ import_exec:1;
- int last_rule;
- int ignore_device;
- int ignore_remove;
- int partitions;
+ size_t bufsize;
+ char buf[];
+};
- char config_file[PATH_SIZE];
- int config_line;
+struct udev_rules {
+ char *buf;
+ size_t bufsize;
+ size_t current;
+ int mapped;
+ int resolve_names;
};
-extern int udev_rules_init(void);
-extern void udev_rules_close(void);
+extern int udev_rules_init(struct udev_rules *rules, int resolve_names);
+extern void udev_rules_close(struct udev_rules *rules);
-extern int udev_rules_iter_init(void);
-extern struct udev_rule *udev_rules_iter_next(void);
+extern void udev_rules_iter_init(struct udev_rules *rules);
+extern struct udev_rule *udev_rules_iter_next(struct udev_rules *rules);
-extern int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_dev);
-extern int udev_rules_get_run(struct udevice *udev, struct sysfs_device *sysfs_device);
+extern int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct sysfs_class_device *class_dev);
+extern int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev, struct sysfs_device *sysfs_device);
#endif
diff --git a/udev_rules_parse.c b/udev_rules_parse.c
index cf69783c2d..2ec9348b01 100644
--- a/udev_rules_parse.c
+++ b/udev_rules_parse.c
@@ -37,65 +37,29 @@
#include "logging.h"
#include "udev_rules.h"
-/* rules parsed from .rules files*/
-static LIST_HEAD(rules_list);
-static struct list_head *rules_list_current;
-/* mapped compiled rules stored on disk */
-static struct udev_rule *rules_array = NULL;
-static size_t rules_array_current;
-static size_t rules_array_size = 0;
-
-static size_t rules_count = 0;
-
-int udev_rules_iter_init(void)
+void udev_rules_iter_init(struct udev_rules *rules)
{
- rules_list_current = rules_list.next;
- rules_array_current = 0;
-
- return 0;
+ dbg("bufsize=%zi", rules->bufsize);
+ rules->current = 0;
}
-struct udev_rule *udev_rules_iter_next(void)
+struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
{
static struct udev_rule *rule;
- if (rules_array) {
- if (rules_array_current >= rules_count)
- return NULL;
- rule = &rules_array[rules_array_current];
- rules_array_current++;
- } else {
- dbg("head=%p current=%p next=%p", &rules_list, rules_list_current, rules_list_current->next);
- if (rules_list_current == &rules_list)
- return NULL;
- rule = list_entry(rules_list_current, struct udev_rule, node);
- rules_list_current = rules_list_current->next;
- }
- return rule;
-}
+ if (!rules)
+ return NULL;
-static int add_rule_to_list(struct udev_rule *rule)
-{
- struct udev_rule *tmp_rule;
-
- tmp_rule = malloc(sizeof(*tmp_rule));
- if (tmp_rule == NULL)
- return -ENOMEM;
- memcpy(tmp_rule, rule, sizeof(struct udev_rule));
- list_add_tail(&tmp_rule->node, &rules_list);
-
- dbg("name='%s', symlink='%s', bus='%s', id='%s', "
- "sysfs_file[0]='%s', sysfs_value[0]='%s', "
- "kernel_name='%s', program='%s', result='%s', "
- "owner='%s', group='%s', mode=%#o, "
- "all_partions=%u, ignore_remove=%u, ignore_device=%u, last_rule=%u",
- rule->name, rule->symlink, rule->bus, rule->id,
- rule->sysfs_pair[0].name, rule->sysfs_pair[0].value,
- rule->kernel_name, rule->program, rule->result, rule->owner, rule->group, rule->mode,
- rule->partitions, rule->ignore_remove, rule->ignore_device, rule->last_rule);
+ dbg("current=%zi", rules->current);
+ if (rules->current >= rules->bufsize)
+ return NULL;
- return 0;
+ /* get next rule */
+ rule = (struct udev_rule *) (rules->buf + rules->current);
+ rules->current += sizeof(struct udev_rule) + rule->bufsize;
+
+ return rule;
}
static int get_key(char **line, char **key, enum key_operation *operation, char **value)
@@ -210,353 +174,389 @@ static char *get_key_attribute(char *str)
return NULL;
}
-static int rules_parse(const char *filename)
+static int add_rule_key(struct udev_rule *rule, struct key *key,
+ enum key_operation operation, const char *value)
{
- char line[LINE_SIZE];
- char *bufline;
- int lineno;
+ size_t val_len = strnlen(value, PATH_SIZE);
+
+ key->operation = operation;
+
+ key->val_off = rule->bufsize;
+ strlcpy(rule->buf + rule->bufsize, value, val_len+1);
+ rule->bufsize += val_len+1;
+
+ return 0;
+}
+
+static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
+ enum key_operation operation, const char *key, const char *value)
+{
+ size_t key_len = strnlen(key, PATH_SIZE);
+
+ if (pairs->count >= PAIRS_MAX) {
+ err("skip, too many keys in a single rule");
+ return -1;
+ }
+
+ add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
+
+ /* add the key-name of the pair */
+ pairs->keys[pairs->count].key_name_off = rule->bufsize;
+ strlcpy(rule->buf + rule->bufsize, key, key_len+1);
+ rule->bufsize += key_len+1;
+
+ pairs->count++;
+
+ return 0;
+}
+
+static int add_to_rules(struct udev_rules *rules, char *line)
+{
+ struct udev_rule *rule;
+ size_t rule_size;
+ int valid;
char *linepos;
char *attr;
- char *buf;
- size_t bufsize;
- size_t cur;
- size_t count;
- int program_given = 0;
- int valid;
- int retval = 0;
- struct udev_rule rule;
+ int retval;
- if (file_map(filename, &buf, &bufsize) != 0) {
- err("can't open '%s' as rules file", filename);
+ /* get all the keys */
+ rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
+ if (!rule) {
+ err("malloc failed");
return -1;
}
- dbg("reading '%s' as rules file", filename);
+ linepos = line;
+ valid = 0;
- /* loop through the whole file */
- cur = 0;
- lineno = 0;
- while (cur < bufsize) {
- unsigned int i, j;
+ while (1) {
+ char *key;
+ char *value;
+ enum key_operation operation = KEY_OP_UNSET;
- count = buf_get_line(buf, bufsize, cur);
- bufline = &buf[cur];
- cur += count+1;
- lineno++;
+ retval = get_key(&linepos, &key, &operation, &value);
+ if (retval)
+ break;
- if (count >= sizeof(line)) {
- info("line too long, rule skipped %s, line %d", filename, lineno);
+ if (strcasecmp(key, "KERNEL") == 0) {
+ add_rule_key(rule, &rule->kernel_name, operation, value);
+ valid = 1;
continue;
}
- /* eat the whitespace */
- while ((count > 0) && isspace(bufline[0])) {
- bufline++;
- count--;
- }
- if (count == 0)
+ if (strcasecmp(key, "SUBSYSTEM") == 0) {
+ add_rule_key(rule, &rule->subsystem, operation, value);
+ valid = 1;
continue;
+ }
- /* see if this is a comment */
- if (bufline[0] == COMMENT_CHARACTER)
+ if (strcasecmp(key, "ACTION") == 0) {
+ add_rule_key(rule, &rule->action, operation, value);
+ valid = 1;
continue;
-
- /* skip backslash and newline from multi line rules */
- for (i = j = 0; i < count; i++) {
- if (bufline[i] == '\\' && bufline[i+1] == '\n')
- continue;
-
- line[j++] = bufline[i];
}
- line[j] = '\0';
- dbg("read '%s'", line);
- /* get all known keys */
- memset(&rule, 0x00, sizeof(struct udev_rule));
- linepos = line;
- valid = 0;
+ if (strcasecmp(key, "DEVPATH") == 0) {
+ add_rule_key(rule, &rule->devpath, operation, value);
+ valid = 1;
+ continue;
+ }
- while (1) {
- char *key;
- char *value;
- enum key_operation operation = KEY_OP_UNSET;
+ if (strcasecmp(key, "BUS") == 0) {
+ add_rule_key(rule, &rule->bus, operation, value);
+ valid = 1;
+ continue;
+ }
- retval = get_key(&linepos, &key, &operation, &value);
- if (retval)
- break;
+ if (strcasecmp(key, "ID") == 0) {
+ add_rule_key(rule, &rule->id, operation, value);
+ valid = 1;
+ continue;
+ }
- if (strcasecmp(key, "KERNEL") == 0) {
- strlcpy(rule.kernel_name, value, sizeof(rule.kernel_name));
- rule.kernel_operation = operation;
- valid = 1;
+ if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) {
+ attr = get_key_attribute(key + sizeof("SYSFS")-1);
+ if (attr == NULL) {
+ err("error parsing SYSFS attribute in '%s'", line);
continue;
}
+ add_rule_key_pair(rule, &rule->sysfs, operation, attr, value);
+ valid = 1;
+ continue;
+ }
- if (strcasecmp(key, "SUBSYSTEM") == 0) {
- strlcpy(rule.subsystem, value, sizeof(rule.subsystem));
- rule.subsystem_operation = operation;
- valid = 1;
+ if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
+ attr = get_key_attribute(key + sizeof("ENV")-1);
+ if (attr == NULL) {
+ err("error parsing ENV attribute");
continue;
}
+ add_rule_key_pair(rule, &rule->env, operation, attr, value);
+ valid = 1;
+ continue;
+ }
- if (strcasecmp(key, "ACTION") == 0) {
- strlcpy(rule.action, value, sizeof(rule.action));
- rule.action_operation = operation;
- valid = 1;
- continue;
- }
+ if (strcasecmp(key, "MODALIAS") == 0) {
+ add_rule_key(rule, &rule->modalias, operation, value);
+ valid = 1;
+ continue;
+ }
- if (strcasecmp(key, "DEVPATH") == 0) {
- strlcpy(rule.devpath, value, sizeof(rule.devpath));
- rule.devpath_operation = operation;
- valid = 1;
- continue;
+ if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
+ attr = get_key_attribute(key + sizeof("IMPORT")-1);
+ if (attr && strstr(attr, "program")) {
+ dbg("IMPORT will be executed");
+ rule->import_exec = 1;
+ } else if (attr && strstr(attr, "file")) {
+ dbg("IMPORT will be included as file");
+ } else {
+ /* figure it out if it is executable */
+ char file[PATH_SIZE];
+ char *pos;
+ struct stat stats;
+
+ strlcpy(file, value, sizeof(file));
+ pos = strchr(file, ' ');
+ if (pos)
+ pos[0] = '\0';
+ dbg("IMPORT auto mode for '%s'", file);
+ if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
+ dbg("IMPORT is executable, will be executed");
+ rule->import_exec = 1;
+ }
}
+ add_rule_key(rule, &rule->import, operation, value);
+ valid = 1;
+ continue;
+ }
- if (strcasecmp(key, "BUS") == 0) {
- strlcpy(rule.bus, value, sizeof(rule.bus));
- rule.bus_operation = operation;
- valid = 1;
- continue;
- }
+ if (strcasecmp(key, "DRIVER") == 0) {
+ add_rule_key(rule, &rule->driver, operation, value);
+ valid = 1;
+ continue;
+ }
- if (strcasecmp(key, "ID") == 0) {
- strlcpy(rule.id, value, sizeof(rule.id));
- rule.id_operation = operation;
- valid = 1;
- continue;
- }
+ if (strcasecmp(key, "RESULT") == 0) {
+ add_rule_key(rule, &rule->result, operation, value);
+ valid = 1;
+ continue;
+ }
- if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) {
- struct key_pair *pair;
+ if (strcasecmp(key, "PROGRAM") == 0) {
+ add_rule_key(rule, &rule->program, operation, value);
+ valid = 1;
+ continue;
+ }
- if (rule.sysfs_pair_count >= KEY_SYSFS_PAIRS_MAX) {
- err("skip rule, to many SYSFS keys in a single rule");
- goto error;
+ if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
+ attr = get_key_attribute(key + sizeof("NAME")-1);
+ if (attr != NULL) {
+ if (strstr(attr, "all_partitions") != NULL) {
+ dbg("creation of partition nodes requested");
+ rule->partitions = DEFAULT_PARTITIONS_COUNT;
}
- pair = &rule.sysfs_pair[rule.sysfs_pair_count];
- attr = get_key_attribute(key + sizeof("SYSFS")-1);
- if (attr == NULL) {
- err("error parsing SYSFS attribute");
- goto error;
+ if (strstr(attr, "ignore_remove") != NULL) {
+ dbg("remove event should be ignored");
+ rule->ignore_remove = 1;
}
- strlcpy(pair->name, attr, sizeof(pair->name));
- strlcpy(pair->value, value, sizeof(pair->value));
- pair->operation = operation;
- rule.sysfs_pair_count++;
- valid = 1;
- continue;
}
+ add_rule_key(rule, &rule->name, operation, value);
+ continue;
+ }
- if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) {
- struct key_pair *pair;
+ if (strcasecmp(key, "SYMLINK") == 0) {
+ add_rule_key(rule, &rule->symlink, operation, value);
+ valid = 1;
+ continue;
+ }
- if (rule.env_pair_count >= KEY_ENV_PAIRS_MAX) {
- err("skip rule, to many ENV keys in a single rule");
- goto error;
- }
- pair = &rule.env_pair[rule.env_pair_count];
- attr = get_key_attribute(key + sizeof("ENV")-1);
- if (attr == NULL) {
- err("error parsing ENV attribute");
+ if (strcasecmp(key, "OWNER") == 0) {
+ valid = 1;
+ if (rules->resolve_names) {
+ char *endptr;
+ strtoul(value, &endptr, 10);
+ if (endptr[0] != '\0') {
+ char owner[32];
+ uid_t uid = lookup_user(value);
+ dbg("replacing username='%s' by id=%i", value, uid);
+ sprintf(owner, "%li", uid);
+ add_rule_key(rule, &rule->owner, operation, owner);
continue;
}
- strlcpy(pair->name, attr, sizeof(pair->name));
- strlcpy(pair->value, value, sizeof(pair->value));
- pair->operation = operation;
- rule.env_pair_count++;
- valid = 1;
- continue;
}
- if (strcasecmp(key, "MODALIAS") == 0) {
- strlcpy(rule.modalias, value, sizeof(rule.modalias));
- rule.modalias_operation = operation;
- valid = 1;
- continue;
- }
+ add_rule_key(rule, &rule->owner, operation, value);
+ continue;
+ }
- if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
- attr = get_key_attribute(key + sizeof("IMPORT")-1);
- if (attr && strstr(attr, "program")) {
- dbg("IMPORT will be executed");
- rule.import_exec = 1;
- } else if (attr && strstr(attr, "file")) {
- dbg("IMPORT will be included as file");
- } else {
- /* figure it out if it is executable */
- char file[PATH_SIZE];
- char *pos;
- struct stat stats;
-
- strlcpy(file, value, sizeof(file));
- pos = strchr(file, ' ');
- if (pos)
- pos[0] = '\0';
- dbg("IMPORT auto mode for '%s'", file);
- if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
- dbg("IMPORT is executable, will be executed");
- rule.import_exec = 1;
- }
+ if (strcasecmp(key, "GROUP") == 0) {
+ valid = 1;
+ if (rules->resolve_names) {
+ char *endptr;
+ strtoul(value, &endptr, 10);
+ if (endptr[0] != '\0') {
+ char group[32];
+ gid_t gid = lookup_group(value);
+ dbg("replacing groupname='%s' by id=%i", value, gid);
+ sprintf(group, "%li", gid);
+ add_rule_key(rule, &rule->owner, operation, group);
+ continue;
}
- strlcpy(rule.import, value, sizeof(rule.import));
- rule.import_operation = operation;
- valid = 1;
- continue;
}
- if (strcasecmp(key, "DRIVER") == 0) {
- strlcpy(rule.driver, value, sizeof(rule.driver));
- rule.driver_operation = operation;
- valid = 1;
- continue;
- }
+ add_rule_key(rule, &rule->group, operation, value);
+ continue;
+ }
- if (strcasecmp(key, "RESULT") == 0) {
- strlcpy(rule.result, value, sizeof(rule.result));
- rule.result_operation = operation;
- valid = 1;
- continue;
- }
+ if (strcasecmp(key, "MODE") == 0) {
+ rule->mode = strtol(value, NULL, 8);
+ rule->mode_operation = operation;
+ valid = 1;
+ continue;
+ }
- if (strcasecmp(key, "PROGRAM") == 0) {
- strlcpy(rule.program, value, sizeof(rule.program));
- rule.program_operation = operation;
- program_given = 1;
- valid = 1;
- continue;
- }
+ if (strcasecmp(key, "RUN") == 0) {
+ add_rule_key(rule, &rule->run, operation, value);
+ valid = 1;
+ continue;
+ }
- if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
- attr = get_key_attribute(key + sizeof("NAME")-1);
- if (attr != NULL) {
- if (strstr(attr, "all_partitions") != NULL) {
- dbg("creation of partition nodes requested");
- rule.partitions = DEFAULT_PARTITIONS_COUNT;
- }
- if (strstr(attr, "ignore_remove") != NULL) {
- dbg("remove event should be ignored");
- rule.ignore_remove = 1;
- }
- }
- rule.name_operation = operation;
- strlcpy(rule.name, value, sizeof(rule.name));
- valid = 1;
- continue;
+ if (strcasecmp(key, "OPTIONS") == 0) {
+ if (strstr(value, "last_rule") != NULL) {
+ dbg("last rule to be applied");
+ rule->last_rule = 1;
}
-
- if (strcasecmp(key, "SYMLINK") == 0) {
- strlcpy(rule.symlink, value, sizeof(rule.symlink));
- rule.symlink_operation = operation;
- valid = 1;
- continue;
+ if (strstr(value, "ignore_device") != NULL) {
+ dbg("device should be ignored");
+ rule->ignore_device = 1;
}
-
- if (strcasecmp(key, "OWNER") == 0) {
- strlcpy(rule.owner, value, sizeof(rule.owner));
- rule.owner_operation = operation;
- valid = 1;
- continue;
+ if (strstr(value, "ignore_remove") != NULL) {
+ dbg("remove event should be ignored");
+ rule->ignore_remove = 1;
}
-
- if (strcasecmp(key, "GROUP") == 0) {
- strlcpy(rule.group, value, sizeof(rule.group));
- rule.group_operation = operation;
- valid = 1;
- continue;
+ if (strstr(value, "all_partitions") != NULL) {
+ dbg("creation of partition nodes requested");
+ rule->partitions = DEFAULT_PARTITIONS_COUNT;
}
+ valid = 1;
+ continue;
+ }
- if (strcasecmp(key, "MODE") == 0) {
- rule.mode = strtol(value, NULL, 8);
- rule.mode_operation = operation;
- valid = 1;
- continue;
- }
+ err("unknown key '%s', in '%s'", key, line);
+ }
- if (strcasecmp(key, "RUN") == 0) {
- strlcpy(rule.run, value, sizeof(rule.run));
- rule.run_operation = operation;
- valid = 1;
- continue;
- }
+ /* skip line if not any valid key was found */
+ if (!valid) {
+ err("invalid rule '%s'", line);
+ goto exit;
+ }
- if (strcasecmp(key, "OPTIONS") == 0) {
- if (strstr(value, "last_rule") != NULL) {
- dbg("last rule to be applied");
- rule.last_rule = 1;
- }
- if (strstr(value, "ignore_device") != NULL) {
- dbg("device should be ignored");
- rule.ignore_device = 1;
- }
- if (strstr(value, "ignore_remove") != NULL) {
- dbg("remove event should be ignored");
- rule.ignore_remove = 1;
- }
- if (strstr(value, "all_partitions") != NULL) {
- dbg("creation of partition nodes requested");
- rule.partitions = DEFAULT_PARTITIONS_COUNT;
- }
- valid = 1;
- continue;
- }
+ /* grow buffer and add rule */
+ rule_size = sizeof(struct udev_rule) + rule->bufsize;
+ rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
+ if (!rules->buf) {
+ err("realloc failed");
+ goto exit;
+ }
+ memcpy(rules->buf + rules->bufsize, rule, rule_size);
+ rules->bufsize += rule_size;
+exit:
+ free(rule);
+ return 0;
+}
- err("unknown key '%s'", key);
- goto error;
- }
+static int parse_file(struct udev_rules *rules, const char *filename)
+{
+ char line[LINE_SIZE];
+ char *bufline;
+ int lineno;
+ char *buf;
+ size_t bufsize;
+ size_t cur;
+ size_t count;
+ int retval = 0;
+
+ if (file_map(filename, &buf, &bufsize) != 0) {
+ err("can't open '%s' as rules file", filename);
+ return -1;
+ }
+ dbg("reading '%s' as rules file", filename);
- /* skip line if not any valid key was found */
- if (!valid)
- goto error;
+ /* loop through the whole file */
+ cur = 0;
+ lineno = 0;
+ while (cur < bufsize) {
+ unsigned int i, j;
+
+ count = buf_get_line(buf, bufsize, cur);
+ bufline = &buf[cur];
+ cur += count+1;
+ lineno++;
+
+ if (count >= sizeof(line)) {
+ info("line too long, rule skipped %s, line %d", filename, lineno);
+ continue;
+ }
- if ((rule.result[0] != '\0') && (program_given == 0)) {
- info("RESULT is only useful when PROGRAM is called in any rule before");
- goto error;
+ /* eat the whitespace */
+ while ((count > 0) && isspace(bufline[0])) {
+ bufline++;
+ count--;
}
+ if (count == 0)
+ continue;
- rule.config_line = lineno;
- strlcpy(rule.config_file, filename, sizeof(rule.config_file));
- retval = add_rule_to_list(&rule);
- if (retval) {
- dbg("add_rule_to_list returned with error %d", retval);
+ /* see if this is a comment */
+ if (bufline[0] == COMMENT_CHARACTER)
continue;
-error:
- err("parse error %s, line %d:%d, rule skipped",
- filename, lineno, (int) (linepos - line));
+
+ /* skip backslash and newline from multi line rules */
+ for (i = j = 0; i < count; i++) {
+ if (bufline[i] == '\\' && bufline[i+1] == '\n')
+ continue;
+
+ line[j++] = bufline[i];
}
+ line[j] = '\0';
+
+ dbg("read '%s'", line);
+ add_to_rules(rules, line);
}
file_unmap(buf, bufsize);
return retval;
}
-static int rules_map(const char *filename)
+static int rules_map(struct udev_rules *rules, const char *filename)
{
- char *buf;
- size_t size;
-
- if (file_map(filename, &buf, &size))
+ if (file_map(filename, &rules->buf, &rules->bufsize)) {
+ rules->buf = NULL;
return -1;
- if (size == 0)
+ }
+ if (rules->bufsize == 0) {
+ file_unmap(rules->buf, rules->bufsize);
+ rules->buf = NULL;
return -1;
- rules_array = (struct udev_rule *) buf;
- rules_array_size = size;
- rules_count = size / sizeof(struct udev_rule);
- dbg("found %zi compiled rules", rules_count);
+ }
return 0;
}
-int udev_rules_init(void)
+int udev_rules_init(struct udev_rules *rules, int resolve_names)
{
char comp[PATH_SIZE];
struct stat stats;
int retval;
+ memset(rules, 0x00, sizeof(struct udev_rules));
+ rules->resolve_names = resolve_names;
+
+ /* check for precompiled rules */
strlcpy(comp, udev_rules_filename, sizeof(comp));
strlcat(comp, ".compiled", sizeof(comp));
if (stat(comp, &stats) == 0) {
- dbg("parse compiled rules '%s'", comp);
- return rules_map(comp);
+ dbg("map compiled rules '%s'", comp);
+ if (rules_map(rules, comp) == 0)
+ return 0;
}
if (stat(udev_rules_filename, &stats) != 0)
@@ -564,7 +564,7 @@ int udev_rules_init(void)
if ((stats.st_mode & S_IFMT) != S_IFDIR) {
dbg("parse single rules file '%s'", udev_rules_filename);
- retval = rules_parse(udev_rules_filename);
+ retval = parse_file(rules, udev_rules_filename);
} else {
struct name_entry *name_loop, *name_tmp;
LIST_HEAD(name_list);
@@ -573,7 +573,7 @@ int udev_rules_init(void)
retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
- rules_parse(name_loop->name);
+ parse_file(rules, name_loop->name);
list_del(&name_loop->node);
}
}
@@ -581,16 +581,12 @@ int udev_rules_init(void)
return retval;
}
-void udev_rules_close(void)
+void udev_rules_close(struct udev_rules *rules)
{
- struct udev_rule *rule;
- struct udev_rule *temp_rule;
-
- if (rules_array)
- file_unmap(rules_array, rules_array_size);
+ if (rules->mapped)
+ file_unmap(rules->buf, rules->bufsize);
else
- list_for_each_entry_safe(rule, temp_rule, &rules_list, node) {
- list_del(&rule->node);
- free(rule);
- }
+ free(rules->buf);
+
+ rules->buf = NULL;
}
diff --git a/udevrulescompile.c b/udevrulescompile.c
index 9162a16327..a71286abe7 100644
--- a/udevrulescompile.c
+++ b/udevrulescompile.c
@@ -50,7 +50,7 @@ void log_message(int priority, const char *format, ...)
int main(int argc, char *argv[], char *envp[])
{
- struct udev_rule *rule;
+ struct udev_rules rules;
FILE *f;
char comp[PATH_SIZE];
char comp_tmp[PATH_SIZE];
@@ -65,12 +65,11 @@ int main(int argc, char *argv[], char *envp[])
strlcpy(comp_tmp, comp, sizeof(comp_tmp));
strlcat(comp_tmp, ".tmp", sizeof(comp_tmp));
- /* remove old version, otherwise we would read it
- * instead of the real rules */
+ /* remove old version, otherwise we would read it instead of the real rules */
unlink(comp);
unlink(comp_tmp);
- udev_rules_init();
+ udev_rules_init(&rules, 1);
f = fopen(comp_tmp, "w");
if (f == NULL) {
@@ -79,40 +78,11 @@ int main(int argc, char *argv[], char *envp[])
retval = 1;
goto exit;
}
- dbg("storing compiled rules in '%s'", comp_tmp);
-
- udev_rules_iter_init();
- while (1) {
- char *endptr;
- unsigned long id;
-
- rule = udev_rules_iter_next();
- if (rule == NULL)
- break;
-
- id = strtoul(rule->owner, &endptr, 10);
- if (endptr[0] != '\0') {
- uid_t uid;
-
- uid = lookup_user(rule->owner);
- dbg("replacing username='%s' by id=%i", rule->owner, uid);
- sprintf(rule->owner, "%li", uid);
- }
-
- id = strtoul(rule->group, &endptr, 10);
- if (endptr[0] != '\0') {
- gid_t gid;
-
- gid = lookup_group(rule->group);
- dbg("replacing groupname='%s' by id=%i", rule->group, gid);
- sprintf(rule->group, "%li", gid);
- }
-
- dbg("kernel_name='%s' name='%s'", rule->kernel_name, rule->name);
- fwrite(rule, sizeof(struct udev_rule), 1, f);
- }
+ dbg("storing compiled rules in '%s' size=%zi", comp_tmp, rules.bufsize);
+ fwrite(rules.buf, rules.bufsize, 1, f);
fclose(f);
+
dbg("activating compiled rules in '%s'", comp);
if (rename(comp_tmp, comp) != 0) {
err("unable to write file");
diff --git a/udevstart.c b/udevstart.c
index 22b781d52a..4ef92fd537 100644
--- a/udevstart.c
+++ b/udevstart.c
@@ -47,6 +47,7 @@
static const char *udev_run_str;
static const char *udev_log_str;
+static struct udev_rules rules;
#ifdef USE_LOG
void log_message(int priority, const char *format, ...)
@@ -139,7 +140,7 @@ static int add_device(const char *path, const char *subsystem)
dbg("sysfs_open_class_device_path failed");
return -1;
}
- udev_rules_get_name(&udev, class_dev);
+ udev_rules_get_name(&rules, &udev, class_dev);
if (udev.ignore_device) {
dbg("device event will be ignored");
goto exit;
@@ -360,11 +361,12 @@ int main(int argc, char *argv[], char *envp[])
/* trigger timeout to prevent hanging processes */
alarm(ALARM_TIMEOUT);
- udev_rules_init();
+ udev_rules_init(&rules, 1);
udev_scan_block();
udev_scan_class();
+ udev_rules_close(&rules);
logging_close();
return 0;
}
diff --git a/udevtest.c b/udevtest.c
index d3e43593bf..3e17b94715 100644
--- a/udevtest.c
+++ b/udevtest.c
@@ -55,6 +55,7 @@ void log_message (int priority, const char *format, ...)
int main(int argc, char *argv[], char *envp[])
{
+ struct udev_rules rules;
struct sysfs_class_device *class_dev;
char *devpath;
char path[PATH_SIZE];
@@ -93,7 +94,7 @@ int main(int argc, char *argv[], char *envp[])
info("looking at device '%s' from subsystem '%s'", devpath, subsystem);
/* initialize the naming deamon */
- udev_rules_init();
+ udev_rules_init(&rules, 0);
/* fill in values and test_run flag*/
udev_init_device(&udev, devpath, subsystem, "add");
@@ -114,7 +115,7 @@ int main(int argc, char *argv[], char *envp[])
/* simulate node creation with test flag */
udev.test_run = 1;
if (udev.type == DEV_NET || udev.devt) {
- udev_rules_get_name(&udev, class_dev);
+ udev_rules_get_name(&rules, &udev, class_dev);
udev_add_device(&udev, class_dev);
} else
info("only char and block devices with a dev-file are supported by this test program");