[libcamera-devel] [PATCH v2] libcamera: v4l2_device: openat(2) with O_CLOEXEC to cleanup after exec(3)

Laurent Pinchart laurent.pinchart at ideasonboard.com
Tue Mar 21 00:56:56 CET 2023


Hi Elias,

Thank you for the patch.

On Sun, Mar 12, 2023 at 03:22:05PM -0600, Elias Naur via libcamera-devel wrote:
> It's generally a good idea to openat(2) with O_CLOEXEC, but this patch
> also fixes a real (corner-)case: I have an excutable that (1) uses
> v4l2-compat to drive a RPi camera, (2) self-updates through exec(3).
> Without O_CLOEXEC of the kernel devices, an update while the
> camera is opened will result in -EBUSY errors when the update tries to
> open the camera.

I agree about the change, but I wonder what returns -EBUSY, as V4L2
allows opening device nodes multiple times. Do you get a -EBUSY error
from V4L2Device::open() after exec(), or from a different location ? If
the error comes from V4L2Device::open(), what device does it come from ?

> Signed-off-by: Elias Naur <mail at eliasnaur.com>
> ---
> 
> This update fixes a style issue raised through review and clarifies the
>  motivatition for the fix.
> 
>  src/libcamera/v4l2_device.cpp | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git src/libcamera/v4l2_device.cpp src/libcamera/v4l2_device.cpp
> index 57a88d96..9eb26839 100644
> --- src/libcamera/v4l2_device.cpp
> +++ src/libcamera/v4l2_device.cpp
> @@ -86,7 +86,7 @@ int V4L2Device::open(unsigned int flags)
>  		return -EBUSY;
>  	}
>  
> -	UniqueFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags));
> +	UniqueFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags | O_CLOEXEC));
>  	if (!fd.isValid()) {
>  		int ret = -errno;
>  		LOG(V4L2, Error) << "Failed to open V4L2 device '"

-- 
Regards,

Laurent Pinchart


More information about the libcamera-devel mailing list