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 "plugin_lib.h"
17 
18 #include <dlfcn.h>
19 #include <filesystem>
20 #include <string>
21 #include <openssl/crypto.h>
22 
23 #include "avsession_log.h"
24 
25 namespace OHOS::AVSession {
26 
PluginLib(const std::string &libName)27 PluginLib::PluginLib(const std::string &libName)
28     : libName_(GetRealPath(libName)), handle_(nullptr)
29 {
30     if (!CheckPathExist(libName_)) {
31         SLOGE("%{public}s path invalid", libName_.c_str());
32         return;
33     }
34     handle_ = dlopen(libName_.c_str(), RTLD_NOW);
35     if (handle_ == nullptr) {
36         LogDlfcnErr("open lib failed");
37     return;
38 }
39 SLOGI("%{public}s open succ", libName_.c_str());
40 }
41 
~PluginLib()42 PluginLib::~PluginLib()
43 {
44 #ifndef TEST_COVERAGE
45     if (handle_ != nullptr) {
46         OPENSSL_thread_stop();
47     }
48     if (handle_ == nullptr || dlclose(handle_) != 0) {
49         LogDlfcnErr("close lib failed");
50     }
51 #endif
52     SLOGI("%{public}s close succ", libName_.c_str());
53 }
54 
LoadSymbol(const std::string &symbolName)55 void *PluginLib::LoadSymbol(const std::string &symbolName)
56 {
57     if (handle_ == nullptr) {
58         SLOGE("%{public}s lib is null", libName_.c_str());
59     return nullptr;
60     }
61     void *sym = dlsym(handle_, symbolName.c_str());
62     if (sym == nullptr) {
63         LogDlfcnErr("load symbol [" + symbolName + "] failed");
64         return nullptr;
65     }
66     SLOGI("%{public}s load symbol succ", symbolName.c_str());
67     return sym;
68 }
69 
LogDlfcnErr(const std::string &desc)70 void PluginLib::LogDlfcnErr(const std::string &desc)
71 {
72     SLOGE("[%{public}s] %{public}s, reason = %{public}s",
73         libName_.c_str(), desc.c_str(), dlerror());
74     // reset errors
75     dlerror();
76 }
77 
GetRealPath(const std::string &path)78 std::string PluginLib::GetRealPath(const std::string &path)
79 {
80     auto realPath = std::filesystem::weakly_canonical(path);
81     return realPath.string();
82 }
83 
CheckPathExist(const std::string &path)84 bool PluginLib::CheckPathExist(const std::string &path)
85 {
86     return std::filesystem::exists(path);
87 }
88 
89 }   // namespace OHOS::AVSession