Search in sources :

Example 6 with LocalSocket

use of android.net.LocalSocket in project fqrouter by fqrouter.

the class SocksVpnService method listenFdServerSocket.

private void listenFdServerSocket(final FileDescriptor tunFD) throws Exception {
    final LocalServerSocket fdServerSocket = new LocalServerSocket("fdsock2");
    try {
        ExecutorService executorService = Executors.newFixedThreadPool(16);
        int count = 0;
        while (isRunning()) {
            try {
                final LocalSocket fdSocket = fdServerSocket.accept();
                executorService.submit(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            passFileDescriptor(fdSocket, tunFD);
                        } catch (Exception e) {
                            LogUtils.e("failed to handle fdsock", e);
                        }
                    }
                });
                count += 1;
                if (count % 200 == 0) {
                    garbageCollectFds();
                }
            } catch (Exception e) {
                LogUtils.e("failed to handle fdsock", e);
            }
        }
        executorService.shutdown();
    } finally {
        fdServerSocket.close();
    }
}
Also used : LocalServerSocket(android.net.LocalServerSocket) ExecutorService(java.util.concurrent.ExecutorService) LocalSocket(android.net.LocalSocket)

Example 7 with LocalSocket

use of android.net.LocalSocket in project XobotOS by xamarin.

the class Process method openZygoteSocketIfNeeded.

/**
     * Tries to open socket to Zygote process if not already open. If
     * already open, does nothing.  May block and retry.
     */
private static void openZygoteSocketIfNeeded() throws ZygoteStartFailedEx {
    int retryCount;
    if (sPreviousZygoteOpenFailed) {
        /*
             * If we've failed before, expect that we'll fail again and
             * don't pause for retries.
             */
        retryCount = 0;
    } else {
        retryCount = 10;
    }
    /*
         * See bug #811181: Sometimes runtime can make it up before zygote.
         * Really, we'd like to do something better to avoid this condition,
         * but for now just wait a bit...
         */
    for (int retry = 0; (sZygoteSocket == null) && (retry < (retryCount + 1)); retry++) {
        if (retry > 0) {
            try {
                Log.i("Zygote", "Zygote not up yet, sleeping...");
                Thread.sleep(ZYGOTE_RETRY_MILLIS);
            } catch (InterruptedException ex) {
            // should never happen
            }
        }
        try {
            sZygoteSocket = new LocalSocket();
            sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET, LocalSocketAddress.Namespace.RESERVED));
            sZygoteInputStream = new DataInputStream(sZygoteSocket.getInputStream());
            sZygoteWriter = new BufferedWriter(new OutputStreamWriter(sZygoteSocket.getOutputStream()), 256);
            Log.i("Zygote", "Process: zygote socket opened");
            sPreviousZygoteOpenFailed = false;
            break;
        } catch (IOException ex) {
            if (sZygoteSocket != null) {
                try {
                    sZygoteSocket.close();
                } catch (IOException ex2) {
                    Log.e(LOG_TAG, "I/O exception on close after exception", ex2);
                }
            }
            sZygoteSocket = null;
        }
    }
    if (sZygoteSocket == null) {
        sPreviousZygoteOpenFailed = true;
        throw new ZygoteStartFailedEx("connect failed");
    }
}
Also used : LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BufferedWriter(java.io.BufferedWriter)

Example 8 with LocalSocket

use of android.net.LocalSocket in project android_frameworks_base by DirtyUnicorns.

the class BluetoothSocket method bindListen.

/**
     * Currently returns unix errno instead of throwing IOException,
     * so that BluetoothAdapter can check the error code for EADDRINUSE
     */
/*package*/
int bindListen() {
    int ret;
    if (mSocketState == SocketState.CLOSED)
        return EBADFD;
    IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null);
    if (bluetoothProxy == null) {
        Log.e(TAG, "bindListen fail, reason: bluetooth is off");
        return -1;
    }
    try {
        mPfd = bluetoothProxy.createSocketChannel(mType, mServiceName, mUuid, mPort, getSecurityFlags());
    } catch (RemoteException e) {
        Log.e(TAG, Log.getStackTraceString(new Throwable()));
        return -1;
    }
    // read out port number
    try {
        synchronized (this) {
            if (DBG)
                Log.d(TAG, "bindListen(), SocketState: " + mSocketState + ", mPfd: " + mPfd);
            if (mSocketState != SocketState.INIT)
                return EBADFD;
            if (mPfd == null)
                return -1;
            FileDescriptor fd = mPfd.getFileDescriptor();
            if (DBG)
                Log.d(TAG, "bindListen(), new LocalSocket ");
            mSocket = new LocalSocket(fd);
            if (DBG)
                Log.d(TAG, "bindListen(), new LocalSocket.getInputStream() ");
            mSocketIS = mSocket.getInputStream();
            mSocketOS = mSocket.getOutputStream();
        }
        if (DBG)
            Log.d(TAG, "bindListen(), readInt mSocketIS: " + mSocketIS);
        int channel = readInt(mSocketIS);
        synchronized (this) {
            if (mSocketState == SocketState.INIT)
                mSocketState = SocketState.LISTENING;
        }
        if (DBG)
            Log.d(TAG, "channel: " + channel);
        if (mPort <= -1) {
            mPort = channel;
        }
        // else ASSERT(mPort == channel)
        ret = 0;
    } catch (IOException e) {
        if (mPfd != null) {
            try {
                mPfd.close();
            } catch (IOException e1) {
                Log.e(TAG, "bindListen, close mPfd: " + e1);
            }
            mPfd = null;
        }
        Log.e(TAG, "bindListen, fail to get port number, exception: " + e);
        return -1;
    }
    return ret;
}
Also used : LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) RemoteException(android.os.RemoteException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 9 with LocalSocket

use of android.net.LocalSocket in project android_frameworks_base by DirtyUnicorns.

the class InstallerConnection method connect.

private boolean connect() {
    if (mSocket != null) {
        return true;
    }
    Slog.i(TAG, "connecting...");
    try {
        mSocket = new LocalSocket();
        LocalSocketAddress address = new LocalSocketAddress("installd", LocalSocketAddress.Namespace.RESERVED);
        mSocket.connect(address);
        mIn = mSocket.getInputStream();
        mOut = mSocket.getOutputStream();
    } catch (IOException ex) {
        disconnect();
        return false;
    }
    return true;
}
Also used : LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException)

