| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 24 апр. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/common/debug.h> |
| 23 |
#include <lsp-plug.in/ipc/Library.h> |
| 24 |
|
| 25 |
#ifdef PLATFORM_WINDOWS |
| 26 |
#include <windows.h> |
| 27 |
#endif /* PLATFORM_WINDOWS */ |
| 28 |
|
| 29 |
namespace lsp |
| 30 |
{ |
| 31 |
namespace ipc |
| 32 |
{ |
| 33 |
int Library::hTag = 0x12345678; |
| 34 |
|
| 35 |
Library::Library() |
| 36 |
{ |
| 37 |
hDlSym = NULL; |
| 38 |
nLastError = STATUS_OK; |
| 39 |
} |
| 40 |
|
| 41 |
Library::~Library() |
| 42 |
{ |
| 43 |
close(); |
| 44 |
} |
| 45 |
|
| 46 |
status_t Library::open(const char *path, bool new_namespace) |
| 47 |
{ |
| 48 |
if (path == NULL) |
| 49 |
return nLastError = STATUS_BAD_ARGUMENTS; |
| 50 |
LSPString tmp; |
| 51 |
if (!tmp.set_utf8(path)) |
| 52 |
return nLastError = STATUS_NO_MEM; |
| 53 |
return open(&tmp); |
| 54 |
} |
| 55 |
|
| 56 |
status_t Library::open(const LSPString *path, bool new_namespace) |
| 57 |
{ |
| 58 |
if (path == NULL) |
| 59 |
return nLastError = STATUS_BAD_ARGUMENTS; |
| 60 |
if (hDlSym != NULL) |
| 61 |
return nLastError = STATUS_OPENED; |
| 62 |
|
| 63 |
#ifdef PLATFORM_WINDOWS |
| 64 |
const WCHAR *str = path->get_utf16(); |
| 65 |
if (str == NULL) |
| 66 |
return STATUS_NO_MEM; |
| 67 |
HMODULE handle = ::LoadLibraryW(str); |
| 68 |
|
| 69 |
if (handle == NULL) |
| 70 |
return nLastError = STATUS_NOT_FOUND; |
| 71 |
#else |
| 72 |
const char *str = path->get_utf8(); |
| 73 |
if (str == NULL) |
| 74 |
return STATUS_NO_MEM; |
| 75 |
|
| 76 |
#ifdef _GNU_SOURCE |
| 77 |
void * const handle = (new_namespace) ? |
| 78 |
::dlmopen(LM_ID_NEWLM, str, RTLD_NOW) : |
| 79 |
::dlopen(str, RTLD_NOW); |
| 80 |
#else |
| 81 |
void * const handle = ::dlopen(str, RTLD_NOW); |
| 82 |
#endif /* _GNU_SOURCE */ |
| 83 |
if (handle == NULL) |
| 84 |
{ |
| 85 |
lsp_warn("Error loading module %s: %s", path->get_native(), ::dlerror()); |
| 86 |
return nLastError = STATUS_NOT_FOUND; |
| 87 |
} |
| 88 |
#endif |
| 89 |
|
| 90 |
hDlSym = handle; |
| 91 |
return nLastError = STATUS_OK; |
| 92 |
} |
| 93 |
|
| 94 |
status_t Library::open(const io::Path *path, bool new_namespace) |
| 95 |
{ |
| 96 |
if (path == NULL) |
| 97 |
return nLastError = STATUS_BAD_ARGUMENTS; |
| 98 |
return open(path->as_string()); |
| 99 |
} |
| 100 |
|
| 101 |
void *Library::import(const char *name) |
| 102 |
{ |
| 103 |
if (name == NULL) |
| 104 |
{ |
| 105 |
nLastError = STATUS_BAD_ARGUMENTS; |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
if (hDlSym == NULL) |
| 109 |
{ |
| 110 |
nLastError = STATUS_BAD_STATE; |
| 111 |
return NULL; |
| 112 |
} |
| 113 |
|
| 114 |
#ifdef PLATFORM_WINDOWS |
| 115 |
void *ptr = reinterpret_cast<void *>(::GetProcAddress(hDlSym, name)); |
| 116 |
#else |
| 117 |
void *ptr = ::dlsym(hDlSym, name); |
| 118 |
#endif |
| 119 |
|
| 120 |
if (ptr == NULL) |
| 121 |
nLastError = STATUS_NOT_FOUND; |
| 122 |
else |
| 123 |
nLastError = STATUS_OK; |
| 124 |
|
| 125 |
return ptr; |
| 126 |
} |
| 127 |
|
| 128 |
void *Library::import(const LSPString *name) |
| 129 |
{ |
| 130 |
if (name == NULL) |
| 131 |
{ |
| 132 |
nLastError = STATUS_BAD_ARGUMENTS; |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
if (hDlSym == NULL) |
| 136 |
{ |
| 137 |
nLastError = STATUS_BAD_STATE; |
| 138 |
return NULL; |
| 139 |
} |
| 140 |
return import(name->get_utf8()); |
| 141 |
} |
| 142 |
|
| 143 |
status_t Library::close() |
| 144 |
{ |
| 145 |
if (hDlSym == NULL) |
| 146 |
return nLastError = STATUS_OK; |
| 147 |
#ifdef PLATFORM_WINDOWS |
| 148 |
::FreeLibrary(hDlSym); |
| 149 |
#else |
| 150 |
::dlclose(hDlSym); |
| 151 |
#endif |
| 152 |
|
| 153 |
hDlSym = NULL; |
| 154 |
return nLastError = STATUS_OK; |
| 155 |
} |
| 156 |
|
| 157 |
void Library::swap(Library *dst) |
| 158 |
{ |
| 159 |
lsp::swap(dst->hDlSym, hDlSym); |
| 160 |
lsp::swap(dst->nLastError, nLastError); |
| 161 |
} |
| 162 |
|
| 163 |
status_t Library::get_module_file(LSPString *path, const void *ptr) |
| 164 |
{ |
| 165 |
if (path == NULL) |
| 166 |
return STATUS_BAD_ARGUMENTS; |
| 167 |
|
| 168 |
#ifdef PLATFORM_WINDOWS |
| 169 |
// Get module handle |
| 170 |
HMODULE hm = NULL; |
| 171 |
|
| 172 |
if (!::GetModuleHandleExW( |
| 173 |
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 174 |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 175 |
reinterpret_cast<LPCWSTR>(ptr), |
| 176 |
&hm)) |
| 177 |
return STATUS_NOT_FOUND; |
| 178 |
|
| 179 |
// Get the path to the module |
| 180 |
DWORD capacity = PATH_MAX + 1, length = 0; |
| 181 |
WCHAR *xpath = NULL; |
| 182 |
lsp_finally { |
| 183 |
if (xpath != NULL) |
| 184 |
::free(xpath); |
| 185 |
}; |
| 186 |
|
| 187 |
while (true) |
| 188 |
{ |
| 189 |
// Allocate the buffer |
| 190 |
WCHAR *new_xpath = reinterpret_cast<WCHAR *>(::realloc(xpath, capacity * sizeof(WCHAR))); |
| 191 |
if (new_xpath == NULL) |
| 192 |
return STATUS_NO_MEM; |
| 193 |
xpath = new_xpath; |
| 194 |
|
| 195 |
// Try to obtain the file name |
| 196 |
length = ::GetModuleFileNameW(hm, xpath, capacity); |
| 197 |
if (length == 0) |
| 198 |
return STATUS_NOT_FOUND; |
| 199 |
|
| 200 |
// Analyze result |
| 201 |
DWORD error = GetLastError(); |
| 202 |
if (error == ERROR_SUCCESS) |
| 203 |
break; |
| 204 |
else if (error != ERROR_INSUFFICIENT_BUFFER) |
| 205 |
return STATUS_UNKNOWN_ERR; |
| 206 |
|
| 207 |
// Increase capacity by 1.5 |
| 208 |
capacity += (capacity >> 1); |
| 209 |
} |
| 210 |
|
| 211 |
if (!path->set_utf16(xpath, length)) |
| 212 |
return STATUS_NO_MEM; |
| 213 |
#else |
| 214 |
Dl_info dli; |
| 215 |
int res = ::dladdr(const_cast<void *>(ptr), &dli); |
| 216 |
if ((res == 0) || (dli.dli_fname == NULL)) |
| 217 |
return STATUS_NOT_FOUND; |
| 218 |
|
| 219 |
if (!path->set_native(dli.dli_fname)) |
| 220 |
return STATUS_NO_MEM; |
| 221 |
#endif /* PLATFORM_WINDOWS */ |
| 222 |
|
| 223 |
return STATUS_OK; |
| 224 |
} |
| 225 |
|
| 226 |
status_t Library::get_module_file(io::Path *path, const void *ptr) |
| 227 |
{ |
| 228 |
if (path == NULL) |
| 229 |
return STATUS_BAD_ARGUMENTS; |
| 230 |
|
| 231 |
LSPString tmp; |
| 232 |
status_t res = get_module_file(&tmp, ptr); |
| 233 |
if (res != STATUS_OK) |
| 234 |
return res; |
| 235 |
|
| 236 |
return path->set(&tmp); |
| 237 |
} |
| 238 |
|
| 239 |
bool Library::valid_library_name(const char *path) |
| 240 |
{ |
| 241 |
if (path == NULL) |
| 242 |
return false; |
| 243 |
io::Path tmp; |
| 244 |
if (tmp.set(path) != STATUS_OK) |
| 245 |
return false; |
| 246 |
return valid_library_name(&tmp); |
| 247 |
} |
| 248 |
|
| 249 |
bool Library::valid_library_name(const LSPString *path) |
| 250 |
{ |
| 251 |
if (path == NULL) |
| 252 |
return false; |
| 253 |
|
| 254 |
io::Path tmp; |
| 255 |
if (tmp.set(path) != STATUS_OK) |
| 256 |
return false; |
| 257 |
|
| 258 |
return valid_library_name(&tmp); |
| 259 |
} |
| 260 |
|
| 261 |
bool Library::valid_library_name(const io::Path *path) |
| 262 |
{ |
| 263 |
if (path == NULL) |
| 264 |
return false; |
| 265 |
LSPString tmp; |
| 266 |
if (path->get_last(&tmp) != STATUS_OK) |
| 267 |
return false; |
| 268 |
|
| 269 |
#if !FILE_SYSTEM_CASE_SENSE |
| 270 |
tmp.tolower(); |
| 271 |
#endif |
| 272 |
|
| 273 |
LSPString ext; |
| 274 |
if (!ext.set_utf8(FILE_LIBRARY_EXT_S)) |
| 275 |
return false; |
| 276 |
|
| 277 |
return tmp.ends_with(&ext); |
| 278 |
} |
| 279 |
|
| 280 |
} /* namespace io */ |
| 281 |
} /* namespace lsp */ |