blob: 56d82445bce74224caa92f5d97723ee4d78324b6 [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
17#include <private/NetdClient.h>
18#include <private/libc_logging.h>
19#include <pthread.h>
20
21#ifdef __i386__
22#define __socketcall __attribute__((__cdecl__))
23#else
24#define __socketcall
25#endif
26
27extern "C" __socketcall int __connect(int, const sockaddr*, socklen_t);
28
29NetdClientDispatch __netdClientDispatch __attribute__((aligned(32))) = {
30 __connect
31};
32
33#ifndef LIBC_STATIC
34
35#include <dlfcn.h>
36
37template <typename FunctionType>
38static void netdClientInitFunction(void* handle, const char* symbol, FunctionType* function) {
39 typedef void (*InitFunctionType)(FunctionType*);
40 InitFunctionType initFunction = reinterpret_cast<InitFunctionType>(dlsym(handle, symbol));
41 if (initFunction != NULL) {
42 initFunction(function);
43 }
44}
45
46static void netdClientInitImpl() {
47 void* netdClientHandle = dlopen("libnetd_client.so", RTLD_LAZY);
48 if (netdClientHandle == NULL) {
49 // If the library is not available, it's not an error. We'll just use
50 // default implementations of functions that it would've overridden.
51 return;
52 }
53 netdClientInitFunction(netdClientHandle, "netdClientInitConnect",
54 &__netdClientDispatch.connect);
55}
56
57static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
58
59#endif // LIBC_STATIC
60
61extern "C" __LIBC_HIDDEN__ void netdClientInit() {
62#ifndef LIBC_STATIC
63 if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
64 __libc_format_log(ANDROID_LOG_ERROR, "netdClient",
65 "Unable to initialize netd_client component.");
66 }
67#endif // LIBC_STATIC
68}