X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=udev_rules.c;h=9606f04d3df64f88b0eb2cbce40d87379ce32ac8;hp=6ed67c2672e86e63694a24e1417211ceb0f7b645;hb=be0856c841e248c491e0fdea013a7af0f61a76b1;hpb=bd0ed2ffbec325512c3131ce0db896234e36c923 diff --git a/udev_rules.c b/udev_rules.c index 6ed67c267..9606f04d3 100644 --- a/udev_rules.c +++ b/udev_rules.c @@ -174,21 +174,26 @@ static int get_key(char **line, char **key, char **value) linepos++; /* get the value*/ - if (linepos[0] == '\0') - return -1; - if (linepos[0] == '"') { linepos++; temp = strchr(linepos, '"'); - if (!temp) + 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) + 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])) @@ -200,26 +205,18 @@ static int get_key(char **line, char **key, char **value) return 0; } -static int import_file_into_env(const char *filename) +static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize) { char line[LINE_SIZE]; - char *bufline; + const char *bufline; char *linepos; char *variable; char *value; - char *buf; - size_t bufsize; size_t cur; size_t count; int lineno; - int retval = 0; - if (file_map(filename, &buf, &bufsize) != 0) { - err("can't open '%s'", filename); - return -1; - } - - /* loop through the whole file */ + /* loop through the whole buffer */ lineno = 0; cur = 0; while (cur < bufsize) { @@ -249,20 +246,42 @@ static int import_file_into_env(const char *filename) linepos = line; if (get_key(&linepos, &variable, &value) == 0) { - dbg("import %s=%s", variable, value); - setenv(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 retval; + + return 0; } -/** 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 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) { @@ -576,117 +595,10 @@ 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) -{ - 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; - - 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]); - } - - retval = pipe(pipefds); - if (retval != 0) { - err("pipe failed"); - return -1; - } - - 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 (!retval) { - remove_trailing_char(value, '\n'); - dbg("result is '%s'", value); - replace_untrusted_chars(value); - } else - value[0] = '\0'; - - return retval; -} - static int match_rule(struct udevice *udev, struct udev_rule *rule, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) { @@ -881,7 +793,7 @@ 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); @@ -909,34 +821,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 (import_file_into_env(import) == 0) { - dbg(KEY_IMPORT " file '%s' imported", rule->import); - if (rule->import_operation == KEY_OP_NOMATCH) + 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 - goto exit; + 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]; strlcpy(program, rule->program, sizeof(program)); apply_format(udev, program, sizeof(program), class_dev, sysfs_device); dbg("check for " KEY_PROGRAM " program='%s", program); - if (execute_program_pipe(program, udev->subsystem, - udev->program_result, sizeof(udev->program_result)) != 0) { - dbg(KEY_PROGRAM " returned nonzero"); + 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;