Commit 8d1f2da1 authored by Mygod's avatar Mygod

Implement SIGTERM in jni-helper

parent 30c8c7e6
...@@ -38,12 +38,23 @@ ...@@ -38,12 +38,23 @@
package com.github.shadowsocks; package com.github.shadowsocks;
import android.os.Build;
import android.system.ErrnoException;
public class JniHelper { public class JniHelper {
static { static {
System.loadLibrary("jni-helper"); System.loadLibrary("jni-helper");
} }
public static native int exec(String cmd); @Deprecated // Use Process.destroy() since API 24
public static void sigtermCompat(Process process) throws Exception {
if (Build.VERSION.SDK_INT >= 24) throw new UnsupportedOperationException("Never call this method in OpenJDK!");
int errno = sigterm(process);
if (errno != 0) throw Build.VERSION.SDK_INT >= 21
? new ErrnoException("kill", errno) : new Exception("kill failed: " + errno);
}
private static native int sigterm(Process process);
public static native int sendFd(int fd, String path); public static native int sendFd(int fd, String path);
public static native void close(int fd); public static native void close(int fd);
} }
#define LOG_TAG "Shadowsocks" #define LOG_TAG "JniHelper"
#include "jni.h" #include "jni.h"
#include <android/log.h> #include <android/log.h>
#include <sys/system_properties.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <signal.h>
#include <sys/un.h> #include <sys/un.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -16,28 +19,26 @@ ...@@ -16,28 +19,26 @@
#define LOGI(...) do { __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__); } while(0) #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 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) #define LOGE(...) do { __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__); } while(0)
#define THROW(env, clazz, msg) do { env->ThrowNew(env->FindClass(clazz), msg); } while (0)
jint Java_com_github_shadowsocks_jnihelper_exec(JNIEnv *env, jobject thiz, jstring cmd) { static jclass ProcessImpl;
const char *cmd_str = env->GetStringUTFChars(cmd, 0); static jfieldID ProcessImpl_pid;
pid_t pid; static int sdk_version() {
char version[PROP_VALUE_MAX + 1];
__system_property_get("ro.build.version.sdk", version);
return atoi(version);
}
/* Fork off the parent process */ jint Java_com_github_shadowsocks_jnihelper_sigterm(JNIEnv *env, jobject thiz, jobject process) {
pid = fork(); if (!env->IsInstanceOf(process, ProcessImpl)) {
if (pid < 0) { THROW(env, "java/lang/ClassCastException",
env->ReleaseStringUTFChars(cmd, cmd_str); "Unsupported process object. Only java.lang.ProcessManager$ProcessImpl is accepted.");
return -1; return -1;
} }
jint pid = env->GetIntField(process, ProcessImpl_pid);
if (pid > 0) { // Suppress "No such process" errors. We just want the process killed. It's fine if it's already killed.
env->ReleaseStringUTFChars(cmd, cmd_str); return kill(pid, SIGTERM) == -1 && errno != ESRCH ? errno : 0;
return pid;
}
execl("/system/bin/sh", "sh", "-c", cmd_str, NULL);
env->ReleaseStringUTFChars(cmd, cmd_str);
return 1;
} }
void Java_com_github_shadowsocks_jnihelper_close(JNIEnv *env, jobject thiz, jint fd) { void Java_com_github_shadowsocks_jnihelper_close(JNIEnv *env, jobject thiz, jint fd) {
...@@ -82,8 +83,8 @@ static JNINativeMethod method_table[] = { ...@@ -82,8 +83,8 @@ static JNINativeMethod method_table[] = {
(void*) Java_com_github_shadowsocks_jnihelper_close }, (void*) Java_com_github_shadowsocks_jnihelper_close },
{ "sendFd", "(ILjava/lang/String;)I", { "sendFd", "(ILjava/lang/String;)I",
(void*) Java_com_github_shadowsocks_jnihelper_sendfd }, (void*) Java_com_github_shadowsocks_jnihelper_sendfd },
{ "exec", "(Ljava/lang/String;)I", { "sigterm", "(Ljava/lang/Process;)I",
(void*) Java_com_github_shadowsocks_jnihelper_exec } (void*) Java_com_github_shadowsocks_jnihelper_sigterm }
}; };
...@@ -139,20 +140,30 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { ...@@ -139,20 +140,30 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {
jint result = -1; jint result = -1;
JNIEnv* env = NULL; JNIEnv* env = NULL;
LOGI("JNI_OnLoad"); if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_6) != JNI_OK) {
THROW(env, "java/lang/RuntimeException", "GetEnv failed");
if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) {
LOGE("ERROR: GetEnv failed");
goto bail; goto bail;
} }
env = uenv.env; env = uenv.env;
if (registerNatives(env) != JNI_TRUE) { if (registerNatives(env) != JNI_TRUE) {
LOGE("ERROR: registerNatives failed"); THROW(env, "java/lang/RuntimeException", "registerNativeMethods failed");
goto bail; goto bail;
} }
result = JNI_VERSION_1_4; if (sdk_version() < 24) {
if (!(ProcessImpl = env->FindClass("java/lang/ProcessManager$ProcessImpl"))) {
THROW(env, "java/lang/RuntimeException", "ProcessManager$ProcessImpl not found");
goto bail;
}
ProcessImpl = (jclass) env->NewGlobalRef((jobject) ProcessImpl);
if (!(ProcessImpl_pid = env->GetFieldID(ProcessImpl, "pid", "I"))) {
THROW(env, "java/lang/RuntimeException", "ProcessManager$ProcessImpl.pid not found");
goto bail;
}
}
result = JNI_VERSION_1_6;
bail: bail:
return result; return result;
......
...@@ -24,6 +24,7 @@ import java.io._ ...@@ -24,6 +24,7 @@ import java.io._
import java.lang.System.currentTimeMillis import java.lang.System.currentTimeMillis
import java.util.concurrent.Semaphore import java.util.concurrent.Semaphore
import android.os.Build
import android.util.Log import android.util.Log
import com.github.shadowsocks.utils.CloseUtils._ import com.github.shadowsocks.utils.CloseUtils._
import com.github.shadowsocks.utils.Commandline import com.github.shadowsocks.utils.Commandline
...@@ -89,7 +90,7 @@ class GuardedProcess(cmd: String*) { ...@@ -89,7 +90,7 @@ class GuardedProcess(cmd: String*) {
} catch { } catch {
case _: InterruptedException => case _: InterruptedException =>
if (BuildConfig.DEBUG) Log.d(TAG, "thread interrupt, destroy process: " + Commandline.toString(cmd)) if (BuildConfig.DEBUG) Log.d(TAG, "thread interrupt, destroy process: " + Commandline.toString(cmd))
process.destroy() destroyProcess()
case e: IOException => ioException = e case e: IOException => ioException = e
} finally semaphore.release() } finally semaphore.release()
}, "GuardThread-" + cmd.head) }, "GuardThread-" + cmd.head)
...@@ -105,16 +106,24 @@ class GuardedProcess(cmd: String*) { ...@@ -105,16 +106,24 @@ class GuardedProcess(cmd: String*) {
def destroy() { def destroy() {
isDestroyed = true isDestroyed = true
guardThread.interrupt() guardThread.interrupt()
process.destroy() destroyProcess()
try guardThread.join() catch { try guardThread.join() catch {
case _: InterruptedException => case _: InterruptedException =>
} }
} }
private def destroyProcess() {
if (Build.VERSION.SDK_INT < 24) {
JniHelper.sigtermCompat(process)
process.waitFor()
}
process.destroy()
}
def restart() { def restart() {
this.synchronized { this.synchronized {
isRestart = true isRestart = true
process.destroy() destroyProcess()
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment