2012-01-05 75 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例子)。