Run shell command from priv-app

I am trying to run shell commands from a system / private application that I added to AOSP (7.1.1)

The command I'm trying to run is: ip link add dev can0 type can enable the can bus.

I created an image for both -eng and -userdebug . The command works fine in the adb shell and successfully enables the CAN bus as expected.

My problem is that I get the following error:

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

When I try to use the following code in a system privileged java application:

//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
+4
source share
1 answer

:

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

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

Source: https://habr.com/ru/post/1686544/


All Articles