PrivilegedPort.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package de.tudarmstadt.informatik.hostage.system;
  2. import java.io.FileDescriptor;
  3. import android.net.LocalServerSocket;
  4. import android.net.LocalSocket;
  5. public class PrivilegedPort implements Runnable {
  6. /**
  7. * Path for UDS in abstract namespace.
  8. */
  9. private final static String NAME = "hostage";
  10. /**
  11. * Port to bind.
  12. */
  13. private int port;
  14. public PrivilegedPort(int port) {
  15. this.port = port;
  16. }
  17. /**
  18. * Run porthack in separate Thread.
  19. */
  20. @Override
  21. public void run() {
  22. Process p;
  23. try {
  24. String command = String.format("/data/local/p %d", port);
  25. p = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
  26. p.waitFor();
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. /**
  32. * Start porthack, wait for connection through UDS and receive a file
  33. * descriptor for bound port.
  34. */
  35. public FileDescriptor bindAndGetFD() {
  36. FileDescriptor fd = null;
  37. try {
  38. new Thread(this).start();
  39. LocalServerSocket lss = new LocalServerSocket(NAME);
  40. LocalSocket ls = lss.accept();
  41. while (ls.getInputStream().read() != -1)
  42. ;
  43. FileDescriptor[] fdarr;
  44. fdarr = ls.getAncillaryFileDescriptors();
  45. if (fdarr != null) {
  46. fd = fdarr[0];
  47. }
  48. ls.close();
  49. lss.close();
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. return fd;
  54. }
  55. }