2017-09-05 01:08:26 +00:00
|
|
|
#include "SharedLibrary.h"
|
|
|
|
#include <string>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/phdr_cache.h>
|
2022-09-17 01:02:34 +00:00
|
|
|
#include <Common/Exception.h>
|
2017-09-05 01:08:26 +00:00
|
|
|
|
2018-12-14 19:28:37 +00:00
|
|
|
|
2017-09-05 01:08:26 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_DLOPEN;
|
|
|
|
extern const int CANNOT_DLSYM;
|
|
|
|
}
|
|
|
|
|
2021-03-14 14:19:48 +00:00
|
|
|
SharedLibrary::SharedLibrary(std::string_view path, int flags)
|
2017-09-05 01:08:26 +00:00
|
|
|
{
|
2021-03-14 11:29:02 +00:00
|
|
|
handle = dlopen(path.data(), flags);
|
2017-09-05 01:08:26 +00:00
|
|
|
if (!handle)
|
2022-08-21 18:24:17 +00:00
|
|
|
throw Exception(ErrorCodes::CANNOT_DLOPEN, "Cannot dlopen: ({})", dlerror()); // NOLINT(concurrency-mt-unsafe) // MT-Safe on Linux, see man dlerror
|
2019-07-25 19:56:51 +00:00
|
|
|
|
|
|
|
updatePHDRCache();
|
2019-07-28 14:55:02 +00:00
|
|
|
|
|
|
|
/// NOTE: race condition exists when loading multiple shared libraries concurrently.
|
|
|
|
/// We don't care (or add global mutex for this method).
|
2017-09-05 01:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedLibrary::~SharedLibrary()
|
|
|
|
{
|
|
|
|
if (handle && dlclose(handle))
|
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
|
2021-03-14 14:19:48 +00:00
|
|
|
void * SharedLibrary::getImpl(std::string_view name, bool no_throw)
|
2017-09-05 01:08:26 +00:00
|
|
|
{
|
2022-08-21 18:24:17 +00:00
|
|
|
dlerror(); // NOLINT(concurrency-mt-unsafe) // MT-Safe on Linux, see man dlerror
|
2017-09-05 01:08:26 +00:00
|
|
|
|
2021-03-14 11:29:02 +00:00
|
|
|
auto * res = dlsym(handle, name.data());
|
2017-09-05 01:08:26 +00:00
|
|
|
|
2022-08-21 18:24:17 +00:00
|
|
|
if (char * error = dlerror()) // NOLINT(concurrency-mt-unsafe) // MT-Safe on Linux, see man dlerror
|
2017-09-05 01:08:26 +00:00
|
|
|
{
|
|
|
|
if (no_throw)
|
|
|
|
return nullptr;
|
2021-03-14 11:29:02 +00:00
|
|
|
|
|
|
|
throw Exception(ErrorCodes::CANNOT_DLSYM, "Cannot dlsym: ({})", error);
|
2017-09-05 01:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|