X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=udev_rules_parse.c;h=cf69783c2daf0985204e74c54fd8e95d83d12ff0;hb=6369839195d7572151d986ddc2050162e6879585;hp=89925a35e42862c6f111ba281cf78f0e5ece4409;hpb=bf5d2964730e63316861e310d1f24c165b11e961;p=elogind.git diff --git a/udev_rules_parse.c b/udev_rules_parse.c index 89925a35e..cf69783c2 100644 --- a/udev_rules_parse.c +++ b/udev_rules_parse.c @@ -37,9 +37,45 @@ #include "logging.h" #include "udev_rules.h" -LIST_HEAD(udev_rule_list); +/* rules parsed from .rules files*/ +static LIST_HEAD(rules_list); +static struct list_head *rules_list_current; -static int add_config_dev(struct udev_rule *rule) +/* 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) +{ + rules_list_current = rules_list.next; + rules_array_current = 0; + + return 0; +} + +struct udev_rule *udev_rules_iter_next(void) +{ + 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; +} + +static int add_rule_to_list(struct udev_rule *rule) { struct udev_rule *tmp_rule; @@ -47,16 +83,16 @@ static int add_config_dev(struct udev_rule *rule) if (tmp_rule == NULL) return -ENOMEM; memcpy(tmp_rule, rule, sizeof(struct udev_rule)); - list_add_tail(&tmp_rule->node, &udev_rule_list); + 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='%s', program='%s', result='%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, rule->program, rule->result, rule->owner, rule->group, rule->mode, + rule->kernel_name, rule->program, rule->result, rule->owner, rule->group, rule->mode, rule->partitions, rule->ignore_remove, rule->ignore_device, rule->last_rule); return 0; @@ -248,59 +284,59 @@ static int rules_parse(const char *filename) if (retval) break; - if (strcasecmp(key, KEY_KERNEL) == 0) { - strlcpy(rule.kernel, value, sizeof(rule.kernel)); + if (strcasecmp(key, "KERNEL") == 0) { + strlcpy(rule.kernel_name, value, sizeof(rule.kernel_name)); rule.kernel_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_SUBSYSTEM) == 0) { + if (strcasecmp(key, "SUBSYSTEM") == 0) { strlcpy(rule.subsystem, value, sizeof(rule.subsystem)); rule.subsystem_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_ACTION) == 0) { + if (strcasecmp(key, "ACTION") == 0) { strlcpy(rule.action, value, sizeof(rule.action)); rule.action_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_DEVPATH) == 0) { + if (strcasecmp(key, "DEVPATH") == 0) { strlcpy(rule.devpath, value, sizeof(rule.devpath)); rule.devpath_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_BUS) == 0) { + if (strcasecmp(key, "BUS") == 0) { strlcpy(rule.bus, value, sizeof(rule.bus)); rule.bus_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_ID) == 0) { + if (strcasecmp(key, "ID") == 0) { strlcpy(rule.id, value, sizeof(rule.id)); rule.id_operation = operation; valid = 1; continue; } - if (strncasecmp(key, KEY_SYSFS, sizeof(KEY_SYSFS)-1) == 0) { + if (strncasecmp(key, "SYSFS", sizeof("SYSFS")-1) == 0) { struct key_pair *pair; if (rule.sysfs_pair_count >= KEY_SYSFS_PAIRS_MAX) { - err("skip rule, to many " KEY_SYSFS " keys in a single rule"); + err("skip rule, to many SYSFS keys in a single rule"); goto error; } pair = &rule.sysfs_pair[rule.sysfs_pair_count]; - attr = get_key_attribute(key + sizeof(KEY_SYSFS)-1); + attr = get_key_attribute(key + sizeof("SYSFS")-1); if (attr == NULL) { - err("error parsing " KEY_SYSFS " attribute"); + err("error parsing SYSFS attribute"); goto error; } strlcpy(pair->name, attr, sizeof(pair->name)); @@ -311,17 +347,17 @@ static int rules_parse(const char *filename) continue; } - if (strncasecmp(key, KEY_ENV, sizeof(KEY_ENV)-1) == 0) { + if (strncasecmp(key, "ENV", sizeof("ENV")-1) == 0) { struct key_pair *pair; if (rule.env_pair_count >= KEY_ENV_PAIRS_MAX) { - err("skip rule, to many " KEY_ENV " keys in a single rule"); + 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(KEY_ENV)-1); + attr = get_key_attribute(key + sizeof("ENV")-1); if (attr == NULL) { - err("error parsing " KEY_ENV " attribute"); + err("error parsing ENV attribute"); continue; } strlcpy(pair->name, attr, sizeof(pair->name)); @@ -332,28 +368,57 @@ static int rules_parse(const char *filename) continue; } - if (strcasecmp(key, KEY_MODALIAS) == 0) { + if (strcasecmp(key, "MODALIAS") == 0) { strlcpy(rule.modalias, value, sizeof(rule.modalias)); rule.modalias_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_DRIVER) == 0) { + 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; + } + } + 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; } - if (strcasecmp(key, KEY_RESULT) == 0) { + if (strcasecmp(key, "RESULT") == 0) { strlcpy(rule.result, value, sizeof(rule.result)); rule.result_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_PROGRAM) == 0) { + if (strcasecmp(key, "PROGRAM") == 0) { strlcpy(rule.program, value, sizeof(rule.program)); rule.program_operation = operation; program_given = 1; @@ -361,15 +426,14 @@ static int rules_parse(const char *filename) continue; } - if (strncasecmp(key, KEY_NAME, sizeof(KEY_NAME)-1) == 0) { - attr = get_key_attribute(key + sizeof(KEY_NAME)-1); + if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) { + attr = get_key_attribute(key + sizeof("NAME")-1); if (attr != NULL) { - if (strstr(attr, OPTION_PARTITIONS) != NULL) { + if (strstr(attr, "all_partitions") != NULL) { dbg("creation of partition nodes requested"); rule.partitions = DEFAULT_PARTITIONS_COUNT; } - /* FIXME: remove old style option and make OPTIONS= mandatory */ - if (strstr(attr, OPTION_IGNORE_REMOVE) != NULL) { + if (strstr(attr, "ignore_remove") != NULL) { dbg("remove event should be ignored"); rule.ignore_remove = 1; } @@ -380,55 +444,55 @@ static int rules_parse(const char *filename) continue; } - if (strcasecmp(key, KEY_SYMLINK) == 0) { + if (strcasecmp(key, "SYMLINK") == 0) { strlcpy(rule.symlink, value, sizeof(rule.symlink)); rule.symlink_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_OWNER) == 0) { + if (strcasecmp(key, "OWNER") == 0) { strlcpy(rule.owner, value, sizeof(rule.owner)); rule.owner_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_GROUP) == 0) { + if (strcasecmp(key, "GROUP") == 0) { strlcpy(rule.group, value, sizeof(rule.group)); rule.group_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_MODE) == 0) { + if (strcasecmp(key, "MODE") == 0) { rule.mode = strtol(value, NULL, 8); rule.mode_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_RUN) == 0) { + if (strcasecmp(key, "RUN") == 0) { strlcpy(rule.run, value, sizeof(rule.run)); rule.run_operation = operation; valid = 1; continue; } - if (strcasecmp(key, KEY_OPTIONS) == 0) { - if (strstr(value, OPTION_LAST_RULE) != NULL) { + if (strcasecmp(key, "OPTIONS") == 0) { + if (strstr(value, "last_rule") != NULL) { dbg("last rule to be applied"); rule.last_rule = 1; } - if (strstr(value, OPTION_IGNORE_DEVICE) != NULL) { + if (strstr(value, "ignore_device") != NULL) { dbg("device should be ignored"); rule.ignore_device = 1; } - if (strstr(value, OPTION_IGNORE_REMOVE) != NULL) { + if (strstr(value, "ignore_remove") != NULL) { dbg("remove event should be ignored"); rule.ignore_remove = 1; } - if (strstr(value, OPTION_PARTITIONS) != NULL) { + if (strstr(value, "all_partitions") != NULL) { dbg("creation of partition nodes requested"); rule.partitions = DEFAULT_PARTITIONS_COUNT; } @@ -445,15 +509,15 @@ static int rules_parse(const char *filename) goto error; if ((rule.result[0] != '\0') && (program_given == 0)) { - info(KEY_RESULT " is only useful when " KEY_PROGRAM " is called in any rule before"); + info("RESULT is only useful when PROGRAM is called in any rule before"); goto error; } rule.config_line = lineno; strlcpy(rule.config_file, filename, sizeof(rule.config_file)); - retval = add_config_dev(&rule); + retval = add_rule_to_list(&rule); if (retval) { - dbg("add_config_dev returned with error %d", retval); + dbg("add_rule_to_list returned with error %d", retval); continue; error: err("parse error %s, line %d:%d, rule skipped", @@ -465,20 +529,47 @@ error: return retval; } +static int rules_map(const char *filename) +{ + char *buf; + size_t size; + + if (file_map(filename, &buf, &size)) + return -1; + if (size == 0) + 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) { + char comp[PATH_SIZE]; struct stat stats; int retval; + 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); + } + if (stat(udev_rules_filename, &stats) != 0) return -1; - if ((stats.st_mode & S_IFMT) != S_IFDIR) + if ((stats.st_mode & S_IFMT) != S_IFDIR) { + dbg("parse single rules file '%s'", udev_rules_filename); retval = rules_parse(udev_rules_filename); - else { + } else { struct name_entry *name_loop, *name_tmp; LIST_HEAD(name_list); + dbg("parse rules directory '%s'", udev_rules_filename); retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX); list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) { @@ -495,9 +586,11 @@ void udev_rules_close(void) struct udev_rule *rule; struct udev_rule *temp_rule; - list_for_each_entry_safe(rule, temp_rule, &udev_rule_list, node) { - list_del(&rule->node); - free(rule); - } + if (rules_array) + file_unmap(rules_array, rules_array_size); + else + list_for_each_entry_safe(rule, temp_rule, &rules_list, node) { + list_del(&rule->node); + free(rule); + } } -