Devi Sandeep Endluri V V | 0ca32a9 | 2016-05-02 14:46:38 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #ifndef _PROPCLIENT_H_ |
| 31 | #define _PROPCLIENT_H_ |
| 32 | |
| 33 | #ifdef LIBC_STATIC |
| 34 | #error PropClient.cpp should NOT be included in static libc builds. |
| 35 | #endif |
| 36 | |
| 37 | #include "private/libc_logging.h" |
| 38 | #include <dlfcn.h> |
| 39 | #include <pthread.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | #include "codeaurora/PropClientDispatch.h" |
| 43 | #include "codeaurora/PropClientDispatchWrite.h" |
| 44 | |
| 45 | static pthread_once_t propClientInitOnce = PTHREAD_ONCE_INIT; |
| 46 | |
| 47 | template <typename FunctionType> |
| 48 | static void propClientInitFunction(void* handle, const char* symbol, FunctionType* function) { |
| 49 | typedef void (*InitFunctionType)(FunctionType*); |
| 50 | InitFunctionType initFunction = reinterpret_cast<InitFunctionType>(dlsym(handle, symbol)); |
| 51 | if (initFunction != NULL) { |
| 52 | initFunction(function); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static void propClientInitImpl() { |
| 57 | |
| 58 | void *propClientHandle = 0; |
| 59 | typedef bool (*VendorExtFunctionType)(); |
| 60 | bool vendorExtFlag = false; |
| 61 | propClientHandle = dlopen("libvendorconn.so", RTLD_NOW); |
| 62 | |
| 63 | if (propClientHandle != NULL) { |
| 64 | VendorExtFunctionType vendorExtFunction = reinterpret_cast<VendorExtFunctionType>(dlsym(propClientHandle, "isVendorExtAvailable")); |
| 65 | if( vendorExtFunction ) { |
| 66 | vendorExtFlag = vendorExtFunction(); |
| 67 | } |
| 68 | if( !vendorExtFlag || !vendorExtFunction ) { |
| 69 | dlclose(propClientHandle); |
| 70 | propClientHandle = NULL; |
| 71 | return; |
| 72 | } |
| 73 | } else { |
| 74 | // If the library is not available, it's not an error. We'll just use |
| 75 | // default implementations of functions that it would've overridden. |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | propClientInitFunction(propClientHandle, "propClientInitSocket", &__propClientDispatch.propSocket); |
| 80 | |
| 81 | propClientInitFunction(propClientHandle, "propClientInitConnect", &__propClientDispatch.propConnect); |
| 82 | |
| 83 | propClientInitFunction(propClientHandle, "propClientInitWrite", &__propClientDispatchWrite.propWrite); |
| 84 | |
| 85 | propClientInitFunction(propClientHandle, "propClientInitWritev", &__propClientDispatchWrite.propWritev); |
| 86 | |
| 87 | propClientInitFunction(propClientHandle, "propClientInitSendmsg", &__propClientDispatchWrite.propSendmsg); |
| 88 | |
| 89 | propClientInitFunction(propClientHandle, "propClientInitSendto", &__propClientDispatchWrite.propSendto); |
| 90 | |
| 91 | propClientInitFunction(propClientHandle, "propClientInitGetHostByNameForNet", &__propClientDispatch.propGetHostByNameForNet); |
| 92 | |
| 93 | propClientInitFunction(propClientHandle, "propClientInitGetHostByAddrForNet", &__propClientDispatch.propGetHostByAddrForNet); |
| 94 | |
| 95 | propClientInitFunction(propClientHandle, "propClientInitGetAddrInfoForNet", &__propClientDispatch.propGetAddrInfoForNet); |
| 96 | |
| 97 | propClientInitFunction(propClientHandle, "propClientInitSendDnsReport", &__propClientDispatch.propSendDnsReport); |
| 98 | |
| 99 | propClientInitFunction(propClientHandle, "propClientInitClose", &__propClientDispatch.propClose); |
| 100 | } |
| 101 | |
| 102 | extern "C" __LIBC_HIDDEN__ void propClientInit() { |
| 103 | if (pthread_once(&propClientInitOnce, propClientInitImpl)) { |
| 104 | __libc_format_log(ANDROID_LOG_ERROR, "propClient", "Failed to initialize prop_client"); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | #endif /* !_PROPCLIENT_H_ */ |