Run adb shell command using android code - android

echo 0 > /sys/class/leds/button-backlight/brightness
The above command is working perfectly through adb shell.But when I try to run through my code there is no effect.Here is my code.
Process mSuProcess;
mSuProcess = Runtime.getRuntime().exec("su");
DataOutputStream mSuDataOutputStream = new DataOutputStream(mSuProcess.getOutputStream());
mSuDataOutputStream.writeBytes("echo 0 > /sys/class/leds/button-backlight/brightness \n");
mSuDataOutputStream.flush();
mSuDataOutputStream.close();
Please help me out on this.

If the device which you are working on is not rooted then this may not work..

The google original su binary checks for UID and only works if run by root or shell users. This is why it works when you run your command from adb shell interactively. In order to run this code from a java app you need to use a patched su binary - without UID checks.

Related

Understanding command through adb shell and through code - Android

I am trying to give BATTERSTATS permission to an app. When I run the command from a PC - it works just fine:
adb shell pm grant com.example.sample.myapplication android.permission.BATTERY_STATS
But the same pm grant command does not work when run from Android app:
java.lang.Process process = Runtime.getRuntime().exec("pm grant com.example.sample.myapplication android.permission.BATTERY_STATS");
Does it require root permission to give this permission?
If it is so, why it does not require the device to be rooted to run it via adb shell?
I am new to Android, please explain a bit more clearly what is happening inside and how to proceed.
The command you run with adb shell gets executed with shell(UID=2000) user privileges. The command you run from your java code gets executed with your app's UID privileges. Thus the difference.

Piping output from 1 command to other command in an adb shell command line

I wish to send an adb shell command to an Android device that pipes output from 1 command to other command, but when I try to do that, only the 1st command is executed on the device, and the 2nd one is executed on the host machine. Here's what I tried:
adb shell command1 | command2
command1 is executed on the device, and command2 on the host machine.
How can I get this to work properly?
Thanks!
You could use something like this:
adb shell "command1 | command2"
One way is to create a shell script containing the commands you want and then run the shell script.
Since the Android root filesystem is not writeable at run time (usually, unless you have rooted your device and remount it), you can copy the file to the removable (or emulated) storage, for example /sdcard.
Then run the script using the command adb shell sh /sdcard/your-script-name. Because each script runs in its own subshell, both of your commands will be executed in the same shell on the device (you can confirm it with ps).
adb shell "command1 && command2"
example:
Recursive listing of all files under /system/lib that contain 'foo':
adb shell "cd /system/lib&&ls -lR .|grep -i foo"
The important thing is the double quotes and the double ampersand.
The only thing is that you cannot use it for input as well, meaning that running an executable that requires stdin using one-liner wouldn't work as it requires user intervention.

Start a process in background from adb shell without attaching the process to the terminal in Android

I have a simple problem.
I want to start/run a program file on an android phone using adb shell.
My Program is in /system/bin folder and has root privileges.
I want to run this program from my command prompt as adb shell runme 3000000 > logs.txt but it should not block the terminal, It should run in background.
I cannot use screen/disown/nohup for my problem as android doesn't have all this.
I tried
adb shell "runme >logs.txt &" but of no use.
When i issue command as
adb shell
# runme 3000000 > logs.txt &
It runs fine, when i exit the terminal/disconnect the device and then connect again to system.
Do adb shell ps | grep runme shows the process is still runnning in background.
Thanks
Busybox has nohup applet which works just fine in Android

Executing a Root access based commands on Android application

I basically want to start a process which resides in /system/bin/... from the Application java command.
I tried all types of Runtime.process.exec() options
Tried the su and ouputStream combination as well, but nothing is able to start the application.
I am using the code in device and emulators, no luck in both.
I am able to run the same commands from ADB Shell successfully (as it has root permissions).
The device is rooted, I am able to see the # when I use adb to debug my device and it also can go into su via adb.
based on my searching I found that (after doing ps from adb shell)
I am able to run any command with lrwz--x--x permission such as (ls->toolbox, ps->toolbox) from the application layer but I am not able to execute any other commands which are not linked to the toolbox.
This doesn't execute ls:
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os=new DataOutputStream(p.getOutputStream());
os.writeBytes("ls \n");
os.flush();
But this does execute ls:
Process p = Runtime.getRuntime().exec("ls");
I would really appreciate if I can get any help on this here! I am posting this after doing lots of research.
Rather than sending ls \n to the su command's standard input, try running:
su -c ls
The -c option to su(1) asks it to run a command with elevated privileges rather than starting an interactive shell.
Try creating a shell script file(eg: init.sh) and write following commands in it:
su
reboot
Now try executing this shell script by
Runtime.getRuntime().exec("sh init.sh");
If this restarted your droid then you can write your required code in init.sh file & run it.

Android ADB Linux problem

I'm trying to test Strace tool in Android Real device but I can't get any information from it.
I was trying the following command but the device still tell me "permission denied".
'$' adb shell su strace -p <PID_number>
I tried to split the commands and make it in two times in this way and it works
'$' adb shell su
'#' strace -p <PID_number>
I tried to put the previous code in this way, to make it secuentially but still is not working:
'$' adb shell su && strace -p <PID_number>
What I'm doing wrong? Thanks in advance
Non-rooted phones won't let you run the su command. Besides, strace is not a default binary in android. In order for this command to work you will need a rooted device and an ARM binary of strace.
Hey guys finally I found the answer and I want to share it with you :)
As you can see in the previous picture I was using shell:shell in strace instead of root:shell so I changed using the following command:
'#' chown root strace
After that I give permission to execute with this one:
'#' chmod 4777 strace
So now, I have strace in this way and I don't see anymore ""ptrace attach failed: Operation not permitted" message
-rwsrwxrwx root shell 134508 2011-03-1 16:41 strace
Thank you to everybody helping me finding the solution ;)
Use:
$ adb shell su -c strace -p <PID>
or set the SETUID bit on strace.

Categories

Resources