ActiveServices.java revision 7d19e0242faac8017033dabb872cdf1542fa184c
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)); 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 = findServiceLocked(service, resolvedType, 281 callerApp == null ? UserId.getCallingUserId() : callerApp.userId); 282 if (r != null) { 283 if (r.record != null) { 284 final long origId = Binder.clearCallingIdentity(); 285 try { 286 stopServiceLocked(r.record); 287 } finally { 288 Binder.restoreCallingIdentity(origId); 289 } 290 return 1; 291 } 292 return -1; 293 } 294 295 return 0; 296 } 297 298 IBinder peekServiceLocked(Intent service, String resolvedType) { 299 ServiceLookupResult r = findServiceLocked(service, resolvedType, 300 UserId.getCallingUserId()); 301 302 IBinder ret = null; 303 if (r != null) { 304 // r.record is null if findServiceLocked() failed the caller permission check 305 if (r.record == null) { 306 throw new SecurityException( 307 "Permission Denial: Accessing service " + r.record.name 308 + " from pid=" + Binder.getCallingPid() 309 + ", uid=" + Binder.getCallingUid() 310 + " requires " + r.permission); 311 } 312 IntentBindRecord ib = r.record.bindings.get(r.record.intent); 313 if (ib != null) { 314 ret = ib.binder; 315 } 316 } 317 318 return ret; 319 } 320 321 boolean stopServiceTokenLocked(ComponentName className, IBinder token, 322 int startId) { 323 if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className 324 + " " + token + " startId=" + startId); 325 ServiceRecord r = findServiceLocked(className, token); 326 if (r != null) { 327 if (startId >= 0) { 328 // Asked to only stop if done with all work. Note that 329 // to avoid leaks, we will take this as dropping all 330 // start items up to and including this one. 331 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); 332 if (si != null) { 333 while (r.deliveredStarts.size() > 0) { 334 ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); 335 cur.removeUriPermissionsLocked(); 336 if (cur == si) { 337 break; 338 } 339 } 340 } 341 342 if (r.getLastStartId() != startId) { 343 return false; 344 } 345 346 if (r.deliveredStarts.size() > 0) { 347 Slog.w(TAG, "stopServiceToken startId " + startId 348 + " is last, but have " + r.deliveredStarts.size() 349 + " remaining args"); 350 } 351 } 352 353 synchronized (r.stats.getBatteryStats()) { 354 r.stats.stopRunningLocked(); 355 r.startRequested = false; 356 r.callStart = false; 357 } 358 final long origId = Binder.clearCallingIdentity(); 359 bringDownServiceLocked(r, false); 360 Binder.restoreCallingIdentity(origId); 361 return true; 362 } 363 return false; 364 } 365 366 public void setServiceForegroundLocked(ComponentName className, IBinder token, 367 int id, Notification notification, boolean removeNotification) { 368 final long origId = Binder.clearCallingIdentity(); 369 try { 370 ServiceRecord r = findServiceLocked(className, token); 371 if (r != null) { 372 if (id != 0) { 373 if (notification == null) { 374 throw new IllegalArgumentException("null notification"); 375 } 376 if (r.foregroundId != id) { 377 r.cancelNotification(); 378 r.foregroundId = id; 379 } 380 notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 381 r.foregroundNoti = notification; 382 r.isForeground = true; 383 r.postNotification(); 384 if (r.app != null) { 385 updateServiceForegroundLocked(r.app, true); 386 } 387 } else { 388 if (r.isForeground) { 389 r.isForeground = false; 390 if (r.app != null) { 391 mAm.updateLruProcessLocked(r.app, false, true); 392 updateServiceForegroundLocked(r.app, true); 393 } 394 } 395 if (removeNotification) { 396 r.cancelNotification(); 397 r.foregroundId = 0; 398 r.foregroundNoti = null; 399 } 400 } 401 } 402 } finally { 403 Binder.restoreCallingIdentity(origId); 404 } 405 } 406 407 private void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) { 408 boolean anyForeground = false; 409 for (ServiceRecord sr : proc.services) { 410 if (sr.isForeground) { 411 anyForeground = true; 412 break; 413 } 414 } 415 if (anyForeground != proc.foregroundServices) { 416 proc.foregroundServices = anyForeground; 417 if (oomAdj) { 418 mAm.updateOomAdjLocked(); 419 } 420 } 421 } 422 423 int bindServiceLocked(IApplicationThread caller, IBinder token, 424 Intent service, String resolvedType, 425 IServiceConnection connection, int flags, int userId) { 426 if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service 427 + " type=" + resolvedType + " conn=" + connection.asBinder() 428 + " flags=0x" + Integer.toHexString(flags)); 429 if (DEBUG_MU) 430 Slog.i(TAG_MU, "bindService uid=" + Binder.getCallingUid() + " origUid=" 431 + Binder.getOrigCallingUid()); 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); 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); 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) { 680 ServiceRecord r = mServiceMap.getServiceByName(name, Binder.getOrigCallingUser()); 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 ServiceLookupResult findServiceLocked(Intent service, 695 String resolvedType, int userId) { 696 ServiceRecord r = null; 697 if (service.getComponent() != null) { 698 r = mServiceMap.getServiceByName(service.getComponent(), userId); 699 } 700 if (r == null) { 701 Intent.FilterComparison filter = new Intent.FilterComparison(service); 702 r = mServiceMap.getServiceByIntent(filter, userId); 703 } 704 705 if (r == null) { 706 try { 707 ResolveInfo rInfo = 708 AppGlobals.getPackageManager().resolveService( 709 service, resolvedType, 0, userId); 710 ServiceInfo sInfo = 711 rInfo != null ? rInfo.serviceInfo : null; 712 if (sInfo == null) { 713 return null; 714 } 715 716 ComponentName name = new ComponentName( 717 sInfo.applicationInfo.packageName, sInfo.name); 718 r = mServiceMap.getServiceByName(name, Binder.getOrigCallingUser()); 719 } catch (RemoteException ex) { 720 // pm is in same process, this will never happen. 721 } 722 } 723 if (r != null) { 724 int callingPid = Binder.getCallingPid(); 725 int callingUid = Binder.getCallingUid(); 726 if (mAm.checkComponentPermission(r.permission, 727 callingPid, callingUid, r.appInfo.uid, r.exported) 728 != PackageManager.PERMISSION_GRANTED) { 729 if (!r.exported) { 730 Slog.w(TAG, "Permission Denial: Accessing service " + r.name 731 + " from pid=" + callingPid 732 + ", uid=" + callingUid 733 + " that is not exported from uid " + r.appInfo.uid); 734 return new ServiceLookupResult(null, "not exported from uid " 735 + r.appInfo.uid); 736 } 737 Slog.w(TAG, "Permission Denial: Accessing service " + r.name 738 + " from pid=" + callingPid 739 + ", uid=" + callingUid 740 + " requires " + r.permission); 741 return new ServiceLookupResult(null, r.permission); 742 } 743 return new ServiceLookupResult(r, null); 744 } 745 return null; 746 } 747 748 private class ServiceRestarter implements Runnable { 749 private ServiceRecord mService; 750 751 void setService(ServiceRecord service) { 752 mService = service; 753 } 754 755 public void run() { 756 synchronized(mAm) { 757 performServiceRestartLocked(mService); 758 } 759 } 760 } 761 762 private ServiceLookupResult retrieveServiceLocked(Intent service, 763 String resolvedType, int callingPid, int callingUid, int userId) { 764 ServiceRecord r = null; 765 if (DEBUG_SERVICE) Slog.v(TAG, "retrieveServiceLocked: " + service 766 + " type=" + resolvedType + " callingUid=" + callingUid); 767 768 if (service.getComponent() != null) { 769 r = mServiceMap.getServiceByName(service.getComponent(), userId); 770 } 771 if (r == null) { 772 Intent.FilterComparison filter = new Intent.FilterComparison(service); 773 r = mServiceMap.getServiceByIntent(filter, userId); 774 } 775 if (r == null) { 776 try { 777 ResolveInfo rInfo = 778 AppGlobals.getPackageManager().resolveService( 779 service, resolvedType, 780 ActivityManagerService.STOCK_PM_FLAGS, userId); 781 ServiceInfo sInfo = 782 rInfo != null ? rInfo.serviceInfo : null; 783 if (sInfo == null) { 784 Slog.w(TAG, "Unable to start service " + service + 785 ": not found"); 786 return null; 787 } 788 ComponentName name = new ComponentName( 789 sInfo.applicationInfo.packageName, sInfo.name); 790 if (userId > 0) { 791 if (mAm.isSingleton(sInfo.processName, sInfo.applicationInfo, 792 sInfo.name, sInfo.flags)) { 793 userId = 0; 794 } 795 sInfo = new ServiceInfo(sInfo); 796 sInfo.applicationInfo = mAm.getAppInfoForUser(sInfo.applicationInfo, userId); 797 } 798 r = mServiceMap.getServiceByName(name, userId); 799 if (r == null) { 800 Intent.FilterComparison filter = new Intent.FilterComparison( 801 service.cloneFilter()); 802 ServiceRestarter res = new ServiceRestarter(); 803 BatteryStatsImpl.Uid.Pkg.Serv ss = null; 804 BatteryStatsImpl stats = mAm.mBatteryStatsService.getActiveStatistics(); 805 synchronized (stats) { 806 ss = stats.getServiceStatsLocked( 807 sInfo.applicationInfo.uid, sInfo.packageName, 808 sInfo.name); 809 } 810 r = new ServiceRecord(mAm, ss, name, filter, sInfo, res); 811 res.setService(r); 812 mServiceMap.putServiceByName(name, UserId.getUserId(r.appInfo.uid), r); 813 mServiceMap.putServiceByIntent(filter, UserId.getUserId(r.appInfo.uid), r); 814 815 // Make sure this component isn't in the pending list. 816 int N = mPendingServices.size(); 817 for (int i=0; i<N; i++) { 818 ServiceRecord pr = mPendingServices.get(i); 819 if (pr.name.equals(name)) { 820 mPendingServices.remove(i); 821 i--; 822 N--; 823 } 824 } 825 } 826 } catch (RemoteException ex) { 827 // pm is in same process, this will never happen. 828 } 829 } 830 if (r != null) { 831 if (mAm.checkComponentPermission(r.permission, 832 callingPid, callingUid, r.appInfo.uid, r.exported) 833 != PackageManager.PERMISSION_GRANTED) { 834 if (!r.exported) { 835 Slog.w(TAG, "Permission Denial: Accessing service " + r.name 836 + " from pid=" + callingPid 837 + ", uid=" + callingUid 838 + " that is not exported from uid " + r.appInfo.uid); 839 return new ServiceLookupResult(null, "not exported from uid " 840 + r.appInfo.uid); 841 } 842 Slog.w(TAG, "Permission Denial: Accessing service " + r.name 843 + " from pid=" + callingPid 844 + ", uid=" + callingUid 845 + " requires " + r.permission); 846 return new ServiceLookupResult(null, r.permission); 847 } 848 return new ServiceLookupResult(r, null); 849 } 850 return null; 851 } 852 853 private final void bumpServiceExecutingLocked(ServiceRecord r, String why) { 854 if (DEBUG_SERVICE) Log.v(TAG, ">>> EXECUTING " 855 + why + " of " + r + " in app " + r.app); 856 else if (DEBUG_SERVICE_EXECUTING) Log.v(TAG, ">>> EXECUTING " 857 + why + " of " + r.shortName); 858 long now = SystemClock.uptimeMillis(); 859 if (r.executeNesting == 0 && r.app != null) { 860 if (r.app.executingServices.size() == 0) { 861 Message msg = mAm.mHandler.obtainMessage( 862 ActivityManagerService.SERVICE_TIMEOUT_MSG); 863 msg.obj = r.app; 864 mAm.mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT); 865 } 866 r.app.executingServices.add(r); 867 } 868 r.executeNesting++; 869 r.executingStart = now; 870 } 871 872 private final boolean requestServiceBindingLocked(ServiceRecord r, 873 IntentBindRecord i, boolean rebind) { 874 if (r.app == null || r.app.thread == null) { 875 // If service is not currently running, can't yet bind. 876 return false; 877 } 878 if ((!i.requested || rebind) && i.apps.size() > 0) { 879 try { 880 bumpServiceExecutingLocked(r, "bind"); 881 r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind); 882 if (!rebind) { 883 i.requested = true; 884 } 885 i.hasBound = true; 886 i.doRebind = false; 887 } catch (RemoteException e) { 888 if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while binding " + r); 889 return false; 890 } 891 } 892 return true; 893 } 894 895 private final boolean scheduleServiceRestartLocked(ServiceRecord r, 896 boolean allowCancel) { 897 boolean canceled = false; 898 899 final long now = SystemClock.uptimeMillis(); 900 long minDuration = SERVICE_RESTART_DURATION; 901 long resetTime = SERVICE_RESET_RUN_DURATION; 902 903 if ((r.serviceInfo.applicationInfo.flags 904 &ApplicationInfo.FLAG_PERSISTENT) != 0) { 905 minDuration /= 4; 906 } 907 908 // Any delivered but not yet finished starts should be put back 909 // on the pending list. 910 final int N = r.deliveredStarts.size(); 911 if (N > 0) { 912 for (int i=N-1; i>=0; i--) { 913 ServiceRecord.StartItem si = r.deliveredStarts.get(i); 914 si.removeUriPermissionsLocked(); 915 if (si.intent == null) { 916 // We'll generate this again if needed. 917 } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT 918 && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) { 919 r.pendingStarts.add(0, si); 920 long dur = SystemClock.uptimeMillis() - si.deliveredTime; 921 dur *= 2; 922 if (minDuration < dur) minDuration = dur; 923 if (resetTime < dur) resetTime = dur; 924 } else { 925 Slog.w(TAG, "Canceling start item " + si.intent + " in service " 926 + r.name); 927 canceled = true; 928 } 929 } 930 r.deliveredStarts.clear(); 931 } 932 933 r.totalRestartCount++; 934 if (r.restartDelay == 0) { 935 r.restartCount++; 936 r.restartDelay = minDuration; 937 } else { 938 // If it has been a "reasonably long time" since the service 939 // was started, then reset our restart duration back to 940 // the beginning, so we don't infinitely increase the duration 941 // on a service that just occasionally gets killed (which is 942 // a normal case, due to process being killed to reclaim memory). 943 if (now > (r.restartTime+resetTime)) { 944 r.restartCount = 1; 945 r.restartDelay = minDuration; 946 } else { 947 if ((r.serviceInfo.applicationInfo.flags 948 &ApplicationInfo.FLAG_PERSISTENT) != 0) { 949 // Services in peristent processes will restart much more 950 // quickly, since they are pretty important. (Think SystemUI). 951 r.restartDelay += minDuration/2; 952 } else { 953 r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR; 954 if (r.restartDelay < minDuration) { 955 r.restartDelay = minDuration; 956 } 957 } 958 } 959 } 960 961 r.nextRestartTime = now + r.restartDelay; 962 963 // Make sure that we don't end up restarting a bunch of services 964 // all at the same time. 965 boolean repeat; 966 do { 967 repeat = false; 968 for (int i=mRestartingServices.size()-1; i>=0; i--) { 969 ServiceRecord r2 = mRestartingServices.get(i); 970 if (r2 != r && r.nextRestartTime 971 >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN) 972 && r.nextRestartTime 973 < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) { 974 r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN; 975 r.restartDelay = r.nextRestartTime - now; 976 repeat = true; 977 break; 978 } 979 } 980 } while (repeat); 981 982 if (!mRestartingServices.contains(r)) { 983 mRestartingServices.add(r); 984 } 985 986 r.cancelNotification(); 987 988 mAm.mHandler.removeCallbacks(r.restarter); 989 mAm.mHandler.postAtTime(r.restarter, r.nextRestartTime); 990 r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay; 991 Slog.w(TAG, "Scheduling restart of crashed service " 992 + r.shortName + " in " + r.restartDelay + "ms"); 993 EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART, 994 r.shortName, r.restartDelay); 995 996 return canceled; 997 } 998 999 final void performServiceRestartLocked(ServiceRecord r) { 1000 if (!mRestartingServices.contains(r)) { 1001 return; 1002 } 1003 bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true); 1004 } 1005 1006 private final boolean unscheduleServiceRestartLocked(ServiceRecord r) { 1007 if (r.restartDelay == 0) { 1008 return false; 1009 } 1010 r.resetRestartCounter(); 1011 mRestartingServices.remove(r); 1012 mAm.mHandler.removeCallbacks(r.restarter); 1013 return true; 1014 } 1015 1016 private final boolean bringUpServiceLocked(ServiceRecord r, 1017 int intentFlags, boolean whileRestarting) { 1018 //Slog.i(TAG, "Bring up service:"); 1019 //r.dump(" "); 1020 1021 if (r.app != null && r.app.thread != null) { 1022 sendServiceArgsLocked(r, false); 1023 return true; 1024 } 1025 1026 if (!whileRestarting && r.restartDelay > 0) { 1027 // If waiting for a restart, then do nothing. 1028 return true; 1029 } 1030 1031 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing up " + r + " " + r.intent); 1032 1033 // We are now bringing the service up, so no longer in the 1034 // restarting state. 1035 mRestartingServices.remove(r); 1036 1037 // Service is now being launched, its package can't be stopped. 1038 try { 1039 AppGlobals.getPackageManager().setPackageStoppedState( 1040 r.packageName, false, r.userId); 1041 } catch (RemoteException e) { 1042 } catch (IllegalArgumentException e) { 1043 Slog.w(TAG, "Failed trying to unstop package " 1044 + r.packageName + ": " + e); 1045 } 1046 1047 final boolean isolated = (r.serviceInfo.flags&ServiceInfo.FLAG_ISOLATED_PROCESS) != 0; 1048 final String procName = r.processName; 1049 ProcessRecord app; 1050 1051 if (!isolated) { 1052 app = mAm.getProcessRecordLocked(procName, r.appInfo.uid); 1053 if (DEBUG_MU) 1054 Slog.v(TAG_MU, "bringUpServiceLocked: appInfo.uid=" + r.appInfo.uid + " app=" + app); 1055 if (app != null && app.thread != null) { 1056 try { 1057 app.addPackage(r.appInfo.packageName); 1058 realStartServiceLocked(r, app); 1059 return true; 1060 } catch (RemoteException e) { 1061 Slog.w(TAG, "Exception when starting service " + r.shortName, e); 1062 } 1063 1064 // If a dead object exception was thrown -- fall through to 1065 // restart the application. 1066 } 1067 } else { 1068 // If this service runs in an isolated process, then each time 1069 // we call startProcessLocked() we will get a new isolated 1070 // process, starting another process if we are currently waiting 1071 // for a previous process to come up. To deal with this, we store 1072 // in the service any current isolated process it is running in or 1073 // waiting to have come up. 1074 app = r.isolatedProc; 1075 } 1076 1077 // Not running -- get it started, and enqueue this service record 1078 // to be executed when the app comes up. 1079 if (app == null) { 1080 if ((app=mAm.startProcessLocked(procName, r.appInfo, true, intentFlags, 1081 "service", r.name, false, isolated)) == null) { 1082 Slog.w(TAG, "Unable to launch app " 1083 + r.appInfo.packageName + "/" 1084 + r.appInfo.uid + " for service " 1085 + r.intent.getIntent() + ": process is bad"); 1086 bringDownServiceLocked(r, true); 1087 return false; 1088 } 1089 if (isolated) { 1090 r.isolatedProc = app; 1091 } 1092 } 1093 1094 if (!mPendingServices.contains(r)) { 1095 mPendingServices.add(r); 1096 } 1097 1098 return true; 1099 } 1100 1101 private final void requestServiceBindingsLocked(ServiceRecord r) { 1102 Iterator<IntentBindRecord> bindings = r.bindings.values().iterator(); 1103 while (bindings.hasNext()) { 1104 IntentBindRecord i = bindings.next(); 1105 if (!requestServiceBindingLocked(r, i, false)) { 1106 break; 1107 } 1108 } 1109 } 1110 1111 private final void realStartServiceLocked(ServiceRecord r, 1112 ProcessRecord app) throws RemoteException { 1113 if (app.thread == null) { 1114 throw new RemoteException(); 1115 } 1116 if (DEBUG_MU) 1117 Slog.v(TAG_MU, "realStartServiceLocked, ServiceRecord.uid = " + r.appInfo.uid 1118 + ", ProcessRecord.uid = " + app.uid); 1119 r.app = app; 1120 r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); 1121 1122 app.services.add(r); 1123 bumpServiceExecutingLocked(r, "create"); 1124 mAm.updateLruProcessLocked(app, true, true); 1125 1126 boolean created = false; 1127 try { 1128 mAm.mStringBuilder.setLength(0); 1129 r.intent.getIntent().toShortString(mAm.mStringBuilder, true, false, true, false); 1130 EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE, 1131 System.identityHashCode(r), r.shortName, 1132 mAm.mStringBuilder.toString(), r.app.pid); 1133 synchronized (r.stats.getBatteryStats()) { 1134 r.stats.startLaunchedLocked(); 1135 } 1136 mAm.ensurePackageDexOpt(r.serviceInfo.packageName); 1137 app.thread.scheduleCreateService(r, r.serviceInfo, 1138 mAm.compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo)); 1139 r.postNotification(); 1140 created = true; 1141 } finally { 1142 if (!created) { 1143 app.services.remove(r); 1144 scheduleServiceRestartLocked(r, false); 1145 } 1146 } 1147 1148 requestServiceBindingsLocked(r); 1149 1150 // If the service is in the started state, and there are no 1151 // pending arguments, then fake up one so its onStartCommand() will 1152 // be called. 1153 if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { 1154 r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), 1155 null, null)); 1156 } 1157 1158 sendServiceArgsLocked(r, true); 1159 } 1160 1161 private final void sendServiceArgsLocked(ServiceRecord r, 1162 boolean oomAdjusted) { 1163 final int N = r.pendingStarts.size(); 1164 if (N == 0) { 1165 return; 1166 } 1167 1168 while (r.pendingStarts.size() > 0) { 1169 try { 1170 ServiceRecord.StartItem si = r.pendingStarts.remove(0); 1171 if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: " 1172 + r + " " + r.intent + " args=" + si.intent); 1173 if (si.intent == null && N > 1) { 1174 // If somehow we got a dummy null intent in the middle, 1175 // then skip it. DO NOT skip a null intent when it is 1176 // the only one in the list -- this is to support the 1177 // onStartCommand(null) case. 1178 continue; 1179 } 1180 si.deliveredTime = SystemClock.uptimeMillis(); 1181 r.deliveredStarts.add(si); 1182 si.deliveryCount++; 1183 if (si.neededGrants != null) { 1184 mAm.grantUriPermissionUncheckedFromIntentLocked(si.neededGrants, 1185 si.getUriPermissionsLocked()); 1186 } 1187 bumpServiceExecutingLocked(r, "start"); 1188 if (!oomAdjusted) { 1189 oomAdjusted = true; 1190 mAm.updateOomAdjLocked(r.app); 1191 } 1192 int flags = 0; 1193 if (si.deliveryCount > 1) { 1194 flags |= Service.START_FLAG_RETRY; 1195 } 1196 if (si.doneExecutingCount > 0) { 1197 flags |= Service.START_FLAG_REDELIVERY; 1198 } 1199 r.app.thread.scheduleServiceArgs(r, si.taskRemoved, si.id, flags, si.intent); 1200 } catch (RemoteException e) { 1201 // Remote process gone... we'll let the normal cleanup take 1202 // care of this. 1203 if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r); 1204 break; 1205 } catch (Exception e) { 1206 Slog.w(TAG, "Unexpected exception", e); 1207 break; 1208 } 1209 } 1210 } 1211 1212 private final void bringDownServiceLocked(ServiceRecord r, boolean force) { 1213 //Slog.i(TAG, "Bring down service:"); 1214 //r.dump(" "); 1215 1216 // Does it still need to run? 1217 if (!force && r.startRequested) { 1218 return; 1219 } 1220 if (r.connections.size() > 0) { 1221 if (!force) { 1222 // XXX should probably keep a count of the number of auto-create 1223 // connections directly in the service. 1224 Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); 1225 while (it.hasNext()) { 1226 ArrayList<ConnectionRecord> cr = it.next(); 1227 for (int i=0; i<cr.size(); i++) { 1228 if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) { 1229 return; 1230 } 1231 } 1232 } 1233 } 1234 1235 // Report to all of the connections that the service is no longer 1236 // available. 1237 Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator(); 1238 while (it.hasNext()) { 1239 ArrayList<ConnectionRecord> c = it.next(); 1240 for (int i=0; i<c.size(); i++) { 1241 ConnectionRecord cr = c.get(i); 1242 // There is still a connection to the service that is 1243 // being brought down. Mark it as dead. 1244 cr.serviceDead = true; 1245 try { 1246 cr.conn.connected(r.name, null); 1247 } catch (Exception e) { 1248 Slog.w(TAG, "Failure disconnecting service " + r.name + 1249 " to connection " + c.get(i).conn.asBinder() + 1250 " (in " + c.get(i).binding.client.processName + ")", e); 1251 } 1252 } 1253 } 1254 } 1255 1256 // Tell the service that it has been unbound. 1257 if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) { 1258 Iterator<IntentBindRecord> it = r.bindings.values().iterator(); 1259 while (it.hasNext()) { 1260 IntentBindRecord ibr = it.next(); 1261 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down binding " + ibr 1262 + ": hasBound=" + ibr.hasBound); 1263 if (r.app != null && r.app.thread != null && ibr.hasBound) { 1264 try { 1265 bumpServiceExecutingLocked(r, "bring down unbind"); 1266 mAm.updateOomAdjLocked(r.app); 1267 ibr.hasBound = false; 1268 r.app.thread.scheduleUnbindService(r, 1269 ibr.intent.getIntent()); 1270 } catch (Exception e) { 1271 Slog.w(TAG, "Exception when unbinding service " 1272 + r.shortName, e); 1273 serviceDoneExecutingLocked(r, true); 1274 } 1275 } 1276 } 1277 } 1278 1279 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent); 1280 EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE, 1281 System.identityHashCode(r), r.shortName, 1282 (r.app != null) ? r.app.pid : -1); 1283 1284 mServiceMap.removeServiceByName(r.name, r.userId); 1285 mServiceMap.removeServiceByIntent(r.intent, r.userId); 1286 r.totalRestartCount = 0; 1287 unscheduleServiceRestartLocked(r); 1288 1289 // Also make sure it is not on the pending list. 1290 int N = mPendingServices.size(); 1291 for (int i=0; i<N; i++) { 1292 if (mPendingServices.get(i) == r) { 1293 mPendingServices.remove(i); 1294 if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r); 1295 i--; 1296 N--; 1297 } 1298 } 1299 1300 r.cancelNotification(); 1301 r.isForeground = false; 1302 r.foregroundId = 0; 1303 r.foregroundNoti = null; 1304 1305 // Clear start entries. 1306 r.clearDeliveredStartsLocked(); 1307 r.pendingStarts.clear(); 1308 1309 if (r.app != null) { 1310 synchronized (r.stats.getBatteryStats()) { 1311 r.stats.stopLaunchedLocked(); 1312 } 1313 r.app.services.remove(r); 1314 if (r.app.thread != null) { 1315 try { 1316 bumpServiceExecutingLocked(r, "stop"); 1317 mStoppingServices.add(r); 1318 mAm.updateOomAdjLocked(r.app); 1319 r.app.thread.scheduleStopService(r); 1320 } catch (Exception e) { 1321 Slog.w(TAG, "Exception when stopping service " 1322 + r.shortName, e); 1323 serviceDoneExecutingLocked(r, true); 1324 } 1325 updateServiceForegroundLocked(r.app, false); 1326 } else { 1327 if (DEBUG_SERVICE) Slog.v( 1328 TAG, "Removed service that has no process: " + r); 1329 } 1330 } else { 1331 if (DEBUG_SERVICE) Slog.v( 1332 TAG, "Removed service that is not running: " + r); 1333 } 1334 1335 if (r.bindings.size() > 0) { 1336 r.bindings.clear(); 1337 } 1338 1339 if (r.restarter instanceof ServiceRestarter) { 1340 ((ServiceRestarter)r.restarter).setService(null); 1341 } 1342 } 1343 1344 void removeConnectionLocked( 1345 ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { 1346 IBinder binder = c.conn.asBinder(); 1347 AppBindRecord b = c.binding; 1348 ServiceRecord s = b.service; 1349 ArrayList<ConnectionRecord> clist = s.connections.get(binder); 1350 if (clist != null) { 1351 clist.remove(c); 1352 if (clist.size() == 0) { 1353 s.connections.remove(binder); 1354 } 1355 } 1356 b.connections.remove(c); 1357 if (c.activity != null && c.activity != skipAct) { 1358 if (c.activity.connections != null) { 1359 c.activity.connections.remove(c); 1360 } 1361 } 1362 if (b.client != skipApp) { 1363 b.client.connections.remove(c); 1364 if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { 1365 b.client.updateHasAboveClientLocked(); 1366 } 1367 } 1368 clist = mServiceConnections.get(binder); 1369 if (clist != null) { 1370 clist.remove(c); 1371 if (clist.size() == 0) { 1372 mServiceConnections.remove(binder); 1373 } 1374 } 1375 1376 if (b.connections.size() == 0) { 1377 b.intent.apps.remove(b.client); 1378 } 1379 1380 if (!c.serviceDead) { 1381 if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent 1382 + ": shouldUnbind=" + b.intent.hasBound); 1383 if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 1384 && b.intent.hasBound) { 1385 try { 1386 bumpServiceExecutingLocked(s, "unbind"); 1387 mAm.updateOomAdjLocked(s.app); 1388 b.intent.hasBound = false; 1389 // Assume the client doesn't want to know about a rebind; 1390 // we will deal with that later if it asks for one. 1391 b.intent.doRebind = false; 1392 s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); 1393 } catch (Exception e) { 1394 Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); 1395 serviceDoneExecutingLocked(s, true); 1396 } 1397 } 1398 1399 if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { 1400 bringDownServiceLocked(s, false); 1401 } 1402 } 1403 } 1404 1405 void serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res) { 1406 boolean inStopping = mStoppingServices.contains(r); 1407 if (r != null) { 1408 if (type == 1) { 1409 // This is a call from a service start... take care of 1410 // book-keeping. 1411 r.callStart = true; 1412 switch (res) { 1413 case Service.START_STICKY_COMPATIBILITY: 1414 case Service.START_STICKY: { 1415 // We are done with the associated start arguments. 1416 r.findDeliveredStart(startId, true); 1417 // Don't stop if killed. 1418 r.stopIfKilled = false; 1419 break; 1420 } 1421 case Service.START_NOT_STICKY: { 1422 // We are done with the associated start arguments. 1423 r.findDeliveredStart(startId, true); 1424 if (r.getLastStartId() == startId) { 1425 // There is no more work, and this service 1426 // doesn't want to hang around if killed. 1427 r.stopIfKilled = true; 1428 } 1429 break; 1430 } 1431 case Service.START_REDELIVER_INTENT: { 1432 // We'll keep this item until they explicitly 1433 // call stop for it, but keep track of the fact 1434 // that it was delivered. 1435 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); 1436 if (si != null) { 1437 si.deliveryCount = 0; 1438 si.doneExecutingCount++; 1439 // Don't stop if killed. 1440 r.stopIfKilled = true; 1441 } 1442 break; 1443 } 1444 case Service.START_TASK_REMOVED_COMPLETE: { 1445 // Special processing for onTaskRemoved(). Don't 1446 // impact normal onStartCommand() processing. 1447 r.findDeliveredStart(startId, true); 1448 break; 1449 } 1450 default: 1451 throw new IllegalArgumentException( 1452 "Unknown service start result: " + res); 1453 } 1454 if (res == Service.START_STICKY_COMPATIBILITY) { 1455 r.callStart = false; 1456 } 1457 } 1458 if (DEBUG_MU) 1459 Slog.v(TAG_MU, "before serviceDontExecutingLocked, uid=" 1460 + Binder.getOrigCallingUid()); 1461 final long origId = Binder.clearCallingIdentity(); 1462 serviceDoneExecutingLocked(r, inStopping); 1463 Binder.restoreCallingIdentity(origId); 1464 } else { 1465 Slog.w(TAG, "Done executing unknown service from pid " 1466 + Binder.getCallingPid()); 1467 } 1468 } 1469 1470 private void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) { 1471 if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r 1472 + ": nesting=" + r.executeNesting 1473 + ", inStopping=" + inStopping + ", app=" + r.app); 1474 else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG, "<<< DONE EXECUTING " + r.shortName); 1475 r.executeNesting--; 1476 if (r.executeNesting <= 0 && r.app != null) { 1477 if (DEBUG_SERVICE) Slog.v(TAG, 1478 "Nesting at 0 of " + r.shortName); 1479 r.app.executingServices.remove(r); 1480 if (r.app.executingServices.size() == 0) { 1481 if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG, 1482 "No more executingServices of " + r.shortName); 1483 mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_TIMEOUT_MSG, r.app); 1484 } 1485 if (inStopping) { 1486 if (DEBUG_SERVICE) Slog.v(TAG, 1487 "doneExecuting remove stopping " + r); 1488 mStoppingServices.remove(r); 1489 r.bindings.clear(); 1490 } 1491 mAm.updateOomAdjLocked(r.app); 1492 } 1493 } 1494 1495 boolean attachApplicationLocked(ProcessRecord proc, String processName) throws Exception { 1496 boolean didSomething = false; 1497 if (mPendingServices.size() > 0) { 1498 ServiceRecord sr = null; 1499 try { 1500 for (int i=0; i<mPendingServices.size(); i++) { 1501 sr = mPendingServices.get(i); 1502 if (proc != sr.isolatedProc && (proc.uid != sr.appInfo.uid 1503 || !processName.equals(sr.processName))) { 1504 continue; 1505 } 1506 1507 mPendingServices.remove(i); 1508 i--; 1509 realStartServiceLocked(sr, proc); 1510 didSomething = true; 1511 } 1512 } catch (Exception e) { 1513 Slog.w(TAG, "Exception in new application when starting service " 1514 + sr.shortName, e); 1515 throw e; 1516 } 1517 } 1518 return didSomething; 1519 } 1520 1521 void processStartTimedOutLocked(ProcessRecord proc) { 1522 for (int i=0; i<mPendingServices.size(); i++) { 1523 ServiceRecord sr = mPendingServices.get(i); 1524 if ((proc.uid == sr.appInfo.uid 1525 && proc.processName.equals(sr.processName)) 1526 || sr.isolatedProc == proc) { 1527 Slog.w(TAG, "Forcing bringing down service: " + sr); 1528 sr.isolatedProc = null; 1529 mPendingServices.remove(i); 1530 i--; 1531 bringDownServiceLocked(sr, true); 1532 } 1533 } 1534 } 1535 1536 boolean forceStopLocked(String name, int userId, boolean evenPersistent, boolean doit) { 1537 boolean didSomething = false; 1538 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 1539 for (ServiceRecord service : mServiceMap.getAllServices(userId)) { 1540 if (service.packageName.equals(name) 1541 && (service.app == null || evenPersistent || !service.app.persistent)) { 1542 if (!doit) { 1543 return true; 1544 } 1545 didSomething = true; 1546 Slog.i(TAG, " Force stopping service " + service); 1547 if (service.app != null) { 1548 service.app.removed = true; 1549 } 1550 service.app = null; 1551 service.isolatedProc = null; 1552 services.add(service); 1553 } 1554 } 1555 1556 int N = services.size(); 1557 for (int i=0; i<N; i++) { 1558 bringDownServiceLocked(services.get(i), true); 1559 } 1560 return didSomething; 1561 } 1562 1563 void cleanUpRemovedTaskLocked(TaskRecord tr, ComponentName component, Intent baseIntent) { 1564 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 1565 for (ServiceRecord sr : mServiceMap.getAllServices(tr.userId)) { 1566 if (sr.packageName.equals(component.getPackageName())) { 1567 services.add(sr); 1568 } 1569 } 1570 1571 // Take care of any running services associated with the app. 1572 for (int i=0; i<services.size(); i++) { 1573 ServiceRecord sr = services.get(i); 1574 if (sr.startRequested) { 1575 if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) { 1576 Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task"); 1577 stopServiceLocked(sr); 1578 } else { 1579 sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true, 1580 sr.makeNextStartId(), baseIntent, null)); 1581 if (sr.app != null && sr.app.thread != null) { 1582 sendServiceArgsLocked(sr, false); 1583 } 1584 } 1585 } 1586 } 1587 } 1588 1589 final void killServicesLocked(ProcessRecord app, 1590 boolean allowRestart) { 1591 // Report disconnected services. 1592 if (false) { 1593 // XXX we are letting the client link to the service for 1594 // death notifications. 1595 if (app.services.size() > 0) { 1596 Iterator<ServiceRecord> it = app.services.iterator(); 1597 while (it.hasNext()) { 1598 ServiceRecord r = it.next(); 1599 if (r.connections.size() > 0) { 1600 Iterator<ArrayList<ConnectionRecord>> jt 1601 = r.connections.values().iterator(); 1602 while (jt.hasNext()) { 1603 ArrayList<ConnectionRecord> cl = jt.next(); 1604 for (int i=0; i<cl.size(); i++) { 1605 ConnectionRecord c = cl.get(i); 1606 if (c.binding.client != app) { 1607 try { 1608 //c.conn.connected(r.className, null); 1609 } catch (Exception e) { 1610 // todo: this should be asynchronous! 1611 Slog.w(TAG, "Exception thrown disconnected servce " 1612 + r.shortName 1613 + " from app " + app.processName, e); 1614 } 1615 } 1616 } 1617 } 1618 } 1619 } 1620 } 1621 } 1622 1623 // Clean up any connections this application has to other services. 1624 if (app.connections.size() > 0) { 1625 Iterator<ConnectionRecord> it = app.connections.iterator(); 1626 while (it.hasNext()) { 1627 ConnectionRecord r = it.next(); 1628 removeConnectionLocked(r, app, null); 1629 } 1630 } 1631 app.connections.clear(); 1632 1633 if (app.services.size() != 0) { 1634 // Any services running in the application need to be placed 1635 // back in the pending list. 1636 Iterator<ServiceRecord> it = app.services.iterator(); 1637 while (it.hasNext()) { 1638 ServiceRecord sr = it.next(); 1639 synchronized (sr.stats.getBatteryStats()) { 1640 sr.stats.stopLaunchedLocked(); 1641 } 1642 sr.app = null; 1643 sr.isolatedProc = null; 1644 sr.executeNesting = 0; 1645 if (mStoppingServices.remove(sr)) { 1646 if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); 1647 } 1648 1649 boolean hasClients = sr.bindings.size() > 0; 1650 if (hasClients) { 1651 Iterator<IntentBindRecord> bindings 1652 = sr.bindings.values().iterator(); 1653 while (bindings.hasNext()) { 1654 IntentBindRecord b = bindings.next(); 1655 if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b 1656 + ": shouldUnbind=" + b.hasBound); 1657 b.binder = null; 1658 b.requested = b.received = b.hasBound = false; 1659 } 1660 } 1661 1662 if (sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags 1663 &ApplicationInfo.FLAG_PERSISTENT) == 0) { 1664 Slog.w(TAG, "Service crashed " + sr.crashCount 1665 + " times, stopping: " + sr); 1666 EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, 1667 sr.crashCount, sr.shortName, app.pid); 1668 bringDownServiceLocked(sr, true); 1669 } else if (!allowRestart) { 1670 bringDownServiceLocked(sr, true); 1671 } else { 1672 boolean canceled = scheduleServiceRestartLocked(sr, true); 1673 1674 // Should the service remain running? Note that in the 1675 // extreme case of so many attempts to deliver a command 1676 // that it failed we also will stop it here. 1677 if (sr.startRequested && (sr.stopIfKilled || canceled)) { 1678 if (sr.pendingStarts.size() == 0) { 1679 sr.startRequested = false; 1680 if (!hasClients) { 1681 // Whoops, no reason to restart! 1682 bringDownServiceLocked(sr, true); 1683 } 1684 } 1685 } 1686 } 1687 } 1688 1689 if (!allowRestart) { 1690 app.services.clear(); 1691 } 1692 } 1693 1694 // Make sure we have no more records on the stopping list. 1695 int i = mStoppingServices.size(); 1696 while (i > 0) { 1697 i--; 1698 ServiceRecord sr = mStoppingServices.get(i); 1699 if (sr.app == app) { 1700 mStoppingServices.remove(i); 1701 if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr); 1702 } 1703 } 1704 1705 app.executingServices.clear(); 1706 } 1707 1708 ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { 1709 ActivityManager.RunningServiceInfo info = 1710 new ActivityManager.RunningServiceInfo(); 1711 info.service = r.name; 1712 if (r.app != null) { 1713 info.pid = r.app.pid; 1714 } 1715 info.uid = r.appInfo.uid; 1716 info.process = r.processName; 1717 info.foreground = r.isForeground; 1718 info.activeSince = r.createTime; 1719 info.started = r.startRequested; 1720 info.clientCount = r.connections.size(); 1721 info.crashCount = r.crashCount; 1722 info.lastActivityTime = r.lastActivity; 1723 if (r.isForeground) { 1724 info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; 1725 } 1726 if (r.startRequested) { 1727 info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; 1728 } 1729 if (r.app != null && r.app.pid == ActivityManagerService.MY_PID) { 1730 info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; 1731 } 1732 if (r.app != null && r.app.persistent) { 1733 info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; 1734 } 1735 1736 for (ArrayList<ConnectionRecord> connl : r.connections.values()) { 1737 for (int i=0; i<connl.size(); i++) { 1738 ConnectionRecord conn = connl.get(i); 1739 if (conn.clientLabel != 0) { 1740 info.clientPackage = conn.binding.client.info.packageName; 1741 info.clientLabel = conn.clientLabel; 1742 return info; 1743 } 1744 } 1745 } 1746 return info; 1747 } 1748 1749 List<ActivityManager.RunningServiceInfo> getRunningServiceInfoLocked(int maxNum, 1750 int flags) { 1751 ArrayList<ActivityManager.RunningServiceInfo> res 1752 = new ArrayList<ActivityManager.RunningServiceInfo>(); 1753 1754 int userId = UserId.getUserId(Binder.getCallingUid()); 1755 if (mServiceMap.getAllServices(userId).size() > 0) { 1756 Iterator<ServiceRecord> it 1757 = mServiceMap.getAllServices(userId).iterator(); 1758 while (it.hasNext() && res.size() < maxNum) { 1759 res.add(makeRunningServiceInfoLocked(it.next())); 1760 } 1761 } 1762 1763 for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { 1764 ServiceRecord r = mRestartingServices.get(i); 1765 ActivityManager.RunningServiceInfo info = 1766 makeRunningServiceInfoLocked(r); 1767 info.restarting = r.nextRestartTime; 1768 res.add(info); 1769 } 1770 1771 return res; 1772 } 1773 1774 public PendingIntent getRunningServiceControlPanelLocked(ComponentName name) { 1775 int userId = UserId.getUserId(Binder.getCallingUid()); 1776 ServiceRecord r = mServiceMap.getServiceByName(name, userId); 1777 if (r != null) { 1778 for (ArrayList<ConnectionRecord> conn : r.connections.values()) { 1779 for (int i=0; i<conn.size(); i++) { 1780 if (conn.get(i).clientIntent != null) { 1781 return conn.get(i).clientIntent; 1782 } 1783 } 1784 } 1785 } 1786 return null; 1787 } 1788 1789 void serviceTimeout(ProcessRecord proc) { 1790 String anrMessage = null; 1791 1792 synchronized(this) { 1793 if (proc.executingServices.size() == 0 || proc.thread == null) { 1794 return; 1795 } 1796 long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT; 1797 Iterator<ServiceRecord> it = proc.executingServices.iterator(); 1798 ServiceRecord timeout = null; 1799 long nextTime = 0; 1800 while (it.hasNext()) { 1801 ServiceRecord sr = it.next(); 1802 if (sr.executingStart < maxTime) { 1803 timeout = sr; 1804 break; 1805 } 1806 if (sr.executingStart > nextTime) { 1807 nextTime = sr.executingStart; 1808 } 1809 } 1810 if (timeout != null && mAm.mLruProcesses.contains(proc)) { 1811 Slog.w(TAG, "Timeout executing service: " + timeout); 1812 anrMessage = "Executing service " + timeout.shortName; 1813 } else { 1814 Message msg = mAm.mHandler.obtainMessage( 1815 ActivityManagerService.SERVICE_TIMEOUT_MSG); 1816 msg.obj = proc; 1817 mAm.mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT); 1818 } 1819 } 1820 1821 if (anrMessage != null) { 1822 mAm.appNotResponding(proc, null, null, anrMessage); 1823 } 1824 } 1825 1826 /** 1827 * Prints a list of ServiceRecords (dumpsys activity services) 1828 */ 1829 boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args, 1830 int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { 1831 boolean needSep = false; 1832 1833 ItemMatcher matcher = new ItemMatcher(); 1834 matcher.build(args, opti); 1835 1836 pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)"); 1837 try { 1838 List<UserInfo> users = AppGlobals.getPackageManager().getUsers(); 1839 for (UserInfo user : users) { 1840 if (mServiceMap.getAllServices(user.id).size() > 0) { 1841 boolean printed = false; 1842 long nowReal = SystemClock.elapsedRealtime(); 1843 Iterator<ServiceRecord> it = mServiceMap.getAllServices( 1844 user.id).iterator(); 1845 needSep = false; 1846 while (it.hasNext()) { 1847 ServiceRecord r = it.next(); 1848 if (!matcher.match(r, r.name)) { 1849 continue; 1850 } 1851 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 1852 continue; 1853 } 1854 if (!printed) { 1855 pw.println(" Active services:"); 1856 printed = true; 1857 } 1858 if (needSep) { 1859 pw.println(); 1860 } 1861 pw.print(" * "); 1862 pw.println(r); 1863 if (dumpAll) { 1864 r.dump(pw, " "); 1865 needSep = true; 1866 } else { 1867 pw.print(" app="); 1868 pw.println(r.app); 1869 pw.print(" created="); 1870 TimeUtils.formatDuration(r.createTime, nowReal, pw); 1871 pw.print(" started="); 1872 pw.print(r.startRequested); 1873 pw.print(" connections="); 1874 pw.println(r.connections.size()); 1875 if (r.connections.size() > 0) { 1876 pw.println(" Connections:"); 1877 for (ArrayList<ConnectionRecord> clist : r.connections.values()) { 1878 for (int i = 0; i < clist.size(); i++) { 1879 ConnectionRecord conn = clist.get(i); 1880 pw.print(" "); 1881 pw.print(conn.binding.intent.intent.getIntent() 1882 .toShortString(false, false, false, false)); 1883 pw.print(" -> "); 1884 ProcessRecord proc = conn.binding.client; 1885 pw.println(proc != null ? proc.toShortString() : "null"); 1886 } 1887 } 1888 } 1889 } 1890 if (dumpClient && r.app != null && r.app.thread != null) { 1891 pw.println(" Client:"); 1892 pw.flush(); 1893 try { 1894 TransferPipe tp = new TransferPipe(); 1895 try { 1896 r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), 1897 r, args); 1898 tp.setBufferPrefix(" "); 1899 // Short timeout, since blocking here can 1900 // deadlock with the application. 1901 tp.go(fd, 2000); 1902 } finally { 1903 tp.kill(); 1904 } 1905 } catch (IOException e) { 1906 pw.println(" Failure while dumping the service: " + e); 1907 } catch (RemoteException e) { 1908 pw.println(" Got a RemoteException while dumping the service"); 1909 } 1910 needSep = true; 1911 } 1912 } 1913 needSep = printed; 1914 } 1915 } 1916 } catch (RemoteException re) { 1917 1918 } 1919 1920 if (mPendingServices.size() > 0) { 1921 boolean printed = false; 1922 for (int i=0; i<mPendingServices.size(); i++) { 1923 ServiceRecord r = mPendingServices.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(" Pending services:"); 1934 printed = true; 1935 } 1936 pw.print(" * Pending "); pw.println(r); 1937 r.dump(pw, " "); 1938 } 1939 needSep = true; 1940 } 1941 1942 if (mRestartingServices.size() > 0) { 1943 boolean printed = false; 1944 for (int i=0; i<mRestartingServices.size(); i++) { 1945 ServiceRecord r = mRestartingServices.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(" Restarting services:"); 1956 printed = true; 1957 } 1958 pw.print(" * Restarting "); pw.println(r); 1959 r.dump(pw, " "); 1960 } 1961 needSep = true; 1962 } 1963 1964 if (mStoppingServices.size() > 0) { 1965 boolean printed = false; 1966 for (int i=0; i<mStoppingServices.size(); i++) { 1967 ServiceRecord r = mStoppingServices.get(i); 1968 if (!matcher.match(r, r.name)) { 1969 continue; 1970 } 1971 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 1972 continue; 1973 } 1974 if (!printed) { 1975 if (needSep) pw.println(" "); 1976 needSep = true; 1977 pw.println(" Stopping services:"); 1978 printed = true; 1979 } 1980 pw.print(" * Stopping "); pw.println(r); 1981 r.dump(pw, " "); 1982 } 1983 needSep = true; 1984 } 1985 1986 if (dumpAll) { 1987 if (mServiceConnections.size() > 0) { 1988 boolean printed = false; 1989 Iterator<ArrayList<ConnectionRecord>> it 1990 = mServiceConnections.values().iterator(); 1991 while (it.hasNext()) { 1992 ArrayList<ConnectionRecord> r = it.next(); 1993 for (int i=0; i<r.size(); i++) { 1994 ConnectionRecord cr = r.get(i); 1995 if (!matcher.match(cr.binding.service, cr.binding.service.name)) { 1996 continue; 1997 } 1998 if (dumpPackage != null && (cr.binding.client == null 1999 || !dumpPackage.equals(cr.binding.client.info.packageName))) { 2000 continue; 2001 } 2002 if (!printed) { 2003 if (needSep) pw.println(" "); 2004 needSep = true; 2005 pw.println(" Connection bindings to services:"); 2006 printed = true; 2007 } 2008 pw.print(" * "); pw.println(cr); 2009 cr.dump(pw, " "); 2010 } 2011 } 2012 needSep = true; 2013 } 2014 } 2015 2016 return needSep; 2017 } 2018 2019 /** 2020 * There are three ways to call this: 2021 * - no service specified: dump all the services 2022 * - a flattened component name that matched an existing service was specified as the 2023 * first arg: dump that one service 2024 * - the first arg isn't the flattened component name of an existing service: 2025 * dump all services whose component contains the first arg as a substring 2026 */ 2027 protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args, 2028 int opti, boolean dumpAll) { 2029 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 2030 2031 if ("all".equals(name)) { 2032 synchronized (this) { 2033 try { 2034 List<UserInfo> users = AppGlobals.getPackageManager().getUsers(); 2035 for (UserInfo user : users) { 2036 for (ServiceRecord r1 : mServiceMap.getAllServices(user.id)) { 2037 services.add(r1); 2038 } 2039 } 2040 } catch (RemoteException re) { 2041 } 2042 } 2043 } else { 2044 ComponentName componentName = name != null 2045 ? ComponentName.unflattenFromString(name) : null; 2046 int objectId = 0; 2047 if (componentName == null) { 2048 // Not a '/' separated full component name; maybe an object ID? 2049 try { 2050 objectId = Integer.parseInt(name, 16); 2051 name = null; 2052 componentName = null; 2053 } catch (RuntimeException e) { 2054 } 2055 } 2056 2057 synchronized (this) { 2058 try { 2059 List<UserInfo> users = AppGlobals.getPackageManager().getUsers(); 2060 for (UserInfo user : users) { 2061 for (ServiceRecord r1 : mServiceMap.getAllServices(user.id)) { 2062 if (componentName != null) { 2063 if (r1.name.equals(componentName)) { 2064 services.add(r1); 2065 } 2066 } else if (name != null) { 2067 if (r1.name.flattenToString().contains(name)) { 2068 services.add(r1); 2069 } 2070 } else if (System.identityHashCode(r1) == objectId) { 2071 services.add(r1); 2072 } 2073 } 2074 } 2075 } catch (RemoteException re) { 2076 } 2077 } 2078 } 2079 2080 if (services.size() <= 0) { 2081 return false; 2082 } 2083 2084 boolean needSep = false; 2085 for (int i=0; i<services.size(); i++) { 2086 if (needSep) { 2087 pw.println(); 2088 } 2089 needSep = true; 2090 dumpService("", fd, pw, services.get(i), args, dumpAll); 2091 } 2092 return true; 2093 } 2094 2095 /** 2096 * Invokes IApplicationThread.dumpService() on the thread of the specified service if 2097 * there is a thread associated with the service. 2098 */ 2099 private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw, 2100 final ServiceRecord r, String[] args, boolean dumpAll) { 2101 String innerPrefix = prefix + " "; 2102 synchronized (this) { 2103 pw.print(prefix); pw.print("SERVICE "); 2104 pw.print(r.shortName); pw.print(" "); 2105 pw.print(Integer.toHexString(System.identityHashCode(r))); 2106 pw.print(" pid="); 2107 if (r.app != null) pw.println(r.app.pid); 2108 else pw.println("(not running)"); 2109 if (dumpAll) { 2110 r.dump(pw, innerPrefix); 2111 } 2112 } 2113 if (r.app != null && r.app.thread != null) { 2114 pw.print(prefix); pw.println(" Client:"); 2115 pw.flush(); 2116 try { 2117 TransferPipe tp = new TransferPipe(); 2118 try { 2119 r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), r, args); 2120 tp.setBufferPrefix(prefix + " "); 2121 tp.go(fd); 2122 } finally { 2123 tp.kill(); 2124 } 2125 } catch (IOException e) { 2126 pw.println(prefix + " Failure while dumping the service: " + e); 2127 } catch (RemoteException e) { 2128 pw.println(prefix + " Got a RemoteException while dumping the service"); 2129 } 2130 } 2131 } 2132 2133} 2134