blob: 7744868ada76bc6595ebaaba5bd08a77e33b9b21 [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 Venture4dc584d2018-09-27 15:00:46 -070024#include <phosphor-logging/log.hpp>
Patrick Williams52509572023-05-10 07:51:18 -050025
26#include <memory>
Patrick Venture4dc584d2018-09-27 15:00:46 -070027#include <regex>
Patrick Venturec18e2b62018-11-21 14:19:28 -080028#include <string>
William A. Kennington III766beb32021-06-16 11:47:51 -070029#include <unordered_set>
Patrick Venturec18e2b62018-11-21 14:19:28 -080030#include <vector>
Patrick Venture4dc584d2018-09-27 15:00:46 -070031
32namespace blobs
33{
34
Patrick Venture4dc584d2018-09-27 15:00:46 -070035using namespace phosphor::logging;
36
Patrick Venturec18e2b62018-11-21 14:19:28 -080037bool matchBlobHandler(const std::string& filename)
38{
William A. Kennington III3ccee072021-06-15 16:51:30 -070039 return filename.find(".so") != std::string::npos;
Patrick Venturec18e2b62018-11-21 14:19:28 -080040}
Patrick Venture6c415c62018-11-14 14:01:36 -080041
Patrick Venturec18e2b62018-11-21 14:19:28 -080042void loadLibraries(ManagerInterface* manager, const std::string& path,
43 const internal::DlSysInterface* sys)
Patrick Venture4dc584d2018-09-27 15:00:46 -070044{
William A. Kennington III766beb32021-06-16 11:47:51 -070045 std::unordered_set<HandlerFactory> seen;
Patrick Venture4dc584d2018-09-27 15:00:46 -070046 void* libHandle = NULL;
Patrick Venture6c415c62018-11-14 14:01:36 -080047 HandlerFactory factory;
Patrick Venture4dc584d2018-09-27 15:00:46 -070048
Patrick Venturec18e2b62018-11-21 14:19:28 -080049 std::vector<std::string> libs = getLibraryList(path, matchBlobHandler);
50
51 for (const auto& p : libs)
Patrick Venture4dc584d2018-09-27 15:00:46 -070052 {
Patrick Venturec18e2b62018-11-21 14:19:28 -080053 libHandle = sys->dlopen(p.c_str(), RTLD_NOW | RTLD_GLOBAL);
Patrick Venture4dc584d2018-09-27 15:00:46 -070054 if (!libHandle)
55 {
Patrick Venturec18e2b62018-11-21 14:19:28 -080056 log<level::ERR>("ERROR opening", entry("HANDLER=%s", p.c_str()),
57 entry("ERROR=%s", sys->dlerror()));
Patrick Venture6c415c62018-11-14 14:01:36 -080058 continue;
Patrick Venture4dc584d2018-09-27 15:00:46 -070059 }
Patrick Venture6c415c62018-11-14 14:01:36 -080060
Patrick Venturec18e2b62018-11-21 14:19:28 -080061 sys->dlerror(); /* Clear any previous error. */
Patrick Venture6c415c62018-11-14 14:01:36 -080062
Patrick Venturec18e2b62018-11-21 14:19:28 -080063 factory = reinterpret_cast<HandlerFactory>(
64 sys->dlsym(libHandle, "createHandler"));
Patrick Venture6c415c62018-11-14 14:01:36 -080065
Patrick Venturec18e2b62018-11-21 14:19:28 -080066 const char* error = sys->dlerror();
Patrick Venture6c415c62018-11-14 14:01:36 -080067 if (error)
68 {
69 log<level::ERR>("ERROR loading symbol",
Patrick Venturec18e2b62018-11-21 14:19:28 -080070 entry("HANDLER=%s", p.c_str()),
Patrick Venture6c415c62018-11-14 14:01:36 -080071 entry("ERROR=%s", error));
72 continue;
73 }
74
William A. Kennington III766beb32021-06-16 11:47:51 -070075 // We may have duplicate libraries in the blobs directory that we only
76 // want to initialize once.
77 if (seen.count(factory) > 0)
78 {
79 continue;
80 }
81 seen.emplace(factory);
82
Patrick Venturec7c941d2020-12-08 12:10:37 -080083 try
Patrick Venture6c415c62018-11-14 14:01:36 -080084 {
Patrick Venturec7c941d2020-12-08 12:10:37 -080085 std::unique_ptr<GenericBlobInterface> result = factory();
86 if (!result)
87 {
88 log<level::ERR>("Unable to create handler",
89 entry("HANDLER=%s", p.c_str()));
90 continue;
91 }
Patrick Venture6c415c62018-11-14 14:01:36 -080092
Patrick Venturec7c941d2020-12-08 12:10:37 -080093 manager->registerHandler(std::move(result));
94 }
95 catch (const std::exception& e)
96 {
97 log<level::ERR>("Received exception loading handler",
98 entry("HANDLER=%s", p.c_str()),
99 entry("EXCEPTION=%s", e.what()));
100 }
Patrick Venture4dc584d2018-09-27 15:00:46 -0700101 }
102}
103
104} // namespace blobs