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() { |
| 37 | void* netdClientHandle = dlopen("libnetd_client.so", RTLD_LAZY); |
| 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 | 8f0cd8a | 2014-05-13 15:40:26 -0700 | [diff] [blame] | 43 | netdClientInitFunction(netdClientHandle, "netdClientInitAccept", &__netdClientDispatch.accept); |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 44 | netdClientInitFunction(netdClientHandle, "netdClientInitConnect", |
| 45 | &__netdClientDispatch.connect); |
| 46 | } |
| 47 | |
| 48 | static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT; |
| 49 | |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 50 | extern "C" __LIBC_HIDDEN__ void netdClientInit() { |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 51 | if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) { |
| 52 | __libc_format_log(ANDROID_LOG_ERROR, "netdClient", |
| 53 | "Unable to initialize netd_client component."); |
| 54 | } |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 55 | } |