180922886Sopenharmony_ci/* 280922886Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 380922886Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 480922886Sopenharmony_ci * you may not use this file except in compliance with the License. 580922886Sopenharmony_ci * You may obtain a copy of the License at 680922886Sopenharmony_ci * 780922886Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 880922886Sopenharmony_ci * 980922886Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1080922886Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1180922886Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1280922886Sopenharmony_ci * See the License for the specific language governing permissions and 1380922886Sopenharmony_ci * limitations under the License. 1480922886Sopenharmony_ci */ 1580922886Sopenharmony_ci 1680922886Sopenharmony_ci#include "plugin_lib.h" 1780922886Sopenharmony_ci 1880922886Sopenharmony_ci#include <dlfcn.h> 1980922886Sopenharmony_ci#include <filesystem> 2080922886Sopenharmony_ci#include <string> 2180922886Sopenharmony_ci#include <openssl/crypto.h> 2280922886Sopenharmony_ci 2380922886Sopenharmony_ci#include "avsession_log.h" 2480922886Sopenharmony_ci 2580922886Sopenharmony_cinamespace OHOS::AVSession { 2680922886Sopenharmony_ci 2780922886Sopenharmony_ciPluginLib::PluginLib(const std::string &libName) 2880922886Sopenharmony_ci : libName_(GetRealPath(libName)), handle_(nullptr) 2980922886Sopenharmony_ci{ 3080922886Sopenharmony_ci if (!CheckPathExist(libName_)) { 3180922886Sopenharmony_ci SLOGE("%{public}s path invalid", libName_.c_str()); 3280922886Sopenharmony_ci return; 3380922886Sopenharmony_ci } 3480922886Sopenharmony_ci handle_ = dlopen(libName_.c_str(), RTLD_NOW); 3580922886Sopenharmony_ci if (handle_ == nullptr) { 3680922886Sopenharmony_ci LogDlfcnErr("open lib failed"); 3780922886Sopenharmony_ci return; 3880922886Sopenharmony_ci} 3980922886Sopenharmony_ciSLOGI("%{public}s open succ", libName_.c_str()); 4080922886Sopenharmony_ci} 4180922886Sopenharmony_ci 4280922886Sopenharmony_ciPluginLib::~PluginLib() 4380922886Sopenharmony_ci{ 4480922886Sopenharmony_ci#ifndef TEST_COVERAGE 4580922886Sopenharmony_ci if (handle_ != nullptr) { 4680922886Sopenharmony_ci OPENSSL_thread_stop(); 4780922886Sopenharmony_ci } 4880922886Sopenharmony_ci if (handle_ == nullptr || dlclose(handle_) != 0) { 4980922886Sopenharmony_ci LogDlfcnErr("close lib failed"); 5080922886Sopenharmony_ci } 5180922886Sopenharmony_ci#endif 5280922886Sopenharmony_ci SLOGI("%{public}s close succ", libName_.c_str()); 5380922886Sopenharmony_ci} 5480922886Sopenharmony_ci 5580922886Sopenharmony_civoid *PluginLib::LoadSymbol(const std::string &symbolName) 5680922886Sopenharmony_ci{ 5780922886Sopenharmony_ci if (handle_ == nullptr) { 5880922886Sopenharmony_ci SLOGE("%{public}s lib is null", libName_.c_str()); 5980922886Sopenharmony_ci return nullptr; 6080922886Sopenharmony_ci } 6180922886Sopenharmony_ci void *sym = dlsym(handle_, symbolName.c_str()); 6280922886Sopenharmony_ci if (sym == nullptr) { 6380922886Sopenharmony_ci LogDlfcnErr("load symbol [" + symbolName + "] failed"); 6480922886Sopenharmony_ci return nullptr; 6580922886Sopenharmony_ci } 6680922886Sopenharmony_ci SLOGI("%{public}s load symbol succ", symbolName.c_str()); 6780922886Sopenharmony_ci return sym; 6880922886Sopenharmony_ci} 6980922886Sopenharmony_ci 7080922886Sopenharmony_civoid PluginLib::LogDlfcnErr(const std::string &desc) 7180922886Sopenharmony_ci{ 7280922886Sopenharmony_ci SLOGE("[%{public}s] %{public}s, reason = %{public}s", 7380922886Sopenharmony_ci libName_.c_str(), desc.c_str(), dlerror()); 7480922886Sopenharmony_ci // reset errors 7580922886Sopenharmony_ci dlerror(); 7680922886Sopenharmony_ci} 7780922886Sopenharmony_ci 7880922886Sopenharmony_cistd::string PluginLib::GetRealPath(const std::string &path) 7980922886Sopenharmony_ci{ 8080922886Sopenharmony_ci auto realPath = std::filesystem::weakly_canonical(path); 8180922886Sopenharmony_ci return realPath.string(); 8280922886Sopenharmony_ci} 8380922886Sopenharmony_ci 8480922886Sopenharmony_cibool PluginLib::CheckPathExist(const std::string &path) 8580922886Sopenharmony_ci{ 8680922886Sopenharmony_ci return std::filesystem::exists(path); 8780922886Sopenharmony_ci} 8880922886Sopenharmony_ci 8980922886Sopenharmony_ci} // namespace OHOS::AVSession