Commit 7a52f700 authored by Max Lv's avatar Max Lv

disable redirect for commands

parent 634e077b
...@@ -99,7 +99,7 @@ static int throwOutOfMemoryError(JNIEnv *env, const char *message) ...@@ -99,7 +99,7 @@ static int throwOutOfMemoryError(JNIEnv *env, const char *message)
return env->ThrowNew(exClass, message); return env->ThrowNew(exClass, message);
} }
static int create_subprocess(const char *cmd, char *const argv[], static int create_subprocess(const int rdt, const char *cmd, char *const argv[],
char *const envp[], const char* scripts, int* pProcessId) char *const envp[], const char* scripts, int* pProcessId)
{ {
pid_t pid; pid_t pid;
...@@ -107,7 +107,10 @@ static int create_subprocess(const char *cmd, char *const argv[], ...@@ -107,7 +107,10 @@ static int create_subprocess(const char *cmd, char *const argv[],
int pfds2[2]; int pfds2[2];
pipe(pfds); pipe(pfds);
pipe(pfds2);
if (rdt) {
pipe(pfds2);
}
pid = fork(); pid = fork();
...@@ -127,9 +130,13 @@ static int create_subprocess(const char *cmd, char *const argv[], ...@@ -127,9 +130,13 @@ static int create_subprocess(const char *cmd, char *const argv[],
dup2(pfds[0], 0); dup2(pfds[0], 0);
close(pfds[1]); close(pfds[1]);
dup2(pfds2[1], 1); if (rdt) {
dup2(pfds2[1], 2); close(1);
close(pfds2[0]); close(2);
dup2(pfds2[1], 1);
dup2(pfds2[1], 2);
close(pfds2[0]);
}
execv(cmd, argv); execv(cmd, argv);
...@@ -144,15 +151,18 @@ static int create_subprocess(const char *cmd, char *const argv[], ...@@ -144,15 +151,18 @@ static int create_subprocess(const char *cmd, char *const argv[],
close(pfds[0]); close(pfds[0]);
write(pfds[1], scripts, strlen(scripts)+1); write(pfds[1], scripts, strlen(scripts)+1);
if (rdt) {
close(pfds2[1]); close(pfds2[1]);
return pfds2[0]; return pfds2[0];
} else {
return -1;
}
} }
} }
static jobject android_os_Exec_createSubProcess(JNIEnv *env, jobject clazz, static jobject android_os_Exec_createSubProcess(JNIEnv *env, jobject clazz,
jstring cmd, jobjectArray args, jobjectArray envVars, jstring scripts, jint rdt, jstring cmd, jobjectArray args, jobjectArray envVars, jstring scripts,
jintArray processIdArray) jintArray processIdArray)
{ {
const jchar* str = cmd ? env->GetStringCritical(cmd, 0) : 0; const jchar* str = cmd ? env->GetStringCritical(cmd, 0) : 0;
...@@ -215,7 +225,7 @@ static jobject android_os_Exec_createSubProcess(JNIEnv *env, jobject clazz, ...@@ -215,7 +225,7 @@ static jobject android_os_Exec_createSubProcess(JNIEnv *env, jobject clazz,
} }
int procId; int procId;
int ptm = create_subprocess(cmd_8.string(), argv, envp, scripts_8.string(), int ptm = create_subprocess(rdt, cmd_8.string(), argv, envp, scripts_8.string(),
&procId); &procId);
if (argv) { if (argv) {
...@@ -324,7 +334,7 @@ static int register_FileDescriptor(JNIEnv *env) ...@@ -324,7 +334,7 @@ static int register_FileDescriptor(JNIEnv *env)
static const char *classPathName = "com/github/shadowsocks/Exec"; static const char *classPathName = "com/github/shadowsocks/Exec";
static JNINativeMethod method_table[] = { static JNINativeMethod method_table[] = {
{ "createSubprocess", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[I)Ljava/io/FileDescriptor;", { "createSubprocess", "(ILjava/lang/String;[Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[I)Ljava/io/FileDescriptor;",
(void*) android_os_Exec_createSubProcess }, (void*) android_os_Exec_createSubProcess },
{ "waitFor", "(I)I", { "waitFor", "(I)I",
(void*) android_os_Exec_waitFor}, (void*) android_os_Exec_waitFor},
......
No preview for this file type
...@@ -43,6 +43,7 @@ public class Exec { ...@@ -43,6 +43,7 @@ public class Exec {
* Callers are responsible for calling Exec.close() on the returned file * Callers are responsible for calling Exec.close() on the returned file
* descriptor. * descriptor.
* *
* @param rdt Whether redirect stdout and stderr
* @param cmd The command to execute. * @param cmd The command to execute.
* @param args An array of arguments to the command. * @param args An array of arguments to the command.
* @param envVars An array of strings of the form "VAR=value" to be added to the * @param envVars An array of strings of the form "VAR=value" to be added to the
...@@ -52,7 +53,7 @@ public class Exec { ...@@ -52,7 +53,7 @@ public class Exec {
* process will be written. * process will be written.
* @return File descriptor * @return File descriptor
*/ */
public static native FileDescriptor createSubprocess(String cmd, public static native FileDescriptor createSubprocess(int rdt, String cmd,
String[] args, String[] envVars, String[] args, String[] envVars,
String scripts, int[] processId); String scripts, int[] processId);
......
...@@ -44,7 +44,7 @@ public class Utils { ...@@ -44,7 +44,7 @@ public class Utils {
String arg0 = argList.get(0); String arg0 = argList.get(0);
String[] args = argList.toArray(new String[1]); String[] args = argList.toArray(new String[1]);
return Exec.createSubprocess(arg0, args, null, return Exec.createSubprocess(result != null ? 1 : 0, arg0, args, null,
scripts + "\nexit\n", processId); scripts + "\nexit\n", processId);
} }
...@@ -112,7 +112,7 @@ public class Utils { ...@@ -112,7 +112,7 @@ public class Utils {
pipe = createSubprocess(pid, getShell()); pipe = createSubprocess(pid, getShell());
} }
if (pipe == null) return; if (result == null || pipe == null) return;
if (pid[0] != -1) { if (pid[0] != -1) {
exitcode = Exec.waitFor(pid[0]); exitcode = Exec.waitFor(pid[0]);
...@@ -125,8 +125,7 @@ public class Utils { ...@@ -125,8 +125,7 @@ public class Utils {
// Read stdout // Read stdout
while (stdout.available() > 0) { while (stdout.available() > 0) {
read = stdout.read(buf); read = stdout.read(buf);
if (result != null) result.append(new String(buf, 0, read));
result.append(new String(buf, 0, read));
} }
} catch (Exception ex) { } catch (Exception ex) {
......
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