Run shell command from priv-app application

Viewed 1378

I am trying to run shell commands from a system/priv-app I have added to the AOSP (7.1.1)

The command I am trying to run is: ip link add dev can0 type can to switch on the can bus.

I have build the image as both -eng & -userdebug releases. The command runs fine in the adb shell and successfully switches on the CAN bus as expected.

My problem is that I get the following error:

Cannot run program "su": error=13, Permission denied

When I try the following code within the system privileged java app:

//ArrayList<String> commands is passed into the method
try {
  if (null != commands && commands.size() > 0) {
     Process suProcess = Runtime.getRuntime().exec("su");
     DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
     for (String currCommand : commands) {
        os.writeBytes(currCommand + "\n");
        os.flush();
     }
     os.writeBytes("exit\n");
     os.flush();
     BufferedReader stderr = new BufferedReader(new InputStreamReader(suProcess.getErrorStream()));
     String line = "";
     String errString = "";
     while ((line = stderr.readLine()) != null) errString += line + "\n";
     suProcess.waitFor();
     if (suProcess.exitValue() != 0)
        throw new Exception(errString);
 } //Handle exception
1 Answers

You may try to do it in this way:

Process mProcess = new ProcessBuilder()
                       .command("/system/xbin/su")
                       .redirectErrorStream(true).start();

DataOutputStream out = new DataOutputStream(mProcess.getOutputStream());
Related