4949#include < unistd.h>
5050#include < termios.h>
5151#include < signal.h>
52+ #include < stdio.h>
5253
5354static jclass class_fileDescriptor;
5455static 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
149154static 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)
311324static const char *classPathName = " com/github/shadowsocks/Exec" ;
312325
313326static 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},
0 commit comments