从priv-app应用程序运行shell命令

Aar*_*son 6 shell android android-source

我试图从我添加到AOSP(7.1.1)的system/priv-app运行shell命令

我试图运行的命令是:ip link add dev can0 type可以打开can总线.

我已经建立了图像既是-eng&-userdebug版本.该命令在adb shell中运行良好,并按预期成功打开CAN总线.

我的问题是我收到以下错误:

无法运行程序"su":错误= 13,权限被拒绝

当我在系统特权java应用程序中尝试以下代码时:

//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
Run Code Online (Sandbox Code Playgroud)

小智 0

您可以尝试按以下方式进行:

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

DataOutputStream out = new DataOutputStream(mProcess.getOutputStream());
Run Code Online (Sandbox Code Playgroud)