ActiveServices.java revision 5e03e2ca7d25b899b129baad2dd5eca6bf99d88a
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.am; 18 19import java.io.FileDescriptor; 20import java.io.IOException; 21import java.io.PrintWriter; 22import java.util.ArrayList; 23import java.util.Collection; 24import java.util.HashMap; 25import java.util.HashSet; 26import java.util.Iterator; 27import java.util.List; 28 29import com.android.internal.os.BatteryStatsImpl; 30import com.android.server.am.ActivityManagerService.ItemMatcher; 31import com.android.server.am.ActivityManagerService.NeededUriGrants; 32 33import android.app.ActivityManager; 34import android.app.AppGlobals; 35import android.app.IApplicationThread; 36import android.app.IServiceConnection; 37import android.app.Notification; 38import android.app.PendingIntent; 39import android.app.Service; 40import android.content.ComponentName; 41import android.content.Context; 42import android.content.Intent; 43import android.content.pm.ApplicationInfo; 44import android.content.pm.PackageManager; 45import android.content.pm.ResolveInfo; 46import android.content.pm.ServiceInfo; 47import android.content.pm.UserInfo; 48import android.os.Binder; 49import android.os.IBinder; 50import android.os.Message; 51import android.os.Process; 52import android.os.RemoteException; 53import android.os.SystemClock; 54import android.os.UserHandle; 55import android.util.EventLog; 56import android.util.Log; 57import android.util.Slog; 58import android.util.SparseArray; 59import android.util.TimeUtils; 60 61public class ActiveServices { 62 static final boolean DEBUG_SERVICE = ActivityManagerService.DEBUG_SERVICE; 63 static final boolean DEBUG_SERVICE_EXECUTING = ActivityManagerService.DEBUG_SERVICE_EXECUTING; 64 static final boolean DEBUG_MU = ActivityManagerService.DEBUG_MU; 65 static final String TAG = ActivityManagerService.TAG; 66 static final String TAG_MU = ActivityManagerService.TAG_MU; 67 68 // How long we wait for a service to finish executing. 69 static final int SERVICE_TIMEOUT = 20*1000; 70 71 // How long a service needs to be running until restarting its process 72 // is no longer considered to be a relaunch of the service. 73 static final int SERVICE_RESTART_DURATION = 5*1000; 74 75 // How long a service needs to be running until it will start back at 76 // SERVICE_RESTART_DURATION after being killed. 77 static final int SERVICE_RESET_RUN_DURATION = 60*1000; 78 79 // Multiplying factor to increase restart duration time by, for each time 80 // a service is killed before it has run for SERVICE_RESET_RUN_DURATION. 81 static final int SERVICE_RESTART_DURATION_FACTOR = 4; 82 83 // The minimum amount of time between restarting services that we allow. 84 // That is, when multiple services are restarting, we won't allow each 85 // to restart less than this amount of time from the last one. 86 static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000; 87 88 // Maximum amount of time for there to be no activity on a service before 89 // we consider it non-essential and allow its process to go on the 90 // LRU background list. 91 static final int MAX_SERVICE_INACTIVITY = 30*60*1000; 92 93 final ActivityManagerService mAm; 94 95 final ServiceMap mServiceMap = new ServiceMap(); 96 97 /** 98 * All currently bound service connections. Keys are the IBinder of 99 * the client's IServiceConnection. 100 */ 101 final HashMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections 102 = new HashMap<IBinder, ArrayList<ConnectionRecord>>(); 103 104 /** 105 * List of services that we have been asked to start, 106 * but haven't yet been able to. It is used to hold start requests 107 * while waiting for their corresponding application thread to get 108 * going. 109 */ 110 final ArrayList<ServiceRecord> mPendingServices 111 = new ArrayList<ServiceRecord>(); 112 113 /** 114 * List of services that are scheduled to restart following a crash. 115 */ 116 final ArrayList<ServiceRecord> mRestartingServices 117 = new ArrayList<ServiceRecord>(); 118 119 /** 120 * List of services that are in the process of being stopped. 121 */ 122 final ArrayList<ServiceRecord> mStoppingServices 123 = new ArrayList<ServiceRecord>(); 124 125 static class ServiceMap { 126 127 private final SparseArray<HashMap<ComponentName, ServiceRecord>> mServicesByNamePerUser 128 = new SparseArray<HashMap<ComponentName, ServiceRecord>>(); 129 private final SparseArray<HashMap<Intent.FilterComparison, ServiceRecord>> 130 mServicesByIntentPerUser = new SparseArray< 131 HashMap<Intent.FilterComparison, ServiceRecord>>(); 132 133 ServiceRecord getServiceByName(ComponentName name, int callingUser) { 134 // TODO: Deal with global services 135 if (DEBUG_MU) 136 Slog.v(TAG_MU, "getServiceByName(" + name + "), callingUser = " + callingUser); 137 return getServices(callingUser).get(name); 138 } 139 140 ServiceRecord getServiceByName(ComponentName name) { 141 return getServiceByName(name, -1); 142 } 143 144 ServiceRecord getServiceByIntent(Intent.FilterComparison filter, int callingUser) { 145 // TODO: Deal with global services 146 if (DEBUG_MU) 147 Slog.v(TAG_MU, "getServiceByIntent(" + filter + "), callingUser = " + callingUser); 148 return getServicesByIntent(callingUser).get(filter); 149 } 150 151 ServiceRecord getServiceByIntent(Intent.FilterComparison filter) { 152 return getServiceByIntent(filter, -1); 153 } 154 155 void putServiceByName(ComponentName name, int callingUser, ServiceRecord value) { 156 // TODO: Deal with global services 157 getServices(callingUser).put(name, value); 158 } 159 160 void putServiceByIntent(Intent.FilterComparison filter, int callingUser, 161 ServiceRecord value) { 162 // TODO: Deal with global services 163 getServicesByIntent(callingUser).put(filter, value); 164 } 165 166 void removeServiceByName(ComponentName name, int callingUser) { 167 // TODO: Deal with global services 168 ServiceRecord removed = getServices(callingUser).remove(name); 169 if (DEBUG_MU) 170 Slog.v(TAG, "removeServiceByName user=" + callingUser + " name=" + name 171 + " removed=" + removed); 172 } 173 174 void removeServiceByIntent(Intent.FilterComparison filter, int callingUser) { 175 // TODO: Deal with global services 176 ServiceRecord removed = getServicesByIntent(callingUser).remove(filter); 177 if (DEBUG_MU) 178 Slog.v(TAG_MU, "removeServiceByIntent user=" + callingUser + " intent=" + filter 179 + " removed=" + removed); 180 } 181 182 Collection<ServiceRecord> getAllServices(int callingUser) { 183 // TODO: Deal with global services 184 return getServices(callingUser).values(); 185 } 186 187 private HashMap<ComponentName, ServiceRecord> getServices(int callingUser) { 188 HashMap<ComponentName, ServiceRecord> map = mServicesByNamePerUser.get(callingUser); 189 if (map == null) { 190 map = new HashMap<ComponentName, ServiceRecord>(); 191 mServicesByNamePerUser.put(callingUser, map); 192 } 193 return map; 194 } 195 196 private HashMap<Intent.FilterComparison, ServiceRecord> getServicesByIntent( 197 int callingUser) { 198 HashMap<Intent.FilterComparison, ServiceRecord> map 199 = mServicesByIntentPerUser.get(callingUser); 200 if (map == null) { 201 map = new HashMap<Intent.FilterComparison, ServiceRecord>(); 202 mServicesByIntentPerUser.put(callingUser, map); 203 } 204 return map; 205 } 206 } 207 208 public ActiveServices(ActivityManagerService service) { 209 mAm = service; 210 } 211 212 ComponentName startServiceLocked(IApplicationThread caller, 213 Intent service, String resolvedType, 214 int callingPid, int callingUid, int userId) { 215 if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service 216 + " type=" + resolvedType + " args=" + service.getExtras()); 217 218 if (caller != null) { 219 final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); 220 if (callerApp == null) { 221 throw new SecurityException( 222 "Unable to find app for caller " + caller 223 + " (pid=" + Binder.getCallingPid() 224 + ") when starting service " + service); 225 } 226 } 227 228 ServiceLookupResult res = 229 retrieveServiceLocked(service, resolvedType, 230 callingPid, callingUid, userId, true); 231 if (res == null) { 232 return null; 233 } 234 if (res.record == null) { 235 return new ComponentName("!", res.permission != null 236 ? res.permission : "private to package"); 237 } 238 ServiceRecord r = res.record; 239 NeededUriGrants neededGrants = mAm.checkGrantUriPermissionFromIntentLocked( 240 callingUid, r.packageName, service, service.getFlags(), null); 241 if (unscheduleServiceRestartLocked(r)) { 242 if (DEBUG_SERVICE) Slog.v(TAG, "START SERVICE WHILE RESTART PENDING: " + r); 243 } 244 r.startRequested = true; 245 r.callStart = false; 246 r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), 247 service, neededGrants)); 248 r.lastActivity = SystemClock.uptimeMillis(); 249 synchronized (r.stats.getBatteryStats()) { 250 r.stats.startRunningLocked(); 251 } 252 if (!bringUpServiceLocked(r, service.getFlags(), false)) { 253 return new ComponentName("!", "Service process is bad"); 254 } 255 return r.name; 256 } 257 258 private void stopServiceLocked(ServiceRecord service) { 259 synchronized (service.stats.getBatteryStats()) { 260 service.stats.stopRunningLocked(); 261 } 262 service.startRequested = false; 263 service.callStart = false; 264 bringDownServiceLocked(service, false); 265 } 266 267 int stopServiceLocked(IApplicationThread caller, Intent service, 268 String resolvedType, int userId) { 269 if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service 270 + " type=" + resolvedType); 271 272 final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); 273 if (caller != null && callerApp == null) { 274 throw new SecurityException( 275 "Unable to find app for caller " + caller 276 + " (pid=" + Binder.getCallingPid() 277 + ") when stopping service " + service); 278 } 279 280 // If this service is active, make sure it is stopped. 281 ServiceLookupResult r = retrieveServiceLocked(service, resolvedType, 282 Binder.getCallingPid(), Binder.getCallingUid(), userId, false); 283 if (r != null) { 284 if (r.record != null) { 285 final long origId = Binder.clearCallingIdentity(); 286 try { 287 stopServiceLocked(r.record); 288 } finally { 289 Binder.restoreCallingIdentity(origId); 290 } 291 return 1; 292 } 293 return -1; 294 } 295 296 return 0; 297 } 298 299 IBinder peekServiceLocked(Intent service, String resolvedType) { 300 ServiceLookupResult r = retrieveServiceLocked(service, resolvedType, 301 Binder.getCallingPid(), Binder.getCallingUid(), 302 UserHandle.getCallingUserId(), false); 303 304 IBinder ret = null; 305 if (r != null) { 306 // r.record is null if findServiceLocked() failed the caller permission check 307 if (r.record == null) { 308 throw new SecurityException( 309 "Permission Denial: Accessing service " + r.record.name 310 + " from pid=" + Binder.getCallingPid() 311 + ", uid=" + Binder.getCallingUid() 312 + " requires " + r.permission); 313 } 314 IntentBindRecord ib = r.record.bindings.get(r.record.intent); 315 if (ib != null) { 316 ret = ib.binder; 317 } 318 } 319 320 return ret; 321 } 322 323 boolean stopServiceTokenLocked(ComponentName className, IBinder token, 324 int startId) { 325 if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className 326 + " " + token + " startId=" + startId); 327 ServiceRecord r = findServiceLocked(className, token, UserHandle.getCallingUserId()); 328 if (r != null) { 329 if (startId >= 0) { 330 // Asked to only stop if done with all work. Note that 331 // to avoid leaks, we will take this as dropping all 332 // start items up to and including this one. 333 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); 334 if (si != null) { 335 while (r.deliveredStarts.size() > 0) { 336 ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); 337 cur.removeUriPermissionsLocked(); 338 if (cur == si) { 339 break; 340 } 341 } 342 } 343 344 if (r.getLastStartId() != startId) { 345 return false; 346 } 347 348 if (r.deliveredStarts.size() > 0) { 349 Slog.w(TAG, "stopServiceToken startId " + startId 350 + " is last, but have " + r.deliveredStarts.size() 351 + " remaining args"); 352 } 353 } 354 355 synchronized (r.stats.getBatteryStats()) { 356 r.stats.stopRunningLocked(); 357 r.startRequested = false; 358 r.callStart = false; 359 } 360 final long origId = Binder.clearCallingIdentity(); 361 bringDownServiceLocked(r, false); 362 Binder.restoreCallingIdentity(origId); 363 return true; 364 } 365 return false; 366 } 367 368 public void setServiceForegroundLocked(ComponentName className, IBinder token, 369 int id, Notification notification, boolean removeNotification) { 370 final int userId = UserHandle.getCallingUserId(); 371 final long origId = Binder.clearCallingIdentity(); 372 try { 373 ServiceRecord r = findServiceLocked(className, token, userId); 374 if (r != null) { 375 if (id != 0) { 376 if (notification == null) { 377 throw new IllegalArgumentException("null notification"); 378 } 379 if (r.foregroundId != id) { 380 r.cancelNotification(); 381 r.foregroundId = id; 382 } 383 notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 384 r.foregroundNoti = notification; 385 r.isForeground = true; 386 r.postNotification(); 387 if (r.app != null) { 388 updateServiceForegroundLocked(r.app, true); 389 } 390 } else { 391 if (r.isForeground) { 392 r.isForeground = false; 393 if (r.app != null) { 394 mAm.updateLruProcessLocked(r.app, false, true); 395 updateServiceForegroundLocked(r.app, true); 396 } 397 } 398 if (removeNotification) { 399 r.cancelNotification(); 400 r.foregroundId = 0; 401 r.foregroundNoti = null; 402 } 403 } 404 } 405 } finally { 406 Binder.restoreCallingIdentity(origId); 407 } 408 } 409 410 private void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) { 411 boolean anyForeground = false; 412 for (ServiceRecord sr : proc.services) { 413 if (sr.isForeground) { 414 anyForeground = true; 415 break; 416 } 417 } 418 if (anyForeground != proc.foregroundServices) { 419 proc.foregroundServices = anyForeground; 420 if (oomAdj) { 421 mAm.updateOomAdjLocked(); 422 } 423 } 424 } 425 426 int bindServiceLocked(IApplicationThread caller, IBinder token, 427 Intent service, String resolvedType, 428 IServiceConnection connection, int flags, int userId) { 429 if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service 430 + " type=" + resolvedType + " conn=" + connection.asBinder() 431 + " flags=0x" + Integer.toHexString(flags)); 432 final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); 433 if (callerApp == null) { 434 throw new SecurityException( 435 "Unable to find app for caller " + caller 436 + " (pid=" + Binder.getCallingPid() 437 + ") when binding service " + service); 438 } 439 440 ActivityRecord activity = null; 441 if (token != null) { 442 activity = mAm.mMainStack.isInStackLocked(token); 443 if (activity == null) { 444 Slog.w(TAG, "Binding with unknown activity: " + token); 445 return 0; 446 } 447 } 448 449 int clientLabel = 0; 450 PendingIntent clientIntent = null; 451 452 if (callerApp.info.uid == Process.SYSTEM_UID) { 453 // Hacky kind of thing -- allow system stuff to tell us 454 // what they are, so we can report this elsewhere for 455 // others to know why certain services are running. 456 try { 457 clientIntent = (PendingIntent)service.getParcelableExtra( 458 Intent.EXTRA_CLIENT_INTENT); 459 } catch (RuntimeException e) { 460 } 461 if (clientIntent != null) { 462 clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); 463 if (clientLabel != 0) { 464 // There are no useful extras in the intent, trash them. 465 // System code calling with this stuff just needs to know 466 // this will happen. 467 service = service.cloneFilter(); 468 } 469 } 470 } 471 472 ServiceLookupResult res = 473 retrieveServiceLocked(service, resolvedType, 474 Binder.getCallingPid(), Binder.getCallingUid(), userId, true); 475 if (res == null) { 476 return 0; 477 } 478 if (res.record == null) { 479 return -1; 480 } 481 if (mAm.isSingleton(res.record.processName, res.record.appInfo, 482 res.record.serviceInfo.name, res.record.serviceInfo.flags)) { 483 userId = 0; 484 res = retrieveServiceLocked(service, resolvedType, Binder.getCallingPid(), 485 Binder.getCallingUid(), 0, true); 486 } 487 ServiceRecord s = res.record; 488 489 final long origId = Binder.clearCallingIdentity(); 490 491 try { 492 if (unscheduleServiceRestartLocked(s)) { 493 if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: " 494 + s); 495 } 496 497 AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); 498 ConnectionRecord c = new ConnectionRecord(b, activity, 499 connection, flags, clientLabel, clientIntent); 500 501 IBinder binder = connection.asBinder(); 502 ArrayList<ConnectionRecord> clist = s.connections.get(binder); 503 if (clist == null) { 504 clist = new ArrayList<ConnectionRecord>(); 505 s.connections.put(binder, clist); 506 } 507 clist.add(c); 508 b.connections.add(c); 509 if (activity != null) { 510 if (activity.connections == null) { 511 activity.connections = new HashSet<ConnectionRecord>(); 512 } 513 activity.connections.add(c); 514 } 515 b.client.connections.add(c); 516 if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { 517 b.client.hasAboveClient = true; 518 } 519 clist = mServiceConnections.get(binder); 520 if (clist == null) { 521 clist = new ArrayList<ConnectionRecord>(); 522 mServiceConnections.put(binder, clist); 523 } 524 clist.add(c); 525 526 if ((flags&Context.BIND_AUTO_CREATE) != 0) { 527 s.lastActivity = SystemClock.uptimeMillis(); 528 if (!bringUpServiceLocked(s, service.getFlags(), false)) { 529 return 0; 530 } 531 } 532 533 if (s.app != null) { 534 // This could have made the service more important. 535 mAm.updateOomAdjLocked(s.app); 536 } 537 538 if (DEBUG_SERVICE) Slog.v(TAG, "Bind " + s + " with " + b 539 + ": received=" + b.intent.received 540 + " apps=" + b.intent.apps.size() 541 + " doRebind=" + b.intent.doRebind); 542 543 if (s.app != null && b.intent.received) { 544 // Service is already running, so we can immediately 545 // publish the connection. 546 try { 547 c.conn.connected(s.name, b.intent.binder); 548 } catch (Exception e) { 549 Slog.w(TAG, "Failure sending service " + s.shortName 550 + " to connection " + c.conn.asBinder() 551 + " (in " + c.binding.client.processName + ")", e); 552 } 553 554 // If this is the first app connected back to this binding, 555 // and the service had previously asked to be told when 556 // rebound, then do so. 557 if (b.intent.apps.size() == 1 && b.intent.doRebind) { 558 requestServiceBindingLocked(s, b.intent, true); 559 } 560 } else if (!b.intent.requested) { 561 requestServiceBindingLocked(s, b.intent, false); 562 } 563 } finally { 564 Binder.restoreCallingIdentity(origId); 565 } 566 567 return 1; 568 } 569 570 void publishServiceLocked(ServiceRecord r, Intent intent, IBinder service) { 571 final long origId = Binder.clearCallingIdentity(); 572 try { 573 if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r 574 + " " + intent + ": " + service); 575 if (r != null) { 576 Intent.FilterComparison filter 577 = new Intent.FilterComparison(intent); 578 IntentBindRecord b = r.bindings.get(filter); 579 if (b != null && !b.received) { 580 b.binder = service; 581 b.requested = true; 582 b.received = true; 583 if (r.connections.size() > 0) { 584 Iterator<ArrayList<ConnectionRecord>> it 585 = r.connections.values().iterator(); 586 while (it.hasNext()) { 587 ArrayList<ConnectionRecord> clist = it.next(); 588 for (int i=0; i<clist.size(); i++) { 589 ConnectionRecord c = clist.get(i); 590 if (!filter.equals(c.binding.intent.intent)) { 591 if (DEBUG_SERVICE) Slog.v( 592 TAG, "Not publishing to: " + c); 593 if (DEBUG_SERVICE) Slog.v( 594 TAG, "Bound intent: " + c.binding.intent.intent); 595 if (DEBUG_SERVICE) Slog.v( 596 TAG, "Published intent: " + intent); 597 continue; 598 } 599 if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c); 600 try { 601 c.conn.connected(r.name, service); 602 } catch (Exception e) { 603 Slog.w(TAG, "Failure sending service " + r.name + 604 " to connection " + c.conn.asBinder() + 605 " (in " + c.binding.client.processName + ")", e); 606 } 607 } 608 } 609 } 610 } 611 612 serviceDoneExecutingLocked(r, mStoppingServices.contains(r)); 613 } 614 } finally { 615 Binder.restoreCallingIdentity(origId); 616 } 617 } 618 619 boolean unbindServiceLocked(IServiceConnection connection) { 620 IBinder binder = connection.asBinder(); 621 if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder); 622 ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder); 623 if (clist == null) { 624 Slog.w(TAG, "Unbind failed: could not find connection for " 625 + connection.asBinder()); 626 return false; 627 } 628 629 final long origId = Binder.clearCallingIdentity(); 630 try { 631 while (clist.size() > 0) { 632 ConnectionRecord r = clist.get(0); 633 removeConnectionLocked(r, null, null); 634 635 if (r.binding.service.app != null) { 636 // This could have made the service less important. 637 mAm.updateOomAdjLocked(r.binding.service.app); 638 } 639 } 640 } finally { 641 Binder.restoreCallingIdentity(origId); 642 } 643 644 return true; 645 } 646 647 void unbindFinishedLocked(ServiceRecord r, Intent intent, boolean doRebind) { 648 final long origId = Binder.clearCallingIdentity(); 649 try { 650 if (r != null) { 651 Intent.FilterComparison filter 652 = new Intent.FilterComparison(intent); 653 IntentBindRecord b = r.bindings.get(filter); 654 if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r 655 + " at " + b + ": apps=" 656 + (b != null ? b.apps.size() : 0)); 657 658 boolean inStopping = mStoppingServices.contains(r); 659 if (b != null) { 660 if (b.apps.size() > 0 && !inStopping) { 661 // Applications have already bound since the last 662 // unbind, so just rebind right here. 663 requestServiceBindingLocked(r, b, true); 664 } else { 665 // Note to tell the service the next time there is 666 // a new client. 667 b.doRebind = true; 668 } 669 } 670 671 serviceDoneExecutingLocked(r, inStopping); 672 } 673 } finally { 674 Binder.restoreCallingIdentity(origId); 675 } 676 } 677 678 private final ServiceRecord findServiceLocked(ComponentName name, 679 IBinder token, int userId) { 680 ServiceRecord r = mServiceMap.getServiceByName(name, userId); 681 return r == token ? r : null; 682 } 683 684 private final class ServiceLookupResult { 685 final ServiceRecord record; 686 final String permission; 687 688 ServiceLookupResult(ServiceRecord _record, String _permission) { 689 record = _record; 690 permission = _permission; 691 } 692 } 693 694 private class ServiceRestarter implements Runnable { 695 private ServiceRecord mService; 696 697 void setService(ServiceRecord service) { 698 mService = service; 699 } 700 701 public void run() { 702 synchronized(mAm) { 703 performServiceRestartLocked(mService); 704 } 705 } 706 } 707 708 private ServiceLookupResult retrieveServiceLocked(Intent service, 709 String resolvedType, int callingPid, int callingUid, int userId, 710 boolean createIfNeeded) { 711 ServiceRecord r = null; 712 if (DEBUG_SERVICE) Slog.v(TAG, "retrieveServiceLocked: " + service 713 + " type=" + resolvedType + " callingUid=" + callingUid); 714 715 userId = mAm.handleIncomingUserLocked(callingPid, callingUid, userId, 716 false, true, "service", null); 717 718 if (service.getComponent() != null) { 719 r = mServiceMap.getServiceByName(service.getComponent(), userId); 720 } 721 if (r == null) { 722 Intent.FilterComparison filter = new Intent.FilterComparison(service); 723 r = mServiceMap.getServiceByIntent(filter, userId); 724 } 725 if (r == null) { 726 try { 727 ResolveInfo rInfo = 728 AppGlobals.getPackageManager().resolveService( 729 service, resolvedType, 730 ActivityManagerService.STOCK_PM_FLAGS, userId); 731 ServiceInfo sInfo = 732 rInfo != null ? rInfo.serviceInfo : null; 733 if (sInfo == null) { 734 Slog.w(TAG, "Unable to start service " + service + " U=" + userId + 735 ": not found"); 736 return null; 737 } 738 ComponentName name = new ComponentName( 739 sInfo.applicationInfo.packageName, sInfo.name); 740 if (userId > 0) { 741 if (mAm.isSingleton(sInfo.processName, sInfo.applicationInfo, 742 sInfo.name, sInfo.flags)) { 743 userId = 0; 744 } 745 sInfo = new ServiceInfo(sInfo); 746 sInfo.applicationInfo = mAm.getAppInfoForUser(sInfo.applicationInfo, userId); 747 } 748 r = mServiceMap.getServiceByName(name, userId); 749 if (r == null && createIfNeeded) { 750 Intent.FilterComparison filter = new Intent.FilterComparison( 751 service.cloneFilter()); 752 ServiceRestarter res = new ServiceRestarter(); 753 BatteryStatsImpl.Uid.Pkg.Serv ss = null; 754 BatteryStatsImpl stats = mAm.mBatteryStatsService.getActiveStatistics(); 755 synchronized (stats) { 756 ss = stats.getServiceStatsLocked( 757 sInfo.applicationInfo.uid, sInfo.packageName, 758 sInfo.name); 759 } 760 r = new ServiceRecord(mAm, ss, name, filter, sInfo, res); 761 res.setService(r); 762 mServiceMap.putServiceByName(name, UserHandle.getUserId(r.appInfo.uid), r); 763 mServiceMap.putServiceByIntent(filter, UserHandle.getUserId(r.appInfo.uid), r); 764 765 // Make sure this component isn't in the pending list. 766 int N = mPendingServices.size(); 767 for (int i=0; i<N; i++) { 768 ServiceRecord pr = mPendingServices.get(i); 769 if (pr.name.equals(name)) { 770 mPendingServices.remove(i); 771 i--; 772 N--; 773 } 774 } 775 } 776 } catch (RemoteException ex) { 777 // pm is in same process, this will never happen. 778 } 779 } 780 if (r != null) { 781 if (mAm.checkComponentPermission(r.permission, 782 callingPid, callingUid, r.appInfo.uid, r.exported) 783 != PackageManager.PERMISSION_GRANTED) { 784 if (!r.exported) { 785 Slog.w(TAG, "Permission Denial: Accessing service " + r.name 786 + " from pid=" + callingPid 787 + ", uid=" + callingUid 788 + " that is not exported from uid " + r.appInfo.uid); 789 return new ServiceLookupResult(null, "not exported from uid " 790 + r.appInfo.uid); 791 } 792 Slog.w(TAG, "Permission Denial: Accessing service " + r.name 793 + " from pid=" + callingPid 794 + ", uid=" + callingUid 795 + " requires " + r.permission); 796 return new ServiceLookupResult(null, r.permission); 797 } 798 return new ServiceLookupResult(r, null); 799 } 800 return null; 801 } 802 803 private final void bumpServiceExecutingLocked(ServiceRecord r, String why) { 804 if (DEBUG_SERVICE) Log.v(TAG, ">>> EXECUTING " 805 + why + " of " + r + " in app " + r.app); 806 else if (DEBUG_SERVICE_EXECUTING) Log.v(TAG, ">>> EXECUTING " 807 + why + " of " + r.shortName); 808 long now = SystemClock.uptimeMillis(); 809 if (r.executeNesting == 0 && r.app != null) { 810 if (r.app.executingServices.size() == 0) { 811 Message msg = mAm.mHandler.obtainMessage( 812 ActivityManagerService.SERVICE_TIMEOUT_MSG); 813 msg.obj = r.app; 814 mAm.mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT); 815 } 816 r.app.executingServices.add(r); 817 } 818 r.executeNesting++; 819 r.executingStart = now; 820 } 821 822 private final boolean requestServiceBindingLocked(ServiceRecord r, 823 IntentBindRecord i, boolean rebind) { 824 if (r.app == null || r.app.thread == null) { 825 // If service is not currently running, can't yet bind. 826 return false; 827 } 828 if ((!i.requested || rebind) && i.apps.size() > 0) { 829 try { 830 bumpServiceExecutingLocked(r, "bind"); 831 r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind); 832 if (!rebind) { 833 i.requested = true; 834 } 835 i.hasBound = true; 836 i.doRebind = false; 837 } catch (RemoteException e) { 838 if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while binding " + r); 839 return false; 840 } 841 } 842 return true; 843 } 844 845 private final boolean scheduleServiceRestartLocked(ServiceRecord r, 846 boolean allowCancel) { 847 boolean canceled = false; 848 849 final long now = SystemClock.uptimeMillis(); 850 851 if ((r.serviceInfo.applicationInfo.flags 852 &ApplicationInfo.FLAG_PERSISTENT) == 0) { 853 long minDuration = SERVICE_RESTART_DURATION; 854 long resetTime = SERVICE_RESET_RUN_DURATION; 855 856 // Any delivered but not yet finished starts should be put back 857 // on the pending list. 858 final int N = r.deliveredStarts.size(); 859 if (N > 0) { 860 for (int i=N-1; i>=0; i--) { 861 ServiceRecord.StartItem si = r.deliveredStarts.get(i); 862 si.removeUriPermissionsLocked(); 863 if (si.intent == null) { 864 // We'll generate this again if needed. 865 } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT 866 && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) { 867 r.pendingStarts.add(0, si); 868 long dur = SystemClock.uptimeMillis() - si.deliveredTime; 869 dur *= 2; 870 if (minDuration < dur) minDuration = dur; 871 if (resetTime < dur) resetTime = dur; 872 } else { 873 Slog.w(TAG, "Canceling start item " + si.intent + " in service " 874 + r.name); 875 canceled = true; 876 } 877 } 878 r.deliveredStarts.clear(); 879 } 880 881 r.totalRestartCount++; 882 if (r.restartDelay == 0) { 883 r.restartCount++; 884 r.restartDelay = minDuration; 885 } else { 886 // If it has been a "reasonably long time" since the service 887 // was started, then reset our restart duration back to 888 // the beginning, so we don't infinitely increase the duration 889 // on a service that just occasionally gets killed (which is 890 // a normal case, due to process being killed to reclaim memory). 891 if (now > (r.restartTime+resetTime)) { 892 r.restartCount = 1; 893 r.restartDelay = minDuration; 894 } else { 895 if ((r.serviceInfo.applicationInfo.flags 896 &ApplicationInfo.FLAG_PERSISTENT) != 0) { 897 // Services in peristent processes will restart much more 898 // quickly, since they are pretty important. (Think SystemUI). 899 r.restartDelay += minDuration/2; 900 } else { 901 r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR; 902 if (r.restartDelay < minDuration) { 903 r.restartDelay = minDuration; 904 } 905 } 906 } 907 } 908 909 r.nextRestartTime = now + r.restartDelay; 910 911 // Make sure that we don't end up restarting a bunch of services 912 // all at the same time. 913 boolean repeat; 914 do { 915 repeat = false; 916 for (int i=mRestartingServices.size()-1; i>=0; i--) { 917 ServiceRecord r2 = mRestartingServices.get(i); 918 if (r2 != r && r.nextRestartTime 919 >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN) 920 && r.nextRestartTime 921 < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) { 922 r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN; 923 r.restartDelay = r.nextRestartTime - now; 924 repeat = true; 925 break; 926 } 927 } 928 } while (repeat); 929 930 } else { 931 // Persistent processes are immediately restrted, so there is no 932 // reason to hold of on restarting their services. 933 r.totalRestartCount++; 934 r.restartCount = 0; 935 r.restartDelay = 0; 936 r.nextRestartTime = now; 937 } 938 939 if (!mRestartingServices.contains(r)) { 940 mRestartingServices.add(r); 941 } 942 943 r.cancelNotification(); 944 945 mAm.mHandler.removeCallbacks(r.restarter); 946 mAm.mHandler.postAtTime(r.restarter, r.nextRestartTime); 947 r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay; 948 Slog.w(TAG, "Scheduling restart of crashed service " 949 + r.shortName + " in " + r.restartDelay + "ms"); 950 EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART, 951 r.shortName, r.restartDelay); 952 953 return canceled; 954 } 955 956 final void performServiceRestartLocked(ServiceRecord r) { 957 if (!mRestartingServices.contains(r)) { 958 return; 959 } 960 bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true); 961 } 962 963 private final boolean unscheduleServiceRestartLocked(ServiceRecord r) { 964 if (r.restartDelay == 0) { 965 return false; 966 } 967 r.resetRestartCounter(); 968 mRestartingServices.remove(r); 969 mAm.mHandler.removeCallbacks(r.restarter); 970 return true; 971 } 972 973 private final boolean bringUpServiceLocked(ServiceRecord r, 974 int intentFlags, boolean whileRestarting) { 975 //Slog.i(TAG, "Bring up service:"); 976 //r.dump(" "); 977 978 if (r.app != null && r.app.thread != null) { 979 sendServiceArgsLocked(r, false); 980 return true; 981 } 982 983 if (!whileRestarting && r.restartDelay > 0) { 984 // If waiting for a restart, then do nothing. 985 return true; 986 } 987 988 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing up " + r + " " + r.intent); 989 990 // We are now bringing the service up, so no longer in the 991 // restarting state. 992 mRestartingServices.remove(r); 993 994 // Make sure that the user who owns this service is started. If not, 995 // we don't want to allow it to run. 996 if (mAm.mStartedUsers.get(r.userId) == null) { 997 Slog.w(TAG, "Unable to launch app " 998 + r.appInfo.packageName + "/" 999 + r.appInfo.uid + " for service " 1000 + r.intent.getIntent() + ": user " + r.userId + " is stopped"); 1001 bringDownServiceLocked(r, true); 1002 return false; 1003 } 1004 1005 // Service is now being launched, its package can't be stopped. 1006 try { 1007 AppGlobals.getPackageManager().setPackageStoppedState( 1008 r.packageName, false, r.userId); 1009 } catch (RemoteException e) { 1010 } catch (IllegalArgumentException e) { 1011 Slog.w(TAG, "Failed trying to unstop package " 1012 + r.packageName + ": " + e); 1013 } 1014 1015 final boolean isolated = (r.serviceInfo.flags&ServiceInfo.FLAG_ISOLATED_PROCESS) != 0; 1016 final String procName = r.processName; 1017 ProcessRecord app; 1018 1019 if (!isolated) { 1020 app = mAm.getProcessRecordLocked(procName, r.appInfo.uid); 1021 if (DEBUG_MU) 1022 Slog.v(TAG_MU, "bringUpServiceLocked: appInfo.uid=" + r.appInfo.uid + " app=" + app); 1023 if (app != null && app.thread != null) { 1024 try { 1025 app.addPackage(r.appInfo.packageName); 1026 realStartServiceLocked(r, app); 1027 return true; 1028 } catch (RemoteException e) { 1029 Slog.w(TAG, "Exception when starting service " + r.shortName, e); 1030 } 1031 1032 // If a dead object exception was thrown -- fall through to 1033 // restart the application. 1034 } 1035 } else { 1036 // If this service runs in an isolated process, then each time 1037 // we call startProcessLocked() we will get a new isolated 1038 // process, starting another process if we are currently waiting 1039 // for a previous process to come up. To deal with this, we store 1040 // in the service any current isolated process it is running in or 1041 // waiting to have come up. 1042 app = r.isolatedProc; 1043 } 1044 1045 // Not running -- get it started, and enqueue this service record 1046 // to be executed when the app comes up. 1047 if (app == null) { 1048 if ((app=mAm.startProcessLocked(procName, r.appInfo, true, intentFlags, 1049 "service", r.name, false, isolated)) == null) { 1050 Slog.w(TAG, "Unable to launch app " 1051 + r.appInfo.packageName + "/" 1052 + r.appInfo.uid + " for service " 1053 + r.intent.getIntent() + ": process is bad"); 1054 bringDownServiceLocked(r, true); 1055 return false; 1056 } 1057 if (isolated) { 1058 r.isolatedProc = app; 1059 } 1060 } 1061 1062 if (!mPendingServices.contains(r)) { 1063 mPendingServices.add(r); 1064 } 1065 1066 return true; 1067 } 1068 1069 private final void requestServiceBindingsLocked(ServiceRecord r) { 1070 Iterator<IntentBindRecord> bindings = r.bindings.values().iterator(); 1071 while (bindings.hasNext()) { 1072 IntentBindRecord i = bindings.next(); 1073 if (!requestServiceBindingLocked(r, i, false)) { 1074 break; 1075 } 1076 } 1077 } 1078 1079 private final void realStartServiceLocked(ServiceRecord r, 1080 ProcessRecord app) throws RemoteException { 1081 if (app.thread == null) { 1082 throw new RemoteException(); 1083 } 1084 if (DEBUG_MU) 1085 Slog.v(TAG_MU, "realStartServiceLocked, ServiceRecord.uid = " + r.appInfo.uid 1086 + ", ProcessRecord.uid = " + app.uid); 1087 r.app = app; 1088 r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); 1089 1090 app.services.add(r); 1091 bumpServiceExecutingLocked(r, "create"); 1092 mAm.updateLruProcessLocked(app, true, true); 1093 1094 boolean created = false; 1095 try { 1096 mAm.mStringBuilder.setLength(0); 1097 r.intent.getIntent().toShortString(mAm.mStringBuilder, true, false, true, false); 1098 EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE, 1099 System.identityHashCode(r), r.shortName, 1100 mAm.mStringBuilder.toString(), r.app.pid); 1101 synchronized (r.stats.getBatteryStats()) { 1102 r.stats.startLaunchedLocked(); 1103 } 1104 mAm.ensurePackageDexOpt(r.serviceInfo.packageName); 1105 app.thread.scheduleCreateService(r, r.serviceInfo, 1106 mAm.compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo)); 1107 r.postNotification(); 1108 created = true; 1109 } finally { 1110 if (!created) { 1111 app.services.remove(r); 1112 scheduleServiceRestartLocked(r, false); 1113 } 1114 } 1115 1116 requestServiceBindingsLocked(r); 1117 1118 // If the service is in the started state, and there are no 1119 // pending arguments, then fake up one so its onStartCommand() will 1120 // be called. 1121 if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { 1122 r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), 1123 null, null)); 1124 } 1125 1126 sendServiceArgsLocked(r, true); 1127 } 1128 1129 private final void sendServiceArgsLocked(ServiceRecord r, 1130 boolean oomAdjusted) { 1131 final int N = r.pendingStarts.size(); 1132 if (N == 0) { 1133 return; 1134 } 1135 1136 while (r.pendingStarts.size() > 0) { 1137 try { 1138 ServiceRecord.StartItem si = r.pendingStarts.remove(0); 1139 if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: " 1140 + r + " " + r.intent + " args=" + si.intent); 1141 if (si.intent == null && N > 1) { 1142 // If somehow we got a dummy null intent in the middle, 1143 // then skip it. DO NOT skip a null intent when it is 1144 // the only one in the list -- this is to support the 1145 // onStartCommand(null) case. 1146 continue; 1147 } 1148 si.deliveredTime = SystemClock.uptimeMillis(); 1149 r.deliveredStarts.add(si); 1150 si.deliveryCount++; 1151 if (si.neededGrants != null) { 1152 mAm.grantUriPermissionUncheckedFromIntentLocked(si.neededGrants, 1153 si.getUriPermissionsLocked()); 1154 } 1155 bumpServiceExecutingLocked(r, "start"); 1156 if (!oomAdjusted) { 1157 oomAdjusted = true; 1158 mAm.updateOomAdjLocked(r.app); 1159 } 1160 int flags = 0; 1161 if (si.deliveryCount > 1) { 1162 flags |= Service.START_FLAG_RETRY; 1163 } 1164 if (si.doneExecutingCount > 0) { 1165 flags |= Service.START_FLAG_REDELIVERY; 1166 } 1167 r.app.thread.scheduleServiceArgs(r, si.taskRemoved, si.id, flags, si.intent); 1168 } catch (RemoteException e) { 1169 // Remote process gone... we'll let the normal cleanup take 1170 // care of this. 1171 if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r); 1172 break; 1173 } catch (Exception e) { 1174 Slog.w(TAG, "Unexpected exception", e); 1175 break; 1176 } 1177 } 1178 } 1179 1180 private final void bringDownServiceLocked(ServiceRecord r, boolean force) { 1181 //Slog.i(TAG, "Bring down service:"); 1182 //r.dump(" "); 1183 1184 // Does it still need to run? 1185 if (!force && r.startRequested) { 1186 return; 1187 } 1188 if (r.connections.size() > 0) { 1189 if (!force) { 1190 // XXX should probably keep a count of the number of auto-create 1191 // connections directly in the service. 1192 Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); 1193 while (it.hasNext()) { 1194 ArrayList<ConnectionRecord> cr = it.next(); 1195 for (int i=0; i<cr.size(); i++) { 1196 if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) { 1197 return; 1198 } 1199 } 1200 } 1201 } 1202 1203 // Report to all of the connections that the service is no longer 1204 // available. 1205 Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); 1206 while (it.hasNext()) { 1207 ArrayList<ConnectionRecord> c = it.next(); 1208 for (int i=0; i<c.size(); i++) { 1209 ConnectionRecord cr = c.get(i); 1210 // There is still a connection to the service that is 1211 // being brought down. Mark it as dead. 1212 cr.serviceDead = true; 1213 try { 1214 cr.conn.connected(r.name, null); 1215 } catch (Exception e) { 1216 Slog.w(TAG, "Failure disconnecting service " + r.name + 1217 " to connection " + c.get(i).conn.asBinder() + 1218 " (in " + c.get(i).binding.client.processName + ")", e); 1219 } 1220 } 1221 } 1222 } 1223 1224 // Tell the service that it has been unbound. 1225 if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) { 1226 Iterator<IntentBindRecord> it = r.bindings.values().iterator(); 1227 while (it.hasNext()) { 1228 IntentBindRecord ibr = it.next(); 1229 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down binding " + ibr 1230 + ": hasBound=" + ibr.hasBound); 1231 if (r.app != null && r.app.thread != null && ibr.hasBound) { 1232 try { 1233 bumpServiceExecutingLocked(r, "bring down unbind"); 1234 mAm.updateOomAdjLocked(r.app); 1235 ibr.hasBound = false; 1236 r.app.thread.scheduleUnbindService(r, 1237 ibr.intent.getIntent()); 1238 } catch (Exception e) { 1239 Slog.w(TAG, "Exception when unbinding service " 1240 + r.shortName, e); 1241 serviceDoneExecutingLocked(r, true); 1242 } 1243 } 1244 } 1245 } 1246 1247 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent); 1248 EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE, 1249 System.identityHashCode(r), r.shortName, 1250 (r.app != null) ? r.app.pid : -1); 1251 1252 mServiceMap.removeServiceByName(r.name, r.userId); 1253 mServiceMap.removeServiceByIntent(r.intent, r.userId); 1254 r.totalRestartCount = 0; 1255 unscheduleServiceRestartLocked(r); 1256 1257 // Also make sure it is not on the pending list. 1258 int N = mPendingServices.size(); 1259 for (int i=0; i<N; i++) { 1260 if (mPendingServices.get(i) == r) { 1261 mPendingServices.remove(i); 1262 if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r); 1263 i--; 1264 N--; 1265 } 1266 } 1267 1268 r.cancelNotification(); 1269 r.isForeground = false; 1270 r.foregroundId = 0; 1271 r.foregroundNoti = null; 1272 1273 // Clear start entries. 1274 r.clearDeliveredStartsLocked(); 1275 r.pendingStarts.clear(); 1276 1277 if (r.app != null) { 1278 synchronized (r.stats.getBatteryStats()) { 1279 r.stats.stopLaunchedLocked(); 1280 } 1281 r.app.services.remove(r); 1282 if (r.app.thread != null) { 1283 try { 1284 bumpServiceExecutingLocked(r, "stop"); 1285 mStoppingServices.add(r); 1286 mAm.updateOomAdjLocked(r.app); 1287 r.app.thread.scheduleStopService(r); 1288 } catch (Exception e) { 1289 Slog.w(TAG, "Exception when stopping service " 1290 + r.shortName, e); 1291 serviceDoneExecutingLocked(r, true); 1292 } 1293 updateServiceForegroundLocked(r.app, false); 1294 } else { 1295 if (DEBUG_SERVICE) Slog.v( 1296 TAG, "Removed service that has no process: " + r); 1297 } 1298 } else { 1299 if (DEBUG_SERVICE) Slog.v( 1300 TAG, "Removed service that is not running: " + r); 1301 } 1302 1303 if (r.bindings.size() > 0) { 1304 r.bindings.clear(); 1305 } 1306 1307 if (r.restarter instanceof ServiceRestarter) { 1308 ((ServiceRestarter)r.restarter).setService(null); 1309 } 1310 } 1311 1312 void removeConnectionLocked( 1313 ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { 1314 IBinder binder = c.conn.asBinder(); 1315 AppBindRecord b = c.binding; 1316 ServiceRecord s = b.service; 1317 ArrayList<ConnectionRecord> clist = s.connections.get(binder); 1318 if (clist != null) { 1319 clist.remove(c); 1320 if (clist.size() == 0) { 1321 s.connections.remove(binder); 1322 } 1323 } 1324 b.connections.remove(c); 1325 if (c.activity != null && c.activity != skipAct) { 1326 if (c.activity.connections != null) { 1327 c.activity.connections.remove(c); 1328 } 1329 } 1330 if (b.client != skipApp) { 1331 b.client.connections.remove(c); 1332 if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { 1333 b.client.updateHasAboveClientLocked(); 1334 } 1335 } 1336 clist = mServiceConnections.get(binder); 1337 if (clist != null) { 1338 clist.remove(c); 1339 if (clist.size() == 0) { 1340 mServiceConnections.remove(binder); 1341 } 1342 } 1343 1344 if (b.connections.size() == 0) { 1345 b.intent.apps.remove(b.client); 1346 } 1347 1348 if (!c.serviceDead) { 1349 if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent 1350 + ": shouldUnbind=" + b.intent.hasBound); 1351 if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 1352 && b.intent.hasBound) { 1353 try { 1354 bumpServiceExecutingLocked(s, "unbind"); 1355 mAm.updateOomAdjLocked(s.app); 1356 b.intent.hasBound = false; 1357 // Assume the client doesn't want to know about a rebind; 1358 // we will deal with that later if it asks for one. 1359 b.intent.doRebind = false; 1360 s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); 1361 } catch (Exception e) { 1362 Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); 1363 serviceDoneExecutingLocked(s, true); 1364 } 1365 } 1366 1367 if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { 1368 bringDownServiceLocked(s, false); 1369 } 1370 } 1371 } 1372 1373 void serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res) { 1374 boolean inStopping = mStoppingServices.contains(r); 1375 if (r != null) { 1376 if (type == 1) { 1377 // This is a call from a service start... take care of 1378 // book-keeping. 1379 r.callStart = true; 1380 switch (res) { 1381 case Service.START_STICKY_COMPATIBILITY: 1382 case Service.START_STICKY: { 1383 // We are done with the associated start arguments. 1384 r.findDeliveredStart(startId, true); 1385 // Don't stop if killed. 1386 r.stopIfKilled = false; 1387 break; 1388 } 1389 case Service.START_NOT_STICKY: { 1390 // We are done with the associated start arguments. 1391 r.findDeliveredStart(startId, true); 1392 if (r.getLastStartId() == startId) { 1393 // There is no more work, and this service 1394 // doesn't want to hang around if killed. 1395 r.stopIfKilled = true; 1396 } 1397 break; 1398 } 1399 case Service.START_REDELIVER_INTENT: { 1400 // We'll keep this item until they explicitly 1401 // call stop for it, but keep track of the fact 1402 // that it was delivered. 1403 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); 1404 if (si != null) { 1405 si.deliveryCount = 0; 1406 si.doneExecutingCount++; 1407 // Don't stop if killed. 1408 r.stopIfKilled = true; 1409 } 1410 break; 1411 } 1412 case Service.START_TASK_REMOVED_COMPLETE: { 1413 // Special processing for onTaskRemoved(). Don't 1414 // impact normal onStartCommand() processing. 1415 r.findDeliveredStart(startId, true); 1416 break; 1417 } 1418 default: 1419 throw new IllegalArgumentException( 1420 "Unknown service start result: " + res); 1421 } 1422 if (res == Service.START_STICKY_COMPATIBILITY) { 1423 r.callStart = false; 1424 } 1425 } 1426 final long origId = Binder.clearCallingIdentity(); 1427 serviceDoneExecutingLocked(r, inStopping); 1428 Binder.restoreCallingIdentity(origId); 1429 } else { 1430 Slog.w(TAG, "Done executing unknown service from pid " 1431 + Binder.getCallingPid()); 1432 } 1433 } 1434 1435 private void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) { 1436 if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r 1437 + ": nesting=" + r.executeNesting 1438 + ", inStopping=" + inStopping + ", app=" + r.app); 1439 else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG, "<<< DONE EXECUTING " + r.shortName); 1440 r.executeNesting--; 1441 if (r.executeNesting <= 0 && r.app != null) { 1442 if (DEBUG_SERVICE) Slog.v(TAG, 1443 "Nesting at 0 of " + r.shortName); 1444 r.app.executingServices.remove(r); 1445 if (r.app.executingServices.size() == 0) { 1446 if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG, 1447 "No more executingServices of " + r.shortName); 1448 mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_TIMEOUT_MSG, r.app); 1449 } 1450 if (inStopping) { 1451 if (DEBUG_SERVICE) Slog.v(TAG, 1452 "doneExecuting remove stopping " + r); 1453 mStoppingServices.remove(r); 1454 r.bindings.clear(); 1455 } 1456 mAm.updateOomAdjLocked(r.app); 1457 } 1458 } 1459 1460 boolean attachApplicationLocked(ProcessRecord proc, String processName) throws Exception { 1461 boolean didSomething = false; 1462 // Collect any services that are waiting for this process to come up. 1463 if (mPendingServices.size() > 0) { 1464 ServiceRecord sr = null; 1465 try { 1466 for (int i=0; i<mPendingServices.size(); i++) { 1467 sr = mPendingServices.get(i); 1468 if (proc != sr.isolatedProc && (proc.uid != sr.appInfo.uid 1469 || !processName.equals(sr.processName))) { 1470 continue; 1471 } 1472 1473 mPendingServices.remove(i); 1474 i--; 1475 realStartServiceLocked(sr, proc); 1476 didSomething = true; 1477 } 1478 } catch (Exception e) { 1479 Slog.w(TAG, "Exception in new application when starting service " 1480 + sr.shortName, e); 1481 throw e; 1482 } 1483 } 1484 // Also, if there are any services that are waiting to restart and 1485 // would run in this process, now is a good time to start them. It would 1486 // be weird to bring up the process but arbitrarily not let the services 1487 // run at this point just because their restart time hasn't come up. 1488 if (mRestartingServices.size() > 0) { 1489 ServiceRecord sr = null; 1490 for (int i=0; i<mRestartingServices.size(); i++) { 1491 sr = mRestartingServices.get(i); 1492 if (proc != sr.isolatedProc && (proc.uid != sr.appInfo.uid 1493 || !processName.equals(sr.processName))) { 1494 continue; 1495 } 1496 mAm.mHandler.removeCallbacks(sr.restarter); 1497 mAm.mHandler.post(sr.restarter); 1498 } 1499 } 1500 return didSomething; 1501 } 1502 1503 void processStartTimedOutLocked(ProcessRecord proc) { 1504 for (int i=0; i<mPendingServices.size(); i++) { 1505 ServiceRecord sr = mPendingServices.get(i); 1506 if ((proc.uid == sr.appInfo.uid 1507 && proc.processName.equals(sr.processName)) 1508 || sr.isolatedProc == proc) { 1509 Slog.w(TAG, "Forcing bringing down service: " + sr); 1510 sr.isolatedProc = null; 1511 mPendingServices.remove(i); 1512 i--; 1513 bringDownServiceLocked(sr, true); 1514 } 1515 } 1516 } 1517 1518 private boolean collectForceStopServicesLocked(String name, int userId, 1519 boolean evenPersistent, boolean doit, 1520 HashMap<ComponentName, ServiceRecord> services, 1521 ArrayList<ServiceRecord> result) { 1522 boolean didSomething = false; 1523 for (ServiceRecord service : services.values()) { 1524 if ((name == null || service.packageName.equals(name)) 1525 && (service.app == null || evenPersistent || !service.app.persistent)) { 1526 if (!doit) { 1527 return true; 1528 } 1529 didSomething = true; 1530 Slog.i(TAG, " Force stopping service " + service); 1531 if (service.app != null) { 1532 service.app.removed = true; 1533 } 1534 service.app = null; 1535 service.isolatedProc = null; 1536 result.add(service); 1537 } 1538 } 1539 return didSomething; 1540 } 1541 1542 boolean forceStopLocked(String name, int userId, boolean evenPersistent, boolean doit) { 1543 boolean didSomething = false; 1544 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 1545 if (userId == UserHandle.USER_ALL) { 1546 for (int i=0; i<mServiceMap.mServicesByNamePerUser.size(); i++) { 1547 didSomething |= collectForceStopServicesLocked(name, userId, evenPersistent, 1548 doit, mServiceMap.mServicesByNamePerUser.valueAt(i), services); 1549 if (!doit && didSomething) { 1550 return true; 1551 } 1552 } 1553 } else { 1554 didSomething = collectForceStopServicesLocked(name, userId, evenPersistent, 1555 doit, mServiceMap.mServicesByNamePerUser.get(userId), services); 1556 } 1557 1558 int N = services.size(); 1559 for (int i=0; i<N; i++) { 1560 bringDownServiceLocked(services.get(i), true); 1561 } 1562 return didSomething; 1563 } 1564 1565 void cleanUpRemovedTaskLocked(TaskRecord tr, ComponentName component, Intent baseIntent) { 1566 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 1567 for (ServiceRecord sr : mServiceMap.getAllServices(tr.userId)) { 1568 if (sr.packageName.equals(component.getPackageName())) { 1569 services.add(sr); 1570 } 1571 } 1572 1573 // Take care of any running services associated with the app. 1574 for (int i=0; i<services.size(); i++) { 1575 ServiceRecord sr = services.get(i); 1576 if (sr.startRequested) { 1577 if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) { 1578 Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task"); 1579 stopServiceLocked(sr); 1580 } else { 1581 sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true, 1582 sr.makeNextStartId(), baseIntent, null)); 1583 if (sr.app != null && sr.app.thread != null) { 1584 sendServiceArgsLocked(sr, false); 1585 } 1586 } 1587 } 1588 } 1589 } 1590 1591 final void killServicesLocked(ProcessRecord app, 1592 boolean allowRestart) { 1593 // Report disconnected services. 1594 if (false) { 1595 // XXX we are letting the client link to the service for 1596 // death notifications. 1597 if (app.services.size() > 0) { 1598 Iterator<ServiceRecord> it = app.services.iterator(); 1599 while (it.hasNext()) { 1600 ServiceRecord r = it.next(); 1601 if (r.connections.size() > 0) { 1602 Iterator<ArrayList<ConnectionRecord>> jt 1603 = r.connections.values().iterator(); 1604 while (jt.hasNext()) { 1605 ArrayList<ConnectionRecord> cl = jt.next(); 1606 for (int i=0; i<cl.size(); i++) { 1607 ConnectionRecord c = cl.get(i); 1608 if (c.binding.client != app) { 1609 try { 1610 //c.conn.connected(r.className, null); 1611 } catch (Exception e) { 1612 // todo: this should be asynchronous! 1613 Slog.w(TAG, "Exception thrown disconnected servce " 1614 + r.shortName 1615 + " from app " + app.processName, e); 1616 } 1617 } 1618 } 1619 } 1620 } 1621 } 1622 } 1623 } 1624 1625 // Clean up any connections this application has to other services. 1626 if (app.connections.size() > 0) { 1627 Iterator<ConnectionRecord> it = app.connections.iterator(); 1628 while (it.hasNext()) { 1629 ConnectionRecord r = it.next(); 1630 removeConnectionLocked(r, app, null); 1631 } 1632 } 1633 app.connections.clear(); 1634 1635 if (app.services.size() != 0) { 1636 // Any services running in the application need to be placed 1637 // back in the pending list. 1638 Iterator<ServiceRecord> it = app.services.iterator(); 1639 while (it.hasNext()) { 1640 ServiceRecord sr = it.next(); 1641 synchronized (sr.stats.getBatteryStats()) { 1642 sr.stats.stopLaunchedLocked(); 1643 } 1644 sr.app = null; 1645 sr.isolatedProc = null; 1646 sr.executeNesting = 0; 1647 if (mStoppingServices.remove(sr)) { 1648 if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); 1649 } 1650 1651 boolean hasClients = sr.bindings.size() > 0; 1652 if (hasClients) { 1653 Iterator<IntentBindRecord> bindings 1654 = sr.bindings.values().iterator(); 1655 while (bindings.hasNext()) { 1656 IntentBindRecord b = bindings.next(); 1657 if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b 1658 + ": shouldUnbind=" + b.hasBound); 1659 b.binder = null; 1660 b.requested = b.received = b.hasBound = false; 1661 } 1662 } 1663 1664 if (sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags 1665 &ApplicationInfo.FLAG_PERSISTENT) == 0) { 1666 Slog.w(TAG, "Service crashed " + sr.crashCount 1667 + " times, stopping: " + sr); 1668 EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, 1669 sr.crashCount, sr.shortName, app.pid); 1670 bringDownServiceLocked(sr, true); 1671 } else if (!allowRestart) { 1672 bringDownServiceLocked(sr, true); 1673 } else { 1674 boolean canceled = scheduleServiceRestartLocked(sr, true); 1675 1676 // Should the service remain running? Note that in the 1677 // extreme case of so many attempts to deliver a command 1678 // that it failed we also will stop it here. 1679 if (sr.startRequested && (sr.stopIfKilled || canceled)) { 1680 if (sr.pendingStarts.size() == 0) { 1681 sr.startRequested = false; 1682 if (!hasClients) { 1683 // Whoops, no reason to restart! 1684 bringDownServiceLocked(sr, true); 1685 } 1686 } 1687 } 1688 } 1689 } 1690 1691 if (!allowRestart) { 1692 app.services.clear(); 1693 } 1694 } 1695 1696 // Make sure we have no more records on the stopping list. 1697 int i = mStoppingServices.size(); 1698 while (i > 0) { 1699 i--; 1700 ServiceRecord sr = mStoppingServices.get(i); 1701 if (sr.app == app) { 1702 mStoppingServices.remove(i); 1703 if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); 1704 } 1705 } 1706 1707 app.executingServices.clear(); 1708 } 1709 1710 ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { 1711 ActivityManager.RunningServiceInfo info = 1712 new ActivityManager.RunningServiceInfo(); 1713 info.service = r.name; 1714 if (r.app != null) { 1715 info.pid = r.app.pid; 1716 } 1717 info.uid = r.appInfo.uid; 1718 info.process = r.processName; 1719 info.foreground = r.isForeground; 1720 info.activeSince = r.createTime; 1721 info.started = r.startRequested; 1722 info.clientCount = r.connections.size(); 1723 info.crashCount = r.crashCount; 1724 info.lastActivityTime = r.lastActivity; 1725 if (r.isForeground) { 1726 info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; 1727 } 1728 if (r.startRequested) { 1729 info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; 1730 } 1731 if (r.app != null && r.app.pid == ActivityManagerService.MY_PID) { 1732 info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; 1733 } 1734 if (r.app != null && r.app.persistent) { 1735 info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; 1736 } 1737 1738 for (ArrayList<ConnectionRecord> connl : r.connections.values()) { 1739 for (int i=0; i<connl.size(); i++) { 1740 ConnectionRecord conn = connl.get(i); 1741 if (conn.clientLabel != 0) { 1742 info.clientPackage = conn.binding.client.info.packageName; 1743 info.clientLabel = conn.clientLabel; 1744 return info; 1745 } 1746 } 1747 } 1748 return info; 1749 } 1750 1751 List<ActivityManager.RunningServiceInfo> getRunningServiceInfoLocked(int maxNum, 1752 int flags) { 1753 ArrayList<ActivityManager.RunningServiceInfo> res 1754 = new ArrayList<ActivityManager.RunningServiceInfo>(); 1755 1756 final int uid = Binder.getCallingUid(); 1757 final long ident = Binder.clearCallingIdentity(); 1758 try { 1759 if (ActivityManager.checkUidPermission( 1760 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, 1761 uid) == PackageManager.PERMISSION_GRANTED) { 1762 List<UserInfo> users = mAm.getUserManager().getUsers(); 1763 for (int ui=0; ui<users.size() && res.size() < maxNum; ui++) { 1764 final UserInfo user = users.get(ui); 1765 if (mServiceMap.getAllServices(user.id).size() > 0) { 1766 Iterator<ServiceRecord> it = mServiceMap.getAllServices( 1767 user.id).iterator(); 1768 while (it.hasNext() && res.size() < maxNum) { 1769 res.add(makeRunningServiceInfoLocked(it.next())); 1770 } 1771 } 1772 } 1773 1774 for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { 1775 ServiceRecord r = mRestartingServices.get(i); 1776 ActivityManager.RunningServiceInfo info = 1777 makeRunningServiceInfoLocked(r); 1778 info.restarting = r.nextRestartTime; 1779 res.add(info); 1780 } 1781 } else { 1782 int userId = UserHandle.getUserId(uid); 1783 if (mServiceMap.getAllServices(userId).size() > 0) { 1784 Iterator<ServiceRecord> it 1785 = mServiceMap.getAllServices(userId).iterator(); 1786 while (it.hasNext() && res.size() < maxNum) { 1787 res.add(makeRunningServiceInfoLocked(it.next())); 1788 } 1789 } 1790 1791 for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { 1792 ServiceRecord r = mRestartingServices.get(i); 1793 if (r.userId == userId) { 1794 ActivityManager.RunningServiceInfo info = 1795 makeRunningServiceInfoLocked(r); 1796 info.restarting = r.nextRestartTime; 1797 res.add(info); 1798 } 1799 } 1800 } 1801 } finally { 1802 Binder.restoreCallingIdentity(ident); 1803 } 1804 1805 return res; 1806 } 1807 1808 public PendingIntent getRunningServiceControlPanelLocked(ComponentName name) { 1809 int userId = UserHandle.getUserId(Binder.getCallingUid()); 1810 ServiceRecord r = mServiceMap.getServiceByName(name, userId); 1811 if (r != null) { 1812 for (ArrayList<ConnectionRecord> conn : r.connections.values()) { 1813 for (int i=0; i<conn.size(); i++) { 1814 if (conn.get(i).clientIntent != null) { 1815 return conn.get(i).clientIntent; 1816 } 1817 } 1818 } 1819 } 1820 return null; 1821 } 1822 1823 void serviceTimeout(ProcessRecord proc) { 1824 String anrMessage = null; 1825 1826 synchronized(this) { 1827 if (proc.executingServices.size() == 0 || proc.thread == null) { 1828 return; 1829 } 1830 long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT; 1831 Iterator<ServiceRecord> it = proc.executingServices.iterator(); 1832 ServiceRecord timeout = null; 1833 long nextTime = 0; 1834 while (it.hasNext()) { 1835 ServiceRecord sr = it.next(); 1836 if (sr.executingStart < maxTime) { 1837 timeout = sr; 1838 break; 1839 } 1840 if (sr.executingStart > nextTime) { 1841 nextTime = sr.executingStart; 1842 } 1843 } 1844 if (timeout != null && mAm.mLruProcesses.contains(proc)) { 1845 Slog.w(TAG, "Timeout executing service: " + timeout); 1846 anrMessage = "Executing service " + timeout.shortName; 1847 } else { 1848 Message msg = mAm.mHandler.obtainMessage( 1849 ActivityManagerService.SERVICE_TIMEOUT_MSG); 1850 msg.obj = proc; 1851 mAm.mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT); 1852 } 1853 } 1854 1855 if (anrMessage != null) { 1856 mAm.appNotResponding(proc, null, null, anrMessage); 1857 } 1858 } 1859 1860 /** 1861 * Prints a list of ServiceRecords (dumpsys activity services) 1862 */ 1863 boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args, 1864 int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { 1865 boolean needSep = false; 1866 1867 ItemMatcher matcher = new ItemMatcher(); 1868 matcher.build(args, opti); 1869 1870 pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)"); 1871 try { 1872 List<UserInfo> users = mAm.getUserManager().getUsers(); 1873 for (int ui=0; ui<users.size(); ui++) { 1874 final UserInfo user = users.get(ui); 1875 if (mServiceMap.getAllServices(user.id).size() > 0) { 1876 boolean printed = false; 1877 long nowReal = SystemClock.elapsedRealtime(); 1878 Iterator<ServiceRecord> it = mServiceMap.getAllServices( 1879 user.id).iterator(); 1880 needSep = false; 1881 while (it.hasNext()) { 1882 ServiceRecord r = it.next(); 1883 if (!matcher.match(r, r.name)) { 1884 continue; 1885 } 1886 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 1887 continue; 1888 } 1889 if (!printed) { 1890 if (ui > 0) { 1891 pw.println(); 1892 } 1893 pw.println(" User " + user.id + " active services:"); 1894 printed = true; 1895 } 1896 if (needSep) { 1897 pw.println(); 1898 } 1899 pw.print(" * "); 1900 pw.println(r); 1901 if (dumpAll) { 1902 r.dump(pw, " "); 1903 needSep = true; 1904 } else { 1905 pw.print(" app="); 1906 pw.println(r.app); 1907 pw.print(" created="); 1908 TimeUtils.formatDuration(r.createTime, nowReal, pw); 1909 pw.print(" started="); 1910 pw.print(r.startRequested); 1911 pw.print(" connections="); 1912 pw.println(r.connections.size()); 1913 if (r.connections.size() > 0) { 1914 pw.println(" Connections:"); 1915 for (ArrayList<ConnectionRecord> clist : r.connections.values()) { 1916 for (int i = 0; i < clist.size(); i++) { 1917 ConnectionRecord conn = clist.get(i); 1918 pw.print(" "); 1919 pw.print(conn.binding.intent.intent.getIntent() 1920 .toShortString(false, false, false, false)); 1921 pw.print(" -> "); 1922 ProcessRecord proc = conn.binding.client; 1923 pw.println(proc != null ? proc.toShortString() : "null"); 1924 } 1925 } 1926 } 1927 } 1928 if (dumpClient && r.app != null && r.app.thread != null) { 1929 pw.println(" Client:"); 1930 pw.flush(); 1931 try { 1932 TransferPipe tp = new TransferPipe(); 1933 try { 1934 r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), 1935 r, args); 1936 tp.setBufferPrefix(" "); 1937 // Short timeout, since blocking here can 1938 // deadlock with the application. 1939 tp.go(fd, 2000); 1940 } finally { 1941 tp.kill(); 1942 } 1943 } catch (IOException e) { 1944 pw.println(" Failure while dumping the service: " + e); 1945 } catch (RemoteException e) { 1946 pw.println(" Got a RemoteException while dumping the service"); 1947 } 1948 needSep = true; 1949 } 1950 } 1951 needSep = printed; 1952 } 1953 } 1954 } catch (Exception e) { 1955 Log.w(TAG, "Exception in dumpServicesLocked: " + e); 1956 } 1957 1958 if (mPendingServices.size() > 0) { 1959 boolean printed = false; 1960 for (int i=0; i<mPendingServices.size(); i++) { 1961 ServiceRecord r = mPendingServices.get(i); 1962 if (!matcher.match(r, r.name)) { 1963 continue; 1964 } 1965 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 1966 continue; 1967 } 1968 if (!printed) { 1969 if (needSep) pw.println(" "); 1970 needSep = true; 1971 pw.println(" Pending services:"); 1972 printed = true; 1973 } 1974 pw.print(" * Pending "); pw.println(r); 1975 r.dump(pw, " "); 1976 } 1977 needSep = true; 1978 } 1979 1980 if (mRestartingServices.size() > 0) { 1981 boolean printed = false; 1982 for (int i=0; i<mRestartingServices.size(); i++) { 1983 ServiceRecord r = mRestartingServices.get(i); 1984 if (!matcher.match(r, r.name)) { 1985 continue; 1986 } 1987 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 1988 continue; 1989 } 1990 if (!printed) { 1991 if (needSep) pw.println(" "); 1992 needSep = true; 1993 pw.println(" Restarting services:"); 1994 printed = true; 1995 } 1996 pw.print(" * Restarting "); pw.println(r); 1997 r.dump(pw, " "); 1998 } 1999 needSep = true; 2000 } 2001 2002 if (mStoppingServices.size() > 0) { 2003 boolean printed = false; 2004 for (int i=0; i<mStoppingServices.size(); i++) { 2005 ServiceRecord r = mStoppingServices.get(i); 2006 if (!matcher.match(r, r.name)) { 2007 continue; 2008 } 2009 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 2010 continue; 2011 } 2012 if (!printed) { 2013 if (needSep) pw.println(" "); 2014 needSep = true; 2015 pw.println(" Stopping services:"); 2016 printed = true; 2017 } 2018 pw.print(" * Stopping "); pw.println(r); 2019 r.dump(pw, " "); 2020 } 2021 needSep = true; 2022 } 2023 2024 if (dumpAll) { 2025 if (mServiceConnections.size() > 0) { 2026 boolean printed = false; 2027 Iterator<ArrayList<ConnectionRecord>> it 2028 = mServiceConnections.values().iterator(); 2029 while (it.hasNext()) { 2030 ArrayList<ConnectionRecord> r = it.next(); 2031 for (int i=0; i<r.size(); i++) { 2032 ConnectionRecord cr = r.get(i); 2033 if (!matcher.match(cr.binding.service, cr.binding.service.name)) { 2034 continue; 2035 } 2036 if (dumpPackage != null && (cr.binding.client == null 2037 || !dumpPackage.equals(cr.binding.client.info.packageName))) { 2038 continue; 2039 } 2040 if (!printed) { 2041 if (needSep) pw.println(" "); 2042 needSep = true; 2043 pw.println(" Connection bindings to services:"); 2044 printed = true; 2045 } 2046 pw.print(" * "); pw.println(cr); 2047 cr.dump(pw, " "); 2048 } 2049 } 2050 needSep = true; 2051 } 2052 } 2053 2054 return needSep; 2055 } 2056 2057 /** 2058 * There are three ways to call this: 2059 * - no service specified: dump all the services 2060 * - a flattened component name that matched an existing service was specified as the 2061 * first arg: dump that one service 2062 * - the first arg isn't the flattened component name of an existing service: 2063 * dump all services whose component contains the first arg as a substring 2064 */ 2065 protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args, 2066 int opti, boolean dumpAll) { 2067 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 2068 2069 List<UserInfo> users = mAm.getUserManager().getUsers(); 2070 if ("all".equals(name)) { 2071 synchronized (this) { 2072 for (UserInfo user : users) { 2073 for (ServiceRecord r1 : mServiceMap.getAllServices(user.id)) { 2074 services.add(r1); 2075 } 2076 } 2077 } 2078 } else { 2079 ComponentName componentName = name != null 2080 ? ComponentName.unflattenFromString(name) : null; 2081 int objectId = 0; 2082 if (componentName == null) { 2083 // Not a '/' separated full component name; maybe an object ID? 2084 try { 2085 objectId = Integer.parseInt(name, 16); 2086 name = null; 2087 componentName = null; 2088 } catch (RuntimeException e) { 2089 } 2090 } 2091 2092 synchronized (this) { 2093 for (UserInfo user : users) { 2094 for (ServiceRecord r1 : mServiceMap.getAllServices(user.id)) { 2095 if (componentName != null) { 2096 if (r1.name.equals(componentName)) { 2097 services.add(r1); 2098 } 2099 } else if (name != null) { 2100 if (r1.name.flattenToString().contains(name)) { 2101 services.add(r1); 2102 } 2103 } else if (System.identityHashCode(r1) == objectId) { 2104 services.add(r1); 2105 } 2106 } 2107 } 2108 } 2109 } 2110 2111 if (services.size() <= 0) { 2112 return false; 2113 } 2114 2115 boolean needSep = false; 2116 for (int i=0; i<services.size(); i++) { 2117 if (needSep) { 2118 pw.println(); 2119 } 2120 needSep = true; 2121 dumpService("", fd, pw, services.get(i), args, dumpAll); 2122 } 2123 return true; 2124 } 2125 2126 /** 2127 * Invokes IApplicationThread.dumpService() on the thread of the specified service if 2128 * there is a thread associated with the service. 2129 */ 2130 private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw, 2131 final ServiceRecord r, String[] args, boolean dumpAll) { 2132 String innerPrefix = prefix + " "; 2133 synchronized (this) { 2134 pw.print(prefix); pw.print("SERVICE "); 2135 pw.print(r.shortName); pw.print(" "); 2136 pw.print(Integer.toHexString(System.identityHashCode(r))); 2137 pw.print(" pid="); 2138 if (r.app != null) pw.println(r.app.pid); 2139 else pw.println("(not running)"); 2140 if (dumpAll) { 2141 r.dump(pw, innerPrefix); 2142 } 2143 } 2144 if (r.app != null && r.app.thread != null) { 2145 pw.print(prefix); pw.println(" Client:"); 2146 pw.flush(); 2147 try { 2148 TransferPipe tp = new TransferPipe(); 2149 try { 2150 r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), r, args); 2151 tp.setBufferPrefix(prefix + " "); 2152 tp.go(fd); 2153 } finally { 2154 tp.kill(); 2155 } 2156 } catch (IOException e) { 2157 pw.println(prefix + " Failure while dumping the service: " + e); 2158 } catch (RemoteException e) { 2159 pw.println(prefix + " Got a RemoteException while dumping the service"); 2160 } 2161 } 2162 } 2163 2164} 2165