]> err.no Git - systemd/commitdiff
[PATCH] allow to match against empty key values
authorkay.sievers@vrfy.org <kay.sievers@vrfy.org>
Mon, 28 Mar 2005 10:20:05 +0000 (12:20 +0200)
committerGreg KH <gregkh@suse.de>
Wed, 27 Apr 2005 06:55:00 +0000 (23:55 -0700)
test/udev-test.pl
udev_rules.c
udev_rules.h
udev_rules_parse.c

index ed94629dd1a5437d9b6c413d5f145c215eb32ca6..27ca37115c721de7353f6cb662bf3984571a11d0 100644 (file)
@@ -806,7 +806,7 @@ EOF
                exp_name        => "link",
                exp_target      => "link",
                exp_rem_error   => "yes",
-               option          => "clear",
+               option          => "clean",
                rules           => <<EOF
 KERNEL=="tty0", NAME="link", SYMLINK="link"
 EOF
@@ -1006,7 +1006,7 @@ EOF
                subsys          => "block",
                devpath         => "/block/sda/sda4",
                exp_name        => "cdrom2",
-               option          => "clear",
+               option          => "clean",
                rules           => <<EOF
 KERNEL=="sda4", NAME="cdrom%e"
 EOF
@@ -1067,7 +1067,7 @@ EOF
                devpath         => "/block/sda",
                exp_name        => "node14",
                exp_rem_error   => "yes",
-               option          => "clear",
+               option          => "clean",
                rules           => <<EOF
 BUS=="scsi", KERNEL=="sda", NAME="node", OPTIONS="ignore_remove, all_partitions"
 EOF
@@ -1231,6 +1231,18 @@ EOF
                exp_name        => "serial-0000:00:09.0",
                rules           => <<EOF
 KERNEL=="ttyUSB*", NAME="serial-%s{serial}"
+EOF
+       },
+       {
+               desc            => "match against empty key string",
+               subsys          => "block",
+               devpath         => "/block/sda",
+               exp_name        => "ok",
+               rules           => <<EOF
+KERNEL=="sda", SYSFS{nothing}!="", NAME="not-1-ok"
+KERNEL=="sda", SYSFS{nothing}=="", NAME="not-2-ok"
+KERNEL=="sda", SYSFS{vendor}!="", NAME="ok"
+KERNEL=="sda", SYSFS{vendor}=="", NAME="not-3-ok"
 EOF
        },
 );
