{"id":11944,"url":"https://patchwork.libcamera.org/api/1.1/patches/11944/?format=json","web_url":"https://patchwork.libcamera.org/patch/11944/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210415083843.3399502-8-hiroh@chromium.org>","date":"2021-04-15T08:38:41","name":"[libcamera-devel,RFC,08/10] libcamera: IPCUnixSocket: Use ScopedFD for a file descriptor","commit_ref":null,"pull_url":null,"state":"changes-requested","archived":false,"hash":"ce79c72bb9b2ce6dfb4be1e8ca1f44372e084e19","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/1.1/people/63/?format=json","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/11944/mbox/","series":[{"id":1933,"url":"https://patchwork.libcamera.org/api/1.1/series/1933/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1933","date":"2021-04-15T08:38:35","name":null,"version":1,"mbox":"https://patchwork.libcamera.org/series/1933/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/11944/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/11944/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 78089BD224\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 15 Apr 2021 08:39:02 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 24F1468821;\n\tThu, 15 Apr 2021 10:39:02 +0200 (CEST)","from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com\n\t[IPv6:2607:f8b0:4864:20::42e])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 95D706880B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 10:39:00 +0200 (CEST)","by mail-pf1-x42e.google.com with SMTP id m11so15592314pfc.11\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 15 Apr 2021 01:39:00 -0700 (PDT)","from hiroh2.tok.corp.google.com\n\t([2401:fa00:8f:2:84f5:7981:dfbe:8f02])\n\tby smtp.gmail.com with ESMTPSA id\n\t205sm1520258pfc.201.2021.04.15.01.38.57\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 15 Apr 2021 01:38:58 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"QhCyrM8/\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=L2GJ5FdpixTlkZnE5WQQJ3Cr2SvhnXOuRKhPG46hJfw=;\n\tb=QhCyrM8/c+4VbnwAvyuohqHLc8ATnk7MJ2H5s1GWQEOY56DGWSfdYLFx5YqJ/k43am\n\tubSlIHgJvAcotvcOwcxu3BocDhSJ+aRv/qsWLQkiJbQL/qEYp3UZD3LWYgDW9xghRvbI\n\tq0sXZS5VEErP/m1KBKBVurn0yEcuZbrts271Q=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=L2GJ5FdpixTlkZnE5WQQJ3Cr2SvhnXOuRKhPG46hJfw=;\n\tb=REfpOkyA5h6m+43NcPGUgkAWCnoXdvdyzLffKu8tXIR4cRzSopdJPCiB6bnsg39+DS\n\tMLdpx3YWZi0shUNyskaUcLZ8KKUt6mHYUBZMH+6Z4KxG+FXRg7ByYDYKEMMrQGqnCGNy\n\tAcJuTpK/GK0L5iE9c+lS0Sf5rK3yP+oxQhwnl7MxWhTmiX+11ZXcV6DqWp+gPXJYbTYU\n\tB6H6o9igVWOjhGiD0xesB4Dnd9nbc8jro6EPE6GCgO6Pzi5VG3JbNYQKafUtc2wTJp9Z\n\tZguKSGVwGWEiQ4fGeevDUZ30CgPl4WSMccE364tpSQRRf3TXSN2K3EdhifgUSsuN4s6O\n\tdoww==","X-Gm-Message-State":"AOAM532jrBwpGFBE7lb04tyk4AawSwSymuTx8aQBBUiylR3wg3DzJJYQ\n\t0unNMZr9EzJUBBG9F+GI7mdWV+xIOOK11Q==","X-Google-Smtp-Source":"ABdhPJzlNSHIBAGITEaV+xsYWQHirm6xq22xFsXPyRZkKdH/C+w3qpdeAX+lHnx1JlAZ4V0uFTIG7Q==","X-Received":"by 2002:a63:fb15:: with SMTP id\n\to21mr2461179pgh.337.1618475938994; \n\tThu, 15 Apr 2021 01:38:58 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 15 Apr 2021 17:38:41 +0900","Message-Id":"<20210415083843.3399502-8-hiroh@chromium.org>","X-Mailer":"git-send-email 2.31.1.368.gbe11c130af-goog","In-Reply-To":"<20210415083843.3399502-1-hiroh@chromium.org>","References":"<20210415083843.3399502-1-hiroh@chromium.org>","MIME-Version":"1.0","Subject":"[libcamera-devel] [RFC PATCH 08/10] libcamera: IPCUnixSocket: Use\n\tScopedFD for a file descriptor","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"IPCUnixSocket::create() creates two file descriptors. One of\nthem is stored in IPCUnixSocket and the other is returned to a\ncaller. This clarifies the ownership using ScopedFD.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\n---\n include/libcamera/internal/ipc_unixsocket.h |  5 +--\n src/libcamera/ipc_pipe_unixsocket.cpp       |  8 ++---\n src/libcamera/ipc_unixsocket.cpp            | 40 ++++++++++-----------\n test/ipc/unixsocket.cpp                     |  6 ++--\n 4 files changed, 30 insertions(+), 29 deletions(-)","diff":"diff --git a/include/libcamera/internal/ipc_unixsocket.h b/include/libcamera/internal/ipc_unixsocket.h\nindex e871b650..c06fee47 100644\n--- a/include/libcamera/internal/ipc_unixsocket.h\n+++ b/include/libcamera/internal/ipc_unixsocket.h\n@@ -12,6 +12,7 @@\n #include <sys/types.h>\n #include <vector>\n \n+#include <libcamera/scoped_file_descriptor.h>\n #include <libcamera/signal.h>\n \n namespace libcamera {\n@@ -29,7 +30,7 @@ public:\n \tIPCUnixSocket();\n \t~IPCUnixSocket();\n \n-\tint create();\n+\tScopedFD create();\n \tint bind(int fd);\n \tvoid close();\n \tbool isBound() const;\n@@ -50,7 +51,7 @@ private:\n \n \tvoid dataNotifier(EventNotifier *notifier);\n \n-\tint fd_;\n+\tScopedFD fd_;\n \tbool headerReceived_;\n \tstruct Header header_;\n \tEventNotifier *notifier_;\ndiff --git a/src/libcamera/ipc_pipe_unixsocket.cpp b/src/libcamera/ipc_pipe_unixsocket.cpp\nindex db0e260f..1997643f 100644\n--- a/src/libcamera/ipc_pipe_unixsocket.cpp\n+++ b/src/libcamera/ipc_pipe_unixsocket.cpp\n@@ -30,14 +30,14 @@ IPCPipeUnixSocket::IPCPipeUnixSocket(const char *ipaModulePath,\n \targs.push_back(ipaModulePath);\n \n \tsocket_ = std::make_unique<IPCUnixSocket>();\n-\tint fd = socket_->create();\n-\tif (fd < 0) {\n+\tScopedFD fd = socket_->create();\n+\tif (!fd.isValid()) {\n \t\tLOG(IPCPipe, Error) << \"Failed to create socket\";\n \t\treturn;\n \t}\n \tsocket_->readyRead.connect(this, &IPCPipeUnixSocket::readyRead);\n-\targs.push_back(std::to_string(fd));\n-\tfds.push_back(fd);\n+\targs.push_back(std::to_string(fd.get()));\n+\tfds.push_back(fd.release());\n \n \tproc_ = std::make_unique<Process>();\n \tint ret = proc_->start(ipaProxyWorkerPath, args, fds);\ndiff --git a/src/libcamera/ipc_unixsocket.cpp b/src/libcamera/ipc_unixsocket.cpp\nindex fdb359f7..2da43188 100644\n--- a/src/libcamera/ipc_unixsocket.cpp\n+++ b/src/libcamera/ipc_unixsocket.cpp\n@@ -68,7 +68,7 @@ LOG_DEFINE_CATEGORY(IPCUnixSocket)\n  */\n \n IPCUnixSocket::IPCUnixSocket()\n-\t: fd_(-1), headerReceived_(false), notifier_(nullptr)\n+\t: headerReceived_(false), notifier_(nullptr)\n {\n }\n \n@@ -86,9 +86,9 @@ IPCUnixSocket::~IPCUnixSocket()\n  * the remote process, where it can be used with IPCUnixSocket::bind() to bind\n  * the remote side socket.\n  *\n- * \\return A file descriptor on success, negative error code on failure\n+ * \\return A file descriptor. It is valid on success or invalid otherwise.\n  */\n-int IPCUnixSocket::create()\n+ScopedFD IPCUnixSocket::create()\n {\n \tint sockets[2];\n \tint ret;\n@@ -98,14 +98,13 @@ int IPCUnixSocket::create()\n \t\tret = -errno;\n \t\tLOG(IPCUnixSocket, Error)\n \t\t\t<< \"Failed to create socket pair: \" << strerror(-ret);\n-\t\treturn ret;\n+\t\treturn ScopedFD();\n \t}\n \n-\tret = bind(sockets[0]);\n-\tif (ret)\n-\t\treturn ret;\n+\tif (bind(sockets[0]) < 0)\n+\t\treturn ScopedFD();\n \n-\treturn sockets[1];\n+\treturn ScopedFD(sockets[1]);\n }\n \n /**\n@@ -116,15 +115,18 @@ int IPCUnixSocket::create()\n  * by the file descriptor \\a fd. The file descriptor is obtained from the\n  * IPCUnixSocket::create() method.\n  *\n- * \\return 0 on success or a negative error code otherwise\n+ * \\return 0 on success or a negative error code otherwise.\n+ *\n+ * \\todo This argument should be ScopedFD because bind() takes over the\n+ * ownership.\n  */\n int IPCUnixSocket::bind(int fd)\n {\n \tif (isBound())\n \t\treturn -EINVAL;\n \n-\tfd_ = fd;\n-\tnotifier_ = new EventNotifier(fd_, EventNotifier::Read);\n+\tfd_ = ScopedFD(fd);\n+\tnotifier_ = new EventNotifier(fd_.get(), EventNotifier::Read);\n \tnotifier_->activated.connect(this, &IPCUnixSocket::dataNotifier);\n \n \treturn 0;\n@@ -143,9 +145,7 @@ void IPCUnixSocket::close()\n \tdelete notifier_;\n \tnotifier_ = nullptr;\n \n-\t::close(fd_);\n-\n-\tfd_ = -1;\n+\tfd_.reset();\n \theaderReceived_ = false;\n }\n \n@@ -155,7 +155,7 @@ void IPCUnixSocket::close()\n  */\n bool IPCUnixSocket::isBound() const\n {\n-\treturn fd_ != -1;\n+\treturn fd_.isValid();\n }\n \n /**\n@@ -182,7 +182,7 @@ int IPCUnixSocket::send(const Payload &payload)\n \tif (!hdr.data && !hdr.fds)\n \t\treturn -EINVAL;\n \n-\tret = ::send(fd_, &hdr, sizeof(hdr), 0);\n+\tret = ::send(fd_.get(), &hdr, sizeof(hdr), 0);\n \tif (ret < 0) {\n \t\tret = -errno;\n \t\tLOG(IPCUnixSocket, Error)\n@@ -262,7 +262,7 @@ int IPCUnixSocket::sendData(const void *buffer, size_t length,\n \tmsg.msg_flags = 0;\n \tmemcpy(CMSG_DATA(cmsg), fds, num * sizeof(uint32_t));\n \n-\tif (sendmsg(fd_, &msg, 0) < 0) {\n+\tif (sendmsg(fd_.get(), &msg, 0) < 0) {\n \t\tint ret = -errno;\n \t\tLOG(IPCUnixSocket, Error)\n \t\t\t<< \"Failed to sendmsg: \" << strerror(-ret);\n@@ -296,7 +296,7 @@ int IPCUnixSocket::recvData(void *buffer, size_t length,\n \tmsg.msg_controllen = cmsg->cmsg_len;\n \tmsg.msg_flags = 0;\n \n-\tif (recvmsg(fd_, &msg, 0) < 0) {\n+\tif (recvmsg(fd_.get(), &msg, 0) < 0) {\n \t\tint ret = -errno;\n \t\tif (ret != -EAGAIN)\n \t\t\tLOG(IPCUnixSocket, Error)\n@@ -315,7 +315,7 @@ void IPCUnixSocket::dataNotifier([[maybe_unused]] EventNotifier *notifier)\n \n \tif (!headerReceived_) {\n \t\t/* Receive the header. */\n-\t\tret = ::recv(fd_, &header_, sizeof(header_), 0);\n+\t\tret = ::recv(fd_.get(), &header_, sizeof(header_), 0);\n \t\tif (ret < 0) {\n \t\t\tret = -errno;\n \t\t\tLOG(IPCUnixSocket, Error)\n@@ -331,7 +331,7 @@ void IPCUnixSocket::dataNotifier([[maybe_unused]] EventNotifier *notifier)\n \t * readyRead signal. The notifier will be reenabled by the receive()\n \t * method.\n \t */\n-\tstruct pollfd fds = { fd_, POLLIN, 0 };\n+\tstruct pollfd fds = { fd_.get(), POLLIN, 0 };\n \tret = poll(&fds, 1, 0);\n \tif (ret < 0)\n \t\treturn;\ndiff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp\nindex 80157b34..9ca0467b 100644\n--- a/test/ipc/unixsocket.cpp\n+++ b/test/ipc/unixsocket.cpp\n@@ -359,11 +359,11 @@ protected:\n \n \tint run()\n \t{\n-\t\tint slavefd = ipc_.create();\n-\t\tif (slavefd < 0)\n+\t\tScopedFD slavefd = ipc_.create();\n+\t\tif (!slavefd.isValid())\n \t\t\treturn TestFail;\n \n-\t\tif (slaveStart(slavefd)) {\n+\t\tif (slaveStart(slavefd.release())) {\n \t\t\tcerr << \"Failed to start slave\" << endl;\n \t\t\treturn TestFail;\n \t\t}\n","prefixes":["libcamera-devel","RFC","08/10"]}