From patchwork Wed Jun 5 22:18:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 1371 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 88C6164772 for ; Thu, 6 Jun 2019 00:18:35 +0200 (CEST) Received: from localhost.localdomain (unknown [96.44.9.117]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id E343C84; Thu, 6 Jun 2019 00:18:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1559773115; bh=EUm7WwfdOoEbXjefZzOV8nULKUqO1Bc4JJNcuQDD1Y8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YpQ1iX3gQoGxoHpNhjmpHj5HSySjF53Qw3eexdBAyD6C9SoxeOwDNHlgN7OhWEe5h K+XIFVcfOTTVvcJHNQ9DA6L1kw22IByuZoJRwjbVFVLyNcPPxilYlcW1tZVLo/SLty 71kh/SILzkbvrTpC9BOHrdK8xHl81f2pPgeKDLH4= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Wed, 5 Jun 2019 18:18:17 -0400 Message-Id: <20190605221817.966-11-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190605221817.966-1-paul.elder@ideasonboard.com> References: <20190605221817.966-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 10/10] libcamera: ipa: shim: load IPA module into an IPAInterface X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 Jun 2019 22:18:35 -0000 Implement the dummy shim's init method that takes a path for the IPA module to be loaded. Set up IPC, then fork and load the IPAInterface implementation from the IPA module shared object. Implement a cleanup method along with it. Signed-off-by: Paul Elder --- This is the most draft-y patch of the whole series. I wasn't sure how to put what kind of IPC, so I kind of just did this very skeletal "give you an idea" type of IPC initialization. Privilege dropping will be looked into for the next version. src/ipa/shim_dummy.cpp | 79 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/src/ipa/shim_dummy.cpp b/src/ipa/shim_dummy.cpp index 4d28c2d..f2e6961 100644 --- a/src/ipa/shim_dummy.cpp +++ b/src/ipa/shim_dummy.cpp @@ -6,6 +6,14 @@ */ #include +#include + +#include +#include +#include +#include +#include +#include #include #include @@ -17,20 +25,87 @@ class ShimDummy : public IPAInterface public: int init(); int init(const char *path); + + void cleanup(); + +private: + std::unique_ptr ipa_; + + int sockets_[2]; + int childPid_; + void *dlHandle_; + + typedef IPAInterface *(*IPAIntfFactory)(void); }; int ShimDummy::init() { - std::cout << "okay shim init without path" << std::endl; + std::cout << "initializing IPA via dummy shim!" << std::endl; return 0; } int ShimDummy::init(const char *path) { - std::cout << "initializing dummy shim!" << std::endl; + std::cout << "initializing dummy shim! loading IPA from " << path + << std::endl; + + /* We know the IPA module is valid, otherwise IPAManager wouldn't + * even give its path to us. */ + + // setup comm + if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets_)) { + int err = errno; + std::cerr << "Error opening sockets: " << strerror(errno) + << std::endl; + return err; + } + + // fork + if ((childPid_ = fork()) == -1) { + int err = errno; + std::cerr << "Failed to fork: " << strerror(errno) << std::endl; + return err; + } else if (childPid_) { + // we are parent + // we can read/write sockets_[1] + close(sockets_[0]); + } else { + // we are child + // we can read/write sockets_[0] + close(sockets_[1]); + + dlHandle_ = dlopen(path, RTLD_LAZY); + if (!dlHandle_) { + std::cerr << "Failed to open IPA module: " + << dlerror() << std::endl; + return -1; + } + + void *symbol = dlsym(dlHandle_, "ipaCreate"); + if (!symbol) { + std::cerr + << "Failed to load ipaCreate() from IPA module shared object: " + << dlerror(); + dlclose(dlHandle_); + dlHandle_ = nullptr; + return -1; + } + + IPAIntfFactory ipaCreate = reinterpret_cast(symbol); + ipa_ = std::unique_ptr(ipaCreate()); + exit(0); + } + return 0; } +void ShimDummy::cleanup() +{ + if (dlHandle_) + dlclose(dlHandle_); + dlHandle_ = nullptr; +} + /* * External IPA module interface */