Guest User

Untitled

a guest
Jul 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. package ch.unibas.cn.ana.service;
  2.  
  3. import java.io.FileDescriptor;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.LineNumberReader;
  9. import java.io.PrintStream;
  10. import java.lang.reflect.InvocationTargetException;
  11. import java.lang.reflect.Method;
  12. import java.util.ArrayList;
  13. import java.util.Map.Entry;
  14.  
  15. import ch.unibas.cn.ana.Constants;
  16.  
  17. import android.util.Log;
  18.  
  19. public class ShellExecutor {
  20. protected static final String TAG = "ShellExecutor";
  21. protected static int mShellPid;
  22. protected static FileDescriptor mShellFd;
  23. protected static FileOutputStream mShellOut;
  24. protected static FileInputStream mShellIn;
  25. protected static PrintStream mOut;
  26. protected static LineNumberReader mIn;
  27. protected static Environment mEnvironment;
  28.  
  29. private static Method createSubprocess;
  30. private static Method waitFor;
  31.  
  32. private static boolean firstTime = true;
  33. private static ShellExecutor instance = new ShellExecutor();
  34.  
  35.  
  36. private ShellExecutor() {}
  37.  
  38. public static synchronized ShellExecutor getInstance(){
  39. if(firstTime){
  40. instance.initShell();
  41. firstTime = false;
  42. }
  43. return instance;
  44. }
  45.  
  46. @Override
  47. protected void finalize() throws Throwable {
  48. executeCmd("exit", null);
  49. try {
  50. waitFor.invoke(null, mShellPid);
  51. } catch (IllegalArgumentException e) {
  52. Log.e(TAG,e.toString());
  53. } catch (IllegalAccessException e) {
  54. Log.e(TAG,e.toString());
  55. } catch (InvocationTargetException e) {
  56. Log.e(TAG,e.toString());
  57. }
  58.  
  59. try {
  60. mIn.close();
  61. mOut.close();
  62. mShellIn.close();
  63. mShellOut.close();
  64.  
  65. } catch (IOException e) {
  66. Log.e(TAG,e.toString());
  67. }
  68. super.finalize();
  69. }
  70.  
  71. private void initShell(){
  72. // android.os.Exec is not included in android.jar so we need to use reflection.
  73. createSubprocess = getMethodFromClass("android.os.Exec", "createSubprocess",
  74. String.class, String.class, String.class, int[].class);
  75. waitFor = getMethodFromClass("android.os.Exec", "waitFor", int.class);
  76.  
  77. int[] pid = new int[1];
  78. try {
  79. mShellFd = (FileDescriptor)createSubprocess.invoke(null, Constants.SHELL, "-" , null, pid);
  80. mShellPid = pid[0];
  81.  
  82. mShellOut = new FileOutputStream(mShellFd);
  83. mShellIn = new FileInputStream(mShellFd);
  84.  
  85. mOut = new PrintStream(mShellOut, true /* autoflush */);
  86. mIn = new LineNumberReader(new InputStreamReader(mShellIn));
  87.  
  88. // Wait until the shell has produced some output before we start writing to it. This prevents
  89. // misplaced $ prompts in the output.
  90. while (!mIn.ready()) {
  91. Thread.sleep(1);
  92. }
  93.  
  94.  
  95. exportEnvironment();
  96.  
  97. } catch (IllegalArgumentException e) {
  98. Log.e(TAG,e.toString());
  99. } catch (IllegalAccessException e) {
  100. Log.e(TAG,e.toString());
  101. } catch (InvocationTargetException e) {
  102. Log.e(TAG,e.toString());
  103. } catch (IOException e) {
  104. Log.e(TAG,e.toString());
  105. } catch (InterruptedException e) {
  106. Log.e(TAG,e.toString());
  107. }
  108.  
  109. }
  110. public boolean executeCmd(String cmd, ArrayList<String> result){
  111. mOut.println(cmd + "; echo sentinel");
  112. boolean flag = true;
  113. String line;
  114.  
  115. try {
  116. //Thread.sleep(100); /* time until the command should be executed, and we may send a sentinel */
  117. //mOut.println("echo sentinel > /dev/null");
  118. do {
  119. line = mIn.readLine();
  120. if(line.equals("sentinel")){
  121. flag = false;
  122. }else{
  123. if(result != null){
  124. if(lineIsResult(line, cmd)){
  125. Log.i(TAG, "Line: "+ line);
  126. result.add(line);
  127. }
  128. }
  129. }
  130. } while (flag);
  131.  
  132. mIn.setLineNumber(mIn.getLineNumber());
  133. return true;
  134.  
  135. } catch (IOException e) {
  136. Log.e(TAG,e.toString());
  137. }
  138. return false;
  139.  
  140. }
  141.  
  142. private boolean lineIsResult(String line, String cmd){
  143. if(!line.contains(cmd)){
  144. if(!line.equals("\n") || !line.equals("\r\n")){
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150.  
  151. private void exportEnvironment() {
  152. for (Entry<String, String> e : mEnvironment.getEnv().entrySet()) {
  153. executeCmd(String.format("export %s=\"%s\"", e.getKey(), e.getValue()), null);
  154. }
  155.  
  156. }
  157. private Method getMethodFromClass(final String _class, final String method, final Class<?>... paramTypes){
  158. Class<?> iclass;
  159. try {
  160. iclass = Class.forName(_class);
  161. return iclass.getMethod(method, paramTypes);
  162.  
  163. } catch (ClassNotFoundException e) {
  164. Log.e(TAG,e.toString());
  165. } catch (SecurityException e) {
  166. Log.e(TAG,e.toString());
  167. } catch (NoSuchMethodException e) {
  168. Log.e(TAG,e.toString());
  169. }
  170. return null;
  171. }
  172. }
Add Comment
Please, Sign In to add comment