1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #include <string>
- #include <dlfcn.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- #define LOG_TAG "Netd"
- #include <log/log.h>
- #include <netd_resolv/resolv_stub.h>
- struct ResolvStub RESOLV_STUB;
- inline constexpr char APEX_LIB64_DIR[] = "/apex/com.android.resolv/lib64";
- inline constexpr char APEX_LIB_DIR[] = "/apex/com.android.resolv/lib";
- inline constexpr char LIBNAME[] = "libnetd_resolv.so";
- template <typename FunctionType>
- static void resolvStubInitFunction(void* handle, const char* symbol, FunctionType** stubPtr) {
- void* f = dlsym(handle, symbol);
- if (f == nullptr) {
- ALOGE("Can't find symbol %s in %s", symbol, LIBNAME);
- abort();
- }
- *stubPtr = reinterpret_cast<FunctionType*>(f);
- }
- int resolv_stub_init() {
- void* netdResolvHandle;
- for (const auto& dir : {APEX_LIB64_DIR, APEX_LIB_DIR}) {
- std::string path = std::string(dir) + "/" + LIBNAME;
- netdResolvHandle = dlopen(path.c_str(), RTLD_NOW);
- if (netdResolvHandle != nullptr) {
- ALOGI("Loaded resolver library from %s", path.c_str());
- break;
- }
- ALOGW("dlopen(%s) failed: %s", path.c_str(), dlerror());
- }
- if (netdResolvHandle == nullptr) {
- ALOGE("Fatal: couldn't open libnetd_resolv");
- abort();
- }
- #define STR(x) #x
- #define RESOLV_STUB_LOAD_SYMBOL(x) resolvStubInitFunction(netdResolvHandle, STR(x), &RESOLV_STUB.x)
- RESOLV_STUB_LOAD_SYMBOL(resolv_has_nameservers);
- RESOLV_STUB_LOAD_SYMBOL(resolv_init);
- #undef RESOLV_STUB_LOAD_SYMBOL
- #undef STR
- return 0;
- }
|