v4l2: v4l2_compat: Fix ioctl() prototype with musl C library
diff mbox series

Message ID 20240721213111.24638-1-laurent.pinchart@ideasonboard.com
State Accepted
Commit 83b3141178282b11665c4485a33b9bc3e61d7c18
Headers show
Series
  • v4l2: v4l2_compat: Fix ioctl() prototype with musl C library
Related show

Commit Message

Laurent Pinchart July 21, 2024, 9:31 p.m. UTC
The musl C library, as well as the POSIX standard, define the ioctl()
function's request argument as an int. glibc and uclibc, on the other
hand, define it as an unsigned long.

This difference between the function prototype and the implementation in
the V4L2 adaptation layer causes a compilation error with musl. Fix it
by detecting the function prototype and declaring the libcamera ioctl()
handler accordingly.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 meson.build              | 9 +++++++++
 src/v4l2/v4l2_compat.cpp | 4 ++++
 2 files changed, 13 insertions(+)


base-commit: 4661a7eedf81f00212956f0d396196b3eb4a417b

Comments

Kieran Bingham July 22, 2024, 8:16 a.m. UTC | #1
Quoting Laurent Pinchart (2024-07-21 22:31:11)
> The musl C library, as well as the POSIX standard, define the ioctl()
> function's request argument as an int. glibc and uclibc, on the other
> hand, define it as an unsigned long.

I love how standards make things ... all the same ...

> This difference between the function prototype and the implementation in
> the V4L2 adaptation layer causes a compilation error with musl. Fix it
> by detecting the function prototype and declaring the libcamera ioctl()
> handler accordingly.

I guess we need an alpine linux target in our CI matrix...



> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  meson.build              | 9 +++++++++
>  src/v4l2/v4l2_compat.cpp | 4 ++++
>  2 files changed, 13 insertions(+)
> 
> diff --git a/meson.build b/meson.build
> index 2acd8c3e8cfe..ccd766a1e98c 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -90,6 +90,15 @@ if cc.has_header_symbol('sys/mman.h', 'memfd_create', prefix : '#define _GNU_SOU
>      config_h.set('HAVE_MEMFD_CREATE', 1)
>  endif
>  
> +ioctl_posix_test = '''
> +#include <sys/ioctl.h>
> +int ioctl (int, int, ...);
> +'''
> +
> +if cc.compiles(ioctl_posix_test)
> +    config_h.set('HAVE_POSIX_IOCTL', 1)
> +endif
> +

I think we should start thinking about how to make our top level
meson.build more modular with so much 'compatibility' checks ... but
that's a tomorrow problem.

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

>  if cc.has_header_symbol('stdlib.h', 'secure_getenv', prefix : '#define _GNU_SOURCE')
>      config_h.set('HAVE_SECURE_GETENV', 1)
>  endif
> diff --git a/src/v4l2/v4l2_compat.cpp b/src/v4l2/v4l2_compat.cpp
> index 66468bf38d5b..6c9dca7201a3 100644
> --- a/src/v4l2/v4l2_compat.cpp
> +++ b/src/v4l2/v4l2_compat.cpp
> @@ -154,7 +154,11 @@ LIBCAMERA_PUBLIC int munmap(void *addr, size_t length)
>         return V4L2CompatManager::instance()->munmap(addr, length);
>  }
>  
> +#if HAVE_POSIX_IOCTL
> +LIBCAMERA_PUBLIC int ioctl(int fd, int request, ...)
> +#else
>  LIBCAMERA_PUBLIC int ioctl(int fd, unsigned long request, ...)
> +#endif
>  {
>         void *arg;
>         extract_va_arg(void *, arg, request);
> 
> base-commit: 4661a7eedf81f00212956f0d396196b3eb4a417b
> -- 
> Regards,
> 
> Laurent Pinchart
>
Paul Elder July 25, 2024, 7:26 a.m. UTC | #2
On Mon, Jul 22, 2024 at 12:31:11AM +0300, Laurent Pinchart wrote:
> The musl C library, as well as the POSIX standard, define the ioctl()
> function's request argument as an int. glibc and uclibc, on the other
> hand, define it as an unsigned long.
> 
> This difference between the function prototype and the implementation in
> the V4L2 adaptation layer causes a compilation error with musl. Fix it
> by detecting the function prototype and declaring the libcamera ioctl()
> handler accordingly.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>

> ---
>  meson.build              | 9 +++++++++
>  src/v4l2/v4l2_compat.cpp | 4 ++++
>  2 files changed, 13 insertions(+)
> 
> diff --git a/meson.build b/meson.build
> index 2acd8c3e8cfe..ccd766a1e98c 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -90,6 +90,15 @@ if cc.has_header_symbol('sys/mman.h', 'memfd_create', prefix : '#define _GNU_SOU
>      config_h.set('HAVE_MEMFD_CREATE', 1)
>  endif
>  
> +ioctl_posix_test = '''
> +#include <sys/ioctl.h>
> +int ioctl (int, int, ...);
> +'''
> +
> +if cc.compiles(ioctl_posix_test)
> +    config_h.set('HAVE_POSIX_IOCTL', 1)
> +endif
> +
>  if cc.has_header_symbol('stdlib.h', 'secure_getenv', prefix : '#define _GNU_SOURCE')
>      config_h.set('HAVE_SECURE_GETENV', 1)
>  endif
> diff --git a/src/v4l2/v4l2_compat.cpp b/src/v4l2/v4l2_compat.cpp
> index 66468bf38d5b..6c9dca7201a3 100644
> --- a/src/v4l2/v4l2_compat.cpp
> +++ b/src/v4l2/v4l2_compat.cpp
> @@ -154,7 +154,11 @@ LIBCAMERA_PUBLIC int munmap(void *addr, size_t length)
>  	return V4L2CompatManager::instance()->munmap(addr, length);
>  }
>  
> +#if HAVE_POSIX_IOCTL
> +LIBCAMERA_PUBLIC int ioctl(int fd, int request, ...)
> +#else
>  LIBCAMERA_PUBLIC int ioctl(int fd, unsigned long request, ...)
> +#endif
>  {
>  	void *arg;
>  	extract_va_arg(void *, arg, request);
> 
> base-commit: 4661a7eedf81f00212956f0d396196b3eb4a417b

Patch
diff mbox series

diff --git a/meson.build b/meson.build
index 2acd8c3e8cfe..ccd766a1e98c 100644
--- a/meson.build
+++ b/meson.build
@@ -90,6 +90,15 @@  if cc.has_header_symbol('sys/mman.h', 'memfd_create', prefix : '#define _GNU_SOU
     config_h.set('HAVE_MEMFD_CREATE', 1)
 endif
 
+ioctl_posix_test = '''
+#include <sys/ioctl.h>
+int ioctl (int, int, ...);
+'''
+
+if cc.compiles(ioctl_posix_test)
+    config_h.set('HAVE_POSIX_IOCTL', 1)
+endif
+
 if cc.has_header_symbol('stdlib.h', 'secure_getenv', prefix : '#define _GNU_SOURCE')
     config_h.set('HAVE_SECURE_GETENV', 1)
 endif
diff --git a/src/v4l2/v4l2_compat.cpp b/src/v4l2/v4l2_compat.cpp
index 66468bf38d5b..6c9dca7201a3 100644
--- a/src/v4l2/v4l2_compat.cpp
+++ b/src/v4l2/v4l2_compat.cpp
@@ -154,7 +154,11 @@  LIBCAMERA_PUBLIC int munmap(void *addr, size_t length)
 	return V4L2CompatManager::instance()->munmap(addr, length);
 }
 
+#if HAVE_POSIX_IOCTL
+LIBCAMERA_PUBLIC int ioctl(int fd, int request, ...)
+#else
 LIBCAMERA_PUBLIC int ioctl(int fd, unsigned long request, ...)
+#endif
 {
 	void *arg;
 	extract_va_arg(void *, arg, request);