From: Kay Sievers Date: Sat, 2 Jun 2007 22:01:46 +0000 (+0200) Subject: add TEST=="" key X-Git-Tag: 174~1917 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=953249a3a01cbd442b42558168da6c76a92a0e40;p=systemd add TEST=="" key --- diff --git a/udev.7 b/udev.7 index 4fbaf924..a2535815 100644 --- a/udev.7 +++ b/udev.7 @@ -151,6 +151,11 @@ Match against the value of an environment variable. Up to five keys can be specified per rule. Depending on the type of operator, this key is also used to export a variable to the environment. .RE .PP +\fBTEST{\fR\fB\fIoctal mode mask\fR\fR\fB}\fR +.RS 4 +Test the existence of a file. An octal mode mask can be specified if needed. +.RE +.PP \fBPROGRAM\fR .RS 4 Execute external program. The key is true, if the program returns with exit code zero. The whole event environment is available to the executed program. The program's output printed to stdout, is available in the RESULT key. diff --git a/udev.h b/udev.h index 94d054f7..64510ed3 100644 --- a/udev.h +++ b/udev.h @@ -119,6 +119,7 @@ extern struct sysfs_device *sysfs_device_get_parent(struct sysfs_device *dev); extern struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct sysfs_device *dev, const char *subsystem); extern char *sysfs_attr_get_value(const char *devpath, const char *attr_name); extern int sysfs_resolve_link(char *path, size_t size); +extern int sysfs_lookup_devpath_by_subsys_id(char *devpath, size_t len, const char *subsystem, const char *id); /* udev_node.c */ extern int udev_node_mknod(struct udevice *udev, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid); diff --git a/udev.xml b/udev.xml index 1f9e6d91..a3658bd5 100644 --- a/udev.xml +++ b/udev.xml @@ -224,6 +224,14 @@ + + + + Test the existence of a file. An octal mode mask can be specified + if needed. + + + diff --git a/udev_node.c b/udev_node.c index b1bbda83..338948fc 100644 --- a/udev_node.c +++ b/udev_node.c @@ -322,7 +322,7 @@ int udev_node_add(struct udevice *udev) /* take the maximum registered minor range */ attr = sysfs_attr_get_value(udev->dev->devpath, "range"); - if (attr) { + if (attr != NULL) { range = atoi(attr); if (range > 1) udev->partitions = range-1; diff --git a/udev_rules.c b/udev_rules.c index e61c9a69..5ded26d0 100644 --- a/udev_rules.c +++ b/udev_rules.c @@ -641,6 +641,29 @@ static int match_rule(struct udevice *udev, struct udev_rule *rule) } } + if (rule->test.operation != KEY_OP_UNSET) { + char filename[PATH_SIZE]; + struct stat statbuf; + int match; + + strlcpy(filename, key_val(rule, &rule->test), sizeof(filename)); + udev_rules_apply_format(udev, filename, sizeof(filename)); + + match = (stat(filename, &statbuf) == 0); + info("'%s' %s", filename, match ? "exists" : "does not exist"); + if (match && rule->test_mode_mask > 0) { + match = ((statbuf.st_mode & rule->test_mode_mask) > 0); + info("'%s' has mode=%#o and %s %#o", filename, statbuf.st_mode, + match ? "matches" : "does not match", + rule->test_mode_mask); + } + if (match && rule->test.operation == KEY_OP_NOMATCH) + goto nomatch; + if (!match && rule->test.operation == KEY_OP_MATCH) + goto nomatch; + dbg("TEST key is true"); + } + if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) { int found; @@ -1037,7 +1060,8 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev) dbg("process rule"); if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET || - rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) { + rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || + rule->group.operation != KEY_OP_UNSET) { dbg("skip rule that names a device"); continue; } @@ -1050,7 +1074,8 @@ int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev) } if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) { - if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) { + if (rule->run.operation == KEY_OP_ASSIGN || + rule->run.operation == KEY_OP_ASSIGN_FINAL) { info("reset run list"); name_list_cleanup(&udev->run_list); } diff --git a/udev_rules.h b/udev_rules.h index 7fbf88ba..0e1ff76b 100644 --- a/udev_rules.h +++ b/udev_rules.h @@ -75,6 +75,8 @@ struct udev_rule { struct key result; struct key import; enum import_type import_type; + struct key test; + mode_t test_mode_mask; struct key run; struct key wait_for_sysfs; struct key label; diff --git a/udev_rules_parse.c b/udev_rules_parse.c index 91669804..44206169 100644 --- a/udev_rules_parse.c +++ b/udev_rules_parse.c @@ -470,6 +470,15 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena continue; } + if (strncasecmp(key, "TEST", sizeof("TEST")-1) == 0) { + attr = get_key_attribute(key + sizeof("TEST")-1); + if (attr != NULL) + rule->test_mode_mask = strtol(attr, NULL, 8); + add_rule_key(rule, &rule->test, operation, value); + valid = 1; + continue; + } + if (strcasecmp(key, "RUN") == 0) { add_rule_key(rule, &rule->run, operation, value); valid = 1;