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.
Related
I am attempting to add a Cloud Export/Import feature to my current Android Application.
I want to allow my users to export a particular Sqlite database table to Google Drive in csv format.
I realise I can perform a select statement and "manually" construct the csv file.
However I would like a approach that resembles running these sqlite3 commands
>sqlite3 c:/sqlite/chinook.db
sqlite> .headers on
sqlite> .mode csv
sqlite> .output data.csv
sqlite> SELECT customerid,
...> firstname,
...> lastname,
...> company
...> FROM customers;
sqlite> .quit
Ideally I would like to be able to directly export to Google sheets
Is it possible to package sqlite3 with my application and programmatically execute the above commands?
Is exporting possible, sqlite3 to csv?
Yeah, is it pretty simple operation, we can do this programatically through various method. As also answered here export sqlite into csv and here Exporting SQLite Database to csv file in android and also here How to implement Export sqlite To excel/csv file in android? and many answers and solutions are available on The StackOverFlow.
But how through sqlite3 commands?
If you really want to do it via commands, you can do it. But it is gonna be a really haptic way, because commands looks simple on terminal but they are really horrible programatically. You need super-user privilege or you need to install your application as a System application. If you really wanna do this, so lets start,
First lets us get the basics right. Android run Linux kernel underneath. Now if you have to run your process on it with super user privileges(run it as root) the only way is to execute your process is via command line because it is the only way you can directly interact with the kernel. Also you need to use su before running any command.
Process process = Runtime.getRuntime().exec("su");
will accomplish nearly nothing. It will just ask for super use privilege using dialog. What you can do is instead of just executing su you can execute your process with su as following
Process process = Runtime.getRuntime().exec(new String[] { "su", "-c", yourCommand});
So your commands goes like,
Process process = Runtime.getRuntime().exec(new String[] { "su", "-c", "sqlite3 /data/data/your-package-name/databases/file.db", ".headers on",".mode csv","...and all other operations"});
The -c Option
Among the most commonly used of su's few options is -c, which tells su to execute the command that directly follows it on the same line. Such command is executed as the new user, and then the terminal window or console from which su was run immediately returns to the account of the former user after the command has completed execution or after any program that it has launched has been closed.
Alternate Option
Alternative to above method one another way that might work is to use command line to copy you app to /system/app/ directory. Then your application will run automatically with root privileges(same as System apps)
Which way is preferable?
I personally like the first way, because working on shell commands in android application is not flexible. Now a days many providers blocks su commands on kernel level executing from third party applications.
Hope, this helps. Thanks!
I am developing an app for taking screenshots with root access.
I am using this call to take screenshot:
"/system/bin/screencap -p " + getFilesDir() + "screen.png"
However it creates this screenshot in root context and I can't access it with my app even if I chmod 777 and chown user_id:user_id. SELinux still says that this access is denied because scontext is u:r:untrusted_app:s0 while tcontext is u:object_r:app_data_file:s0. I have tried calling su with --context u:r:untrusted_app:s0 but it didn't help.
Any idea on how to perform correct screen capture call which will save it to app internal storage and then allow access for app?
If you have accessing the file because of permissions you can remove the -p <filename> part of the command. In this case the created image will be piped to stdout which is definitely accessible by your app.
You just have to connect to the InputStream of the Process executing the su comand and save the data yourself to a file or use it in your app.
How to read Stdout if shown here: Read command output inside su process
Note that in this example the asker wants to read text, hence the accepted solution reads data to a buffer and converts to a String - of course you don't need the conversion as the PNG file is not a printable String...
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 want to copy some files from it's own data folder(e.g. "/data/data/com.example.copy/") to "/data/local/tmp/". I can't access /data/local/tmp/ in my app. Is it possible to do it?
I don't have root access on my device.
Here's my code:
Process p=Runtime.getRuntime().exec("cat "+ this.getApplicationInfo().dataDir +"1.txt > /data/local/tmp/1.txt" );
p.waitFor();
No, you cannot do this from an application unless your device has something like a hacked su which lets you run a helper process as a more privileged user (ie, unless it is "rooted").
You should put the file somewhere else - such as the external storage. (If the adb shell is allowed to create directories under /data/local/tmp you might be able to create one there and chmod or chown it to give your app access, but that's non-portable across versions)
Or if you are merely trying to expose it, change the access permissions (someone will probably come along and point out the java constant for setting a file world readable is superficially deprecated, but actual disabling the capability would require a drastic change to the underlying operating system)
I'm writing an application that should modify some files it doesn't have access to: another application's database, default.prop, etc.
Currently, it is done with "su" request and temporary "chmod 0777" to required files. But I think that's not the best solution.
Is there any preferable way to do such operations?
And how, for example, I should check if shell commands (chown, chmod, grep, find) are supported by toolbox (if there's not busybox)?