问题
I am developing an application for One Plus 6. This applications is using a shared library(lib.so) to perform some task. For the task it needs to open dev/diag first and then send some commands through code. Below is the code to open dev/diag:
fd = open("/dev/diag", O_RDWR|O_LARGEFILE|O_NONBLOCK);
if (fd < 0) {
perror("open diag dev");
return -8002;
}
I am able to build a executable with include $(BUILD_EXECUTABLE)
written in Android.mk
and with ndk-build
command. I am able to run open the dev/diag with placing the executable in /system folder of android.
The thing that failing is to do the same with an android application. I have tried following:
- Rooted the One Plus 6
- Call
Runtime.getRuntime().exec("su")
inMainActivity::onCreate
. - Give
<uses-permission android:name="android.permission.ACCESS_SUPERUSER"/>
in manifest. - Then call the required jni function.
Please suggest how can I enable su access on a function which is opening dev/diag for me?
I have also tried to do the same with running executable commands using ndk-build executable in /system e.g.
For READ:
// Run the command
Process process = Runtime.getRuntime().exec("su -c /system/lib get key");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
// Grab the results
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line).append("\n");
}
String allLogs = log.toString();
For WRITE:
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
os.writeBytes("/system/lib set key " + value);
// Close the terminal
os.writeBytes("exit\n");
os.flush();
and it is working fine. Still I am wondering how to do the same with shared library. Any help?
来源:https://stackoverflow.com/questions/55232995/how-to-open-dev-diag-with-super-user-permissions