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

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


On Tue, May 16, 2023 at 10:20:39PM +0530, Bhanuprakash Modem wrote:
> Add new helper drm_close_driver() to close the drm fd. Now
> every user could call drm_close_driver() instead of close().
> 
> V2: - Fix commit message
>     - Add debug prints
> 
> Signed-off-by: Bhanuprakash Modem <bhanuprakash.modem at intel.com>
> ---
>  lib/drmtest.c | 20 ++++++++++++++++++++
>  lib/drmtest.h |  1 +
>  2 files changed, 21 insertions(+)
> 
> diff --git a/lib/drmtest.c b/lib/drmtest.c
> index 7630bb793..41ad2694c 100644
> --- a/lib/drmtest.c
> +++ b/lib/drmtest.c
> @@ -605,6 +605,26 @@ 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) {
> +		igt_debug("Don't attempt to close standard/invalid file "
> +			  "descriptor: %d\n", fd);
> +		return -1;
> +	}

Maybe you shouldn't allow passing invalid descriptor at all and assert?

igt_assert(fd >= 0);

?

We can avoid situation when test will blow up long time after this code.

If you want to verify your fd is really drm fd just check what fd is
leading to. Like readlink() with checking /dev/dri/card|render or sth.

--
Zbigniew

> +
> +	return close(fd);
> +}
> +
>  /**
>   * 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 ae86ee19a..9c3ea5d14 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);
>  
>  int drm_reopen_driver(int fd);
>  
> -- 
> 2.40.0
> 


More information about the igt-dev mailing list