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