blob: e03ec909aaaf723047e697c80074e5ce319e0585 [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
Patrick Venturec18e2b62018-11-21 14:19:28 -080019#include "fs.hpp"
Patrick Venturecd8dab42019-01-15 19:57:38 -080020#include "manager.hpp"
Patrick Venturec18e2b62018-11-21 14:19:28 -080021
Patrick Venture4dc584d2018-09-27 15:00:46 -070022#include <dlfcn.h>
23
Patrick Venture6c415c62018-11-14 14:01:36 -080024#include <memory>
Patrick Venture4dc584d2018-09-27 15:00:46 -070025#include <phosphor-logging/log.hpp>
26#include <regex>
Patrick Venturec18e2b62018-11-21 14:19:28 -080027#include <string>
28#include <vector>
Patrick Venture4dc584d2018-09-27 15:00:46 -070029
30namespace blobs
31{
32
Patrick Venture4dc584d2018-09-27 15:00:46 -070033using namespace phosphor::logging;
34
Patrick Venturec18e2b62018-11-21 14:19:28 -080035bool matchBlobHandler(const std::string& filename)
36{
37 return std::regex_match(filename, std::regex(".+\\.so\\.\\d+$"));
38}
Patrick Venture6c415c62018-11-14 14:01:36 -080039
Patrick Venturec18e2b62018-11-21 14:19:28 -080040void loadLibraries(ManagerInterface* manager, const std::string& path,
41 const internal::DlSysInterface* sys)
Patrick Venture4dc584d2018-09-27 15:00:46 -070042{
43 void* libHandle = NULL;
Patrick Venture6c415c62018-11-14 14:01:36 -080044 HandlerFactory factory;
Patrick Venture4dc584d2018-09-27 15:00:46 -070045
Patrick Venturec18e2b62018-11-21 14:19:28 -080046 std::vector<std::string> libs = getLibraryList(path, matchBlobHandler);
47
48 for (const auto& p : libs)
Patrick Venture4dc584d2018-09-27 15:00:46 -070049 {
Patrick Venturec18e2b62018-11-21 14:19:28 -080050 libHandle = sys->dlopen(p.c_str(), RTLD_NOW | RTLD_GLOBAL);
Patrick Venture4dc584d2018-09-27 15:00:46 -070051 if (!libHandle)
52 {
Patrick Venturec18e2b62018-11-21 14:19:28 -080053 log<level::ERR>("ERROR opening", entry("HANDLER=%s", p.c_str()),
54 entry("ERROR=%s", sys->dlerror()));
Patrick Venture6c415c62018-11-14 14:01:36 -080055 continue;
Patrick Venture4dc584d2018-09-27 15:00:46 -070056 }
Patrick Venture6c415c62018-11-14 14:01:36 -080057
Patrick Venturec18e2b62018-11-21 14:19:28 -080058 sys->dlerror(); /* Clear any previous error. */
Patrick Venture6c415c62018-11-14 14:01:36 -080059
Patrick Venturec18e2b62018-11-21 14:19:28 -080060 factory = reinterpret_cast<HandlerFactory>(
61 sys->dlsym(libHandle, "createHandler"));
Patrick Venture6c415c62018-11-14 14:01:36 -080062
Patrick Venturec18e2b62018-11-21 14:19:28 -080063 const char* error = sys->dlerror();
Patrick Venture6c415c62018-11-14 14:01:36 -080064 if (error)
65 {
66 log<level::ERR>("ERROR loading symbol",
Patrick Venturec18e2b62018-11-21 14:19:28 -080067 entry("HANDLER=%s", p.c_str()),
Patrick Venture6c415c62018-11-14 14:01:36 -080068 entry("ERROR=%s", error));
69 continue;
70 }
71
72 std::unique_ptr<GenericBlobInterface> result = factory();
73 if (!result)
74 {
75 log<level::ERR>("Unable to create handler",
Patrick Venturec18e2b62018-11-21 14:19:28 -080076 entry("HANDLER=%s", p.c_str()));
Patrick Venture6c415c62018-11-14 14:01:36 -080077 continue;
78 }
79
80 manager->registerHandler(std::move(result));
Patrick Venture4dc584d2018-09-27 15:00:46 -070081 }
82}
83
84} // namespace blobs