[PATCH libdrm v3 5/5] xf86drm: Reuse sysfs_uevent_get()

Thierry Reding thierry.reding at gmail.com
Wed Jan 18 09:02:09 UTC 2017


From: Thierry Reding <treding at nvidia.com>

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.

Signed-off-by: Thierry Reding <treding at nvidia.com>
---
 xf86drm.c | 66 +++++++++++++++++----------------------------------------------
 1 file changed, 18 insertions(+), 48 deletions(-)

diff --git a/xf86drm.c b/xf86drm.c
index 3c6ec1cc9e00..e4a9e2eb1da4 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -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];
-- 
2.11.0



More information about the dri-devel mailing list