| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.SystemApi; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.util.ArrayMap; |
| 24 |
import android.util.Log; |
| 25 |
|
| 26 |
import com.android.internal.annotations.GuardedBy; |
| 27 |
import com.android.internal.os.BinderInternal; |
| 28 |
import com.android.internal.util.StatLogger; |
| 29 |
|
| 30 |
import java.util.Map; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 41 |
public final class ServiceManager { |
| 42 |
private static final String TAG = "ServiceManager"; |
| 43 |
private static final Object sLock = new Object(); |
| 44 |
|
| 45 |
@UnsupportedAppUsage |
| 46 |
private static IServiceManager sServiceManager; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
@UnsupportedAppUsage |
| 52 |
private static Map<String, IBinder> sCache = new ArrayMap<String, IBinder>(); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private static final int SLOW_LOG_INTERVAL_MS = 5000; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private static final int STATS_LOG_INTERVAL_MS = 5000; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
private static final long GET_SERVICE_SLOW_THRESHOLD_US_CORE = |
| 69 |
SystemProperties.getInt("debug.servicemanager.slow_call_core_ms", 10) * 1000; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private static final long GET_SERVICE_SLOW_THRESHOLD_US_NON_CORE = |
| 76 |
SystemProperties.getInt("debug.servicemanager.slow_call_ms", 50) * 1000; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private static final int GET_SERVICE_LOG_EVERY_CALLS_CORE = |
| 82 |
SystemProperties.getInt("debug.servicemanager.log_calls_core", 100); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private static final int GET_SERVICE_LOG_EVERY_CALLS_NON_CORE = |
| 88 |
SystemProperties.getInt("debug.servicemanager.log_calls", 200); |
| 89 |
|
| 90 |
@GuardedBy("sLock") |
| 91 |
private static int sGetServiceAccumulatedUs; |
| 92 |
|
| 93 |
@GuardedBy("sLock") |
| 94 |
private static int sGetServiceAccumulatedCallCount; |
| 95 |
|
| 96 |
@GuardedBy("sLock") |
| 97 |
private static long sLastStatsLogUptime; |
| 98 |
|
| 99 |
@GuardedBy("sLock") |
| 100 |
private static long sLastSlowLogUptime; |
| 101 |
|
| 102 |
@GuardedBy("sLock") |
| 103 |
private static long sLastSlowLogActualTime; |
| 104 |
|
| 105 |
interface Stats { |
| 106 |
int GET_SERVICE = 0; |
| 107 |
|
| 108 |
int COUNT = GET_SERVICE + 1; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
public static final StatLogger sStatLogger = new StatLogger(new String[] { |
| 113 |
"getService()", |
| 114 |
}); |
| 115 |
|
| 116 |
|
| 117 |
@UnsupportedAppUsage |
| 118 |
public ServiceManager() { |
| 119 |
} |
| 120 |
|
| 121 |
@UnsupportedAppUsage |
| 122 |
private static IServiceManager getIServiceManager() { |
| 123 |
if (sServiceManager != null) { |
| 124 |
return sServiceManager; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
sServiceManager = ServiceManagerNative |
| 129 |
.asInterface(Binder.allowBlocking(BinderInternal.getContextObject())); |
| 130 |
return sServiceManager; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
@UnsupportedAppUsage |
| 141 |
public static IBinder getService(String name) { |
| 142 |
try { |
| 143 |
IBinder service = sCache.get(name); |
| 144 |
if (service != null) { |
| 145 |
return service; |
| 146 |
} else { |
| 147 |
return Binder.allowBlocking(rawGetService(name)); |
| 148 |
} |
| 149 |
} catch (RemoteException e) { |
| 150 |
Log.e(TAG, "error in getService", e); |
| 151 |
} |
| 152 |
return null; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public static IBinder getServiceOrThrow(String name) throws ServiceNotFoundException { |
| 162 |
final IBinder binder = getService(name); |
| 163 |
if (binder != null) { |
| 164 |
return binder; |
| 165 |
} else { |
| 166 |
throw new ServiceNotFoundException(name); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
@UnsupportedAppUsage |
| 179 |
public static void addService(String name, IBinder service) { |
| 180 |
addService(name, service, false, IServiceManager.DUMP_FLAG_PRIORITY_DEFAULT); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
@UnsupportedAppUsage |
| 194 |
public static void addService(String name, IBinder service, boolean allowIsolated) { |
| 195 |
addService(name, service, allowIsolated, IServiceManager.DUMP_FLAG_PRIORITY_DEFAULT); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 210 |
public static void addService(String name, IBinder service, boolean allowIsolated, |
| 211 |
int dumpPriority) { |
| 212 |
try { |
| 213 |
getIServiceManager().addService(name, service, allowIsolated, dumpPriority); |
| 214 |
} catch (RemoteException e) { |
| 215 |
Log.e(TAG, "error in addService", e); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
@UnsupportedAppUsage |
| 225 |
public static IBinder checkService(String name) { |
| 226 |
try { |
| 227 |
IBinder service = sCache.get(name); |
| 228 |
if (service != null) { |
| 229 |
return service; |
| 230 |
} else { |
| 231 |
return Binder.allowBlocking(getIServiceManager().checkService(name)); |
| 232 |
} |
| 233 |
} catch (RemoteException e) { |
| 234 |
Log.e(TAG, "error in checkService", e); |
| 235 |
return null; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public static boolean isDeclared(@NonNull String name) { |
| 246 |
try { |
| 247 |
return getIServiceManager().isDeclared(name); |
| 248 |
} catch (RemoteException e) { |
| 249 |
Log.e(TAG, "error in isDeclared", e); |
| 250 |
return false; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 264 |
@NonNull |
| 265 |
public static String[] getDeclaredInstances(@NonNull String iface) { |
| 266 |
try { |
| 267 |
return getIServiceManager().getDeclaredInstances(iface); |
| 268 |
} catch (RemoteException e) { |
| 269 |
Log.e(TAG, "error in getDeclaredInstances", e); |
| 270 |
throw e.rethrowFromSystemServer(); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public static IBinder waitForService(@NonNull String name) { |
| 284 |
return Binder.allowBlocking(waitForServiceNative(name)); |
| 285 |
} |
| 286 |
|
| 287 |
private static native IBinder waitForServiceNative(@NonNull String name); |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 302 |
@Nullable public static IBinder waitForDeclaredService(@NonNull String name) { |
| 303 |
return isDeclared(name) ? waitForService(name) : null; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public static void registerForNotifications( |
| 313 |
@NonNull String name, @NonNull IServiceCallback callback) throws RemoteException { |
| 314 |
getIServiceManager().registerForNotifications(name, callback); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
@UnsupportedAppUsage |
| 324 |
public static String[] listServices() { |
| 325 |
try { |
| 326 |
return getIServiceManager().listServices(IServiceManager.DUMP_FLAG_PRIORITY_ALL); |
| 327 |
} catch (RemoteException e) { |
| 328 |
Log.e(TAG, "error in listServices", e); |
| 329 |
return null; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public static ServiceDebugInfo[] getServiceDebugInfo() { |
| 339 |
try { |
| 340 |
return getIServiceManager().getServiceDebugInfo(); |
| 341 |
} catch (RemoteException e) { |
| 342 |
Log.e(TAG, "error in getServiceDebugInfo", e); |
| 343 |
return null; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public static void initServiceCache(Map<String, IBinder> cache) { |
| 356 |
if (sCache.size() != 0) { |
| 357 |
throw new IllegalStateException("setServiceCache may only be called once"); |
| 358 |
} |
| 359 |
sCache.putAll(cache); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
public static class ServiceNotFoundException extends Exception { |
| 370 |
public ServiceNotFoundException(String name) { |
| 371 |
super("No service published for: " + name); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
private static IBinder rawGetService(String name) throws RemoteException { |
| 376 |
final long start = sStatLogger.getTime(); |
| 377 |
|
| 378 |
final IBinder binder = getIServiceManager().getService(name); |
| 379 |
|
| 380 |
final int time = (int) sStatLogger.logDurationStat(Stats.GET_SERVICE, start); |
| 381 |
|
| 382 |
final int myUid = Process.myUid(); |
| 383 |
final boolean isCore = UserHandle.isCore(myUid); |
| 384 |
|
| 385 |
final long slowThreshold = isCore |
| 386 |
? GET_SERVICE_SLOW_THRESHOLD_US_CORE |
| 387 |
: GET_SERVICE_SLOW_THRESHOLD_US_NON_CORE; |
| 388 |
|
| 389 |
synchronized (sLock) { |
| 390 |
sGetServiceAccumulatedUs += time; |
| 391 |
sGetServiceAccumulatedCallCount++; |
| 392 |
|
| 393 |
final long nowUptime = SystemClock.uptimeMillis(); |
| 394 |
|
| 395 |
|
| 396 |
if (time >= slowThreshold) { |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
if ((nowUptime > (sLastSlowLogUptime + SLOW_LOG_INTERVAL_MS)) |
| 401 |
|| (sLastSlowLogActualTime < time)) { |
| 402 |
EventLogTags.writeServiceManagerSlow(time / 1000, name); |
| 403 |
|
| 404 |
sLastSlowLogUptime = nowUptime; |
| 405 |
sLastSlowLogActualTime = time; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
final int logInterval = isCore |
| 412 |
? GET_SERVICE_LOG_EVERY_CALLS_CORE |
| 413 |
: GET_SERVICE_LOG_EVERY_CALLS_NON_CORE; |
| 414 |
|
| 415 |
if ((sGetServiceAccumulatedCallCount >= logInterval) |
| 416 |
&& (nowUptime >= (sLastStatsLogUptime + STATS_LOG_INTERVAL_MS))) { |
| 417 |
|
| 418 |
EventLogTags.writeServiceManagerStats( |
| 419 |
sGetServiceAccumulatedCallCount, |
| 420 |
sGetServiceAccumulatedUs / 1000, |
| 421 |
(int) (nowUptime - sLastStatsLogUptime)); |
| 422 |
sGetServiceAccumulatedCallCount = 0; |
| 423 |
sGetServiceAccumulatedUs = 0; |
| 424 |
sLastStatsLogUptime = nowUptime; |
| 425 |
} |
| 426 |
} |
| 427 |
return binder; |
| 428 |
} |
| 429 |
} |