| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2014 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
| Sreeram Ramachandran | 8205a61 | 2014-05-13 17:24:03 -0700 | [diff] [blame] | 17 | #ifdef LIBC_STATIC | 
 | 18 | #error NetdClient.cpp should NOT be included in static libc builds. | 
 | 19 | #endif | 
 | 20 |  | 
| Sreeram Ramachandran | 72c5393 | 2014-05-18 15:18:36 -0700 | [diff] [blame] | 21 | #include "private/libc_logging.h" | 
 | 22 | #include "private/NetdClientDispatch.h" | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 23 |  | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 24 | #include <dlfcn.h> | 
| Sreeram Ramachandran | 72c5393 | 2014-05-18 15:18:36 -0700 | [diff] [blame] | 25 | #include <pthread.h> | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 26 |  | 
 | 27 | template <typename FunctionType> | 
 | 28 | static void netdClientInitFunction(void* handle, const char* symbol, FunctionType* function) { | 
 | 29 |     typedef void (*InitFunctionType)(FunctionType*); | 
 | 30 |     InitFunctionType initFunction = reinterpret_cast<InitFunctionType>(dlsym(handle, symbol)); | 
 | 31 |     if (initFunction != NULL) { | 
 | 32 |         initFunction(function); | 
 | 33 |     } | 
 | 34 | } | 
 | 35 |  | 
 | 36 | static void netdClientInitImpl() { | 
| Dmitriy Ivanov | 84c10c2 | 2015-03-23 14:58:45 -0700 | [diff] [blame] | 37 |     void* netdClientHandle = dlopen("libnetd_client.so", RTLD_NOW); | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 38 |     if (netdClientHandle == NULL) { | 
 | 39 |         // If the library is not available, it's not an error. We'll just use | 
 | 40 |         // default implementations of functions that it would've overridden. | 
 | 41 |         return; | 
 | 42 |     } | 
| Sreeram Ramachandran | 903b788 | 2014-05-19 13:39:57 -0700 | [diff] [blame] | 43 |     netdClientInitFunction(netdClientHandle, "netdClientInitAccept4", | 
 | 44 |                            &__netdClientDispatch.accept4); | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 45 |     netdClientInitFunction(netdClientHandle, "netdClientInitConnect", | 
 | 46 |                            &__netdClientDispatch.connect); | 
| Paul Jensen | 5240b56 | 2014-05-15 14:43:07 -0400 | [diff] [blame] | 47 |     netdClientInitFunction(netdClientHandle, "netdClientInitNetIdForResolv", | 
 | 48 |                            &__netdClientDispatch.netIdForResolv); | 
| Sreeram Ramachandran | 903b788 | 2014-05-19 13:39:57 -0700 | [diff] [blame] | 49 |     netdClientInitFunction(netdClientHandle, "netdClientInitSocket", &__netdClientDispatch.socket); | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 50 | } | 
 | 51 |  | 
 | 52 | static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT; | 
 | 53 |  | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 54 | extern "C" __LIBC_HIDDEN__ void netdClientInit() { | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 55 |     if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) { | 
| Sreeram Ramachandran | 903b788 | 2014-05-19 13:39:57 -0700 | [diff] [blame] | 56 |         __libc_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize netd_client"); | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 57 |     } | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 58 | } |