blob: 1e1ef91c1afa3c15286d1b6e188c9ad99ff35ca6 [file] [log] [blame]
Patrick Venturec0fe5cd2018-11-21 14:10:26 -08001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Patrick Venture4dc584d2018-09-27 15:00:46 -070017#include "utils.hpp"
18
19#include <dlfcn.h>
20
Patrick Venture6c415c62018-11-14 14:01:36 -080021#include <blobs-ipmid/manager.hpp>
Patrick Venture4dc584d2018-09-27 15:00:46 -070022#include <experimental/filesystem>
Patrick Venture6c415c62018-11-14 14:01:36 -080023#include <memory>
Patrick Venture4dc584d2018-09-27 15:00:46 -070024#include <phosphor-logging/log.hpp>
25#include <regex>
26
27namespace blobs
28{
29
30namespace fs = std::experimental::filesystem;
31using namespace phosphor::logging;
32
Patrick Venture6c415c62018-11-14 14:01:36 -080033using HandlerFactory = std::unique_ptr<GenericBlobInterface> (*)();
34
Patrick Venture4dc584d2018-09-27 15:00:46 -070035void loadLibraries(const std::string& path)
36{
37 void* libHandle = NULL;
Patrick Venture6c415c62018-11-14 14:01:36 -080038 HandlerFactory factory;
39 auto* manager = getBlobManager();
Patrick Venture4dc584d2018-09-27 15:00:46 -070040
41 for (const auto& p : fs::recursive_directory_iterator(path))
42 {
43 auto ps = p.path().string();
44
Patrick Venture1d5cccb2018-11-13 13:08:03 -080045 /* The bitbake recipe symlinks the library lib*.so.? into the folder
46 * only, and not the other names, .so, .so.?.?, .so.?.?.?
47 *
48 * Therefore only care if it's lib*.so.?
49 */
50 if (!std::regex_match(ps, std::regex(".+\\.so\\.\\d+$")))
Patrick Venture4dc584d2018-09-27 15:00:46 -070051 {
52 continue;
53 }
54
Patrick Venture6c415c62018-11-14 14:01:36 -080055 libHandle = dlopen(ps.c_str(), RTLD_NOW | RTLD_GLOBAL);
Patrick Venture4dc584d2018-09-27 15:00:46 -070056 if (!libHandle)
57 {
58 log<level::ERR>("ERROR opening", entry("HANDLER=%s", ps.c_str()),
59 entry("ERROR=%s", dlerror()));
Patrick Venture6c415c62018-11-14 14:01:36 -080060 continue;
Patrick Venture4dc584d2018-09-27 15:00:46 -070061 }
Patrick Venture6c415c62018-11-14 14:01:36 -080062
63 dlerror(); /* Clear any previous error. */
64
65 factory =
66 reinterpret_cast<HandlerFactory>(dlsym(libHandle, "createHandler"));
67
68 const char* error = dlerror();
69 if (error)
70 {
71 log<level::ERR>("ERROR loading symbol",
72 entry("HANDLER=%s", ps.c_str()),
73 entry("ERROR=%s", error));
74 continue;
75 }
76
77 std::unique_ptr<GenericBlobInterface> result = factory();
78 if (!result)
79 {
80 log<level::ERR>("Unable to create handler",
81 entry("HANDLER=%s", ps.c_str()));
82 continue;
83 }
84
85 manager->registerHandler(std::move(result));
Patrick Venture4dc584d2018-09-27 15:00:46 -070086 }
87}
88
89} // namespace blobs