ResolvStub.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  18. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  19. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  21. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  22. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  23. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  25. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <string>
  29. #include <dlfcn.h>
  30. #include <errno.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #define LOG_TAG "Netd"
  34. #include <log/log.h>
  35. #include <netd_resolv/resolv_stub.h>
  36. struct ResolvStub RESOLV_STUB;
  37. inline constexpr char APEX_LIB64_DIR[] = "/apex/com.android.resolv/lib64";
  38. inline constexpr char APEX_LIB_DIR[] = "/apex/com.android.resolv/lib";
  39. inline constexpr char LIBNAME[] = "libnetd_resolv.so";
  40. template <typename FunctionType>
  41. static void resolvStubInitFunction(void* handle, const char* symbol, FunctionType** stubPtr) {
  42. void* f = dlsym(handle, symbol);
  43. if (f == nullptr) {
  44. ALOGE("Can't find symbol %s in %s", symbol, LIBNAME);
  45. abort();
  46. }
  47. *stubPtr = reinterpret_cast<FunctionType*>(f);
  48. }
  49. int resolv_stub_init() {
  50. void* netdResolvHandle;
  51. for (const auto& dir : {APEX_LIB64_DIR, APEX_LIB_DIR}) {
  52. std::string path = std::string(dir) + "/" + LIBNAME;
  53. netdResolvHandle = dlopen(path.c_str(), RTLD_NOW);
  54. if (netdResolvHandle != nullptr) {
  55. ALOGI("Loaded resolver library from %s", path.c_str());
  56. break;
  57. }
  58. ALOGW("dlopen(%s) failed: %s", path.c_str(), dlerror());
  59. }
  60. if (netdResolvHandle == nullptr) {
  61. ALOGE("Fatal: couldn't open libnetd_resolv");
  62. abort();
  63. }
  64. #define STR(x) #x
  65. #define RESOLV_STUB_LOAD_SYMBOL(x) resolvStubInitFunction(netdResolvHandle, STR(x), &RESOLV_STUB.x)
  66. RESOLV_STUB_LOAD_SYMBOL(resolv_has_nameservers);
  67. RESOLV_STUB_LOAD_SYMBOL(resolv_init);
  68. #undef RESOLV_STUB_LOAD_SYMBOL
  69. #undef STR
  70. return 0;
  71. }