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
8d1f2da1
Commit
8d1f2da1
authored
Jan 24, 2017
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement SIGTERM in jni-helper
parent
30c8c7e6
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
35 deletions
+66
-35
mobile/src/main/java/com/github/shadowsocks/JniHelper.java
mobile/src/main/java/com/github/shadowsocks/JniHelper.java
+17
-6
mobile/src/main/jni/jni-helper.cpp
mobile/src/main/jni/jni-helper.cpp
+37
-26
mobile/src/main/scala/com/github/shadowsocks/GuardedProcess.scala
...rc/main/scala/com/github/shadowsocks/GuardedProcess.scala
+12
-3
No files found.
mobile/src/main/java/com/github/shadowsocks/JniHelper.java
View file @
8d1f2da1
...
@@ -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
);
}
}
mobile/src/main/jni/jni-helper.cpp
View file @
8d1f2da1
#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: registerNative
s failed"
);
THROW
(
env
,
"java/lang/RuntimeException"
,
"registerNativeMethod
s 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
;
...
...
mobile/src/main/scala/com/github/shadowsocks/GuardedProcess.scala
View file @
8d1f2da1
...
@@ -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
()
}
}
}
}
...
...
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