Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 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 | |
Sreeram Ramachandran | 8205a61 | 2014-05-13 17:24:03 -0700 | [diff] [blame] | 17 | #ifdef LIBC_STATIC |
| 18 | #error NetdClient.cpp should NOT be included in static libc builds. |
| 19 | #endif |
| 20 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 21 | #include <async_safe/log.h> |
| 22 | |
Sreeram Ramachandran | 72c5393 | 2014-05-18 15:18:36 -0700 | [diff] [blame] | 23 | #include "private/NetdClientDispatch.h" |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 24 | |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 25 | #include <dlfcn.h> |
Sreeram Ramachandran | 72c5393 | 2014-05-18 15:18:36 -0700 | [diff] [blame] | 26 | #include <pthread.h> |
Bernie Innocenti | 1b2ecee | 2018-09-05 10:53:17 +0900 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <unistd.h> |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 29 | |
| 30 | template <typename FunctionType> |
| 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)); |
Elliott Hughes | 5c6a3f9 | 2019-06-13 14:24:45 -0700 | [diff] [blame^] | 34 | if (initFunction != nullptr) { |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 35 | initFunction(function); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static void netdClientInitImpl() { |
Bernie Innocenti | 1b2ecee | 2018-09-05 10:53:17 +0900 | [diff] [blame] | 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. |
Bernie Innocenti | fb4eaa0 | 2018-09-07 21:26:58 +0900 | [diff] [blame] | 42 | if (getuid() == 0 && strcmp(basename(getprogname()), "netd") == 0) { |
Bernie Innocenti | 1b2ecee | 2018-09-05 10:53:17 +0900 | [diff] [blame] | 43 | async_safe_format_log(ANDROID_LOG_INFO, "netdClient", |
| 44 | "Skipping libnetd_client init since *we* are netd"); |
| 45 | return; |
| 46 | } |
| 47 | |
Elliott Hughes | 5c6a3f9 | 2019-06-13 14:24:45 -0700 | [diff] [blame^] | 48 | void* handle = dlopen("libnetd_client.so", RTLD_NOW); |
| 49 | if (handle == nullptr) { |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 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 | } |
Elliott Hughes | 5c6a3f9 | 2019-06-13 14:24:45 -0700 | [diff] [blame^] | 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", |
Paul Jensen | 5240b56 | 2014-05-15 14:43:07 -0400 | [diff] [blame] | 63 | &__netdClientDispatch.netIdForResolv); |
Elliott Hughes | 5c6a3f9 | 2019-06-13 14:24:45 -0700 | [diff] [blame^] | 64 | netdClientInitFunction(handle, "netdClientInitDnsOpenProxy", |
Luke Huang | e3ed892 | 2018-11-19 16:59:08 +0800 | [diff] [blame] | 65 | &__netdClientDispatch.dnsOpenProxy); |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT; |
| 69 | |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 70 | extern "C" __LIBC_HIDDEN__ void netdClientInit() { |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 71 | if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) { |
Bernie Innocenti | 1b2ecee | 2018-09-05 10:53:17 +0900 | [diff] [blame] | 72 | async_safe_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize libnetd_client"); |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 73 | } |
Sreeram Ramachandran | ceb5bd7 | 2014-05-12 11:19:16 -0700 | [diff] [blame] | 74 | } |