@@ -1415,7 +1427,7 @@ sub run_test {
 
        print "\n";
 
-       if (defined($rules->{option}) && $rules->{option} eq "clear") {
+       if (defined($rules->{option}) && $rules->{option} eq "clean") {
                system("rm -rf $udev_db");
                system("rm -rf $udev_root");
                mkdir($udev_root) || die "unable to create udev_root: $udev_root\n";
index 00199a062fd844a137baeb4486f1b3bca814b5f8..b7905477de8b8dc3531b9dcd24a83ed343bd485b 100644 (file)
@@ -463,36 +463,12 @@ static int execute_program(struct udevice *udev, const char *path, char *value,
        return retval;
 }
 
-static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct key_pair *pair)
-{
-       char value[VALUE_SIZE];
-       int i;
-
-       if (find_sysfs_attribute(class_dev, sysfs_device, pair->name, value, sizeof(value)) != 0)
-               return -1;
-
-       /* strip trailing whitespace of value, if not asked to match for it */
-       if (!isspace(pair->value[strlen(pair->value)-1])) {
-               i = strlen(value);
-               while (i > 0 && isspace(value[i-1]))
-                       value[--i] = '\0';
-               dbg("removed %i trailing whitespace chars from '%s'", strlen(value)-i, value);
-       }
-
-       dbg("compare attribute '%s' value '%s' with '%s'", pair->name, value, pair->value);
-       if (strcmp_pattern(pair->value, value) != 0)
-               return -1;
-
-       dbg("found matching attribute '%s' with value '%s'", pair->name, pair->value);
-       return 0;
-}
-
 static int match_rule(struct udevice *udev, struct udev_rule *rule,
                      struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
 {
        struct sysfs_device *parent_device = sysfs_device;
 
-       if (rule->kernel[0] != '\0') {
+       if (rule->kernel_operation != KEY_OP_UNSET) {
                dbg("check for " KEY_KERNEL " rule->kernel='%s' class_dev->name='%s'",
                    rule->kernel, class_dev->name);
                if (strcmp_pattern(rule->kernel, class_dev->name) != 0) {
@@ -507,7 +483,7 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                dbg(KEY_KERNEL " key is true");
        }
 
-       if (rule->subsystem[0] != '\0') {
+       if (rule->subsystem_operation != KEY_OP_UNSET) {
                dbg("check for " KEY_SUBSYSTEM " rule->subsystem='%s' class_dev->name='%s'",
                    rule->subsystem, class_dev->name);
                if (strcmp_pattern(rule->subsystem, udev->subsystem) != 0) {
@@ -552,7 +528,7 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
        /* walk up the chain of physical devices and find a match */
        while (1) {
                /* check for matching driver */
-               if (rule->driver[0] != '\0') {
+               if (rule->driver_operation != KEY_OP_UNSET) {
                        if (parent_device == NULL) {
                                dbg("device has no sysfs_device");
                                goto exit;
@@ -572,7 +548,7 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                }
 
                /* check for matching bus value */
-               if (rule->bus[0] != '\0') {
+               if (rule->bus_operation != KEY_OP_UNSET) {
                        if (parent_device == NULL) {
                                dbg("device has no sysfs_device");
                                goto exit;
@@ -592,7 +568,7 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                }
 
                /* check for matching bus id */
-               if (rule->id[0] != '\0') {
+               if (rule->id_operation != KEY_OP_UNSET) {
                        if (parent_device == NULL) {
                                dbg("device has no sysfs_device");
                                goto exit;
@@ -617,9 +593,23 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule,
                        dbg("check " KEY_SYSFS " pairs");
                        for (i = 0; i < rule->sysfs_pair_count; i++) {
                                struct key_pair *pair;
+                               char value[VALUE_SIZE];
 
                                pair = &rule->sysfs_pair[i];
-                               if (compare_sysfs_attribute(class_dev, parent_device, pair) != 0) {
+                               if (find_sysfs_attribute(class_dev, parent_device, pair->name, value, sizeof(value)) != 0)
+                                       goto try_parent;
+
+                               /* strip trailing whitespace of value, if not asked to match for it */
+                               if (!isspace(pair->value[strlen(pair->value)-1])) {
+                                       size_t 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("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;
@@ -644,7 +634,7 @@ try_parent:
        }
 
        /* execute external program */
-       if (rule->program[0] != '\0') {
+       if (rule->program_operation != KEY_OP_UNSET) {
                char program[PATH_SIZE];
 
                dbg("check " KEY_PROGRAM);
@@ -663,7 +653,7 @@ try_parent:
        }
 
        /* check for matching result of external program */
-       if (rule->result[0] != '\0') {
+       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) {
index 423a6f641ecb1f24632c4c5eeeecc595caf0e44a..561ba1687d370d320ff9501bb6be7439c81e454e 100644 (file)
@@ -55,7 +55,7 @@
 #define RULEFILE_SUFFIX                ".rules"
 
 enum key_operation {
-       KEY_OP_UNKNOWN,
+       KEY_OP_UNSET,
        KEY_OP_MATCH,
        KEY_OP_NOMATCH,
        KEY_OP_ADD,
index 71ca4827288daca318dc9ba81ca5554ea93c0385..3c1631ab49c54590bd1cf86fb0e75d1d6ca127f7 100644 (file)
@@ -236,7 +236,7 @@ static int rules_parse(const char *filename)
                while (1) {
                        char *key;
                        char *value;
-                       enum key_operation operation = KEY_OP_UNKNOWN;
+                       enum key_operation operation = KEY_OP_UNSET;
 
                        retval = get_key(&linepos, &key, &operation, &value);
                        if (retval)
@@ -408,13 +408,6 @@ static int rules_parse(const char *filename)
                if (!valid)
                        goto error;
 
-               /* simple plausibility checks for given keys */
-               if ((rule.sysfs_pair[0].name[0] == '\0') ^
-                   (rule.sysfs_pair[0].value[0] == '\0')) {
-                       err("inconsistency in " KEY_SYSFS " key");
-                       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");
                        goto error;