From bd0ed2ffbec325512c3131ce0db896234e36c923 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Sat, 25 Jun 2005 13:10:16 +0200 Subject: [PATCH] IMPORT= allow to import a shell-var style config-file This allows to source-in a file into the udev environment to have the defined keys available for later processing by udev itself or the forked helper programs. Signed-off-by: Kay Sievers --- test/udev-test.pl | 19 ++++++ udev_rules.c | 155 ++++++++++++++++++++++++++++++++++++++++++++- udev_rules.h | 3 + udev_rules_parse.c | 7 ++ 4 files changed, 182 insertions(+), 2 deletions(-) diff --git a/test/udev-test.pl b/test/udev-test.pl index 92253521..c7a99e51 100755 --- a/test/udev-test.pl +++ b/test/udev-test.pl @@ -256,6 +256,16 @@ EOF exp_name => "M8-m3-n3-b0:0-sIBM" , rules => < "import of shellvalue file", + subsys => "block", + devpath => "/block/sda", + exp_name => "subdir/sys/node" , + rules => < "env substitution", + subsys => "block", + devpath => "/block/sda", + exp_name => "node-add-me", + rules => <= 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); + setenv(variable, value, 0); + } + } + + file_unmap(buf, bufsize); + return retval; +} + /** Finds the lowest positive N such that N isn't present in * $(udevroot) either as a file or a symlink. * @@ -198,7 +321,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 +341,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 +361,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 +556,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; @@ -773,13 +909,28 @@ try_parent: dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id); } + if (rule->import_operation != KEY_OP_UNSET) { + char import[PATH_SIZE]; + + 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) + goto exit; + } else + goto exit; + dbg(KEY_IMPORT " key is true"); + } + /* execute external program */ if (rule->program_operation != KEY_OP_UNSET) { char program[PATH_SIZE]; - dbg("check " KEY_PROGRAM); 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"); diff --git a/udev_rules.h b/udev_rules.h index cb628398..f4b0a6f1 100644 --- a/udev_rules.h +++ b/udev_rules.h @@ -40,6 +40,7 @@ #define KEY_SYSFS "SYSFS" #define KEY_ENV "ENV" #define KEY_MODALIAS "MODALIAS" +#define KEY_IMPORT "IMPORT" #define KEY_NAME "NAME" #define KEY_SYMLINK "SYMLINK" #define KEY_OWNER "OWNER" @@ -100,6 +101,8 @@ struct udev_rule { int env_pair_count; enum key_operation modalias_operation; char modalias[PATH_SIZE]; + enum key_operation import_operation; + char import[PATH_SIZE]; char name[PATH_SIZE]; enum key_operation name_operation; diff --git a/udev_rules_parse.c b/udev_rules_parse.c index 36bf9717..12123682 100644 --- a/udev_rules_parse.c +++ b/udev_rules_parse.c @@ -375,6 +375,13 @@ static int rules_parse(const char *filename) continue; } + if (strcasecmp(key, KEY_IMPORT) == 0) { + strlcpy(rule.import, value, sizeof(rule.import)); + rule.import_operation = operation; + valid = 1; + continue; + } + if (strcasecmp(key, KEY_DRIVER) == 0) { strlcpy(rule.driver, value, sizeof(rule.driver)); rule.driver_operation = operation; -- 2.39.5