From: Kay Sievers Date: Mon, 15 Sep 2008 18:19:56 +0000 (-0700) Subject: libudev: handle "device" link as parent, handle "class" "block" as "subsystem" X-Git-Tag: 174~1548 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=0518da3b74a388680896747696b3644bbd3d98e5 libudev: handle "device" link as parent, handle "class" "block" as "subsystem" --- diff --git a/TODO b/TODO index 763bcd1fc..18f75b5f3 100644 --- a/TODO +++ b/TODO @@ -11,6 +11,5 @@ These things will change in future udev versions: o symlink names to udevadm will no longer be resolved to old command names before next release: - o handle old "device" link in get_parent (CONFIG_SYSFS_DEPRECATED*=y) - o handle "subsystem", "driver", as device o switch libudev API to syspath instead of devpath + o udev_* prefix for private methods (ctrl_*)? diff --git a/udev/lib/libudev-device.c b/udev/lib/libudev-device.c index cda2de749..918398b79 100644 --- a/udev/lib/libudev-device.c +++ b/udev/lib/libudev-device.c @@ -142,7 +142,7 @@ static int device_read_db(struct udev_device *udev_device) device_set_ignore_remove(udev_device, atoi(val)); break; case 'E': - device_add_property(udev_device, val); + device_add_property_from_string(udev_device, val); break; } } @@ -236,15 +236,29 @@ static struct udev_device *device_new_from_parent(struct udev_device *udev_devic pos[0] = '\0'; udev_device_parent = udev_device_new_from_devpath(udev_device->udev, path); if (udev_device_parent != NULL) - break; + return udev_device_parent; + } + + /* follow "device" link in deprecated sysfs /sys/class/ layout */ + if (strncmp(udev_device->devpath, "/class/", 7) == 0) { + util_strlcpy(path, udev_device->devpath, sizeof(path)); + util_strlcat(path, "/device", sizeof(path)); + if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) { + udev_device_parent = udev_device_new_from_devpath(udev_device->udev, path); + if (udev_device_parent != NULL) + return udev_device_parent; + } } - return udev_device_parent; + return NULL; } struct udev_device *udev_device_get_parent(struct udev_device *udev_device) { - if (udev_device->parent_device == NULL) - udev_device->parent_device = device_new_from_parent(udev_device); + if (udev_device->parent_device != NULL) { + info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device); + return udev_device->parent_device; + } + udev_device->parent_device = device_new_from_parent(udev_device); return udev_device->parent_device; } @@ -382,10 +396,29 @@ const char *udev_device_get_subsystem(struct udev_device *udev_device) return NULL; if (udev_device->subsystem != NULL) return udev_device->subsystem; - if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2) - return NULL; - udev_device->subsystem = strdup(subsystem); - return udev_device->subsystem; + + /* read "subsytem" link */ + if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) == 0) { + udev_device->subsystem = strdup(subsystem); + return udev_device->subsystem; + } + + /* implicit names */ + if (strncmp(udev_device->devpath, "/module/", 8) == 0) { + udev_device->subsystem = strdup("module"); + return udev_device->subsystem; + } + if (strstr(udev_device->devpath, "/drivers/") != NULL) { + udev_device->subsystem = strdup("drivers"); + return udev_device->subsystem; + } + if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 || + strncmp(udev_device->devpath, "/class/", 7) == 0 || + strncmp(udev_device->devpath, "/bus/", 5) == 0) { + udev_device->subsystem = strdup("subsystem"); + return udev_device->subsystem; + } + return NULL; } /** @@ -442,17 +475,8 @@ int udev_device_get_properties(struct udev_device *udev_device, if (udev_device == NULL) return -1; list_for_each_entry(name_loop, &udev_device->env_list, node) { - char name[UTIL_NAME_SIZE]; - char *val; - - strncpy(name, name_loop->name, sizeof(name)); - val = strchr(name, '='); - if (val == NULL) - continue; - val[0] = '\0'; - val = &val[1]; count++; - if (cb(udev_device, name, val, data) != 0) + if (cb(udev_device, name_loop->name, name_loop->value, data) != 0) break; } return count; @@ -495,6 +519,7 @@ unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device) const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr) { + struct util_name_entry *name_loop; char path[UTIL_PATH_SIZE]; char value[UTIL_NAME_SIZE]; struct stat statbuf; @@ -502,6 +527,14 @@ const char *udev_device_get_attr_value(struct udev_device *udev_device, const ch ssize_t size; const char *val = NULL; + /* look for possibly already cached result */ + list_for_each_entry(name_loop, &udev_device->attr_list, node) { + if (strcmp(name_loop->name, attr) == 0) { + info(udev_device->udev, "'%s' in cache '%s'\n", attr, name_loop->value); + return name_loop->value; + } + } + util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path)); util_strlcat(path, "/", sizeof(path)); util_strlcat(path, attr, sizeof(path)); @@ -551,7 +584,7 @@ const char *udev_device_get_attr_value(struct udev_device *udev_device, const ch if (size == sizeof(value)) goto out; - /* got a valid value, store and return it */ + /* got a valid value, store it in cache and return it */ value[size] = '\0'; util_remove_trailing_chars(value, '\n'); info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value); @@ -593,13 +626,30 @@ int device_add_devlink(struct udev_device *udev_device, const char *devlink) return 0; } -int device_add_property(struct udev_device *udev_device, const char *property) +int device_add_property(struct udev_device *udev_device, const char *key, const char *value) { - if (util_name_list_add(udev_device->udev, &udev_device->env_list, property, NULL, 0) == NULL) + if (util_name_list_add(udev_device->udev, &udev_device->env_list, key, value, 0) == NULL) return -ENOMEM; return 0; } +int device_add_property_from_string(struct udev_device *udev_device, const char *property) +{ + char name[UTIL_PATH_SIZE]; + char *val; + + strncpy(name, property, sizeof(name)); + val = strchr(name, '='); + if (val == NULL) + return -1; + val[0] = '\0'; + val = &val[1]; + if (val[0] == '\0') + val = NULL; + device_add_property(udev_device, name, val); + return 0; +} + int device_set_action(struct udev_device *udev_device, const char *action) { udev_device->action = strdup(action); diff --git a/udev/lib/libudev-monitor.c b/udev/lib/libudev-monitor.c index 60f7bc29e..8e9cf2d0e 100644 --- a/udev/lib/libudev-monitor.c +++ b/udev/lib/libudev-monitor.c @@ -336,7 +336,7 @@ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monito } else if (strncmp(key, "TIMEOUT=", 8) == 0) { device_set_timeout(udev_device, strtoull(&key[8], NULL, 10)); } - device_add_property(udev_device, key); + device_add_property_from_string(udev_device, key); } device_set_devnum(udev_device, makedev(maj, min)); diff --git a/udev/lib/libudev-private.h b/udev/lib/libudev-private.h index c2a326803..f28bd9acf 100644 --- a/udev/lib/libudev-private.h +++ b/udev/lib/libudev-private.h @@ -59,7 +59,8 @@ extern int device_set_devpath(struct udev_device *udev_device, const char *devpa extern int device_set_subsystem(struct udev_device *udev_device, const char *subsystem); extern int device_set_devname(struct udev_device *udev_device, const char *devname); extern int device_add_devlink(struct udev_device *udev_device, const char *devlink); -extern int device_add_property(struct udev_device *udev_device, const char *property); +extern int device_add_property(struct udev_device *udev_device, const char *key, const char *value); +extern int device_add_property_from_string(struct udev_device *udev_device, const char *property); extern int device_set_action(struct udev_device *udev_device, const char *action); extern int device_set_driver(struct udev_device *udev_device, const char *driver); extern const char *device_get_devpath_old(struct udev_device *udev_device);