| 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 |  | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 21 | #include <async_safe/log.h> | 
|  | 22 |  | 
| Sreeram Ramachandran | 72c5393 | 2014-05-18 15:18:36 -0700 | [diff] [blame] | 23 | #include "private/NetdClientDispatch.h" | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 24 |  | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 25 | #include <dlfcn.h> | 
| Sreeram Ramachandran | 72c5393 | 2014-05-18 15:18:36 -0700 | [diff] [blame] | 26 | #include <pthread.h> | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | template <typename FunctionType> | 
|  | 29 | static void netdClientInitFunction(void* handle, const char* symbol, FunctionType* function) { | 
|  | 30 | typedef void (*InitFunctionType)(FunctionType*); | 
|  | 31 | InitFunctionType initFunction = reinterpret_cast<InitFunctionType>(dlsym(handle, symbol)); | 
|  | 32 | if (initFunction != NULL) { | 
|  | 33 | initFunction(function); | 
|  | 34 | } | 
|  | 35 | } | 
|  | 36 |  | 
|  | 37 | static void netdClientInitImpl() { | 
| Dmitriy Ivanov | 84c10c2 | 2015-03-23 14:58:45 -0700 | [diff] [blame] | 38 | void* netdClientHandle = dlopen("libnetd_client.so", RTLD_NOW); | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 39 | if (netdClientHandle == NULL) { | 
|  | 40 | // If the library is not available, it's not an error. We'll just use | 
|  | 41 | // default implementations of functions that it would've overridden. | 
|  | 42 | return; | 
|  | 43 | } | 
| Sreeram Ramachandran | 903b788 | 2014-05-19 13:39:57 -0700 | [diff] [blame] | 44 | netdClientInitFunction(netdClientHandle, "netdClientInitAccept4", | 
|  | 45 | &__netdClientDispatch.accept4); | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 46 | netdClientInitFunction(netdClientHandle, "netdClientInitConnect", | 
|  | 47 | &__netdClientDispatch.connect); | 
| Paul Jensen | 5240b56 | 2014-05-15 14:43:07 -0400 | [diff] [blame] | 48 | netdClientInitFunction(netdClientHandle, "netdClientInitNetIdForResolv", | 
|  | 49 | &__netdClientDispatch.netIdForResolv); | 
| Sreeram Ramachandran | 903b788 | 2014-05-19 13:39:57 -0700 | [diff] [blame] | 50 | netdClientInitFunction(netdClientHandle, "netdClientInitSocket", &__netdClientDispatch.socket); | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
|  | 53 | static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT; | 
|  | 54 |  | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 55 | extern "C" __LIBC_HIDDEN__ void netdClientInit() { | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 56 | if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) { | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 57 | async_safe_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize netd_client"); | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 58 | } | 
| Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 59 | } |