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

Commit a18f536d authored by Srinu Jella's avatar Srinu Jella Committed by Ricardo Cerqueira
Browse files

Bluetooth: Avoid fd leak for Bluetooth server socket connections

-As the close function of local socket implementation is dependent
 on mFdCreatedInternally, it will not be set for bluetooth server
 sockets, this will lead to fd leak on bluetooth socket connection.

-Exposed the API to set mFdCreatedExternally flag to close fd which
 was opened externally, so that when the actual close request comes
 fd will be closed successfully based on flag check.

CRs-Fixed: 638042
Change-Id: I22c6a973b30531407148f9d9e50820422d4427b0
parent eaa6696c
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -192,6 +192,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;
@@ -266,7 +268,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;
            }
@@ -374,6 +377,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.
     *