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 <fstream>
6#include <iostream>
7#include <mutex>
8#include <sstream>
9#include <string>
10#include <utility>
11#include <dlfcn.h>
12#include <unistd.h>
13
14//#include "RuntimeExceptions.h"
15#include "FullPath.h"
16
17namespace {
19{
20 static std::mutex mutex;
21 static int count = 0;
22 std::lock_guard<std::mutex> lock(mutex);
23 return count++;
24}
25} // namespace
26
27DynamicLibrary::DynamicLibrary(std::string libname_param, bool unique_name) : fLibName(std::move(libname_param))
28{
29 if(unique_name) {
30 std::ostringstream str;
31 str << "/tmp/temp_dynlib_" << getpid() << "_" << incremental_id() << ".so";
32 fTempName = str.str();
33
34 // Need to symlink to full path, not a relative path.
35 // If a relative path is given, then the symlink will look for that library
36 // relative to /tmp, instead of relative to the current directory.
38
39 int error = symlink(fLibName.c_str(), fTempName.c_str());
40 if(error != 0) {
41 return;
42 // throw RuntimeSymlinkCreation("Could not make temp symlink");
43 }
44 fLibrary = dlopen(fTempName.c_str(), RTLD_NOW);
45 } else {
46 fLibrary = dlopen(fLibName.c_str(), RTLD_NOW);
47 }
48
49 if(fLibrary == nullptr) {
50 throw std::runtime_error(dlerror());
51 }
52}
53
55{
56 if(fLibrary != nullptr) {
57 dlclose(fLibrary);
58 if(fTempName.length() != 0u) {
59 unlink(fTempName.c_str());
60 }
61 }
62}
63
64DynamicLibrary::DynamicLibrary(DynamicLibrary&& other) noexcept : fLibrary(nullptr)
65{
66 swap(other);
67}
68
70{
71 swap(other);
72 return *this;
73}
74
76{
77 std::swap(fLibrary, other.fLibrary);
78 std::swap(fLibName, other.fLibName);
79 std::swap(fTempName, other.fTempName);
80}
81
82void* DynamicLibrary::GetSymbol(const char* symbol)
83{
84 return dlsym(fLibrary, symbol);
85}
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.
void swap(DynamicLibrary &other)
std::string fLibName