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" |
| 20 | |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 21 | #include <dlfcn.h> |
| 22 | |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 23 | #include <blobs-ipmid/manager.hpp> |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 24 | #include <memory> |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 25 | #include <phosphor-logging/log.hpp> |
| 26 | #include <regex> |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame^] | 27 | #include <string> |
| 28 | #include <vector> |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 29 | |
| 30 | namespace blobs |
| 31 | { |
| 32 | |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 33 | using namespace phosphor::logging; |
| 34 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame^] | 35 | bool matchBlobHandler(const std::string& filename) |
| 36 | { |
| 37 | return std::regex_match(filename, std::regex(".+\\.so\\.\\d+$")); |
| 38 | } |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 39 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame^] | 40 | void loadLibraries(ManagerInterface* manager, const std::string& path, |
| 41 | const internal::DlSysInterface* sys) |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 42 | { |
| 43 | void* libHandle = NULL; |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 44 | HandlerFactory factory; |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 45 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame^] | 46 | std::vector<std::string> libs = getLibraryList(path, matchBlobHandler); |
| 47 | |
| 48 | for (const auto& p : libs) |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 49 | { |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame^] | 50 | libHandle = sys->dlopen(p.c_str(), RTLD_NOW | RTLD_GLOBAL); |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 51 | if (!libHandle) |
| 52 | { |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame^] | 53 | log<level::ERR>("ERROR opening", entry("HANDLER=%s", p.c_str()), |
| 54 | entry("ERROR=%s", sys->dlerror())); |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 55 | continue; |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 56 | } |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 57 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame^] | 58 | sys->dlerror(); /* Clear any previous error. */ |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 59 | |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame^] | 60 | factory = reinterpret_cast<HandlerFactory>( |
| 61 | sys->dlsym(libHandle, "createHandler")); |
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 | const char* error = sys->dlerror(); |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 64 | if (error) |
| 65 | { |
| 66 | log<level::ERR>("ERROR loading symbol", |
Patrick Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame^] | 67 | entry("HANDLER=%s", p.c_str()), |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 68 | 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 Venture | c18e2b6 | 2018-11-21 14:19:28 -0800 | [diff] [blame^] | 76 | entry("HANDLER=%s", p.c_str())); |
Patrick Venture | 6c415c6 | 2018-11-14 14:01:36 -0800 | [diff] [blame] | 77 | continue; |
| 78 | } |
| 79 | |
| 80 | manager->registerHandler(std::move(result)); |
Patrick Venture | 4dc584d | 2018-09-27 15:00:46 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | } // namespace blobs |