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