1

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") in MainActivity::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?

Vatish Sharma
  • 1,536
  • 3
  • 16
  • 35

1 Answers1

1

I was able to send commands to the the /dev/diag device and activate the QCDM messages on a Pixel 3a phone. I did this in the open source Android app Network Survey+.

Specifically, take a look at this line of code, which is something like this:

new String[]{"su", "-c", "exec <path_to_shared_library>"};

That line of code builds the shared command that is then executed by the ProcessBuilder.

The source code for the shared library can be found here. Note that I did not write the diag_revealer code, instead that was taken from the Mobile Insight app.

I think maybe the problem you ran into is executing from /system. Instead, make sure the shared library is in your app's private directory. The actual code I used pointed to the app's files directory:

return new String[]{"su", "-c", "exec " + diagRevealer + " " + context.getFilesDir() + "/" + context.getResources().getResourceEntryName(R.raw.rrc_filter_diag_edit) + " " + fifoPipeName};
Craxiom
  • 25
  • 3
  • how would you pass the argv through the exec? @Craxiom – MRDRAG May 21 '21 at 07:36
  • @MRDRAG, I passed them in after the command. In my example the `diagRevealer` was the command, I added a space, and then the concatenation that follows was argv[1] and `fifoPipeName` was argv[2]. – Craxiom May 25 '21 at 12:19