From: kay.sievers@vrfy.org Date: Sun, 13 Mar 2005 10:40:32 +0000 (+0100) Subject: [PATCH] add ENV{} key to match agains environment variables X-Git-Tag: 055~11 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=3e5958dee5f24283eb5c6a2d2d95e1a39428a3b8 [PATCH] add ENV{} key to match agains environment variables --- diff --git a/test/udev-test.pl b/test/udev-test.pl index ebceccecb..2ece43173 100644 --- a/test/udev-test.pl +++ b/test/udev-test.pl @@ -256,15 +256,6 @@ EOF exp_name => "M8-m3-n3-b0:0-sIBM" , conf => < "old style SYSFS_ attribute", - subsys => "block", - devpath => "/block/sda", - exp_name => "good" , - conf => < < "ENV{} test", + subsys => "block", + devpath => "/block/sda/sda1", + exp_name => "true", + conf => < "ENV{} test", + subsys => "block", + devpath => "/block/sda/sda1", + exp_name => "true", + conf => <sysfs_pair_count; i++) { - struct key_pair *pair; - - pair = &rule->sysfs_pair[i]; - if ((pair->name[0] == '\0') || (pair->value[0] == '\0')) - break; - if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) { - dbg("sysfs pair #%u does not match", i); - if (pair->operation != KEY_OP_NOMATCH) - return -1; - } else { - dbg("sysfs pair #%u matches", i); - if (pair->operation == KEY_OP_NOMATCH) - return -1; - } - } - - return 0; -} - static int match_id(struct udev_rule *rule, struct sysfs_device *sysfs_device) { char path[PATH_SIZE]; @@ -570,6 +546,33 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule, dbg(KEY_SUBSYSTEM " key is true"); } + if (rule->env_pair_count) { + int i; + + dbg("check for " KEY_ENV " pairs"); + for (i = 0; i < rule->env_pair_count; i++) { + struct key_pair *pair; + const char *value; + + pair = &rule->env_pair[i]; + value = getenv(pair->name); + if (!value) { + 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(KEY_ENV " key is true"); + } + /* walk up the chain of physical devices and find a match */ while (1) { /* check for matching driver */ @@ -632,11 +635,23 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule, } /* check for matching sysfs pairs */ - if (rule->sysfs_pair[0].name[0] != '\0') { + if (rule->sysfs_pair_count) { + int i; + dbg("check " KEY_SYSFS " pairs"); - if (match_sysfs_pairs(rule, class_dev, sysfs_device) != 0) { - dbg(KEY_SYSFS " is not matching"); - goto try_parent; + for (i = 0; i < rule->sysfs_pair_count; i++) { + struct key_pair *pair; + + pair = &rule->sysfs_pair[i]; + if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 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(KEY_SYSFS " keys are true"); } diff --git a/udev_rules.h b/udev_rules.h index 94a5d9d5f..423a6f641 100644 --- a/udev_rules.h +++ b/udev_rules.h @@ -31,11 +31,12 @@ #define KEY_KERNEL "KERNEL" #define KEY_SUBSYSTEM "SUBSYSTEM" #define KEY_BUS "BUS" -#define KEY_SYSFS "SYSFS" #define KEY_ID "ID" #define KEY_PROGRAM "PROGRAM" #define KEY_RESULT "RESULT" #define KEY_DRIVER "DRIVER" +#define KEY_SYSFS "SYSFS" +#define KEY_ENV "ENV" #define KEY_NAME "NAME" #define KEY_SYMLINK "SYMLINK" #define KEY_OWNER "OWNER" @@ -49,6 +50,7 @@ #define OPTION_PARTITIONS "all_partitions" #define KEY_SYSFS_PAIRS_MAX 5 +#define KEY_ENV_PAIRS_MAX 5 #define RULEFILE_SUFFIX ".rules" @@ -85,6 +87,8 @@ struct udev_rule { 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 name[PATH_SIZE]; char symlink[PATH_SIZE]; diff --git a/udev_rules_parse.c b/udev_rules_parse.c index c4c684d05..face496ab 100644 --- a/udev_rules_parse.c +++ b/udev_rules_parse.c @@ -278,16 +278,36 @@ static int rules_parse(struct udevice *udev, const char *filename) goto error; } pair = &rule.sysfs_pair[rule.sysfs_pair_count]; - rule.sysfs_pair_count++; - attr = get_key_attribute(key + sizeof(KEY_SYSFS)-1); if (attr == NULL) { dbg("error parsing " KEY_SYSFS " attribute"); + goto error; + } + strlcpy(pair->name, attr, sizeof(pair->name)); + strlcpy(pair->value, value, sizeof(pair->value)); + pair->operation = operation; + rule.sysfs_pair_count++; + valid = 1; + continue; + } + + if (strncasecmp(key, KEY_ENV, sizeof(KEY_ENV)-1) == 0) { + struct key_pair *pair; + + if (rule.env_pair_count >= KEY_ENV_PAIRS_MAX) { + dbg("skip rule, to many " KEY_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); + if (attr == NULL) { + dbg("error parsing " KEY_ENV " attribute"); 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; }