package de.tudarmstadt.informatik.hostage.system; import java.io.FileDescriptor; import android.net.LocalServerSocket; import android.net.LocalSocket; public class PrivilegedPort implements Runnable { /** * Path for UDS in abstract namespace. */ private final static String NAME = "hostage"; /** * Port to bind. */ private int port; public PrivilegedPort(int port) { this.port = port; } /** * Run porthack in separate Thread. */ @Override public void run() { Process p; try { String command = String.format("/data/local/p %d", port); p = Runtime.getRuntime().exec(new String[] { "su", "-c", command }); p.waitFor(); } catch (Exception e) { e.printStackTrace(); } } /** * Start porthack, wait for connection through UDS and receive a file * descriptor for bound port. */ public FileDescriptor bindAndGetFD() { FileDescriptor fd = null; try { new Thread(this).start(); LocalServerSocket lss = new LocalServerSocket(NAME); LocalSocket ls = lss.accept(); while (ls.getInputStream().read() != -1) ; FileDescriptor[] fdarr; fdarr = ls.getAncillaryFileDescriptors(); if (fdarr != null) { fd = fdarr[0]; } ls.close(); lss.close(); } catch (Exception e) { e.printStackTrace(); } return fd; } }