X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udev_rules.c;h=117ffdc2d07b4a24b80052b7691e216d059400a7;hp=52e0712d350411c7bd003028e294c70820a06da8;hb=a0e5382d66567cc91c1a1f571e9fb5288106e876;hpb=972d318a3123b00d0ed6b78bbcf70a0965841a8e diff --git a/udev_rules.c b/udev_rules.c index 52e0712d3..117ffdc2d 100644 --- a/udev_rules.c +++ b/udev_rules.c @@ -135,11 +135,153 @@ static int get_format_len(char **str) return -1; } -/** Finds the lowest positive N such that N isn't present in - * $(udevroot) either as a file or a symlink. - * - * @param name Name to check for - * @return 0 if didn't exist and N otherwise. +static int get_key(char **line, char **key, char **value) +{ + char *linepos; + char *temp; + + linepos = *line; + if (!linepos) + return -1; + + if (strchr(linepos, '\\')) { + dbg("escaped characters are not supported, skip"); + return -1; + } + + /* skip whitespace */ + while (isspace(linepos[0])) + linepos++; + + /* get the key */ + *key = linepos; + while (1) { + linepos++; + if (linepos[0] == '\0') + return -1; + if (isspace(linepos[0])) + break; + if (linepos[0] == '=') + break; + } + + /* terminate key */ + linepos[0] = '\0'; + linepos++; + + /* skip whitespace */ + while (isspace(linepos[0])) + linepos++; + + /* get the value*/ + if (linepos[0] == '"') { + linepos++; + temp = strchr(linepos, '"'); + if (!temp) { + dbg("missing closing quote"); + return -1; + } + dbg("value is quoted"); + temp[0] = '\0'; + } else if (linepos[0] == '\'') { + linepos++; + temp = strchr(linepos, '\''); + if (!temp) { + dbg("missing closing quote"); + return -1; + } + dbg("value is quoted"); + temp[0] = '\0'; + } else if (linepos[0] == '\0') { + dbg("value is empty"); + } else { + temp = linepos; + while (temp[0] && !isspace(temp[0])) + temp++; + temp[0] = '\0'; + } + *value = linepos; + + return 0; +} + +static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize) +{ + char line[LINE_SIZE]; + const char *bufline; + char *linepos; + char *variable; + char *value; + size_t cur; + size_t count; + int lineno; + + /* loop through the whole buffer */ + lineno = 0; + cur = 0; + while (cur < bufsize) { + count = buf_get_line(buf, bufsize, cur); + bufline = &buf[cur]; + cur += count+1; + lineno++; + + if (count >= sizeof(line)) { + err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno); + continue; + } + + /* eat the whitespace */ + while ((count > 0) && isspace(bufline[0])) { + bufline++; + count--; + } + if (count == 0) + continue; + + /* see if this is a comment */ + if (bufline[0] == COMMENT_CHARACTER) + continue; + + strlcpy(line, bufline, count+1); + + linepos = line; + if (get_key(&linepos, &variable, &value) == 0) { + dbg("import '%s=%s'", variable, value); + name_list_key_add(&udev->env_list, variable, value); + setenv(variable, value, 1); + } + } + + return 0; +} + +static int import_file_into_env(struct udevice *udev, const char *filename) +{ + char *buf; + size_t bufsize; + + if (file_map(filename, &buf, &bufsize) != 0) { + err("can't open '%s'", filename); + return -1; + } + import_keys_into_env(udev, buf, bufsize); + file_unmap(buf, bufsize); + + return 0; +} + +static int import_program_into_env(struct udevice *udev, const char *program) +{ + char result[1024]; + size_t reslen; + + if (execute_program(program, udev->subsystem, result, sizeof(result), &reslen) != 0) + return -1; + return import_keys_into_env(udev, result, reslen); +} + +/* finds the lowest positive N such that N isn't present in the udevdb + * if doesn't exist, 0 is returned, N otherwise */ static int find_free_number(struct udevice *udev, const char *name) { @@ -198,7 +340,7 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize, { char temp[PATH_SIZE]; char temp2[PATH_SIZE]; - char *head, *tail, *cpos, *attr, *rest; + char *head, *tail, *pos, *cpos, *attr, *rest; int len; int i; unsigned int next_free_number; @@ -218,6 +360,7 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize, SUBST_TEMP_NODE, SUBST_ROOT, SUBST_MODALIAS, + SUBST_ENV, }; static const struct subst_map { char *name; @@ -237,6 +380,7 @@ static void apply_format(struct udevice *udev, char *string, size_t maxsize, { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE }, { .name = "root", .fmt = 'r', .type = SUBST_ROOT }, { .name = "modalias", .fmt = 'A', .type = SUBST_MODALIAS }, + { .name = "env", .fmt = 'E', .type = SUBST_ENV }, {} }; enum subst_type type; @@ -431,6 +575,17 @@ found: strlcat(string, temp2, maxsize); dbg("substitute MODALIAS '%s'", temp2); break; + case SUBST_ENV: + if (attr == NULL) { + dbg("missing attribute"); + break; + } + pos = getenv(attr); + if (pos == NULL) + break; + strlcat(string, pos, maxsize); + dbg("substitute env '%s=%s'", attr, pos); + break; default: err("unknown substitution type=%i", type); break; @@ -440,115 +595,30 @@ found: head[len] = '\0'; dbg("truncate to %i chars, subtitution string becomes '%s'", len, head); } - strlcat(string, temp, maxsize); } } -static int execute_program_pipe(const char *command, const char *subsystem, char *value, int len) +static int match_key(const char *key, const char *key_val, enum key_operation key_op, const char *val) { - int retval; - int count; - int status; - int pipefds[2]; - pid_t pid; - char *pos; - char arg[PATH_SIZE]; - char *argv[(sizeof(arg) / 2) + 1]; - int devnull; - int i; + int match; - strlcpy(arg, command, sizeof(arg)); - i = 0; - if (strchr(arg, ' ')) { - pos = arg; - while (pos != NULL) { - if (pos[0] == '\'') { - /* don't separate if in apostrophes */ - pos++; - argv[i] = strsep(&pos, "\'"); - while (pos && pos[0] == ' ') - pos++; - } else { - argv[i] = strsep(&pos, " "); - } - dbg("arg[%i] '%s'", i, argv[i]); - i++; - } - argv[i] = NULL; - dbg("execute '%s' with parsed arguments", arg); - } else { - argv[0] = arg; - argv[1] = (char *) subsystem; - argv[2] = NULL; - dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]); - } + if (key_op == KEY_OP_UNSET) + return 0; - retval = pipe(pipefds); - if (retval != 0) { - err("pipe failed"); - return -1; + 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); + return 0; } - - pid = fork(); - switch(pid) { - case 0: - /* child dup2 write side of pipe to STDOUT */ - devnull = open("/dev/null", O_RDWR); - if (devnull >= 0) { - dup2(devnull, STDIN_FILENO); - dup2(devnull, STDERR_FILENO); - close(devnull); - } - dup2(pipefds[1], STDOUT_FILENO); - retval = execv(arg, argv); - err("exec of program failed"); - _exit(1); - case -1: - err("fork of '%s' failed", arg); - retval = -1; - break; - default: - /* parent reads from pipefds[0] */ - close(pipefds[1]); - retval = 0; - i = 0; - while (1) { - count = read(pipefds[0], value + i, len - i-1); - if (count < 0) { - err("read failed with '%s'", strerror(errno)); - retval = -1; - } - - if (count == 0) - break; - - i += count; - if (i >= len-1) { - err("result len %d too short", len); - retval = -1; - break; - } - } - value[i] = '\0'; - - close(pipefds[0]); - waitpid(pid, &status, 0); - - if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { - dbg("exec program status 0x%x", status); - retval = -1; - } + if (!match && (key_op == KEY_OP_NOMATCH)) { + dbg("%s key is matching, (non matching value)", key); + return 0; } - if (!retval) { - remove_trailing_char(value, '\n'); - dbg("result is '%s'", value); - replace_untrusted_chars(value); - } else - value[0] = '\0'; - - return retval; + dbg("%s key is not matching", key); + return -1; } static int match_rule(struct udevice *udev, struct udev_rule *rule, @@ -556,65 +626,17 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule, { struct sysfs_device *parent_device = sysfs_device; - if (rule->action_operation != KEY_OP_UNSET) { - dbg("check for " KEY_ACTION " rule->action='%s' udev->action='%s'", - rule->action, udev->action); - if (strcmp_pattern(rule->action, udev->action) != 0) { - dbg(KEY_ACTION " is not matching"); - if (rule->action_operation != KEY_OP_NOMATCH) - goto exit; - } else { - dbg(KEY_ACTION " matches"); - if (rule->action_operation == KEY_OP_NOMATCH) - goto exit; - } - dbg(KEY_ACTION " key is true"); - } + if (match_key(KEY_ACTION, rule->action, rule->action_operation, udev->action)) + goto exit; - if (rule->kernel_operation != KEY_OP_UNSET) { - dbg("check for " KEY_KERNEL " rule->kernel='%s' udev_kernel_name='%s'", - rule->kernel, udev->kernel_name); - if (strcmp_pattern(rule->kernel, udev->kernel_name) != 0) { - dbg(KEY_KERNEL " is not matching"); - if (rule->kernel_operation != KEY_OP_NOMATCH) - goto exit; - } else { - dbg(KEY_KERNEL " matches"); - if (rule->kernel_operation == KEY_OP_NOMATCH) - goto exit; - } - dbg(KEY_KERNEL " key is true"); - } + if (match_key(KEY_KERNEL, rule->kernel_name, rule->kernel_operation, udev->kernel_name)) + goto exit; - if (rule->subsystem_operation != KEY_OP_UNSET) { - dbg("check for " KEY_SUBSYSTEM " rule->subsystem='%s' udev->subsystem='%s'", - rule->subsystem, udev->subsystem); - if (strcmp_pattern(rule->subsystem, udev->subsystem) != 0) { - dbg(KEY_SUBSYSTEM " is not matching"); - if (rule->subsystem_operation != KEY_OP_NOMATCH) - goto exit; - } else { - dbg(KEY_SUBSYSTEM " matches"); - if (rule->subsystem_operation == KEY_OP_NOMATCH) - goto exit; - } - dbg(KEY_SUBSYSTEM " key is true"); - } + if (match_key(KEY_SUBSYSTEM, rule->subsystem, rule->subsystem_operation, udev->subsystem)) + goto exit; - if (rule->devpath_operation != KEY_OP_UNSET) { - dbg("check for " KEY_DEVPATH " rule->devpath='%s' udev->devpath='%s'", - rule->devpath, udev->devpath); - if (strcmp_pattern(rule->devpath, udev->devpath) != 0) { - dbg(KEY_DEVPATH " is not matching"); - if (rule->devpath_operation != KEY_OP_NOMATCH) - goto exit; - } else { - dbg(KEY_DEVPATH " matches"); - if (rule->devpath_operation == KEY_OP_NOMATCH) - goto exit; - } - dbg(KEY_DEVPATH " key is true"); - } + if (match_key(KEY_DEVPATH, rule->devpath, rule->devpath_operation, udev->devpath)) + goto exit; if (rule->modalias_operation != KEY_OP_UNSET) { char value[NAME_SIZE]; @@ -623,18 +645,8 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule, dbg(KEY_MODALIAS " value not found"); goto exit; } - dbg("check for " KEY_MODALIAS " rule->modalias='%s' modalias='%s'", - rule->modalias, value); - if (strcmp_pattern(rule->modalias, value) != 0) { - dbg(KEY_MODALIAS " is not matching"); - if (rule->modalias_operation != KEY_OP_NOMATCH) - goto exit; - } else { - dbg(KEY_MODALIAS " matches"); - if (rule->modalias_operation == KEY_OP_NOMATCH) - goto exit; - } - dbg(KEY_MODALIAS " key is true"); + if (match_key(KEY_MODALIAS, rule->modalias, rule->modalias_operation, value)) + goto exit; } if (rule->env_pair_count) { @@ -651,17 +663,11 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule, dbg(KEY_ENV "{'%s'} is not found", pair->name); goto exit; } - if (strcmp_pattern(pair->value, value) != 0) { - dbg(KEY_ENV "{'%s'} is not matching", pair->name); - if (pair->operation != KEY_OP_NOMATCH) - goto exit; - } else { - dbg(KEY_ENV "{'%s'} matches", pair->name); - if (pair->operation == KEY_OP_NOMATCH) - goto exit; - } + dbg("check %i " KEY_ENV " keys", rule->env_pair_count); + if (match_key(pair->name, pair->value, pair->operation, value)) + goto exit; } - dbg(KEY_ENV " key is true"); + dbg("all %i " KEY_ENV " keys matched", rule->env_pair_count); } /* walk up the chain of physical devices and find a match */ @@ -672,18 +678,8 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule, dbg("device has no sysfs_device"); goto exit; } - dbg("check for " KEY_DRIVER " rule->driver='%s' sysfs_device->driver_name='%s'", - rule->driver, parent_device->driver_name); - if (strcmp_pattern(rule->driver, parent_device->driver_name) != 0) { - dbg(KEY_DRIVER " is not matching"); - if (rule->driver_operation != KEY_OP_NOMATCH) - goto try_parent; - } else { - dbg(KEY_DRIVER " matches"); - if (rule->driver_operation == KEY_OP_NOMATCH) - goto try_parent; - } - dbg(KEY_DRIVER " key is true"); + if (match_key(KEY_BUS, rule->driver, rule->driver_operation, parent_device->driver_name)) + goto try_parent; } /* check for matching bus value */ @@ -692,18 +688,8 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule, dbg("device has no sysfs_device"); goto exit; } - dbg("check for " KEY_BUS " rule->bus='%s' sysfs_device->bus='%s'", - rule->bus, parent_device->bus); - if (strcmp_pattern(rule->bus, parent_device->bus) != 0) { - dbg(KEY_BUS " is not matching"); - if (rule->bus_operation != KEY_OP_NOMATCH) - goto try_parent; - } else { - dbg(KEY_BUS " matches"); - if (rule->bus_operation == KEY_OP_NOMATCH) - goto try_parent; - } - dbg(KEY_BUS " key is true"); + if (match_key(KEY_BUS, rule->bus, rule->bus_operation, parent_device->bus)) + goto try_parent; } /* check for matching bus id */ @@ -712,17 +698,8 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule, dbg("device has no sysfs_device"); goto exit; } - dbg("check " KEY_ID); - if (strcmp_pattern(rule->id, parent_device->bus_id) != 0) { - dbg(KEY_ID " is not matching"); - if (rule->id_operation != KEY_OP_NOMATCH) - goto try_parent; - } else { - dbg(KEY_ID " matches"); - if (rule->id_operation == KEY_OP_NOMATCH) - goto try_parent; - } - dbg(KEY_ID " key is true"); + if (match_key(KEY_ID, rule->id, rule->id_operation, parent_device->bus_id)) + goto try_parent; } /* check for matching sysfs pairs */ @@ -745,21 +722,14 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule, len = strlen(value); while (len > 0 && isspace(value[len-1])) value[--len] = '\0'; - dbg("removed %i trailing whitespace chars from '%s'", strlen(value)-len, value); + dbg("removed %zi trailing whitespace chars from '%s'", strlen(value)-len, value); } - dbg("compare attribute '%s' value '%s' with '%s'", pair->name, value, pair->value); - if (strcmp_pattern(pair->value, value) != 0) { - dbg(KEY_SYSFS "{'%s'} is not matching", pair->name); - if (pair->operation != KEY_OP_NOMATCH) - goto try_parent; - } else { - dbg(KEY_SYSFS "{'%s'} matches", pair->name); - if (pair->operation == KEY_OP_NOMATCH) - goto try_parent; - } + dbg("check %i " KEY_SYSFS " keys", rule->sysfs_pair_count); + if (match_key(pair->name, pair->value, pair->operation, value)) + goto try_parent; } - dbg(KEY_SYSFS " keys are true"); + dbg("all %i " KEY_SYSFS " keys matched", rule->sysfs_pair_count); } /* found matching physical device */ @@ -773,19 +743,48 @@ try_parent: dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id); } + /* import variables from file into environment */ + if (rule->import_operation != KEY_OP_UNSET) { + char import[PATH_SIZE]; + int rc = -1; + + strlcpy(import, rule->import, sizeof(import)); + apply_format(udev, import, sizeof(import), class_dev, sysfs_device); + dbg("check for " KEY_IMPORT " import='%s'", import); + if (rule->import_exec) { + dbg("run executable file import='%s'", import); + rc = import_program_into_env(udev, import); + } else { + dbg("import file import='%s'", import); + rc = import_file_into_env(udev, import); + } + if (rc) { + dbg(KEY_IMPORT " failed"); + if (rule->import_operation != KEY_OP_NOMATCH) + goto exit; + } else + dbg(KEY_IMPORT " '%s' imported", rule->import); + dbg(KEY_IMPORT " key is true"); + } + /* execute external program */ if (rule->program_operation != KEY_OP_UNSET) { char program[PATH_SIZE]; + char result[PATH_SIZE]; - dbg("check " KEY_PROGRAM); strlcpy(program, rule->program, sizeof(program)); apply_format(udev, program, sizeof(program), class_dev, sysfs_device); - if (execute_program_pipe(program, udev->subsystem, - udev->program_result, sizeof(udev->program_result)) != 0) { - dbg(KEY_PROGRAM " returned nonzero"); + dbg("check for " KEY_PROGRAM " program='%s", program); + if (execute_program(program, udev->subsystem, result, sizeof(result), NULL) != 0) { + dbg(KEY_PROGRAM " is not matching"); if (rule->program_operation != KEY_OP_NOMATCH) goto exit; } else { + dbg(KEY_PROGRAM " matches"); + remove_trailing_char(result, '\n'); + replace_untrusted_chars(result); + dbg("result is '%s'", result); + strlcpy(udev->program_result, result, sizeof(udev->program_result)); dbg(KEY_PROGRAM " returned successful"); if (rule->program_operation == KEY_OP_NOMATCH) goto exit; @@ -794,20 +793,8 @@ try_parent: } /* check for matching result of external program */ - if (rule->result_operation != KEY_OP_UNSET) { - dbg("check for " KEY_RESULT " rule->result='%s', udev->program_result='%s'", - rule->result, udev->program_result); - if (strcmp_pattern(rule->result, udev->program_result) != 0) { - dbg(KEY_RESULT " is not matching"); - if (rule->result_operation != KEY_OP_NOMATCH) - goto exit; - } else { - dbg(KEY_RESULT " matches"); - if (rule->result_operation == KEY_OP_NOMATCH) - goto exit; - } - dbg(KEY_RESULT " key is true"); - } + if (match_key(KEY_RESULT, rule->result, rule->result_operation, udev->program_result)) + goto exit; /* rule matches */ return 0; @@ -846,7 +833,12 @@ 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 */ - list_for_each_entry(rule, &udev_rule_list, node) { + udev_rules_iter_init(); + while (1) { + rule = udev_rules_iter_next(); + if (rule == NULL) + break; + if (udev->name_set && rule->name_operation != KEY_OP_UNSET) { dbg("node name already set, rule ignored"); continue; @@ -1000,12 +992,13 @@ int udev_rules_get_run(struct udevice *udev, struct sysfs_device *sysfs_device) struct udev_rule *rule; /* look for a matching rule to apply */ - list_for_each_entry(rule, &udev_rule_list, node) { - dbg("process rule"); - - if (rule->run_operation == KEY_OP_UNSET) - continue; + udev_rules_iter_init(); + while (1) { + rule = udev_rules_iter_next(); + 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') { dbg("skip rule that names a device");