From b222570f371c3da3f987087e1d467b65bd6f1070 Mon Sep 17 00:00:00 2001 From: paulhu Date: Wed, 17 Nov 2021 09:35:33 +0800 Subject: [PATCH] Replace @hide APIs NSD associated files are going to move into connectivity mainline module in T, so need convert all hidden APIs to formal API surfaces. - Replace Slog with Log - Remove useless implements Watchdog.Monitor - Replace Build.IS_DEBUGGABLE with Build.isDebuggable() - Replace Preconditions.checkState90 by checking null directly. - Replace Lists.newArrayList() by new ArrayList<>() - Replace DumpUtils.checkDumpPermission() by checking DUMP permission directly. Bug: 206702844 Test: atest FrameworksNetTests CtsNetTestCases Change-Id: I6bc478b852c10591959d9e0615af63e675532abe --- .../android/server/NativeDaemonConnector.java | 43 ++++++------- .../com/android/server/NativeDaemonEvent.java | 15 +++-- .../src/com/android/server/NsdService.java | 60 ++++++++++--------- 3 files changed, 59 insertions(+), 59 deletions(-) diff --git a/packages/Nsd/service/src/com/android/server/NativeDaemonConnector.java b/packages/Nsd/service/src/com/android/server/NativeDaemonConnector.java index eac767f735..02475dd1ea 100644 --- a/packages/Nsd/service/src/com/android/server/NativeDaemonConnector.java +++ b/packages/Nsd/service/src/com/android/server/NativeDaemonConnector.java @@ -26,12 +26,10 @@ import android.os.PowerManager; import android.os.SystemClock; import android.os.SystemProperties; import android.util.LocalLog; -import android.util.Slog; +import android.util.Log; import com.android.internal.annotations.VisibleForTesting; -import com.android.internal.util.Preconditions; import com.android.server.power.ShutdownThread; -import com.google.android.collect.Lists; import java.io.FileDescriptor; import java.io.IOException; @@ -40,19 +38,19 @@ import java.io.OutputStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.LinkedList; +import java.util.Objects; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.LinkedList; -import java.util.Objects; +import java.util.concurrent.atomic.AtomicInteger; /** * Generic connector class for interfacing with a native daemon which uses the * {@code libsysutils} FrameworkListener protocol. */ -final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdog.Monitor { +final class NativeDaemonConnector implements Runnable, Handler.Callback { private final static boolean VDBG = false; private final String TAG; @@ -107,7 +105,7 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo /** * Enable Set debugging mode, which causes messages to also be written to both - * {@link Slog} in addition to internal log. + * {@link Log} in addition to internal log. */ public void setDebug(boolean debug) { mDebug = debug; @@ -126,7 +124,9 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo * calls while holding a lock on the given object. */ public void setWarnIfHeld(Object warnIfHeld) { - Preconditions.checkState(mWarnIfHeld == null); + if (mWarnIfHeld != null) { + throw new IllegalStateException("warnIfHeld is already set."); + } mWarnIfHeld = Objects.requireNonNull(warnIfHeld); } @@ -183,7 +183,7 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo // In order to ensure that unprivileged apps aren't able to impersonate native daemons on // production devices, even if said native daemons ill-advisedly pick a socket name that // starts with __test__, only allow this on debug builds. - if (mSocket.startsWith("__test__") && Build.IS_DEBUGGABLE) { + if (mSocket.startsWith("__test__") && Build.isDebuggable()) { return new LocalSocketAddress(mSocket); } else { return new LocalSocketAddress(mSocket, LocalSocketAddress.Namespace.RESERVED); @@ -375,7 +375,7 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo try { latch.await(); } catch (InterruptedException e) { - Slog.wtf(TAG, "Interrupted while waiting for unsolicited response handling", e); + Log.wtf(TAG, "Interrupted while waiting for unsolicited response handling", e); } } @@ -462,13 +462,13 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo public NativeDaemonEvent[] executeForList(long timeoutMs, String cmd, Object... args) throws NativeDaemonConnectorException { if (mWarnIfHeld != null && Thread.holdsLock(mWarnIfHeld)) { - Slog.wtf(TAG, "Calling thread " + Thread.currentThread().getName() + " is holding 0x" + Log.wtf(TAG, "Calling thread " + Thread.currentThread().getName() + " is holding 0x" + Integer.toHexString(System.identityHashCode(mWarnIfHeld)), new Throwable()); } final long startTime = SystemClock.elapsedRealtime(); - final ArrayList events = Lists.newArrayList(); + final ArrayList events = new ArrayList<>(); final StringBuilder rawBuilder = new StringBuilder(); final StringBuilder logBuilder = new StringBuilder(); @@ -571,7 +571,7 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo */ public static class Command { private String mCmd; - private ArrayList mArguments = Lists.newArrayList(); + private ArrayList mArguments = new ArrayList<>(); public Command(String cmd, Object... args) { mCmd = cmd; @@ -586,11 +586,6 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo } } - /** {@inheritDoc} */ - public void monitor() { - synchronized (mDaemonLock) { } - } - public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { mLocalLog.dump(fd, pw, args); pw.println(); @@ -598,12 +593,12 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo } private void log(String logstring) { - if (mDebug) Slog.d(TAG, logstring); + if (mDebug) Log.d(TAG, logstring); mLocalLog.log(logstring); } private void loge(String logstring) { - Slog.e(TAG, logstring); + Log.e(TAG, logstring); mLocalLog.log(logstring); } @@ -659,12 +654,12 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo if (found == null) { // didn't find it - make sure our queue isn't too big before adding while (mPendingCmds.size() >= mMaxCount) { - Slog.e("NativeDaemonConnector.ResponseQueue", + Log.e("NativeDaemonConnector.ResponseQueue", "more buffered than allowed: " + mPendingCmds.size() + " >= " + mMaxCount); // let any waiter timeout waiting for this PendingCmd pendingCmd = mPendingCmds.remove(); - Slog.e("NativeDaemonConnector.ResponseQueue", + Log.e("NativeDaemonConnector.ResponseQueue", "Removing request: " + pendingCmd.logCmd + " (" + pendingCmd.cmdNum + ")"); } @@ -706,7 +701,7 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo result = found.responses.poll(timeoutMs, TimeUnit.MILLISECONDS); } catch (InterruptedException e) {} if (result == null) { - Slog.e("NativeDaemonConnector.ResponseQueue", "Timeout waiting for response"); + Log.e("NativeDaemonConnector.ResponseQueue", "Timeout waiting for response"); } return result; } diff --git a/packages/Nsd/service/src/com/android/server/NativeDaemonEvent.java b/packages/Nsd/service/src/com/android/server/NativeDaemonEvent.java index e6feda3dad..5683694346 100644 --- a/packages/Nsd/service/src/com/android/server/NativeDaemonEvent.java +++ b/packages/Nsd/service/src/com/android/server/NativeDaemonEvent.java @@ -16,8 +16,7 @@ package com.android.server; -import android.util.Slog; -import com.google.android.collect.Lists; +import android.util.Log; import java.io.FileDescriptor; import java.util.ArrayList; @@ -179,7 +178,7 @@ public class NativeDaemonEvent { * {@link #getMessage()} for any events matching the requested code. */ public static String[] filterMessageList(NativeDaemonEvent[] events, int matchCode) { - final ArrayList result = Lists.newArrayList(); + final ArrayList result = new ArrayList<>(); for (NativeDaemonEvent event : events) { if (event.getCode() == matchCode) { result.add(event.getMessage()); @@ -212,7 +211,7 @@ public class NativeDaemonEvent { int wordEnd = -1; boolean quoted = false; - if (DEBUG_ROUTINE) Slog.e(LOGTAG, "parsing '" + rawEvent + "'"); + if (DEBUG_ROUTINE) Log.e(LOGTAG, "parsing '" + rawEvent + "'"); if (rawEvent.charAt(current) == '\"') { quoted = true; current++; @@ -240,14 +239,14 @@ public class NativeDaemonEvent { word = word.replace("\\\\", "\\"); word = word.replace("\\\"", "\""); - if (DEBUG_ROUTINE) Slog.e(LOGTAG, "found '" + word + "'"); + if (DEBUG_ROUTINE) Log.e(LOGTAG, "found '" + word + "'"); parsed.add(word); // find the beginning of the next word - either of these options int nextSpace = rawEvent.indexOf(' ', current); int nextQuote = rawEvent.indexOf(" \"", current); if (DEBUG_ROUTINE) { - Slog.e(LOGTAG, "nextSpace=" + nextSpace + ", nextQuote=" + nextQuote); + Log.e(LOGTAG, "nextSpace=" + nextSpace + ", nextQuote=" + nextQuote); } if (nextQuote > -1 && nextQuote <= nextSpace) { quoted = true; @@ -259,8 +258,8 @@ public class NativeDaemonEvent { } } // else we just start the next word after the current and read til the end if (DEBUG_ROUTINE) { - Slog.e(LOGTAG, "next loop - current=" + current + - ", length=" + length + ", quoted=" + quoted); + Log.e(LOGTAG, "next loop - current=" + current + + ", length=" + length + ", quoted=" + quoted); } } return parsed.toArray(new String[parsed.size()]); diff --git a/packages/Nsd/service/src/com/android/server/NsdService.java b/packages/Nsd/service/src/com/android/server/NsdService.java index 3e0208411c..76ecea726b 100644 --- a/packages/Nsd/service/src/com/android/server/NsdService.java +++ b/packages/Nsd/service/src/com/android/server/NsdService.java @@ -19,6 +19,7 @@ package com.android.server; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.database.ContentObserver; import android.net.Uri; import android.net.nsd.INsdManager; @@ -36,12 +37,10 @@ import android.provider.Settings; import android.util.Base64; import android.util.Log; import android.util.Pair; -import android.util.Slog; import android.util.SparseArray; import android.util.SparseIntArray; import com.android.internal.annotations.VisibleForTesting; -import com.android.internal.util.DumpUtils; import com.android.internal.util.State; import com.android.internal.util.StateMachine; import com.android.net.module.util.DnsSdTxtRecord; @@ -228,7 +227,7 @@ public class NsdService extends INsdManager.Stub { break; case NsdManager.NATIVE_DAEMON_EVENT: default: - Slog.e(TAG, "Unhandled " + msg); + Log.e(TAG, "Unhandled " + msg); return NOT_HANDLED; } return HANDLED; @@ -274,7 +273,7 @@ public class NsdService extends INsdManager.Stub { private boolean requestLimitReached(ClientInfo clientInfo) { if (clientInfo.mClientIds.size() >= ClientInfo.MAX_LIMIT) { - if (DBG) Slog.d(TAG, "Exceeded max outstanding requests " + clientInfo); + if (DBG) Log.d(TAG, "Exceeded max outstanding requests " + clientInfo); return true; } return false; @@ -307,7 +306,7 @@ public class NsdService extends INsdManager.Stub { transitionTo(mDisabledState); break; case NsdManager.DISCOVER_SERVICES: - if (DBG) Slog.d(TAG, "Discover services"); + if (DBG) Log.d(TAG, "Discover services"); args = (ListenerArgs) msg.obj; clientInfo = mClients.get(args.connector); @@ -321,8 +320,8 @@ public class NsdService extends INsdManager.Stub { id = getUniqueId(); if (discoverServices(id, args.serviceInfo.getServiceType())) { if (DBG) { - Slog.d(TAG, "Discover " + msg.arg2 + " " + id + - args.serviceInfo.getServiceType()); + Log.d(TAG, "Discover " + msg.arg2 + " " + id + + args.serviceInfo.getServiceType()); } storeRequestMap(clientId, id, clientInfo, msg.what); clientInfo.onDiscoverServicesStarted(clientId, args.serviceInfo); @@ -333,7 +332,7 @@ public class NsdService extends INsdManager.Stub { } break; case NsdManager.STOP_DISCOVERY: - if (DBG) Slog.d(TAG, "Stop service discovery"); + if (DBG) Log.d(TAG, "Stop service discovery"); args = (ListenerArgs) msg.obj; clientInfo = mClients.get(args.connector); @@ -353,7 +352,7 @@ public class NsdService extends INsdManager.Stub { } break; case NsdManager.REGISTER_SERVICE: - if (DBG) Slog.d(TAG, "Register service"); + if (DBG) Log.d(TAG, "Register service"); args = (ListenerArgs) msg.obj; clientInfo = mClients.get(args.connector); if (requestLimitReached(clientInfo)) { @@ -365,7 +364,7 @@ public class NsdService extends INsdManager.Stub { maybeStartDaemon(); id = getUniqueId(); if (registerService(id, args.serviceInfo)) { - if (DBG) Slog.d(TAG, "Register " + clientId + " " + id); + if (DBG) Log.d(TAG, "Register " + clientId + " " + id); storeRequestMap(clientId, id, clientInfo, msg.what); // Return success after mDns reports success } else { @@ -375,11 +374,11 @@ public class NsdService extends INsdManager.Stub { } break; case NsdManager.UNREGISTER_SERVICE: - if (DBG) Slog.d(TAG, "unregister service"); + if (DBG) Log.d(TAG, "unregister service"); args = (ListenerArgs) msg.obj; clientInfo = mClients.get(args.connector); if (clientInfo == null) { - Slog.e(TAG, "Unknown connector in unregistration"); + Log.e(TAG, "Unknown connector in unregistration"); break; } id = clientInfo.mClientIds.get(clientId); @@ -392,7 +391,7 @@ public class NsdService extends INsdManager.Stub { } break; case NsdManager.RESOLVE_SERVICE: - if (DBG) Slog.d(TAG, "Resolve service"); + if (DBG) Log.d(TAG, "Resolve service"); args = (ListenerArgs) msg.obj; clientInfo = mClients.get(args.connector); @@ -430,7 +429,7 @@ public class NsdService extends INsdManager.Stub { ClientInfo clientInfo = mIdToClientInfoMap.get(id); if (clientInfo == null) { String name = NativeResponseCode.nameOf(code); - Slog.e(TAG, String.format("id %d for %s has no client mapping", id, name)); + Log.e(TAG, String.format("id %d for %s has no client mapping", id, name)); return false; } @@ -441,14 +440,14 @@ public class NsdService extends INsdManager.Stub { // SERVICE_FOUND may race with STOP_SERVICE_DISCOVERY, // and we may get in this situation. String name = NativeResponseCode.nameOf(code); - Slog.d(TAG, String.format( + Log.d(TAG, String.format( "Notification %s for listener id %d that is no longer active", name, id)); return false; } if (DBG) { String name = NativeResponseCode.nameOf(code); - Slog.d(TAG, String.format("Native daemon message %s: %s", name, raw)); + Log.d(TAG, String.format("Native daemon message %s: %s", name, raw)); } switch (code) { case NativeResponseCode.SERVICE_FOUND: @@ -492,7 +491,7 @@ public class NsdService extends INsdManager.Stub { ++index; } if (index >= cooked[2].length()) { - Slog.e(TAG, "Invalid service found " + raw); + Log.e(TAG, "Invalid service found " + raw); break; } String name = cooked[2].substring(0, index); @@ -562,13 +561,13 @@ public class NsdService extends INsdManager.Stub { char c = s.charAt(i); if (c == '\\') { if (++i >= s.length()) { - Slog.e(TAG, "Unexpected end of escape sequence in: " + s); + Log.e(TAG, "Unexpected end of escape sequence in: " + s); break; } c = s.charAt(i); if (c != '.' && c != '\\') { if (i + 2 >= s.length()) { - Slog.e(TAG, "Unexpected end of escape sequence in: " + s); + Log.e(TAG, "Unexpected end of escape sequence in: " + s); break; } c = (char) ((c-'0') * 100 + (s.charAt(i+1)-'0') * 10 + (s.charAt(i+2)-'0')); @@ -685,7 +684,7 @@ public class NsdService extends INsdManager.Stub { private boolean isNsdEnabled() { boolean ret = mNsdSettings.isEnabled(); if (DBG) { - Slog.d(TAG, "Network service discovery is " + (ret ? "enabled" : "disabled")); + Log.d(TAG, "Network service discovery is " + (ret ? "enabled" : "disabled")); } return ret; } @@ -795,12 +794,12 @@ public class NsdService extends INsdManager.Stub { */ public boolean execute(Object... args) { if (DBG) { - Slog.d(TAG, "mdnssd " + Arrays.toString(args)); + Log.d(TAG, "mdnssd " + Arrays.toString(args)); } try { mNativeConnector.execute("mdnssd", args); } catch (NativeDaemonConnectorException e) { - Slog.e(TAG, "Failed to execute mdnssd " + Arrays.toString(args), e); + Log.e(TAG, "Failed to execute mdnssd " + Arrays.toString(args), e); return false; } return true; @@ -831,7 +830,7 @@ public class NsdService extends INsdManager.Stub { private boolean registerService(int regId, NsdServiceInfo service) { if (DBG) { - Slog.d(TAG, "registerService: " + regId + " " + service); + Log.d(TAG, "registerService: " + regId + " " + service); } String name = service.getServiceName(); String type = service.getServiceType(); @@ -880,7 +879,12 @@ public class NsdService extends INsdManager.Stub { @Override public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { - if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; + if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) + != PackageManager.PERMISSION_GRANTED) { + pw.println("Permission Denial: can't dump " + TAG + + " due to missing android.permission.DUMP permission"); + return; + } for (ClientInfo client : mClients.values()) { pw.println("Client Info"); @@ -909,7 +913,7 @@ public class NsdService extends INsdManager.Stub { private ClientInfo(INsdManagerCallback cb) { mCb = cb; - if (DBG) Slog.d(TAG, "New client"); + if (DBG) Log.d(TAG, "New client"); } @Override @@ -943,8 +947,10 @@ public class NsdService extends INsdManager.Stub { clientId = mClientIds.keyAt(i); globalId = mClientIds.valueAt(i); mIdToClientInfoMap.remove(globalId); - if (DBG) Slog.d(TAG, "Terminating client-ID " + clientId + - " global-ID " + globalId + " type " + mClientRequests.get(clientId)); + if (DBG) { + Log.d(TAG, "Terminating client-ID " + clientId + + " global-ID " + globalId + " type " + mClientRequests.get(clientId)); + } switch (mClientRequests.get(clientId)) { case NsdManager.DISCOVER_SERVICES: stopServiceDiscovery(globalId);