Always close file descriptors

Every error thrown leaked a netlink NETFILTER socket.

Test: as follows
    - built, flashed, booted
    - runtest frameworks-net passes
    - enabled tethering and tried to cause conntrack updates to sessions
      that had expired (which causes ErrnoExceptions to be thrown)
      while monitoring lsof for system_server's NETFILTER sockets

Bug: 32163131
Bug: 64976634
Bug: 110122306

Merged-In: Id25f948b6b4d25ac033d9b608ab4a6b474f2c606
Merged-In: I212cbbe134bba4580a6e73de9bb77e712d25c9a6

Change-Id: Ib52b812e8434c27ad9f2596666400c13e03c2216
This commit is contained in:
Erik Kline
2018-07-04 17:57:51 +09:00
parent 3f4b3c946d
commit 3c0dc27a4d

View File

@@ -59,10 +59,9 @@ public class NetlinkSocket {
final String errPrefix = "Error in NetlinkSocket.sendOneShotKernelMessage";
final long IO_TIMEOUT = 300L;
FileDescriptor fd;
final FileDescriptor fd = forProto(nlProto);
try {
fd = forProto(nlProto);
connectToKernel(fd);
sendMessage(fd, msg, 0, msg.length, IO_TIMEOUT);
final ByteBuffer bytes = recvMessage(fd, DEFAULT_RECV_BUFSIZE, IO_TIMEOUT);
@@ -96,9 +95,9 @@ public class NetlinkSocket {
} catch (SocketException e) {
Log.e(TAG, errPrefix, e);
throw new ErrnoException(errPrefix, EIO, e);
} finally {
IoUtils.closeQuietly(fd);
}
IoUtils.closeQuietly(fd);
}
public static FileDescriptor forProto(int nlProto) throws ErrnoException {