diff options
author | Thierry Reding <treding@nvidia.com> | 2017-01-18 08:29:23 +0100 |
---|---|---|
committer | Thierry Reding <treding@nvidia.com> | 2017-01-20 16:27:33 +0100 |
commit | 5403cb39c124c444babec51bd4499971cd86ccfa (patch) | |
tree | 7a3a23cfcd24ce1856eca10ba8c067d3f9130a4d | |
parent | 13b99f2a898a67320499e538303a689a270bb7f4 (diff) | |
download | libdrm-5403cb39c124c444babec51bd4499971cd86ccfa.tar.gz libdrm-5403cb39c124c444babec51bd4499971cd86ccfa.tar.bz2 libdrm-5403cb39c124c444babec51bd4499971cd86ccfa.zip |
xf86drm: Reuse sysfs_uevent_get()
Recent patches for USB, platform and host1x bus support introduced the
sysfs_uevent_get() function that provides a generic way of parsing the
sysfs uevent file that is associated with each device in Linux.
Open-coded variants of this still exist in other places, so make those
reuse the new function to remove some code duplication.
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Signed-off-by: Thierry Reding <treding@nvidia.com>
-rw-r--r-- | xf86drm.c | 66 |
1 files changed, 18 insertions, 48 deletions
@@ -2971,32 +2971,21 @@ static int drmParseSubsystemType(int maj, int min) static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info) { #ifdef __linux__ - char path[PATH_MAX + 1]; - char data[512 + 1]; - char *str; - int domain, bus, dev, func; - int fd, ret; + unsigned int domain, bus, dev, func; + char path[PATH_MAX + 1], *value; + int num; - snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent", maj, min); - fd = open(path, O_RDONLY); - if (fd < 0) - return -errno; + snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min); - ret = read(fd, data, sizeof(data)-1); - close(fd); - if (ret < 0) - return -errno; - data[ret] = '\0'; + value = sysfs_uevent_get(path, "PCI_SLOT_NAME"); + if (!value) + return -ENOENT; -#define TAG "PCI_SLOT_NAME=" - str = strstr(data, TAG); - if (str == NULL) - return -EINVAL; + num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func); + free(value); - if (sscanf(str, TAG "%04x:%02x:%02x.%1u", - &domain, &bus, &dev, &func) != 4) + if (num != 4) return -EINVAL; -#undef TAG info->domain = domain; info->bus = bus; @@ -4084,13 +4073,8 @@ char *drmGetDeviceNameFromFd2(int fd) { #ifdef __linux__ struct stat sbuf; - char *device_name = NULL; + char path[PATH_MAX + 1], *value; unsigned int maj, min; - FILE *f; - char buf[512]; - static const char match[9] = "\nDEVNAME="; - size_t expected = 1; - if (fstat(fd, &sbuf)) return NULL; @@ -4101,30 +4085,16 @@ char *drmGetDeviceNameFromFd2(int fd) if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) return NULL; - snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min); - if (!(f = fopen(buf, "r"))) - return NULL; - - while (expected < sizeof(match)) { - int c = getc(f); + snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min); - if (c == EOF) { - fclose(f); - return NULL; - } else if (c == match[expected] ) - expected++; - else - expected = 0; - } + value = sysfs_uevent_get(path, "DEVNAME"); + if (!value) + return NULL; - strcpy(buf, "/dev/"); - if (fgets(buf + 5, sizeof(buf) - 5, f)) { - buf[strcspn(buf, "\n")] = '\0'; - device_name = strdup(buf); - } + snprintf(path, sizeof(path), "/dev/%s", value); + free(value); - fclose(f); - return device_name; + return strdup(path); #else struct stat sbuf; char node[PATH_MAX + 1]; |