dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Thierry Reding <thierry.reding@gmail.com>
To: dri-devel@lists.freedesktop.org
Cc: xorg-devel@lists.x.org, Emil Velikov <emil.l.velikov@gmail.com>,
	Mark Kettenis <mark.kettenis@xs4all.nl>
Subject: [PATCH libdrm v3 5/5] xf86drm: Reuse sysfs_uevent_get()
Date: Wed, 18 Jan 2017 10:02:09 +0100	[thread overview]
Message-ID: <20170118090209.13819-6-thierry.reding@gmail.com> (raw)
In-Reply-To: <20170118090209.13819-1-thierry.reding@gmail.com>

From: Thierry Reding <treding@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@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

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

      parent reply	other threads:[~2017-01-18  9:02 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-18  9:02 [PATCH libdrm v3 0/5] xf86drm: Add USB, platform and host1x bus support Thierry Reding
2017-01-18  9:02 ` [PATCH libdrm v3 1/5] xf86drm: Factor out drmDeviceAlloc() Thierry Reding
     [not found] ` <20170118090209.13819-1-thierry.reding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-01-18  9:02   ` [PATCH libdrm v3 2/5] xf86drm: Add USB support Thierry Reding
     [not found]     ` <587F4FB3.2070006@bfs.de>
2017-01-19 10:20       ` Thierry Reding
     [not found]         ` <20170119102038.GB30182-EkSeR96xj6Pcmrwk2tT4+A@public.gmane.org>
2017-01-21 17:44           ` walter harms
2017-01-18  9:02   ` [PATCH libdrm v3 3/5] xf86drm: Add platform and host1x bus support Thierry Reding
     [not found]     ` <587F8304.90105@bfs.de>
2017-01-19 10:25       ` Thierry Reding
     [not found]     ` <20170118090209.13819-4-thierry.reding-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-01-20 14:03       ` Emil Velikov
2017-01-18  9:02   ` [PATCH libdrm v3 4/5] tests/drmdevice: Add USB, platform and host1x support Thierry Reding
2017-01-18  9:02 ` Thierry Reding [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170118090209.13819-6-thierry.reding@gmail.com \
    --to=thierry.reding@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=emil.l.velikov@gmail.com \
    --cc=mark.kettenis@xs4all.nl \
    --cc=xorg-devel@lists.x.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox