[libcamera-devel,v2,06/10] libcamera: V4L2Device: Use ScopedFD for a file descriptor
diff mbox series

Message ID 20210610075027.523672-7-hiroh@chromium.org
State Superseded
Headers show
Series
  • Introduce ScopedFD
Related show

Commit Message

Hirokazu Honda June 10, 2021, 7:50 a.m. UTC
Manages a file descriptor owned by V4L2Device for a v4l2 device node
by ScopedFD.

Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
---
 include/libcamera/internal/v4l2_device.h |  9 ++++----
 src/libcamera/v4l2_device.cpp            | 26 ++++++++++--------------
 src/libcamera/v4l2_videodevice.cpp       | 15 ++++++--------
 3 files changed, 22 insertions(+), 28 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h
index b82e2a14..1d9bc506 100644
--- a/include/libcamera/internal/v4l2_device.h
+++ b/include/libcamera/internal/v4l2_device.h
@@ -14,6 +14,7 @@ 
 #include <linux/videodev2.h>
 
 #include <libcamera/controls.h>
+#include <libcamera/scoped_fd.h>
 #include <libcamera/signal.h>
 #include <libcamera/span.h>
 
@@ -27,7 +28,7 @@  class V4L2Device : protected Loggable
 {
 public:
 	void close();
-	bool isOpen() const { return fd_ != -1; }
+	bool isOpen() const { return fd_.isValid(); }
 
 	const ControlInfoMap &controls() const { return controls_; }
 
@@ -49,11 +50,11 @@  protected:
 	~V4L2Device();
 
 	int open(unsigned int flags);
-	int setFd(int fd);
+	int setFd(ScopedFD fd);
 
 	int ioctl(unsigned long request, void *argp);
 
-	int fd() const { return fd_; }
+	int fd() const { return fd_.get(); }
 
 private:
 	void listControls();
@@ -66,7 +67,7 @@  private:
 	std::vector<std::unique_ptr<ControlId>> controlIds_;
 	ControlInfoMap controls_;
 	std::string deviceNode_;
-	int fd_;
+	ScopedFD fd_;
 
 	EventNotifier *fdEventNotifier_;
 	bool frameStartEnabled_;
diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
index b39c6266..c6c36359 100644
--- a/src/libcamera/v4l2_device.cpp
+++ b/src/libcamera/v4l2_device.cpp
@@ -119,7 +119,7 @@  ControlInfo v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl)
  * at open() time, and the \a logTag to prefix log messages with.
  */
 V4L2Device::V4L2Device(const std::string &deviceNode)
-	: deviceNode_(deviceNode), fd_(-1), fdEventNotifier_(nullptr),
+	: deviceNode_(deviceNode), fdEventNotifier_(nullptr),
 	  frameStartEnabled_(false)
 {
 }
@@ -147,15 +147,14 @@  int V4L2Device::open(unsigned int flags)
 		return -EBUSY;
 	}
 
-	int ret = syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags);
-	if (ret < 0) {
-		ret = -errno;
+	ScopedFD fd(syscall(SYS_openat, AT_FDCWD, deviceNode_.c_str(), flags));
+	if (!fd.isValid()) {
 		LOG(V4L2, Error) << "Failed to open V4L2 device: "
-				 << strerror(-ret);
-		return ret;
+				 << strerror(errno);
+		return errno;
 	}
 
-	setFd(ret);
+	setFd(std::move(fd));
 
 	listControls();
 
@@ -178,14 +177,14 @@  int V4L2Device::open(unsigned int flags)
  *
  * \return 0 on success or a negative error code otherwise
  */
-int V4L2Device::setFd(int fd)
+int V4L2Device::setFd(ScopedFD fd)
 {
 	if (isOpen())
 		return -EBUSY;
 
-	fd_ = fd;
+	fd_ = std::move(fd);
 
-	fdEventNotifier_ = new EventNotifier(fd_, EventNotifier::Exception);
+	fdEventNotifier_ = new EventNotifier(fd_.get(), EventNotifier::Exception);
 	fdEventNotifier_->activated.connect(this, &V4L2Device::eventAvailable);
 	fdEventNotifier_->setEnabled(false);
 
@@ -204,10 +203,7 @@  void V4L2Device::close()
 
 	delete fdEventNotifier_;
 
-	if (::close(fd_) < 0)
-		LOG(V4L2, Error) << "Failed to close V4L2 device: "
-				 << strerror(errno);
-	fd_ = -1;
+	fd_.reset();
 }
 
 /**
@@ -506,7 +502,7 @@  int V4L2Device::ioctl(unsigned long request, void *argp)
 	 * Printing out an error message is usually better performed
 	 * in the caller, which can provide more context.
 	 */
-	if (::ioctl(fd_, request, argp) < 0)
+	if (::ioctl(fd_.get(), request, argp) < 0)
 		return -errno;
 
 	return 0;
diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index 12c09dc7..ce6ad8b5 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -22,6 +22,7 @@ 
 #include <linux/version.h>
 
 #include <libcamera/file_descriptor.h>
+#include <libcamera/scoped_fd.h>
 
 #include "libcamera/internal/event_notifier.h"
 #include "libcamera/internal/log.h"
@@ -603,22 +604,18 @@  int V4L2VideoDevice::open()
  */
 int V4L2VideoDevice::open(int handle, enum v4l2_buf_type type)
 {
-	int ret;
-	int newFd;
 
-	newFd = dup(handle);
-	if (newFd < 0) {
-		ret = -errno;
+	ScopedFD newFd(dup(handle));
+	if (!newFd.isValid()) {
 		LOG(V4L2, Error) << "Failed to duplicate file handle: "
-				 << strerror(-ret);
-		return ret;
+				 << strerror(errno);
+		return -errno;
 	}
 
-	ret = V4L2Device::setFd(newFd);
+	int ret = V4L2Device::setFd(std::move(newFd));
 	if (ret < 0) {
 		LOG(V4L2, Error) << "Failed to set file handle: "
 				 << strerror(-ret);
-		::close(newFd);
 		return ret;
 	}