• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 != NULL) {
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* netdClientHandle = dlopen("libnetd_client.so", RTLD_NOW);
49     if (netdClientHandle == NULL) {
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     netdClientInitFunction(netdClientHandle, "netdClientInitAccept4",
55                            &__netdClientDispatch.accept4);
56     netdClientInitFunction(netdClientHandle, "netdClientInitConnect",
57                            &__netdClientDispatch.connect);
58     netdClientInitFunction(netdClientHandle, "netdClientInitNetIdForResolv",
59                            &__netdClientDispatch.netIdForResolv);
60     netdClientInitFunction(netdClientHandle, "netdClientInitSocket", &__netdClientDispatch.socket);
61     netdClientInitFunction(netdClientHandle, "netdClientInitDnsOpenProxy",
62                            &__netdClientDispatch.dnsOpenProxy);
63 }
64 
65 static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
66 
netdClientInit()67 extern "C" __LIBC_HIDDEN__ void netdClientInit() {
68     if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
69         async_safe_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize libnetd_client");
70     }
71 }
72