Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
com.ccwangluo.accelerator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sheteng
com.ccwangluo.accelerator
Commits
86773085
Commit
86773085
authored
Mar 01, 2017
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Wait for process to terminate with a better way
Refine #1122.
parent
b6667b9e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
19 deletions
+35
-19
mobile/src/main/java/com/github/shadowsocks/JniHelper.java
mobile/src/main/java/com/github/shadowsocks/JniHelper.java
+9
-4
mobile/src/main/jni/jni-helper.cpp
mobile/src/main/jni/jni-helper.cpp
+25
-9
mobile/src/main/scala/com/github/shadowsocks/GuardedProcess.scala
...rc/main/scala/com/github/shadowsocks/GuardedProcess.scala
+1
-6
No files found.
mobile/src/main/java/com/github/shadowsocks/JniHelper.java
View file @
86773085
...
@@ -54,14 +54,19 @@ public class JniHelper {
...
@@ -54,14 +54,19 @@ public class JniHelper {
?
new
ErrnoException
(
"kill"
,
errno
)
:
new
Exception
(
"kill failed: "
+
errno
);
?
new
ErrnoException
(
"kill"
,
errno
)
:
new
Exception
(
"kill failed: "
+
errno
);
}
}
@Deprecated
//
Use Process.destroy() sinc
e API 24
@Deprecated
//
only implemented for befor
e API 24
public
static
int
waitpidCompat
(
Process
proces
s
)
throws
Exception
{
public
static
boolean
waitForCompat
(
Process
process
,
long
milli
s
)
throws
Exception
{
if
(
Build
.
VERSION
.
SDK_INT
>=
24
)
throw
new
UnsupportedOperationException
(
"Never call this method in OpenJDK!"
);
if
(
Build
.
VERSION
.
SDK_INT
>=
24
)
throw
new
UnsupportedOperationException
(
"Never call this method in OpenJDK!"
);
return
waitpid
(
process
);
final
Object
mutex
=
getExitValueMutex
(
process
);
synchronized
(
mutex
)
{
if
(
getExitValue
(
process
)
==
null
)
mutex
.
wait
(
millis
);
return
getExitValue
(
process
)
!=
null
;
}
}
}
private
static
native
int
sigterm
(
Process
process
);
private
static
native
int
sigterm
(
Process
process
);
private
static
native
int
waitpid
(
Process
process
);
private
static
native
Integer
getExitValue
(
Process
process
);
private
static
native
Object
getExitValueMutex
(
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
);
}
}
mobile/src/main/jni/jni-helper.cpp
View file @
86773085
...
@@ -14,7 +14,6 @@
...
@@ -14,7 +14,6 @@
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <ancillary.h>
#include <ancillary.h>
#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)
...
@@ -23,7 +22,7 @@
...
@@ -23,7 +22,7 @@
#define THROW(env, clazz, msg) do { env->ThrowNew(env->FindClass(clazz), msg); } while (0)
#define THROW(env, clazz, msg) do { env->ThrowNew(env->FindClass(clazz), msg); } while (0)
static
jclass
ProcessImpl
;
static
jclass
ProcessImpl
;
static
jfieldID
ProcessImpl_pid
;
static
jfieldID
ProcessImpl_pid
,
ProcessImpl_exitValue
,
ProcessImpl_exitValueMutex
;
static
int
sdk_version
()
{
static
int
sdk_version
()
{
char
version
[
PROP_VALUE_MAX
+
1
];
char
version
[
PROP_VALUE_MAX
+
1
];
...
@@ -42,15 +41,22 @@ jint Java_com_github_shadowsocks_jnihelper_sigterm(JNIEnv *env, jobject thiz, jo
...
@@ -42,15 +41,22 @@ jint Java_com_github_shadowsocks_jnihelper_sigterm(JNIEnv *env, jobject thiz, jo
return
kill
(
pid
,
SIGTERM
)
==
-
1
&&
errno
!=
ESRCH
?
errno
:
0
;
return
kill
(
pid
,
SIGTERM
)
==
-
1
&&
errno
!=
ESRCH
?
errno
:
0
;
}
}
j
int
Java_com_github_shadowsocks_jnihelper_waitpid
(
JNIEnv
*
env
,
jobject
thiz
,
jobject
process
)
{
j
object
Java_com_github_shadowsocks_jnihelper_getExitValue
(
JNIEnv
*
env
,
jobject
thiz
,
jobject
process
)
{
if
(
!
env
->
IsInstanceOf
(
process
,
ProcessImpl
))
{
if
(
!
env
->
IsInstanceOf
(
process
,
ProcessImpl
))
{
THROW
(
env
,
"java/lang/ClassCastException"
,
THROW
(
env
,
"java/lang/ClassCastException"
,
"Unsupported process object. Only java.lang.ProcessManager$ProcessImpl is accepted."
);
"Unsupported process object. Only java.lang.ProcessManager$ProcessImpl is accepted."
);
return
-
1
;
return
NULL
;
}
}
jint
pid
=
env
->
GetIntField
(
process
,
ProcessImpl_pid
);
return
env
->
GetObjectField
(
process
,
ProcessImpl_exitValue
);
int
status
;
}
return
waitpid
(
pid
,
&
status
,
WNOHANG
);
jobject
Java_com_github_shadowsocks_jnihelper_getExitValueMutex
(
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
NULL
;
}
return
env
->
GetObjectField
(
process
,
ProcessImpl_exitValueMutex
);
}
}
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
)
{
...
@@ -97,8 +103,10 @@ static JNINativeMethod method_table[] = {
...
@@ -97,8 +103,10 @@ static JNINativeMethod method_table[] = {
(
void
*
)
Java_com_github_shadowsocks_jnihelper_sendfd
},
(
void
*
)
Java_com_github_shadowsocks_jnihelper_sendfd
},
{
"sigterm"
,
"(Ljava/lang/Process;)I"
,
{
"sigterm"
,
"(Ljava/lang/Process;)I"
,
(
void
*
)
Java_com_github_shadowsocks_jnihelper_sigterm
},
(
void
*
)
Java_com_github_shadowsocks_jnihelper_sigterm
},
{
"waitpid"
,
"(Ljava/lang/Process;)I"
,
{
"getExitValue"
,
"(Ljava/lang/Process;)Ljava/lang/Integer"
,
(
void
*
)
Java_com_github_shadowsocks_jnihelper_waitpid
}
(
void
*
)
Java_com_github_shadowsocks_jnihelper_getExitValue
},
{
"getExitValueMutex"
,
"(Ljava/lang/Process;)Ljava/lang/Object"
,
(
void
*
)
Java_com_github_shadowsocks_jnihelper_getExitValueMutex
}
};
};
/*
/*
...
@@ -173,6 +181,14 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {
...
@@ -173,6 +181,14 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {
THROW
(
env
,
"java/lang/RuntimeException"
,
"ProcessManager$ProcessImpl.pid not found"
);
THROW
(
env
,
"java/lang/RuntimeException"
,
"ProcessManager$ProcessImpl.pid not found"
);
goto
bail
;
goto
bail
;
}
}
if
(
!
(
ProcessImpl_exitValue
=
env
->
GetFieldID
(
ProcessImpl
,
"exitValue"
,
"Ljava/lang/Integer"
)))
{
THROW
(
env
,
"java/lang/RuntimeException"
,
"ProcessManager$ProcessImpl.exitValue not found"
);
goto
bail
;
}
if
(
!
(
ProcessImpl_exitValueMutex
=
env
->
GetFieldID
(
ProcessImpl
,
"exitValueMutex"
,
"Ljava/lang/Object"
)))
{
THROW
(
env
,
"java/lang/RuntimeException"
,
"ProcessManager$ProcessImpl.exitValueMutex not found"
);
goto
bail
;
}
}
}
result
=
JNI_VERSION_1_6
;
result
=
JNI_VERSION_1_6
;
...
...
mobile/src/main/scala/com/github/shadowsocks/GuardedProcess.scala
View file @
86773085
...
@@ -115,12 +115,7 @@ class GuardedProcess(cmd: String*) {
...
@@ -115,12 +115,7 @@ class GuardedProcess(cmd: String*) {
private
def
destroyProcess
()
{
private
def
destroyProcess
()
{
if
(
Build
.
VERSION
.
SDK_INT
<
24
)
{
if
(
Build
.
VERSION
.
SDK_INT
<
24
)
{
JniHelper
.
sigtermCompat
(
process
)
JniHelper
.
sigtermCompat
(
process
)
var
tries
=
0
JniHelper
.
waitForCompat
(
process
,
500
)
while
(
JniHelper
.
waitpidCompat
(
process
)
==
0
&&
tries
<
3
)
{
Log
.
w
(
TAG
,
"still waiting for process "
+
Commandline
.
toString
(
cmd
))
Thread
.
sleep
(
200
)
tries
+=
1
}
}
}
process
.
destroy
()
process
.
destroy
()
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment