{"id":12541,"url":"https://patchwork.libcamera.org/api/patches/12541/?format=json","web_url":"https://patchwork.libcamera.org/patch/12541/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20210610075027.523672-9-hiroh@chromium.org>","date":"2021-06-10T07:50:25","name":"[libcamera-devel,v2,08/10] libcamera: IPCUnixSocket: Use ScopedFD for a file descriptor","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"da1075143df573db76161e075a1cc0fa21bb0ed0","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/people/63/?format=json","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/12541/mbox/","series":[{"id":2115,"url":"https://patchwork.libcamera.org/api/series/2115/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2115","date":"2021-06-10T07:50:17","name":"Introduce ScopedFD","version":2,"mbox":"https://patchwork.libcamera.org/series/2115/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12541/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12541/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 2CD0EBD78E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 10 Jun 2021 07:50:49 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DDC9968931;\n\tThu, 10 Jun 2021 09:50:48 +0200 (CEST)","from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com\n\t[IPv6:2607:f8b0:4864:20::62e])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8817968940\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 10 Jun 2021 09:50:47 +0200 (CEST)","by mail-pl1-x62e.google.com with SMTP id e1so534519pld.13\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 10 Jun 2021 00:50:47 -0700 (PDT)","from hiroh2.tok.corp.google.com\n\t([2401:fa00:8f:203:98e0:b356:1c8a:25d4])\n\tby smtp.gmail.com with ESMTPSA id\n\td66sm1565161pfa.32.2021.06.10.00.50.44\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 10 Jun 2021 00:50:45 -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=\"TOTW+fMc\"; 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=L/ATuyWRbMv4FyvQrPLRiCOVFfyVOn3wfjrXN4gF/64=;\n\tb=TOTW+fMc3aSmxTDzaEmG7gZDduOLmW1Wz2JNRXsa9jTwRqbz94E6ht/Fh2p2T+agEE\n\tmH1lbET1qsLX2q2adOs80R3bIv75y9m0wPhgTGAEr6RlEFbHPZOCshZ4Kv89hn571wJe\n\taHqlBhtOnfdJTHFyXSBi5Ucx9ne0cd12uwibo=","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=L/ATuyWRbMv4FyvQrPLRiCOVFfyVOn3wfjrXN4gF/64=;\n\tb=a0+tCTftwABmOp4IR1FKXKtJpffQxedwK4htZlIRnjlwUnEmScqxqyl0UQhFSz3uRJ\n\tpR4ELDU+1hzTS3Z4bEMZYiii1Q9h3JYzxCZWZnVqi+cM0GsN6krhjNz/4nd69Vt8JRr4\n\t7YsbFpGMawoRa2xNp2uxSy/+6pu083xTFo4jN+79bF/ZReBs1rdVCOkVyilKnSVsd+yA\n\tTl99aFPgHNct/qdMYZaV6YrBZ07yw1vaqZNziuZO8yztmBvBUBUPLec/XkN+TdcNJ0AS\n\tP6iGsee5m6PndUP6Vc8JU/IjCKFz/x7bO51YKYww5DaKoFhv9UoEezXuAEppSjiKYZcH\n\t840A==","X-Gm-Message-State":"AOAM532FbdKQUtWqn9qG5Z9snQXtd0IR0FJCpcjbHi45gvYL1GXADuhQ\n\tqn0LiZI7WzO97vHOMQ/BP6HIXkBIQoXlbg==","X-Google-Smtp-Source":"ABdhPJygdFXsHxtmydDPEFYK1k3XL0ilkOOcQGIG/ghAuWhpsUlLycGetUK3WSk9NFKSRWAsVNJOxw==","X-Received":"by 2002:a17:90a:fd05:: with SMTP id\n\tcv5mr2007108pjb.24.1623311445822; \n\tThu, 10 Jun 2021 00:50:45 -0700 (PDT)","From":"Hirokazu Honda <hiroh@chromium.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 10 Jun 2021 16:50:25 +0900","Message-Id":"<20210610075027.523672-9-hiroh@chromium.org>","X-Mailer":"git-send-email 2.32.0.rc1.229.g3e70b5a671-goog","In-Reply-To":"<20210610075027.523672-1-hiroh@chromium.org>","References":"<20210610075027.523672-1-hiroh@chromium.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 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>","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..c620992b 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_fd.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","v2","08/10"]}