From aae95df43609cf31ff1cf7898f00ef6379e7ad57 Mon Sep 17 00:00:00 2001 From: Max Lv Date: Sun, 1 Dec 2013 10:49:33 +0800 Subject: [PATCH] add abi helper function --- .../java/com/github/shadowsocks/System.java | 1 + src/main/jni/Android.mk | 4 +++ src/main/jni/system.cpp | 21 ++++++++++++++-- .../com/github/shadowsocks/Shadowsocks.scala | 2 +- .../com/github/shadowsocks/utils/Utils.scala | 25 ------------------- 5 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/github/shadowsocks/System.java b/src/main/java/com/github/shadowsocks/System.java index a34838d1..0f17bcaf 100755 --- a/src/main/java/com/github/shadowsocks/System.java +++ b/src/main/java/com/github/shadowsocks/System.java @@ -44,4 +44,5 @@ public class System { } public static native void exec(String cmd); + public static native String getABI(); } diff --git a/src/main/jni/Android.mk b/src/main/jni/Android.mk index e591d213..19aec910 100755 --- a/src/main/jni/Android.mk +++ b/src/main/jni/Android.mk @@ -136,6 +136,8 @@ LOCAL_SRC_FILES:= \ LOCAL_LDLIBS := -ldl -llog +LOCAL_STATIC_LIBRARIES := cpufeatures + include $(BUILD_SHARED_LIBRARY) ######################################################## @@ -241,3 +243,5 @@ iptables_subdirs := $(addprefix $(LOCAL_PATH)/iptables/,$(addsuffix /Android.mk, )) include $(iptables_subdirs) +# Import cpufeatures +$(call import-module,android/cpufeatures) \ No newline at end of file diff --git a/src/main/jni/system.cpp b/src/main/jni/system.cpp index fa2d3dc8..69b6b95d 100644 --- a/src/main/jni/system.cpp +++ b/src/main/jni/system.cpp @@ -5,12 +5,27 @@ #include #include #include +#include #define LOGI(...) do { __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__); } while(0) #define LOGW(...) do { __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__); } while(0) #define LOGE(...) do { __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__); } while(0) -void Java_com_github_shadowsocks_Node_exec(JNIEnv *env, jobject thiz, jstring cmd) { +jstring Java_com_github_shadowsocks_system_getabi(JNIEnv *env, jobject thiz) { + AndroidCpuFamily family = android_getCpuFamily(); + const char *abi; + + if (family == ANDROID_CPU_FAMILY_X86) { + abi = "x86"; + } else if (family == ANDROID_CPU_FAMILY_MIPS) { + abi = "mips"; + } else if (family == ANDROID_CPU_FAMILY_ARM) { + abi = "arm"; + } + return env->NewStringUTF(abi); +} + +void Java_com_github_shadowsocks_system_exec(JNIEnv *env, jobject thiz, jstring cmd) { const char *str = env->GetStringUTFChars(cmd, 0); setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 1); setegid(getgid()); @@ -23,7 +38,9 @@ static const char *classPathName = "com/github/shadowsocks/System"; static JNINativeMethod method_table[] = { { "exec", "(Ljava/lang/String;)V", - (void*) Java_com_github_shadowsocks_Node_exec } + (void*) Java_com_github_shadowsocks_system_exec }, + { "getABI", "()Ljava/lang/String;", + (void*) Java_com_github_shadowsocks_system_getabi } }; /* diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index bb82c9c7..2c373356 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -800,7 +800,7 @@ class Shadowsocks def reset() { crash_recovery() - copyAssets(Utils.getABI) + copyAssets(System.getABI) Utils.runCommand("chmod 755 /data/data/com.github.shadowsocks/iptables\n" + "chmod 755 /data/data/com.github.shadowsocks/redsocks\n" + "chmod 755 /data/data/com.github.shadowsocks/pdnsd\n" diff --git a/src/main/scala/com/github/shadowsocks/utils/Utils.scala b/src/main/scala/com/github/shadowsocks/utils/Utils.scala index a76dafc3..85397395 100644 --- a/src/main/scala/com/github/shadowsocks/utils/Utils.scala +++ b/src/main/scala/com/github/shadowsocks/utils/Utils.scala @@ -282,31 +282,6 @@ object Utils { false } - /** - * Get the ABI of the device - * @return The ABI of the device, or ARM_ABI if not found - */ - def getABI: String = { - val prop = getSystemProperty(ABI_PROP) - prop match { - case abi if abi.toLowerCase.contains(ARM_ABI) => ARM_ABI - case abi if abi.toLowerCase.contains(X86_ABI) => X86_ABI - case _ => ARM_ABI - } - } - - /** - * Returns a SystemProperty - * - * @param propName The Property to retrieve - * @return The Property, or NULL if not found - */ - def getSystemProperty(propName: String): String = { - val p: Process = Runtime.getRuntime.exec("getprop " + propName) - val lines = scala.io.Source.fromInputStream(p.getInputStream).getLines() - if (lines.hasNext) lines.next() else null - } - /** * Check the system's iptables * Default to use the app's iptables