forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSharedLibrary.cpp
49 lines (38 loc) · 1.21 KB
/
SharedLibrary.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
#include "SharedLibrary.h"
#include <string>
#include <base/phdr_cache.h>
#include <Common/Exception.h>
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_DLOPEN;
extern const int CANNOT_DLSYM;
}
SharedLibrary::SharedLibrary(std::string_view path, int flags)
{
handle = dlopen(path.data(), flags);
if (!handle)
throw Exception(ErrorCodes::CANNOT_DLOPEN, "Cannot dlopen: ({})", dlerror()); // NOLINT(concurrency-mt-unsafe) // MT-Safe on Linux, see man dlerror
updatePHDRCache();
/// NOTE: race condition exists when loading multiple shared libraries concurrently.
/// We don't care (or add global mutex for this method).
}
SharedLibrary::~SharedLibrary()
{
if (handle && dlclose(handle))
std::terminate();
}
void * SharedLibrary::getImpl(std::string_view name, bool no_throw)
{
dlerror(); // NOLINT(concurrency-mt-unsafe) // MT-Safe on Linux, see man dlerror
auto * res = dlsym(handle, name.data());
if (char * error = dlerror()) // NOLINT(concurrency-mt-unsafe) // MT-Safe on Linux, see man dlerror
{
if (no_throw)
return nullptr;
throw Exception(ErrorCodes::CANNOT_DLSYM, "Cannot dlsym: ({})", error);
}
return res;
}
}