libcamera: device_enumerator: exclude uvcvideo device when enumerate
diff mbox series

Message ID 20240326083804.3718161-1-anle.pan@nxp.com
State Rejected
Headers show
Series
  • libcamera: device_enumerator: exclude uvcvideo device when enumerate
Related show

Commit Message

Anle Pan March 26, 2024, 8:38 a.m. UTC
Camera service startup crash when camera and
usb camera are connected at the same time,
service will always reboot due to the null pointer
in populateEntities.

In this case, a UVC device was used to populate the
related entities, pads and links, which was not reasonable.

To fix the issue, ensure the media node is not
a "uvcvideo" device when enumerate before createDevice.

Change-Id: I656a542e83449df287d34a4e358ae2e26e784f17
Signed-off-by: Anle Pan <anle.pan@nxp.com>
---
 src/libcamera/device_enumerator_sysfs.cpp | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

Comments

Jacopo Mondi March 26, 2024, 9:03 a.m. UTC | #1
Hi Anle

On Tue, Mar 26, 2024 at 05:38:04PM +0900, Anle Pan wrote:
> Camera service startup crash when camera and
> usb camera are connected at the same time,
> service will always reboot due to the null pointer
> in populateEntities.

This is weird, libcamera has been used with uvcvideo devices since the
very beginning. As far as I understand, this is not related to Android
or its CameraService, right ? Just loading the library fails ?

>
> In this case, a UVC device was used to populate the
> related entities, pads and links, which was not reasonable.

Not a UVC expert, but why this is not reasonable ? uvc devices have a
media graph with entities, links and pads like every other device,
don't they ?

>
> To fix the issue, ensure the media node is not
> a "uvcvideo" device when enumerate before createDevice.

So this is disabling uvc support in libcamera completely, preventing
uvcvideo devices from being enumerated ?

What about uvcvideo users ? :)

>
> Change-Id: I656a542e83449df287d34a4e358ae2e26e784f17
> Signed-off-by: Anle Pan <anle.pan@nxp.com>
> ---
>  src/libcamera/device_enumerator_sysfs.cpp | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
>
> diff --git a/src/libcamera/device_enumerator_sysfs.cpp b/src/libcamera/device_enumerator_sysfs.cpp
> index 9979d68a..224ec8c8 100644
> --- a/src/libcamera/device_enumerator_sysfs.cpp
> +++ b/src/libcamera/device_enumerator_sysfs.cpp
> @@ -73,6 +73,27 @@ int DeviceEnumeratorSysfs::enumerate()
>  			continue;
>  		}
>
> +		/* Check that the device node not a uvcvideo. */
> +		struct media_device_info devinfo;
> +		int fd = open(devnode.c_str(), O_RDWR | O_CLOEXEC);
> +		if (fd < 0) {
> +			LOG(DeviceEnumerator, Warning)
> +				<< "Failed to open media device:" + devnode
> +				<< " (" << strerror(errno) << "), skipping";
> +			continue;
> +		}
> +		int retval = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &devinfo);
> +		close(fd);
> +		if (retval != 0) {
> +			LOG(DeviceEnumerator, Warning)
> +				<< "Failed to get info from device:" + devnode
> +				<< " (" << strerror(errno) << "), skipping";
> +			continue;
> +		}
> +		if (strstr("uvcvideo", devinfo.driver)) {
> +			continue;
> +		}
> +
>  		std::unique_ptr<MediaDevice> media = createDevice(devnode);
>  		if (!media)
>  			continue;
> --
> 2.34.1
>
Anle Pan March 26, 2024, 10:44 a.m. UTC | #2
Hi Jacopo,

    It is the code added by our own project for debug that causes the crash. Not related to libcamera, please omit the patch. Thanks.

Best Regards
Anle Pan

