I need to remount android file system as read write instead of read only. I know how to do it through command prompt and also how to go about it for application development.
But I am into tweaking the android source code and there if I use
Process proc=Runtime.getRuntime.exec("su");
it doesnot work. Therefore I need to use android.os.process object for the same and I am stuck here can anybody help me with it.
Try this code to get superuser access (you'll need a rooted phone with superuser installed):
try {
// Perform su to get root privledges
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("<..... here you may write commands ....>\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
} catch (IOException e) {
toastMessage("not root");
}
Once you have superuser access, you may start adbd listening on some TCP port using:
setprop service.adb.tcp.port 5555
adbd &
setprop service.adb.tcp.port -1
Open a TCP connection to the selected port and issue remount command.
Actually, it's difficult to understand what are you going to do. But if you're tweaking android sources (aosp) then it's will be easier for you to modify init.rc (which is under system/core/rootdir) file to get system image rw. If you want to change the state during the runtime you need to create your own native application (for instance, look into the run-as command), change inside it the uid of the process, make it as suid, embed it into the sources, and call it from your application.
Related
I am relatively new to android and I just came across adb shell commands which provide a lot of useful things. My issue is that when I execute the command "top -bn1" using adb shell from terminal it gives me a bunch of processes with users - root, system and other users. But when I run the same command from an application using Runtime.getRuntime().exec("top -bn1") the ouput is totally different and only processes for a particular user are displayed.
Here is the adb output -
And here is the code in the app and its output -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
process = Runtime.getRuntime().exec("top -bn1");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String msg = bufferedReader.lines().collect(Collectors.joining("\n"));
Log.d("Data", msg);
} catch (IOException e) {
e.printStackTrace();
}
}
Can somebody please explain -
Why is there a difference in the outputs as such?
How can I get the entire output by running the command from application?
What all requirements are there for the same?
adb shell is member of a lot of groups so it has a lot of privileges. It is not root, but it is as close as it gets. You can see the list of groups that the shell user is added by running id command from the terminal or check out this answer. On the other hand, your application runs in a sandbox and have very limited privileges. That's why you see a limited info when you run a command from the app code.
If you want to have unlimited access to the system from within your application, you need to request root access. You can do that by running the su command in a rooted phone:
Runtime.getRuntime().exec("su");
I want to write an application which roots the device on which it is installed, I mean by installing this app you will be able to root your device without a computer, just like the app in the following link,
http://www.kingoapp.com/root-tutorials/how-to-root-android-without-computer.htm
I've searched a lot on how to do that using Java code for Android devices, but there was no clear solution to me. Based on my research, I think we need the following steps:
1- Being able to use shell commands in Android using Runtime.getRuntime().exec();
2- Executing a command that gains root privileges (I think su, but this needs a rooted device to be executed).
3- Initiate a root command that will root the device.
I couldn't find a code explanation on how to do the steps above. I want to understand this process first, the commands that can be used in it, then I want to try to implement it by myself. Since there are many apps on the store that offer this feature, then implementing it must be feasible.
Could anyone please explain to me how to implement this process?
Also, is there a possibility to write a code for the opposite process, which is unrooting the device?
Any help is appreciated.
Thanks.
To run root commands, you have to use the following format:
public void RunAsRoot(String[] cmds){
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
}
os.writeBytes("exit\n");
os.flush();
}
where you pass in an array of strings, each string being a command that needs to be executed. For example:
String[] cmds = {"sysrw", "rm /data/local/bootanimation.zip", "sysro"};
I have android device with root and i try to implement some small app. This app need to read files from /proc/pid/net . I made it with Runtime.getRuntime().exec(new String[] { "su", "-c", "cat /proc/"+PID+ "/net/tcp6" }); but I must accept su -permission for each pid. There are some other possibilities how i can to read system files in android from my app? Something with FileReader? How can I get the su-permissions without exec -commando?
The exec command IS how you get su permissions. You might be able to chmod 777 the files you want and then they can likely be read via java. That, or you could move the files you want to read to the sdcard, or your apps data location and read them from there. Here is something very useful for root. You won't have to manually use the exec command each time, but RootTools does still use exec.
I believe if you do something like:
Process p = Runtime.getRuntime().exec("su");
you will get the root access.
And then you can do just:
p.getRuntime().exec("command");
and then you won't have to put the su in as long as that process is still active.
Though, I haven't done what I explained above (with the process) in quite some time, so I may be wrong. You may still have to include su each time. But either way, I'd recommend using RootTools.
I am trying to send terminal commands programmaticly from an android activity. At the moment I'm using something like the following:
Process process = null;
DataOutputStream os = null;
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes("./data/program1\n");
os.writeBytes("./data/program2\n");
os.writeBytes("exit\n");
os.flush();
However, my program1 is failing to run successfully and I believe it is due to inadequate user permissions. Now for my question:
Does anyone know how I can dump the terminal to a file and save it on the phone or sdcard? The program is tying into the terminal to feed it commands, I want to know a way to open a connection the otherway and access the (what is normally visual on a terminal screen) output.
See the sources for the Terminal application, as that is bidirectional.
Sadly, running shell commands or launching native processes is not an officially supported part of android, so no future stability is guaranteed.
I am trying to execute a command from within my code,
the command is "echo 125 > /sys/devices/platform/flashlight.0/leds/flashlight/brightness"
and I can run it without problems from adb shell
I am using Runtime class to execute it :
Runtime.getRuntime().exec("echo 125 > /sys/devices/platform/flashlight.0/leds/flashlight/brightness");
However I get a permissions error since I am not supposed to access the sys directory.
I have also tried to place the command in a String[] just in case spaces caused a problem but it didn't make much differense.
Does anyone know any workaround for this ?
The phone needs to be rooted, afterwards you can do something like:
public static void doCmds(List<String> cmds) throws Exception {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
}
os.writeBytes("exit\n");
os.flush();
os.close();
process.waitFor();
}
If you're just trying to set brightness, why don't you do so through the provided API (AKA, is there a reason you are trying to do it the way you are).
int brightness = 125;
Settings.System.putInt(
ftaContext.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS,
brightness);
Agreed you probably need to root the phone to write to system files. I'm surprised the brightness isn't exposed through the SDK.
For details on running shell commands from code, check out this project:
You also may remout /system with permissions to write..
I think the device will need to be "rooted" for this to work. Have a search on google and see how other developers have dont this, there is no shortage of flashlight apps.
The adb shell can behave as a superuser without rooted devices.
it's a debug bridge. you can do whatever you want through it.
BUT, when your calling Runtime.getRuntime().exec, you don't have the same premissions. some shell commands aren't even available from exec.
so not only you need a rooted device, you also need su premmisions.