diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2020-12-14 08:40:10 +0100 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2020-12-15 18:14:47 +0100 |
commit | 1223227f952a1939a592760ae437e417ba24bb01 (patch) | |
tree | 6bb57d0eea3d5a0dfd1f6564be84fe4c40630b3e /src/shared/udev-util.c | |
parent | udev: move util_replace_chars() to udev-util.c (diff) | |
download | systemd-1223227f952a1939a592760ae437e417ba24bb01.tar.xz systemd-1223227f952a1939a592760ae437e417ba24bb01.zip |
udev: move util_resolve_subsys_kernel() to udev-util.c
Diffstat (limited to 'src/shared/udev-util.c')
-rw-r--r-- | src/shared/udev-util.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/shared/udev-util.c b/src/shared/udev-util.c index d03e4bbe6f..5a506887e7 100644 --- a/src/shared/udev-util.c +++ b/src/shared/udev-util.c @@ -16,6 +16,7 @@ #include "signal-util.h" #include "string-table.h" #include "string-util.h" +#include "strxcpyx.h" #include "udev-util.h" #include "utf8.h" @@ -471,3 +472,67 @@ size_t udev_replace_chars(char *str, const char *allow) { } return replaced; } + +int udev_resolve_subsys_kernel(const char *string, char *result, size_t maxsize, bool read_value) { + _cleanup_(sd_device_unrefp) sd_device *dev = NULL; + _cleanup_free_ char *temp = NULL; + char *subsys, *sysname, *attr; + const char *val; + int r; + + assert(string); + assert(result); + + /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */ + + if (string[0] != '[') + return -EINVAL; + + temp = strdup(string); + if (!temp) + return -ENOMEM; + + subsys = &temp[1]; + + sysname = strchr(subsys, '/'); + if (!sysname) + return -EINVAL; + sysname[0] = '\0'; + sysname = &sysname[1]; + + attr = strchr(sysname, ']'); + if (!attr) + return -EINVAL; + attr[0] = '\0'; + attr = &attr[1]; + if (attr[0] == '/') + attr = &attr[1]; + if (attr[0] == '\0') + attr = NULL; + + if (read_value && !attr) + return -EINVAL; + + r = sd_device_new_from_subsystem_sysname(&dev, subsys, sysname); + if (r < 0) + return r; + + if (read_value) { + r = sd_device_get_sysattr_value(dev, attr, &val); + if (r < 0 && r != -ENOENT) + return r; + if (r == -ENOENT) + result[0] = '\0'; + else + strscpy(result, maxsize, val); + log_debug("value '[%s/%s]%s' is '%s'", subsys, sysname, attr, result); + } else { + r = sd_device_get_syspath(dev, &val); + if (r < 0) + return r; + + strscpyl(result, maxsize, val, attr ? "/" : NULL, attr ?: NULL, NULL); + log_debug("path '[%s/%s]%s' is '%s'", subsys, sysname, strempty(attr), result); + } + return 0; +} |