From 14f4025607e905cb230c6e5b32488e9f2d07a0d2 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Wed, 22 Oct 2008 18:03:38 +0200 Subject: [PATCH] add util_resolve_subsys_kernel() --- extras/floppy/Makefile.am | 2 ++ udev/udev-util.c | 63 +++++++++++++++++++++++++++++++++++++++ udev/udev.h | 2 ++ 3 files changed, 67 insertions(+) diff --git a/extras/floppy/Makefile.am b/extras/floppy/Makefile.am index c8126895..8dadefa0 100644 --- a/extras/floppy/Makefile.am +++ b/extras/floppy/Makefile.am @@ -10,6 +10,8 @@ create_floppy_devices_SOURCES = \ ../../udev/lib/libudev.c \ ../../udev/lib/libudev-list.c \ ../../udev/lib/libudev-util.c \ + ../../udev/lib/libudev-device.c \ + ../../udev/lib/libudev-enumerate.c \ ../../udev/udev-util.c if USE_SELINUX diff --git a/udev/udev-util.c b/udev/udev-util.c index 76445bbc..3c2e2a50 100644 --- a/udev/udev-util.c +++ b/udev/udev-util.c @@ -162,6 +162,69 @@ extern gid_t util_lookup_group(struct udev *udev, const char *group) return gid; } +/* handle "[/]" format */ +int util_resolve_subsys_kernel(struct udev *udev, const char *string, + char *result, size_t maxsize, int read_value) +{ + char temp[UTIL_PATH_SIZE]; + char *subsys; + char *sysname; + struct udev_device *dev; + char *attr; + + 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, subsys, sysname); + if (dev == NULL) + return -1; + + if (read_value) { + const char *val; + + val = udev_device_get_sysattr_value(dev, attr); + if (val != NULL) + util_strlcpy(result, val, maxsize); + else + result[0] = '\0'; + info(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, "path '[%s/%s]%s' is '%s'\n", subsys, sysname, attr, result); + } + udev_device_unref(dev); + return 0; +} + int util_run_program(struct udev *udev, const char *command, char **envp, char *result, size_t ressize, size_t *reslen) { diff --git a/udev/udev.h b/udev/udev.h index 89f6f11e..217e942c 100644 --- a/udev/udev.h +++ b/udev/udev.h @@ -112,6 +112,8 @@ extern uid_t util_lookup_user(struct udev *udev, const char *user); extern gid_t util_lookup_group(struct udev *udev, const char *group); extern int util_run_program(struct udev *udev, const char *command, char **envp, char *result, size_t ressize, size_t *reslen); +extern int util_resolve_subsys_kernel(struct udev *udev, const char *string, + char *result, size_t maxsize, int read_value); /* udev-selinux.c */ #ifndef USE_SELINUX -- 2.39.5