[igt-dev] [i-g-t 1/6] lib: Interface to close the drm fd

Zbigniew Kempczyński zbigniew.kempczynski at intel.com
Thu May 18 18:35:59 UTC 2023


On Thu, May 04, 2023 at 10:27:21AM +0530, Bhanuprakash Modem wrote:
> Add new helper drm_close_driver() to close the drm fd. So that
> every user could call drm_close_driver() instead of close().
> 
> Signed-off-by: Bhanuprakash Modem <bhanuprakash.modem at intel.com>
> ---
>  lib/drmtest.c | 17 +++++++++++++++++
>  lib/drmtest.h |  1 +
>  2 files changed, 18 insertions(+)
> 
> diff --git a/lib/drmtest.c b/lib/drmtest.c
> index c91a91425..03b5239b2 100644
> --- a/lib/drmtest.c
> +++ b/lib/drmtest.c
> @@ -604,6 +604,23 @@ int drm_open_driver(int chipset)
>  	return fd;
>  }
>  
> +/**
> + * drm_close_driver:
> + * @fd: a drm file descriptor
> + *
> + * Close the drm file descriptor @fd.
> + *
> + * Returns: 0 on success or -1 on error.
> + */
> +int drm_close_driver(int fd)
> +{
> +	/* Avoid closing standard file descriptors (0, 1, and 2). */
> +	if (fd < 3)
> +		return -1;
> +
> +	return close(fd);
> +}

Drm fd also can be 0-2 if one of it was previously closed so I'm
not sure you should check this way. Maybe readlink() or sth else?

--
Zbigniew

> +
>  /**
>   * drm_open_driver_master:
>   * @chipset: OR'd flags for each chipset to search, eg. #DRIVER_INTEL
> diff --git a/lib/drmtest.h b/lib/drmtest.h
> index 3c88b85c6..2b7ffc765 100644
> --- a/lib/drmtest.h
> +++ b/lib/drmtest.h
> @@ -105,6 +105,7 @@ int drm_open_driver_render(int chipset);
>  int __drm_open_driver_another(int idx, int chipset);
>  int __drm_open_driver(int chipset);
>  int __drm_open_driver_render(int chipset);
> +int drm_close_driver(int fd);
>  
>  void igt_require_amdgpu(int fd);
>  void igt_require_intel(int fd);
> -- 
> 2.40.0
> 


More information about the igt-dev mailing list