From 91436688874a2450c7da22527c59e34c2053b9ef Mon Sep 17 00:00:00 2001
From: Hirokazu Honda <hiroh@chromium.org>
Date: Thu, 10 Jun 2021 16:50:25 +0900
Subject: libcamera: ipc_unixsocket: Use UniqueFD for a file descriptor

IPCUnixSocket::create() creates two file descriptors. One of
them is stored in IPCUnixSocket and the other is returned to a
caller. This clarifies the ownership using UniqueFD.

Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
---
 test/ipc/unixsocket.cpp     | 14 +++++++-------
 test/ipc/unixsocket_ipc.cpp |  8 ++++----
 2 files changed, 11 insertions(+), 11 deletions(-)

(limited to 'test/ipc')

diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp
index b3568c06..7e90e629 100644
--- a/test/ipc/unixsocket.cpp
+++ b/test/ipc/unixsocket.cpp
@@ -52,9 +52,9 @@ public:
 		ipc_.readyRead.connect(this, &UnixSocketTestSlave::readyRead);
 	}
 
-	int run(int fd)
+	int run(UniqueFD fd)
 	{
-		if (ipc_.bind(fd)) {
+		if (ipc_.bind(std::move(fd))) {
 			cerr << "Failed to connect to IPC channel" << endl;
 			return EXIT_FAILURE;
 		}
@@ -359,11 +359,11 @@ protected:
 
 	int run()
 	{
-		int slavefd = ipc_.create();
-		if (slavefd < 0)
+		UniqueFD slavefd = ipc_.create();
+		if (!slavefd.isValid())
 			return TestFail;
 
-		if (slaveStart(slavefd)) {
+		if (slaveStart(slavefd.release())) {
 			cerr << "Failed to start slave" << endl;
 			return TestFail;
 		}
@@ -495,9 +495,9 @@ private:
 int main(int argc, char **argv)
 {
 	if (argc == 2) {
-		int ipcfd = std::stoi(argv[1]);
+		UniqueFD ipcfd = UniqueFD(std::stoi(argv[1]));
 		UnixSocketTestSlave slave;
-		return slave.run(ipcfd);
+		return slave.run(std::move(ipcfd));
 	}
 
 	UnixSocketTest test;
diff --git a/test/ipc/unixsocket_ipc.cpp b/test/ipc/unixsocket_ipc.cpp
index 1a8d06a1..3ee6017e 100644
--- a/test/ipc/unixsocket_ipc.cpp
+++ b/test/ipc/unixsocket_ipc.cpp
@@ -49,9 +49,9 @@ public:
 		ipc_.readyRead.connect(this, &UnixSocketTestIPCSlave::readyRead);
 	}
 
-	int run(int fd)
+	int run(UniqueFD fd)
 	{
-		if (ipc_.bind(fd)) {
+		if (ipc_.bind(std::move(fd))) {
 			cerr << "Failed to connect to IPC channel" << endl;
 			return EXIT_FAILURE;
 		}
@@ -222,9 +222,9 @@ int main(int argc, char **argv)
 {
 	/* IPCPipeUnixSocket passes IPA module path in argv[1] */
 	if (argc == 3) {
-		int ipcfd = std::stoi(argv[2]);
+		UniqueFD ipcfd = UniqueFD(std::stoi(argv[2]));
 		UnixSocketTestIPCSlave slave;
-		return slave.run(ipcfd);
+		return slave.run(std::move(ipcfd));
 	}
 
 	UnixSocketTestIPC test;
-- 
cgit v1.2.1