Example 10 with LocalSocket

use of android.net.LocalSocket in project android_frameworks_base by AOSPA.

the class LocalSocketTest method testBasic.

@SmallTest
public void testBasic() throws Exception {
    LocalServerSocket ss;
    LocalSocket ls;
    LocalSocket ls1;
    ss = new LocalServerSocket("android.net.LocalSocketTest");
    ls = new LocalSocket();
    ls.connect(new LocalSocketAddress("android.net.LocalSocketTest"));
    ls1 = ss.accept();
    // Test trivial read and write
    ls.getOutputStream().write(42);
    assertEquals(42, ls1.getInputStream().read());
    // Test getting credentials
    Credentials c = ls1.getPeerCredentials();
    MoreAsserts.assertNotEqual(0, c.getPid());
    // Test sending and receiving file descriptors
    ls.setFileDescriptorsForSend(new FileDescriptor[] { FileDescriptor.in });
    ls.getOutputStream().write(42);
    assertEquals(42, ls1.getInputStream().read());
    FileDescriptor[] out = ls1.getAncillaryFileDescriptors();
    assertEquals(1, out.length);
    // Test multible byte write and available()
    ls1.getOutputStream().write(new byte[] { 0, 1, 2, 3, 4, 5 }, 1, 5);
    assertEquals(1, ls.getInputStream().read());
    assertEquals(4, ls.getInputStream().available());
    byte[] buffer = new byte[16];
    int countRead;
    countRead = ls.getInputStream().read(buffer, 1, 15);
    assertEquals(4, countRead);
    assertEquals(2, buffer[1]);
    assertEquals(3, buffer[2]);
    assertEquals(4, buffer[3]);
    assertEquals(5, buffer[4]);
    // Try various array-out-of-bound cases
    try {
        ls.getInputStream().read(buffer, 1, 16);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, 1, 16);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, -1, 15);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getOutputStream().write(buffer, 0, -1);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getInputStream().read(buffer, -1, 15);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    try {
        ls.getInputStream().read(buffer, 0, -1);
        fail("expected exception");
    } catch (ArrayIndexOutOfBoundsException ex) {
    // excpected
    }
    // Try read of length 0
    ls.getOutputStream().write(42);
    countRead = ls1.getInputStream().read(buffer, 0, 0);
    assertEquals(0, countRead);
    assertEquals(42, ls1.getInputStream().read());
    ss.close();
    ls.close();
    try {
        ls.getOutputStream().write(42);
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    try {
        ls.getInputStream().read();
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    try {
        ls1.getOutputStream().write(42);
        fail("expected exception");
    } catch (IOException ex) {
    // Expected
    }
    // Try read on socket whose peer has closed
    assertEquals(-1, ls1.getInputStream().read());
    ls1.close();
}
Also used : LocalServerSocket(android.net.LocalServerSocket) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) Credentials(android.net.Credentials) FileDescriptor(java.io.FileDescriptor) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

LocalSocket (android.net.LocalSocket)51 IOException (java.io.IOException)44 LocalSocketAddress (android.net.LocalSocketAddress)32 FileDescriptor (java.io.FileDescriptor)26 ParcelFileDescriptor (android.os.ParcelFileDescriptor)17 LocalServerSocket (android.net.LocalServerSocket)14 SmallTest (android.test.suitebuilder.annotation.SmallTest)11 RemoteException (android.os.RemoteException)10 InputStream (java.io.InputStream)8 Credentials (android.net.Credentials)6 Message (android.os.Message)6 SocketException (java.net.SocketException)6 BufferedWriter (java.io.BufferedWriter)2 DataInputStream (java.io.DataInputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 SuppressLint (android.annotation.SuppressLint)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1