17

I've got an app that's supposed to use some shell commands to copy a file from the sdcard to /system/media/. It will require root, and I am testing on a rooted device. I'm using runtimes to execute the shell commands, but it's not working. Here's what I've got for my runtimes

public void RunAsRoot{String[] commands = {"sysrw", "rm /data/local/bootanimation.zip", "sysro"};{
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());            
    for (String tmpCmd : commands) {
    os.writeBytes(tmpCmd+"\n");
    }           
    os.writeBytes("exit\n");  
    os.flush();
}

But my logcat only shows two of them not getting rejected

07-30 03:14:11.112: WARN/su(3593): request rejected (10047->0 /system/bin/sh)
07-30 03:14:11.132: DEBUG/su(3592): 10047 com.bba.stormdroid executing 0 /system/bin/sh using shell /system/bin/sh : sh
07-30 03:14:11.152: WARN/su(3594): request rejected (0->0 /system/bin/sh)
07-30 03:14:11.182: WARN/su(3595): request rejected (0->0 /system/bin/sh)
07-30 03:14:11.202: WARN/su(3596): request rejected (0->0 /system/bin/sh)
07-30 03:14:11.242: DEBUG/su(3597): 10047 com.bba.stormdroid executing 0 /system/bin/sh using shell /system/bin/sh : sh

Those two look to be the sysrw and sysro commands, yet the app still asks for root permission when I trigger this code. I'm new to working with root stuff and I can't seem to figure out how to get this to work.

Michelle
  • 2,830
  • 26
  • 33
Ben
  • 758
  • 1
  • 8
  • 19
  • can i get a working project of this sample? – Manoj Kumar Sep 26 '12 at 11:26
  • This isn't working for me and I'm not getting any FileNotFound exceptions or anything in Java. Also, SuperSU intercepts the root request and I'm able to grant it so I know that `getRuntime()` is working. For some reason it just won't let me manipulate files in the `/system/media` folder. – fIwJlxSzApHEZIl Mar 14 '13 at 00:24
  • Ah it looks like sysrw and sysro are unknown commants. – fIwJlxSzApHEZIl Mar 14 '13 at 00:24
  • Sorry for the naive question: this only works with a rooted device, isn't it? – Diana Oct 24 '13 at 10:09

1 Answers1

28

To run root commands, you have to use the flllowing format:

    public void RunAsRoot(String[] cmds){
            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());            
            for (String tmpCmd : cmds) {
                    os.writeBytes(tmpCmd+"\n");
            }           
            os.writeBytes("exit\n");  
            os.flush();
}

where you pass in an array of strings, each string being a command that needs to be executed. For example:

String[] commands = {"sysrw", "rm /data/local/bootanimation.zip", "sysro"};
user496854
  • 6,461
  • 10
  • 47
  • 84
  • When I put that in my onClick, it says the ()'s around (String[] cmds) should be ;'s, which just adds more errors when I do that. – Ben Jul 31 '11 at 04:26
  • I just edited my original answer to show an example of the string array that needs to be passed in – user496854 Jul 31 '11 at 21:48
  • Unless I'm doing this wrong, it's highlighting RunAsRoot and saying void is an invalid type for RunAsRoot. I've updated my question with the code I have now – Ben Aug 01 '11 at 01:12
  • You can't pass in a string array like that. You have to assign it first, then pass it in as a variable. The RunAsRoot function should be identical to what I posted – user496854 Aug 02 '11 at 09:05
  • 3
    One thing I wonder is why input is via `outputstream` and output via `inputstream`, naming issue ? – S.D. Apr 29 '13 at 06:18
  • 3
    Is it possible to get the output of the command I just executed..? for example "ls /data" – 3lokh May 26 '15 at 17:14
  • 1
    The above code seems not working with non-Rooted devices – vgokul129 Jan 03 '17 at 14:22