问题
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.
回答1:
Femi answer is close, I have tested, the following works for me:
system("su -c \"find / -perm -2000 -o -perm -4000; ps; ls\"");
回答2:
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
回答3:
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");
来源:https://stackoverflow.com/questions/12469403/execute-a-command-with-superuser-permission-in-jni-method-in-android