Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "bqs_so_manager.h"
12 :
13 : #include <dlfcn.h>
14 : #include "bqs_log.h"
15 :
16 : namespace bqs {
17 114 : void *SoManager::GetFuncHandle(const std::string &funcName) const
18 : {
19 114 : if (soHandle_ == nullptr) {
20 10 : BQS_LOG_ERROR("Get func handle failed by so handle is nullptr, soName=%s, funcName=%s.",
21 : soName_.c_str(), funcName.c_str());
22 10 : return nullptr;
23 : }
24 :
25 104 : const auto &iter = funcHandleMap_.find(funcName);
26 104 : if (iter == funcHandleMap_.end()) {
27 2 : BQS_LOG_ERROR("Func not loaded during init, soName=%s, funcName=%s.",
28 : soName_.c_str(), funcName.c_str());
29 2 : return nullptr;
30 : }
31 :
32 102 : return iter->second;
33 : }
34 :
35 15 : bool SoManager::IsSoLoad() const
36 : {
37 15 : return soHandle_ != nullptr;
38 : }
39 :
40 14 : void SoManager::OpenSo()
41 : {
42 14 : if (soName_.empty()) {
43 2 : BQS_LOG_ERROR("Open so failed by so name is empty.");
44 2 : return;
45 : }
46 :
47 12 : soHandle_ = dlopen(soName_.c_str(), RTLD_LAZY);
48 12 : if (soHandle_ == nullptr) {
49 8 : BQS_LOG_ERROR("Open so failed, soName=%s, reason=%s.", soName_.c_str(), dlerror());
50 : }
51 :
52 12 : return;
53 : }
54 :
55 14 : void SoManager::CloseSo()
56 : {
57 14 : if (soHandle_ == nullptr) {
58 11 : return;
59 : }
60 :
61 3 : const int32_t ret = dlclose(soHandle_);
62 3 : if (ret != 0) {
63 0 : BQS_LOG_WARN("Close so failed, soName=%s, reason=%s.", soName_.c_str(), dlerror());
64 : }
65 :
66 3 : soHandle_ = nullptr;
67 3 : return;
68 : }
69 :
70 4 : void SoManager::BatchLoadFunc(const std::vector<std::string> &funcNames)
71 : {
72 4 : if (soHandle_ == nullptr) {
73 0 : BQS_LOG_ERROR("Get func handle failed by so handle is nullptr, soName=%s.", soName_.c_str());
74 0 : return;
75 : }
76 :
77 15 : for (const std::string &funcName: funcNames) {
78 11 : void *funcPtr = GetFuncHandleFromSo(funcName);
79 11 : if (funcPtr != nullptr) {
80 10 : (void)funcHandleMap_.emplace(funcName, funcPtr);
81 : }
82 : }
83 :
84 4 : return;
85 : }
86 :
87 11 : void *SoManager::GetFuncHandleFromSo(const std::string &funcName) const
88 : {
89 11 : void *funcPtr = dlsym(soHandle_, funcName.c_str());
90 11 : if (funcPtr == nullptr) {
91 1 : BQS_LOG_ERROR("Get func handle from so failed, soName=%s, funcName=%s, error=%s.",
92 : soName_.c_str(), funcName.c_str(), dlerror());
93 1 : return nullptr;
94 : }
95 :
96 10 : return funcPtr;
97 : }
98 : } // namespace bqs
|