1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "library_linux.h" 17 18 #include <dlfcn.h> 19 20 #include <base/containers/string.h> 21 #include <core/log.h> 22 #include <core/namespace.h> 23 24 CORE_BEGIN_NAMESPACE() 25 using BASE_NS::string; 26 using BASE_NS::string_view; 27 LibraryLinux(const string_view filename)28LibraryLinux::LibraryLinux(const string_view filename) 29 { 30 string tmp(filename); 31 libraryHandle_ = dlopen(tmp.c_str(), RTLD_NOW | RTLD_LOCAL); 32 if (!libraryHandle_) { 33 const char* errorMessage = dlerror(); 34 if (errorMessage != nullptr) { 35 CORE_LOG_E("Loading dynamic library '%s' failed: %s", filename.data(), errorMessage); 36 } 37 } 38 } 39 ~LibraryLinux()40LibraryLinux::~LibraryLinux() 41 { 42 if (libraryHandle_) { 43 dlclose(libraryHandle_); 44 libraryHandle_ = nullptr; 45 } 46 } 47 GetPlugin() const48IPlugin* LibraryLinux::GetPlugin() const 49 { 50 if (!libraryHandle_) { 51 return nullptr; 52 } 53 54 return reinterpret_cast<IPlugin*>(dlsym(libraryHandle_, "gPluginData")); 55 } 56 Destroy()57void LibraryLinux::Destroy() 58 { 59 delete this; 60 } 61 Load(const string_view filePath)62ILibrary::Ptr ILibrary::Load(const string_view filePath) 63 { 64 return ILibrary::Ptr { new LibraryLinux(filePath) }; 65 } 66 GetFileExtension()67string_view ILibrary::GetFileExtension() 68 { 69 return ".so"; 70 } 71 CORE_END_NAMESPACE() 72