From 53f0193fca8c0c5a7cd50ba737625a1b95b2eced Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Wed, 18 Mar 2020 18:29:30 +0900 Subject: [PATCH 1/2] Remove NETWORK_STACK_SERVICE constant Instead, have a dedicated method in android.net.NetworkStack allowing to fetch the stable AIDL token for the service. This avoids returning IBinder from getSystemService, as getSystemService should generally return manager classes. Test: atest FrameworksNetTests NetworkStackTests Fixes: 151243982 Change-Id: I58a6e1f27aff052050197d1901f43a98d7aa1167 --- api/system-current.txt | 2 +- api/test-current.txt | 2 +- .../java/android/app/SystemServiceRegistry.java | 8 -------- core/java/android/content/Context.java | 2 -- core/java/android/net/NetworkStack.java | 17 +++++++++++++++-- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/api/system-current.txt b/api/system-current.txt index c102e661d400e..1363b91719768 100755 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -1811,7 +1811,6 @@ package android.content { field public static final String NETD_SERVICE = "netd"; field public static final String NETWORK_POLICY_SERVICE = "netpolicy"; field public static final String NETWORK_SCORE_SERVICE = "network_score"; - field public static final String NETWORK_STACK_SERVICE = "network_stack"; field public static final String OEM_LOCK_SERVICE = "oem_lock"; field public static final String PERMISSION_SERVICE = "permission"; field public static final String PERSISTENT_DATA_BLOCK_SERVICE = "persistent_data_block"; @@ -6438,6 +6437,7 @@ package android.net { } public class NetworkStack { + method @Nullable public static android.os.IBinder getService(); field public static final String PERMISSION_MAINLINE_NETWORK_STACK = "android.permission.MAINLINE_NETWORK_STACK"; } diff --git a/api/test-current.txt b/api/test-current.txt index a5f1f68b82ac7..95e060bd61079 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -816,7 +816,6 @@ package android.content { field public static final String DEVICE_IDLE_CONTROLLER = "deviceidle"; field public static final String DREAM_SERVICE = "dream"; field public static final String ETHERNET_SERVICE = "ethernet"; - field public static final String NETWORK_STACK_SERVICE = "network_stack"; field public static final String PERMISSION_SERVICE = "permission"; field public static final String POWER_WHITELIST_MANAGER = "power_whitelist"; field public static final String ROLLBACK_SERVICE = "rollback"; @@ -1817,6 +1816,7 @@ package android.net { } public class NetworkStack { + method @Nullable public static android.os.IBinder getService(); field public static final String PERMISSION_MAINLINE_NETWORK_STACK = "android.permission.MAINLINE_NETWORK_STACK"; } diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java index a3bcc9cee39bf..b6edc4fbd7187 100644 --- a/core/java/android/app/SystemServiceRegistry.java +++ b/core/java/android/app/SystemServiceRegistry.java @@ -356,14 +356,6 @@ public final class SystemServiceRegistry { } }); - registerService(Context.NETWORK_STACK_SERVICE, IBinder.class, - new StaticServiceFetcher() { - @Override - public IBinder createService() { - return ServiceManager.getService(Context.NETWORK_STACK_SERVICE); - } - }); - registerService(Context.TETHERING_SERVICE, TetheringManager.class, new CachedServiceFetcher() { @Override diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index 5e9ed60260830..67f6d0edfbc84 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -3975,8 +3975,6 @@ public abstract class Context { * @hide * @see NetworkStackClient */ - @SystemApi - @TestApi public static final String NETWORK_STACK_SERVICE = "network_stack"; /** diff --git a/core/java/android/net/NetworkStack.java b/core/java/android/net/NetworkStack.java index a46c410bd55ef..a6d52d133ce90 100644 --- a/core/java/android/net/NetworkStack.java +++ b/core/java/android/net/NetworkStack.java @@ -19,15 +19,17 @@ import static android.Manifest.permission.NETWORK_STACK; import static android.content.pm.PackageManager.PERMISSION_GRANTED; import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.SystemApi; import android.annotation.TestApi; import android.content.Context; +import android.os.IBinder; +import android.os.ServiceManager; import java.util.ArrayList; import java.util.Arrays; /** - * - * Constants for client code communicating with the network stack service. + * Constants and utilities for client code communicating with the network stack service. * @hide */ @SystemApi @@ -43,6 +45,17 @@ public class NetworkStack { public static final String PERMISSION_MAINLINE_NETWORK_STACK = "android.permission.MAINLINE_NETWORK_STACK"; + /** + * Get an {@link IBinder} representing the NetworkStack stable AIDL Interface, if registered. + * @hide + */ + @Nullable + @SystemApi + @TestApi + public static IBinder getService() { + return ServiceManager.getService(Context.NETWORK_STACK_SERVICE); + } + private NetworkStack() {} /** From 438018d1c0a3039fada617c5f4d06ca783842088 Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Wed, 18 Mar 2020 18:31:39 +0900 Subject: [PATCH 2/2] Remove usage of Context.NETWORK_STACK_SERVICE The NetworkStack.getService() API should be used instead. Bug: 151243982 Test: atest FrameworksNetTests TetheringTests Manual tethering test Change-Id: I7855090bffbe895c8349ad4903b8f2eb55515f0b --- .../server/connectivity/tethering/TetheringService.java | 4 ++-- .../net/java/com/android/server/ConnectivityServiceTest.java | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/Tethering/src/com/android/server/connectivity/tethering/TetheringService.java b/packages/Tethering/src/com/android/server/connectivity/tethering/TetheringService.java index 020b32adcfd73..c5329d8d3316d 100644 --- a/packages/Tethering/src/com/android/server/connectivity/tethering/TetheringService.java +++ b/packages/Tethering/src/com/android/server/connectivity/tethering/TetheringService.java @@ -33,6 +33,7 @@ import android.net.ITetheringConnector; import android.net.ITetheringEventCallback; import android.net.NetworkCapabilities; import android.net.NetworkRequest; +import android.net.NetworkStack; import android.net.TetheringRequestParcel; import android.net.dhcp.DhcpServerCallbacks; import android.net.dhcp.DhcpServingParamsParcel; @@ -364,8 +365,7 @@ public class TetheringService extends Service { IBinder connector; try { final long before = System.currentTimeMillis(); - while ((connector = (IBinder) mContext.getSystemService( - Context.NETWORK_STACK_SERVICE)) == null) { + while ((connector = NetworkStack.getService()) == null) { if (System.currentTimeMillis() - before > NETWORKSTACK_TIMEOUT_MS) { Log.wtf(TAG, "Timeout, fail to get INetworkStackConnector"); return null; diff --git a/tests/net/java/com/android/server/ConnectivityServiceTest.java b/tests/net/java/com/android/server/ConnectivityServiceTest.java index 8c0c36b89bf9a..5c5e22525fa68 100644 --- a/tests/net/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/net/java/com/android/server/ConnectivityServiceTest.java @@ -428,7 +428,6 @@ public class ConnectivityServiceTest { public Object getSystemService(String name) { if (Context.CONNECTIVITY_SERVICE.equals(name)) return mCm; if (Context.NOTIFICATION_SERVICE.equals(name)) return mNotificationManager; - if (Context.NETWORK_STACK_SERVICE.equals(name)) return mNetworkStack; if (Context.USER_SERVICE.equals(name)) return mUserManager; if (Context.ALARM_SERVICE.equals(name)) return mAlarmManager; if (Context.LOCATION_SERVICE.equals(name)) return mLocationManager;