OSDN Git Service

Use languages_full.mk instead of locales_full.mk
[android-x86/device-generic-common.git] / nativebridge / src / libnb.cpp
1 /*
2  * Copyright (C) 2015 The Android-x86 Open Source Project
3  *
4  * by Chih-Wei Huang <cwhuang@linux.org.tw>
5  *
6  * Licensed under the GNU General Public License Version 2 or later.
7  * You may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.gnu.org/licenses/gpl.html
11  *
12  */
13
14 #define LOG_TAG "libnb"
15
16 #include <dlfcn.h>
17 #include <cutils/log.h>
18 #include <cutils/properties.h>
19 #include "nativebridge/native_bridge.h"
20
21 namespace android {
22
23 static void *native_handle = nullptr;
24
25 static NativeBridgeCallbacks *get_callbacks()
26 {
27     static NativeBridgeCallbacks *callbacks = nullptr;
28
29     if (!callbacks) {
30         const char *libnb = "/system/lib"
31 #ifdef __LP64__
32                 "64"
33 #endif
34                 "/libhoudini.so";
35         if (!native_handle) {
36             native_handle = dlopen(libnb, RTLD_LAZY);
37             if (!native_handle) {
38                 ALOGE("Unable to open %s", libnb);
39                 return nullptr;
40             }
41         }
42         callbacks = reinterpret_cast<NativeBridgeCallbacks *>(dlsym(native_handle, "NativeBridgeItf"));
43     }
44     return callbacks;
45 }
46
47 // NativeBridgeCallbacks implementations
48 static bool native_bridge2_initialize(const NativeBridgeRuntimeCallbacks *art_cbs,
49                                       const char *app_code_cache_dir,
50                                       const char *isa)
51 {
52     ALOGV("enter native_bridge2_initialize %s %s", app_code_cache_dir, isa);
53     if (property_get_bool("persist.sys.nativebridge", 0)) {
54         if (NativeBridgeCallbacks *cb = get_callbacks()) {
55             return cb->initialize(art_cbs, app_code_cache_dir, isa);
56         }
57     } else {
58         ALOGW("Native bridge is disabled");
59     }
60     return false;
61 }
62
63 static void *native_bridge2_loadLibrary(const char *libpath, int flag)
64 {
65     ALOGV("enter native_bridge2_loadLibrary %s", libpath);
66     NativeBridgeCallbacks *cb = get_callbacks();
67     return cb ? cb->loadLibrary(libpath, flag) : nullptr;
68 }
69
70 static void *native_bridge2_getTrampoline(void *handle, const char *name,
71                                           const char* shorty, uint32_t len)
72 {
73     ALOGV("enter native_bridge2_getTrampoline %s", name);
74     NativeBridgeCallbacks *cb = get_callbacks();
75     return cb ? cb->getTrampoline(handle, name, shorty, len) : nullptr;
76 }
77
78 static bool native_bridge2_isSupported(const char *libpath)
79 {
80     ALOGV("enter native_bridge2_isSupported %s", libpath);
81     NativeBridgeCallbacks *cb = get_callbacks();
82     return cb ? cb->isSupported(libpath) : false;
83 }
84
85 static const struct NativeBridgeRuntimeValues *native_bridge2_getAppEnv(const char *abi)
86 {
87     ALOGV("enter native_bridge2_getAppEnv %s", abi);
88     NativeBridgeCallbacks *cb = get_callbacks();
89     return cb ? cb->getAppEnv(abi) : nullptr;
90 }
91
92 static bool native_bridge2_is_compatible_compatible_with(uint32_t version)
93 {
94     // For testing, allow 1 and 2, but disallow 3+.
95     return version <= 2;
96 }
97
98 static NativeBridgeSignalHandlerFn native_bridge2_get_signal_handler(int signal)
99 {
100     ALOGV("enter native_bridge2_getAppEnv %d", signal);
101     NativeBridgeCallbacks *cb = get_callbacks();
102     return cb ? cb->getSignalHandler(signal) : nullptr;
103 }
104
105 static void __attribute__ ((destructor)) on_dlclose()
106 {
107     if (native_handle) {
108         dlclose(native_handle);
109         native_handle = nullptr;
110     }
111 }
112
113 extern "C" {
114
115 NativeBridgeCallbacks NativeBridgeItf = {
116     .version = 2,
117     .initialize = &native_bridge2_initialize,
118     .loadLibrary = &native_bridge2_loadLibrary,
119     .getTrampoline = &native_bridge2_getTrampoline,
120     .isSupported = &native_bridge2_isSupported,
121     .getAppEnv = &native_bridge2_getAppEnv,
122     .isCompatibleWith = &native_bridge2_is_compatible_compatible_with,
123     .getSignalHandler = &native_bridge2_get_signal_handler,
124 };
125
126 } // extern "C"
127 } // namespace android