Lorenzo Colitti | afaaa8e | 2018-12-18 19:16:12 +0900 | [diff] [blame] | 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 | |
| 29 | #include <string> |
| 30 | |
| 31 | #include <dlfcn.h> |
| 32 | #include <errno.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | #define LOG_TAG "Netd" |
| 37 | #include <log/log.h> |
| 38 | |
| 39 | #include <netd_resolv/resolv_stub.h> |
| 40 | |
| 41 | struct ResolvStub RESOLV_STUB; |
| 42 | |
| 43 | inline constexpr char APEX_LIB64_DIR[] = "/apex/com.android.resolv/lib64"; |
| 44 | inline constexpr char APEX_LIB_DIR[] = "/apex/com.android.resolv/lib"; |
| 45 | inline constexpr char LIBNAME[] = "libnetd_resolv.so"; |
| 46 | |
| 47 | template <typename FunctionType> |
| 48 | static void resolvStubInitFunction(void* handle, const char* symbol, FunctionType** stubPtr) { |
| 49 | void* f = dlsym(handle, symbol); |
| 50 | if (f == nullptr) { |
| 51 | ALOGE("Can't find symbol %s in %s", symbol, LIBNAME); |
| 52 | abort(); |
| 53 | } |
| 54 | *stubPtr = reinterpret_cast<FunctionType*>(f); |
| 55 | } |
| 56 | |
| 57 | int resolv_stub_init() { |
| 58 | void* netdResolvHandle; |
| 59 | |
| 60 | for (const auto& dir : {APEX_LIB64_DIR, APEX_LIB_DIR}) { |
| 61 | std::string path = std::string(dir) + "/" + LIBNAME; |
| 62 | netdResolvHandle = dlopen(path.c_str(), RTLD_NOW); |
| 63 | if (netdResolvHandle != nullptr) { |
Lorenzo Colitti | 80332d4 | 2018-12-21 08:48:39 +0900 | [diff] [blame] | 64 | ALOGI("Loaded resolver library from %s", path.c_str()); |
Lorenzo Colitti | afaaa8e | 2018-12-18 19:16:12 +0900 | [diff] [blame] | 65 | break; |
| 66 | } |
| 67 | ALOGW("dlopen(%s) failed: %s", path.c_str(), strerror(errno)); |
| 68 | } |
| 69 | |
| 70 | if (netdResolvHandle == nullptr) { |
| 71 | ALOGE("Fatal: couldn't open libnetd_resolv"); |
| 72 | abort(); |
| 73 | } |
| 74 | |
| 75 | #define STR(x) #x |
| 76 | #define RESOLV_STUB_LOAD_SYMBOL(x) resolvStubInitFunction(netdResolvHandle, STR(x), &RESOLV_STUB.x) |
Lorenzo Colitti | afaaa8e | 2018-12-18 19:16:12 +0900 | [diff] [blame] | 77 | RESOLV_STUB_LOAD_SYMBOL(resolv_has_nameservers); |
Mike Yu | 0ae31af | 2018-11-15 21:58:19 +0800 | [diff] [blame] | 78 | RESOLV_STUB_LOAD_SYMBOL(resolv_init); |
Lorenzo Colitti | afaaa8e | 2018-12-18 19:16:12 +0900 | [diff] [blame] | 79 | #undef RESOLV_STUB_LOAD_SYMBOL |
| 80 | #undef STR |
| 81 | |
| 82 | return 0; |
| 83 | } |