3

私は次の方法を実行しようとしています:

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();
    }

次の入力を使用してこのメ​​ソッドを呼び出しました。

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();
                }

しかし、logcat では次のエラーが表示されます。

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)

問題は何ですか?

4

1 に答える 1

4

アプリではなく、suバイナリに問題があるようです。'adbshel​​l'からroot化されたシェルを正常に実行できるかどうかを確認します。「adbshel​​l」が最初からルート化されたシェルを提供する場合は、「su 1000」を実行してルート権限を失い、「su」を実行してルート化されたシェルに再度アクセスしてみてください。それが失敗した場合、suは機能していません。

ああ、関連する注意事項:UIスレッドをブロックしないように、おそらくハンドラーまたはAsyncTaskを介して別のスレッドでsuを実行するようにしてください。

于 2012-07-06T10:11:03.203 に答える