2 * Copyright (C) 2015 The Android-x86 Open Source Project
4 * by Chih-Wei Huang <cwhuang@linux.org.tw>
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
10 * http://www.gnu.org/licenses/gpl.html
14 #define LOG_TAG "libnb"
17 #include <cutils/log.h>
18 #include <cutils/properties.h>
19 #include "nativebridge/native_bridge.h"
23 static void *native_handle = nullptr;
25 static NativeBridgeCallbacks *get_callbacks()
27 static NativeBridgeCallbacks *callbacks = nullptr;
30 const char *libnb = "/system/"
38 native_handle = dlopen(libnb, RTLD_LAZY);
40 ALOGE("Unable to open %s", libnb);
44 callbacks = reinterpret_cast<NativeBridgeCallbacks *>(dlsym(native_handle, "NativeBridgeItf"));
49 // NativeBridgeCallbacks implementations
50 static bool native_bridge2_initialize(const NativeBridgeRuntimeCallbacks *art_cbs,
51 const char *app_code_cache_dir,
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);
60 ALOGW("Native bridge is disabled");
65 static void *native_bridge2_loadLibrary(const char *libpath, int flag)
67 ALOGV("enter native_bridge2_loadLibrary %s", libpath);
68 NativeBridgeCallbacks *cb = get_callbacks();
69 return cb ? cb->loadLibrary(libpath, flag) : nullptr;
72 static void *native_bridge2_getTrampoline(void *handle, const char *name,
73 const char* shorty, uint32_t len)
75 ALOGV("enter native_bridge2_getTrampoline %s", name);
76 NativeBridgeCallbacks *cb = get_callbacks();
77 return cb ? cb->getTrampoline(handle, name, shorty, len) : nullptr;
80 static bool native_bridge2_isSupported(const char *libpath)
82 ALOGV("enter native_bridge2_isSupported %s", libpath);
83 NativeBridgeCallbacks *cb = get_callbacks();
84 return cb ? cb->isSupported(libpath) : false;
87 static const struct NativeBridgeRuntimeValues *native_bridge2_getAppEnv(const char *abi)
89 ALOGV("enter native_bridge2_getAppEnv %s", abi);
90 NativeBridgeCallbacks *cb = get_callbacks();
91 return cb ? cb->getAppEnv(abi) : nullptr;
94 static bool native_bridge2_is_compatible_compatible_with(uint32_t version)
96 // For testing, allow 1 and 2, but disallow 3+.
100 static NativeBridgeSignalHandlerFn native_bridge2_get_signal_handler(int signal)
102 ALOGV("enter native_bridge2_getAppEnv %d", signal);
103 NativeBridgeCallbacks *cb = get_callbacks();
104 return cb ? cb->getSignalHandler(signal) : nullptr;
107 static void __attribute__ ((destructor)) on_dlclose()
110 dlclose(native_handle);
111 native_handle = nullptr;
117 NativeBridgeCallbacks NativeBridgeItf = {
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,
129 } // namespace android