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 <async_safe/log.h>
22
23 #include "private/NetdClientDispatch.h"
24
25 #include <dlfcn.h>
26 #include <pthread.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 template <typename FunctionType>
netdClientInitFunction(void * handle,const char * symbol,FunctionType * function)31 static void netdClientInitFunction(void* handle, const char* symbol, FunctionType* function) {
32 typedef void (*InitFunctionType)(FunctionType*);
33 InitFunctionType initFunction = reinterpret_cast<InitFunctionType>(dlsym(handle, symbol));
34 if (initFunction != nullptr) {
35 initFunction(function);
36 }
37 }
38
netdClientInitImpl()39 static void netdClientInitImpl() {
40 // Prevent netd from looping back fwmarkd connections to itself. It would work, but it's
41 // a deadlock hazard and unnecessary overhead for the resolver.
42 if (getuid() == 0 && strcmp(basename(getprogname()), "netd") == 0) {
43 async_safe_format_log(ANDROID_LOG_INFO, "netdClient",
44 "Skipping libnetd_client init since *we* are netd");
45 return;
46 }
47
48 void* handle = dlopen("libnetd_client.so", RTLD_NOW);
49 if (handle == nullptr) {
50 // If the library is not available, it's not an error. We'll just use
51 // default implementations of functions that it would've overridden.
52 return;
53 }
54
55 netdClientInitFunction(handle, "netdClientInitAccept4", &__netdClientDispatch.accept4);
56 netdClientInitFunction(handle, "netdClientInitConnect", &__netdClientDispatch.connect);
57 netdClientInitFunction(handle, "netdClientInitSendmmsg", &__netdClientDispatch.sendmmsg);
58 netdClientInitFunction(handle, "netdClientInitSendmsg", &__netdClientDispatch.sendmsg);
59 netdClientInitFunction(handle, "netdClientInitSendto", &__netdClientDispatch.sendto);
60 netdClientInitFunction(handle, "netdClientInitSocket", &__netdClientDispatch.socket);
61
62 netdClientInitFunction(handle, "netdClientInitNetIdForResolv",
63 &__netdClientDispatch.netIdForResolv);
64 netdClientInitFunction(handle, "netdClientInitDnsOpenProxy",
65 &__netdClientDispatch.dnsOpenProxy);
66 }
67
68 static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
69
netdClientInit()70 extern "C" __LIBC_HIDDEN__ void netdClientInit() {
71 if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
72 async_safe_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize libnetd_client");
73 }
74 }
75