blob: 5b0f4fddee951a2a47a8ed6ca51eff4f3b100c3f [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 Ramachandran903b7882014-05-19 13:39:57 -070043 netdClientInitFunction(netdClientHandle, "netdClientInitAccept4",
44 &__netdClientDispatch.accept4);
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070045 netdClientInitFunction(netdClientHandle, "netdClientInitConnect",
46 &__netdClientDispatch.connect);
Paul Jensen5240b562014-05-15 14:43:07 -040047 netdClientInitFunction(netdClientHandle, "netdClientInitNetIdForResolv",
48 &__netdClientDispatch.netIdForResolv);
Sreeram Ramachandran903b7882014-05-19 13:39:57 -070049 netdClientInitFunction(netdClientHandle, "netdClientInitSocket", &__netdClientDispatch.socket);
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070050}
51
52static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
53
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070054extern "C" __LIBC_HIDDEN__ void netdClientInit() {
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070055 if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
Sreeram Ramachandran903b7882014-05-19 13:39:57 -070056 __libc_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize netd_client");
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070057 }
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070058}