Execute a command with superuser permission in JNI method in Android

Deadly 提交于 2020-01-03 03:28:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!