Skip to content

Commit

Permalink
stored: rename variables and move a struct out of global scope
Browse files Browse the repository at this point in the history
- cleanup backend loader code
  • Loading branch information
franku committed May 7, 2020
1 parent 9cd0ee7 commit aefe6df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
39 changes: 24 additions & 15 deletions core/src/stored/sd_backends.cc
Expand Up @@ -55,6 +55,14 @@ static std::map<DeviceType, const char*> device_type_to_name_mapping = {
{DeviceType::B_UNKNOWN_DEV, nullptr}};


struct BackendDeviceLibraryDescriptor {
DeviceType device_type{DeviceType::B_UNKNOWN_DEV};

void* dynamic_library_handle{};
BackendInterface* backend_interface;
};


static std::vector<std::unique_ptr<BackendDeviceLibraryDescriptor>>
loaded_backends;
static std::vector<std::string> backend_directories;
Expand Down Expand Up @@ -88,18 +96,18 @@ Device* InitBackendDevice(JobControlRecord* jcr, DeviceType device_type)

for (const auto& b : loaded_backends) {
if (b->device_type == device_type) {
return b->backend->GetDevice(jcr, device_type);
return b->backend_interface->GetDevice(jcr, device_type);
}
}

// backend not loaded yet

t_backend_base GetBackend;

void* dl_handle = nullptr;
void* dynamic_library_handle = nullptr;

for (const auto& backend_dir : backend_directories) {
if (dl_handle != nullptr) { break; }
if (dynamic_library_handle != nullptr) { break; }

std::string shared_library_name = backend_dir + "/libbareossd-";
shared_library_name += interface_name;
Expand All @@ -116,8 +124,9 @@ Device* InitBackendDevice(JobControlRecord* jcr, DeviceType device_type)
return nullptr;
}

dl_handle = dlopen(shared_library_name.c_str(), RTLD_NOW);
if (dl_handle == nullptr) {
dynamic_library_handle = dlopen(shared_library_name.c_str(), RTLD_NOW);

if (dynamic_library_handle == nullptr) {
const char* error = get_dlerror();
Jmsg(jcr, M_ERROR, 0, _("Unable to load shared library: %s ERR=%s\n"),
shared_library_name.c_str(), error);
Expand All @@ -126,8 +135,8 @@ Device* InitBackendDevice(JobControlRecord* jcr, DeviceType device_type)
continue;
}

GetBackend =
reinterpret_cast<t_backend_base>(dlsym(dl_handle, "GetBackend"));
GetBackend = reinterpret_cast<t_backend_base>(
dlsym(dynamic_library_handle, "GetBackend"));

if (GetBackend == nullptr) {
const char* error = get_dlerror();
Expand All @@ -139,13 +148,13 @@ Device* InitBackendDevice(JobControlRecord* jcr, DeviceType device_type)
_("Lookup of GetBackend in shared library %s failed: "
"ERR=%s\n"),
shared_library_name.c_str(), error);
dlclose(dl_handle);
dl_handle = nullptr;
dlclose(dynamic_library_handle);
dynamic_library_handle = nullptr;
continue;
}
}

if (dl_handle == nullptr) { // none of the backends was loaded
if (dynamic_library_handle == nullptr) { // none of the backends was loaded
Jmsg(jcr, M_ERROR_TERM, 0,
_("Unable to load any shared library for libbareossd-%s%s\n"),
interface_name, DYN_LIB_EXTENSION);
Expand All @@ -154,19 +163,19 @@ Device* InitBackendDevice(JobControlRecord* jcr, DeviceType device_type)

auto b = std::make_unique<BackendDeviceLibraryDescriptor>();
b->device_type = device_type;
b->handle = dl_handle;
b->backend = GetBackend();
b->dynamic_library_handle = dynamic_library_handle;
b->backend_interface = GetBackend();

Device* d = b->backend->GetDevice(jcr, device_type);
Device* d = b->backend_interface->GetDevice(jcr, device_type);
loaded_backends.push_back(std::move(b));
return d;
}

void FlushAndCloseBackendDevices()
{
for (const auto& b : loaded_backends) {
b->backend->FlushDevice();
dlclose(b->handle);
b->backend_interface->FlushDevice();
dlclose(b->dynamic_library_handle);
}
loaded_backends.clear();
}
Expand Down
12 changes: 0 additions & 12 deletions core/src/stored/sd_backends.h
Expand Up @@ -44,18 +44,6 @@ typedef BackendInterface* (*t_backend_base)(void);
BackendInterface* GetBackend(void);
}

/**
* Loaded shared library with a certain backend interface type.
*/
struct BackendDeviceLibraryDescriptor {
DeviceType device_type{DeviceType::B_UNKNOWN_DEV};
void* handle{};
/*
* Entry points into loaded shared library.
*/
BackendInterface* backend;
};

#if defined(HAVE_WIN32)
#define DYN_LIB_EXTENSION ".dll"
#elif defined(HAVE_DARWIN_OS)
Expand Down

0 comments on commit aefe6df

Please sign in to comment.