JohnC1
JohnC1

Reputation: 861

Root Android shell hangs on ReadLine() using Runtime().Exec("command")

please look at my code below.

public static bool RunRootCommands(List<string> commands)
{
    Java.Lang.Process rootProcess;
    try
    {
        rootProcess = Runtime.GetRuntime().Exec("su");

        var outputStream = new Java.IO.DataOutputStream(rootProcess.OutputStream);
        var inputStream = new Java.IO.DataInputStream(rootProcess.InputStream);

        if (inputStream != null && outputStream != null)
        {
            // This works. Can ReadLine() just fine.
            // outputStream.WriteBytes("id\n");

            outputStream.WriteBytes("pm disable com.example.app");

            string line = inputStream.ReadLine(); // Gets stuck here. Waiting and app lags.

            outputStream.WriteBytes("exit\n");
            outputStream.Flush();

            return true;
        }
    }
    catch { }

    return false;
}

I am able to get root access. When I run the "id\n" command, I am able to continue and get data back from Readline(). However, with the current command you see, it works perfect on a regular shell but it's not working from my application. What happens is that it gets stuck on ReadLine(), it does not display any errors, just hangs in there. The command I enter via outpustStream gets activated. I tried it without Input.ReadLine(), however, in that case, the command does not get activated successfully. Tried it with "/n" at the end also.

Upvotes: 2

Views: 671

Answers (1)

Marknl
Marknl

Reputation: 182

You have to end each command with \n or the command won't be executed.

outputStream.WriteBytes("pm disable com.example.app\n");

Upvotes: 1

Related Questions