Commit a61b03c8 authored by Max Lv's avatar Max Lv

Add 600ms timeout for waitpid

parent f43362fb
......@@ -54,7 +54,14 @@ public class JniHelper {
? new ErrnoException("kill", errno) : new Exception("kill failed: " + errno);
}
@Deprecated // Use Process.destroy() since API 24
public static int waitpidCompat(Process process) throws Exception {
if (Build.VERSION.SDK_INT >= 24) throw new UnsupportedOperationException("Never call this method in OpenJDK!");
return waitpid(process);
}
private static native int sigterm(Process process);
private static native int waitpid(Process process);
public static native int sendFd(int fd, String path);
public static native void close(int fd);
}
......@@ -14,6 +14,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <ancillary.h>
#define LOGI(...) do { __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__); } while(0)
......@@ -41,6 +42,17 @@ jint Java_com_github_shadowsocks_jnihelper_sigterm(JNIEnv *env, jobject thiz, jo
return kill(pid, SIGTERM) == -1 && errno != ESRCH ? errno : 0;
}
jint Java_com_github_shadowsocks_jnihelper_waitpid(JNIEnv *env, jobject thiz, jobject process) {
if (!env->IsInstanceOf(process, ProcessImpl)) {
THROW(env, "java/lang/ClassCastException",
"Unsupported process object. Only java.lang.ProcessManager$ProcessImpl is accepted.");
return -1;
}
jint pid = env->GetIntField(process, ProcessImpl_pid);
int status;
return waitpid(pid, &status, WNOHANG);
}
void Java_com_github_shadowsocks_jnihelper_close(JNIEnv *env, jobject thiz, jint fd) {
close(fd);
}
......@@ -84,11 +96,11 @@ static JNINativeMethod method_table[] = {
{ "sendFd", "(ILjava/lang/String;)I",
(void*) Java_com_github_shadowsocks_jnihelper_sendfd },
{ "sigterm", "(Ljava/lang/Process;)I",
(void*) Java_com_github_shadowsocks_jnihelper_sigterm }
(void*) Java_com_github_shadowsocks_jnihelper_sigterm },
{ "waitpid", "(Ljava/lang/Process;)I",
(void*) Java_com_github_shadowsocks_jnihelper_waitpid }
};
/*
* Register several native methods for one class.
*/
......
......@@ -115,7 +115,12 @@ class GuardedProcess(cmd: String*) {
private def destroyProcess() {
if (Build.VERSION.SDK_INT < 24) {
JniHelper.sigtermCompat(process)
process.waitFor()
var tries = 0
while (JniHelper.waitpidCompat(process) == 0 && tries < 3) {
Log.w(TAG, "still waiting for process " + Commandline.toString(cmd))
Thread.sleep(200)
tries += 1
}
}
process.destroy()
}
......
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