开发者

Getting result of executed shell command fails/hangs

开发者 https://www.devze.com 2023-01-16 10:03 出处:网络
Itry to execute shell commands, this does work as it should. Even the result comes back (as is see on LogCat). The problem ist the last line of the result. Every time a readLine() on the last line occ

I try to execute shell commands, this does work as it should. Even the result comes back (as is see on LogCat). The problem ist the last line of the result. Every time a readLine() on the last line occurs (which shouldn't occur, temp should be null), the app hangs forever and doesn't come back from the readLine call. Maybe you find the error. I tried readUTF and standart read(), all the same problem. And yes, the app got su-rights.

try
                {
                    Process process = Runtime.getRuntime().exec("su");
                    DataOutputStream os = new DataOutputStream(process.getOutputStream());
                    DataInputStream osRes = new DataInputStream(process.getInputStream());
                    for (String single : commands) {
                       os.writeBytes(single + "\n");
                       os.flush();
                       String temp = new String();
                       while(  (temp = osRes.readLine()) != null)
                       {                                      
                           Log.v("NITRO", temp);
                           result2 += temp + "\n";                            
                       }                      
                    }
                    os.writeBytes("exit\n");
                    os.flush();
                    process.waitFor();
                } catch (IOException e)
                {
                    Toast.makeText(Main.this, "Error", Toast.LENGTH_LONG);
                } catch 开发者_StackOverflow中文版(InterruptedException e)
                {
                    Toast.makeText(Main.this, "Error", Toast.LENGTH_LONG);
                }

That is the StackTrace where it hangs when i stop debugger when hanging:

OSFileSystem.readImpl(int, byte[], int, int) line: not available [native method]    
OSFileSystem.read(int, byte[], int, int) line: 118  
ProcessManager$ProcessInputStream(FileInputStream).read(byte[], int, int) line: 312 
ProcessManager$ProcessInputStream(FileInputStream).read() line: 250 
DataInputStream.readLine() line: 309    
Main$2$1.run() line: 84 
ViewRoot(Handler).handleCallback(Message) line: 587 
ViewRoot(Handler).dispatchMessage(Message) line: 92 
Looper.loop() line: 123 
ActivityThread.main(String[]) line: 4627    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 521  
ZygoteInit$MethodAndArgsCaller.run() line: 868  
ZygoteInit.main(String[]) line: 626 
NativeStart.main(String[]) line: not available [native method]  


Try moving the exit before the while loop...You're application never terminates, hence the while loop never ends....Moving the exit should cause the application to terminate which should cause the loop to get a null...

0

精彩评论

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

关注公众号