1

In Java, I can have something like this:

Process p = Runtime.getRuntime().exec("su");
DataOutputStream pOut = new DataOutputStream(p.getOutputStream());
pOut.writeBytes("find / -perm -2000 -o -perm -4000\n");
pOut.writeBytes("ps\n");
pOut.writeBytes("ls\n");
pOut.writeBytes("exit\n");
pOut.flush();
p.waitFor();

I know that to execute the find command in JNI method, we can use system or popen function. But I don't know how to execute it with su privilege?

PS: Since the system function forks a new child process. I want to have a single child process spawning up to execute multiple commands like in Java.

Krypton
  • 3,337
  • 5
  • 32
  • 52

3 Answers3

3

I don't see what's the point for you to use system() to execute, system() doesn't return you stream to read the output.

I think you should try popen() instead

2

Assuming you have su installed on your Android device and it is in the path, you could try it like this:

system("su -c \"find / -perm -2000 -o -perm -4000\" root");

su accepts arguments: the -c argument specifies a command to run.

EDIT: you might be out of luck there. I'm not sure if you can pass multiple commands to the su command. You might get away with passing it a shell and then passing that shell commands, but no guarantees there.

system("su -c \"sh -c 'find / -perm -2000 -o -perm -4000; rm /tmp/1'\" root");
Femi
  • 64,273
  • 8
  • 118
  • 148
  • Please check my edit, since the system function forks a new child process. I want to have a single child process spawning up to execute multiple commands like in Java. – Krypton Sep 18 '12 at 02:24
1

Femi answer is close, I have tested, the following works for me:

system("su -c \"find / -perm -2000 -o -perm -4000; ps; ls\"");