blob: 6826ee8eefbbb9d1a8c9130c3ffcb2cd25360c52 [file] [log] [blame]
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -07001/*
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 Ramachandran8205a612014-05-13 17:24:03 -070017#ifdef LIBC_STATIC
18#error NetdClient.cpp should NOT be included in static libc builds.
19#endif
20
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070021#include <private/NetdClient.h>
22#include <private/libc_logging.h>
23#include <pthread.h>
24
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070025#include <dlfcn.h>
26
27template <typename FunctionType>
28static 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
36static 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 }
43 netdClientInitFunction(netdClientHandle, "netdClientInitConnect",
44 &__netdClientDispatch.connect);
45}
46
47static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
48
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070049extern "C" __LIBC_HIDDEN__ void netdClientInit() {
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070050 if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
51 __libc_format_log(ANDROID_LOG_ERROR, "netdClient",
52 "Unable to initialize netd_client component.");
53 }
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070054}