Search in sources :

Example 26 with LocalSocketAddress

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

the class ProcessList method openLmkdSocket.

private static boolean openLmkdSocket() {
    try {
        sLmkdSocket = new LocalSocket(LocalSocket.SOCKET_SEQPACKET);
        sLmkdSocket.connect(new LocalSocketAddress("lmkd", LocalSocketAddress.Namespace.RESERVED));
        sLmkdOutputStream = sLmkdSocket.getOutputStream();
    } catch (IOException ex) {
        Slog.w(TAG, "lowmemorykiller daemon socket open failed");
        sLmkdSocket = null;
        return false;
    }
    return true;
}
Also used : LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException)

Example 27 with LocalSocketAddress

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

the class NativeDaemonConnector method listenToSocket.

private void listenToSocket() throws IOException {
    LocalSocket socket = null;
    try {
        socket = new LocalSocket();
        LocalSocketAddress address = determineSocketAddress();
        socket.connect(address);
        InputStream inputStream = socket.getInputStream();
        synchronized (mDaemonLock) {
            mOutputStream = socket.getOutputStream();
        }
        mCallbacks.onDaemonConnected();
        FileDescriptor[] fdList = null;
        byte[] buffer = new byte[BUFFER_SIZE];
        int start = 0;
        while (true) {
            int count = inputStream.read(buffer, start, BUFFER_SIZE - start);
            if (count < 0) {
                loge("got " + count + " reading with start = " + start);
                break;
            }
            fdList = socket.getAncillaryFileDescriptors();
            // Add our starting point to the count and reset the start.
            count += start;
            start = 0;
            for (int i = 0; i < count; i++) {
                if (buffer[i] == 0) {
                    // Note - do not log this raw message since it may contain
                    // sensitive data
                    final String rawEvent = new String(buffer, start, i - start, StandardCharsets.UTF_8);
                    boolean releaseWl = false;
                    try {
                        final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent(rawEvent, fdList);
                        log("RCV <- {" + event + "}");
                        if (event.isClassUnsolicited()) {
                            // TODO: migrate to sending NativeDaemonEvent instances
                            if (mCallbacks.onCheckHoldWakeLock(event.getCode()) && mWakeLock != null) {
                                mWakeLock.acquire();
                                releaseWl = true;
                            }
                            Message msg = mCallbackHandler.obtainMessage(event.getCode(), uptimeMillisInt(), 0, event.getRawEvent());
                            if (mCallbackHandler.sendMessage(msg)) {
                                releaseWl = false;
                            }
                        } else {
                            mResponseQueue.add(event.getCmdNumber(), event);
                        }
                    } catch (IllegalArgumentException e) {
                        log("Problem parsing message " + e);
                    } finally {
                        if (releaseWl) {
                            mWakeLock.release();
                        }
                    }
                    start = i + 1;
                }
            }
            if (start == 0) {
                log("RCV incomplete");
            }
            // buffer and read again.
            if (start != count) {
                final int remaining = BUFFER_SIZE - start;
                System.arraycopy(buffer, start, buffer, 0, remaining);
                start = remaining;
            } else {
                start = 0;
            }
        }
    } catch (IOException ex) {
        loge("Communications error: " + ex);
        throw ex;
    } finally {
        synchronized (mDaemonLock) {
            if (mOutputStream != null) {
                try {
                    loge("closing stream for " + mSocket);
                    mOutputStream.close();
                } catch (IOException e) {
                    loge("Failed closing output stream: " + e);
                }
                mOutputStream = null;
            }
        }
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException ex) {
            loge("Failed closing socket: " + ex);
        }
    }
}
Also used : Message(android.os.Message) InputStream(java.io.InputStream) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor)

Example 28 with LocalSocketAddress

use of android.net.LocalSocketAddress in project android_frameworks_base by ResurrectionRemix.

the class ProcessList method openLmkdSocket.

private static boolean openLmkdSocket() {
    try {
        sLmkdSocket = new LocalSocket(LocalSocket.SOCKET_SEQPACKET);
        sLmkdSocket.connect(new LocalSocketAddress("lmkd", LocalSocketAddress.Namespace.RESERVED));
        sLmkdOutputStream = sLmkdSocket.getOutputStream();
    } catch (IOException ex) {
        Slog.w(TAG, "lowmemorykiller daemon socket open failed");
        sLmkdSocket = null;
        return false;
    }
    return true;
}
Also used : LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException)

Example 29 with LocalSocketAddress

use of android.net.LocalSocketAddress in project android_frameworks_base by crdroidandroid.

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 30 with LocalSocketAddress

use of android.net.LocalSocketAddress in project android_frameworks_base by crdroidandroid.

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)32 LocalSocketAddress (android.net.LocalSocketAddress)32 IOException (java.io.IOException)27 LocalServerSocket (android.net.LocalServerSocket)12 SmallTest (android.test.suitebuilder.annotation.SmallTest)11 FileDescriptor (java.io.FileDescriptor)11 InputStream (java.io.InputStream)7 Credentials (android.net.Credentials)6 Message (android.os.Message)6 SocketException (java.net.SocketException)5 BufferedWriter (java.io.BufferedWriter)2 DataInputStream (java.io.DataInputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 SuppressLint (android.annotation.SuppressLint)1 ParcelFileDescriptor (android.os.ParcelFileDescriptor)1 Method (java.lang.reflect.Method)1 Random (java.util.Random)1