diff options
| author | 2021-01-19 15:35:03 +0800 | |
|---|---|---|
| committer | 2021-01-21 08:40:12 +0000 | |
| commit | 92b94e1ec58ecb70548e06f4a146e9a8efa8abbd (patch) | |
| tree | ea0dad2060b843a35846416760edd0defff696ec | |
| parent | e8bf3e902b42d22efe9eb4fd7330895c10f95221 (diff) | |
Use ParcelFileDescriptor instead of FileDescriptor in the aidl
Using FileDescriptor in the aidl will refer to
Parcel.readRawFileDescriptor() and Parcel.writeRawFileDescriptor()
whilie trying to do parcel operations. Those two APIs are hidden
and not accessible for the incoming ConnectivityService mainline
module. For such use cases in a module, it should be replaced by
using ParcelFileDescriptor that is designed for such usages.
Bug: 170598012
Test: atest FrameworksNetTests CtsNetTestCasesLatestSdk
Change-Id: Ia7e3a71ccb4b136cc55c9e90a384870c32cfd37b
4 files changed, 12 insertions, 12 deletions
diff --git a/core/java/android/net/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl index 5e925b6a2bd8..47c7a1af029b 100644 --- a/core/java/android/net/IConnectivityManager.aidl +++ b/core/java/android/net/IConnectivityManager.aidl @@ -206,11 +206,11 @@ interface IConnectivityManager void startNattKeepalive(in Network network, int intervalSeconds, in ISocketKeepaliveCallback cb, String srcAddr, int srcPort, String dstAddr); - void startNattKeepaliveWithFd(in Network network, in FileDescriptor fd, int resourceId, + void startNattKeepaliveWithFd(in Network network, in ParcelFileDescriptor pfd, int resourceId, int intervalSeconds, in ISocketKeepaliveCallback cb, String srcAddr, String dstAddr); - void startTcpKeepalive(in Network network, in FileDescriptor fd, int intervalSeconds, + void startTcpKeepalive(in Network network, in ParcelFileDescriptor pfd, int intervalSeconds, in ISocketKeepaliveCallback cb); void stopKeepalive(in Network network, int slot); diff --git a/core/java/android/net/NattSocketKeepalive.java b/core/java/android/net/NattSocketKeepalive.java index b0ce0c71fbeb..a15d165e65e7 100644 --- a/core/java/android/net/NattSocketKeepalive.java +++ b/core/java/android/net/NattSocketKeepalive.java @@ -51,7 +51,7 @@ public final class NattSocketKeepalive extends SocketKeepalive { void startImpl(int intervalSec) { mExecutor.execute(() -> { try { - mService.startNattKeepaliveWithFd(mNetwork, mPfd.getFileDescriptor(), mResourceId, + mService.startNattKeepaliveWithFd(mNetwork, mPfd, mResourceId, intervalSec, mCallback, mSource.getHostAddress(), mDestination.getHostAddress()); } catch (RemoteException e) { diff --git a/core/java/android/net/TcpSocketKeepalive.java b/core/java/android/net/TcpSocketKeepalive.java index 436397ea7754..d89814d49bd0 100644 --- a/core/java/android/net/TcpSocketKeepalive.java +++ b/core/java/android/net/TcpSocketKeepalive.java @@ -21,7 +21,6 @@ import android.os.ParcelFileDescriptor; import android.os.RemoteException; import android.util.Log; -import java.io.FileDescriptor; import java.util.concurrent.Executor; /** @hide */ @@ -54,8 +53,7 @@ final class TcpSocketKeepalive extends SocketKeepalive { void startImpl(int intervalSec) { mExecutor.execute(() -> { try { - final FileDescriptor fd = mPfd.getFileDescriptor(); - mService.startTcpKeepalive(mNetwork, fd, intervalSec, mCallback); + mService.startTcpKeepalive(mNetwork, mPfd, intervalSec, mCallback); } catch (RemoteException e) { Log.e(TAG, "Error starting packet keepalive: ", e); throw e.rethrowFromSystemServer(); diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index 7541833b1569..807f972643a9 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -7912,10 +7912,11 @@ public class ConnectivityService extends IConnectivityManager.Stub } @Override - public void startNattKeepaliveWithFd(Network network, FileDescriptor fd, int resourceId, + public void startNattKeepaliveWithFd(Network network, ParcelFileDescriptor pfd, int resourceId, int intervalSeconds, ISocketKeepaliveCallback cb, String srcAddr, String dstAddr) { try { + final FileDescriptor fd = pfd.getFileDescriptor(); mKeepaliveTracker.startNattKeepalive( getNetworkAgentInfoForNetwork(network), fd, resourceId, intervalSeconds, cb, @@ -7923,24 +7924,25 @@ public class ConnectivityService extends IConnectivityManager.Stub } finally { // FileDescriptors coming from AIDL calls must be manually closed to prevent leaks. // startNattKeepalive calls Os.dup(fd) before returning, so we can close immediately. - if (fd != null && Binder.getCallingPid() != Process.myPid()) { - IoUtils.closeQuietly(fd); + if (pfd != null && Binder.getCallingPid() != Process.myPid()) { + IoUtils.closeQuietly(pfd); } } } @Override - public void startTcpKeepalive(Network network, FileDescriptor fd, int intervalSeconds, + public void startTcpKeepalive(Network network, ParcelFileDescriptor pfd, int intervalSeconds, ISocketKeepaliveCallback cb) { try { enforceKeepalivePermission(); + final FileDescriptor fd = pfd.getFileDescriptor(); mKeepaliveTracker.startTcpKeepalive( getNetworkAgentInfoForNetwork(network), fd, intervalSeconds, cb); } finally { // FileDescriptors coming from AIDL calls must be manually closed to prevent leaks. // startTcpKeepalive calls Os.dup(fd) before returning, so we can close immediately. - if (fd != null && Binder.getCallingPid() != Process.myPid()) { - IoUtils.closeQuietly(fd); + if (pfd != null && Binder.getCallingPid() != Process.myPid()) { + IoUtils.closeQuietly(pfd); } } } |