1 | #include "SharedLibrary.h" |
---|---|
2 | #include <string> |
3 | #include <boost/core/noncopyable.hpp> |
4 | #include <common/phdr_cache.h> |
5 | #include "Exception.h" |
6 | |
7 | |
8 | namespace DB |
9 | { |
10 | namespace ErrorCodes |
11 | { |
12 | extern const int CANNOT_DLOPEN; |
13 | extern const int CANNOT_DLSYM; |
14 | } |
15 | |
16 | SharedLibrary::SharedLibrary(const std::string & path, int flags) |
17 | { |
18 | handle = dlopen(path.c_str(), flags); |
19 | if (!handle) |
20 | throw Exception(std::string("Cannot dlopen: ") + dlerror(), ErrorCodes::CANNOT_DLOPEN); |
21 | |
22 | updatePHDRCache(); |
23 | |
24 | /// NOTE: race condition exists when loading multiple shared libraries concurrently. |
25 | /// We don't care (or add global mutex for this method). |
26 | } |
27 | |
28 | SharedLibrary::~SharedLibrary() |
29 | { |
30 | if (handle && dlclose(handle)) |
31 | std::terminate(); |
32 | } |
33 | |
34 | void * SharedLibrary::getImpl(const std::string & name, bool no_throw) |
35 | { |
36 | dlerror(); |
37 | |
38 | auto res = dlsym(handle, name.c_str()); |
39 | |
40 | if (char * error = dlerror()) |
41 | { |
42 | if (no_throw) |
43 | return nullptr; |
44 | throw Exception(std::string("Cannot dlsym: ") + error, ErrorCodes::CANNOT_DLSYM); |
45 | } |
46 | |
47 | return res; |
48 | } |
49 | } |
50 |