From e81f99d5328063219eb61ce716f253579b3efb1f Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Tue, 17 Mar 2020 00:11:37 +0900 Subject: [PATCH] Support setting the default interface to null again. Currently, the default interface can only ever go from null to non-null. This is correct for fixed interfaces (because they don't get unplugged) and it's generally correct for USB interfaces that use ethX as their device name (because when they are unplugged and plugged in again, the device name won't change). But it is not correct if, for example, the default interface is a test interface. So, allow mDefaultInterface to go back to null. This CL also fixes a crash if a tethered interface request is added and removed when there is no default interface. Also, make dump() report tethered interface requests. Also remove an unused variable that I missed in the previous CL. Bug: 150644681 Test: tested by EthernetTetheringTest in same topic Change-Id: I5109d8da3aeb6c1f6523291d9e2ec92c64b5ad2d --- .../server/ethernet/EthernetTracker.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/service-t/src/com/android/server/ethernet/EthernetTracker.java b/service-t/src/com/android/server/ethernet/EthernetTracker.java index 91453abe1a..d1f1402311 100644 --- a/service-t/src/com/android/server/ethernet/EthernetTracker.java +++ b/service-t/src/com/android/server/ethernet/EthernetTracker.java @@ -240,17 +240,17 @@ final class EthernetTracker { private void maybeUntetherDefaultInterface() { if (mTetheredInterfaceRequests.getRegisteredCallbackCount() > 0) return; - // mDefaultInterface null means that there never was a default interface (it is never set - // to null). - if (mDefaultInterfaceMode == INTERFACE_MODE_CLIENT || mDefaultInterface == null) return; - + if (mDefaultInterfaceMode == INTERFACE_MODE_CLIENT) return; setDefaultInterfaceMode(INTERFACE_MODE_CLIENT); } private void setDefaultInterfaceMode(int mode) { + Log.d(TAG, "Setting default interface mode to " + mode); mDefaultInterfaceMode = mode; - removeInterface(mDefaultInterface); - addInterface(mDefaultInterface); + if (mDefaultInterface != null) { + removeInterface(mDefaultInterface); + addInterface(mDefaultInterface); + } } private int getInterfaceMode(final String iface) { @@ -264,6 +264,13 @@ final class EthernetTracker { mFactory.removeInterface(iface); } + private void stopTrackingInterface(String iface) { + removeInterface(iface); + if (iface.equals(mDefaultInterface)) { + mDefaultInterface = null; + } + } + private void addInterface(String iface) { InterfaceConfiguration config = null; // Bring up the interface so we get link status indications. @@ -406,7 +413,7 @@ final class EthernetTracker { @Override public void interfaceRemoved(String iface) { - mHandler.post(() -> removeInterface(iface)); + mHandler.post(() -> stopTrackingInterface(iface)); } } @@ -585,7 +592,6 @@ final class EthernetTracker { } private void updateIfaceMatchRegexp() { - final String testInterfaceMatch = TEST_TAP_PREFIX + ".*"; final String match = mContext.getResources().getString( com.android.internal.R.string.config_ethernet_iface_regex); mIfaceMatch = mIncludeTestInterfaces @@ -604,6 +610,8 @@ final class EthernetTracker { pw.println("Ethernet interface name filter: " + mIfaceMatch); pw.println("Default interface: " + mDefaultInterface); pw.println("Default interface mode: " + mDefaultInterfaceMode); + pw.println("Tethered interface requests: " + + mTetheredInterfaceRequests.getRegisteredCallbackCount()); pw.println("Listeners: " + mListeners.getRegisteredCallbackCount()); pw.println("IP Configurations:"); pw.increaseIndent();