GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
DynamicLibrary.cxx
Go to the documentation of this file.
1#include "DynamicLibrary.h"
2
3#include <cstdlib>
4#include <algorithm>
5#include <iostream>
6#include <mutex>
7#include <sstream>
8#include <string>
9#include <utility>
10#include <dlfcn.h>
11#include <unistd.h>
12
13#include "FullPath.h"
14
15namespace {
17{
18 static std::mutex mutex;
19 static int count = 0;
20 std::lock_guard<std::mutex> lock(mutex);
21 return count++;
22}
23} // namespace
24
25DynamicLibrary::DynamicLibrary(std::string libname_param, bool unique_name) : fLibName(std::move(libname_param))
26{
27 if(unique_name) {
28 std::ostringstream str;
29 str << "/tmp/temp_dynlib_" << getpid() << "_" << incremental_id() << ".so";
30 fTempName = str.str();
31
32 // Need to symlink to full path, not a relative path.
33 // If a relative path is given, then the symlink will look for that library
34 // relative to /tmp, instead of relative to the current directory.
36
37 int error = symlink(fLibName.c_str(), fTempName.c_str());
38 if(error != 0) {
39 return;
40 // throw RuntimeSymlinkCreation("Could not make temp symlink");
41 }
42 fLibrary = dlopen(fTempName.c_str(), RTLD_NOW);
43 } else {
44 fLibrary = dlopen(fLibName.c_str(), RTLD_NOW);
45 }
46
47 if(fLibrary == nullptr) {
48 throw std::runtime_error(dlerror());
49 }
50}
51
53{
54 if(fLibrary != nullptr) {
55 dlclose(fLibrary);
56 if(!fTempName.empty()) {
57 unlink(fTempName.c_str());
58 }
59 }
60}
61
62DynamicLibrary::DynamicLibrary(DynamicLibrary&& other) noexcept : fLibrary(nullptr)
63{
64 swap(other);
65}
66
68{
69 swap(other);
70 return *this;
71}
72
74{
75 std::swap(fLibrary, other.fLibrary);
76 std::swap(fLibName, other.fLibName);
77 std::swap(fTempName, other.fTempName);
78}
79
80void* DynamicLibrary::GetSymbol(const char* symbol)
81{
82 return dlsym(fLibrary, symbol);
83}
std::string full_path(const std::string &path)
Definition FullPath.cxx:9
#define dlsym
Loads a Shared Object library.
DynamicLibrary(std::string libname_param, bool unique_name=false)
Loads a shared object library.
~DynamicLibrary()
Destructs the shared object library.
DynamicLibrary & operator=(DynamicLibrary &&other) noexcept
Move assignment operator.
std::string fTempName
void * GetSymbol(const char *symbol)
Extracts a symbol from the shared library.
std::string fLibName
void swap(DynamicLibrary &other) noexcept