1 /* 2 * Copyright (C) 2023 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 19 #include <dlfcn.h> 20 21 #include <base/containers/string.h> 22 #include <core/log.h> 23 #include <core/namespace.h> 24 25 CORE_BEGIN_NAMESPACE() 26 using BASE_NS::string; 27 using BASE_NS::string_view; 28 LibraryLinux(const string_view filename)29LibraryLinux::LibraryLinux(const string_view filename) 30 { 31 string tmp(filename); 32 libraryHandle_ = dlopen(tmp.c_str(), RTLD_NOW | RTLD_LOCAL); 33 if (!libraryHandle_) { 34 const char* errorMessage = dlerror(); 35 if (errorMessage != NULL) { 36 CORE_LOG_E("Loading dynamic library '%s' failed: %s", filename.data(), errorMessage); 37 } 38 } 39 } 40 ~LibraryLinux()41LibraryLinux::~LibraryLinux() 42 { 43 if (libraryHandle_) { 44 dlclose(libraryHandle_); 45 libraryHandle_ = nullptr; 46 } 47 } 48 GetPlugin() const49IPlugin* LibraryLinux::GetPlugin() const 50 { 51 if (!libraryHandle_) { 52 return nullptr; 53 } 54 55 return reinterpret_cast<IPlugin*>(dlsym(libraryHandle_, "gPluginData")); 56 } 57 Destroy()58void LibraryLinux::Destroy() 59 { 60 delete this; 61 } 62 Load(const string_view filePath)63ILibrary::Ptr ILibrary::Load(const string_view filePath) 64 { 65 return ILibrary::Ptr { new LibraryLinux(filePath) }; 66 } 67 GetFileExtension()68string_view ILibrary::GetFileExtension() 69 { 70 return ".so"; 71 } 72 CORE_END_NAMESPACE() 73 74