ActiveServices.java revision f88dd0b32ea2042eb2011170be465259a21d2563
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.UserId; 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 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 map = mServicesByIntentPerUser.get(callingUser); 199 if (map == null) { 200 map = new HashMap<Intent.FilterComparison, ServiceRecord>(); 201 mServicesByIntentPerUser.put(callingUser, map); 202 } 203 return map; 204 } 205 } 206 207 public ActiveServices(ActivityManagerService service) { 208 mAm = service; 209 } 210 211 ComponentName startServiceLocked(IApplicationThread caller, 212 Intent service, String resolvedType, 213 int callingPid, int callingUid) { 214 if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service 215 + " type=" + resolvedType + " args=" + service.getExtras()); 216 217 if (caller != null) { 218 final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); 219 if (callerApp == null) { 220 throw new SecurityException( 221 "Unable to find app for caller " + caller 222 + " (pid=" + Binder.getCallingPid() 223 + ") when starting service " + service); 224 } 225 } 226 227 ServiceLookupResult res = 228 retrieveServiceLocked(service, resolvedType, 229 callingPid, callingUid, UserId.getUserId(callingUid), true); 230 if (res == null) { 231 return null; 232 } 233 if (res.record == null) { 234 return new ComponentName("!", res.permission != null 235 ? res.permission : "private to package"); 236 } 237 ServiceRecord r = res.record; 238 NeededUriGrants neededGrants = mAm.checkGrantUriPermissionFromIntentLocked( 239 callingUid, r.packageName, service, service.getFlags(), null); 240 if (unscheduleServiceRestartLocked(r)) { 241 if (DEBUG_SERVICE) Slog.v(TAG, "START SERVICE WHILE RESTART PENDING: " + r); 242 } 243 r.startRequested = true; 244 r.callStart = false; 245 r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), 246 service, neededGrants)); 247 r.lastActivity = SystemClock.uptimeMillis(); 248 synchronized (r.stats.getBatteryStats()) { 249 r.stats.startRunningLocked(); 250 } 251 if (!bringUpServiceLocked(r, service.getFlags(), false)) { 252 return new ComponentName("!", "Service process is bad"); 253 } 254 return r.name; 255 } 256 257 private void stopServiceLocked(ServiceRecord service) { 258 synchronized (service.stats.getBatteryStats()) { 259 service.stats.stopRunningLocked(); 260 } 261 service.startRequested = false; 262 service.callStart = false; 263 bringDownServiceLocked(service, false); 264 } 265 266 int stopServiceLocked(IApplicationThread caller, Intent service, 267 String resolvedType) { 268 if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service 269 + " type=" + resolvedType); 270 271 final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); 272 if (caller != null && callerApp == null) { 273 throw new SecurityException( 274 "Unable to find app for caller " + caller 275 + " (pid=" + Binder.getCallingPid() 276 + ") when stopping service " + service); 277 } 278 279 // If this service is active, make sure it is stopped. 280 ServiceLookupResult r = retrieveServiceLocked(service, resolvedType, 281 Binder.getCallingPid(), Binder.getCallingUid(), 282 callerApp == null ? UserId.getCallingUserId() : callerApp.userId, 283 false); 284 if (r != null) { 285 if (r.record != null) { 286 final long origId = Binder.clearCallingIdentity(); 287 try { 288 stopServiceLocked(r.record); 289 } finally { 290 Binder.restoreCallingIdentity(origId); 291 } 292 return 1; 293 } 294 return -1; 295 } 296 297 return 0; 298 } 299 300 IBinder peekServiceLocked(Intent service, String resolvedType) { 301 ServiceLookupResult r = retrieveServiceLocked(service, resolvedType, 302 Binder.getCallingPid(), Binder.getCallingUid(), 303 UserId.getCallingUserId(), false); 304 305 IBinder ret = null; 306 if (r != null) { 307 // r.record is null if findServiceLocked() failed the caller permission check 308 if (r.record == null) { 309 throw new SecurityException( 310 "Permission Denial: Accessing service " + r.record.name 311 + " from pid=" + Binder.getCallingPid() 312 + ", uid=" + Binder.getCallingUid() 313 + " requires " + r.permission); 314 } 315 IntentBindRecord ib = r.record.bindings.get(r.record.intent); 316 if (ib != null) { 317 ret = ib.binder; 318 } 319 } 320 321 return ret; 322 } 323 324 boolean stopServiceTokenLocked(ComponentName className, IBinder token, 325 int startId) { 326 if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className 327 + " " + token + " startId=" + startId); 328 ServiceRecord r = findServiceLocked(className, token); 329 if (r != null) { 330 if (startId >= 0) { 331 // Asked to only stop if done with all work. Note that 332 // to avoid leaks, we will take this as dropping all 333 // start items up to and including this one. 334 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); 335 if (si != null) { 336 while (r.deliveredStarts.size() > 0) { 337 ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); 338 cur.removeUriPermissionsLocked(); 339 if (cur == si) { 340 break; 341 } 342 } 343 } 344 345 if (r.getLastStartId() != startId) { 346 return false; 347 } 348 349 if (r.deliveredStarts.size() > 0) { 350 Slog.w(TAG, "stopServiceToken startId " + startId 351 + " is last, but have " + r.deliveredStarts.size() 352 + " remaining args"); 353 } 354 } 355 356 synchronized (r.stats.getBatteryStats()) { 357 r.stats.stopRunningLocked(); 358 r.startRequested = false; 359 r.callStart = false; 360 } 361 final long origId = Binder.clearCallingIdentity(); 362 bringDownServiceLocked(r, false); 363 Binder.restoreCallingIdentity(origId); 364 return true; 365 } 366 return false; 367 } 368 369 public void setServiceForegroundLocked(ComponentName className, IBinder token, 370 int id, Notification notification, boolean removeNotification) { 371 final long origId = Binder.clearCallingIdentity(); 372 try { 373 ServiceRecord r = findServiceLocked(className, token); 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 if (DEBUG_MU) 433 Slog.i(TAG_MU, "bindService uid=" + Binder.getCallingUid() + " origUid=" 434 + Binder.getOrigCallingUid()); 435 final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); 436 if (callerApp == null) { 437 throw new SecurityException( 438 "Unable to find app for caller " + caller 439 + " (pid=" + Binder.getCallingPid() 440 + ") when binding service " + service); 441 } 442 443 ActivityRecord activity = null; 444 if (token != null) { 445 activity = mAm.mMainStack.isInStackLocked(token); 446 if (activity == null) { 447 Slog.w(TAG, "Binding with unknown activity: " + token); 448 return 0; 449 } 450 } 451 452 int clientLabel = 0; 453 PendingIntent clientIntent = null; 454 455 if (callerApp.info.uid == Process.SYSTEM_UID) { 456 // Hacky kind of thing -- allow system stuff to tell us 457 // what they are, so we can report this elsewhere for 458 // others to know why certain services are running. 459 try { 460 clientIntent = (PendingIntent)service.getParcelableExtra( 461 Intent.EXTRA_CLIENT_INTENT); 462 } catch (RuntimeException e) { 463 } 464 if (clientIntent != null) { 465 clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); 466 if (clientLabel != 0) { 467 // There are no useful extras in the intent, trash them. 468 // System code calling with this stuff just needs to know 469 // this will happen. 470 service = service.cloneFilter(); 471 } 472 } 473 } 474 475 ServiceLookupResult res = 476 retrieveServiceLocked(service, resolvedType, 477 Binder.getCallingPid(), Binder.getCallingUid(), userId, true); 478 if (res == null) { 479 return 0; 480 } 481 if (res.record == null) { 482 return -1; 483 } 484 if (mAm.isSingleton(res.record.processName, res.record.appInfo, 485 res.record.serviceInfo.name, res.record.serviceInfo.flags)) { 486 userId = 0; 487 res = retrieveServiceLocked(service, resolvedType, Binder.getCallingPid(), 488 Binder.getCallingUid(), 0, true); 489 } 490 ServiceRecord s = res.record; 491 492 final long origId = Binder.clearCallingIdentity(); 493 494 try { 495 if (unscheduleServiceRestartLocked(s)) { 496 if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: " 497 + s); 498 } 499 500 AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); 501 ConnectionRecord c = new ConnectionRecord(b, activity, 502 connection, flags, clientLabel, clientIntent); 503 504 IBinder binder = connection.asBinder(); 505 ArrayList<ConnectionRecord> clist = s.connections.get(binder); 506 if (clist == null) { 507 clist = new ArrayList<ConnectionRecord>(); 508 s.connections.put(binder, clist); 509 } 510 clist.add(c); 511 b.connections.add(c); 512 if (activity != null) { 513 if (activity.connections == null) { 514 activity.connections = new HashSet<ConnectionRecord>(); 515 } 516 activity.connections.add(c); 517 } 518 b.client.connections.add(c); 519 if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { 520 b.client.hasAboveClient = true; 521 } 522 clist = mServiceConnections.get(binder); 523 if (clist == null) { 524 clist = new ArrayList<ConnectionRecord>(); 525 mServiceConnections.put(binder, clist); 526 } 527 clist.add(c); 528 529 if ((flags&Context.BIND_AUTO_CREATE) != 0) { 530 s.lastActivity = SystemClock.uptimeMillis(); 531 if (!bringUpServiceLocked(s, service.getFlags(), false)) { 532 return 0; 533 } 534 } 535 536 if (s.app != null) { 537 // This could have made the service more important. 538 mAm.updateOomAdjLocked(s.app); 539 } 540 541 if (DEBUG_SERVICE) Slog.v(TAG, "Bind " + s + " with " + b 542 + ": received=" + b.intent.received 543 + " apps=" + b.intent.apps.size() 544 + " doRebind=" + b.intent.doRebind); 545 546 if (s.app != null && b.intent.received) { 547 // Service is already running, so we can immediately 548 // publish the connection. 549 try { 550 c.conn.connected(s.name, b.intent.binder); 551 } catch (Exception e) { 552 Slog.w(TAG, "Failure sending service " + s.shortName 553 + " to connection " + c.conn.asBinder() 554 + " (in " + c.binding.client.processName + ")", e); 555 } 556 557 // If this is the first app connected back to this binding, 558 // and the service had previously asked to be told when 559 // rebound, then do so. 560 if (b.intent.apps.size() == 1 && b.intent.doRebind) { 561 requestServiceBindingLocked(s, b.intent, true); 562 } 563 } else if (!b.intent.requested) { 564 requestServiceBindingLocked(s, b.intent, false); 565 } 566 } finally { 567 Binder.restoreCallingIdentity(origId); 568 } 569 570 return 1; 571 } 572 573 void publishServiceLocked(ServiceRecord r, Intent intent, IBinder service) { 574 final long origId = Binder.clearCallingIdentity(); 575 try { 576 if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r 577 + " " + intent + ": " + service); 578 if (r != null) { 579 Intent.FilterComparison filter 580 = new Intent.FilterComparison(intent); 581 IntentBindRecord b = r.bindings.get(filter); 582 if (b != null && !b.received) { 583 b.binder = service; 584 b.requested = true; 585 b.received = true; 586 if (r.connections.size() > 0) { 587 Iterator<ArrayList<ConnectionRecord>> it 588 = r.connections.values().iterator(); 589 while (it.hasNext()) { 590 ArrayList<ConnectionRecord> clist = it.next(); 591 for (int i=0; i<clist.size(); i++) { 592 ConnectionRecord c = clist.get(i); 593 if (!filter.equals(c.binding.intent.intent)) { 594 if (DEBUG_SERVICE) Slog.v( 595 TAG, "Not publishing to: " + c); 596 if (DEBUG_SERVICE) Slog.v( 597 TAG, "Bound intent: " + c.binding.intent.intent); 598 if (DEBUG_SERVICE) Slog.v( 599 TAG, "Published intent: " + intent); 600 continue; 601 } 602 if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c); 603 try { 604 c.conn.connected(r.name, service); 605 } catch (Exception e) { 606 Slog.w(TAG, "Failure sending service " + r.name + 607 " to connection " + c.conn.asBinder() + 608 " (in " + c.binding.client.processName + ")", e); 609 } 610 } 611 } 612 } 613 } 614 615 serviceDoneExecutingLocked(r, mStoppingServices.contains(r)); 616 } 617 } finally { 618 Binder.restoreCallingIdentity(origId); 619 } 620 } 621 622 boolean unbindServiceLocked(IServiceConnection connection) { 623 IBinder binder = connection.asBinder(); 624 if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder); 625 ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder); 626 if (clist == null) { 627 Slog.w(TAG, "Unbind failed: could not find connection for " 628 + connection.asBinder()); 629 return false; 630 } 631 632 final long origId = Binder.clearCallingIdentity(); 633 try { 634 while (clist.size() > 0) { 635 ConnectionRecord r = clist.get(0); 636 removeConnectionLocked(r, null, null); 637 638 if (r.binding.service.app != null) { 639 // This could have made the service less important. 640 mAm.updateOomAdjLocked(r.binding.service.app); 641 } 642 } 643 } finally { 644 Binder.restoreCallingIdentity(origId); 645 } 646 647 return true; 648 } 649 650 void unbindFinishedLocked(ServiceRecord r, Intent intent, boolean doRebind) { 651 final long origId = Binder.clearCallingIdentity(); 652 try { 653 if (r != null) { 654 Intent.FilterComparison filter 655 = new Intent.FilterComparison(intent); 656 IntentBindRecord b = r.bindings.get(filter); 657 if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r 658 + " at " + b + ": apps=" 659 + (b != null ? b.apps.size() : 0)); 660 661 boolean inStopping = mStoppingServices.contains(r); 662 if (b != null) { 663 if (b.apps.size() > 0 && !inStopping) { 664 // Applications have already bound since the last 665 // unbind, so just rebind right here. 666 requestServiceBindingLocked(r, b, true); 667 } else { 668 // Note to tell the service the next time there is 669 // a new client. 670 b.doRebind = true; 671 } 672 } 673 674 serviceDoneExecutingLocked(r, inStopping); 675 } 676 } finally { 677 Binder.restoreCallingIdentity(origId); 678 } 679 } 680 681 private final ServiceRecord findServiceLocked(ComponentName name, 682 IBinder token) { 683 ServiceRecord r = mServiceMap.getServiceByName(name, Binder.getOrigCallingUser()); 684 return r == token ? r : null; 685 } 686 687 private final class ServiceLookupResult { 688 final ServiceRecord record; 689 final String permission; 690 691 ServiceLookupResult(ServiceRecord _record, String _permission) { 692 record = _record; 693 permission = _permission; 694 } 695 } 696 697 private class ServiceRestarter implements Runnable { 698 private ServiceRecord mService; 699 700 void setService(ServiceRecord service) { 701 mService = service; 702 } 703 704 public void run() { 705 synchronized(mAm) { 706 performServiceRestartLocked(mService); 707 } 708 } 709 } 710 711 private ServiceLookupResult retrieveServiceLocked(Intent service, 712 String resolvedType, int callingPid, int callingUid, int userId, 713 boolean createIfNeeded) { 714 ServiceRecord r = null; 715 if (DEBUG_SERVICE) Slog.v(TAG, "retrieveServiceLocked: " + service 716 + " type=" + resolvedType + " callingUid=" + callingUid); 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 + 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, UserId.getUserId(r.appInfo.uid), r); 763 mServiceMap.putServiceByIntent(filter, UserId.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 // Service is now being launched, its package can't be stopped. 995 try { 996 AppGlobals.getPackageManager().setPackageStoppedState( 997 r.packageName, false, r.userId); 998 } catch (RemoteException e) { 999 } catch (IllegalArgumentException e) { 1000 Slog.w(TAG, "Failed trying to unstop package " 1001 + r.packageName + ": " + e); 1002 } 1003 1004 final boolean isolated = (r.serviceInfo.flags&ServiceInfo.FLAG_ISOLATED_PROCESS) != 0; 1005 final String procName = r.processName; 1006 ProcessRecord app; 1007 1008 if (!isolated) { 1009 app = mAm.getProcessRecordLocked(procName, r.appInfo.uid); 1010 if (DEBUG_MU) 1011 Slog.v(TAG_MU, "bringUpServiceLocked: appInfo.uid=" + r.appInfo.uid + " app=" + app); 1012 if (app != null && app.thread != null) { 1013 try { 1014 app.addPackage(r.appInfo.packageName); 1015 realStartServiceLocked(r, app); 1016 return true; 1017 } catch (RemoteException e) { 1018 Slog.w(TAG, "Exception when starting service " + r.shortName, e); 1019 } 1020 1021 // If a dead object exception was thrown -- fall through to 1022 // restart the application. 1023 } 1024 } else { 1025 // If this service runs in an isolated process, then each time 1026 // we call startProcessLocked() we will get a new isolated 1027 // process, starting another process if we are currently waiting 1028 // for a previous process to come up. To deal with this, we store 1029 // in the service any current isolated process it is running in or 1030 // waiting to have come up. 1031 app = r.isolatedProc; 1032 } 1033 1034 // Not running -- get it started, and enqueue this service record 1035 // to be executed when the app comes up. 1036 if (app == null) { 1037 if ((app=mAm.startProcessLocked(procName, r.appInfo, true, intentFlags, 1038 "service", r.name, false, isolated)) == null) { 1039 Slog.w(TAG, "Unable to launch app " 1040 + r.appInfo.packageName + "/" 1041 + r.appInfo.uid + " for service " 1042 + r.intent.getIntent() + ": process is bad"); 1043 bringDownServiceLocked(r, true); 1044 return false; 1045 } 1046 if (isolated) { 1047 r.isolatedProc = app; 1048 } 1049 } 1050 1051 if (!mPendingServices.contains(r)) { 1052 mPendingServices.add(r); 1053 } 1054 1055 return true; 1056 } 1057 1058 private final void requestServiceBindingsLocked(ServiceRecord r) { 1059 Iterator<IntentBindRecord> bindings = r.bindings.values().iterator(); 1060 while (bindings.hasNext()) { 1061 IntentBindRecord i = bindings.next(); 1062 if (!requestServiceBindingLocked(r, i, false)) { 1063 break; 1064 } 1065 } 1066 } 1067 1068 private final void realStartServiceLocked(ServiceRecord r, 1069 ProcessRecord app) throws RemoteException { 1070 if (app.thread == null) { 1071 throw new RemoteException(); 1072 } 1073 if (DEBUG_MU) 1074 Slog.v(TAG_MU, "realStartServiceLocked, ServiceRecord.uid = " + r.appInfo.uid 1075 + ", ProcessRecord.uid = " + app.uid); 1076 r.app = app; 1077 r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); 1078 1079 app.services.add(r); 1080 bumpServiceExecutingLocked(r, "create"); 1081 mAm.updateLruProcessLocked(app, true, true); 1082 1083 boolean created = false; 1084 try { 1085 mAm.mStringBuilder.setLength(0); 1086 r.intent.getIntent().toShortString(mAm.mStringBuilder, true, false, true, false); 1087 EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE, 1088 System.identityHashCode(r), r.shortName, 1089 mAm.mStringBuilder.toString(), r.app.pid); 1090 synchronized (r.stats.getBatteryStats()) { 1091 r.stats.startLaunchedLocked(); 1092 } 1093 mAm.ensurePackageDexOpt(r.serviceInfo.packageName); 1094 app.thread.scheduleCreateService(r, r.serviceInfo, 1095 mAm.compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo)); 1096 r.postNotification(); 1097 created = true; 1098 } finally { 1099 if (!created) { 1100 app.services.remove(r); 1101 scheduleServiceRestartLocked(r, false); 1102 } 1103 } 1104 1105 requestServiceBindingsLocked(r); 1106 1107 // If the service is in the started state, and there are no 1108 // pending arguments, then fake up one so its onStartCommand() will 1109 // be called. 1110 if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { 1111 r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), 1112 null, null)); 1113 } 1114 1115 sendServiceArgsLocked(r, true); 1116 } 1117 1118 private final void sendServiceArgsLocked(ServiceRecord r, 1119 boolean oomAdjusted) { 1120 final int N = r.pendingStarts.size(); 1121 if (N == 0) { 1122 return; 1123 } 1124 1125 while (r.pendingStarts.size() > 0) { 1126 try { 1127 ServiceRecord.StartItem si = r.pendingStarts.remove(0); 1128 if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: " 1129 + r + " " + r.intent + " args=" + si.intent); 1130 if (si.intent == null && N > 1) { 1131 // If somehow we got a dummy null intent in the middle, 1132 // then skip it. DO NOT skip a null intent when it is 1133 // the only one in the list -- this is to support the 1134 // onStartCommand(null) case. 1135 continue; 1136 } 1137 si.deliveredTime = SystemClock.uptimeMillis(); 1138 r.deliveredStarts.add(si); 1139 si.deliveryCount++; 1140 if (si.neededGrants != null) { 1141 mAm.grantUriPermissionUncheckedFromIntentLocked(si.neededGrants, 1142 si.getUriPermissionsLocked()); 1143 } 1144 bumpServiceExecutingLocked(r, "start"); 1145 if (!oomAdjusted) { 1146 oomAdjusted = true; 1147 mAm.updateOomAdjLocked(r.app); 1148 } 1149 int flags = 0; 1150 if (si.deliveryCount > 1) { 1151 flags |= Service.START_FLAG_RETRY; 1152 } 1153 if (si.doneExecutingCount > 0) { 1154 flags |= Service.START_FLAG_REDELIVERY; 1155 } 1156 r.app.thread.scheduleServiceArgs(r, si.taskRemoved, si.id, flags, si.intent); 1157 } catch (RemoteException e) { 1158 // Remote process gone... we'll let the normal cleanup take 1159 // care of this. 1160 if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r); 1161 break; 1162 } catch (Exception e) { 1163 Slog.w(TAG, "Unexpected exception", e); 1164 break; 1165 } 1166 } 1167 } 1168 1169 private final void bringDownServiceLocked(ServiceRecord r, boolean force) { 1170 //Slog.i(TAG, "Bring down service:"); 1171 //r.dump(" "); 1172 1173 // Does it still need to run? 1174 if (!force && r.startRequested) { 1175 return; 1176 } 1177 if (r.connections.size() > 0) { 1178 if (!force) { 1179 // XXX should probably keep a count of the number of auto-create 1180 // connections directly in the service. 1181 Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); 1182 while (it.hasNext()) { 1183 ArrayList<ConnectionRecord> cr = it.next(); 1184 for (int i=0; i<cr.size(); i++) { 1185 if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) { 1186 return; 1187 } 1188 } 1189 } 1190 } 1191 1192 // Report to all of the connections that the service is no longer 1193 // available. 1194 Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); 1195 while (it.hasNext()) { 1196 ArrayList<ConnectionRecord> c = it.next(); 1197 for (int i=0; i<c.size(); i++) { 1198 ConnectionRecord cr = c.get(i); 1199 // There is still a connection to the service that is 1200 // being brought down. Mark it as dead. 1201 cr.serviceDead = true; 1202 try { 1203 cr.conn.connected(r.name, null); 1204 } catch (Exception e) { 1205 Slog.w(TAG, "Failure disconnecting service " + r.name + 1206 " to connection " + c.get(i).conn.asBinder() + 1207 " (in " + c.get(i).binding.client.processName + ")", e); 1208 } 1209 } 1210 } 1211 } 1212 1213 // Tell the service that it has been unbound. 1214 if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) { 1215 Iterator<IntentBindRecord> it = r.bindings.values().iterator(); 1216 while (it.hasNext()) { 1217 IntentBindRecord ibr = it.next(); 1218 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down binding " + ibr 1219 + ": hasBound=" + ibr.hasBound); 1220 if (r.app != null && r.app.thread != null && ibr.hasBound) { 1221 try { 1222 bumpServiceExecutingLocked(r, "bring down unbind"); 1223 mAm.updateOomAdjLocked(r.app); 1224 ibr.hasBound = false; 1225 r.app.thread.scheduleUnbindService(r, 1226 ibr.intent.getIntent()); 1227 } catch (Exception e) { 1228 Slog.w(TAG, "Exception when unbinding service " 1229 + r.shortName, e); 1230 serviceDoneExecutingLocked(r, true); 1231 } 1232 } 1233 } 1234 } 1235 1236 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent); 1237 EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE, 1238 System.identityHashCode(r), r.shortName, 1239 (r.app != null) ? r.app.pid : -1); 1240 1241 mServiceMap.removeServiceByName(r.name, r.userId); 1242 mServiceMap.removeServiceByIntent(r.intent, r.userId); 1243 r.totalRestartCount = 0; 1244 unscheduleServiceRestartLocked(r); 1245 1246 // Also make sure it is not on the pending list. 1247 int N = mPendingServices.size(); 1248 for (int i=0; i<N; i++) { 1249 if (mPendingServices.get(i) == r) { 1250 mPendingServices.remove(i); 1251 if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r); 1252 i--; 1253 N--; 1254 } 1255 } 1256 1257 r.cancelNotification(); 1258 r.isForeground = false; 1259 r.foregroundId = 0; 1260 r.foregroundNoti = null; 1261 1262 // Clear start entries. 1263 r.clearDeliveredStartsLocked(); 1264 r.pendingStarts.clear(); 1265 1266 if (r.app != null) { 1267 synchronized (r.stats.getBatteryStats()) { 1268 r.stats.stopLaunchedLocked(); 1269 } 1270 r.app.services.remove(r); 1271 if (r.app.thread != null) { 1272 try { 1273 bumpServiceExecutingLocked(r, "stop"); 1274 mStoppingServices.add(r); 1275 mAm.updateOomAdjLocked(r.app); 1276 r.app.thread.scheduleStopService(r); 1277 } catch (Exception e) { 1278 Slog.w(TAG, "Exception when stopping service " 1279 + r.shortName, e); 1280 serviceDoneExecutingLocked(r, true); 1281 } 1282 updateServiceForegroundLocked(r.app, false); 1283 } else { 1284 if (DEBUG_SERVICE) Slog.v( 1285 TAG, "Removed service that has no process: " + r); 1286 } 1287 } else { 1288 if (DEBUG_SERVICE) Slog.v( 1289 TAG, "Removed service that is not running: " + r); 1290 } 1291 1292 if (r.bindings.size() > 0) { 1293 r.bindings.clear(); 1294 } 1295 1296 if (r.restarter instanceof ServiceRestarter) { 1297 ((ServiceRestarter)r.restarter).setService(null); 1298 } 1299 } 1300 1301 void removeConnectionLocked( 1302 ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { 1303 IBinder binder = c.conn.asBinder(); 1304 AppBindRecord b = c.binding; 1305 ServiceRecord s = b.service; 1306 ArrayList<ConnectionRecord> clist = s.connections.get(binder); 1307 if (clist != null) { 1308 clist.remove(c); 1309 if (clist.size() == 0) { 1310 s.connections.remove(binder); 1311 } 1312 } 1313 b.connections.remove(c); 1314 if (c.activity != null && c.activity != skipAct) { 1315 if (c.activity.connections != null) { 1316 c.activity.connections.remove(c); 1317 } 1318 } 1319 if (b.client != skipApp) { 1320 b.client.connections.remove(c); 1321 if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { 1322 b.client.updateHasAboveClientLocked(); 1323 } 1324 } 1325 clist = mServiceConnections.get(binder); 1326 if (clist != null) { 1327 clist.remove(c); 1328 if (clist.size() == 0) { 1329 mServiceConnections.remove(binder); 1330 } 1331 } 1332 1333 if (b.connections.size() == 0) { 1334 b.intent.apps.remove(b.client); 1335 } 1336 1337 if (!c.serviceDead) { 1338 if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent 1339 + ": shouldUnbind=" + b.intent.hasBound); 1340 if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 1341 && b.intent.hasBound) { 1342 try { 1343 bumpServiceExecutingLocked(s, "unbind"); 1344 mAm.updateOomAdjLocked(s.app); 1345 b.intent.hasBound = false; 1346 // Assume the client doesn't want to know about a rebind; 1347 // we will deal with that later if it asks for one. 1348 b.intent.doRebind = false; 1349 s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); 1350 } catch (Exception e) { 1351 Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); 1352 serviceDoneExecutingLocked(s, true); 1353 } 1354 } 1355 1356 if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { 1357 bringDownServiceLocked(s, false); 1358 } 1359 } 1360 } 1361 1362 void serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res) { 1363 boolean inStopping = mStoppingServices.contains(r); 1364 if (r != null) { 1365 if (type == 1) { 1366 // This is a call from a service start... take care of 1367 // book-keeping. 1368 r.callStart = true; 1369 switch (res) { 1370 case Service.START_STICKY_COMPATIBILITY: 1371 case Service.START_STICKY: { 1372 // We are done with the associated start arguments. 1373 r.findDeliveredStart(startId, true); 1374 // Don't stop if killed. 1375 r.stopIfKilled = false; 1376 break; 1377 } 1378 case Service.START_NOT_STICKY: { 1379 // We are done with the associated start arguments. 1380 r.findDeliveredStart(startId, true); 1381 if (r.getLastStartId() == startId) { 1382 // There is no more work, and this service 1383 // doesn't want to hang around if killed. 1384 r.stopIfKilled = true; 1385 } 1386 break; 1387 } 1388 case Service.START_REDELIVER_INTENT: { 1389 // We'll keep this item until they explicitly 1390 // call stop for it, but keep track of the fact 1391 // that it was delivered. 1392 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); 1393 if (si != null) { 1394 si.deliveryCount = 0; 1395 si.doneExecutingCount++; 1396 // Don't stop if killed. 1397 r.stopIfKilled = true; 1398 } 1399 break; 1400 } 1401 case Service.START_TASK_REMOVED_COMPLETE: { 1402 // Special processing for onTaskRemoved(). Don't 1403 // impact normal onStartCommand() processing. 1404 r.findDeliveredStart(startId, true); 1405 break; 1406 } 1407 default: 1408 throw new IllegalArgumentException( 1409 "Unknown service start result: " + res); 1410 } 1411 if (res == Service.START_STICKY_COMPATIBILITY) { 1412 r.callStart = false; 1413 } 1414 } 1415 if (DEBUG_MU) 1416 Slog.v(TAG_MU, "before serviceDontExecutingLocked, uid=" 1417 + Binder.getOrigCallingUid()); 1418 final long origId = Binder.clearCallingIdentity(); 1419 serviceDoneExecutingLocked(r, inStopping); 1420 Binder.restoreCallingIdentity(origId); 1421 } else { 1422 Slog.w(TAG, "Done executing unknown service from pid " 1423 + Binder.getCallingPid()); 1424 } 1425 } 1426 1427 private void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) { 1428 if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r 1429 + ": nesting=" + r.executeNesting 1430 + ", inStopping=" + inStopping + ", app=" + r.app); 1431 else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG, "<<< DONE EXECUTING " + r.shortName); 1432 r.executeNesting--; 1433 if (r.executeNesting <= 0 && r.app != null) { 1434 if (DEBUG_SERVICE) Slog.v(TAG, 1435 "Nesting at 0 of " + r.shortName); 1436 r.app.executingServices.remove(r); 1437 if (r.app.executingServices.size() == 0) { 1438 if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG, 1439 "No more executingServices of " + r.shortName); 1440 mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_TIMEOUT_MSG, r.app); 1441 } 1442 if (inStopping) { 1443 if (DEBUG_SERVICE) Slog.v(TAG, 1444 "doneExecuting remove stopping " + r); 1445 mStoppingServices.remove(r); 1446 r.bindings.clear(); 1447 } 1448 mAm.updateOomAdjLocked(r.app); 1449 } 1450 } 1451 1452 boolean attachApplicationLocked(ProcessRecord proc, String processName) throws Exception { 1453 boolean didSomething = false; 1454 // Collect any services that are waiting for this process to come up. 1455 if (mPendingServices.size() > 0) { 1456 ServiceRecord sr = null; 1457 try { 1458 for (int i=0; i<mPendingServices.size(); i++) { 1459 sr = mPendingServices.get(i); 1460 if (proc != sr.isolatedProc && (proc.uid != sr.appInfo.uid 1461 || !processName.equals(sr.processName))) { 1462 continue; 1463 } 1464 1465 mPendingServices.remove(i); 1466 i--; 1467 realStartServiceLocked(sr, proc); 1468 didSomething = true; 1469 } 1470 } catch (Exception e) { 1471 Slog.w(TAG, "Exception in new application when starting service " 1472 + sr.shortName, e); 1473 throw e; 1474 } 1475 } 1476 // Also, if there are any services that are waiting to restart and 1477 // would run in this process, now is a good time to start them. It would 1478 // be weird to bring up the process but arbitrarily not let the services 1479 // run at this point just because their restart time hasn't come up. 1480 if (mRestartingServices.size() > 0) { 1481 ServiceRecord sr = null; 1482 for (int i=0; i<mRestartingServices.size(); i++) { 1483 sr = mRestartingServices.get(i); 1484 if (proc != sr.isolatedProc && (proc.uid != sr.appInfo.uid 1485 || !processName.equals(sr.processName))) { 1486 continue; 1487 } 1488 mAm.mHandler.removeCallbacks(sr.restarter); 1489 mAm.mHandler.post(sr.restarter); 1490 } 1491 } 1492 return didSomething; 1493 } 1494 1495 void processStartTimedOutLocked(ProcessRecord proc) { 1496 for (int i=0; i<mPendingServices.size(); i++) { 1497 ServiceRecord sr = mPendingServices.get(i); 1498 if ((proc.uid == sr.appInfo.uid 1499 && proc.processName.equals(sr.processName)) 1500 || sr.isolatedProc == proc) { 1501 Slog.w(TAG, "Forcing bringing down service: " + sr); 1502 sr.isolatedProc = null; 1503 mPendingServices.remove(i); 1504 i--; 1505 bringDownServiceLocked(sr, true); 1506 } 1507 } 1508 } 1509 1510 boolean forceStopLocked(String name, int userId, boolean evenPersistent, boolean doit) { 1511 boolean didSomething = false; 1512 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 1513 for (ServiceRecord service : mServiceMap.getAllServices(userId)) { 1514 if (service.packageName.equals(name) 1515 && (service.app == null || evenPersistent || !service.app.persistent)) { 1516 if (!doit) { 1517 return true; 1518 } 1519 didSomething = true; 1520 Slog.i(TAG, " Force stopping service " + service); 1521 if (service.app != null) { 1522 service.app.removed = true; 1523 } 1524 service.app = null; 1525 service.isolatedProc = null; 1526 services.add(service); 1527 } 1528 } 1529 1530 int N = services.size(); 1531 for (int i=0; i<N; i++) { 1532 bringDownServiceLocked(services.get(i), true); 1533 } 1534 return didSomething; 1535 } 1536 1537 void cleanUpRemovedTaskLocked(TaskRecord tr, ComponentName component, Intent baseIntent) { 1538 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 1539 for (ServiceRecord sr : mServiceMap.getAllServices(tr.userId)) { 1540 if (sr.packageName.equals(component.getPackageName())) { 1541 services.add(sr); 1542 } 1543 } 1544 1545 // Take care of any running services associated with the app. 1546 for (int i=0; i<services.size(); i++) { 1547 ServiceRecord sr = services.get(i); 1548 if (sr.startRequested) { 1549 if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) { 1550 Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task"); 1551 stopServiceLocked(sr); 1552 } else { 1553 sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true, 1554 sr.makeNextStartId(), baseIntent, null)); 1555 if (sr.app != null && sr.app.thread != null) { 1556 sendServiceArgsLocked(sr, false); 1557 } 1558 } 1559 } 1560 } 1561 } 1562 1563 final void killServicesLocked(ProcessRecord app, 1564 boolean allowRestart) { 1565 // Report disconnected services. 1566 if (false) { 1567 // XXX we are letting the client link to the service for 1568 // death notifications. 1569 if (app.services.size() > 0) { 1570 Iterator<ServiceRecord> it = app.services.iterator(); 1571 while (it.hasNext()) { 1572 ServiceRecord r = it.next(); 1573 if (r.connections.size() > 0) { 1574 Iterator<ArrayList<ConnectionRecord>> jt 1575 = r.connections.values().iterator(); 1576 while (jt.hasNext()) { 1577 ArrayList<ConnectionRecord> cl = jt.next(); 1578 for (int i=0; i<cl.size(); i++) { 1579 ConnectionRecord c = cl.get(i); 1580 if (c.binding.client != app) { 1581 try { 1582 //c.conn.connected(r.className, null); 1583 } catch (Exception e) { 1584 // todo: this should be asynchronous! 1585 Slog.w(TAG, "Exception thrown disconnected servce " 1586 + r.shortName 1587 + " from app " + app.processName, e); 1588 } 1589 } 1590 } 1591 } 1592 } 1593 } 1594 } 1595 } 1596 1597 // Clean up any connections this application has to other services. 1598 if (app.connections.size() > 0) { 1599 Iterator<ConnectionRecord> it = app.connections.iterator(); 1600 while (it.hasNext()) { 1601 ConnectionRecord r = it.next(); 1602 removeConnectionLocked(r, app, null); 1603 } 1604 } 1605 app.connections.clear(); 1606 1607 if (app.services.size() != 0) { 1608 // Any services running in the application need to be placed 1609 // back in the pending list. 1610 Iterator<ServiceRecord> it = app.services.iterator(); 1611 while (it.hasNext()) { 1612 ServiceRecord sr = it.next(); 1613 synchronized (sr.stats.getBatteryStats()) { 1614 sr.stats.stopLaunchedLocked(); 1615 } 1616 sr.app = null; 1617 sr.isolatedProc = null; 1618 sr.executeNesting = 0; 1619 if (mStoppingServices.remove(sr)) { 1620 if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); 1621 } 1622 1623 boolean hasClients = sr.bindings.size() > 0; 1624 if (hasClients) { 1625 Iterator<IntentBindRecord> bindings 1626 = sr.bindings.values().iterator(); 1627 while (bindings.hasNext()) { 1628 IntentBindRecord b = bindings.next(); 1629 if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b 1630 + ": shouldUnbind=" + b.hasBound); 1631 b.binder = null; 1632 b.requested = b.received = b.hasBound = false; 1633 } 1634 } 1635 1636 if (sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags 1637 &ApplicationInfo.FLAG_PERSISTENT) == 0) { 1638 Slog.w(TAG, "Service crashed " + sr.crashCount 1639 + " times, stopping: " + sr); 1640 EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, 1641 sr.crashCount, sr.shortName, app.pid); 1642 bringDownServiceLocked(sr, true); 1643 } else if (!allowRestart) { 1644 bringDownServiceLocked(sr, true); 1645 } else { 1646 boolean canceled = scheduleServiceRestartLocked(sr, true); 1647 1648 // Should the service remain running? Note that in the 1649 // extreme case of so many attempts to deliver a command 1650 // that it failed we also will stop it here. 1651 if (sr.startRequested && (sr.stopIfKilled || canceled)) { 1652 if (sr.pendingStarts.size() == 0) { 1653 sr.startRequested = false; 1654 if (!hasClients) { 1655 // Whoops, no reason to restart! 1656 bringDownServiceLocked(sr, true); 1657 } 1658 } 1659 } 1660 } 1661 } 1662 1663 if (!allowRestart) { 1664 app.services.clear(); 1665 } 1666 } 1667 1668 // Make sure we have no more records on the stopping list. 1669 int i = mStoppingServices.size(); 1670 while (i > 0) { 1671 i--; 1672 ServiceRecord sr = mStoppingServices.get(i); 1673 if (sr.app == app) { 1674 mStoppingServices.remove(i); 1675 if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); 1676 } 1677 } 1678 1679 app.executingServices.clear(); 1680 } 1681 1682 ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { 1683 ActivityManager.RunningServiceInfo info = 1684 new ActivityManager.RunningServiceInfo(); 1685 info.service = r.name; 1686 if (r.app != null) { 1687 info.pid = r.app.pid; 1688 } 1689 info.uid = r.appInfo.uid; 1690 info.process = r.processName; 1691 info.foreground = r.isForeground; 1692 info.activeSince = r.createTime; 1693 info.started = r.startRequested; 1694 info.clientCount = r.connections.size(); 1695 info.crashCount = r.crashCount; 1696 info.lastActivityTime = r.lastActivity; 1697 if (r.isForeground) { 1698 info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; 1699 } 1700 if (r.startRequested) { 1701 info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; 1702 } 1703 if (r.app != null && r.app.pid == ActivityManagerService.MY_PID) { 1704 info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; 1705 } 1706 if (r.app != null && r.app.persistent) { 1707 info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; 1708 } 1709 1710 for (ArrayList<ConnectionRecord> connl : r.connections.values()) { 1711 for (int i=0; i<connl.size(); i++) { 1712 ConnectionRecord conn = connl.get(i); 1713 if (conn.clientLabel != 0) { 1714 info.clientPackage = conn.binding.client.info.packageName; 1715 info.clientLabel = conn.clientLabel; 1716 return info; 1717 } 1718 } 1719 } 1720 return info; 1721 } 1722 1723 List<ActivityManager.RunningServiceInfo> getRunningServiceInfoLocked(int maxNum, 1724 int flags) { 1725 ArrayList<ActivityManager.RunningServiceInfo> res 1726 = new ArrayList<ActivityManager.RunningServiceInfo>(); 1727 1728 int userId = UserId.getUserId(Binder.getCallingUid()); 1729 if (mServiceMap.getAllServices(userId).size() > 0) { 1730 Iterator<ServiceRecord> it 1731 = mServiceMap.getAllServices(userId).iterator(); 1732 while (it.hasNext() && res.size() < maxNum) { 1733 res.add(makeRunningServiceInfoLocked(it.next())); 1734 } 1735 } 1736 1737 for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { 1738 ServiceRecord r = mRestartingServices.get(i); 1739 ActivityManager.RunningServiceInfo info = 1740 makeRunningServiceInfoLocked(r); 1741 info.restarting = r.nextRestartTime; 1742 res.add(info); 1743 } 1744 1745 return res; 1746 } 1747 1748 public PendingIntent getRunningServiceControlPanelLocked(ComponentName name) { 1749 int userId = UserId.getUserId(Binder.getCallingUid()); 1750 ServiceRecord r = mServiceMap.getServiceByName(name, userId); 1751 if (r != null) { 1752 for (ArrayList<ConnectionRecord> conn : r.connections.values()) { 1753 for (int i=0; i<conn.size(); i++) { 1754 if (conn.get(i).clientIntent != null) { 1755 return conn.get(i).clientIntent; 1756 } 1757 } 1758 } 1759 } 1760 return null; 1761 } 1762 1763 void serviceTimeout(ProcessRecord proc) { 1764 String anrMessage = null; 1765 1766 synchronized(this) { 1767 if (proc.executingServices.size() == 0 || proc.thread == null) { 1768 return; 1769 } 1770 long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT; 1771 Iterator<ServiceRecord> it = proc.executingServices.iterator(); 1772 ServiceRecord timeout = null; 1773 long nextTime = 0; 1774 while (it.hasNext()) { 1775 ServiceRecord sr = it.next(); 1776 if (sr.executingStart < maxTime) { 1777 timeout = sr; 1778 break; 1779 } 1780 if (sr.executingStart > nextTime) { 1781 nextTime = sr.executingStart; 1782 } 1783 } 1784 if (timeout != null && mAm.mLruProcesses.contains(proc)) { 1785 Slog.w(TAG, "Timeout executing service: " + timeout); 1786 anrMessage = "Executing service " + timeout.shortName; 1787 } else { 1788 Message msg = mAm.mHandler.obtainMessage( 1789 ActivityManagerService.SERVICE_TIMEOUT_MSG); 1790 msg.obj = proc; 1791 mAm.mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT); 1792 } 1793 } 1794 1795 if (anrMessage != null) { 1796 mAm.appNotResponding(proc, null, null, anrMessage); 1797 } 1798 } 1799 1800 /** 1801 * Prints a list of ServiceRecords (dumpsys activity services) 1802 */ 1803 boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args, 1804 int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { 1805 boolean needSep = false; 1806 1807 ItemMatcher matcher = new ItemMatcher(); 1808 matcher.build(args, opti); 1809 1810 pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)"); 1811 try { 1812 List<UserInfo> users = mAm.getUserManager().getUsers(); 1813 for (int ui=0; ui<users.size(); ui++) { 1814 final UserInfo user = users.get(ui); 1815 if (mServiceMap.getAllServices(user.id).size() > 0) { 1816 boolean printed = false; 1817 long nowReal = SystemClock.elapsedRealtime(); 1818 Iterator<ServiceRecord> it = mServiceMap.getAllServices( 1819 user.id).iterator(); 1820 needSep = false; 1821 while (it.hasNext()) { 1822 ServiceRecord r = it.next(); 1823 if (!matcher.match(r, r.name)) { 1824 continue; 1825 } 1826 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 1827 continue; 1828 } 1829 if (!printed) { 1830 if (ui > 0) { 1831 pw.println(); 1832 } 1833 pw.println(" User " + user.id + " active services:"); 1834 printed = true; 1835 } 1836 if (needSep) { 1837 pw.println(); 1838 } 1839 pw.print(" * "); 1840 pw.println(r); 1841 if (dumpAll) { 1842 r.dump(pw, " "); 1843 needSep = true; 1844 } else { 1845 pw.print(" app="); 1846 pw.println(r.app); 1847 pw.print(" created="); 1848 TimeUtils.formatDuration(r.createTime, nowReal, pw); 1849 pw.print(" started="); 1850 pw.print(r.startRequested); 1851 pw.print(" connections="); 1852 pw.println(r.connections.size()); 1853 if (r.connections.size() > 0) { 1854 pw.println(" Connections:"); 1855 for (ArrayList<ConnectionRecord> clist : r.connections.values()) { 1856 for (int i = 0; i < clist.size(); i++) { 1857 ConnectionRecord conn = clist.get(i); 1858 pw.print(" "); 1859 pw.print(conn.binding.intent.intent.getIntent() 1860 .toShortString(false, false, false, false)); 1861 pw.print(" -> "); 1862 ProcessRecord proc = conn.binding.client; 1863 pw.println(proc != null ? proc.toShortString() : "null"); 1864 } 1865 } 1866 } 1867 } 1868 if (dumpClient && r.app != null && r.app.thread != null) { 1869 pw.println(" Client:"); 1870 pw.flush(); 1871 try { 1872 TransferPipe tp = new TransferPipe(); 1873 try { 1874 r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), 1875 r, args); 1876 tp.setBufferPrefix(" "); 1877 // Short timeout, since blocking here can 1878 // deadlock with the application. 1879 tp.go(fd, 2000); 1880 } finally { 1881 tp.kill(); 1882 } 1883 } catch (IOException e) { 1884 pw.println(" Failure while dumping the service: " + e); 1885 } catch (RemoteException e) { 1886 pw.println(" Got a RemoteException while dumping the service"); 1887 } 1888 needSep = true; 1889 } 1890 } 1891 needSep = printed; 1892 } 1893 } 1894 } catch (Exception e) { 1895 Log.w(TAG, "Exception in dumpServicesLocked: " + e); 1896 } 1897 1898 if (mPendingServices.size() > 0) { 1899 boolean printed = false; 1900 for (int i=0; i<mPendingServices.size(); i++) { 1901 ServiceRecord r = mPendingServices.get(i); 1902 if (!matcher.match(r, r.name)) { 1903 continue; 1904 } 1905 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 1906 continue; 1907 } 1908 if (!printed) { 1909 if (needSep) pw.println(" "); 1910 needSep = true; 1911 pw.println(" Pending services:"); 1912 printed = true; 1913 } 1914 pw.print(" * Pending "); pw.println(r); 1915 r.dump(pw, " "); 1916 } 1917 needSep = true; 1918 } 1919 1920 if (mRestartingServices.size() > 0) { 1921 boolean printed = false; 1922 for (int i=0; i<mRestartingServices.size(); i++) { 1923 ServiceRecord r = mRestartingServices.get(i); 1924 if (!matcher.match(r, r.name)) { 1925 continue; 1926 } 1927 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 1928 continue; 1929 } 1930 if (!printed) { 1931 if (needSep) pw.println(" "); 1932 needSep = true; 1933 pw.println(" Restarting services:"); 1934 printed = true; 1935 } 1936 pw.print(" * Restarting "); pw.println(r); 1937 r.dump(pw, " "); 1938 } 1939 needSep = true; 1940 } 1941 1942 if (mStoppingServices.size() > 0) { 1943 boolean printed = false; 1944 for (int i=0; i<mStoppingServices.size(); i++) { 1945 ServiceRecord r = mStoppingServices.get(i); 1946 if (!matcher.match(r, r.name)) { 1947 continue; 1948 } 1949 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 1950 continue; 1951 } 1952 if (!printed) { 1953 if (needSep) pw.println(" "); 1954 needSep = true; 1955 pw.println(" Stopping services:"); 1956 printed = true; 1957 } 1958 pw.print(" * Stopping "); pw.println(r); 1959 r.dump(pw, " "); 1960 } 1961 needSep = true; 1962 } 1963 1964 if (dumpAll) { 1965 if (mServiceConnections.size() > 0) { 1966 boolean printed = false; 1967 Iterator<ArrayList<ConnectionRecord>> it 1968 = mServiceConnections.values().iterator(); 1969 while (it.hasNext()) { 1970 ArrayList<ConnectionRecord> r = it.next(); 1971 for (int i=0; i<r.size(); i++) { 1972 ConnectionRecord cr = r.get(i); 1973 if (!matcher.match(cr.binding.service, cr.binding.service.name)) { 1974 continue; 1975 } 1976 if (dumpPackage != null && (cr.binding.client == null 1977 || !dumpPackage.equals(cr.binding.client.info.packageName))) { 1978 continue; 1979 } 1980 if (!printed) { 1981 if (needSep) pw.println(" "); 1982 needSep = true; 1983 pw.println(" Connection bindings to services:"); 1984 printed = true; 1985 } 1986 pw.print(" * "); pw.println(cr); 1987 cr.dump(pw, " "); 1988 } 1989 } 1990 needSep = true; 1991 } 1992 } 1993 1994 return needSep; 1995 } 1996 1997 /** 1998 * There are three ways to call this: 1999 * - no service specified: dump all the services 2000 * - a flattened component name that matched an existing service was specified as the 2001 * first arg: dump that one service 2002 * - the first arg isn't the flattened component name of an existing service: 2003 * dump all services whose component contains the first arg as a substring 2004 */ 2005 protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args, 2006 int opti, boolean dumpAll) { 2007 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 2008 2009 List<UserInfo> users = mAm.getUserManager().getUsers(); 2010 if ("all".equals(name)) { 2011 synchronized (this) { 2012 for (UserInfo user : users) { 2013 for (ServiceRecord r1 : mServiceMap.getAllServices(user.id)) { 2014 services.add(r1); 2015 } 2016 } 2017 } 2018 } else { 2019 ComponentName componentName = name != null 2020 ? ComponentName.unflattenFromString(name) : null; 2021 int objectId = 0; 2022 if (componentName == null) { 2023 // Not a '/' separated full component name; maybe an object ID? 2024 try { 2025 objectId = Integer.parseInt(name, 16); 2026 name = null; 2027 componentName = null; 2028 } catch (RuntimeException e) { 2029 } 2030 } 2031 2032 synchronized (this) { 2033 for (UserInfo user : users) { 2034 for (ServiceRecord r1 : mServiceMap.getAllServices(user.id)) { 2035 if (componentName != null) { 2036 if (r1.name.equals(componentName)) { 2037 services.add(r1); 2038 } 2039 } else if (name != null) { 2040 if (r1.name.flattenToString().contains(name)) { 2041 services.add(r1); 2042 } 2043 } else if (System.identityHashCode(r1) == objectId) { 2044 services.add(r1); 2045 } 2046 } 2047 } 2048 } 2049 } 2050 2051 if (services.size() <= 0) { 2052 return false; 2053 } 2054 2055 boolean needSep = false; 2056 for (int i=0; i<services.size(); i++) { 2057 if (needSep) { 2058 pw.println(); 2059 } 2060 needSep = true; 2061 dumpService("", fd, pw, services.get(i), args, dumpAll); 2062 } 2063 return true; 2064 } 2065 2066 /** 2067 * Invokes IApplicationThread.dumpService() on the thread of the specified service if 2068 * there is a thread associated with the service. 2069 */ 2070 private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw, 2071 final ServiceRecord r, String[] args, boolean dumpAll) { 2072 String innerPrefix = prefix + " "; 2073 synchronized (this) { 2074 pw.print(prefix); pw.print("SERVICE "); 2075 pw.print(r.shortName); pw.print(" "); 2076 pw.print(Integer.toHexString(System.identityHashCode(r))); 2077 pw.print(" pid="); 2078 if (r.app != null) pw.println(r.app.pid); 2079 else pw.println("(not running)"); 2080 if (dumpAll) { 2081 r.dump(pw, innerPrefix); 2082 } 2083 } 2084 if (r.app != null && r.app.thread != null) { 2085 pw.print(prefix); pw.println(" Client:"); 2086 pw.flush(); 2087 try { 2088 TransferPipe tp = new TransferPipe(); 2089 try { 2090 r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), r, args); 2091 tp.setBufferPrefix(prefix + " "); 2092 tp.go(fd); 2093 } finally { 2094 tp.kill(); 2095 } 2096 } catch (IOException e) { 2097 pw.println(prefix + " Failure while dumping the service: " + e); 2098 } catch (RemoteException e) { 2099 pw.println(prefix + " Got a RemoteException while dumping the service"); 2100 } 2101 } 2102 } 2103 2104} 2105