开发者

Root access in file explorer

I'm writing a file explorer which will be capable of modifying system files with root access, but I came across some problems.

What I'm doing now is to grant my app root access, but executing "su" doesn't work. If I set permissions to the folder in adb shell, the app works fine but I think root browsing doesn't rely on chmo开发者_如何学运维ds.

Can anyone tell me is there a proper way to make my app work as if it were with root privileges?


Running an android application process (its dalvik VM and native libraries) as root is extremely difficult to achieve, and inadvisable for a number of reasons including not just security but memory waste resulting from having to load private copies of system libraries instead of using the shared read-only copies available when you inherit an unprivileged process from zygote as in a normal application launch.

What the unofficial "su" hack on some rooted phones does is lets you launch a helper process which runs as root while your application process remains unprivileged. It does not change the userid of the application calling it - indeed, there really isn't by design any mechanism for doing that on unix-like operating systems.

Once you have a privileged helper process, you would then need to communicate with it via some means of interprocess communication such as its stdin/stdout or unix domain sockets to have it do file operations on your behalf. The shell present on the phone could probably even be used as the helper application - most of what a file manager needs to do can be implemented with the 'cat' command. Officially, none of this is a stable API, but then an application-accesable "su" hack isn't in official android anyway, so the whole project is deep in "unsupported" territory to begin with.


Root access creates a new Process, so, your app does not have root privileges.
The unique thing you can do with root privileges is execute commands, so, you have to know the commands of android, many of commands is based on Linux, like cp, ls and more.

Use this code for execute commands and get output:

/**
 * Execute command and get entire output
 * @return Command output
 */
private String executeCommand(String cmd) throws IOException, InterruptedException {
    Process process = Runtime.getRuntime().exec("su");
    InputStream in = process.getInputStream();
    OutputStream out = process.getOutputStream();
    out.write(cmd.getBytes());
    out.flush();
    out.close();
    byte[] buffer = new byte[1024];
    int length = buffer.read(buffer);
    String result = new String(buffer, 0, length);
    process.waitFor();
    return result;
}

/**
 * Execute command and an array separates each line
 * @return Command output separated by lines in a String array
 */
private String[] executeCmdByLines(String cmd) throws IOException, InterruptedException {
    Process process = Runtime.getRuntime().exec("su");
    InputStream in = process.getInputStream();
    OutputStream out = process.getOutputStream();
    out.write(cmd.getBytes());
    out.flush();
    out.close();
    byte[] buffer = new byte[1024];
    int length = buffer.read(buffer);
    String output = new String(buffer, 0, length);
    String[] result = output.split("\n");
    process.waitFor();
    return result;
}

Usages for a file explorer:

Get list of files:

for (String value : executeCmdByLines("ls /data/data")) {
    //Do your stuff here
}

Read text file:

String content = executeCommand("cat /data/someFile.txt");
//Do your stuff here with "content" string

Copy file (cp command not working on some devices):

executeCommand("cp /source/of/file /destination/file");

Delete file (rm command not working on some devices):

executeCommand("rm /path/to/file");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