java - ANDROID: How to gain root access in an Android application? -
i'm developing first android
application, , i'm curious if there "standard" ways executing privileged shell
commands. i've been able find 1 way it, executing su
, , appending commands stdin
of su
process.
dataoutputstream pout = new dataoutputstream(p.getoutputstream()); datainputstream pin = new datainputstream(p.getinputstream()); string rv = ""; // su must exit before output can read pout.writebytes(cmd + "\nexit\n"); pout.flush(); p.waitfor(); while (pin.available() > 0) rv += pin.readline() + "\n";
i've read wrapping privileged (superuser
) calls in jni
: possible? if so, how 1 go accomplishing it? other that, there other ways of calling privileged instructions java
?
as far know, can run command-line commands using root privileges. can use generic class made wraps root access in code: http://muzikant-android.blogspot.com/2011/02/how-to-get-root-access-and-execute.html
all need extend class , override getcommandstoexecute
method return commands want execute root.
public abstract class executeasrootbase { public static boolean canrunrootcommands() { boolean retval = false; process suprocess; try { suprocess = runtime.getruntime().exec("su"); dataoutputstream os = new dataoutputstream(suprocess.getoutputstream()); datainputstream osres = new datainputstream(suprocess.getinputstream()); if (null != os && null != osres) { // getting id of current user check if root os.writebytes("id\n"); os.flush(); string curruid = osres.readline(); boolean exitsu = false; if (null == curruid) { retval = false; exitsu = false; log.d("root", "can't root access or denied user"); } else if (true == curruid.contains("uid=0")) { retval = true; exitsu = true; log.d("root", "root access granted"); } else { retval = false; exitsu = true; log.d("root", "root access rejected: " + curruid); } if (exitsu) { os.writebytes("exit\n"); os.flush(); } } } catch (exception e) { // can't root ! // broken pipe exception on trying write output stream (os) after su failed, meaning device not rooted retval = false; log.d("root", "root access rejected [" + e.getclass().getname() + "] : " + e.getmessage()); } return retval; } public final boolean execute() { boolean retval = false; try { arraylist<string> commands = getcommandstoexecute(); if (null != commands && commands.size() > 0) { process suprocess = runtime.getruntime().exec("su"); dataoutputstream os = new dataoutputstream(suprocess.getoutputstream()); // execute commands require root access (string currcommand : commands) { os.writebytes(currcommand + "\n"); os.flush(); } os.writebytes("exit\n"); os.flush(); try { int suprocessretval = suprocess.waitfor(); if (255 != suprocessretval) { // root access granted retval = true; } else { // root access denied retval = false; } } catch (exception ex) { log.e("root", "error executing root action", ex); } } } catch (ioexception ex) { log.w("root", "can't root access", ex); } catch (securityexception ex) { log.w("root", "can't root access", ex); } catch (exception ex) { log.w("root", "error executing internal operation", ex); } return retval; } protected abstract arraylist<string> getcommandstoexecute(); }
Comments
Post a Comment