Skip to content

Commit 89da9ab

Browse files
committed
refine libexec
1 parent 5bb9ccd commit 89da9ab

File tree

9 files changed

+102
-109
lines changed

9 files changed

+102
-109
lines changed

AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.github.shadowsocks"
44
android:installLocation="auto"
5-
android:versionCode="4"
6-
android:versionName="1.1">
5+
android:versionCode="5"
6+
android:versionName="1.1.1">
77

88
<uses-permission android:name="android.permission.INTERNET"/>
99
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

jni/Application.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
APP_PLATFORM = anrdoid-8
1+
APP_PLATFORM = android-9

jni/termExec.cpp

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <unistd.h>
5050
#include <termios.h>
5151
#include <signal.h>
52+
#include <stdio.h>
5253

5354
static jclass class_fileDescriptor;
5455
static jfieldID field_fileDescriptor_descriptor;
@@ -98,24 +99,15 @@ static int throwOutOfMemoryError(JNIEnv *env, const char *message)
9899
return env->ThrowNew(exClass, message);
99100
}
100101

101-
static int create_subprocess(const char *cmd,
102-
char *const argv[], char *const envp[], int* pProcessId)
102+
static int create_subprocess(const char *cmd, char *const argv[],
103+
char *const envp[], const char* scripts, int* pProcessId)
103104
{
104-
char filename[] = "/data/data/com.github.shadowsocks/defout";
105-
char script[] = "/data/data/com.github.shadowsocks/script";
106105
pid_t pid;
106+
int pfds[2];
107+
int pfds2[2];
107108

108-
if (chmod(script, 0755) < 0) {
109-
LOGE("error to chmod\n");
110-
exit(-1);
111-
}
112-
113-
int defout = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600);
114-
115-
if(defout < 0) {
116-
LOGE("open file error\n");
117-
exit(-1);
118-
}
109+
pipe(pfds);
110+
pipe(pfds2);
119111

120112
pid = fork();
121113

@@ -126,28 +118,41 @@ static int create_subprocess(const char *cmd,
126118

127119
if(pid == 0){
128120

129-
//setsid();
130-
131-
dup2(defout, 1);
132-
dup2(defout, 2);
133-
134121
if (envp) {
135122
for (; *envp; ++envp) {
136123
putenv(*envp);
137124
}
138125
}
139126

127+
dup2(pfds[0], 0);
128+
close(pfds[1]);
129+
130+
dup2(pfds2[1], 1);
131+
dup2(pfds2[1], 2);
132+
close(pfds2[0]);
133+
140134
execv(cmd, argv);
141-
exit(-1);
135+
136+
fflush(NULL);
137+
138+
exit(0);
139+
142140
} else {
143141
*pProcessId = (int) pid;
144-
return defout;
142+
143+
dup2(pfds[1], 1);
144+
close(pfds[0]);
145+
146+
write(pfds[1], scripts, strlen(scripts)+1);
147+
148+
close(pfds2[1]);
149+
return pfds2[0];
145150
}
146151
}
147152

148153

149154
static jobject android_os_Exec_createSubProcess(JNIEnv *env, jobject clazz,
150-
jstring cmd, jobjectArray args, jobjectArray envVars,
155+
jstring cmd, jobjectArray args, jobjectArray envVars, jstring scripts,
151156
jintArray processIdArray)
152157
{
153158
const jchar* str = cmd ? env->GetStringCritical(cmd, 0) : 0;
@@ -157,6 +162,13 @@ static jobject android_os_Exec_createSubProcess(JNIEnv *env, jobject clazz,
157162
env->ReleaseStringCritical(cmd, str);
158163
}
159164

165+
const jchar* str_scripts = scripts ? env->GetStringCritical(scripts, 0) : 0;
166+
String8 scripts_8;
167+
if (str_scripts) {
168+
scripts_8.set(str_scripts, env->GetStringLength(scripts));
169+
env->ReleaseStringCritical(scripts, str_scripts);
170+
}
171+
160172
jsize size = args ? env->GetArrayLength(args) : 0;
161173
char **argv = NULL;
162174
String8 tmp_8;
@@ -203,7 +215,8 @@ static jobject android_os_Exec_createSubProcess(JNIEnv *env, jobject clazz,
203215
}
204216

205217
int procId;
206-
int ptm = create_subprocess(cmd_8.string(), argv, envp, &procId);
218+
int ptm = create_subprocess(cmd_8.string(), argv, envp, scripts_8.string(),
219+
&procId);
207220

208221
if (argv) {
209222
for (char **tmp = argv; *tmp; ++tmp) {
@@ -311,7 +324,7 @@ static int register_FileDescriptor(JNIEnv *env)
311324
static const char *classPathName = "com/github/shadowsocks/Exec";
312325

313326
static JNINativeMethod method_table[] = {
314-
{ "createSubprocess", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;[I)Ljava/io/FileDescriptor;",
327+
{ "createSubprocess", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[I)Ljava/io/FileDescriptor;",
315328
(void*) android_os_Exec_createSubProcess },
316329
{ "waitFor", "(I)I",
317330
(void*) android_os_Exec_waitFor},

libs/armeabi/libexec.so

0 Bytes
Binary file not shown.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.github.shadowsocks</groupId>
66
<artifactId>shadowsocks</artifactId>
7-
<version>1.1</version>
7+
<version>1.1.1</version>
88
<packaging>apk</packaging>
99
<name>Shadowsocks</name>
1010

src/com/github/shadowsocks/Exec.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ public class Exec {
4343
* Callers are responsible for calling Exec.close() on the returned file
4444
* descriptor.
4545
*
46-
* @param cmd The command to execute
47-
* @param args An array of arguments to the command
46+
* @param cmd The command to execute.
47+
* @param args An array of arguments to the command.
4848
* @param envVars An array of strings of the form "VAR=value" to be added to the
49-
* environment of the process
49+
* environment of the process.
50+
* @param scripts The scripts to execute.
5051
* @param processId A one-element array to which the process ID of the started
5152
* process will be written.
52-
* @return the file descriptor of the started process.
53+
* @return File descriptor
5354
*/
5455
public static native FileDescriptor createSubprocess(String cmd,
55-
String[] args, String[] envVars, int[] processId);
56+
String[] args, String[] envVars,
57+
String scripts, int[] processId);
5658

5759
/**
5860
* Send SIGHUP to a process group.

src/com/github/shadowsocks/Shadowsocks.java

100644100755
File mode changed.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package com.github.shadowsocks;
22

33
import android.app.Application;
4+
import android.content.Context;
45
import com.google.analytics.tracking.android.EasyTracker;
56

67
public class ShadowsocksApplication extends Application {
78

9+
private static String sTmpDir;
10+
11+
public static String getTmpDir() {
12+
return sTmpDir;
13+
}
14+
815
@Override
916
public void onCreate() {
1017
EasyTracker.getInstance().setContext(this);
18+
sTmpDir = getCacheDir().getAbsolutePath();
1119
}
1220

1321
}

0 commit comments

Comments
 (0)