diff options
author | Kay Sievers <kay.sievers@vrfy.org> | 2008-10-18 19:19:56 +0200 |
---|---|---|
committer | Kay Sievers <kay.sievers@vrfy.org> | 2008-10-18 19:19:56 +0200 |
commit | f1128767cbc805461a8f2e54810146c562d54524 (patch) | |
tree | cc6ea8da709eafb0c6f89e66378fb237f6c1b6d9 | |
parent | 2d73813ebc5a9b507655ec629ba8b40e4fbf28a8 (diff) |
move udev_rules_apply_format() to udev-event.c
-rw-r--r-- | udev/udev-event.c | 433 | ||||
-rw-r--r-- | udev/udev-rules.c | 495 | ||||
-rw-r--r-- | udev/udev.h | 22 | ||||
-rw-r--r-- | udev/udevadm-test.c | 2 |
4 files changed, 461 insertions, 491 deletions
diff --git a/udev/udev-event.c b/udev/udev-event.c index b7c3ff142a..59ae7c5378 100644 --- a/udev/udev-event.c +++ b/udev/udev-event.c @@ -57,7 +57,432 @@ void udev_event_unref(struct udev_event *event) free(event); } -static void kernel_log(struct ifreq ifr) +/* extract possible {attr} and move str behind it */ +static char *get_format_attribute(struct udev *udev, char **str) +{ + char *pos; + char *attr = NULL; + + if (*str[0] == '{') { + pos = strchr(*str, '}'); + if (pos == NULL) { + err(udev, "missing closing brace for format\n"); + return NULL; + } + pos[0] = '\0'; + attr = *str+1; + *str = pos+1; + dbg(udev, "attribute='%s', str='%s'\n", attr, *str); + } + return attr; +} + +/* extract possible format length and move str behind it*/ +static int get_format_len(struct udev *udev, char **str) +{ + int num; + char *tail; + + if (isdigit(*str[0])) { + num = (int) strtoul(*str, &tail, 10); + if (num > 0) { + *str = tail; + dbg(udev, "format length=%i\n", num); + return num; + } else { + err(udev, "format parsing error '%s'\n", *str); + } + } + return -1; +} + +/* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */ +int udev_event_apply_subsys_kernel(struct udev_event *udev_event, const char *string, + char *result, size_t maxsize, int read_value) +{ + char temp[UTIL_PATH_SIZE]; + char *subsys; + char *sysname; + char *attr; + struct udev_device *dev; + + if (string == NULL) + string = result; + if (string[0] != '[') + return -1; + + util_strlcpy(temp, string, sizeof(temp)); + + subsys = &temp[1]; + + sysname = strchr(subsys, '/'); + if (sysname == NULL) + return -1; + sysname[0] = '\0'; + sysname = &sysname[1]; + + attr = strchr(sysname, ']'); + if (attr == NULL) + return -1; + attr[0] = '\0'; + attr = &attr[1]; + if (attr[0] == '/') + attr = &attr[1]; + if (attr[0] == '\0') + attr = NULL; + + if (read_value && attr == NULL) + return -1; + + dev = udev_device_new_from_subsystem_sysname(udev_event->udev, subsys, sysname); + if (dev == NULL) + return -1; + + if (read_value) { + const char *val; + + val = udev_device_get_attr_value(dev, attr); + if (val != NULL) + util_strlcpy(result, val, maxsize); + else + result[0] = '\0'; + info(udev_event->udev, "value '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result); + } else { + util_strlcpy(result, udev_device_get_syspath(dev), maxsize); + if (attr != NULL) { + util_strlcat(result, "/", maxsize); + util_strlcat(result, attr, maxsize); + } + info(udev_event->udev, "path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result); + } + udev_device_unref(dev); + return 0; +} + +void udev_event_apply_format(struct udev_event *event, char *string, size_t maxsize) +{ + struct udev_device *dev = event->dev; + char temp[UTIL_PATH_SIZE]; + char temp2[UTIL_PATH_SIZE]; + char *head, *tail, *cpos, *attr, *rest; + int len; + int i; + int count; + enum subst_type { + SUBST_UNKNOWN, + SUBST_DEVPATH, + SUBST_KERNEL, + SUBST_KERNEL_NUMBER, + SUBST_ID, + SUBST_DRIVER, + SUBST_MAJOR, + SUBST_MINOR, + SUBST_RESULT, + SUBST_ATTR, + SUBST_PARENT, + SUBST_TEMP_NODE, + SUBST_NAME, + SUBST_LINKS, + SUBST_ROOT, + SUBST_SYS, + SUBST_ENV, + }; + static const struct subst_map { + char *name; + char fmt; + enum subst_type type; + } map[] = { + { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH }, + { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER }, + { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL }, + { .name = "id", .fmt = 'b', .type = SUBST_ID }, + { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER }, + { .name = "major", .fmt = 'M', .type = SUBST_MAJOR }, + { .name = "minor", .fmt = 'm', .type = SUBST_MINOR }, + { .name = "result", .fmt = 'c', .type = SUBST_RESULT }, + { .name = "attr", .fmt = 's', .type = SUBST_ATTR }, + { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR }, + { .name = "parent", .fmt = 'P', .type = SUBST_PARENT }, + { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE }, + { .name = "name", .fmt = 'D', .type = SUBST_NAME }, + { .name = "links", .fmt = 'L', .type = SUBST_LINKS }, + { .name = "root", .fmt = 'r', .type = SUBST_ROOT }, + { .name = "sys", .fmt = 'S', .type = SUBST_SYS }, + { .name = "env", .fmt = 'E', .type = SUBST_ENV }, + { NULL, '\0', 0 } + }; + enum subst_type type; + const struct subst_map *subst; + + head = string; + while (1) { + len = -1; + while (head[0] != '\0') { + if (head[0] == '$') { + /* substitute named variable */ + if (head[1] == '\0') + break; + if (head[1] == '$') { + util_strlcpy(temp, head+2, sizeof(temp)); + util_strlcpy(head+1, temp, maxsize); + head++; + continue; + } + head[0] = '\0'; + for (subst = map; subst->name; subst++) { + if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) { + type = subst->type; + tail = head + strlen(subst->name)+1; + dbg(event->udev, "will substitute format name '%s'\n", subst->name); + goto found; + } + } + head[0] = '$'; + err(event->udev, "unknown format variable '%s'\n", head); + } else if (head[0] == '%') { + /* substitute format char */ + if (head[1] == '\0') + break; + if (head[1] == '%') { + util_strlcpy(temp, head+2, sizeof(temp)); + util_strlcpy(head+1, temp, maxsize); + head++; + continue; + } + head[0] = '\0'; + tail = head+1; + len = get_format_len(event->udev, &tail); + for (subst = map; subst->name; subst++) { + if (tail[0] == subst->fmt) { + type = subst->type; + tail++; + dbg(event->udev, "will substitute format char '%c'\n", subst->fmt); + goto found; + } + } + head[0] = '%'; + err(event->udev, "unknown format char '%c'\n", tail[0]); + } + head++; + } + break; +found: + attr = get_format_attribute(event->udev, &tail); + util_strlcpy(temp, tail, sizeof(temp)); + dbg(event->udev, "format=%i, string='%s', tail='%s'\n", type ,string, tail); + + switch (type) { + case SUBST_DEVPATH: + util_strlcat(string, udev_device_get_devpath(dev), maxsize); + dbg(event->udev, "substitute devpath '%s'\n", udev_device_get_devpath(dev)); + break; + case SUBST_KERNEL: + util_strlcat(string, udev_device_get_sysname(dev), maxsize); + dbg(event->udev, "substitute kernel name '%s'\n", udev_device_get_sysname(dev)); + break; + case SUBST_KERNEL_NUMBER: + if (udev_device_get_sysnum(dev) == NULL) + break; + util_strlcat(string, udev_device_get_sysnum(dev), maxsize); + dbg(event->udev, "substitute kernel number '%s'\n", udev_device_get_sysnum(dev)); + break; + case SUBST_ID: + if (event->dev_parent != NULL) { + util_strlcat(string, udev_device_get_sysname(event->dev_parent), maxsize); + dbg(event->udev, "substitute id '%s'\n", udev_device_get_sysname(event->dev_parent)); + } + break; + case SUBST_DRIVER: + if (event->dev_parent != NULL) { + const char *driver = udev_device_get_driver(event->dev_parent); + + if (driver == NULL) + break; + util_strlcat(string, driver, maxsize); + dbg(event->udev, "substitute driver '%s'\n", driver); + } + break; + case SUBST_MAJOR: + sprintf(temp2, "%d", major(udev_device_get_devnum(dev))); + util_strlcat(string, temp2, maxsize); + dbg(event->udev, "substitute major number '%s'\n", temp2); + break; + case SUBST_MINOR: + sprintf(temp2, "%d", minor(udev_device_get_devnum(dev))); + util_strlcat(string, temp2, maxsize); + dbg(event->udev, "substitute minor number '%s'\n", temp2); + break; + case SUBST_RESULT: + if (event->program_result[0] == '\0') + break; + /* get part part of the result string */ + i = 0; + if (attr != NULL) + i = strtoul(attr, &rest, 10); + if (i > 0) { + dbg(event->udev, "request part #%d of result string\n", i); + cpos = event->program_result; + while (--i) { + while (cpos[0] != '\0' && !isspace(cpos[0])) + cpos++; + while (isspace(cpos[0])) + cpos++; + } + if (i > 0) { + err(event->udev, "requested part of result string not found\n"); + break; + } + util_strlcpy(temp2, cpos, sizeof(temp2)); + /* %{2+}c copies the whole string from the second part on */ + if (rest[0] != '+') { + cpos = strchr(temp2, ' '); + if (cpos) + cpos[0] = '\0'; + } + util_strlcat(string, temp2, maxsize); + dbg(event->udev, "substitute part of result string '%s'\n", temp2); + } else { + util_strlcat(string, event->program_result, maxsize); + dbg(event->udev, "substitute result string '%s'\n", event->program_result); + } + break; + case SUBST_ATTR: + if (attr == NULL) + err(event->udev, "missing file parameter for attr\n"); + else { + char value[UTIL_NAME_SIZE] = ""; + size_t size; + + udev_event_apply_subsys_kernel(event, attr, value, sizeof(value), 1); + + /* try the current device, other matches may have selected */ + if (value[0] == '\0' && event->dev_parent != NULL && event->dev_parent != event->dev) { + const char *val; + + val = udev_device_get_attr_value(event->dev_parent, attr); + if (val != NULL) + util_strlcpy(value, val, sizeof(value)); + } + + /* look at all devices along the chain of parents */ + if (value[0] == '\0') { + struct udev_device *dev_parent = dev; + const char *val; + + do { + dbg(event->udev, "looking at '%s'\n", udev_device_get_syspath(dev_parent)); + val = udev_device_get_attr_value(dev_parent, attr); + if (val != NULL) { + util_strlcpy(value, val, sizeof(value)); + break; + } + dev_parent = udev_device_get_parent(dev_parent); + } while (dev_parent != NULL); + } + + if (value[0]=='\0') + break; + + /* strip trailing whitespace, and replace unwanted characters */ + size = strlen(value); + while (size > 0 && isspace(value[--size])) + value[size] = '\0'; + count = util_replace_chars(value, ALLOWED_CHARS_INPUT); + if (count > 0) + info(event->udev, "%i character(s) replaced\n" , count); + util_strlcat(string, value, maxsize); + dbg(event->udev, "substitute sysfs value '%s'\n", value); + } + break; + case SUBST_PARENT: + { + struct udev_device *dev_parent; + const char *devnode; + + dev_parent = udev_device_get_parent(event->dev); + if (dev_parent == NULL) + break; + devnode = udev_device_get_devnode(dev_parent); + if (devnode != NULL) { + size_t devlen = strlen(udev_get_dev_path(event->udev))+1; + + util_strlcat(string, &devnode[devlen], maxsize); + dbg(event->udev, "found parent '%s', got node name '%s'\n", + udev_device_get_syspath(dev_parent), &devnode[devlen]); + } + } + break; + case SUBST_TEMP_NODE: + if (event->tmp_node[0] == '\0' && major(udev_device_get_devnum(dev)) > 0) { + dbg(event->udev, "create temporary device node for callout\n"); + snprintf(event->tmp_node, sizeof(event->tmp_node), "%s/.tmp-%u-%u", + udev_get_dev_path(event->udev), + major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev))); + udev_node_mknod(dev, event->tmp_node, makedev(0,0), 0600, 0, 0); + } + util_strlcat(string, event->tmp_node, maxsize); + dbg(event->udev, "substitute temporary device node name '%s'\n", event->tmp_node); + break; + case SUBST_NAME: + if (event->name != NULL) { + util_strlcat(string, event->name, maxsize); + dbg(event->udev, "substitute name '%s'\n", event->name); + } else { + util_strlcat(string, udev_device_get_sysname(dev), maxsize); + dbg(event->udev, "substitute sysname '%s'\n", udev_device_get_sysname(dev)); + } + break; + case SUBST_LINKS: + { + struct udev_list_entry *list_entry; + + list_entry = udev_device_get_properties_list_entry(dev); + util_strlcpy(string, udev_list_entry_get_name(list_entry), maxsize); + udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) { + util_strlcat(string, " ", maxsize); + util_strlcat(string, udev_list_entry_get_name(list_entry), maxsize); + } + } + break; + case SUBST_ROOT: + util_strlcat(string, udev_get_dev_path(event->udev), maxsize); + dbg(event->udev, "substitute udev_root '%s'\n", udev_get_dev_path(event->udev)); + break; + case SUBST_SYS: + util_strlcat(string, udev_get_sys_path(event->udev), maxsize); + dbg(event->udev, "substitute sys_path '%s'\n", udev_get_sys_path(event->udev)); + break; + case SUBST_ENV: + if (attr == NULL) { + dbg(event->udev, "missing attribute\n"); + break; + } else { + struct udev_list_entry *list_entry; + const char *value; + + list_entry = udev_device_get_properties_list_entry(event->dev); + list_entry = udev_list_entry_get_by_name(list_entry, attr); + if (list_entry == NULL) + break; + value = udev_list_entry_get_value(list_entry); + dbg(event->udev, "substitute env '%s=%s'\n", attr, value); + util_strlcat(string, value, maxsize); + break; + } + default: + err(event->udev, "unknown substitution type=%i\n", type); + break; + } + /* possibly truncate to format-char specified length */ + if (len >= 0 && len < (int)strlen(head)) { + head[len] = '\0'; + dbg(event->udev, "truncate to %i chars, subtitution string becomes '%s'\n", len, head); + } + util_strlcat(string, temp, maxsize); + } +} + +static void rename_netif_kernel_log(struct ifreq ifr) { int klog; FILE *f; @@ -100,7 +525,7 @@ static int rename_netif(struct udev_event *event) util_strlcpy(ifr.ifr_newname, event->name, IFNAMSIZ); err = ioctl(sk, SIOCSIFNAME, &ifr); if (err == 0) - kernel_log(ifr); + rename_netif_kernel_log(ifr); else { int loop; @@ -128,7 +553,7 @@ static int rename_netif(struct udev_event *event) while (loop--) { err = ioctl(sk, SIOCSIFNAME, &ifr); if (err == 0) { - kernel_log(ifr); + rename_netif_kernel_log(ifr); break; } @@ -305,7 +730,7 @@ int udev_event_execute_run(struct udev_event *event) char **envp; util_strlcpy(program, cmd, sizeof(program)); - udev_rules_apply_format(event, program, sizeof(program)); + udev_event_apply_format(event, program, sizeof(program)); envp = udev_device_get_properties_envp(event->dev); if (run_program(event->udev, program, envp, NULL, 0, NULL) != 0) { if (!udev_list_entry_get_flag(list_entry)) diff --git a/udev/udev-rules.c b/udev/udev-rules.c index 978581fb50..a9793f4e8d 100644 --- a/udev/udev-rules.c +++ b/udev/udev-rules.c @@ -190,45 +190,6 @@ next: return current; } -/* extract possible {attr} and move str behind it */ -static char *get_format_attribute(struct udev *udev, char **str) -{ - char *pos; - char *attr = NULL; - - if (*str[0] == '{') { - pos = strchr(*str, '}'); - if (pos == NULL) { - err(udev, "missing closing brace for format\n"); - return NULL; - } - pos[0] = '\0'; - attr = *str+1; - *str = pos+1; - dbg(udev, "attribute='%s', str='%s'\n", attr, *str); - } - return attr; -} - -/* extract possible format length and move str behind it*/ -static int get_format_len(struct udev *udev, char **str) -{ - int num; - char *tail; - - if (isdigit(*str[0])) { - num = (int) strtoul(*str, &tail, 10); - if (num > 0) { - *str = tail; - dbg(udev, "format length=%i\n", num); - return num; - } else { - err(udev, "format parsing error '%s'\n", *str); - } - } - return -1; -} - static int import_property_from_string(struct udev_device *dev, char *line) { struct udev *udev = udev_device_get_udev(dev); @@ -412,36 +373,6 @@ static int wait_for_file(struct udev_event *event, const char *file, int timeout return -1; } -/* handle "[$SUBSYSTEM/$KERNEL]<attribute>" lookup */ -static int split_subsys_sysname(struct udev *udev, char *attrstr, char **subsys, char **sysname, char **attr) -{ - char *pos; - - if (attrstr[0] != '[') - return -1; - - *subsys = &attrstr[1]; - pos = strchr(*subsys, ']'); - if (pos == NULL) - return -1; - pos[0] = '\0'; - pos = &pos[1]; - - if (pos[0] == '/') - pos = &pos[1]; - if (pos[0] != '\0') - *attr = pos; - else - *attr = NULL; - - pos = strchr(*subsys, '/'); - if (pos == NULL) - return -1; - pos[0] = '\0'; - *sysname = &pos[1]; - return 0; -} - static int attr_subst_subdir(char *attr, size_t len) { char *pos; @@ -480,345 +411,6 @@ static int attr_subst_subdir(char *attr, size_t len) return found; } -void udev_rules_apply_format(struct udev_event *event, char *string, size_t maxsize) -{ - struct udev_device *dev = event->dev; - char temp[UTIL_PATH_SIZE]; - char temp2[UTIL_PATH_SIZE]; - char *head, *tail, *cpos, *attr, *rest; - int len; - int i; - int count; - enum subst_type { - SUBST_UNKNOWN, - SUBST_DEVPATH, - SUBST_KERNEL, - SUBST_KERNEL_NUMBER, - SUBST_ID, - SUBST_DRIVER, - SUBST_MAJOR, - SUBST_MINOR, - SUBST_RESULT, - SUBST_ATTR, - SUBST_PARENT, - SUBST_TEMP_NODE, - SUBST_NAME, - SUBST_LINKS, - SUBST_ROOT, - SUBST_SYS, - SUBST_ENV, - }; - static const struct subst_map { - char *name; - char fmt; - enum subst_type type; - } map[] = { - { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH }, - { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER }, - { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL }, - { .name = "id", .fmt = 'b', .type = SUBST_ID }, - { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER }, - { .name = "major", .fmt = 'M', .type = SUBST_MAJOR }, - { .name = "minor", .fmt = 'm', .type = SUBST_MINOR }, - { .name = "result", .fmt = 'c', .type = SUBST_RESULT }, - { .name = "attr", .fmt = 's', .type = SUBST_ATTR }, - { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR }, - { .name = "parent", .fmt = 'P', .type = SUBST_PARENT }, - { .name = "tempnode", .fmt = 'N', .type = SUBST_TEMP_NODE }, - { .name = "name", .fmt = 'D', .type = SUBST_NAME }, - { .name = "links", .fmt = 'L', .type = SUBST_LINKS }, - { .name = "root", .fmt = 'r', .type = SUBST_ROOT }, - { .name = "sys", .fmt = 'S', .type = SUBST_SYS }, - { .name = "env", .fmt = 'E', .type = SUBST_ENV }, - { NULL, '\0', 0 } - }; - enum subst_type type; - const struct subst_map *subst; - - head = string; - while (1) { - len = -1; - while (head[0] != '\0') { - if (head[0] == '$') { - /* substitute named variable */ - if (head[1] == '\0') - break; - if (head[1] == '$') { - util_strlcpy(temp, head+2, sizeof(temp)); - util_strlcpy(head+1, temp, maxsize); - head++; - continue; - } - head[0] = '\0'; - for (subst = map; subst->name; subst++) { - if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) { - type = subst->type; - tail = head + strlen(subst->name)+1; - dbg(event->udev, "will substitute format name '%s'\n", subst->name); - goto found; - } - } - head[0] = '$'; - err(event->udev, "unknown format variable '%s'\n", head); - } else if (head[0] == '%') { - /* substitute format char */ - if (head[1] == '\0') - break; - if (head[1] == '%') { - util_strlcpy(temp, head+2, sizeof(temp)); - util_strlcpy(head+1, temp, maxsize); - head++; - continue; - } - head[0] = '\0'; - tail = head+1; - len = get_format_len(event->udev, &tail); - for (subst = map; subst->name; subst++) { - if (tail[0] == subst->fmt) { - type = subst->type; - tail++; - dbg(event->udev, "will substitute format char '%c'\n", subst->fmt); - goto found; - } - } - head[0] = '%'; - err(event->udev, "unknown format char '%c'\n", tail[0]); - } - head++; - } - break; -found: - attr = get_format_attribute(event->udev, &tail); - util_strlcpy(temp, tail, sizeof(temp)); - dbg(event->udev, "format=%i, string='%s', tail='%s'\n", type ,string, tail); - - switch (type) { - case SUBST_DEVPATH: - util_strlcat(string, udev_device_get_devpath(dev), maxsize); - dbg(event->udev, "substitute devpath '%s'\n", udev_device_get_devpath(dev)); - break; - case SUBST_KERNEL: - util_strlcat(string, udev_device_get_sysname(dev), maxsize); - dbg(event->udev, "substitute kernel name '%s'\n", udev_device_get_sysname(dev)); - break; - case SUBST_KERNEL_NUMBER: - if (udev_device_get_sysnum(dev) == NULL) - break; - util_strlcat(string, udev_device_get_sysnum(dev), maxsize); - dbg(event->udev, "substitute kernel number '%s'\n", udev_device_get_sysnum(dev)); - break; - case SUBST_ID: - if (event->dev_parent != NULL) { - util_strlcat(string, udev_device_get_sysname(event->dev_parent), maxsize); - dbg(event->udev, "substitute id '%s'\n", udev_device_get_sysname(event->dev_parent)); - } - break; - case SUBST_DRIVER: - if (event->dev_parent != NULL) { - const char *driver = udev_device_get_driver(event->dev_parent); - - if (driver == NULL) - break; - util_strlcat(string, driver, maxsize); - dbg(event->udev, "substitute driver '%s'\n", driver); - } - break; - case SUBST_MAJOR: - sprintf(temp2, "%d", major(udev_device_get_devnum(dev))); - util_strlcat(string, temp2, maxsize); - dbg(event->udev, "substitute major number '%s'\n", temp2); - break; - case SUBST_MINOR: - sprintf(temp2, "%d", minor(udev_device_get_devnum(dev))); - util_strlcat(string, temp2, maxsize); - dbg(event->udev, "substitute minor number '%s'\n", temp2); - break; - case SUBST_RESULT: - if (event->program_result[0] == '\0') - break; - /* get part part of the result string */ - i = 0; - if (attr != NULL) - i = strtoul(attr, &rest, 10); - if (i > 0) { - dbg(event->udev, "request part #%d of result string\n", i); - cpos = event->program_result; - while (--i) { - while (cpos[0] != '\0' && !isspace(cpos[0])) - cpos++; - while (isspace(cpos[0])) - cpos++; - } - if (i > 0) { - err(event->udev, "requested part of result string not found\n"); - break; - } - util_strlcpy(temp2, cpos, sizeof(temp2)); - /* %{2+}c copies the whole string from the second part on */ - if (rest[0] != '+') { - cpos = strchr(temp2, ' '); - if (cpos) - cpos[0] = '\0'; - } - util_strlcat(string, temp2, maxsize); - dbg(event->udev, "substitute part of result string '%s'\n", temp2); - } else { - util_strlcat(string, event->program_result, maxsize); - dbg(event->udev, "substitute result string '%s'\n", event->program_result); - } - break; - case SUBST_ATTR: - if (attr == NULL) - err(event->udev, "missing file parameter for attr\n"); - else { - char *subsys; - char *sysname; - char *attrib; - char value[UTIL_NAME_SIZE] = ""; - size_t size; - - if (split_subsys_sysname(event->udev, attr, &subsys, &sysname, &attrib) == 0) { - struct udev_device *d; - const char *val; - - if (attrib == NULL) - break; - d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname); - if (d == NULL) - break; - val = udev_device_get_attr_value(d, attrib); - if (val != NULL) - util_strlcpy(value, val, sizeof(value)); - udev_device_unref(d); - } - - /* try the current device, other matches may have selected */ - if (value[0]=='\0' && event->dev_parent != NULL && event->dev_parent != event->dev) { - const char *val; - - val = udev_device_get_attr_value(event->dev_parent, attr); - if (val != NULL) - util_strlcpy(value, val, sizeof(value)); - } - - /* look at all devices along the chain of parents */ - if (value[0]=='\0') { - struct udev_device *dev_parent = dev; - const char *val; - - do { - dbg(event->udev, "looking at '%s'\n", udev_device_get_syspath(dev_parent)); - val = udev_device_get_attr_value(dev_parent, attr); - if (val != NULL) { - util_strlcpy(value, val, sizeof(value)); - break; - } - dev_parent = udev_device_get_parent(dev_parent); - } while (dev_parent != NULL); - } - - if (value[0]=='\0') - break; - - /* strip trailing whitespace, and replace unwanted characters */ - size = strlen(value); - while (size > 0 && isspace(value[--size])) - value[size] = '\0'; - count = util_replace_chars(value, ALLOWED_CHARS_INPUT); - if (count > 0) - info(event->udev, "%i character(s) replaced\n" , count); - util_strlcat(string, value, maxsize); - dbg(event->udev, "substitute sysfs value '%s'\n", value); - } - break; - case SUBST_PARENT: - { - struct udev_device *dev_parent; - const char *devnode; - - dev_parent = udev_device_get_parent(event->dev); - if (dev_parent == NULL) - break; - devnode = udev_device_get_devnode(dev_parent); - if (devnode != NULL) { - size_t devlen = strlen(udev_get_dev_path(event->udev))+1; - - util_strlcat(string, &devnode[devlen], maxsize); - dbg(event->udev, "found parent '%s', got node name '%s'\n", - udev_device_get_syspath(dev_parent), &devnode[devlen]); - } - } - break; - case SUBST_TEMP_NODE: - if (event->tmp_node[0] == '\0' && major(udev_device_get_devnum(dev)) > 0) { - dbg(event->udev, "create temporary device node for callout\n"); - snprintf(event->tmp_node, sizeof(event->tmp_node), "%s/.tmp-%u-%u", - udev_get_dev_path(event->udev), - major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev))); - udev_node_mknod(dev, event->tmp_node, makedev(0,0), 0600, 0, 0); - } - util_strlcat(string, event->tmp_node, maxsize); - dbg(event->udev, "substitute temporary device node name '%s'\n", event->tmp_node); - break; - case SUBST_NAME: - if (event->name != NULL) { - util_strlcat(string, event->name, maxsize); - dbg(event->udev, "substitute name '%s'\n", event->name); - } else { - util_strlcat(string, udev_device_get_sysname(dev), maxsize); - dbg(event->udev, "substitute sysname '%s'\n", udev_device_get_sysname(dev)); - } - break; - case SUBST_LINKS: - { - struct udev_list_entry *list_entry; - - list_entry = udev_device_get_properties_list_entry(dev); - util_strlcpy(string, udev_list_entry_get_name(list_entry), maxsize); - udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) { - util_strlcat(string, " ", maxsize); - util_strlcat(string, udev_list_entry_get_name(list_entry), maxsize); - } - } - break; - case SUBST_ROOT: - util_strlcat(string, udev_get_dev_path(event->udev), maxsize); - dbg(event->udev, "substitute udev_root '%s'\n", udev_get_dev_path(event->udev)); - break; - case SUBST_SYS: - util_strlcat(string, udev_get_sys_path(event->udev), maxsize); - dbg(event->udev, "substitute sys_path '%s'\n", udev_get_sys_path(event->udev)); - break; - case SUBST_ENV: - if (attr == NULL) { - dbg(event->udev, "missing attribute\n"); - break; - } else { - struct udev_list_entry *list_entry; - const char *value; - - list_entry = udev_device_get_properties_list_entry(event->dev); - list_entry = udev_list_entry_get_by_name(list_entry, attr); - if (list_entry == NULL) - break; - value = udev_list_entry_get_value(list_entry); - dbg(event->udev, "substitute env '%s=%s'\n", attr, value); - util_strlcat(string, value, maxsize); - break; - } - default: - err(event->udev, "unknown substitution type=%i\n", type); - break; - } - /* possibly truncate to format-char specified length */ - if (len >= 0 && len < (int)strlen(head)) { - head[len] = '\0'; - dbg(event->udev, "truncate to %i chars, subtitution string becomes '%s'\n", len, head); - } - util_strlcat(string, temp, maxsize); - } -} - static char *key_val(struct udev_rule *rule, struct key *key) { return rule->buf + key->val_off; @@ -943,34 +535,20 @@ static int match_rule(struct udev_event *event, struct udev_rule *rule) if (rule->test.operation == KEY_OP_MATCH || rule->test.operation == KEY_OP_NOMATCH) { char filename[UTIL_PATH_SIZE]; - char *subsys; - char *sysname; - char *attrib; struct stat statbuf; int match; util_strlcpy(filename, key_val(rule, &rule->test), sizeof(filename)); - udev_rules_apply_format(event, filename, sizeof(filename)); - - if (split_subsys_sysname(event->udev, filename, &subsys, &sysname, &attrib) == 0) { - struct udev_device *d; - d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname); - if (d != NULL) { - util_strlcpy(filename, udev_device_get_syspath(d), sizeof(filename)); - if (attrib != NULL) { - util_strlcat(filename, "/", sizeof(filename)); - util_strlcat(filename, attrib, sizeof(filename)); - } - udev_device_unref(d); - } - } else if (filename[0] != '/') { - char tmp[UTIL_PATH_SIZE]; + udev_event_apply_format(event, filename, sizeof(filename)); + if (udev_event_apply_subsys_kernel(event, NULL, filename, sizeof(filename), 0) != 0) + if (filename[0] != '/') { + char tmp[UTIL_PATH_SIZE]; - util_strlcpy(tmp, udev_device_get_syspath(dev), sizeof(tmp)); - util_strlcat(tmp, "/", sizeof(tmp)); - util_strlcat(tmp, filename, sizeof(tmp)); - util_strlcpy(filename, tmp, sizeof(filename)); - } + util_strlcpy(tmp, udev_device_get_syspath(dev), sizeof(tmp)); + util_strlcat(tmp, "/", sizeof(tmp)); + util_strlcat(tmp, filename, sizeof(tmp)); + util_strlcpy(filename, tmp, sizeof(filename)); + } attr_subst_subdir(filename, sizeof(filename)); @@ -994,7 +572,7 @@ static int match_rule(struct udev_event *event, struct udev_rule *rule) int found; util_strlcpy(filename, key_val(rule, &rule->wait_for), sizeof(filename)); - udev_rules_apply_format(event, filename, sizeof(filename)); + udev_event_apply_format(event, filename, sizeof(filename)); found = (wait_for_file(event, filename, 10) == 0); if (!found && (rule->wait_for.operation != KEY_OP_NOMATCH)) goto nomatch; @@ -1009,27 +587,11 @@ static int match_rule(struct udev_event *event, struct udev_rule *rule) char attr[UTIL_PATH_SIZE]; const char *key_name = key_pair_name(rule, pair); const char *key_value = key_val(rule, &pair->key); - char *subsys; - char *sysname; - char *attrib; char value[UTIL_NAME_SIZE] = ""; size_t len; util_strlcpy(attr, key_name, sizeof(attr)); - if (split_subsys_sysname(event->udev, attr, &subsys, &sysname, &attrib) == 0) { - struct udev_device *d; - const char *val; - - if (attrib == NULL) - goto nomatch; - d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname); - if (d == NULL) - goto nomatch; - val = udev_device_get_attr_value(d, attrib); - if (val != NULL) - util_strlcpy(value, val, sizeof(value)); - udev_device_unref(d); - } + udev_event_apply_subsys_kernel(event, attr, value, sizeof(value), 1); if (value[0]=='\0') { const char *val; @@ -1126,7 +688,7 @@ try_parent: char result[UTIL_PATH_SIZE]; util_strlcpy(program, key_val(rule, &rule->program), sizeof(program)); - udev_rules_apply_format(event, program, sizeof(program)); + udev_event_apply_format(event, program, sizeof(program)); envp = udev_device_get_properties_envp(dev); if (run_program(event->udev, program, envp, result, sizeof(result), NULL) != 0) { dbg(event->udev, "PROGRAM is false\n"); @@ -1163,7 +725,7 @@ try_parent: int rc = -1; util_strlcpy(import, key_val(rule, &rule->import), sizeof(import)); - udev_rules_apply_format(event, import, sizeof(import)); + udev_event_apply_format(event, import, sizeof(import)); dbg(event->udev, "check for IMPORT import='%s'\n", import); if (rule->import_type == IMPORT_PROGRAM) { rc = import_program_into_env(event->dev, import); @@ -1194,7 +756,7 @@ try_parent: /* make sure we don't write to the same string we possibly read from */ util_strlcpy(temp_value, value, sizeof(temp_value)); - udev_rules_apply_format(event, temp_value, sizeof(temp_value)); + udev_event_apply_format(event, temp_value, sizeof(temp_value)); if (temp_value[0] != '\0') { struct udev_list_entry *entry; @@ -1213,27 +775,12 @@ try_parent: if (pair->key.operation == KEY_OP_ASSIGN) { const char *key_name = key_pair_name(rule, pair); - char *subsys; - char *sysname; - char *attrib; char attr[UTIL_PATH_SIZE]; char value[UTIL_NAME_SIZE]; FILE *f; util_strlcpy(attr, key_name, sizeof(attr)); - if (split_subsys_sysname(event->udev, attr, &subsys, &sysname, &attrib) == 0) { - struct udev_device *d; - - d = udev_device_new_from_subsystem_sysname(event->udev, subsys, sysname); - if (d != NULL) { - util_strlcpy(attr, udev_device_get_syspath(d), sizeof(attr)); - if (attrib != NULL) { - util_strlcat(attr, "/", sizeof(attr)); - util_strlcat(attr, attrib, sizeof(attr)); - } - udev_device_unref(d); - } - } else { + if (udev_event_apply_subsys_kernel(event, key_name, attr, sizeof(attr), 0) != 0) { util_strlcpy(attr, udev_device_get_syspath(dev), sizeof(attr)); util_strlcat(attr, "/", sizeof(attr)); util_strlcat(attr, key_name, sizeof(attr)); @@ -1242,7 +789,7 @@ try_parent: attr_subst_subdir(attr, sizeof(attr)); util_strlcpy(value, key_val(rule, &pair->key), sizeof(value)); - udev_rules_apply_format(event, value, sizeof(value)); + udev_event_apply_format(event, value, sizeof(value)); info(event->udev, "writing '%s' to sysfs file '%s'\n", value, attr); f = fopen(attr, "w"); if (f != NULL) { @@ -1318,7 +865,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udev_event *event) event->mode_final = 1; char buf[20]; util_strlcpy(buf, key_val(rule, &rule->mode), sizeof(buf)); - udev_rules_apply_format(event, buf, sizeof(buf)); + udev_event_apply_format(event, buf, sizeof(buf)); event->mode = strtol(buf, NULL, 8); dbg(event->udev, "applied mode=%#o to '%s'\n", event->mode, udev_device_get_sysname(dev)); @@ -1327,7 +874,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udev_event *event) if (rule->owner.operation == KEY_OP_ASSIGN_FINAL) event->owner_final = 1; util_strlcpy(event->owner, key_val(rule, &rule->owner), sizeof(event->owner)); - udev_rules_apply_format(event, event->owner, sizeof(event->owner)); + udev_event_apply_format(event, event->owner, sizeof(event->owner)); dbg(event->udev, "applied owner='%s' to '%s'\n", event->owner, udev_device_get_sysname(dev)); } @@ -1335,7 +882,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udev_event *event) if (rule->group.operation == KEY_OP_ASSIGN_FINAL) event->group_final = 1; util_strlcpy(event->group, key_val(rule, &rule->group), sizeof(event->group)); - udev_rules_apply_format(event, event->group, sizeof(event->group)); + udev_event_apply_format(event, event->group, sizeof(event->group)); dbg(event->udev, "applied group='%s' to '%s'\n", event->group, udev_device_get_sysname(dev)); } @@ -1359,7 +906,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udev_event *event) } /* allow multiple symlinks separated by spaces */ util_strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp)); - udev_rules_apply_format(event, temp, sizeof(temp)); + udev_event_apply_format(event, temp, sizeof(temp)); if (rule->string_escape == ESCAPE_UNSET) count = util_replace_chars(temp, ALLOWED_CHARS_FILE " "); else if (rule->string_escape == ESCAPE_REPLACE) @@ -1400,7 +947,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udev_event *event) name_set = 1; util_strlcpy(event->name, key_val(rule, &rule->name), sizeof(event->name)); - udev_rules_apply_format(event, event->name, sizeof(event->name)); + udev_event_apply_format(event, event->name, sizeof(event->name)); if (rule->string_escape == ESCAPE_UNSET || rule->string_escape == ESCAPE_REPLACE) { count = util_replace_chars(event->name, ALLOWED_CHARS_FILE); diff --git a/udev/udev.h b/udev/udev.h index c8425f768f..c73ec906e1 100644 --- a/udev/udev.h +++ b/udev/udev.h @@ -60,7 +60,6 @@ static inline void logging_close(void) closelog(); } -/* udev-event.c */ struct udev_event { struct udev *udev; struct udev_device *dev; @@ -86,18 +85,22 @@ struct udev_event { int exitstatus; time_t queue_time; }; -struct udev_rules; -extern struct udev_event *udev_event_new(struct udev_device *dev); -extern void udev_event_unref(struct udev_event *event); -extern int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules); -extern int udev_event_execute_run(struct udev_event *event); /* udev-rules.c */ +struct udev_rules; extern struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names); extern void udev_rules_unref(struct udev_rules *rules); extern int udev_rules_get_name(struct udev_rules *rules, struct udev_event *event); extern int udev_rules_get_run(struct udev_rules *rules, struct udev_event *event); -extern void udev_rules_apply_format(struct udev_event *event, char *string, size_t maxsize); + +/* udev-event.c */ +extern struct udev_event *udev_event_new(struct udev_device *dev); +extern void udev_event_unref(struct udev_event *event); +extern int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules); +extern int udev_event_execute_run(struct udev_event *event); +extern void udev_event_apply_format(struct udev_event *event, char *string, size_t maxsize); +extern int udev_event_apply_subsys_kernel(struct udev_event *event, const char *string, + char *result, size_t maxsize, int read_value); /* udev-node.c */ extern int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid); @@ -114,11 +117,6 @@ extern gid_t lookup_group(struct udev *udev, const char *group); extern int run_program(struct udev *udev, const char *command, char **envp, char *result, size_t ressize, size_t *reslen); -/* udev_utils_file.c */ -extern int file_map(const char *filename, char **buf, size_t *bufsize); -extern void file_unmap(void *buf, size_t bufsize); -extern size_t buf_get_line(const char *buf, size_t buflen, size_t cur); - /* udev-selinux.c */ #ifndef USE_SELINUX static inline void selinux_init(struct udev *udev) {} diff --git a/udev/udevadm-test.c b/udev/udevadm-test.c index 342b89d084..ceeead0c8a 100644 --- a/udev/udevadm-test.c +++ b/udev/udevadm-test.c @@ -133,7 +133,7 @@ int udevadm_test(struct udev *udev, int argc, char *argv[]) char program[UTIL_PATH_SIZE]; util_strlcpy(program, udev_list_entry_get_name(entry), sizeof(program)); - udev_rules_apply_format(event, program, sizeof(program)); + udev_event_apply_format(event, program, sizeof(program)); info(udev, "run: '%s'\n", program); } } |