OSDN Git Service

Merge "linker: add more directories to default lib paths"
[android-x86/bionic.git] / libc / bionic / NetdClient.cpp
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
17 #ifdef LIBC_STATIC
18 #error NetdClient.cpp should NOT be included in static libc builds.
19 #endif
20
21 #include "private/libc_logging.h"
22 #include "private/NetdClientDispatch.h"
23
24 #include <dlfcn.h>
25 #include <pthread.h>
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_NOW);
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, "netdClientInitAccept4",
44                            &__netdClientDispatch.accept4);
45     netdClientInitFunction(netdClientHandle, "netdClientInitConnect",
46                            &__netdClientDispatch.connect);
47     netdClientInitFunction(netdClientHandle, "netdClientInitNetIdForResolv",
48                            &__netdClientDispatch.netIdForResolv);
49     netdClientInitFunction(netdClientHandle, "netdClientInitSocket", &__netdClientDispatch.socket);
50 }
51
52 static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
53
54 extern "C" __LIBC_HIDDEN__ void netdClientInit() {
55     if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
56         __libc_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize netd_client");
57     }
58 }