[CLATJ#23] Close the file descriptor manually
ParcelFileDescriptor rely on garbage collection to close handler.
When there is any error during starting clat or quick on/off
IPv6 only network, the file descriptor may not be able to be closed
before next clatStart is called. This may be problematic. For
example, the same v4- tun interface has not closed yet and clatStart
has been called again.
Test: connect/disconnect to IPv6 only network and ping 8.8.8.8
repeat 10 times
Change-Id: I8e1c66206dc221827a039213ecc86d5cbd777dff
diff --git a/service/src/com/android/server/connectivity/ClatCoordinator.java b/service/src/com/android/server/connectivity/ClatCoordinator.java
index 9cdc086..7616457 100644
--- a/service/src/com/android/server/connectivity/ClatCoordinator.java
+++ b/service/src/com/android/server/connectivity/ClatCoordinator.java
@@ -257,6 +257,7 @@
try {
mNetd.interfaceSetEnableIPv6(tunIface, false /* enabled */);
} catch (RemoteException | ServiceSpecificException e) {
+ tunFd.close();
Log.e(TAG, "Disable IPv6 on " + tunIface + " failed: " + e);
}
@@ -273,6 +274,7 @@
try {
mNetd.interfaceSetMtu(tunIface, mtu);
} catch (RemoteException | ServiceSpecificException e) {
+ tunFd.close();
throw new IOException("Set MTU " + mtu + " on " + tunIface + " failed: " + e);
}
final InterfaceConfigurationParcel ifConfig = new InterfaceConfigurationParcel();
@@ -284,6 +286,7 @@
try {
mNetd.interfaceSetCfg(ifConfig);
} catch (RemoteException | ServiceSpecificException e) {
+ tunFd.close();
throw new IOException("Setting IPv4 address to " + ifConfig.ipv4Addr + "/"
+ ifConfig.prefixLength + " failed on " + ifConfig.ifName + ": " + e);
}
@@ -293,11 +296,12 @@
final ParcelFileDescriptor readSock6;
try {
// Use a JNI call to get native file descriptor instead of Os.socket() because we would
- // like to use ParcelFileDescriptor to close file descriptor automatically. But ctor
+ // like to use ParcelFileDescriptor to manage file descriptor. But ctor
// ParcelFileDescriptor(FileDescriptor fd) is a @hide function. Need to use native file
// descriptor to initialize ParcelFileDescriptor object instead.
readSock6 = mDeps.adoptFd(mDeps.openPacketSocket());
} catch (IOException e) {
+ tunFd.close();
throw new IOException("Open packet socket failed: " + e);
}
@@ -308,11 +312,16 @@
// reason why we use jniOpenPacketSocket6().
writeSock6 = mDeps.adoptFd(mDeps.openRawSocket6(fwmark));
} catch (IOException e) {
+ tunFd.close();
+ readSock6.close();
throw new IOException("Open raw socket failed: " + e);
}
final int ifaceIndex = mDeps.getInterfaceIndex(iface);
if (ifaceIndex == INVALID_IFINDEX) {
+ tunFd.close();
+ readSock6.close();
+ writeSock6.close();
throw new IOException("Fail to get interface index for interface " + iface);
}
@@ -320,6 +329,9 @@
try {
mDeps.addAnycastSetsockopt(writeSock6.getFileDescriptor(), v6, ifaceIndex);
} catch (IOException e) {
+ tunFd.close();
+ readSock6.close();
+ writeSock6.close();
throw new IOException("add anycast sockopt failed: " + e);
}
@@ -327,6 +339,9 @@
try {
mDeps.configurePacketSocket(readSock6.getFileDescriptor(), v6, ifaceIndex);
} catch (IOException e) {
+ tunFd.close();
+ readSock6.close();
+ writeSock6.close();
throw new IOException("configure packet socket failed: " + e);
}
@@ -340,6 +355,10 @@
mXlatLocalAddress6 = v6;
} catch (IOException e) {
throw new IOException("Error start clatd on " + iface + ": " + e);
+ } finally {
+ tunFd.close();
+ readSock6.close();
+ writeSock6.close();
}
return v6;