summaryrefslogtreecommitdiff
path: root/utils/loc_misc_utils.cpp
diff options
context:
space:
mode:
authorKevin Tang <zhikait@codeaurora.org>2019-02-25 13:31:49 -0800
committerGerrit - the friendly Code Review server <code-review@localhost>2019-02-25 16:36:26 -0800
commita928d60188ff466fce61c2c83e381790c4497426 (patch)
tree9b800e82b1e962438ddf3185e47d415d00938301 /utils/loc_misc_utils.cpp
parentd31be6470042dd744a408c207d2b5b7fb7839959 (diff)
downloadgps-a928d60188ff466fce61c2c83e381790c4497426.tar.gz
util to get get symbol from a library
Added dlGetSymFromLib() to provide a utility that dlopen()'s a library, and get the pointer to a symbol name. Change-Id: I770eaef18a61aaa458833b3ca778f44b299585f5 CRs-Fixed: 2404939
Diffstat (limited to 'utils/loc_misc_utils.cpp')
-rw-r--r--utils/loc_misc_utils.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/utils/loc_misc_utils.cpp b/utils/loc_misc_utils.cpp
index b7c8406..70fdbc3 100644
--- a/utils/loc_misc_utils.cpp
+++ b/utils/loc_misc_utils.cpp
@@ -30,6 +30,7 @@
#define LOG_TAG "LocSvc_misc_utils"
#include <stdio.h>
#include <string.h>
+#include <dlfcn.h>
#include <log_util.h>
#include <loc_misc_utils.h>
#include <ctype.h>
@@ -112,3 +113,33 @@ void loc_util_trim_space(char *org_string)
err:
return;
}
+
+inline void logDlError(const char* failedCall) {
+ const char * err = dlerror();
+ LOC_LOGe("%s error: %s", failedCall, (nullptr == err) ? "unknown" : err);
+}
+
+void* dlGetSymFromLib(void*& libHandle, const char* libName, const char* symName)
+{
+ void* sym = nullptr;
+ if ((nullptr != libHandle || nullptr != libName) && nullptr != symName) {
+ if (nullptr == libHandle) {
+ libHandle = dlopen(libName, RTLD_NOW);
+ if (nullptr == libHandle) {
+ logDlError("dlopen");
+ }
+ }
+ // NOT else, as libHandle gets assigned 5 line above
+ if (nullptr != libHandle) {
+ sym = dlsym(libHandle, symName);
+ if (nullptr == sym) {
+ logDlError("dlsym");
+ }
+ }
+ } else {
+ LOC_LOGe("Either libHandle (%p) or libName (%p) must not be null; "
+ "symName (%p) can not be null.", libHandle, libName, symName);
+ }
+
+ return sym;
+}