Krypton
Krypton

Reputation: 3325

Execute a command with superuser permission in JNI method in Android

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.

Upvotes: 1

Views: 6006

Answers (3)

user637119
user637119

Reputation:

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

Upvotes: 3

user1679072
user1679072

Reputation:

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

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

Upvotes: 1

Femi
Femi

Reputation: 64700

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");

Upvotes: 2

Related Questions