executing su command from android activity

android

Solution

It looks like there's an issue with the su binary, not with your app. Check if you can succesfully run a rooted shell from 'adb shell'. If 'adb shell' gives you a rooted shell from start, run 'su 1000' to lose root privileges and then run 'su' to try getting into a rooted shell again. If that fails, su isn't working.

Oh and on a related note: Be sure to run su in another Thread, maybe via a Handler or AsyncTask, so it won't block your UI thread.

Problem

i am trying to execute the following method: ``` public void runAsRoot(String[] cmds) throws Exception { Process p = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(p.getOutputStream()); InputStream is = p.getInputStream(); for (String tmpCmd : cmds) { os.writeBytes(tmpCmd+"\n"); int readed = 0; byte[] buff = new byte[4096]; // if cmd requires an output // due to the blocking behaviour of read(...) boolean cmdRequiresAnOutput = true; if (cmdRequiresAnOutput) { while( is.available() <= 0) { try { Thread.sleep(200); } catch(Exception ex) {} } while( is.available() > 0) { readed = is.read(buff); if ( readed <= 0 ) break; String seg = new String(buff,0,readed); Log.i("#>", seg); } } } os.writeBytes("exit\n"); os.flush(); } ``` i invoked this method using the following input: ``` String[] cmds = {"/system/bin/sendevent /dev/input/event0 1 107 0 \n", "sleep 1", "/system/bin/sendevent /dev/input/event0 1 107 1 \n"}; try { runAsRoot(cmds); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } ``` but in logcat i receiving following errors: ``` 07-06 15:19:27.007: E/su(6547): sudb - Opening database 07-06 15:19:27.007: E/(6547): Couldn't open database: unable to open database file 07-06 15:19:27.017: E/su(6547): sudb - Could not open database, prompt user 07-06 15:19:47.082: E/su(6547): select failed with 2: No such file or directory 07-06 15:19:47.082: W/su(6547): request rejected (10060->0 /system/bin/sh) ``` Any idea whats the issue?

Original source