OSDN Git Service

Fix building errors of nougat-x86
[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/"
31 #ifdef __LP64__
32                 "lib64/arm64/"
33 #else
34                 "lib/arm/"
35 #endif
36                 "libhoudini.so";
37         if (!native_handle) {
38             native_handle = dlopen(libnb, RTLD_LAZY);
39             if (!native_handle) {
40                 ALOGE("Unable to open %s", libnb);
41                 return nullptr;
42             }
43         }
44         callbacks = reinterpret_cast<NativeBridgeCallbacks *>(dlsym(native_handle, "NativeBridgeItf"));
45     }
46     return callbacks;
47 }
48
49 // NativeBridgeCallbacks implementations
50 static bool native_bridge2_initialize(const NativeBridgeRuntimeCallbacks *art_cbs,
51                                       const char *app_code_cache_dir,
52                                       const char *isa)
53 {
54     ALOGV("enter native_bridge2_initialize %s %s", app_code_cache_dir, isa);
55     if (property_get_bool("persist.sys.nativebridge", 0)) {
56         if (NativeBridgeCallbacks *cb = get_callbacks()) {
57             return cb->initialize(art_cbs, app_code_cache_dir, isa);
58         }
59     } else {
60         ALOGW("Native bridge is disabled");
61     }
62     return false;
63 }
64
65 static void *native_bridge2_loadLibrary(const char *libpath, int flag)
66 {
67     ALOGV("enter native_bridge2_loadLibrary %s", libpath);
68     NativeBridgeCallbacks *cb = get_callbacks();
69     return cb ? cb->loadLibrary(libpath, flag) : nullptr;
70 }
71
72 static void *native_bridge2_getTrampoline(void *handle, const char *name,
73                                           const char* shorty, uint32_t len)
74 {
75     ALOGV("enter native_bridge2_getTrampoline %s", name);
76     NativeBridgeCallbacks *cb = get_callbacks();
77     return cb ? cb->getTrampoline(handle, name, shorty, len) : nullptr;
78 }
79
80 static bool native_bridge2_isSupported(const char *libpath)
81 {
82     ALOGV("enter native_bridge2_isSupported %s", libpath);
83     NativeBridgeCallbacks *cb = get_callbacks();
84     return cb ? cb->isSupported(libpath) : false;
85 }
86
87 static const struct NativeBridgeRuntimeValues *native_bridge2_getAppEnv(const char *abi)
88 {
89     ALOGV("enter native_bridge2_getAppEnv %s", abi);
90     NativeBridgeCallbacks *cb = get_callbacks();
91     return cb ? cb->getAppEnv(abi) : nullptr;
92 }
93
94 static bool native_bridge2_is_compatible_compatible_with(uint32_t version)
95 {
96     // For testing, allow 1 and 2, but disallow 3+.
97     return version <= 2;
98 }
99
100 static NativeBridgeSignalHandlerFn native_bridge2_get_signal_handler(int signal)
101 {
102     ALOGV("enter native_bridge2_getAppEnv %d", signal);
103     NativeBridgeCallbacks *cb = get_callbacks();
104     return cb ? cb->getSignalHandler(signal) : nullptr;
105 }
106
107 static void __attribute__ ((destructor)) on_dlclose()
108 {
109     if (native_handle) {
110         dlclose(native_handle);
111         native_handle = nullptr;
112     }
113 }
114
115 extern "C" {
116
117 NativeBridgeCallbacks NativeBridgeItf = {
118     .version = 2,
119     .initialize = &native_bridge2_initialize,
120     .loadLibrary = &native_bridge2_loadLibrary,
121     .getTrampoline = &native_bridge2_getTrampoline,
122     .isSupported = &native_bridge2_isSupported,
123     .getAppEnv = &native_bridge2_getAppEnv,
124     .isCompatibleWith = &native_bridge2_is_compatible_compatible_with,
125     .getSignalHandler = &native_bridge2_get_signal_handler,
126 };
127
128 } // extern "C"
129 } // namespace android