-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathcustom_node_library_manager.cpp
220 lines (191 loc) · 8.86 KB
/
custom_node_library_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//*****************************************************************************
// Copyright 2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include "custom_node_library_manager.hpp"
#include <utility>
#ifdef _WIN32
#include <system_error>
#elif __linux__
#include <dlfcn.h>
#endif
#include "../filesystem.hpp"
#include "../logging.hpp"
#include "../status.hpp"
namespace ovms {
Status CustomNodeLibraryManager::loadLibrary(const std::string& name, const std::string& basePath) {
if (FileSystem::isPathEscaped(basePath)) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Path {} escape with .. is forbidden.", basePath);
return StatusCode::PATH_INVALID;
}
if (!FileSystem::isFullPath(basePath)) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Path {} is relative, should have already been constructed as full path for library {}.", basePath, name);
return StatusCode::PATH_INVALID;
}
auto it = libraries.find(name);
if (it != libraries.end() && it->second.basePath == basePath) {
SPDLOG_LOGGER_DEBUG(modelmanager_logger, "Custom node library name: {} is already loaded", name);
return StatusCode::NODE_LIBRARY_ALREADY_LOADED;
}
SPDLOG_LOGGER_INFO(modelmanager_logger, "Loading custom node library name: {}; base_path: {}", name, basePath);
#ifdef __linux__
void* handle = dlopen(basePath.c_str(), RTLD_LAZY | RTLD_LOCAL);
char* error = dlerror();
if (handle == nullptr) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Library name: {} failed to open base_path: {} with error: {}", name, basePath, error);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_OPEN;
}
initialize_fn initialize = reinterpret_cast<initialize_fn>(dlsym(handle, "initialize"));
error = dlerror();
if (error || initialize == nullptr) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {}", name, error);
dlclose(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
deinitialize_fn deinitialize = reinterpret_cast<deinitialize_fn>(dlsym(handle, "deinitialize"));
error = dlerror();
if (error || deinitialize == nullptr) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {}", name, error);
dlclose(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
execute_fn execute = reinterpret_cast<execute_fn>(dlsym(handle, "execute"));
error = dlerror();
if (error || execute == nullptr) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {}", name, error);
dlclose(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
metadata_fn getInputsInfo = reinterpret_cast<metadata_fn>(dlsym(handle, "getInputsInfo"));
error = dlerror();
if (error || getInputsInfo == nullptr) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {}", name, error);
dlclose(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
metadata_fn getOutputsInfo = reinterpret_cast<metadata_fn>(dlsym(handle, "getOutputsInfo"));
error = dlerror();
if (error || getOutputsInfo == nullptr) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {}", name, error);
dlclose(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
release_fn release = reinterpret_cast<release_fn>(dlsym(handle, "release"));
error = dlerror();
if (error || release == nullptr) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {}", name, error);
dlclose(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
libraries[name] = NodeLibrary{
initialize,
deinitialize,
execute,
getInputsInfo,
getOutputsInfo,
release,
basePath};
SPDLOG_LOGGER_INFO(modelmanager_logger, "Successfully loaded custom node library name: {}; base_path: {}", name, basePath);
#elif _WIN32
HMODULE handle = LoadLibraryA(basePath.c_str());
DWORD error = GetLastError();
std::string message = std::system_category().message(error);
if (!handle) {
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Library name: {} failed to open base_path: {} with error: {} message: {}", name, basePath, error, message);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_OPEN;
}
initialize_fn initialize = reinterpret_cast<initialize_fn>(GetProcAddress(handle, "initialize"));
if (!initialize) {
error = GetLastError();
message = std::system_category().message(error);
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {} message: {}", name, error, message);
FreeLibrary(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
deinitialize_fn deinitialize = reinterpret_cast<deinitialize_fn>(GetProcAddress(handle, "deinitialize"));
if (!deinitialize) {
error = GetLastError();
message = std::system_category().message(error);
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {} message: {}", name, error, message);
FreeLibrary(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
execute_fn execute = reinterpret_cast<execute_fn>(GetProcAddress(handle, "execute"));
if (!execute) {
error = GetLastError();
message = std::system_category().message(error);
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {} message: {}", name, error, message);
FreeLibrary(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
metadata_fn getInputsInfo = reinterpret_cast<metadata_fn>(GetProcAddress(handle, "getInputsInfo"));
if (!getInputsInfo) {
error = GetLastError();
message = std::system_category().message(error);
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {} message: {}", name, error, message);
FreeLibrary(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
metadata_fn getOutputsInfo = reinterpret_cast<metadata_fn>(GetProcAddress(handle, "getOutputsInfo"));
if (!getOutputsInfo) {
error = GetLastError();
message = std::system_category().message(error);
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {} message: {}", name, error, message);
FreeLibrary(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
release_fn release = reinterpret_cast<release_fn>(GetProcAddress(handle, "release"));
if (!release) {
error = GetLastError();
message = std::system_category().message(error);
SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to load library name: {} with error: {} message: {}", name, error, message);
FreeLibrary(handle);
return StatusCode::NODE_LIBRARY_LOAD_FAILED_SYM;
}
libraries[name] = NodeLibrary{
initialize,
deinitialize,
execute,
getInputsInfo,
getOutputsInfo,
release,
basePath};
SPDLOG_LOGGER_INFO(modelmanager_logger, "Successfully loaded custom node library name: {}; base_path: {}", name, basePath);
#endif
return StatusCode::OK;
}
Status CustomNodeLibraryManager::getLibrary(const std::string& name, NodeLibrary& library) const {
auto it = libraries.find(name);
if (it == libraries.end()) {
return StatusCode::NODE_LIBRARY_MISSING;
} else {
library = it->second;
return StatusCode::OK;
}
}
void CustomNodeLibraryManager::unloadLibrariesRemovedFromConfig(const std::set<std::string>& librariesInConfig) {
std::set<std::string> librariesCurrentlyLoaded;
for (auto& library : libraries) {
librariesCurrentlyLoaded.emplace(library.first);
}
std::set<std::string> librariesToUnload;
std::set_difference(
librariesCurrentlyLoaded.begin(), librariesCurrentlyLoaded.end(),
librariesInConfig.begin(), librariesInConfig.end(),
std::inserter(librariesToUnload, librariesToUnload.end()));
for (auto& library : librariesToUnload) {
libraries.erase(library);
}
}
} // namespace ovms