diff --git a/jni/Android.mk b/jni/Android.mk index 0213c71b..162205d4 100755 --- a/jni/Android.mk +++ b/jni/Android.mk @@ -61,7 +61,7 @@ include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) -LOCAL_MODULE:= libexec +LOCAL_MODULE:= exec LOCAL_SRC_FILES:= \ termExec.cpp @@ -72,12 +72,12 @@ include $(BUILD_SHARED_LIBRARY) include $(CLEAR_VARS) -LOCAL_MODULE:= node +LOCAL_MODULE:= system LOCAL_SRC_FILES:= \ - node.c + system.cpp -LOCAL_LDLIBS := -ldl +LOCAL_LDLIBS := -ldl -llog include $(BUILD_SHARED_LIBRARY) diff --git a/jni/node.c b/jni/node.c deleted file mode 100644 index b1a4dd95..00000000 --- a/jni/node.c +++ /dev/null @@ -1,10 +0,0 @@ -#include - -void Java_com_github_shadowsocks_Node_exec(JNIEnv *env, jobject thiz, jstring cmd) { - const char *str = (*env)->GetStringUTFChars(env, cmd, 0); - setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 1); - setegid(getgid()); - seteuid(getuid()); - system(str); - (*env)->ReleaseStringUTFChars(env, cmd, str); -} diff --git a/jni/system.cpp b/jni/system.cpp new file mode 100644 index 00000000..fa2d3dc8 --- /dev/null +++ b/jni/system.cpp @@ -0,0 +1,97 @@ +#define LOG_TAG "Shadowsocks" + +#include "jni.h" +#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) { + const char *str = env->GetStringUTFChars(cmd, 0); + setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 1); + setegid(getgid()); + seteuid(getuid()); + system(str); + env->ReleaseStringUTFChars(cmd, str); +} + +static const char *classPathName = "com/github/shadowsocks/System"; + +static JNINativeMethod method_table[] = { + { "exec", "(Ljava/lang/String;)V", + (void*) Java_com_github_shadowsocks_Node_exec } +}; + +/* + * Register several native methods for one class. + */ +static int registerNativeMethods(JNIEnv* env, const char* className, + JNINativeMethod* gMethods, int numMethods) +{ + jclass clazz; + + clazz = env->FindClass(className); + if (clazz == NULL) { + LOGE("Native registration unable to find class '%s'", className); + return JNI_FALSE; + } + if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { + LOGE("RegisterNatives failed for '%s'", className); + return JNI_FALSE; + } + + return JNI_TRUE; +} + +/* + * Register native methods for all classes we know about. + * + * returns JNI_TRUE on success. + */ +static int registerNatives(JNIEnv* env) +{ + if (!registerNativeMethods(env, classPathName, method_table, + sizeof(method_table) / sizeof(method_table[0]))) { + return JNI_FALSE; + } + + return JNI_TRUE; +} + +/* + * This is called by the VM when the shared library is first loaded. + */ + +typedef union { + JNIEnv* env; + void* venv; +} UnionJNIEnvToVoid; + +jint JNI_OnLoad(JavaVM* vm, void* reserved) { + UnionJNIEnvToVoid uenv; + uenv.venv = NULL; + jint result = -1; + JNIEnv* env = NULL; + + LOGI("JNI_OnLoad"); + + if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) { + LOGE("ERROR: GetEnv failed"); + goto bail; + } + env = uenv.env; + + if (registerNatives(env) != JNI_TRUE) { + LOGE("ERROR: registerNatives failed"); + goto bail; + } + + result = JNI_VERSION_1_4; + +bail: + return result; +} diff --git a/jni/termExec.cpp b/jni/termExec.cpp index f8881eda..a01c84bb 100644 --- a/jni/termExec.cpp +++ b/jni/termExec.cpp @@ -30,7 +30,7 @@ * limitations under the License. */ -#define LOG_TAG "Exec" +#define LOG_TAG "Shadowsocks" #include "jni.h" #include diff --git a/libs/armeabi/libexec.so b/libs/armeabi/libexec.so index 302f782c..f16d8678 100755 Binary files a/libs/armeabi/libexec.so and b/libs/armeabi/libexec.so differ diff --git a/libs/armeabi/libnode.so b/libs/armeabi/libsystem.so similarity index 54% rename from libs/armeabi/libnode.so rename to libs/armeabi/libsystem.so index 869e1f3e..4ab8fd1a 100755 Binary files a/libs/armeabi/libnode.so and b/libs/armeabi/libsystem.so differ diff --git a/src/com/github/shadowsocks/Exec.java b/src/com/github/shadowsocks/Exec.java index ef450766..66d2d7f5 100755 --- a/src/com/github/shadowsocks/Exec.java +++ b/src/com/github/shadowsocks/Exec.java @@ -28,7 +28,7 @@ import java.io.FileDescriptor; public class Exec { static { - System.loadLibrary("exec"); + java.lang.System.loadLibrary("exec"); } /** diff --git a/src/com/github/shadowsocks/ShadowsocksService.java b/src/com/github/shadowsocks/ShadowsocksService.java index a096b673..84ce223e 100755 --- a/src/com/github/shadowsocks/ShadowsocksService.java +++ b/src/com/github/shadowsocks/ShadowsocksService.java @@ -210,7 +210,7 @@ public class ShadowsocksService extends Service { final String cmd = String.format(BASE + "shadowsocks -s \"%s\" -p \"%d\" -l \"%d\" -k \"%s\" -m \"%s\"", appHost, remotePort, localPort, sitekey, encMethod); - Node.exec(cmd); + System.exec(cmd); } }.start(); } @@ -222,7 +222,7 @@ public class ShadowsocksService extends Service { final String cmd = String.format(BASE + "polipo proxyPort=%d socksParentProxy=127.0.0.1:%d daemonise=true pidFile=\"%s\" logLevel=1 logFile=\"%s\"", localPort + 1, localPort, BASE + "polipo.pid", BASE + "polipo.log"); - Node.exec(cmd); + System.exec(cmd); } }.start(); } diff --git a/src/com/github/shadowsocks/Node.java b/src/com/github/shadowsocks/System.java similarity index 96% rename from src/com/github/shadowsocks/Node.java rename to src/com/github/shadowsocks/System.java index 6756ce84..db5ef9b8 100755 --- a/src/com/github/shadowsocks/Node.java +++ b/src/com/github/shadowsocks/System.java @@ -38,9 +38,9 @@ package com.github.shadowsocks; -public class Node { +public class System { static { - System.loadLibrary("node"); + java.lang.System.loadLibrary("system"); } public static native void exec(String cmd);