Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eefcdd8a authored by Linux Build Service Account's avatar Linux Build Service Account Committed by Gerrit - the friendly Code Review server
Browse files

Merge "Bluetooth: Avoid fd leak for Bluetooth server socket connections"

parents 964aa46a abaa19ef
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -197,6 +197,11 @@ public final class BluetoothSocket implements Closeable {
            throw new IOException("bt socket acept failed");
        }
        as.mSocket = new LocalSocket(fds[0]);
        try {
            as.mSocket.closeExternalFd();
        } catch (IOException e) {
            Log.e(TAG, "closeExternalFd failed");
        }
        as.mSocketIS = as.mSocket.getInputStream();
        as.mSocketOS = as.mSocket.getOutputStream();
        as.mAddress = RemoteAddr;
+13 −0
Original line number Diff line number Diff line
@@ -185,6 +185,19 @@ public class LocalSocket implements Closeable {
        return impl.getOutputStream();
    }

    /**
     * Set the flag to close the fd whcih was opened
     * externally
     *
     * @return none
     * @throws IOException if socket has been closed
     * @hide
     */
    public void closeExternalFd() throws IOException {
        implCreateIfNeeded();
        impl.closeExternalFd();
    }

    /**
     * Closes the socket.
     *
+19 −1
Original line number Diff line number Diff line
@@ -42,6 +42,8 @@ class LocalSocketImpl
    /** whether fd is created internally */
    private boolean mFdCreatedInternally;

    private boolean mFdCreatedExternally = false;

    // These fields are accessed by native code;
    /** file descriptor array received during a previous read */
    FileDescriptor[] inboundFileDescriptors;
@@ -263,7 +265,8 @@ class LocalSocketImpl
     */
    public void close() throws IOException {
        synchronized (LocalSocketImpl.this) {
            if ((fd == null) || (mFdCreatedInternally == false)) {
            if ((fd == null) || ((mFdCreatedInternally == false) &&
                                 (mFdCreatedExternally == false))) {
                fd = null;
                return;
            }
@@ -371,6 +374,21 @@ class LocalSocketImpl
        }
    }

    /**
     * Set the flag to close the fd which was opened
     * externally.
     *
     * @return none
     * @throws IOException if socket has been closed
     */
    protected void closeExternalFd() throws IOException
    {
        if (fd == null) {
            throw new IOException("socket not created");
        }
        mFdCreatedExternally = true;
    }

    /**
     * Returns the number of bytes available for reading without blocking.
     *