blob: 75e84ee663664e01930ce115cc107f7227f4a60d [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 Ramachandran72c53932014-05-18 15:18:36 -070021#include "private/libc_logging.h"
22#include "private/NetdClientDispatch.h"
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070023
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070024#include <dlfcn.h>
Sreeram Ramachandran72c53932014-05-18 15:18:36 -070025#include <pthread.h>
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070026
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 }
Sreeram Ramachandran8f0cd8a2014-05-13 15:40:26 -070043 netdClientInitFunction(netdClientHandle, "netdClientInitAccept", &__netdClientDispatch.accept);
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070044 netdClientInitFunction(netdClientHandle, "netdClientInitConnect",
45 &__netdClientDispatch.connect);
46}
47
48static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
49
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070050extern "C" __LIBC_HIDDEN__ void netdClientInit() {
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070051 if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
52 __libc_format_log(ANDROID_LOG_ERROR, "netdClient",
53 "Unable to initialize netd_client component.");
54 }
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070055}