From 6c415c67011532fc0fb7c3051a4cf657ea93dfe0 Mon Sep 17 00:00:00 2001 From: Patrick Venture Date: Wed, 14 Nov 2018 14:01:36 -0800 Subject: bugfix: load handlers and use factory symbol Use a predefined factory symbol to build each handler after loading the library. Change-Id: I0369c6e46a57c2e8533409d8b06eb74a3962434c Signed-off-by: Patrick Venture --- utils.cpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'utils.cpp') diff --git a/utils.cpp b/utils.cpp index 5253c1c..2395a2e 100644 --- a/utils.cpp +++ b/utils.cpp @@ -2,7 +2,9 @@ #include +#include #include +#include #include #include @@ -12,9 +14,13 @@ namespace blobs namespace fs = std::experimental::filesystem; using namespace phosphor::logging; +using HandlerFactory = std::unique_ptr (*)(); + void loadLibraries(const std::string& path) { void* libHandle = NULL; + HandlerFactory factory; + auto* manager = getBlobManager(); for (const auto& p : fs::recursive_directory_iterator(path)) { @@ -30,12 +36,37 @@ void loadLibraries(const std::string& path) continue; } - libHandle = dlopen(ps.c_str(), RTLD_NOW); + libHandle = dlopen(ps.c_str(), RTLD_NOW | RTLD_GLOBAL); if (!libHandle) { log("ERROR opening", entry("HANDLER=%s", ps.c_str()), entry("ERROR=%s", dlerror())); + continue; } + + dlerror(); /* Clear any previous error. */ + + factory = + reinterpret_cast(dlsym(libHandle, "createHandler")); + + const char* error = dlerror(); + if (error) + { + log("ERROR loading symbol", + entry("HANDLER=%s", ps.c_str()), + entry("ERROR=%s", error)); + continue; + } + + std::unique_ptr result = factory(); + if (!result) + { + log("Unable to create handler", + entry("HANDLER=%s", ps.c_str())); + continue; + } + + manager->registerHandler(std::move(result)); } } -- cgit v1.2.3