5

I have an Android phone that has only su binary installed and it works, meaning I can adb shell into the phone and run an 'su' command and I will be root.

When I try to run a command via code, it doesn't seem to work no matter which way I try to run it. I've tried many different variants of the following command.

Runtime.getRuntime().exec("su -c ps");

When I run this command on another rooted phone with a Superuser.apk or SuperSU.apk app installed, I get a dialog asking if I want to allow it to run with root permissions. When the apks are not there, it never asks and the command never works.

I've tried installing the apks on the first phone but they don't seem to do anything. So, as the original question asks --> Is there any way to run the elevated command from within the app without the SU apps installed?

Charles
  • 50,943
  • 13
  • 104
  • 142
brianestey
  • 8,202
  • 5
  • 33
  • 48
  • This is basically unanswerable without knowing *which* su binary is installed - if it is the stock android one, it is not usable *by an app* at all, under any circumstances. – Chris Stratton Nov 18 '14 at 21:18
  • Please checkout this: http://stackoverflow.com/questions/17615412/android-write-failed-epipe-broken-pipe-error-on-write-file – benchuk Jun 28 '15 at 13:01

4 Answers4

6

It might be because you need to pass the commands to su as parameters like this:
su -c 'ls -l'
Or you might need to specify the full path to su, but I don't see why it wouldn't work the way you have it: Runtime.exec("/system/bin/su -c ps")
Or maybe
Runtime.exec("/system/bin/su -c \'ps\'")

Try checking the output of this command too: System.getenv("PATH")

Another variant could be Runtime.exec("su -c \'ls -s \'")
Make sure you don't forget to escape the single quotes as they are part of the actual String.

Thats the way that I've found works most consistently, and it has also worked on devices that don't have Superuser or SuperSU installed, as those apps only listen for the Broadcast that is sent out when an application tries to run a command as root. @Boardy SuperSU and Superuser intercept the broadcast and so act as a middle man between the app and root privileges, but its not necessary for a rooted device. It IS necessary if you want to have more control over the applications that are running commands as root, but even then it still only limits you to deciding which applications, not which commands, are given root privileges.

Also, you might wanna take a look at RootTools and more specifically, the RootTools.isAccessGiven() command, which requests root privileges for your app.

Source: Launch a script as root through ADB

Community
  • 1
  • 1
cnexus
  • 454
  • 1
  • 3
  • 14
  • I have done most of the suggestions listed here. I will try the System.getenv and also try the single-quotes (that's a great suggestion and I'm kicking myself for not trying earlier!). One thing I can say is I did try RootTools and it returns false when I call isAccessGiven() but it shows true when I call isRootAvailable() so perhaps it's not as simple as adding single-quotes. The only issue now is I don't have access to the device at the moment so it will take some time to test this. Thanks for the advice! – brianestey Jan 21 '13 at 07:29
2

Not all versions of an 'su' for Android will accept a command to execute from the command line parameters.

Where they do not, you will need to let 'su' launch a privileged shell, obtain its input file descriptor, and pipe command(s) into that. This has been covered numerous times here on Stackoverflow.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • Could you please point to one such example on Stackoverflow. I am in need of one working example. – zeeshan Sep 27 '16 at 01:08
  • Any serious effort at searching will find them. But it's kind of beside the point now as this hasn't been workable for several Android versions since the switch to SELinux which requires far more involved workarounds, unless you have a custom or engineering build without that. – Chris Stratton Sep 27 '16 at 01:17
1

I believe you would need to have the SU apps installed as they are what provide the user the question as to whether the app should be allowed to run as root or not.

Boardy
  • 35,417
  • 104
  • 256
  • 447
  • Do you know what those apps do or how they work? Is this a dialog that I can provide from my own code? The reason I ask is I've tried both apps on my phone and neither pop up the dialog as I expected. – brianestey Jan 16 '13 at 05:19
  • @brianestey Unfortunately I don't know how they work. But if the device is properly rooted then the SU, or SuperSU apps should popup asking for permission to run as root. Are you using JellyBean 4.2 where Google have included multi user support. I've noticed a couple of root apps I use no longer work since Google introduced this feature. If you have both the apps installed it might be causing a conflict but don't know for sure – Boardy Jan 16 '13 at 09:25
  • No, this is not the answer. It's entirely possible (if risky) to have a simple 'su' without a linked user dialog app, such that the effect is "always allow". Of course an 'su' binary meant to be used with such an app probably would not work without it. – Chris Stratton Mar 16 '13 at 22:51
-3

You should be able to do this.

try :

adb root shell ls -l

rm-vanda
  • 3,122
  • 3
  • 23
  • 34
  • 1
    Could you expand this answer? As it stands now it is not a quality answer, and is at risk of being downvoted or deleted. – oɔɯǝɹ Oct 28 '14 at 20:15