required root permissions to an android application - android

I am writing an application to access the many of the system device nodes. To open the device nodes, I wrote native methods, When I am trying to execute it, I am unable to open the device node as there is no root permissions to my application. Could any one please tell give root permission to my android application. device details: android 2.0.1 - motorola milestone.
rtc_fd=open("/dev/rtc",0777);
if(rtc_fd == -1) {
__android_log_write(ANDROID_LOG_ERROR, "","UNABLE TO OPEN THE DEVICE....");
strcpy(result_string,"Fail: /dev/rtc open error\n");
__android_log_write(ANDROID_LOG_ERROR, ""," DEVICE...ERROR. ");
return result_string;
}
ret = ioctl(rtc_fd, RTC_RD_TIME, &rtc_tm);
if (ret == -1) {
strcpy(result_string,"Fail: rtc ioctl RTC_RD_TIME error\r\n");
return result_string;
}
It is always saying UNABLE TO OPEN DEVICE, could any one please suggest a solution to open a device node.

First and foremost, you will obviously need a rooted phone for any of this to work.
That being said, Android does not allow for a user-application to gain super-user rights, even on a rooted phone. Instead, it allows you to launch a new process with super-user rights.
The easiest method for running things as super-user is to create a virtual terminal, as follows:
Process proc = Runtime.getRuntime().exec("su");
DataOutputStream standard_in = new DataOutputStream(proc.getOutputStream());
DataInputStream standard_out = new DataInputStream(proc.getInputStream());
Using the input and output streams you now effectively have console access as root, which you can use to run typical command-line commands, or to run the process that accesses your device for you.

Related

Complete terminal control on android

I have been trying to run a few Linux commands on my android phone with
Process process = Runtime.getRuntime().exec(COMMAND);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
And noticed that I can only run specific commands and get the proper output even if the device is rooted with SuperSU (I have also tested it with a device without SuperSU on it).
For example, if I run ls and try to put it on a screen (through a TextView) as follows:
public void onBtnClick(View view) {
try {
EditText commandLine = findViewById(R.id.commandText);
Process process = Runtime.getRuntime().exec(commandLine.getText().toString());
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
((TextView) findViewById(R.id.mainTextView)).setText(((TextView) findViewById(R.id.mainTextView)).getText() + "\n" + in.readLine());
commandLine.setText("");
} catch (IOException e) {
e.printStackTrace();
}
}
the output is acct which makes sense.
But on the other hand, if lets say I run pwd it gives me the following error:
W/System.err: java.io.IOException: Error running exec(). Command:
[pwd] Working Directory: null Environment: null
I did some research online and stumbled upon Termux that can have complete control over the phone through a terminal which is exactly what I'm looking to make (for my own learning and testing purposes).
And although it's just an emulator it can do exactly what I want but the only problem is that it requires I input the commands through the terminal.
What I'm here for is to sort of replicate what Termux does for myself so that I could run Linux commands properly from the Java code of the application, not requiring the user to actually input commands.
Can anyone help me with where to start and the basics of running those commands properly on my device?
Termux states that it doesn't work as a traditional Linux bash does since it sets its own virtual-ish environment when first setting up in its data directory in /data/data/com.termux/files/usr see here and here
Also, according to the official android docs, the exec(command) method,
Executes the specified string command in a separate process.
This is a convenience method. An invocation of the form exec(command)
behaves in exactly the same way as the invocation exec(command, null,
null).
So if it's a separate process, when executing a command, it will run it inside that process' directory (Each process in Linux gets its directory and is assigned an PID which os uses). So what the ls command gives you is simply whatever's inside that process' directory. You should be somewhere like /proc/31415/ and there's only a acct file (cgroup in regular Linux).
What you should be doing is running the command inside a directory by declaring it when invoking getRuntime().exec() see the link above to find the right one you'll need. I'd suggest using the override which handles all the parameters.
You'll need something like this:
String[] cmd = {"mkdir", "testDir"};
File env = new File(getFilesDir().getAbsolutePath());
Runtime.getRuntime().exec(cmd, null, env);
Also, it doesn't hurt to take a look at Termux's installer code (exec(). It'll give you a good overlook to setting up your environment as well as working with basic commands.
Also, I think you've done it already but double check to make sure that you're requesting WRITE_EXTERNAL_STORAGE permission for your application.

How to check if custom android is rooted?

We've bought some custom android tablets, that we're going to use as kiosk tablets. For our kiosk app to work properly the app needs root access.
How can I check if the device is rooted properly?
And how can I root it if it isn't?
Things that point to that the device IS rooted:
When I ADB shell into the tablet the line starts with "#" (I read somewhere that this means that the tablet is rooted).
When I run "su" in the shell nothing happens (no errors).
This file exist /system/xbin/su"
Things that point to that the device IS NOT rooted:
I have installed Root Checker, it says "Sorry! This device does not
have root access"
When I run the following code in our app, which I know for a fact is working on
other rooted devices:
Process process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
I get an error: "java.io.IOException: write failed: EPIPE (Broken pipe)".
Had to try a number of different root tools. Turns out KingRoot (https://kingroot.net/) did the job. Afterwards Root Checker says that it is rooted correctly and my app works again.
There is this nice guide which you can follow to check if the phone is rooted or not.
Link to website

How to run root commands from Android application?

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"};

android emulator root access

I need to execute a C program in my App by simply adding the executable to the android project and building the .apk. Then I try to execute the program in my application like this:
Process result = Runtime.getRuntime().exec("su");
String cmd = "PROGRAM_NAME";
DataOutputStream dos = new DataOutputStream(result.getOutputStream());
DataInputStreamdis = new DataInputStream(result.getInputStream());
dos.writeBytes(cmd + "\n");
dos.writeBytes("exit\n");
dos.flush();
I know I need root access to do this so I installed Superuser.apk but that didn't work. Is there another possible way to do this? Btw the code is not fully extended it should just give a look at the way the program should be executed
I'm running the emulator with Android 4.2.1
Edit:
Checking root permission first with
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
if (null != os && null != osRes) {
os.writeBytes("id\n");
os.flush();
String currUid = osRes.readLine();
boolean exitSu = false;
if (null == currUid) {
Log.d("ROOT", "Can't get root access or denied by user");
}
I have got the same problem as you. you can find many answers like here , but they are too old and they are not working anymore (in the new sdk).
I found the best answer here which says that the Security Tips of Android are not allowing to any developer to have root access :
A central design point of the Android security architecture is that no application, by default,
has permission to perform any operations that would adversely impact other applications, the
operating system, or the user. This includes reading or writing the user's private data (such as
contacts or e-mails), reading or writing another application's files, performing network access,
keeping the device awake, etc.
So the only access that you have under the Application Layer is by permissions.

Android Terminal and Log Dumping

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.

Categories

Resources