Re: [PATCH libdrm 05/10] xf86drm: introduce a get_real_pci_path() helper

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Feel free to add my r-b to this patch.

On 2018-06-25 19:36, Emil Velikov wrote:
From: Emil Velikov <emil.velikov@xxxxxxxxxxxxx>

Introduce a helper which gets the real sysfs path for the given pci
device.

In other words, instead opening the /sys/dev/char/*/device symlink, we
opt for the actual /sys/devices/pci*/*/

It folds three (nearly identical) snprintf's and paves the way of adding
extra devices (see next patch) a piece of pie.

Signed-off-by: Emil Velikov <emil.velikov@xxxxxxxxxxxxx>
---
  xf86drm.c | 62 ++++++++++++++++++++++++++++++++++++++++++++-----------
  1 file changed, 50 insertions(+), 12 deletions(-)

diff --git a/xf86drm.c b/xf86drm.c
index d4810740..8ccd528f 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -2992,16 +2992,37 @@ static int drmParseSubsystemType(int maj, int min)
  #endif
  }
+static char *
+get_real_pci_path(int maj, int min)
+{
+    char path[PATH_MAX + 1];
+    char *real_path = malloc(PATH_MAX);
+
+    if (!real_path)
+        return NULL;
+
+    snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
+    if (!realpath(path, real_path)) {
+        free(real_path);
+        return NULL;
+    }
+
+    return real_path;
+}
+
  static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
  {
  #ifdef __linux__
      unsigned int domain, bus, dev, func;
-    char path[PATH_MAX + 1], *value;
+    char *real_path, *value;
      int num;
- snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
+    real_path = get_real_pci_path(maj, min);
+    if (!real_path)
+        return -ENOENT;
- value = sysfs_uevent_get(path, "PCI_SLOT_NAME");
+    value = sysfs_uevent_get(real_path, "PCI_SLOT_NAME");
+    free(real_path);
      if (!value)
          return -ENOENT;
@@ -3114,24 +3135,32 @@ static int parse_separate_sysfs_files(int maj, int min,
        "subsystem_vendor",
        "subsystem_device",
      };
-    char path[PATH_MAX + 1];
+    char path[PATH_MAX + 1], *real_path;
      unsigned int data[ARRAY_SIZE(attrs)];
      FILE *fp;
      int ret;
+ real_path = get_real_pci_path(maj, min);
+    if (!real_path)
+        return -ENOENT;
+
      for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
-        snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/%s", maj, min,
-                 attrs[i]);
+        snprintf(path, PATH_MAX, "%s/%s", real_path, attrs[i]);
          fp = fopen(path, "r");
-        if (!fp)
+        if (!fp) {
+            free(real_path);
              return -errno;
+        }
ret = fscanf(fp, "%x", &data[i]);
          fclose(fp);
-        if (ret != 1)
+        if (ret != 1) {
+            free(real_path);
              return -errno;
+        }
}
+    free(real_path);
device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
      device->vendor_id = data[1] & 0xffff;
@@ -3145,19 +3174,28 @@ static int parse_separate_sysfs_files(int maj, int min,
  static int parse_config_sysfs_file(int maj, int min,
                                     drmPciDeviceInfoPtr device)
  {
-    char path[PATH_MAX + 1];
+    char path[PATH_MAX + 1], *real_path;
      unsigned char config[64];
      int fd, ret;
- snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/config", maj, min);
+    real_path = get_real_pci_path(maj, min);
+    if (!real_path)
+        return -ENOENT;
+
+    snprintf(path, PATH_MAX, "%s/config", real_path);
      fd = open(path, O_RDONLY);
-    if (fd < 0)
+    if (fd < 0) {
+        free(real_path);
          return -errno;
+    }
ret = read(fd, config, sizeof(config));
      close(fd);
-    if (ret < 0)
+    if (ret < 0) {
+        free(real_path);
          return -errno;
+    }
+    free(real_path);
device->vendor_id = config[0] | (config[1] << 8);
      device->device_id = config[2] | (config[3] << 8);

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




[Index of Archives]     [Linux DRI Users]     [Linux Intel Graphics]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux