blob: d9debbfcdbc2b9026fac8231e44600de4a76499a [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
Christopher Ferris7a3681e2017-04-24 17:48:32 -070021#include <async_safe/log.h>
22
Sreeram Ramachandran72c53932014-05-18 15:18:36 -070023#include "private/NetdClientDispatch.h"
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070024
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070025#include <dlfcn.h>
Sreeram Ramachandran72c53932014-05-18 15:18:36 -070026#include <pthread.h>
Bernie Innocenti1b2ecee2018-09-05 10:53:17 +090027#include <string.h>
28#include <unistd.h>
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070029
30template <typename FunctionType>
31static void netdClientInitFunction(void* handle, const char* symbol, FunctionType* function) {
32 typedef void (*InitFunctionType)(FunctionType*);
33 InitFunctionType initFunction = reinterpret_cast<InitFunctionType>(dlsym(handle, symbol));
Elliott Hughes5c6a3f92019-06-13 14:24:45 -070034 if (initFunction != nullptr) {
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070035 initFunction(function);
36 }
37}
38
39static void netdClientInitImpl() {
Bernie Innocenti1b2ecee2018-09-05 10:53:17 +090040 // 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 Innocentifb4eaa02018-09-07 21:26:58 +090042 if (getuid() == 0 && strcmp(basename(getprogname()), "netd") == 0) {
Bernie Innocenti1b2ecee2018-09-05 10:53:17 +090043 async_safe_format_log(ANDROID_LOG_INFO, "netdClient",
44 "Skipping libnetd_client init since *we* are netd");
45 return;
46 }
47
Elliott Hughes5c6a3f92019-06-13 14:24:45 -070048 void* handle = dlopen("libnetd_client.so", RTLD_NOW);
49 if (handle == nullptr) {
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070050 // 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 Hughes5c6a3f92019-06-13 14:24:45 -070054
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 Jensen5240b562014-05-15 14:43:07 -040063 &__netdClientDispatch.netIdForResolv);
Elliott Hughes5c6a3f92019-06-13 14:24:45 -070064 netdClientInitFunction(handle, "netdClientInitDnsOpenProxy",
Luke Huange3ed8922018-11-19 16:59:08 +080065 &__netdClientDispatch.dnsOpenProxy);
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070066}
67
68static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
69
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070070extern "C" __LIBC_HIDDEN__ void netdClientInit() {
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070071 if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
Bernie Innocenti1b2ecee2018-09-05 10:53:17 +090072 async_safe_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize libnetd_client");
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070073 }
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070074}