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 17 #include "utils.hpp" 18 19 #include "fs.hpp" 20 #include "manager.hpp" 21 22 #include <dlfcn.h> 23 24 #include <memory> 25 #include <phosphor-logging/log.hpp> 26 #include <regex> 27 #include <string> 28 #include <unordered_set> 29 #include <vector> 30 31 namespace blobs 32 { 33 34 using namespace phosphor::logging; 35 36 bool matchBlobHandler(const std::string& filename) 37 { 38 return filename.find(".so") != std::string::npos; 39 } 40 41 void loadLibraries(ManagerInterface* manager, const std::string& path, 42 const internal::DlSysInterface* sys) 43 { 44 std::unordered_set<HandlerFactory> seen; 45 void* libHandle = NULL; 46 HandlerFactory factory; 47 48 std::vector<std::string> libs = getLibraryList(path, matchBlobHandler); 49 50 for (const auto& p : libs) 51 { 52 libHandle = sys->dlopen(p.c_str(), RTLD_NOW | RTLD_GLOBAL); 53 if (!libHandle) 54 { 55 log<level::ERR>("ERROR opening", entry("HANDLER=%s", p.c_str()), 56 entry("ERROR=%s", sys->dlerror())); 57 continue; 58 } 59 60 sys->dlerror(); /* Clear any previous error. */ 61 62 factory = reinterpret_cast<HandlerFactory>( 63 sys->dlsym(libHandle, "createHandler")); 64 65 const char* error = sys->dlerror(); 66 if (error) 67 { 68 log<level::ERR>("ERROR loading symbol", 69 entry("HANDLER=%s", p.c_str()), 70 entry("ERROR=%s", error)); 71 continue; 72 } 73 74 // We may have duplicate libraries in the blobs directory that we only 75 // want to initialize once. 76 if (seen.count(factory) > 0) 77 { 78 continue; 79 } 80 seen.emplace(factory); 81 82 try 83 { 84 std::unique_ptr<GenericBlobInterface> result = factory(); 85 if (!result) 86 { 87 log<level::ERR>("Unable to create handler", 88 entry("HANDLER=%s", p.c_str())); 89 continue; 90 } 91 92 manager->registerHandler(std::move(result)); 93 } 94 catch (const std::exception& e) 95 { 96 log<level::ERR>("Received exception loading handler", 97 entry("HANDLER=%s", p.c_str()), 98 entry("EXCEPTION=%s", e.what())); 99 } 100 } 101 } 102 103 } // namespace blobs 104