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)
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)
{
pid_t pid;
......@@ -107,7 +107,10 @@ static int create_subprocess(const char *cmd, char *const argv[],
int pfds2[2];
pipe(pfds);
pipe(pfds2);
if (rdt) {
pipe(pfds2);
}
pid = fork();
......@@ -127,9 +130,13 @@ static int create_subprocess(const char *cmd, char *const argv[],
dup2(pfds[0], 0);
close(pfds[1]);
dup2(pfds2[1], 1);
dup2(pfds2[1], 2);
close(pfds2[0]);
if (rdt) {
close(1);
close(2);
dup2(pfds2[1], 1);
dup2(pfds2[1], 2);
close(pfds2[0]);
}
execv(cmd, argv);
......@@ -144,15 +151,18 @@ static int create_subprocess(const char *cmd, char *const argv[],
close(pfds[0]);
write(pfds[1], scripts, strlen(scripts)+1);
close(pfds2[1]);
return pfds2[0];
if (rdt) {
close(pfds2[1]);
return pfds2[0];
} else {
return -1;
}
}
}
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)
{
const jchar* str = cmd ? env->GetStringCritical(cmd, 0) : 0;
......@@ -215,7 +225,7 @@ static jobject android_os_Exec_createSubProcess(JNIEnv *env, jobject clazz,
}
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);
if (argv) {
......@@ -324,7 +334,7 @@ static int register_FileDescriptor(JNIEnv *env)
static const char *classPathName = "com/github/shadowsocks/Exec";
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 },
{ "waitFor", "(I)I",
(void*) android_os_Exec_waitFor},
......
No preview for this file type
......@@ -43,6 +43,7 @@ public class Exec {
* Callers are responsible for calling Exec.close() on the returned file
* descriptor.
*
* @param rdt Whether redirect stdout and stderr
* @param cmd The command to execute.
* @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
......@@ -52,7 +53,7 @@ public class Exec {
* process will be written.
* @return File descriptor
*/
public static native FileDescriptor createSubprocess(String cmd,
public static native FileDescriptor createSubprocess(int rdt, String cmd,
String[] args, String[] envVars,
String scripts, int[] processId);
......
......@@ -44,7 +44,7 @@ public class Utils {
String arg0 = argList.get(0);
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);
}
......@@ -112,7 +112,7 @@ public class Utils {
pipe = createSubprocess(pid, getShell());
}
if (pipe == null) return;
if (result == null || pipe == null) return;
if (pid[0] != -1) {
exitcode = Exec.waitFor(pid[0]);
......@@ -125,8 +125,7 @@ public class Utils {
// Read stdout
while (stdout.available() > 0) {
read = stdout.read(buf);
if (result != null)
result.append(new String(buf, 0, read));
result.append(new String(buf, 0, read));
}
} 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