blob: a5c519eee32e9fa512ae0451a16556b03de9dd2a [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));
34 if (initFunction != NULL) {
35 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
Dmitriy Ivanov84c10c22015-03-23 14:58:45 -070048 void* netdClientHandle = dlopen("libnetd_client.so", RTLD_NOW);
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070049 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 }
Sreeram Ramachandran903b7882014-05-19 13:39:57 -070054 netdClientInitFunction(netdClientHandle, "netdClientInitAccept4",
55 &__netdClientDispatch.accept4);
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070056 netdClientInitFunction(netdClientHandle, "netdClientInitConnect",
57 &__netdClientDispatch.connect);
Paul Jensen5240b562014-05-15 14:43:07 -040058 netdClientInitFunction(netdClientHandle, "netdClientInitNetIdForResolv",
59 &__netdClientDispatch.netIdForResolv);
Sreeram Ramachandran903b7882014-05-19 13:39:57 -070060 netdClientInitFunction(netdClientHandle, "netdClientInitSocket", &__netdClientDispatch.socket);
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070061}
62
63static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
64
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070065extern "C" __LIBC_HIDDEN__ void netdClientInit() {
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070066 if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
Bernie Innocenti1b2ecee2018-09-05 10:53:17 +090067 async_safe_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize libnetd_client");
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070068 }
Sreeram Ramachandranceb5bd72014-05-12 11:19:16 -070069}