summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTimur Alperovich <timuralp@swiftstack.com>2016-03-07 15:02:53 -0800
committerTimur Alperovich <timuralp@swiftstack.com>2016-03-08 22:06:19 -0800
commitc7a94df0724af30b26e3856f9c14344fc9b73a09 (patch)
tree62d1f0b4d5de236303a9c7f659eb48f916ed0f50 /src
parent1785a5894151a8422020516797655be4024dc12b (diff)
downloadliberasurecode-c7a94df0724af30b26e3856f9c14344fc9b73a09.tar.gz
Add a method to check if a backend is present.
Uses dlopen to check if a backend is present. This may be used by consumers who need to check which backends are present on a system. Issue #23
Diffstat (limited to 'src')
-rw-r--r--src/erasurecode.c51
1 files changed, 32 insertions, 19 deletions
diff --git a/src/erasurecode.c b/src/erasurecode.c
index 13e5f02..6528eec 100644
--- a/src/erasurecode.c
+++ b/src/erasurecode.c
@@ -168,21 +168,10 @@ static void print_dlerror(const char *caller)
}
/* Generic dlopen/dlclose routines */
-int liberasurecode_backend_open(ec_backend_t instance)
+void* liberasurecode_backend_open(ec_backend_t instance)
{
- if (instance && NULL != instance->desc.backend_sohandle)
- return 0;
-
/* Use RTLD_LOCAL to avoid symbol collisions */
- instance->desc.backend_sohandle = dlopen(instance->common.soname,
- RTLD_LAZY | RTLD_LOCAL);
- if (NULL == instance->desc.backend_sohandle) {
- print_dlerror(__func__);
- return -EBACKENDNOTAVAIL;
- }
-
- dlerror(); /* Clear any existing errors */
- return 0;
+ return dlopen(instance->common.soname, RTLD_LAZY | RTLD_LOCAL);
}
int liberasurecode_backend_close(ec_backend_t instance)
@@ -226,6 +215,28 @@ liberasurecode_exit(void) {
/* =~=*=~==~=*=~= liberasurecode frontend API implementation =~=*=~==~=*=~== */
/**
+ * Checks if a given backend is available.
+ *
+ * @param backend_id - one of the supported backends.
+ *
+ * @returns 1 if a backend is available; 0 otherwise
+ */
+int liberasurecode_backend_available(const ec_backend_id_t backend_id) {
+ struct ec_backend backend;
+ if (backend_id >= EC_BACKENDS_MAX)
+ return 0;
+
+ backend.desc.backend_sohandle = liberasurecode_backend_open(
+ ec_backends_supported[backend_id]);
+ if (!backend.desc.backend_sohandle) {
+ return 0;
+ }
+
+ liberasurecode_backend_close(&backend);
+ return 1;
+}
+
+/**
* Create a liberasurecode instance and return a descriptor
* for use with EC operations (encode, decode, reconstruct)
*
@@ -247,7 +258,6 @@ liberasurecode_exit(void) {
int liberasurecode_instance_create(const ec_backend_id_t id,
struct ec_args *args)
{
- int err = 0;
ec_backend_t instance = NULL;
struct ec_backend_args bargs;
if (!args)
@@ -274,11 +284,14 @@ int liberasurecode_instance_create(const ec_backend_id_t id,
/* Open backend .so if not already open */
/* .so handle is returned in instance->desc.backend_sohandle */
- err = liberasurecode_backend_open(instance);
- if (err < 0) {
- /* ignore during init, return the same handle */
- free(instance);
- return err;
+ if (!instance->desc.backend_sohandle) {
+ instance->desc.backend_sohandle = liberasurecode_backend_open(instance);
+ if (!instance->desc.backend_sohandle) {
+ /* ignore during init, return the same handle */
+ print_dlerror(__func__);
+ free(instance);
+ return -EBACKENDNOTAVAIL;
+ }
}
/* Call private init() for the backend */