Patrick Venture | c0fe5cd | 2018-11-21 14:10:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 17 | #include "utils.hpp" |
| 18 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 19 | #include "fs.hpp" |
Patrick Venture | cd8dab4 | 2019-01-15 19:57:38 -0800 | [diff] [blame] | 20 | #include "manager.hpp" |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 21 | |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 22 | #include <dlfcn.h> |
| 23 | |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 24 | #include <phosphor-logging/log.hpp> |
Patrick Williams | 5250957 | 2023-05-10 07:51:18 -0500 | [diff] [blame] | 25 | |
| 26 | #include <memory> |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 27 | #include <regex> |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 28 | #include <string> |
William A. Kennington III | 766beb3 | 2021-06-16 11:47:51 -0700 | [diff] [blame] | 29 | #include <unordered_set> |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 30 | #include <vector> |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 31 | |
| 32 | namespace blobs |
| 33 | { |
| 34 | |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 35 | using namespace phosphor::logging; |
| 36 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 37 | bool matchBlobHandler(const std::string& filename) |
| 38 | { |
William A. Kennington III | 3ccee07 | 2021-06-15 16:51:30 -0700 | [diff] [blame] | 39 | return filename.find(".so") != std::string::npos; |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 40 | } |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 41 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 42 | void loadLibraries(ManagerInterface* manager, const std::string& path, |
| 43 | const internal::DlSysInterface* sys) |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 44 | { |
William A. Kennington III | 766beb3 | 2021-06-16 11:47:51 -0700 | [diff] [blame] | 45 | std::unordered_set<HandlerFactory> seen; |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 46 | void* libHandle = NULL; |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 47 | HandlerFactory factory; |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 48 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 49 | std::vector<std::string> libs = getLibraryList(path, matchBlobHandler); |
| 50 | |
| 51 | for (const auto& p : libs) |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 52 | { |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 53 | libHandle = sys->dlopen(p.c_str(), RTLD_NOW | RTLD_GLOBAL); |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 54 | if (!libHandle) |
| 55 | { |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 56 | log<level::ERR>("ERROR opening", entry("HANDLER=%s", p.c_str()), |
| 57 | entry("ERROR=%s", sys->dlerror())); |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 58 | continue; |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 59 | } |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 60 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 61 | sys->dlerror(); /* Clear any previous error. */ |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 62 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 63 | factory = reinterpret_cast<HandlerFactory>( |
| 64 | sys->dlsym(libHandle, "createHandler")); |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 65 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 66 | const char* error = sys->dlerror(); |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 67 | if (error) |
| 68 | { |
| 69 | log<level::ERR>("ERROR loading symbol", |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame] | 70 | entry("HANDLER=%s", p.c_str()), |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 71 | entry("ERROR=%s", error)); |
| 72 | continue; |
| 73 | } |
| 74 | |
William A. Kennington III | 766beb3 | 2021-06-16 11:47:51 -0700 | [diff] [blame] | 75 | // 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 Venture | c7c941d | 2020-12-08 12:10:37 -0800 | [diff] [blame] | 83 | try |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 84 | { |
Patrick Venture | c7c941d | 2020-12-08 12:10:37 -0800 | [diff] [blame] | 85 | 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 Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 92 | |
Patrick Venture | c7c941d | 2020-12-08 12:10:37 -0800 | [diff] [blame] | 93 | 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 Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | } // namespace blobs |