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>
This commit is contained in:
Hirokazu Honda 2021-06-10 16:50:25 +09:00 committed by Laurent Pinchart
parent edd70612e5
commit 9143668887
6 changed files with 49 additions and 44 deletions

View file

@ -12,6 +12,7 @@
#include <vector> #include <vector>
#include <libcamera/base/signal.h> #include <libcamera/base/signal.h>
#include <libcamera/base/unique_fd.h>
namespace libcamera { namespace libcamera {
@ -28,8 +29,8 @@ public:
IPCUnixSocket(); IPCUnixSocket();
~IPCUnixSocket(); ~IPCUnixSocket();
int create(); UniqueFD create();
int bind(int fd); int bind(UniqueFD fd);
void close(); void close();
bool isBound() const; bool isBound() const;
@ -49,7 +50,7 @@ private:
void dataNotifier(); void dataNotifier();
int fd_; UniqueFD fd_;
bool headerReceived_; bool headerReceived_;
struct Header header_; struct Header header_;
EventNotifier *notifier_; EventNotifier *notifier_;

View file

@ -31,14 +31,14 @@ IPCPipeUnixSocket::IPCPipeUnixSocket(const char *ipaModulePath,
args.push_back(ipaModulePath); args.push_back(ipaModulePath);
socket_ = std::make_unique<IPCUnixSocket>(); socket_ = std::make_unique<IPCUnixSocket>();
int fd = socket_->create(); UniqueFD fd = socket_->create();
if (fd < 0) { if (!fd.isValid()) {
LOG(IPCPipe, Error) << "Failed to create socket"; LOG(IPCPipe, Error) << "Failed to create socket";
return; return;
} }
socket_->readyRead.connect(this, &IPCPipeUnixSocket::readyRead); socket_->readyRead.connect(this, &IPCPipeUnixSocket::readyRead);
args.push_back(std::to_string(fd)); args.push_back(std::to_string(fd.get()));
fds.push_back(fd); fds.push_back(fd.release());
proc_ = std::make_unique<Process>(); proc_ = std::make_unique<Process>();
int ret = proc_->start(ipaProxyWorkerPath, args, fds); int ret = proc_->start(ipaProxyWorkerPath, args, fds);

View file

@ -7,6 +7,7 @@
#include "libcamera/internal/ipc_unixsocket.h" #include "libcamera/internal/ipc_unixsocket.h"
#include <array>
#include <poll.h> #include <poll.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -68,7 +69,7 @@ LOG_DEFINE_CATEGORY(IPCUnixSocket)
*/ */
IPCUnixSocket::IPCUnixSocket() IPCUnixSocket::IPCUnixSocket()
: fd_(-1), headerReceived_(false), notifier_(nullptr) : headerReceived_(false), notifier_(nullptr)
{ {
} }
@ -86,9 +87,9 @@ IPCUnixSocket::~IPCUnixSocket()
* to the remote process, where it can be used with IPCUnixSocket::bind() to * to the remote process, where it can be used with IPCUnixSocket::bind() to
* bind the remote side socket. * bind the remote side socket.
* *
* \return A file descriptor on success, negative error code on failure * \return A file descriptor. It is valid on success or invalid otherwise.
*/ */
int IPCUnixSocket::create() UniqueFD IPCUnixSocket::create()
{ {
int sockets[2]; int sockets[2];
int ret; int ret;
@ -98,14 +99,18 @@ int IPCUnixSocket::create()
ret = -errno; ret = -errno;
LOG(IPCUnixSocket, Error) LOG(IPCUnixSocket, Error)
<< "Failed to create socket pair: " << strerror(-ret); << "Failed to create socket pair: " << strerror(-ret);
return ret; return {};
} }
ret = bind(sockets[0]); std::array<UniqueFD, 2> socketFds{
if (ret) UniqueFD(sockets[0]),
return ret; UniqueFD(sockets[1]),
};
return sockets[1]; if (bind(std::move(socketFds[0])) < 0)
return {};
return std::move(socketFds[1]);
} }
/** /**
@ -118,13 +123,13 @@ int IPCUnixSocket::create()
* *
* \return 0 on success or a negative error code otherwise * \return 0 on success or a negative error code otherwise
*/ */
int IPCUnixSocket::bind(int fd) int IPCUnixSocket::bind(UniqueFD fd)
{ {
if (isBound()) if (isBound())
return -EINVAL; return -EINVAL;
fd_ = fd; fd_ = std::move(fd);
notifier_ = new EventNotifier(fd_, EventNotifier::Read); notifier_ = new EventNotifier(fd_.get(), EventNotifier::Read);
notifier_->activated.connect(this, &IPCUnixSocket::dataNotifier); notifier_->activated.connect(this, &IPCUnixSocket::dataNotifier);
return 0; return 0;
@ -143,9 +148,7 @@ void IPCUnixSocket::close()
delete notifier_; delete notifier_;
notifier_ = nullptr; notifier_ = nullptr;
::close(fd_); fd_.reset();
fd_ = -1;
headerReceived_ = false; headerReceived_ = false;
} }
@ -155,7 +158,7 @@ void IPCUnixSocket::close()
*/ */
bool IPCUnixSocket::isBound() const bool IPCUnixSocket::isBound() const
{ {
return fd_ != -1; return fd_.isValid();
} }
/** /**
@ -182,7 +185,7 @@ int IPCUnixSocket::send(const Payload &payload)
if (!hdr.data && !hdr.fds) if (!hdr.data && !hdr.fds)
return -EINVAL; return -EINVAL;
ret = ::send(fd_, &hdr, sizeof(hdr), 0); ret = ::send(fd_.get(), &hdr, sizeof(hdr), 0);
if (ret < 0) { if (ret < 0) {
ret = -errno; ret = -errno;
LOG(IPCUnixSocket, Error) LOG(IPCUnixSocket, Error)
@ -263,7 +266,7 @@ int IPCUnixSocket::sendData(const void *buffer, size_t length,
if (fds) if (fds)
memcpy(CMSG_DATA(cmsg), fds, num * sizeof(uint32_t)); memcpy(CMSG_DATA(cmsg), fds, num * sizeof(uint32_t));
if (sendmsg(fd_, &msg, 0) < 0) { if (sendmsg(fd_.get(), &msg, 0) < 0) {
int ret = -errno; int ret = -errno;
LOG(IPCUnixSocket, Error) LOG(IPCUnixSocket, Error)
<< "Failed to sendmsg: " << strerror(-ret); << "Failed to sendmsg: " << strerror(-ret);
@ -297,7 +300,7 @@ int IPCUnixSocket::recvData(void *buffer, size_t length,
msg.msg_controllen = cmsg->cmsg_len; msg.msg_controllen = cmsg->cmsg_len;
msg.msg_flags = 0; msg.msg_flags = 0;
if (recvmsg(fd_, &msg, 0) < 0) { if (recvmsg(fd_.get(), &msg, 0) < 0) {
int ret = -errno; int ret = -errno;
if (ret != -EAGAIN) if (ret != -EAGAIN)
LOG(IPCUnixSocket, Error) LOG(IPCUnixSocket, Error)
@ -317,7 +320,7 @@ void IPCUnixSocket::dataNotifier()
if (!headerReceived_) { if (!headerReceived_) {
/* Receive the header. */ /* Receive the header. */
ret = ::recv(fd_, &header_, sizeof(header_), 0); ret = ::recv(fd_.get(), &header_, sizeof(header_), 0);
if (ret < 0) { if (ret < 0) {
ret = -errno; ret = -errno;
LOG(IPCUnixSocket, Error) LOG(IPCUnixSocket, Error)
@ -333,7 +336,7 @@ void IPCUnixSocket::dataNotifier()
* readyRead signal. The notifier will be reenabled by the receive() * readyRead signal. The notifier will be reenabled by the receive()
* function. * function.
*/ */
struct pollfd fds = { fd_, POLLIN, 0 }; struct pollfd fds = { fd_.get(), POLLIN, 0 };
ret = poll(&fds, 1, 0); ret = poll(&fds, 1, 0);
if (ret < 0) if (ret < 0)
return; return;

View file

@ -52,9 +52,9 @@ public:
ipc_.readyRead.connect(this, &UnixSocketTestSlave::readyRead); 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; cerr << "Failed to connect to IPC channel" << endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -359,11 +359,11 @@ protected:
int run() int run()
{ {
int slavefd = ipc_.create(); UniqueFD slavefd = ipc_.create();
if (slavefd < 0) if (!slavefd.isValid())
return TestFail; return TestFail;
if (slaveStart(slavefd)) { if (slaveStart(slavefd.release())) {
cerr << "Failed to start slave" << endl; cerr << "Failed to start slave" << endl;
return TestFail; return TestFail;
} }
@ -495,9 +495,9 @@ private:
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc == 2) { if (argc == 2) {
int ipcfd = std::stoi(argv[1]); UniqueFD ipcfd = UniqueFD(std::stoi(argv[1]));
UnixSocketTestSlave slave; UnixSocketTestSlave slave;
return slave.run(ipcfd); return slave.run(std::move(ipcfd));
} }
UnixSocketTest test; UnixSocketTest test;

View file

@ -49,9 +49,9 @@ public:
ipc_.readyRead.connect(this, &UnixSocketTestIPCSlave::readyRead); 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; cerr << "Failed to connect to IPC channel" << endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -222,9 +222,9 @@ int main(int argc, char **argv)
{ {
/* IPCPipeUnixSocket passes IPA module path in argv[1] */ /* IPCPipeUnixSocket passes IPA module path in argv[1] */
if (argc == 3) { if (argc == 3) {
int ipcfd = std::stoi(argv[2]); UniqueFD ipcfd = UniqueFD(std::stoi(argv[2]));
UnixSocketTestIPCSlave slave; UnixSocketTestIPCSlave slave;
return slave.run(ipcfd); return slave.run(std::move(ipcfd));
} }
UnixSocketTestIPC test; UnixSocketTestIPC test;

View file

@ -27,8 +27,9 @@
#include <libcamera/logging.h> #include <libcamera/logging.h>
#include <libcamera/base/event_dispatcher.h> #include <libcamera/base/event_dispatcher.h>
#include <libcamera/base/thread.h>
#include <libcamera/base/log.h> #include <libcamera/base/log.h>
#include <libcamera/base/thread.h>
#include <libcamera/base/unique_fd.h>
#include "libcamera/internal/camera_sensor.h" #include "libcamera/internal/camera_sensor.h"
#include "libcamera/internal/control_serializer.h" #include "libcamera/internal/control_serializer.h"
@ -122,9 +123,9 @@ public:
} }
} }
int init(std::unique_ptr<IPAModule> &ipam, int socketfd) int init(std::unique_ptr<IPAModule> &ipam, UniqueFD socketfd)
{ {
if (socket_.bind(socketfd) < 0) { if (socket_.bind(std::move(socketfd)) < 0) {
LOG({{proxy_worker_name}}, Error) LOG({{proxy_worker_name}}, Error)
<< "IPC socket binding failed"; << "IPC socket binding failed";
return EXIT_FAILURE; return EXIT_FAILURE;
@ -203,10 +204,10 @@ int main(int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
int fd = std::stoi(argv[2]); UniqueFD fd(std::stoi(argv[2]));
LOG({{proxy_worker_name}}, Info) LOG({{proxy_worker_name}}, Info)
<< "Starting worker for IPA module " << argv[1] << "Starting worker for IPA module " << argv[1]
<< " with IPC fd = " << fd; << " with IPC fd = " << fd.get();
std::unique_ptr<IPAModule> ipam = std::make_unique<IPAModule>(argv[1]); std::unique_ptr<IPAModule> ipam = std::make_unique<IPAModule>(argv[1]);
if (!ipam->isValid() || !ipam->load()) { if (!ipam->isValid() || !ipam->load()) {
@ -228,7 +229,7 @@ int main(int argc, char **argv)
} }
{{proxy_worker_name}} proxyWorker; {{proxy_worker_name}} proxyWorker;
int ret = proxyWorker.init(ipam, fd); int ret = proxyWorker.init(ipam, std::move(fd));
if (ret < 0) { if (ret < 0) {
LOG({{proxy_worker_name}}, Error) LOG({{proxy_worker_name}}, Error)
<< "Failed to initialize proxy worker"; << "Failed to initialize proxy worker";