2012-01-05 98 views
2

是否有任何方法可以使我拥有的进程拥有所有继承的进程权限。如何生成具有继承权限和权限的进程

例如我有一些过程;

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

,我能够得到输出流,并执行这样的

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

命令,但我没有posobilities处理执行的命令的结果,所以我真的瓦纳是已经产生的分离过程通过另一方法(例如,通过“superUserShell”)

任何想法?


当然这是不作恶^ _ ^这仅仅是第一件事,我记住了。 其实我的工作fbgrab为Android的小wraper ...

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(); 
+1

你运行“su”,然后Procces完成...使用man su – Selvin 2012-01-05 17:38:19

+0

@Selvin ...你错了 – Selvin 2012-01-05 18:15:04

回答

0

的几个注意事项:

  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. 如果您连续发送多个命令(和读取回复),那么您可能希望在单独的线程中发送和接收数据。

+0

是的这是我现在做的,但我想要的是处理从进程返回的值。 – 2012-01-05 18:04:43

+0

这应该是正确的 - 原则上。请显示更多代码,以便我们检查它。 – 2012-01-05 18:07:09

-1

Selvin的权利,su立即返回,并且不会像应用真正的交互式shell那样为您的应用程序提供'shell'类型的情况。你想看什么就像sudo <command>让苏运行你想要的命令。

+0

'man su'说:su - 运行一个替代用户和组ID的shell – 2012-01-05 17:54:32

+0

我认为你错了。苏不立即返回,我发送throght sdtin命令,他们执行,直到我通过“退出\ n” – 2012-01-05 18:02:54

3

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

如果你这样做Runtime.getRuntime().exec("bash")你会得到,你可以发送命令和从得到响应的壳。因此,举例来说,你可以配合控制台进去:

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上,如果我想要从此进程中获取命令,我遇到了无法接受来自STDIN的密码的问题,因为它不是真正的登录shell。所以,我必须这样做:

SUDO_ASKPASS="password.sh" sudo -A <command> 

...其中“password.sh”只是呼应我的密码,就是我要以root身份运行命令(我用漂亮的安全“PWD”,而不是你的wipe-my-root-filesystem例子)。