Run shell command from priv-app application

cancel
Showing results for 
Search instead for 
Did you mean: 

Run shell command from priv-app application

2,539 Views
tommos23
Contributor III

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

Labels (2)
Tags (2)
0 Kudos
2 Replies

396 Views
b36401
NXP TechSupport
NXP TechSupport

The user has no rights to perform "su" action.
You need to add android.permission.ACCESS_SUPERUSER permissions for the user.

Have a great day,
Victor

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

396 Views
tommos23
Contributor III

Thanks for the reply Victor,

I believe android.permission.ACCESS_SUPERUSER has been deprecated since Android 5 and I receive the following error in the logcat report.

W PackageManager: Unknown permission android.permission.ACCESS_SUPERUSER in package com.carnation.flexcan

Is there another way to give an application su privileges?

0 Kudos