如何生成具有inheritance权限的进程

有没有办法让流程具有该流程的所有inheritance权限,我已经拥有了。

例如,我有一些过程;

Process superUserShell = Runtime.getRuntime().exec("su"); 

我能够获得输出流并执行这样的命令

 DataOutputStream outputStream = new DataOutputStream(superUserShell.getOutputStream()); // for example outputStream.writeBytes("rm -rf /*"); outputStream.flush(); 

但是我没有处理执行命令结果的可能性,所以我真的想要将另一个进程生成的进程分离(例如“ superUserShell ”)

有什么想法吗?


当然这不是为了邪恶的目的^ _ ^这只是我想到的第一件事。 实际上我正在为fbgrab的小型包装机工作…

 p = Runtime.getRuntime().exec("su");//lets assume my android os grants super user premissions. this is not the question!!!! DataOutputStream outputStream = new DataOutputStream(p.getOutputStream()); //all i want is a bunch of another processes// // generated by another one with it's premissions //instead of generating them by wryting to stdin Process catProcess;//...... Process someAnotherBinaryExecutionProcess;//...... outputStream.writeBytes("cat /dev/graphics/fb0 > "+ getFilesDir() + "/fb0\n"); outputStream.writeBytes("exit\n"); outputStream.flush(); p.waitFor(); 

首先,我希望这不是用于邪恶目的。 你的"rm -rf /*"例子引起了我的一些担忧。

如果您执行Runtime.getRuntime().exec("bash")您将获得一个shell,您可以发送命令并从中获取响应。 因此,例如,您可以将控制台绑定到它:

 final Process process = Runtime.getRuntime().exec("bash"); new Thread() { public void run() { try { InputStreamReader reader = new InputStreamReader(process.getInputStream()); for(int c = reader.read(); c != -1; c = reader.read()) { System.out.print((char)c); } } catch(IOException e) { e.printStackTrace(); } } }.start(); // (Same for redirecting the process's error stream to System.err if you want) InputStreamReader fromKeyboard = new InputStreamReader(System.in); OutputStreamWriter toProcess = new OutputStreamWriter(process.getOutputStream()); for(int c = fromKeyboard.read(); c != -1; c = fromKeyboard.read()) { toProcess.write((char)c); toProcess.flush(); } 

这是一个很好的实验方法,可以看看你的操作系统会让你做什么。 在Mac OS上,如果我想从这个过程sudo一个命令,我遇到的问题是它无法接受来自STDIN的密码,因为它实际上不是一个登录shell。 所以,我必须这样做:

 SUDO_ASKPASS="password.sh" sudo -A  

…其中“password.sh”只是回显我的密码,并且是我想以root身份运行的命令(我使用了很好的安全“pwd”而不是你的wipe-my-root-filesystem示例)。

几点说明:

  1. 我想你已经通过Process.getInputStream()获得了这个过程的输出?

     BufferedReader buf = new BufferedReader( new InputStreamReader( superUserShell.getInputStream() ) ) ; while ( ( String line ; line = buf.readLine() ) != null ) { // do domething with data from process; } 
  2. 尝试在命令中添加换行符,例如"rm -rf /* \r\n"

  3. 如果连续发送多个命令(并读取回复),那么您可能希望在单独的线程中发送和接收数据。

Selvin是对的,su立刻返回,并没有为你的应用程序提供类似真实的交互式shell的’shell’类型的情况。 你想要研究的是像sudo 让su运行你想要的命令。