[igt-dev] [PATCH i-g-t 5/6] lib/igt_sysfs: Simplify obtaining sysfs path

Chris Wilson chris at chris-wilson.co.uk
Wed Mar 13 14:09:23 UTC 2019


Quoting Michał Winiarski (2019-03-13 12:53:31)
> Now that we've extracted card index, we no longer have the need to
> iterate over device nodes.
> 
> Signed-off-by: Michał Winiarski <michal.winiarski at intel.com>
> ---
>  lib/igt_sysfs.c | 29 +++++++----------------------
>  1 file changed, 7 insertions(+), 22 deletions(-)
> 
> diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c
> index 13a2ee04..974bf45a 100644
> --- a/lib/igt_sysfs.c
> +++ b/lib/igt_sysfs.c
> @@ -100,6 +100,7 @@ static int writeN(int fd, const char *buf, int len)
>  char *igt_sysfs_path(int device, char *path, int pathlen)
>  {
>         struct stat st;
> +       int ret;
>  
>         if (device < 0)
>                 return NULL;
> @@ -107,30 +108,14 @@ char *igt_sysfs_path(int device, char *path, int pathlen)
>         if (fstat(device, &st) || !S_ISCHR(st.st_mode))
>                 return NULL;
>  
> -       for (int n = 0; n < 16; n++) {
> -               int len, ret, maj, min;
> -               FILE *file;
> -
> -               len = snprintf(path, pathlen, "/sys/class/drm/card%d", n);
> -
> -               sprintf(path + len, "/dev");
> -               file = fopen(path, "r");
> -               if (!file)
> -                       continue;
> -
> -               ret = fscanf(file, "%d:%d", &maj, &min);
> -               fclose(file);
> +       snprintf(path, pathlen, "/sys/dev/char/%d:%d",
> +                major(st.st_rdev), minor(st.st_rdev));

Neat.

> -               if (ret != 2 || major(st.st_rdev) != maj ||
> -                   minor(st.st_rdev) != min)
> -                       continue;
> -
> -               path[len] = '\0';
> -
> -               return path;
> -       }
> +       ret = access(path, F_OK);
> +       if (ret)
> +               return NULL;

You don't even need to keep ret.

Reviewed-by: Chris Wilson <chris at chris-wilson.co.uk>
-Chris


More information about the igt-dev mailing list