ClickHouse/src/Common/SharedLibrary.cpp

51 lines
1.1 KiB
C++
Raw Normal View History

#include "SharedLibrary.h"
#include <string>
#include <boost/core/noncopyable.hpp>
2019-07-25 19:56:51 +00:00
#include <common/phdr_cache.h>
#include "Exception.h"
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_DLOPEN;
extern const int CANNOT_DLSYM;
}
2021-03-14 11:29:02 +00:00
SharedLibrary::SharedLibrary(const std::string_view & path, int flags)
{
2021-03-14 11:29:02 +00:00
handle = dlopen(path.data(), flags);
if (!handle)
2021-03-14 11:29:02 +00:00
throw Exception(ErrorCodes::CANNOT_DLOPEN, "Cannot dlopen: ({})", 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).
}
SharedLibrary::~SharedLibrary()
{
if (handle && dlclose(handle))
std::terminate();
}
2021-03-14 11:29:02 +00:00
void * SharedLibrary::getImpl(const std::string_view & name, bool no_throw)
{
dlerror();
2021-03-14 11:29:02 +00:00
auto * res = dlsym(handle, name.data());
if (char * error = dlerror())
{
if (no_throw)
return nullptr;
2021-03-14 11:29:02 +00:00
throw Exception(ErrorCodes::CANNOT_DLSYM, "Cannot dlsym: ({})", error);
}
return res;
}
}