Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
| 3 | #include <dlfcn.h> |
| 4 | |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame^] | 5 | #include <blobs-ipmid/manager.hpp> |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 6 | #include <experimental/filesystem> |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame^] | 7 | #include <memory> |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
| 9 | #include <regex> |
| 10 | |
| 11 | namespace blobs |
| 12 | { |
| 13 | |
| 14 | namespace fs = std::experimental::filesystem; |
| 15 | using namespace phosphor::logging; |
| 16 | |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame^] | 17 | using HandlerFactory = std::unique_ptr<GenericBlobInterface> (*)(); |
| 18 | |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 19 | void loadLibraries(const std::string& path) |
| 20 | { |
| 21 | void* libHandle = NULL; |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame^] | 22 | HandlerFactory factory; |
| 23 | auto* manager = getBlobManager(); |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 24 | |
| 25 | for (const auto& p : fs::recursive_directory_iterator(path)) |
| 26 | { |
| 27 | auto ps = p.path().string(); |
| 28 | |
Patrick Venture | 1d5cccb | 2018-11-13 13:08:03 -0800 | [diff] [blame] | 29 | /* The bitbake recipe symlinks the library lib*.so.? into the folder |
| 30 | * only, and not the other names, .so, .so.?.?, .so.?.?.? |
| 31 | * |
| 32 | * Therefore only care if it's lib*.so.? |
| 33 | */ |
| 34 | if (!std::regex_match(ps, std::regex(".+\\.so\\.\\d+$"))) |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 35 | { |
| 36 | continue; |
| 37 | } |
| 38 | |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame^] | 39 | libHandle = dlopen(ps.c_str(), RTLD_NOW | RTLD_GLOBAL); |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 40 | if (!libHandle) |
| 41 | { |
| 42 | log<level::ERR>("ERROR opening", entry("HANDLER=%s", ps.c_str()), |
| 43 | entry("ERROR=%s", dlerror())); |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame^] | 44 | continue; |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 45 | } |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame^] | 46 | |
| 47 | dlerror(); /* Clear any previous error. */ |
| 48 | |
| 49 | factory = |
| 50 | reinterpret_cast<HandlerFactory>(dlsym(libHandle, "createHandler")); |
| 51 | |
| 52 | const char* error = dlerror(); |
| 53 | if (error) |
| 54 | { |
| 55 | log<level::ERR>("ERROR loading symbol", |
| 56 | entry("HANDLER=%s", ps.c_str()), |
| 57 | entry("ERROR=%s", error)); |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | std::unique_ptr<GenericBlobInterface> result = factory(); |
| 62 | if (!result) |
| 63 | { |
| 64 | log<level::ERR>("Unable to create handler", |
| 65 | entry("HANDLER=%s", ps.c_str())); |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | manager->registerHandler(std::move(result)); |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | } // namespace blobs |