-----Original Message-----
From: Jacopo Mondi <jacopo.mondi@ideasonboard.com> 
Sent: 2024年3月26日 17:03
To: Anle Pan <anle.pan@nxp.com>
Cc: libcamera-devel@lists.libcamera.org; Hui Fang <hui.fang@nxp.com>
Subject: [EXT] Re: [PATCH] libcamera: device_enumerator: exclude uvcvideo device when enumerate

Caution: This is an external email. Please take care when clicking links or opening attachments. When in doubt, report the message using the 'Report this email' button


Hi Anle

On Tue, Mar 26, 2024 at 05:38:04PM +0900, Anle Pan wrote:
> Camera service startup crash when camera and usb camera are connected 
> at the same time, service will always reboot due to the null pointer 
> in populateEntities.

This is weird, libcamera has been used with uvcvideo devices since the very beginning. As far as I understand, this is not related to Android or its CameraService, right ? Just loading the library fails ?

>
> In this case, a UVC device was used to populate the related entities, 
> pads and links, which was not reasonable.

Not a UVC expert, but why this is not reasonable ? uvc devices have a media graph with entities, links and pads like every other device, don't they ?

>
> To fix the issue, ensure the media node is not a "uvcvideo" device 
> when enumerate before createDevice.

So this is disabling uvc support in libcamera completely, preventing uvcvideo devices from being enumerated ?

What about uvcvideo users ? :)

>
> Change-Id: I656a542e83449df287d34a4e358ae2e26e784f17
> Signed-off-by: Anle Pan <anle.pan@nxp.com>
> ---
>  src/libcamera/device_enumerator_sysfs.cpp | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
>
> diff --git a/src/libcamera/device_enumerator_sysfs.cpp 
> b/src/libcamera/device_enumerator_sysfs.cpp
> index 9979d68a..224ec8c8 100644
> --- a/src/libcamera/device_enumerator_sysfs.cpp
> +++ b/src/libcamera/device_enumerator_sysfs.cpp
> @@ -73,6 +73,27 @@ int DeviceEnumeratorSysfs::enumerate()
>                       continue;
>               }
>
> +             /* Check that the device node not a uvcvideo. */
> +             struct media_device_info devinfo;
> +             int fd = open(devnode.c_str(), O_RDWR | O_CLOEXEC);
> +             if (fd < 0) {
> +                     LOG(DeviceEnumerator, Warning)
> +                             << "Failed to open media device:" + devnode
> +                             << " (" << strerror(errno) << "), skipping";
> +                     continue;
> +             }
> +             int retval = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &devinfo);
> +             close(fd);
> +             if (retval != 0) {
> +                     LOG(DeviceEnumerator, Warning)
> +                             << "Failed to get info from device:" + devnode
> +                             << " (" << strerror(errno) << "), skipping";
> +                     continue;
> +             }
> +             if (strstr("uvcvideo", devinfo.driver)) {
> +                     continue;
> +             }
> +
>               std::unique_ptr<MediaDevice> media = createDevice(devnode);
>               if (!media)
>                       continue;
> --
> 2.34.1
>
Jacopo Mondi March 26, 2024, 10:50 a.m. UTC | #3
No worries, happy you got it solved!

Cheers
   j

