blob: a1071d2f46eba2e73088017ae21d69ff0f4d4bf2 [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
Christopher Ferris7a3681e2017-04-24 17:48:32 -070021#include <async_safe/log.h>
22
Sreeram Ramachandran72c53932014-05-18 15:18:36 -070023#include "private/NetdClientDispatch.h"
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070024
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070025#include <dlfcn.h>
Sreeram Ramachandran72c53932014-05-18 15:18:36 -070026#include <pthread.h>
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070027
28template <typename FunctionType>
29static 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
37static void netdClientInitImpl() {
Dmitriy Ivanov84c10c22015-03-23 14:58:45 -070038 void* netdClientHandle = dlopen("libnetd_client.so", RTLD_NOW);
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070039 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 Ramachandran903b7882014-05-19 13:39:57 -070044 netdClientInitFunction(netdClientHandle, "netdClientInitAccept4",
45 &__netdClientDispatch.accept4);
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070046 netdClientInitFunction(netdClientHandle, "netdClientInitConnect",
47 &__netdClientDispatch.connect);
Paul Jensen5240b562014-05-15 14:43:07 -040048 netdClientInitFunction(netdClientHandle, "netdClientInitNetIdForResolv",
49 &__netdClientDispatch.netIdForResolv);
Sreeram Ramachandran903b7882014-05-19 13:39:57 -070050 netdClientInitFunction(netdClientHandle, "netdClientInitSocket", &__netdClientDispatch.socket);
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070051}
52
53static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
54
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070055extern "C" __LIBC_HIDDEN__ void netdClientInit() {
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070056 if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
Christopher Ferris7a3681e2017-04-24 17:48:32 -070057 async_safe_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize netd_client");
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070058 }
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070059}