ActiveServices.java revision f7097a5b697fedb6976774e55a51471405a23c0e
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.HashSet; 24import java.util.Iterator; 25import java.util.List; 26 27import android.os.Handler; 28import android.os.Looper; 29import android.os.SystemProperties; 30import android.util.ArrayMap; 31import com.android.internal.app.ProcessMap; 32import com.android.internal.app.ProcessStats; 33import com.android.internal.os.BatteryStatsImpl; 34import com.android.internal.os.TransferPipe; 35import com.android.server.am.ActivityManagerService.ItemMatcher; 36import com.android.server.am.ActivityManagerService.NeededUriGrants; 37 38import android.app.ActivityManager; 39import android.app.AppGlobals; 40import android.app.IApplicationThread; 41import android.app.IServiceConnection; 42import android.app.Notification; 43import android.app.PendingIntent; 44import android.app.Service; 45import android.content.ComponentName; 46import android.content.Context; 47import android.content.Intent; 48import android.content.pm.ApplicationInfo; 49import android.content.pm.PackageManager; 50import android.content.pm.ResolveInfo; 51import android.content.pm.ServiceInfo; 52import android.os.Binder; 53import android.os.IBinder; 54import android.os.Message; 55import android.os.Process; 56import android.os.RemoteException; 57import android.os.SystemClock; 58import android.os.UserHandle; 59import android.util.EventLog; 60import android.util.Slog; 61import android.util.SparseArray; 62import android.util.TimeUtils; 63 64public final class ActiveServices { 65 static final boolean DEBUG_SERVICE = ActivityManagerService.DEBUG_SERVICE; 66 static final boolean DEBUG_SERVICE_EXECUTING = ActivityManagerService.DEBUG_SERVICE_EXECUTING; 67 static final boolean DEBUG_DELAYED_SERVICE = ActivityManagerService.DEBUG_SERVICE; 68 static final boolean DEBUG_DELAYED_STARTS = DEBUG_DELAYED_SERVICE; 69 static final boolean DEBUG_MU = ActivityManagerService.DEBUG_MU; 70 static final String TAG = ActivityManagerService.TAG; 71 static final String TAG_MU = ActivityManagerService.TAG_MU; 72 73 // How long we wait for a service to finish executing. 74 static final int SERVICE_TIMEOUT = 20*1000; 75 76 // How long we wait for a service to finish executing. 77 static final int SERVICE_BACKGROUND_TIMEOUT = SERVICE_TIMEOUT * 10; 78 79 // How long a service needs to be running until restarting its process 80 // is no longer considered to be a relaunch of the service. 81 static final int SERVICE_RESTART_DURATION = 1*1000; 82 83 // How long a service needs to be running until it will start back at 84 // SERVICE_RESTART_DURATION after being killed. 85 static final int SERVICE_RESET_RUN_DURATION = 60*1000; 86 87 // Multiplying factor to increase restart duration time by, for each time 88 // a service is killed before it has run for SERVICE_RESET_RUN_DURATION. 89 static final int SERVICE_RESTART_DURATION_FACTOR = 4; 90 91 // The minimum amount of time between restarting services that we allow. 92 // That is, when multiple services are restarting, we won't allow each 93 // to restart less than this amount of time from the last one. 94 static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000; 95 96 // Maximum amount of time for there to be no activity on a service before 97 // we consider it non-essential and allow its process to go on the 98 // LRU background list. 99 static final int MAX_SERVICE_INACTIVITY = 30*60*1000; 100 101 // How long we wait for a background started service to stop itself before 102 // allowing the next pending start to run. 103 static final int BG_START_TIMEOUT = 15*1000; 104 105 final ActivityManagerService mAm; 106 107 // Maximum number of services that we allow to start in the background 108 // at the same time. 109 final int mMaxStartingBackground; 110 111 final SparseArray<ServiceMap> mServiceMap = new SparseArray<ServiceMap>(); 112 113 /** 114 * All currently bound service connections. Keys are the IBinder of 115 * the client's IServiceConnection. 116 */ 117 final ArrayMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections 118 = new ArrayMap<IBinder, ArrayList<ConnectionRecord>>(); 119 120 /** 121 * List of services that we have been asked to start, 122 * but haven't yet been able to. It is used to hold start requests 123 * while waiting for their corresponding application thread to get 124 * going. 125 */ 126 final ArrayList<ServiceRecord> mPendingServices 127 = new ArrayList<ServiceRecord>(); 128 129 /** 130 * List of services that are scheduled to restart following a crash. 131 */ 132 final ArrayList<ServiceRecord> mRestartingServices 133 = new ArrayList<ServiceRecord>(); 134 135 /** 136 * List of services that are in the process of being destroyed. 137 */ 138 final ArrayList<ServiceRecord> mDestroyingServices 139 = new ArrayList<ServiceRecord>(); 140 141 static final class DelayingProcess extends ArrayList<ServiceRecord> { 142 long timeoout; 143 } 144 145 /** 146 * Information about services for a single user. 147 */ 148 class ServiceMap extends Handler { 149 final int mUserId; 150 final ArrayMap<ComponentName, ServiceRecord> mServicesByName 151 = new ArrayMap<ComponentName, ServiceRecord>(); 152 final ArrayMap<Intent.FilterComparison, ServiceRecord> mServicesByIntent 153 = new ArrayMap<Intent.FilterComparison, ServiceRecord>(); 154 155 final ArrayList<ServiceRecord> mDelayedStartList 156 = new ArrayList<ServiceRecord>(); 157 /* XXX eventually I'd like to have this based on processes instead of services. 158 * That is, if we try to start two services in a row both running in the same 159 * process, this should be one entry in mStartingBackground for that one process 160 * that remains until all services in it are done. 161 final ArrayMap<ProcessRecord, DelayingProcess> mStartingBackgroundMap 162 = new ArrayMap<ProcessRecord, DelayingProcess>(); 163 final ArrayList<DelayingProcess> mStartingProcessList 164 = new ArrayList<DelayingProcess>(); 165 */ 166 167 final ArrayList<ServiceRecord> mStartingBackground 168 = new ArrayList<ServiceRecord>(); 169 170 static final int MSG_BG_START_TIMEOUT = 1; 171 172 ServiceMap(Looper looper, int userId) { 173 super(looper); 174 mUserId = userId; 175 } 176 177 @Override 178 public void handleMessage(Message msg) { 179 switch (msg.what) { 180 case MSG_BG_START_TIMEOUT: { 181 synchronized (mAm) { 182 rescheduleDelayedStarts(); 183 } 184 } break; 185 } 186 } 187 188 void ensureNotStartingBackground(ServiceRecord r) { 189 if (mStartingBackground.remove(r)) { 190 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "No longer background starting: " + r); 191 rescheduleDelayedStarts(); 192 } 193 if (mDelayedStartList.remove(r)) { 194 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "No longer delaying start: " + r); 195 } 196 } 197 198 void rescheduleDelayedStarts() { 199 removeMessages(MSG_BG_START_TIMEOUT); 200 final long now = SystemClock.uptimeMillis(); 201 for (int i=0, N=mStartingBackground.size(); i<N; i++) { 202 ServiceRecord r = mStartingBackground.get(i); 203 if (r.startingBgTimeout <= now) { 204 Slog.i(TAG, "Waited long enough for: " + r); 205 mStartingBackground.remove(i); 206 N--; 207 i--; 208 } 209 } 210 while (mDelayedStartList.size() > 0 211 && mStartingBackground.size() < mMaxStartingBackground) { 212 ServiceRecord r = mDelayedStartList.remove(0); 213 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "REM FR DELAY LIST (exec next): " + r); 214 if (r.pendingStarts.size() <= 0) { 215 Slog.w(TAG, "**** NO PENDING STARTS! " + r + " startReq=" + r.startRequested 216 + " delayedStop=" + r.delayedStop); 217 } 218 if (DEBUG_DELAYED_SERVICE) { 219 if (mDelayedStartList.size() > 0) { 220 Slog.v(TAG, "Remaining delayed list:"); 221 for (int i=0; i<mDelayedStartList.size(); i++) { 222 Slog.v(TAG, " #" + i + ": " + mDelayedStartList.get(i)); 223 } 224 } 225 } 226 r.delayed = false; 227 startServiceInnerLocked(this, r.pendingStarts.get(0).intent, r, false, true); 228 } 229 if (mStartingBackground.size() > 0) { 230 ServiceRecord next = mStartingBackground.get(0); 231 long when = next.startingBgTimeout > now ? next.startingBgTimeout : now; 232 if (DEBUG_DELAYED_SERVICE) Slog.v(TAG, "Top bg start is " + next 233 + ", can delay others up to " + when); 234 Message msg = obtainMessage(MSG_BG_START_TIMEOUT); 235 sendMessageAtTime(msg, when); 236 } 237 if (mStartingBackground.size() < mMaxStartingBackground) { 238 mAm.backgroundServicesFinishedLocked(mUserId); 239 } 240 } 241 } 242 243 public ActiveServices(ActivityManagerService service) { 244 mAm = service; 245 int maxBg = 0; 246 try { 247 maxBg = Integer.parseInt(SystemProperties.get("ro.config.max_starting_bg", "0")); 248 } catch(RuntimeException e) { 249 } 250 mMaxStartingBackground = maxBg > 0 251 ? maxBg : ActivityManager.isLowRamDeviceStatic() ? 1 : 8; 252 } 253 254 ServiceRecord getServiceByName(ComponentName name, int callingUser) { 255 // TODO: Deal with global services 256 if (DEBUG_MU) 257 Slog.v(TAG_MU, "getServiceByName(" + name + "), callingUser = " + callingUser); 258 return getServiceMap(callingUser).mServicesByName.get(name); 259 } 260 261 boolean hasBackgroundServices(int callingUser) { 262 ServiceMap smap = mServiceMap.get(callingUser); 263 return smap != null ? smap.mStartingBackground.size() >= mMaxStartingBackground : false; 264 } 265 266 private ServiceMap getServiceMap(int callingUser) { 267 ServiceMap smap = mServiceMap.get(callingUser); 268 if (smap == null) { 269 smap = new ServiceMap(mAm.mHandler.getLooper(), callingUser); 270 mServiceMap.put(callingUser, smap); 271 } 272 return smap; 273 } 274 275 ArrayMap<ComponentName, ServiceRecord> getServices(int callingUser) { 276 return getServiceMap(callingUser).mServicesByName; 277 } 278 279 ComponentName startServiceLocked(IApplicationThread caller, 280 Intent service, String resolvedType, 281 int callingPid, int callingUid, int userId) { 282 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "startService: " + service 283 + " type=" + resolvedType + " args=" + service.getExtras()); 284 285 final boolean callerFg; 286 if (caller != null) { 287 final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); 288 if (callerApp == null) { 289 throw new SecurityException( 290 "Unable to find app for caller " + caller 291 + " (pid=" + Binder.getCallingPid() 292 + ") when starting service " + service); 293 } 294 callerFg = callerApp.setSchedGroup != Process.THREAD_GROUP_BG_NONINTERACTIVE; 295 } else { 296 callerFg = true; 297 } 298 299 300 ServiceLookupResult res = 301 retrieveServiceLocked(service, resolvedType, 302 callingPid, callingUid, userId, true, callerFg); 303 if (res == null) { 304 return null; 305 } 306 if (res.record == null) { 307 return new ComponentName("!", res.permission != null 308 ? res.permission : "private to package"); 309 } 310 ServiceRecord r = res.record; 311 NeededUriGrants neededGrants = mAm.checkGrantUriPermissionFromIntentLocked( 312 callingUid, r.packageName, service, service.getFlags(), null, r.userId); 313 if (unscheduleServiceRestartLocked(r, callingUid, false)) { 314 if (DEBUG_SERVICE) Slog.v(TAG, "START SERVICE WHILE RESTART PENDING: " + r); 315 } 316 r.lastActivity = SystemClock.uptimeMillis(); 317 r.startRequested = true; 318 r.delayedStop = false; 319 r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), 320 service, neededGrants)); 321 322 final ServiceMap smap = getServiceMap(r.userId); 323 boolean addToStarting = false; 324 if (!callerFg && r.app == null && mAm.mStartedUsers.get(r.userId) != null) { 325 ProcessRecord proc = mAm.getProcessRecordLocked(r.processName, r.appInfo.uid, false); 326 if (proc == null || proc.curProcState > ActivityManager.PROCESS_STATE_RECEIVER) { 327 // If this is not coming from a foreground caller, then we may want 328 // to delay the start if there are already other background services 329 // that are starting. This is to avoid process start spam when lots 330 // of applications are all handling things like connectivity broadcasts. 331 // We only do this for cached processes, because otherwise an application 332 // can have assumptions about calling startService() for a service to run 333 // in its own process, and for that process to not be killed before the 334 // service is started. This is especially the case for receivers, which 335 // may start a service in onReceive() to do some additional work and have 336 // initialized some global state as part of that. 337 if (DEBUG_DELAYED_SERVICE) Slog.v(TAG, "Potential start delay of " + r + " in " 338 + proc); 339 if (r.delayed) { 340 // This service is already scheduled for a delayed start; just leave 341 // it still waiting. 342 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Continuing to delay: " + r); 343 return r.name; 344 } 345 if (smap.mStartingBackground.size() >= mMaxStartingBackground) { 346 // Something else is starting, delay! 347 Slog.i(TAG, "Delaying start of: " + r); 348 smap.mDelayedStartList.add(r); 349 r.delayed = true; 350 return r.name; 351 } 352 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Not delaying: " + r); 353 addToStarting = true; 354 } else if (proc.curProcState >= ActivityManager.PROCESS_STATE_SERVICE) { 355 // We slightly loosen when we will enqueue this new service as a background 356 // starting service we are waiting for, to also include processes that are 357 // currently running other services or receivers. 358 addToStarting = true; 359 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Not delaying, but counting as bg: " + r); 360 } else if (DEBUG_DELAYED_STARTS) { 361 StringBuilder sb = new StringBuilder(128); 362 sb.append("Not potential delay (state=").append(proc.curProcState) 363 .append(' ').append(proc.adjType); 364 String reason = proc.makeAdjReason(); 365 if (reason != null) { 366 sb.append(' '); 367 sb.append(reason); 368 } 369 sb.append("): "); 370 sb.append(r.toString()); 371 Slog.v(TAG, sb.toString()); 372 } 373 } else if (DEBUG_DELAYED_STARTS) { 374 if (callerFg) { 375 Slog.v(TAG, "Not potential delay (callerFg=" + callerFg + " uid=" 376 + callingUid + " pid=" + callingPid + "): " + r); 377 } else if (r.app != null) { 378 Slog.v(TAG, "Not potential delay (cur app=" + r.app + "): " + r); 379 } else { 380 Slog.v(TAG, "Not potential delay (user " + r.userId + " not started): " + r); 381 } 382 } 383 384 return startServiceInnerLocked(smap, service, r, callerFg, addToStarting); 385 } 386 387 ComponentName startServiceInnerLocked(ServiceMap smap, Intent service, 388 ServiceRecord r, boolean callerFg, boolean addToStarting) { 389 ProcessStats.ServiceState stracker = r.getTracker(); 390 if (stracker != null) { 391 stracker.setStarted(true, mAm.mProcessStats.getMemFactorLocked(), r.lastActivity); 392 } 393 r.callStart = false; 394 synchronized (r.stats.getBatteryStats()) { 395 r.stats.startRunningLocked(); 396 } 397 String error = bringUpServiceLocked(r, service.getFlags(), callerFg, false); 398 if (error != null) { 399 return new ComponentName("!!", error); 400 } 401 402 if (r.startRequested && addToStarting) { 403 boolean first = smap.mStartingBackground.size() == 0; 404 smap.mStartingBackground.add(r); 405 r.startingBgTimeout = SystemClock.uptimeMillis() + BG_START_TIMEOUT; 406 if (DEBUG_DELAYED_SERVICE) { 407 RuntimeException here = new RuntimeException("here"); 408 here.fillInStackTrace(); 409 Slog.v(TAG, "Starting background (first=" + first + "): " + r, here); 410 } else if (DEBUG_DELAYED_STARTS) { 411 Slog.v(TAG, "Starting background (first=" + first + "): " + r); 412 } 413 if (first) { 414 smap.rescheduleDelayedStarts(); 415 } 416 } else if (callerFg) { 417 smap.ensureNotStartingBackground(r); 418 } 419 420 return r.name; 421 } 422 423 private void stopServiceLocked(ServiceRecord service) { 424 if (service.delayed) { 425 // If service isn't actually running, but is is being held in the 426 // delayed list, then we need to keep it started but note that it 427 // should be stopped once no longer delayed. 428 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Delaying stop of pending: " + service); 429 service.delayedStop = true; 430 return; 431 } 432 synchronized (service.stats.getBatteryStats()) { 433 service.stats.stopRunningLocked(); 434 } 435 service.startRequested = false; 436 if (service.tracker != null) { 437 service.tracker.setStarted(false, mAm.mProcessStats.getMemFactorLocked(), 438 SystemClock.uptimeMillis()); 439 } 440 service.callStart = false; 441 bringDownServiceIfNeededLocked(service, false, false); 442 } 443 444 int stopServiceLocked(IApplicationThread caller, Intent service, 445 String resolvedType, int userId) { 446 if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service 447 + " type=" + resolvedType); 448 449 final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); 450 if (caller != null && callerApp == null) { 451 throw new SecurityException( 452 "Unable to find app for caller " + caller 453 + " (pid=" + Binder.getCallingPid() 454 + ") when stopping service " + service); 455 } 456 457 // If this service is active, make sure it is stopped. 458 ServiceLookupResult r = retrieveServiceLocked(service, resolvedType, 459 Binder.getCallingPid(), Binder.getCallingUid(), userId, false, false); 460 if (r != null) { 461 if (r.record != null) { 462 final long origId = Binder.clearCallingIdentity(); 463 try { 464 stopServiceLocked(r.record); 465 } finally { 466 Binder.restoreCallingIdentity(origId); 467 } 468 return 1; 469 } 470 return -1; 471 } 472 473 return 0; 474 } 475 476 IBinder peekServiceLocked(Intent service, String resolvedType) { 477 ServiceLookupResult r = retrieveServiceLocked(service, resolvedType, 478 Binder.getCallingPid(), Binder.getCallingUid(), 479 UserHandle.getCallingUserId(), false, false); 480 481 IBinder ret = null; 482 if (r != null) { 483 // r.record is null if findServiceLocked() failed the caller permission check 484 if (r.record == null) { 485 throw new SecurityException( 486 "Permission Denial: Accessing service " + r.record.name 487 + " from pid=" + Binder.getCallingPid() 488 + ", uid=" + Binder.getCallingUid() 489 + " requires " + r.permission); 490 } 491 IntentBindRecord ib = r.record.bindings.get(r.record.intent); 492 if (ib != null) { 493 ret = ib.binder; 494 } 495 } 496 497 return ret; 498 } 499 500 boolean stopServiceTokenLocked(ComponentName className, IBinder token, 501 int startId) { 502 if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className 503 + " " + token + " startId=" + startId); 504 ServiceRecord r = findServiceLocked(className, token, UserHandle.getCallingUserId()); 505 if (r != null) { 506 if (startId >= 0) { 507 // Asked to only stop if done with all work. Note that 508 // to avoid leaks, we will take this as dropping all 509 // start items up to and including this one. 510 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); 511 if (si != null) { 512 while (r.deliveredStarts.size() > 0) { 513 ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); 514 cur.removeUriPermissionsLocked(); 515 if (cur == si) { 516 break; 517 } 518 } 519 } 520 521 if (r.getLastStartId() != startId) { 522 return false; 523 } 524 525 if (r.deliveredStarts.size() > 0) { 526 Slog.w(TAG, "stopServiceToken startId " + startId 527 + " is last, but have " + r.deliveredStarts.size() 528 + " remaining args"); 529 } 530 } 531 532 synchronized (r.stats.getBatteryStats()) { 533 r.stats.stopRunningLocked(); 534 } 535 r.startRequested = false; 536 if (r.tracker != null) { 537 r.tracker.setStarted(false, mAm.mProcessStats.getMemFactorLocked(), 538 SystemClock.uptimeMillis()); 539 } 540 r.callStart = false; 541 final long origId = Binder.clearCallingIdentity(); 542 bringDownServiceIfNeededLocked(r, false, false); 543 Binder.restoreCallingIdentity(origId); 544 return true; 545 } 546 return false; 547 } 548 549 public void setServiceForegroundLocked(ComponentName className, IBinder token, 550 int id, Notification notification, boolean removeNotification) { 551 final int userId = UserHandle.getCallingUserId(); 552 final long origId = Binder.clearCallingIdentity(); 553 try { 554 ServiceRecord r = findServiceLocked(className, token, userId); 555 if (r != null) { 556 if (id != 0) { 557 if (notification == null) { 558 throw new IllegalArgumentException("null notification"); 559 } 560 if (r.foregroundId != id) { 561 r.cancelNotification(); 562 r.foregroundId = id; 563 } 564 notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; 565 r.foregroundNoti = notification; 566 r.isForeground = true; 567 r.postNotification(); 568 if (r.app != null) { 569 updateServiceForegroundLocked(r.app, true); 570 } 571 getServiceMap(r.userId).ensureNotStartingBackground(r); 572 } else { 573 if (r.isForeground) { 574 r.isForeground = false; 575 if (r.app != null) { 576 mAm.updateLruProcessLocked(r.app, false, null); 577 updateServiceForegroundLocked(r.app, true); 578 } 579 } 580 if (removeNotification) { 581 r.cancelNotification(); 582 r.foregroundId = 0; 583 r.foregroundNoti = null; 584 } 585 } 586 } 587 } finally { 588 Binder.restoreCallingIdentity(origId); 589 } 590 } 591 592 private void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) { 593 boolean anyForeground = false; 594 for (int i=proc.services.size()-1; i>=0; i--) { 595 ServiceRecord sr = proc.services.valueAt(i); 596 if (sr.isForeground) { 597 anyForeground = true; 598 break; 599 } 600 } 601 mAm.updateProcessForegroundLocked(proc, anyForeground, oomAdj); 602 } 603 604 private boolean updateServiceClientActivitiesLocked(ProcessRecord proc, 605 ConnectionRecord modCr) { 606 if (modCr != null && modCr.binding.client != null) { 607 if (modCr.binding.client.activities.size() <= 0) { 608 // This connection is from a client without activities, so adding 609 // and removing is not interesting. 610 return false; 611 } 612 } 613 614 boolean anyClientActivities = false; 615 for (int i=proc.services.size()-1; i>=0 && !anyClientActivities; i--) { 616 ServiceRecord sr = proc.services.valueAt(i); 617 for (int conni=sr.connections.size()-1; conni>=0 && !anyClientActivities; conni--) { 618 ArrayList<ConnectionRecord> clist = sr.connections.valueAt(conni); 619 for (int cri=clist.size()-1; cri>=0; cri--) { 620 ConnectionRecord cr = clist.get(cri); 621 if (cr.binding.client == null || cr.binding.client == proc) { 622 // Binding to ourself is not interesting. 623 continue; 624 } 625 if (cr.binding.client.activities.size() > 0) { 626 anyClientActivities = true; 627 break; 628 } 629 } 630 } 631 } 632 if (anyClientActivities != proc.hasClientActivities) { 633 proc.hasClientActivities = anyClientActivities; 634 mAm.updateLruProcessLocked(proc, anyClientActivities, null); 635 return true; 636 } 637 return false; 638 } 639 640 int bindServiceLocked(IApplicationThread caller, IBinder token, 641 Intent service, String resolvedType, 642 IServiceConnection connection, int flags, int userId) { 643 if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service 644 + " type=" + resolvedType + " conn=" + connection.asBinder() 645 + " flags=0x" + Integer.toHexString(flags)); 646 final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); 647 if (callerApp == null) { 648 throw new SecurityException( 649 "Unable to find app for caller " + caller 650 + " (pid=" + Binder.getCallingPid() 651 + ") when binding service " + service); 652 } 653 654 ActivityRecord activity = null; 655 if (token != null) { 656 activity = ActivityRecord.isInStackLocked(token); 657 if (activity == null) { 658 Slog.w(TAG, "Binding with unknown activity: " + token); 659 return 0; 660 } 661 } 662 663 int clientLabel = 0; 664 PendingIntent clientIntent = null; 665 666 if (callerApp.info.uid == Process.SYSTEM_UID) { 667 // Hacky kind of thing -- allow system stuff to tell us 668 // what they are, so we can report this elsewhere for 669 // others to know why certain services are running. 670 try { 671 clientIntent = service.getParcelableExtra(Intent.EXTRA_CLIENT_INTENT); 672 } catch (RuntimeException e) { 673 } 674 if (clientIntent != null) { 675 clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); 676 if (clientLabel != 0) { 677 // There are no useful extras in the intent, trash them. 678 // System code calling with this stuff just needs to know 679 // this will happen. 680 service = service.cloneFilter(); 681 } 682 } 683 } 684 685 if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) { 686 mAm.enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS, 687 "BIND_TREAT_LIKE_ACTIVITY"); 688 } 689 690 final boolean callerFg = callerApp.setSchedGroup != Process.THREAD_GROUP_BG_NONINTERACTIVE; 691 692 ServiceLookupResult res = 693 retrieveServiceLocked(service, resolvedType, 694 Binder.getCallingPid(), Binder.getCallingUid(), userId, true, callerFg); 695 if (res == null) { 696 return 0; 697 } 698 if (res.record == null) { 699 return -1; 700 } 701 ServiceRecord s = res.record; 702 703 final long origId = Binder.clearCallingIdentity(); 704 705 try { 706 if (unscheduleServiceRestartLocked(s, callerApp.info.uid, false)) { 707 if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: " 708 + s); 709 } 710 711 if ((flags&Context.BIND_AUTO_CREATE) != 0) { 712 s.lastActivity = SystemClock.uptimeMillis(); 713 if (!s.hasAutoCreateConnections()) { 714 // This is the first binding, let the tracker know. 715 ProcessStats.ServiceState stracker = s.getTracker(); 716 if (stracker != null) { 717 stracker.setBound(true, mAm.mProcessStats.getMemFactorLocked(), 718 s.lastActivity); 719 } 720 } 721 } 722 723 AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); 724 ConnectionRecord c = new ConnectionRecord(b, activity, 725 connection, flags, clientLabel, clientIntent); 726 727 IBinder binder = connection.asBinder(); 728 ArrayList<ConnectionRecord> clist = s.connections.get(binder); 729 if (clist == null) { 730 clist = new ArrayList<ConnectionRecord>(); 731 s.connections.put(binder, clist); 732 } 733 clist.add(c); 734 b.connections.add(c); 735 if (activity != null) { 736 if (activity.connections == null) { 737 activity.connections = new HashSet<ConnectionRecord>(); 738 } 739 activity.connections.add(c); 740 } 741 b.client.connections.add(c); 742 if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { 743 b.client.hasAboveClient = true; 744 } 745 if (s.app != null) { 746 updateServiceClientActivitiesLocked(s.app, c); 747 } 748 clist = mServiceConnections.get(binder); 749 if (clist == null) { 750 clist = new ArrayList<ConnectionRecord>(); 751 mServiceConnections.put(binder, clist); 752 } 753 clist.add(c); 754 755 if ((flags&Context.BIND_AUTO_CREATE) != 0) { 756 s.lastActivity = SystemClock.uptimeMillis(); 757 if (bringUpServiceLocked(s, service.getFlags(), callerFg, false) != null) { 758 return 0; 759 } 760 } 761 762 if (s.app != null) { 763 if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) { 764 s.app.treatLikeActivity = true; 765 } 766 // This could have made the service more important. 767 mAm.updateLruProcessLocked(s.app, s.app.hasClientActivities 768 || s.app.treatLikeActivity, b.client); 769 mAm.updateOomAdjLocked(s.app); 770 } 771 772 if (DEBUG_SERVICE) Slog.v(TAG, "Bind " + s + " with " + b 773 + ": received=" + b.intent.received 774 + " apps=" + b.intent.apps.size() 775 + " doRebind=" + b.intent.doRebind); 776 777 if (s.app != null && b.intent.received) { 778 // Service is already running, so we can immediately 779 // publish the connection. 780 try { 781 c.conn.connected(s.name, b.intent.binder); 782 } catch (Exception e) { 783 Slog.w(TAG, "Failure sending service " + s.shortName 784 + " to connection " + c.conn.asBinder() 785 + " (in " + c.binding.client.processName + ")", e); 786 } 787 788 // If this is the first app connected back to this binding, 789 // and the service had previously asked to be told when 790 // rebound, then do so. 791 if (b.intent.apps.size() == 1 && b.intent.doRebind) { 792 requestServiceBindingLocked(s, b.intent, callerFg, true); 793 } 794 } else if (!b.intent.requested) { 795 requestServiceBindingLocked(s, b.intent, callerFg, false); 796 } 797 798 getServiceMap(s.userId).ensureNotStartingBackground(s); 799 800 } finally { 801 Binder.restoreCallingIdentity(origId); 802 } 803 804 return 1; 805 } 806 807 void publishServiceLocked(ServiceRecord r, Intent intent, IBinder service) { 808 final long origId = Binder.clearCallingIdentity(); 809 try { 810 if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r 811 + " " + intent + ": " + service); 812 if (r != null) { 813 Intent.FilterComparison filter 814 = new Intent.FilterComparison(intent); 815 IntentBindRecord b = r.bindings.get(filter); 816 if (b != null && !b.received) { 817 b.binder = service; 818 b.requested = true; 819 b.received = true; 820 for (int conni=r.connections.size()-1; conni>=0; conni--) { 821 ArrayList<ConnectionRecord> clist = r.connections.valueAt(conni); 822 for (int i=0; i<clist.size(); i++) { 823 ConnectionRecord c = clist.get(i); 824 if (!filter.equals(c.binding.intent.intent)) { 825 if (DEBUG_SERVICE) Slog.v( 826 TAG, "Not publishing to: " + c); 827 if (DEBUG_SERVICE) Slog.v( 828 TAG, "Bound intent: " + c.binding.intent.intent); 829 if (DEBUG_SERVICE) Slog.v( 830 TAG, "Published intent: " + intent); 831 continue; 832 } 833 if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c); 834 try { 835 c.conn.connected(r.name, service); 836 } catch (Exception e) { 837 Slog.w(TAG, "Failure sending service " + r.name + 838 " to connection " + c.conn.asBinder() + 839 " (in " + c.binding.client.processName + ")", e); 840 } 841 } 842 } 843 } 844 845 serviceDoneExecutingLocked(r, mDestroyingServices.contains(r), false); 846 } 847 } finally { 848 Binder.restoreCallingIdentity(origId); 849 } 850 } 851 852 boolean unbindServiceLocked(IServiceConnection connection) { 853 IBinder binder = connection.asBinder(); 854 if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder); 855 ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder); 856 if (clist == null) { 857 Slog.w(TAG, "Unbind failed: could not find connection for " 858 + connection.asBinder()); 859 return false; 860 } 861 862 final long origId = Binder.clearCallingIdentity(); 863 try { 864 while (clist.size() > 0) { 865 ConnectionRecord r = clist.get(0); 866 removeConnectionLocked(r, null, null); 867 868 if (r.binding.service.app != null) { 869 // This could have made the service less important. 870 if ((r.flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) { 871 r.binding.service.app.treatLikeActivity = true; 872 mAm.updateLruProcessLocked(r.binding.service.app, 873 r.binding.service.app.hasClientActivities 874 || r.binding.service.app.treatLikeActivity, null); 875 } 876 mAm.updateOomAdjLocked(r.binding.service.app); 877 } 878 } 879 } finally { 880 Binder.restoreCallingIdentity(origId); 881 } 882 883 return true; 884 } 885 886 void unbindFinishedLocked(ServiceRecord r, Intent intent, boolean doRebind) { 887 final long origId = Binder.clearCallingIdentity(); 888 try { 889 if (r != null) { 890 Intent.FilterComparison filter 891 = new Intent.FilterComparison(intent); 892 IntentBindRecord b = r.bindings.get(filter); 893 if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r 894 + " at " + b + ": apps=" 895 + (b != null ? b.apps.size() : 0)); 896 897 boolean inDestroying = mDestroyingServices.contains(r); 898 if (b != null) { 899 if (b.apps.size() > 0 && !inDestroying) { 900 // Applications have already bound since the last 901 // unbind, so just rebind right here. 902 boolean inFg = false; 903 for (int i=b.apps.size()-1; i>=0; i--) { 904 ProcessRecord client = b.apps.valueAt(i).client; 905 if (client != null && client.setSchedGroup 906 != Process.THREAD_GROUP_BG_NONINTERACTIVE) { 907 inFg = true; 908 break; 909 } 910 } 911 requestServiceBindingLocked(r, b, inFg, true); 912 } else { 913 // Note to tell the service the next time there is 914 // a new client. 915 b.doRebind = true; 916 } 917 } 918 919 serviceDoneExecutingLocked(r, inDestroying, false); 920 } 921 } finally { 922 Binder.restoreCallingIdentity(origId); 923 } 924 } 925 926 private final ServiceRecord findServiceLocked(ComponentName name, 927 IBinder token, int userId) { 928 ServiceRecord r = getServiceByName(name, userId); 929 return r == token ? r : null; 930 } 931 932 private final class ServiceLookupResult { 933 final ServiceRecord record; 934 final String permission; 935 936 ServiceLookupResult(ServiceRecord _record, String _permission) { 937 record = _record; 938 permission = _permission; 939 } 940 } 941 942 private class ServiceRestarter implements Runnable { 943 private ServiceRecord mService; 944 945 void setService(ServiceRecord service) { 946 mService = service; 947 } 948 949 public void run() { 950 synchronized(mAm) { 951 performServiceRestartLocked(mService); 952 } 953 } 954 } 955 956 private ServiceLookupResult retrieveServiceLocked(Intent service, 957 String resolvedType, int callingPid, int callingUid, int userId, 958 boolean createIfNeeded, boolean callingFromFg) { 959 ServiceRecord r = null; 960 if (DEBUG_SERVICE) Slog.v(TAG, "retrieveServiceLocked: " + service 961 + " type=" + resolvedType + " callingUid=" + callingUid); 962 963 userId = mAm.handleIncomingUser(callingPid, callingUid, userId, 964 false, true, "service", null); 965 966 ServiceMap smap = getServiceMap(userId); 967 final ComponentName comp = service.getComponent(); 968 if (comp != null) { 969 r = smap.mServicesByName.get(comp); 970 } 971 if (r == null) { 972 Intent.FilterComparison filter = new Intent.FilterComparison(service); 973 r = smap.mServicesByIntent.get(filter); 974 } 975 if (r == null) { 976 try { 977 ResolveInfo rInfo = 978 AppGlobals.getPackageManager().resolveService( 979 service, resolvedType, 980 ActivityManagerService.STOCK_PM_FLAGS, userId); 981 ServiceInfo sInfo = 982 rInfo != null ? rInfo.serviceInfo : null; 983 if (sInfo == null) { 984 Slog.w(TAG, "Unable to start service " + service + " U=" + userId + 985 ": not found"); 986 return null; 987 } 988 ComponentName name = new ComponentName( 989 sInfo.applicationInfo.packageName, sInfo.name); 990 if (userId > 0) { 991 if (mAm.isSingleton(sInfo.processName, sInfo.applicationInfo, 992 sInfo.name, sInfo.flags) 993 && mAm.isValidSingletonCall(callingUid, sInfo.applicationInfo.uid)) { 994 userId = 0; 995 smap = getServiceMap(0); 996 } 997 sInfo = new ServiceInfo(sInfo); 998 sInfo.applicationInfo = mAm.getAppInfoForUser(sInfo.applicationInfo, userId); 999 } 1000 r = smap.mServicesByName.get(name); 1001 if (r == null && createIfNeeded) { 1002 Intent.FilterComparison filter 1003 = new Intent.FilterComparison(service.cloneFilter()); 1004 ServiceRestarter res = new ServiceRestarter(); 1005 BatteryStatsImpl.Uid.Pkg.Serv ss = null; 1006 BatteryStatsImpl stats = mAm.mBatteryStatsService.getActiveStatistics(); 1007 synchronized (stats) { 1008 ss = stats.getServiceStatsLocked( 1009 sInfo.applicationInfo.uid, sInfo.packageName, 1010 sInfo.name); 1011 } 1012 r = new ServiceRecord(mAm, ss, name, filter, sInfo, callingFromFg, res); 1013 res.setService(r); 1014 smap.mServicesByName.put(name, r); 1015 smap.mServicesByIntent.put(filter, r); 1016 1017 // Make sure this component isn't in the pending list. 1018 for (int i=mPendingServices.size()-1; i>=0; i--) { 1019 ServiceRecord pr = mPendingServices.get(i); 1020 if (pr.serviceInfo.applicationInfo.uid == sInfo.applicationInfo.uid 1021 && pr.name.equals(name)) { 1022 mPendingServices.remove(i); 1023 } 1024 } 1025 } 1026 } catch (RemoteException ex) { 1027 // pm is in same process, this will never happen. 1028 } 1029 } 1030 if (r != null) { 1031 if (mAm.checkComponentPermission(r.permission, 1032 callingPid, callingUid, r.appInfo.uid, r.exported) 1033 != PackageManager.PERMISSION_GRANTED) { 1034 if (!r.exported) { 1035 Slog.w(TAG, "Permission Denial: Accessing service " + r.name 1036 + " from pid=" + callingPid 1037 + ", uid=" + callingUid 1038 + " that is not exported from uid " + r.appInfo.uid); 1039 return new ServiceLookupResult(null, "not exported from uid " 1040 + r.appInfo.uid); 1041 } 1042 Slog.w(TAG, "Permission Denial: Accessing service " + r.name 1043 + " from pid=" + callingPid 1044 + ", uid=" + callingUid 1045 + " requires " + r.permission); 1046 return new ServiceLookupResult(null, r.permission); 1047 } 1048 if (!mAm.mIntentFirewall.checkService(r.name, service, callingUid, callingPid, 1049 resolvedType, r.appInfo)) { 1050 return null; 1051 } 1052 return new ServiceLookupResult(r, null); 1053 } 1054 return null; 1055 } 1056 1057 private final void bumpServiceExecutingLocked(ServiceRecord r, boolean fg, String why) { 1058 if (DEBUG_SERVICE) Slog.v(TAG, ">>> EXECUTING " 1059 + why + " of " + r + " in app " + r.app); 1060 else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG, ">>> EXECUTING " 1061 + why + " of " + r.shortName); 1062 long now = SystemClock.uptimeMillis(); 1063 if (r.executeNesting == 0) { 1064 r.executeFg = fg; 1065 ProcessStats.ServiceState stracker = r.getTracker(); 1066 if (stracker != null) { 1067 stracker.setExecuting(true, mAm.mProcessStats.getMemFactorLocked(), now); 1068 } 1069 if (r.app != null) { 1070 r.app.executingServices.add(r); 1071 r.app.execServicesFg |= fg; 1072 if (r.app.executingServices.size() == 1) { 1073 scheduleServiceTimeoutLocked(r.app); 1074 } 1075 } 1076 } else if (r.app != null && fg && !r.app.execServicesFg) { 1077 r.app.execServicesFg = true; 1078 scheduleServiceTimeoutLocked(r.app); 1079 } 1080 r.executeFg |= fg; 1081 r.executeNesting++; 1082 r.executingStart = now; 1083 } 1084 1085 private final boolean requestServiceBindingLocked(ServiceRecord r, 1086 IntentBindRecord i, boolean execInFg, boolean rebind) { 1087 if (r.app == null || r.app.thread == null) { 1088 // If service is not currently running, can't yet bind. 1089 return false; 1090 } 1091 if ((!i.requested || rebind) && i.apps.size() > 0) { 1092 try { 1093 bumpServiceExecutingLocked(r, execInFg, "bind"); 1094 r.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_SERVICE); 1095 r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind, 1096 r.app.repProcState); 1097 if (!rebind) { 1098 i.requested = true; 1099 } 1100 i.hasBound = true; 1101 i.doRebind = false; 1102 } catch (RemoteException e) { 1103 if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while binding " + r); 1104 return false; 1105 } 1106 } 1107 return true; 1108 } 1109 1110 private final boolean scheduleServiceRestartLocked(ServiceRecord r, 1111 boolean allowCancel) { 1112 boolean canceled = false; 1113 1114 ServiceMap smap = getServiceMap(r.userId); 1115 if (smap.mServicesByName.get(r.name) != r) { 1116 ServiceRecord cur = smap.mServicesByName.get(r.name); 1117 Slog.wtf(TAG, "Attempting to schedule restart of " + r 1118 + " when found in map: " + cur); 1119 return false; 1120 } 1121 1122 final long now = SystemClock.uptimeMillis(); 1123 1124 if ((r.serviceInfo.applicationInfo.flags 1125 &ApplicationInfo.FLAG_PERSISTENT) == 0) { 1126 long minDuration = SERVICE_RESTART_DURATION; 1127 long resetTime = SERVICE_RESET_RUN_DURATION; 1128 1129 // Any delivered but not yet finished starts should be put back 1130 // on the pending list. 1131 final int N = r.deliveredStarts.size(); 1132 if (N > 0) { 1133 for (int i=N-1; i>=0; i--) { 1134 ServiceRecord.StartItem si = r.deliveredStarts.get(i); 1135 si.removeUriPermissionsLocked(); 1136 if (si.intent == null) { 1137 // We'll generate this again if needed. 1138 } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT 1139 && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) { 1140 r.pendingStarts.add(0, si); 1141 long dur = SystemClock.uptimeMillis() - si.deliveredTime; 1142 dur *= 2; 1143 if (minDuration < dur) minDuration = dur; 1144 if (resetTime < dur) resetTime = dur; 1145 } else { 1146 Slog.w(TAG, "Canceling start item " + si.intent + " in service " 1147 + r.name); 1148 canceled = true; 1149 } 1150 } 1151 r.deliveredStarts.clear(); 1152 } 1153 1154 r.totalRestartCount++; 1155 if (r.restartDelay == 0) { 1156 r.restartCount++; 1157 r.restartDelay = minDuration; 1158 } else { 1159 // If it has been a "reasonably long time" since the service 1160 // was started, then reset our restart duration back to 1161 // the beginning, so we don't infinitely increase the duration 1162 // on a service that just occasionally gets killed (which is 1163 // a normal case, due to process being killed to reclaim memory). 1164 if (now > (r.restartTime+resetTime)) { 1165 r.restartCount = 1; 1166 r.restartDelay = minDuration; 1167 } else { 1168 r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR; 1169 if (r.restartDelay < minDuration) { 1170 r.restartDelay = minDuration; 1171 } 1172 } 1173 } 1174 1175 r.nextRestartTime = now + r.restartDelay; 1176 1177 // Make sure that we don't end up restarting a bunch of services 1178 // all at the same time. 1179 boolean repeat; 1180 do { 1181 repeat = false; 1182 for (int i=mRestartingServices.size()-1; i>=0; i--) { 1183 ServiceRecord r2 = mRestartingServices.get(i); 1184 if (r2 != r && r.nextRestartTime 1185 >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN) 1186 && r.nextRestartTime 1187 < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) { 1188 r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN; 1189 r.restartDelay = r.nextRestartTime - now; 1190 repeat = true; 1191 break; 1192 } 1193 } 1194 } while (repeat); 1195 1196 } else { 1197 // Persistent processes are immediately restarted, so there is no 1198 // reason to hold of on restarting their services. 1199 r.totalRestartCount++; 1200 r.restartCount = 0; 1201 r.restartDelay = 0; 1202 r.nextRestartTime = now; 1203 } 1204 1205 if (!mRestartingServices.contains(r)) { 1206 r.createdFromFg = false; 1207 mRestartingServices.add(r); 1208 r.makeRestarting(mAm.mProcessStats.getMemFactorLocked(), now); 1209 } 1210 1211 r.cancelNotification(); 1212 1213 mAm.mHandler.removeCallbacks(r.restarter); 1214 mAm.mHandler.postAtTime(r.restarter, r.nextRestartTime); 1215 r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay; 1216 Slog.w(TAG, "Scheduling restart of crashed service " 1217 + r.shortName + " in " + r.restartDelay + "ms"); 1218 EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART, 1219 r.userId, r.shortName, r.restartDelay); 1220 1221 return canceled; 1222 } 1223 1224 final void performServiceRestartLocked(ServiceRecord r) { 1225 if (!mRestartingServices.contains(r)) { 1226 return; 1227 } 1228 bringUpServiceLocked(r, r.intent.getIntent().getFlags(), r.createdFromFg, true); 1229 } 1230 1231 private final boolean unscheduleServiceRestartLocked(ServiceRecord r, int callingUid, 1232 boolean force) { 1233 if (!force && r.restartDelay == 0) { 1234 return false; 1235 } 1236 // Remove from the restarting list; if the service is currently on the 1237 // restarting list, or the call is coming from another app, then this 1238 // service has become of much more interest so we reset the restart interval. 1239 boolean removed = mRestartingServices.remove(r); 1240 if (removed || callingUid != r.appInfo.uid) { 1241 r.resetRestartCounter(); 1242 } 1243 if (removed) { 1244 clearRestartingIfNeededLocked(r); 1245 } 1246 mAm.mHandler.removeCallbacks(r.restarter); 1247 return true; 1248 } 1249 1250 private void clearRestartingIfNeededLocked(ServiceRecord r) { 1251 if (r.restartTracker != null) { 1252 // If this is the last restarting record with this tracker, then clear 1253 // the tracker's restarting state. 1254 boolean stillTracking = false; 1255 for (int i=mRestartingServices.size()-1; i>=0; i--) { 1256 if (mRestartingServices.get(i).restartTracker == r.restartTracker) { 1257 stillTracking = true; 1258 break; 1259 } 1260 } 1261 if (!stillTracking) { 1262 r.restartTracker.setRestarting(false, mAm.mProcessStats.getMemFactorLocked(), 1263 SystemClock.uptimeMillis()); 1264 r.restartTracker = null; 1265 } 1266 } 1267 } 1268 1269 private final String bringUpServiceLocked(ServiceRecord r, 1270 int intentFlags, boolean execInFg, boolean whileRestarting) { 1271 //Slog.i(TAG, "Bring up service:"); 1272 //r.dump(" "); 1273 1274 if (r.app != null && r.app.thread != null) { 1275 sendServiceArgsLocked(r, execInFg, false); 1276 return null; 1277 } 1278 1279 if (!whileRestarting && r.restartDelay > 0) { 1280 // If waiting for a restart, then do nothing. 1281 return null; 1282 } 1283 1284 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing up " + r + " " + r.intent); 1285 1286 // We are now bringing the service up, so no longer in the 1287 // restarting state. 1288 if (mRestartingServices.remove(r)) { 1289 clearRestartingIfNeededLocked(r); 1290 } 1291 1292 // Make sure this service is no longer considered delayed, we are starting it now. 1293 if (r.delayed) { 1294 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "REM FR DELAY LIST (bring up): " + r); 1295 getServiceMap(r.userId).mDelayedStartList.remove(r); 1296 r.delayed = false; 1297 } 1298 1299 // Make sure that the user who owns this service is started. If not, 1300 // we don't want to allow it to run. 1301 if (mAm.mStartedUsers.get(r.userId) == null) { 1302 String msg = "Unable to launch app " 1303 + r.appInfo.packageName + "/" 1304 + r.appInfo.uid + " for service " 1305 + r.intent.getIntent() + ": user " + r.userId + " is stopped"; 1306 Slog.w(TAG, msg); 1307 bringDownServiceLocked(r); 1308 return msg; 1309 } 1310 1311 // Service is now being launched, its package can't be stopped. 1312 try { 1313 AppGlobals.getPackageManager().setPackageStoppedState( 1314 r.packageName, false, r.userId); 1315 } catch (RemoteException e) { 1316 } catch (IllegalArgumentException e) { 1317 Slog.w(TAG, "Failed trying to unstop package " 1318 + r.packageName + ": " + e); 1319 } 1320 1321 final boolean isolated = (r.serviceInfo.flags&ServiceInfo.FLAG_ISOLATED_PROCESS) != 0; 1322 final String procName = r.processName; 1323 ProcessRecord app; 1324 1325 if (!isolated) { 1326 app = mAm.getProcessRecordLocked(procName, r.appInfo.uid, false); 1327 if (DEBUG_MU) Slog.v(TAG_MU, "bringUpServiceLocked: appInfo.uid=" + r.appInfo.uid 1328 + " app=" + app); 1329 if (app != null && app.thread != null) { 1330 try { 1331 app.addPackage(r.appInfo.packageName, r.appInfo.versionCode, mAm.mProcessStats); 1332 realStartServiceLocked(r, app, execInFg); 1333 return null; 1334 } catch (RemoteException e) { 1335 Slog.w(TAG, "Exception when starting service " + r.shortName, e); 1336 } 1337 1338 // If a dead object exception was thrown -- fall through to 1339 // restart the application. 1340 } 1341 } else { 1342 // If this service runs in an isolated process, then each time 1343 // we call startProcessLocked() we will get a new isolated 1344 // process, starting another process if we are currently waiting 1345 // for a previous process to come up. To deal with this, we store 1346 // in the service any current isolated process it is running in or 1347 // waiting to have come up. 1348 app = r.isolatedProc; 1349 } 1350 1351 // Not running -- get it started, and enqueue this service record 1352 // to be executed when the app comes up. 1353 if (app == null) { 1354 if ((app=mAm.startProcessLocked(procName, r.appInfo, true, intentFlags, 1355 "service", r.name, false, isolated, false)) == null) { 1356 String msg = "Unable to launch app " 1357 + r.appInfo.packageName + "/" 1358 + r.appInfo.uid + " for service " 1359 + r.intent.getIntent() + ": process is bad"; 1360 Slog.w(TAG, msg); 1361 bringDownServiceLocked(r); 1362 return msg; 1363 } 1364 if (isolated) { 1365 r.isolatedProc = app; 1366 } 1367 } 1368 1369 if (!mPendingServices.contains(r)) { 1370 mPendingServices.add(r); 1371 } 1372 1373 if (r.delayedStop) { 1374 // Oh and hey we've already been asked to stop! 1375 r.delayedStop = false; 1376 if (r.startRequested) { 1377 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Applying delayed stop (in bring up): " + r); 1378 stopServiceLocked(r); 1379 } 1380 } 1381 1382 return null; 1383 } 1384 1385 private final void requestServiceBindingsLocked(ServiceRecord r, boolean execInFg) { 1386 for (int i=r.bindings.size()-1; i>=0; i--) { 1387 IntentBindRecord ibr = r.bindings.valueAt(i); 1388 if (!requestServiceBindingLocked(r, ibr, execInFg, false)) { 1389 break; 1390 } 1391 } 1392 } 1393 1394 private final void realStartServiceLocked(ServiceRecord r, 1395 ProcessRecord app, boolean execInFg) throws RemoteException { 1396 if (app.thread == null) { 1397 throw new RemoteException(); 1398 } 1399 if (DEBUG_MU) 1400 Slog.v(TAG_MU, "realStartServiceLocked, ServiceRecord.uid = " + r.appInfo.uid 1401 + ", ProcessRecord.uid = " + app.uid); 1402 r.app = app; 1403 r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); 1404 1405 app.services.add(r); 1406 bumpServiceExecutingLocked(r, execInFg, "create"); 1407 mAm.updateLruProcessLocked(app, false, null); 1408 mAm.updateOomAdjLocked(); 1409 1410 boolean created = false; 1411 try { 1412 String nameTerm; 1413 int lastPeriod = r.shortName.lastIndexOf('.'); 1414 nameTerm = lastPeriod >= 0 ? r.shortName.substring(lastPeriod) : r.shortName; 1415 EventLogTags.writeAmCreateService( 1416 r.userId, System.identityHashCode(r), nameTerm, r.app.uid, r.app.pid); 1417 synchronized (r.stats.getBatteryStats()) { 1418 r.stats.startLaunchedLocked(); 1419 } 1420 mAm.ensurePackageDexOpt(r.serviceInfo.packageName); 1421 app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_SERVICE); 1422 app.thread.scheduleCreateService(r, r.serviceInfo, 1423 mAm.compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo), 1424 app.repProcState); 1425 r.postNotification(); 1426 created = true; 1427 } finally { 1428 if (!created) { 1429 app.services.remove(r); 1430 r.app = null; 1431 scheduleServiceRestartLocked(r, false); 1432 } 1433 } 1434 1435 requestServiceBindingsLocked(r, execInFg); 1436 1437 // If the service is in the started state, and there are no 1438 // pending arguments, then fake up one so its onStartCommand() will 1439 // be called. 1440 if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { 1441 r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), 1442 null, null)); 1443 } 1444 1445 sendServiceArgsLocked(r, execInFg, true); 1446 1447 if (r.delayed) { 1448 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "REM FR DELAY LIST (new proc): " + r); 1449 getServiceMap(r.userId).mDelayedStartList.remove(r); 1450 r.delayed = false; 1451 } 1452 1453 if (r.delayedStop) { 1454 // Oh and hey we've already been asked to stop! 1455 r.delayedStop = false; 1456 if (r.startRequested) { 1457 if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Applying delayed stop (from start): " + r); 1458 stopServiceLocked(r); 1459 } 1460 } 1461 } 1462 1463 private final void sendServiceArgsLocked(ServiceRecord r, boolean execInFg, 1464 boolean oomAdjusted) { 1465 final int N = r.pendingStarts.size(); 1466 if (N == 0) { 1467 return; 1468 } 1469 1470 while (r.pendingStarts.size() > 0) { 1471 try { 1472 ServiceRecord.StartItem si = r.pendingStarts.remove(0); 1473 if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: " 1474 + r + " " + r.intent + " args=" + si.intent); 1475 if (si.intent == null && N > 1) { 1476 // If somehow we got a dummy null intent in the middle, 1477 // then skip it. DO NOT skip a null intent when it is 1478 // the only one in the list -- this is to support the 1479 // onStartCommand(null) case. 1480 continue; 1481 } 1482 si.deliveredTime = SystemClock.uptimeMillis(); 1483 r.deliveredStarts.add(si); 1484 si.deliveryCount++; 1485 if (si.neededGrants != null) { 1486 mAm.grantUriPermissionUncheckedFromIntentLocked(si.neededGrants, 1487 si.getUriPermissionsLocked()); 1488 } 1489 bumpServiceExecutingLocked(r, execInFg, "start"); 1490 if (!oomAdjusted) { 1491 oomAdjusted = true; 1492 mAm.updateOomAdjLocked(r.app); 1493 } 1494 int flags = 0; 1495 if (si.deliveryCount > 1) { 1496 flags |= Service.START_FLAG_RETRY; 1497 } 1498 if (si.doneExecutingCount > 0) { 1499 flags |= Service.START_FLAG_REDELIVERY; 1500 } 1501 r.app.thread.scheduleServiceArgs(r, si.taskRemoved, si.id, flags, si.intent); 1502 } catch (RemoteException e) { 1503 // Remote process gone... we'll let the normal cleanup take 1504 // care of this. 1505 if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r); 1506 break; 1507 } catch (Exception e) { 1508 Slog.w(TAG, "Unexpected exception", e); 1509 break; 1510 } 1511 } 1512 } 1513 1514 private final boolean isServiceNeeded(ServiceRecord r, boolean knowConn, boolean hasConn) { 1515 // Are we still explicitly being asked to run? 1516 if (r.startRequested) { 1517 return true; 1518 } 1519 1520 // Is someone still bound to us keepign us running? 1521 if (!knowConn) { 1522 hasConn = r.hasAutoCreateConnections(); 1523 } 1524 if (hasConn) { 1525 return true; 1526 } 1527 1528 return false; 1529 } 1530 1531 private final void bringDownServiceIfNeededLocked(ServiceRecord r, boolean knowConn, 1532 boolean hasConn) { 1533 //Slog.i(TAG, "Bring down service:"); 1534 //r.dump(" "); 1535 1536 if (isServiceNeeded(r, knowConn, hasConn)) { 1537 return; 1538 } 1539 1540 // Are we in the process of launching? 1541 if (mPendingServices.contains(r)) { 1542 return; 1543 } 1544 1545 bringDownServiceLocked(r); 1546 } 1547 1548 private final void bringDownServiceLocked(ServiceRecord r) { 1549 //Slog.i(TAG, "Bring down service:"); 1550 //r.dump(" "); 1551 1552 // Report to all of the connections that the service is no longer 1553 // available. 1554 for (int conni=r.connections.size()-1; conni>=0; conni--) { 1555 ArrayList<ConnectionRecord> c = r.connections.valueAt(conni); 1556 for (int i=0; i<c.size(); i++) { 1557 ConnectionRecord cr = c.get(i); 1558 // There is still a connection to the service that is 1559 // being brought down. Mark it as dead. 1560 cr.serviceDead = true; 1561 try { 1562 cr.conn.connected(r.name, null); 1563 } catch (Exception e) { 1564 Slog.w(TAG, "Failure disconnecting service " + r.name + 1565 " to connection " + c.get(i).conn.asBinder() + 1566 " (in " + c.get(i).binding.client.processName + ")", e); 1567 } 1568 } 1569 } 1570 1571 // Tell the service that it has been unbound. 1572 if (r.app != null && r.app.thread != null) { 1573 for (int i=r.bindings.size()-1; i>=0; i--) { 1574 IntentBindRecord ibr = r.bindings.valueAt(i); 1575 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down binding " + ibr 1576 + ": hasBound=" + ibr.hasBound); 1577 if (ibr.hasBound) { 1578 try { 1579 bumpServiceExecutingLocked(r, false, "bring down unbind"); 1580 mAm.updateOomAdjLocked(r.app); 1581 ibr.hasBound = false; 1582 r.app.thread.scheduleUnbindService(r, 1583 ibr.intent.getIntent()); 1584 } catch (Exception e) { 1585 Slog.w(TAG, "Exception when unbinding service " 1586 + r.shortName, e); 1587 serviceProcessGoneLocked(r); 1588 } 1589 } 1590 } 1591 } 1592 1593 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent); 1594 EventLogTags.writeAmDestroyService( 1595 r.userId, System.identityHashCode(r), (r.app != null) ? r.app.pid : -1); 1596 1597 final ServiceMap smap = getServiceMap(r.userId); 1598 smap.mServicesByName.remove(r.name); 1599 smap.mServicesByIntent.remove(r.intent); 1600 r.totalRestartCount = 0; 1601 unscheduleServiceRestartLocked(r, 0, true); 1602 1603 // Also make sure it is not on the pending list. 1604 for (int i=mPendingServices.size()-1; i>=0; i--) { 1605 if (mPendingServices.get(i) == r) { 1606 mPendingServices.remove(i); 1607 if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r); 1608 } 1609 } 1610 1611 r.cancelNotification(); 1612 r.isForeground = false; 1613 r.foregroundId = 0; 1614 r.foregroundNoti = null; 1615 1616 // Clear start entries. 1617 r.clearDeliveredStartsLocked(); 1618 r.pendingStarts.clear(); 1619 1620 if (r.app != null) { 1621 synchronized (r.stats.getBatteryStats()) { 1622 r.stats.stopLaunchedLocked(); 1623 } 1624 r.app.services.remove(r); 1625 if (r.app.thread != null) { 1626 updateServiceForegroundLocked(r.app, false); 1627 try { 1628 bumpServiceExecutingLocked(r, false, "destroy"); 1629 mDestroyingServices.add(r); 1630 mAm.updateOomAdjLocked(r.app); 1631 r.app.thread.scheduleStopService(r); 1632 } catch (Exception e) { 1633 Slog.w(TAG, "Exception when destroying service " 1634 + r.shortName, e); 1635 serviceProcessGoneLocked(r); 1636 } 1637 } else { 1638 if (DEBUG_SERVICE) Slog.v( 1639 TAG, "Removed service that has no process: " + r); 1640 } 1641 } else { 1642 if (DEBUG_SERVICE) Slog.v( 1643 TAG, "Removed service that is not running: " + r); 1644 } 1645 1646 if (r.bindings.size() > 0) { 1647 r.bindings.clear(); 1648 } 1649 1650 if (r.restarter instanceof ServiceRestarter) { 1651 ((ServiceRestarter)r.restarter).setService(null); 1652 } 1653 1654 int memFactor = mAm.mProcessStats.getMemFactorLocked(); 1655 long now = SystemClock.uptimeMillis(); 1656 if (r.tracker != null) { 1657 r.tracker.setStarted(false, memFactor, now); 1658 r.tracker.setBound(false, memFactor, now); 1659 if (r.executeNesting == 0) { 1660 r.tracker.clearCurrentOwner(r, false); 1661 r.tracker = null; 1662 } 1663 } 1664 1665 smap.ensureNotStartingBackground(r); 1666 } 1667 1668 void removeConnectionLocked( 1669 ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { 1670 IBinder binder = c.conn.asBinder(); 1671 AppBindRecord b = c.binding; 1672 ServiceRecord s = b.service; 1673 ArrayList<ConnectionRecord> clist = s.connections.get(binder); 1674 if (clist != null) { 1675 clist.remove(c); 1676 if (clist.size() == 0) { 1677 s.connections.remove(binder); 1678 } 1679 } 1680 b.connections.remove(c); 1681 if (c.activity != null && c.activity != skipAct) { 1682 if (c.activity.connections != null) { 1683 c.activity.connections.remove(c); 1684 } 1685 } 1686 if (b.client != skipApp) { 1687 b.client.connections.remove(c); 1688 if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { 1689 b.client.updateHasAboveClientLocked(); 1690 } 1691 if (s.app != null) { 1692 updateServiceClientActivitiesLocked(s.app, c); 1693 } 1694 } 1695 clist = mServiceConnections.get(binder); 1696 if (clist != null) { 1697 clist.remove(c); 1698 if (clist.size() == 0) { 1699 mServiceConnections.remove(binder); 1700 } 1701 } 1702 1703 if (b.connections.size() == 0) { 1704 b.intent.apps.remove(b.client); 1705 } 1706 1707 if (!c.serviceDead) { 1708 if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent 1709 + ": shouldUnbind=" + b.intent.hasBound); 1710 if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 1711 && b.intent.hasBound) { 1712 try { 1713 bumpServiceExecutingLocked(s, false, "unbind"); 1714 if (b.client != s.app && (c.flags&Context.BIND_WAIVE_PRIORITY) == 0 1715 && s.app.setProcState <= ActivityManager.PROCESS_STATE_RECEIVER) { 1716 // If this service's process is not already in the cached list, 1717 // then update it in the LRU list here because this may be causing 1718 // it to go down there and we want it to start out near the top. 1719 mAm.updateLruProcessLocked(s.app, false, null); 1720 } 1721 mAm.updateOomAdjLocked(s.app); 1722 b.intent.hasBound = false; 1723 // Assume the client doesn't want to know about a rebind; 1724 // we will deal with that later if it asks for one. 1725 b.intent.doRebind = false; 1726 s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); 1727 } catch (Exception e) { 1728 Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); 1729 serviceProcessGoneLocked(s); 1730 } 1731 } 1732 1733 if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { 1734 boolean hasAutoCreate = s.hasAutoCreateConnections(); 1735 if (!hasAutoCreate) { 1736 if (s.tracker != null) { 1737 s.tracker.setBound(false, mAm.mProcessStats.getMemFactorLocked(), 1738 SystemClock.uptimeMillis()); 1739 } 1740 } 1741 bringDownServiceIfNeededLocked(s, true, hasAutoCreate); 1742 } 1743 } 1744 } 1745 1746 void serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res) { 1747 boolean inDestroying = mDestroyingServices.contains(r); 1748 if (r != null) { 1749 if (type == 1) { 1750 // This is a call from a service start... take care of 1751 // book-keeping. 1752 r.callStart = true; 1753 switch (res) { 1754 case Service.START_STICKY_COMPATIBILITY: 1755 case Service.START_STICKY: { 1756 // We are done with the associated start arguments. 1757 r.findDeliveredStart(startId, true); 1758 // Don't stop if killed. 1759 r.stopIfKilled = false; 1760 break; 1761 } 1762 case Service.START_NOT_STICKY: { 1763 // We are done with the associated start arguments. 1764 r.findDeliveredStart(startId, true); 1765 if (r.getLastStartId() == startId) { 1766 // There is no more work, and this service 1767 // doesn't want to hang around if killed. 1768 r.stopIfKilled = true; 1769 } 1770 break; 1771 } 1772 case Service.START_REDELIVER_INTENT: { 1773 // We'll keep this item until they explicitly 1774 // call stop for it, but keep track of the fact 1775 // that it was delivered. 1776 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); 1777 if (si != null) { 1778 si.deliveryCount = 0; 1779 si.doneExecutingCount++; 1780 // Don't stop if killed. 1781 r.stopIfKilled = true; 1782 } 1783 break; 1784 } 1785 case Service.START_TASK_REMOVED_COMPLETE: { 1786 // Special processing for onTaskRemoved(). Don't 1787 // impact normal onStartCommand() processing. 1788 r.findDeliveredStart(startId, true); 1789 break; 1790 } 1791 default: 1792 throw new IllegalArgumentException( 1793 "Unknown service start result: " + res); 1794 } 1795 if (res == Service.START_STICKY_COMPATIBILITY) { 1796 r.callStart = false; 1797 } 1798 } 1799 final long origId = Binder.clearCallingIdentity(); 1800 serviceDoneExecutingLocked(r, inDestroying, inDestroying); 1801 Binder.restoreCallingIdentity(origId); 1802 } else { 1803 Slog.w(TAG, "Done executing unknown service from pid " 1804 + Binder.getCallingPid()); 1805 } 1806 } 1807 1808 private void serviceProcessGoneLocked(ServiceRecord r) { 1809 if (r.tracker != null) { 1810 int memFactor = mAm.mProcessStats.getMemFactorLocked(); 1811 long now = SystemClock.uptimeMillis(); 1812 r.tracker.setExecuting(false, memFactor, now); 1813 r.tracker.setBound(false, memFactor, now); 1814 r.tracker.setStarted(false, memFactor, now); 1815 } 1816 serviceDoneExecutingLocked(r, true, true); 1817 } 1818 1819 private void serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying, 1820 boolean finishing) { 1821 if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r 1822 + ": nesting=" + r.executeNesting 1823 + ", inDestroying=" + inDestroying + ", app=" + r.app); 1824 else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG, "<<< DONE EXECUTING " + r.shortName); 1825 r.executeNesting--; 1826 if (r.executeNesting <= 0) { 1827 if (r.app != null) { 1828 if (DEBUG_SERVICE) Slog.v(TAG, 1829 "Nesting at 0 of " + r.shortName); 1830 r.app.execServicesFg = false; 1831 r.app.executingServices.remove(r); 1832 if (r.app.executingServices.size() == 0) { 1833 if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG, 1834 "No more executingServices of " + r.shortName); 1835 mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_TIMEOUT_MSG, r.app); 1836 } else if (r.executeFg) { 1837 // Need to re-evaluate whether the app still needs to be in the foreground. 1838 for (int i=r.app.executingServices.size()-1; i>=0; i--) { 1839 if (r.app.executingServices.valueAt(i).executeFg) { 1840 r.app.execServicesFg = true; 1841 break; 1842 } 1843 } 1844 } 1845 if (inDestroying) { 1846 if (DEBUG_SERVICE) Slog.v(TAG, 1847 "doneExecuting remove destroying " + r); 1848 mDestroyingServices.remove(r); 1849 r.bindings.clear(); 1850 } 1851 mAm.updateOomAdjLocked(r.app); 1852 } 1853 r.executeFg = false; 1854 if (r.tracker != null) { 1855 r.tracker.setExecuting(false, mAm.mProcessStats.getMemFactorLocked(), 1856 SystemClock.uptimeMillis()); 1857 if (finishing) { 1858 r.tracker.clearCurrentOwner(r, false); 1859 r.tracker = null; 1860 } 1861 } 1862 if (finishing) { 1863 if (r.app != null && !r.app.persistent) { 1864 r.app.services.remove(r); 1865 } 1866 r.app = null; 1867 } 1868 } 1869 } 1870 1871 boolean attachApplicationLocked(ProcessRecord proc, String processName) throws Exception { 1872 boolean didSomething = false; 1873 // Collect any services that are waiting for this process to come up. 1874 if (mPendingServices.size() > 0) { 1875 ServiceRecord sr = null; 1876 try { 1877 for (int i=0; i<mPendingServices.size(); i++) { 1878 sr = mPendingServices.get(i); 1879 if (proc != sr.isolatedProc && (proc.uid != sr.appInfo.uid 1880 || !processName.equals(sr.processName))) { 1881 continue; 1882 } 1883 1884 mPendingServices.remove(i); 1885 i--; 1886 proc.addPackage(sr.appInfo.packageName, sr.appInfo.versionCode, 1887 mAm.mProcessStats); 1888 realStartServiceLocked(sr, proc, sr.createdFromFg); 1889 didSomething = true; 1890 } 1891 } catch (Exception e) { 1892 Slog.w(TAG, "Exception in new application when starting service " 1893 + sr.shortName, e); 1894 throw e; 1895 } 1896 } 1897 // Also, if there are any services that are waiting to restart and 1898 // would run in this process, now is a good time to start them. It would 1899 // be weird to bring up the process but arbitrarily not let the services 1900 // run at this point just because their restart time hasn't come up. 1901 if (mRestartingServices.size() > 0) { 1902 ServiceRecord sr = null; 1903 for (int i=0; i<mRestartingServices.size(); i++) { 1904 sr = mRestartingServices.get(i); 1905 if (proc != sr.isolatedProc && (proc.uid != sr.appInfo.uid 1906 || !processName.equals(sr.processName))) { 1907 continue; 1908 } 1909 mAm.mHandler.removeCallbacks(sr.restarter); 1910 mAm.mHandler.post(sr.restarter); 1911 } 1912 } 1913 return didSomething; 1914 } 1915 1916 void processStartTimedOutLocked(ProcessRecord proc) { 1917 for (int i=0; i<mPendingServices.size(); i++) { 1918 ServiceRecord sr = mPendingServices.get(i); 1919 if ((proc.uid == sr.appInfo.uid 1920 && proc.processName.equals(sr.processName)) 1921 || sr.isolatedProc == proc) { 1922 Slog.w(TAG, "Forcing bringing down service: " + sr); 1923 sr.isolatedProc = null; 1924 mPendingServices.remove(i); 1925 i--; 1926 bringDownServiceLocked(sr); 1927 } 1928 } 1929 } 1930 1931 private boolean collectForceStopServicesLocked(String name, int userId, 1932 boolean evenPersistent, boolean doit, 1933 ArrayMap<ComponentName, ServiceRecord> services, 1934 ArrayList<ServiceRecord> result) { 1935 boolean didSomething = false; 1936 for (int i=0; i<services.size(); i++) { 1937 ServiceRecord service = services.valueAt(i); 1938 if ((name == null || service.packageName.equals(name)) 1939 && (service.app == null || evenPersistent || !service.app.persistent)) { 1940 if (!doit) { 1941 return true; 1942 } 1943 didSomething = true; 1944 Slog.i(TAG, " Force stopping service " + service); 1945 if (service.app != null) { 1946 service.app.removed = true; 1947 if (!service.app.persistent) { 1948 service.app.services.remove(service); 1949 } 1950 } 1951 service.app = null; 1952 service.isolatedProc = null; 1953 result.add(service); 1954 } 1955 } 1956 return didSomething; 1957 } 1958 1959 boolean forceStopLocked(String name, int userId, boolean evenPersistent, boolean doit) { 1960 boolean didSomething = false; 1961 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 1962 if (userId == UserHandle.USER_ALL) { 1963 for (int i=0; i<mServiceMap.size(); i++) { 1964 didSomething |= collectForceStopServicesLocked(name, userId, evenPersistent, 1965 doit, mServiceMap.valueAt(i).mServicesByName, services); 1966 if (!doit && didSomething) { 1967 return true; 1968 } 1969 } 1970 } else { 1971 ServiceMap smap = mServiceMap.get(userId); 1972 if (smap != null) { 1973 ArrayMap<ComponentName, ServiceRecord> items = smap.mServicesByName; 1974 didSomething = collectForceStopServicesLocked(name, userId, evenPersistent, 1975 doit, items, services); 1976 } 1977 } 1978 1979 int N = services.size(); 1980 for (int i=0; i<N; i++) { 1981 bringDownServiceLocked(services.get(i)); 1982 } 1983 return didSomething; 1984 } 1985 1986 void cleanUpRemovedTaskLocked(TaskRecord tr, ComponentName component, Intent baseIntent) { 1987 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 1988 ArrayMap<ComponentName, ServiceRecord> alls = getServices(tr.userId); 1989 for (int i=0; i<alls.size(); i++) { 1990 ServiceRecord sr = alls.valueAt(i); 1991 if (sr.packageName.equals(component.getPackageName())) { 1992 services.add(sr); 1993 } 1994 } 1995 1996 // Take care of any running services associated with the app. 1997 for (int i=0; i<services.size(); i++) { 1998 ServiceRecord sr = services.get(i); 1999 if (sr.startRequested) { 2000 if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) { 2001 Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task"); 2002 stopServiceLocked(sr); 2003 } else { 2004 sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true, 2005 sr.makeNextStartId(), baseIntent, null)); 2006 if (sr.app != null && sr.app.thread != null) { 2007 // We always run in the foreground, since this is called as 2008 // part of the "remove task" UI operation. 2009 sendServiceArgsLocked(sr, true, false); 2010 } 2011 } 2012 } 2013 } 2014 } 2015 2016 final void killServicesLocked(ProcessRecord app, boolean allowRestart) { 2017 // Report disconnected services. 2018 if (false) { 2019 // XXX we are letting the client link to the service for 2020 // death notifications. 2021 if (app.services.size() > 0) { 2022 Iterator<ServiceRecord> it = app.services.iterator(); 2023 while (it.hasNext()) { 2024 ServiceRecord r = it.next(); 2025 for (int conni=r.connections.size()-1; conni>=0; conni--) { 2026 ArrayList<ConnectionRecord> cl = r.connections.valueAt(conni); 2027 for (int i=0; i<cl.size(); i++) { 2028 ConnectionRecord c = cl.get(i); 2029 if (c.binding.client != app) { 2030 try { 2031 //c.conn.connected(r.className, null); 2032 } catch (Exception e) { 2033 // todo: this should be asynchronous! 2034 Slog.w(TAG, "Exception thrown disconnected servce " 2035 + r.shortName 2036 + " from app " + app.processName, e); 2037 } 2038 } 2039 } 2040 } 2041 } 2042 } 2043 } 2044 2045 // First clear app state from services. 2046 for (int i=app.services.size()-1; i>=0; i--) { 2047 ServiceRecord sr = app.services.valueAt(i); 2048 synchronized (sr.stats.getBatteryStats()) { 2049 sr.stats.stopLaunchedLocked(); 2050 } 2051 if (sr.app != app && sr.app != null && !sr.app.persistent) { 2052 sr.app.services.remove(sr); 2053 } 2054 sr.app = null; 2055 sr.isolatedProc = null; 2056 sr.executeNesting = 0; 2057 sr.forceClearTracker(); 2058 if (mDestroyingServices.remove(sr)) { 2059 if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove destroying " + sr); 2060 } 2061 2062 final int numClients = sr.bindings.size(); 2063 for (int bindingi=numClients-1; bindingi>=0; bindingi--) { 2064 IntentBindRecord b = sr.bindings.valueAt(bindingi); 2065 if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b 2066 + ": shouldUnbind=" + b.hasBound); 2067 b.binder = null; 2068 b.requested = b.received = b.hasBound = false; 2069 } 2070 } 2071 2072 // Clean up any connections this application has to other services. 2073 for (int i=app.connections.size()-1; i>=0; i--) { 2074 ConnectionRecord r = app.connections.valueAt(i); 2075 removeConnectionLocked(r, app, null); 2076 } 2077 app.connections.clear(); 2078 2079 ServiceMap smap = getServiceMap(app.userId); 2080 2081 // Now do remaining service cleanup. 2082 for (int i=app.services.size()-1; i>=0; i--) { 2083 ServiceRecord sr = app.services.valueAt(i); 2084 2085 // Unless the process is persistent, this process record is going away, 2086 // so make sure the service is cleaned out of it. 2087 if (!app.persistent) { 2088 app.services.removeAt(i); 2089 } 2090 2091 // Sanity check: if the service listed for the app is not one 2092 // we actually are maintaining, just let it drop. 2093 final ServiceRecord curRec = smap.mServicesByName.get(sr.name); 2094 if (curRec != sr) { 2095 if (curRec != null) { 2096 Slog.wtf(TAG, "Service " + sr + " in process " + app 2097 + " not same as in map: " + curRec); 2098 } 2099 continue; 2100 } 2101 2102 // Any services running in the application may need to be placed 2103 // back in the pending list. 2104 if (allowRestart && sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags 2105 &ApplicationInfo.FLAG_PERSISTENT) == 0) { 2106 Slog.w(TAG, "Service crashed " + sr.crashCount 2107 + " times, stopping: " + sr); 2108 EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, 2109 sr.userId, sr.crashCount, sr.shortName, app.pid); 2110 bringDownServiceLocked(sr); 2111 } else if (!allowRestart) { 2112 bringDownServiceLocked(sr); 2113 } else { 2114 boolean canceled = scheduleServiceRestartLocked(sr, true); 2115 2116 // Should the service remain running? Note that in the 2117 // extreme case of so many attempts to deliver a command 2118 // that it failed we also will stop it here. 2119 if (sr.startRequested && (sr.stopIfKilled || canceled)) { 2120 if (sr.pendingStarts.size() == 0) { 2121 sr.startRequested = false; 2122 if (sr.tracker != null) { 2123 sr.tracker.setStarted(false, mAm.mProcessStats.getMemFactorLocked(), 2124 SystemClock.uptimeMillis()); 2125 } 2126 if (!sr.hasAutoCreateConnections()) { 2127 // Whoops, no reason to restart! 2128 bringDownServiceLocked(sr); 2129 } 2130 } 2131 } 2132 } 2133 } 2134 2135 if (!allowRestart) { 2136 app.services.clear(); 2137 2138 // Make sure there are no more restarting services for this process. 2139 for (int i=mRestartingServices.size()-1; i>=0; i--) { 2140 ServiceRecord r = mRestartingServices.get(i); 2141 if (r.processName.equals(app.processName) && 2142 r.serviceInfo.applicationInfo.uid == app.info.uid) { 2143 mRestartingServices.remove(i); 2144 clearRestartingIfNeededLocked(r); 2145 } 2146 } 2147 for (int i=mPendingServices.size()-1; i>=0; i--) { 2148 ServiceRecord r = mPendingServices.get(i); 2149 if (r.processName.equals(app.processName) && 2150 r.serviceInfo.applicationInfo.uid == app.info.uid) { 2151 mPendingServices.remove(i); 2152 } 2153 } 2154 } 2155 2156 // Make sure we have no more records on the stopping list. 2157 int i = mDestroyingServices.size(); 2158 while (i > 0) { 2159 i--; 2160 ServiceRecord sr = mDestroyingServices.get(i); 2161 if (sr.app == app) { 2162 sr.forceClearTracker(); 2163 mDestroyingServices.remove(i); 2164 if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove destroying " + sr); 2165 } 2166 } 2167 2168 app.executingServices.clear(); 2169 } 2170 2171 ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { 2172 ActivityManager.RunningServiceInfo info = 2173 new ActivityManager.RunningServiceInfo(); 2174 info.service = r.name; 2175 if (r.app != null) { 2176 info.pid = r.app.pid; 2177 } 2178 info.uid = r.appInfo.uid; 2179 info.process = r.processName; 2180 info.foreground = r.isForeground; 2181 info.activeSince = r.createTime; 2182 info.started = r.startRequested; 2183 info.clientCount = r.connections.size(); 2184 info.crashCount = r.crashCount; 2185 info.lastActivityTime = r.lastActivity; 2186 if (r.isForeground) { 2187 info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; 2188 } 2189 if (r.startRequested) { 2190 info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; 2191 } 2192 if (r.app != null && r.app.pid == ActivityManagerService.MY_PID) { 2193 info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; 2194 } 2195 if (r.app != null && r.app.persistent) { 2196 info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; 2197 } 2198 2199 for (int conni=r.connections.size()-1; conni>=0; conni--) { 2200 ArrayList<ConnectionRecord> connl = r.connections.valueAt(conni); 2201 for (int i=0; i<connl.size(); i++) { 2202 ConnectionRecord conn = connl.get(i); 2203 if (conn.clientLabel != 0) { 2204 info.clientPackage = conn.binding.client.info.packageName; 2205 info.clientLabel = conn.clientLabel; 2206 return info; 2207 } 2208 } 2209 } 2210 return info; 2211 } 2212 2213 List<ActivityManager.RunningServiceInfo> getRunningServiceInfoLocked(int maxNum, 2214 int flags) { 2215 ArrayList<ActivityManager.RunningServiceInfo> res 2216 = new ArrayList<ActivityManager.RunningServiceInfo>(); 2217 2218 final int uid = Binder.getCallingUid(); 2219 final long ident = Binder.clearCallingIdentity(); 2220 try { 2221 if (ActivityManager.checkUidPermission( 2222 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, 2223 uid) == PackageManager.PERMISSION_GRANTED) { 2224 int[] users = mAm.getUsersLocked(); 2225 for (int ui=0; ui<users.length && res.size() < maxNum; ui++) { 2226 ArrayMap<ComponentName, ServiceRecord> alls = getServices(users[ui]); 2227 for (int i=0; i<alls.size() && res.size() < maxNum; i++) { 2228 ServiceRecord sr = alls.valueAt(i); 2229 res.add(makeRunningServiceInfoLocked(sr)); 2230 } 2231 } 2232 2233 for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { 2234 ServiceRecord r = mRestartingServices.get(i); 2235 ActivityManager.RunningServiceInfo info = 2236 makeRunningServiceInfoLocked(r); 2237 info.restarting = r.nextRestartTime; 2238 res.add(info); 2239 } 2240 } else { 2241 int userId = UserHandle.getUserId(uid); 2242 ArrayMap<ComponentName, ServiceRecord> alls = getServices(userId); 2243 for (int i=0; i<alls.size() && res.size() < maxNum; i++) { 2244 ServiceRecord sr = alls.valueAt(i); 2245 res.add(makeRunningServiceInfoLocked(sr)); 2246 } 2247 2248 for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { 2249 ServiceRecord r = mRestartingServices.get(i); 2250 if (r.userId == userId) { 2251 ActivityManager.RunningServiceInfo info = 2252 makeRunningServiceInfoLocked(r); 2253 info.restarting = r.nextRestartTime; 2254 res.add(info); 2255 } 2256 } 2257 } 2258 } finally { 2259 Binder.restoreCallingIdentity(ident); 2260 } 2261 2262 return res; 2263 } 2264 2265 public PendingIntent getRunningServiceControlPanelLocked(ComponentName name) { 2266 int userId = UserHandle.getUserId(Binder.getCallingUid()); 2267 ServiceRecord r = getServiceByName(name, userId); 2268 if (r != null) { 2269 for (int conni=r.connections.size()-1; conni>=0; conni--) { 2270 ArrayList<ConnectionRecord> conn = r.connections.valueAt(conni); 2271 for (int i=0; i<conn.size(); i++) { 2272 if (conn.get(i).clientIntent != null) { 2273 return conn.get(i).clientIntent; 2274 } 2275 } 2276 } 2277 } 2278 return null; 2279 } 2280 2281 void serviceTimeout(ProcessRecord proc) { 2282 String anrMessage = null; 2283 2284 synchronized(this) { 2285 if (proc.executingServices.size() == 0 || proc.thread == null) { 2286 return; 2287 } 2288 long maxTime = SystemClock.uptimeMillis() - 2289 (proc.execServicesFg ? SERVICE_TIMEOUT : SERVICE_BACKGROUND_TIMEOUT); 2290 ServiceRecord timeout = null; 2291 long nextTime = 0; 2292 for (int i=proc.executingServices.size()-1; i>=0; i--) { 2293 ServiceRecord sr = proc.executingServices.valueAt(i); 2294 if (sr.executingStart < maxTime) { 2295 timeout = sr; 2296 break; 2297 } 2298 if (sr.executingStart > nextTime) { 2299 nextTime = sr.executingStart; 2300 } 2301 } 2302 if (timeout != null && mAm.mLruProcesses.contains(proc)) { 2303 Slog.w(TAG, "Timeout executing service: " + timeout); 2304 anrMessage = "Executing service " + timeout.shortName; 2305 } else { 2306 Message msg = mAm.mHandler.obtainMessage( 2307 ActivityManagerService.SERVICE_TIMEOUT_MSG); 2308 msg.obj = proc; 2309 mAm.mHandler.sendMessageAtTime(msg, proc.execServicesFg 2310 ? (nextTime+SERVICE_TIMEOUT) : (nextTime + SERVICE_BACKGROUND_TIMEOUT)); 2311 } 2312 } 2313 2314 if (anrMessage != null) { 2315 mAm.appNotResponding(proc, null, null, false, anrMessage); 2316 } 2317 } 2318 2319 void scheduleServiceTimeoutLocked(ProcessRecord proc) { 2320 if (proc.executingServices.size() == 0 || proc.thread == null) { 2321 return; 2322 } 2323 long now = SystemClock.uptimeMillis(); 2324 Message msg = mAm.mHandler.obtainMessage( 2325 ActivityManagerService.SERVICE_TIMEOUT_MSG); 2326 msg.obj = proc; 2327 mAm.mHandler.sendMessageAtTime(msg, 2328 proc.execServicesFg ? (now+SERVICE_TIMEOUT) : (now+ SERVICE_BACKGROUND_TIMEOUT)); 2329 } 2330 2331 /** 2332 * Prints a list of ServiceRecords (dumpsys activity services) 2333 */ 2334 void dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args, 2335 int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) { 2336 boolean needSep = false; 2337 boolean printedAnything = false; 2338 2339 ItemMatcher matcher = new ItemMatcher(); 2340 matcher.build(args, opti); 2341 2342 pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)"); 2343 try { 2344 int[] users = mAm.getUsersLocked(); 2345 for (int user : users) { 2346 ServiceMap smap = getServiceMap(user); 2347 boolean printed = false; 2348 if (smap.mServicesByName.size() > 0) { 2349 long nowReal = SystemClock.elapsedRealtime(); 2350 needSep = false; 2351 for (int si=0; si<smap.mServicesByName.size(); si++) { 2352 ServiceRecord r = smap.mServicesByName.valueAt(si); 2353 if (!matcher.match(r, r.name)) { 2354 continue; 2355 } 2356 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 2357 continue; 2358 } 2359 if (!printed) { 2360 if (printedAnything) { 2361 pw.println(); 2362 } 2363 pw.println(" User " + user + " active services:"); 2364 printed = true; 2365 } 2366 printedAnything = true; 2367 if (needSep) { 2368 pw.println(); 2369 } 2370 pw.print(" * "); 2371 pw.println(r); 2372 if (dumpAll) { 2373 r.dump(pw, " "); 2374 needSep = true; 2375 } else { 2376 pw.print(" app="); 2377 pw.println(r.app); 2378 pw.print(" created="); 2379 TimeUtils.formatDuration(r.createTime, nowReal, pw); 2380 pw.print(" started="); 2381 pw.print(r.startRequested); 2382 pw.print(" connections="); 2383 pw.println(r.connections.size()); 2384 if (r.connections.size() > 0) { 2385 pw.println(" Connections:"); 2386 for (int conni=0; conni<r.connections.size(); conni++) { 2387 ArrayList<ConnectionRecord> clist = r.connections.valueAt(conni); 2388 for (int i = 0; i < clist.size(); i++) { 2389 ConnectionRecord conn = clist.get(i); 2390 pw.print(" "); 2391 pw.print(conn.binding.intent.intent.getIntent() 2392 .toShortString(false, false, false, false)); 2393 pw.print(" -> "); 2394 ProcessRecord proc = conn.binding.client; 2395 pw.println(proc != null ? proc.toShortString() : "null"); 2396 } 2397 } 2398 } 2399 } 2400 if (dumpClient && r.app != null && r.app.thread != null) { 2401 pw.println(" Client:"); 2402 pw.flush(); 2403 try { 2404 TransferPipe tp = new TransferPipe(); 2405 try { 2406 r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), 2407 r, args); 2408 tp.setBufferPrefix(" "); 2409 // Short timeout, since blocking here can 2410 // deadlock with the application. 2411 tp.go(fd, 2000); 2412 } finally { 2413 tp.kill(); 2414 } 2415 } catch (IOException e) { 2416 pw.println(" Failure while dumping the service: " + e); 2417 } catch (RemoteException e) { 2418 pw.println(" Got a RemoteException while dumping the service"); 2419 } 2420 needSep = true; 2421 } 2422 } 2423 needSep |= printed; 2424 } 2425 printed = false; 2426 for (int si=0, SN=smap.mDelayedStartList.size(); si<SN; si++) { 2427 ServiceRecord r = smap.mDelayedStartList.get(si); 2428 if (!matcher.match(r, r.name)) { 2429 continue; 2430 } 2431 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 2432 continue; 2433 } 2434 if (!printed) { 2435 if (printedAnything) { 2436 pw.println(); 2437 } 2438 pw.println(" User " + user + " delayed start services:"); 2439 printed = true; 2440 } 2441 printedAnything = true; 2442 pw.print(" * Delayed start "); pw.println(r); 2443 } 2444 printed = false; 2445 for (int si=0, SN=smap.mStartingBackground.size(); si<SN; si++) { 2446 ServiceRecord r = smap.mStartingBackground.get(si); 2447 if (!matcher.match(r, r.name)) { 2448 continue; 2449 } 2450 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 2451 continue; 2452 } 2453 if (!printed) { 2454 if (printedAnything) { 2455 pw.println(); 2456 } 2457 pw.println(" User " + user + " starting in background:"); 2458 printed = true; 2459 } 2460 printedAnything = true; 2461 pw.print(" * Starting bg "); pw.println(r); 2462 } 2463 } 2464 } catch (Exception e) { 2465 Slog.w(TAG, "Exception in dumpServicesLocked", e); 2466 } 2467 2468 if (mPendingServices.size() > 0) { 2469 boolean printed = false; 2470 for (int i=0; i<mPendingServices.size(); i++) { 2471 ServiceRecord r = mPendingServices.get(i); 2472 if (!matcher.match(r, r.name)) { 2473 continue; 2474 } 2475 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 2476 continue; 2477 } 2478 printedAnything = true; 2479 if (!printed) { 2480 if (needSep) pw.println(); 2481 needSep = true; 2482 pw.println(" Pending services:"); 2483 printed = true; 2484 } 2485 pw.print(" * Pending "); pw.println(r); 2486 r.dump(pw, " "); 2487 } 2488 needSep = true; 2489 } 2490 2491 if (mRestartingServices.size() > 0) { 2492 boolean printed = false; 2493 for (int i=0; i<mRestartingServices.size(); i++) { 2494 ServiceRecord r = mRestartingServices.get(i); 2495 if (!matcher.match(r, r.name)) { 2496 continue; 2497 } 2498 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 2499 continue; 2500 } 2501 printedAnything = true; 2502 if (!printed) { 2503 if (needSep) pw.println(); 2504 needSep = true; 2505 pw.println(" Restarting services:"); 2506 printed = true; 2507 } 2508 pw.print(" * Restarting "); pw.println(r); 2509 r.dump(pw, " "); 2510 } 2511 needSep = true; 2512 } 2513 2514 if (mDestroyingServices.size() > 0) { 2515 boolean printed = false; 2516 for (int i=0; i< mDestroyingServices.size(); i++) { 2517 ServiceRecord r = mDestroyingServices.get(i); 2518 if (!matcher.match(r, r.name)) { 2519 continue; 2520 } 2521 if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { 2522 continue; 2523 } 2524 printedAnything = true; 2525 if (!printed) { 2526 if (needSep) pw.println(); 2527 needSep = true; 2528 pw.println(" Destroying services:"); 2529 printed = true; 2530 } 2531 pw.print(" * Destroy "); pw.println(r); 2532 r.dump(pw, " "); 2533 } 2534 needSep = true; 2535 } 2536 2537 if (dumpAll) { 2538 boolean printed = false; 2539 for (int ic=0; ic<mServiceConnections.size(); ic++) { 2540 ArrayList<ConnectionRecord> r = mServiceConnections.valueAt(ic); 2541 for (int i=0; i<r.size(); i++) { 2542 ConnectionRecord cr = r.get(i); 2543 if (!matcher.match(cr.binding.service, cr.binding.service.name)) { 2544 continue; 2545 } 2546 if (dumpPackage != null && (cr.binding.client == null 2547 || !dumpPackage.equals(cr.binding.client.info.packageName))) { 2548 continue; 2549 } 2550 printedAnything = true; 2551 if (!printed) { 2552 if (needSep) pw.println(); 2553 needSep = true; 2554 pw.println(" Connection bindings to services:"); 2555 printed = true; 2556 } 2557 pw.print(" * "); pw.println(cr); 2558 cr.dump(pw, " "); 2559 } 2560 } 2561 } 2562 2563 if (!printedAnything) { 2564 pw.println(" (nothing)"); 2565 } 2566 } 2567 2568 /** 2569 * There are three ways to call this: 2570 * - no service specified: dump all the services 2571 * - a flattened component name that matched an existing service was specified as the 2572 * first arg: dump that one service 2573 * - the first arg isn't the flattened component name of an existing service: 2574 * dump all services whose component contains the first arg as a substring 2575 */ 2576 protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args, 2577 int opti, boolean dumpAll) { 2578 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); 2579 2580 synchronized (this) { 2581 int[] users = mAm.getUsersLocked(); 2582 if ("all".equals(name)) { 2583 for (int user : users) { 2584 ServiceMap smap = mServiceMap.get(user); 2585 if (smap == null) { 2586 continue; 2587 } 2588 ArrayMap<ComponentName, ServiceRecord> alls = smap.mServicesByName; 2589 for (int i=0; i<alls.size(); i++) { 2590 ServiceRecord r1 = alls.valueAt(i); 2591 services.add(r1); 2592 } 2593 } 2594 } else { 2595 ComponentName componentName = name != null 2596 ? ComponentName.unflattenFromString(name) : null; 2597 int objectId = 0; 2598 if (componentName == null) { 2599 // Not a '/' separated full component name; maybe an object ID? 2600 try { 2601 objectId = Integer.parseInt(name, 16); 2602 name = null; 2603 componentName = null; 2604 } catch (RuntimeException e) { 2605 } 2606 } 2607 2608 for (int user : users) { 2609 ServiceMap smap = mServiceMap.get(user); 2610 if (smap == null) { 2611 continue; 2612 } 2613 ArrayMap<ComponentName, ServiceRecord> alls = smap.mServicesByName; 2614 for (int i=0; i<alls.size(); i++) { 2615 ServiceRecord r1 = alls.valueAt(i); 2616 if (componentName != null) { 2617 if (r1.name.equals(componentName)) { 2618 services.add(r1); 2619 } 2620 } else if (name != null) { 2621 if (r1.name.flattenToString().contains(name)) { 2622 services.add(r1); 2623 } 2624 } else if (System.identityHashCode(r1) == objectId) { 2625 services.add(r1); 2626 } 2627 } 2628 } 2629 } 2630 } 2631 2632 if (services.size() <= 0) { 2633 return false; 2634 } 2635 2636 boolean needSep = false; 2637 for (int i=0; i<services.size(); i++) { 2638 if (needSep) { 2639 pw.println(); 2640 } 2641 needSep = true; 2642 dumpService("", fd, pw, services.get(i), args, dumpAll); 2643 } 2644 return true; 2645 } 2646 2647 /** 2648 * Invokes IApplicationThread.dumpService() on the thread of the specified service if 2649 * there is a thread associated with the service. 2650 */ 2651 private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw, 2652 final ServiceRecord r, String[] args, boolean dumpAll) { 2653 String innerPrefix = prefix + " "; 2654 synchronized (this) { 2655 pw.print(prefix); pw.print("SERVICE "); 2656 pw.print(r.shortName); pw.print(" "); 2657 pw.print(Integer.toHexString(System.identityHashCode(r))); 2658 pw.print(" pid="); 2659 if (r.app != null) pw.println(r.app.pid); 2660 else pw.println("(not running)"); 2661 if (dumpAll) { 2662 r.dump(pw, innerPrefix); 2663 } 2664 } 2665 if (r.app != null && r.app.thread != null) { 2666 pw.print(prefix); pw.println(" Client:"); 2667 pw.flush(); 2668 try { 2669 TransferPipe tp = new TransferPipe(); 2670 try { 2671 r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), r, args); 2672 tp.setBufferPrefix(prefix + " "); 2673 tp.go(fd); 2674 } finally { 2675 tp.kill(); 2676 } 2677 } catch (IOException e) { 2678 pw.println(prefix + " Failure while dumping the service: " + e); 2679 } catch (RemoteException e) { 2680 pw.println(prefix + " Got a RemoteException while dumping the service"); 2681 } 2682 } 2683 } 2684 2685} 2686