On Tue, Mar 26, 2024 at 10:44:34AM +0000, Anle Pan wrote:
> Hi Jacopo,
>
>     It is the code added by our own project for debug that causes the crash. Not related to libcamera, please omit the patch. Thanks.
>
> Best Regards
> Anle Pan
>
> -----Original Message-----
> From: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
> Sent: 2024年3月26日 17:03
> To: Anle Pan <anle.pan@nxp.com>
> Cc: libcamera-devel@lists.libcamera.org; Hui Fang <hui.fang@nxp.com>
> Subject: [EXT] Re: [PATCH] libcamera: device_enumerator: exclude uvcvideo device when enumerate
>
> Caution: This is an external email. Please take care when clicking links or opening attachments. When in doubt, report the message using the 'Report this email' button
>
>
> Hi Anle
>
> On Tue, Mar 26, 2024 at 05:38:04PM +0900, Anle Pan wrote:
> > Camera service startup crash when camera and usb camera are connected
> > at the same time, service will always reboot due to the null pointer
> > in populateEntities.
>
> This is weird, libcamera has been used with uvcvideo devices since the very beginning. As far as I understand, this is not related to Android or its CameraService, right ? Just loading the library fails ?
>
> >
> > In this case, a UVC device was used to populate the related entities,
> > pads and links, which was not reasonable.
>
> Not a UVC expert, but why this is not reasonable ? uvc devices have a media graph with entities, links and pads like every other device, don't they ?
>
> >
> > To fix the issue, ensure the media node is not a "uvcvideo" device
> > when enumerate before createDevice.
>
> So this is disabling uvc support in libcamera completely, preventing uvcvideo devices from being enumerated ?
>
> What about uvcvideo users ? :)
>
> >
> > Change-Id: I656a542e83449df287d34a4e358ae2e26e784f17
> > Signed-off-by: Anle Pan <anle.pan@nxp.com>
> > ---
> >  src/libcamera/device_enumerator_sysfs.cpp | 21 +++++++++++++++++++++
> >  1 file changed, 21 insertions(+)
> >
> > diff --git a/src/libcamera/device_enumerator_sysfs.cpp
> > b/src/libcamera/device_enumerator_sysfs.cpp
> > index 9979d68a..224ec8c8 100644
> > --- a/src/libcamera/device_enumerator_sysfs.cpp
> > +++ b/src/libcamera/device_enumerator_sysfs.cpp
> > @@ -73,6 +73,27 @@ int DeviceEnumeratorSysfs::enumerate()
> >                       continue;
> >               }
> >
> > +             /* Check that the device node not a uvcvideo. */
> > +             struct media_device_info devinfo;
> > +             int fd = open(devnode.c_str(), O_RDWR | O_CLOEXEC);
> > +             if (fd < 0) {
> > +                     LOG(DeviceEnumerator, Warning)
> > +                             << "Failed to open media device:" + devnode
> > +                             << " (" << strerror(errno) << "), skipping";
> > +                     continue;
> > +             }
> > +             int retval = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &devinfo);
> > +             close(fd);
> > +             if (retval != 0) {
> > +                     LOG(DeviceEnumerator, Warning)
> > +                             << "Failed to get info from device:" + devnode
> > +                             << " (" << strerror(errno) << "), skipping";
> > +                     continue;
> > +             }
> > +             if (strstr("uvcvideo", devinfo.driver)) {
> > +                     continue;
> > +             }
> > +
> >               std::unique_ptr<MediaDevice> media = createDevice(devnode);
> >               if (!media)
> >                       continue;
> > --
> > 2.34.1
> >

Patch
diff mbox series

diff --git a/src/libcamera/device_enumerator_sysfs.cpp b/src/libcamera/device_enumerator_sysfs.cpp
index 9979d68a..224ec8c8 100644
--- a/src/libcamera/device_enumerator_sysfs.cpp
+++ b/src/libcamera/device_enumerator_sysfs.cpp
@@ -73,6 +73,27 @@  int DeviceEnumeratorSysfs::enumerate()
 			continue;
 		}
 
+		/* Check that the device node not a uvcvideo. */
+		struct media_device_info devinfo;
+		int fd = open(devnode.c_str(), O_RDWR | O_CLOEXEC);
+		if (fd < 0) {
+			LOG(DeviceEnumerator, Warning)
+				<< "Failed to open media device:" + devnode
+				<< " (" << strerror(errno) << "), skipping";
+			continue;
+		}
+		int retval = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &devinfo);
+		close(fd);
+		if (retval != 0) {
+			LOG(DeviceEnumerator, Warning)
+				<< "Failed to get info from device:" + devnode
+				<< " (" << strerror(errno) << "), skipping";
+			continue;
+		}
+		if (strstr("uvcvideo", devinfo.driver)) {
+			continue;
+		}
+
 		std::unique_ptr<MediaDevice> media = createDevice(devnode);
 		if (!media)
 			continue;