• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #undef LOG_TAG
17 #define LOG_TAG "AVSessionDynamicLoader"
18 
19 #include <dlfcn.h>
20 #include <openssl/crypto.h>
21 #include "avsession_log.h"
22 #include "avsession_errors.h"
23 #include "avsession_dynamic_loader.h"
24 #include "directory_ex.h"
25 
26 namespace OHOS {
27 namespace AVSession {
28 using namespace std;
29 
AVSessionDynamicLoader()30 AVSessionDynamicLoader::AVSessionDynamicLoader()
31 {
32     SLOGI("AVSessionDynamicLoader ctor");
33 }
34 
~AVSessionDynamicLoader()35 AVSessionDynamicLoader::~AVSessionDynamicLoader()
36 {
37     SLOGI("AVSessionDynamicLoader dtor");
38     for (auto iterator = dynamicLibHandle_.begin(); iterator != dynamicLibHandle_.end(); ++iterator) {
39 #ifndef TEST_COVERAGE
40         if (iterator->second != nullptr) {
41             OPENSSL_thread_stop();
42         }
43         dlclose(iterator->second);
44 #endif
45         SLOGI("close library avsession_dynamic success: %{public}s", iterator->first.c_str());
46     }
47 }
48 
OpenDynamicHandle(std::string dynamicLibrary)49 void* AVSessionDynamicLoader::OpenDynamicHandle(std::string dynamicLibrary)
50 {
51     std::lock_guard loaderLock(libLock_);
52     // if not opened, then open directly
53     // do we need lock?
54     // further optimization:
55     // 1. split all dependencies to separate libraries
56     // 2. just close each library not all
57     char realCachePath[PATH_MAX] = { 0X00 };
58     char *realPathRes = realpath(dynamicLibrary.c_str(), realCachePath);
59     if (realPathRes == nullptr || dynamicLibrary.find(".so") == std::string::npos) {
60         SLOGD("OpenDynamicHandle get dynamicLibrary %{public}s", dynamicLibrary.c_str());
61     }
62     if (dynamicLibHandle_[dynamicLibrary] == nullptr) {
63 #ifndef TEST_COVERAGE
64         void* dynamicLibHandle = dlopen(dynamicLibrary.c_str(), RTLD_NOW);
65         if (dynamicLibHandle == nullptr) {
66             SLOGE("Failed to open %{public}s, reason: %{public}sn", dynamicLibrary.c_str(), dlerror());
67             return nullptr;
68         }
69         SLOGI("open library %{public}s success", dynamicLibrary.c_str());
70         dynamicLibHandle_[dynamicLibrary] = dynamicLibHandle;
71 #else
72         SLOGI("in test coverage state, dlclose/dlopen may conflict with llvm");
73 #endif
74     }
75     return dynamicLibHandle_[dynamicLibrary];
76 }
77 
GetFuntion(std::string dynamicLibrary,std::string function)78 void* AVSessionDynamicLoader::GetFuntion(std::string dynamicLibrary, std::string function)
79 {
80     std::lock_guard loaderLock(libLock_);
81     // if not opened, then open directly
82     if (dynamicLibHandle_[dynamicLibrary] == nullptr) {
83         OpenDynamicHandle(dynamicLibrary);
84     }
85 
86     void* handle = nullptr;
87     if (dynamicLibHandle_[dynamicLibrary] != nullptr) {
88         handle = dlsym(dynamicLibHandle_[dynamicLibrary], function.c_str());
89         if (handle == nullptr) {
90             SLOGE("Failed to load %{public}s, reason: %{public}sn", function.c_str(), dlerror());
91             return nullptr;
92         }
93         SLOGI("GetFuntion %{public}s success", function.c_str());
94     }
95     return handle;
96 }
97 
CloseDynamicHandle(std::string dynamicLibrary)98 void AVSessionDynamicLoader::CloseDynamicHandle(std::string dynamicLibrary)
99 {
100     std::lock_guard loaderLock(libLock_);
101     // if already opened, then close all
102     if (dynamicLibHandle_[dynamicLibrary] != nullptr) {
103 #ifndef TEST_COVERAGE
104         OPENSSL_thread_stop();
105         dlclose(dynamicLibHandle_[dynamicLibrary]);
106 #endif
107         dynamicLibHandle_[dynamicLibrary] = nullptr;
108         SLOGI("close library avsession_dynamic success: %{public}s", dynamicLibrary.c_str());
109     }
110 }
111 
112 }  // namespace AVSession
113 }  // namespace OHOS