diff mbox

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

Message ID 20180625173626.8046-5-emil.l.velikov@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Emil Velikov June 25, 2018, 5:36 p.m. UTC
From: Emil Velikov <emil.velikov@collabora.com>

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@collabora.com>
---
 xf86drm.c | 62 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 50 insertions(+), 12 deletions(-)

Comments

Eric Engestrom June 28, 2018, 10:21 a.m. UTC | #1
On Monday, 2018-06-25 17:39:14 +0000, Emil Velikov wrote:
> From: Emil Velikov <emil.velikov@collabora.com>
> 
> 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@collabora.com>
> ---
>  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);

Why not allocate this on the stack and pass it in?
It would avoid the error handling you had to add in this patch :)

> +
> +    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);
> -- 
> 2.18.0
> 
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel
Eric Engestrom June 28, 2018, 10:23 a.m. UTC | #2
On Thursday, 2018-06-28 11:21:26 +0100, Eric Engestrom wrote:
> On Monday, 2018-06-25 17:39:14 +0000, Emil Velikov wrote:
> > From: Emil Velikov <emil.velikov@collabora.com>
> > 
> > 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@collabora.com>
> > ---
> >  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);
> 
> Why not allocate this on the stack and pass it in?
> It would avoid the error handling you had to add in this patch :)

Sorry, I forgot to add: with that amend, the series is
Reviewed-by: Eric Engestrom <eric@engestrom.ch>

> 
> > +
> > +    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);
> > -- 
> > 2.18.0
> > 
> > _______________________________________________
> > dri-devel mailing list
> > dri-devel@lists.freedesktop.org
> > https://lists.freedesktop.org/mailman/listinfo/dri-devel
Robert Foss June 28, 2018, 4:06 p.m. UTC | #3
Feel free to add my r-b to this patch.

On 2018-06-25 19:36, Emil Velikov wrote:
> From: Emil Velikov <emil.velikov@collabora.com>
> 
> 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@collabora.com>
> ---
>   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);
>
Emil Velikov June 28, 2018, 4:42 p.m. UTC | #4
Hi Eric,

On 28 June 2018 at 11:21, Eric Engestrom <eric@engestrom.ch> wrote:

>> @@ -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);
>
> Why not allocate this on the stack and pass it in?
> It would avoid the error handling you had to add in this patch :)
>
As always, thanks for having a look.

I was mostly hesitant since some callers already have a char foo[PATH_MAX +1].
Thinking about it - the default stack size should be enough to
accommodate an extra ~4k bytes.

Will fix.

Thanks
Emil
diff mbox

Patch

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);