2

I'm developing a small application that has to read the contents of a protected folder, so I came to the conclusion that I had to use su in combination with ls and an inputstream.

However, when the readLine function reaches the end of the output, it just hangs. I'm guessing this is because the last line doesn't include an EOL mark...

Code:

 p = Runtime.getRuntime().exec("su");
 outputS = new DataOutputStream(p.getOutputStream()); 
 inputS = new DataInputStream(p.getInputStream());
 errorS = new DataInputStream(p.getErrorStream());


 outputS.writeBytes("ls /datadata" + "\n");
 outputS.flush();

 while((s = inputS.readLine()) != null)
 {
      Log.i("DS", s);
 }
 Log.i("DS", "done");

How can I get around this? I've tried reading chars and then just wait for -1, but that didn't work, same thing as above. Ideally I'd do this without system calls, but Android sadly won't let me list or copy the contents of this folder...

Thank you for reading this, Quint

EDIT: Thanks to the answer below I solved my problem, whatever you do, just make sure that you end the "su" task just like you would do when using adb shell. You can use the method described in the accepted answer, or manually run "exit" after you did whatever you needed to do. Running exit does generate two empty lines in the errorstream for reasons unknown to me.

Quint Stoffers
  • 790
  • 8
  • 23

1 Answers1

1

su is still waiting for more commands to execute

You can tell su to execute only one command, and exit. Change

p = Runtime.getRuntime().exec("su");

to

p = Runtime.getRuntime().exec(new String[]{"su", "-c", "ls /datadata"});

and remove the outputS... rows

Daniel Fekete
  • 4,988
  • 3
  • 23
  • 23
  • Ah right, so basically I just request su every time I do something instead of once. What also worked was adding another output line: "outputS.writeBytes("exit" + "\n");", I assume that's more efficient when you have numerous tasks that require su, correct? So thanks for that explanation! – Quint Stoffers Sep 05 '11 at 11:20