Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.server.am; |
| 18 | |
Bryce Lee | 290e578 | 2017-02-01 16:41:20 -0800 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 20 | import static com.android.server.am.ActivityManagerDebugConfig.*; |
| 21 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 22 | import java.io.FileDescriptor; |
| 23 | import java.io.IOException; |
| 24 | import java.io.PrintWriter; |
Dianne Hackborn | cff1bbf | 2015-01-20 13:43:32 -0800 | [diff] [blame] | 25 | import java.io.StringWriter; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 26 | import java.util.ArrayList; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 27 | import java.util.HashSet; |
| 28 | import java.util.Iterator; |
| 29 | import java.util.List; |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 30 | import java.util.Set; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 31 | |
Dianne Hackborn | 455625e | 2015-01-21 09:55:13 -0800 | [diff] [blame] | 32 | import android.app.ActivityThread; |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 33 | import android.app.AppOpsManager; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 34 | import android.app.NotificationManager; |
Dianne Hackborn | 3f7c9f2 | 2017-04-04 15:36:33 -0700 | [diff] [blame] | 35 | import android.app.ServiceStartArgs; |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 36 | import android.content.IIntentSender; |
| 37 | import android.content.IntentSender; |
Dianne Hackborn | 3f7c9f2 | 2017-04-04 15:36:33 -0700 | [diff] [blame] | 38 | import android.content.pm.ParceledListSlice; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 39 | import android.net.Uri; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 40 | import android.os.Build; |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 41 | import android.os.Bundle; |
Craig Mautner | 4a8dddbf | 2014-08-13 10:49:26 -0700 | [diff] [blame] | 42 | import android.os.DeadObjectException; |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 43 | import android.os.Handler; |
Dianne Hackborn | 13c590d | 2013-10-07 14:32:00 -0700 | [diff] [blame] | 44 | import android.os.Looper; |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 45 | import android.os.RemoteCallback; |
Dianne Hackborn | 2303741 | 2013-11-04 18:11:29 -0800 | [diff] [blame] | 46 | import android.os.SystemProperties; |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 47 | import android.os.TransactionTooLargeException; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 48 | import android.provider.Settings; |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 49 | import android.util.ArrayMap; |
Dianne Hackborn | 465fa39 | 2014-09-14 14:21:18 -0700 | [diff] [blame] | 50 | import android.util.ArraySet; |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 51 | |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 52 | import com.android.internal.R; |
Joe Onorato | 4eb64fd | 2016-03-21 15:30:09 -0700 | [diff] [blame] | 53 | import com.android.internal.app.procstats.ServiceState; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 54 | import com.android.internal.messages.nano.SystemMessageProto; |
| 55 | import com.android.internal.notification.SystemNotificationChannels; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 56 | import com.android.internal.os.BatteryStatsImpl; |
Dianne Hackborn | cbfd23e | 2013-06-11 14:26:53 -0700 | [diff] [blame] | 57 | import com.android.internal.os.TransferPipe; |
Dianne Hackborn | cff1bbf | 2015-01-20 13:43:32 -0800 | [diff] [blame] | 58 | import com.android.internal.util.FastPrintWriter; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 59 | import com.android.server.am.ActivityManagerService.ItemMatcher; |
| 60 | import com.android.server.am.ActivityManagerService.NeededUriGrants; |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 61 | import com.android.server.am.proto.ActiveServicesProto; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 62 | |
| 63 | import android.app.ActivityManager; |
| 64 | import android.app.AppGlobals; |
| 65 | import android.app.IApplicationThread; |
| 66 | import android.app.IServiceConnection; |
| 67 | import android.app.Notification; |
| 68 | import android.app.PendingIntent; |
| 69 | import android.app.Service; |
| 70 | import android.content.ComponentName; |
| 71 | import android.content.Context; |
| 72 | import android.content.Intent; |
| 73 | import android.content.pm.ApplicationInfo; |
| 74 | import android.content.pm.PackageManager; |
| 75 | import android.content.pm.ResolveInfo; |
| 76 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 77 | import android.os.Binder; |
| 78 | import android.os.IBinder; |
| 79 | import android.os.Message; |
| 80 | import android.os.Process; |
| 81 | import android.os.RemoteException; |
| 82 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 83 | import android.os.UserHandle; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 84 | import android.util.EventLog; |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 85 | import android.util.PrintWriterPrinter; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 86 | import android.util.Slog; |
| 87 | import android.util.SparseArray; |
| 88 | import android.util.TimeUtils; |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 89 | import android.util.proto.ProtoOutputStream; |
Robert Sesek | c5f8664 | 2016-11-04 10:20:38 -0400 | [diff] [blame] | 90 | import android.webkit.WebViewZygote; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 91 | |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 92 | public final class ActiveServices { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 93 | private static final String TAG = TAG_WITH_CLASS_NAME ? "ActiveServices" : TAG_AM; |
| 94 | private static final String TAG_MU = TAG + POSTFIX_MU; |
| 95 | private static final String TAG_SERVICE = TAG + POSTFIX_SERVICE; |
| 96 | private static final String TAG_SERVICE_EXECUTING = TAG + POSTFIX_SERVICE_EXECUTING; |
| 97 | |
| 98 | private static final boolean DEBUG_DELAYED_SERVICE = DEBUG_SERVICE; |
| 99 | private static final boolean DEBUG_DELAYED_STARTS = DEBUG_DELAYED_SERVICE; |
| 100 | |
| 101 | private static final boolean LOG_SERVICE_START_STOP = false; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 102 | |
| 103 | // How long we wait for a service to finish executing. |
| 104 | static final int SERVICE_TIMEOUT = 20*1000; |
| 105 | |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 106 | // How long we wait for a service to finish executing. |
| 107 | static final int SERVICE_BACKGROUND_TIMEOUT = SERVICE_TIMEOUT * 10; |
| 108 | |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 109 | // How long the startForegroundService() grace period is to get around to |
| 110 | // calling startForeground() before we ANR + stop it. |
| 111 | static final int SERVICE_START_FOREGROUND_TIMEOUT = 5*1000; |
| 112 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 113 | final ActivityManagerService mAm; |
| 114 | |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 115 | // Maximum number of services that we allow to start in the background |
| 116 | // at the same time. |
| 117 | final int mMaxStartingBackground; |
| 118 | |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 119 | final SparseArray<ServiceMap> mServiceMap = new SparseArray<>(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * All currently bound service connections. Keys are the IBinder of |
| 123 | * the client's IServiceConnection. |
| 124 | */ |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 125 | final ArrayMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections = new ArrayMap<>(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * List of services that we have been asked to start, |
| 129 | * but haven't yet been able to. It is used to hold start requests |
| 130 | * while waiting for their corresponding application thread to get |
| 131 | * going. |
| 132 | */ |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 133 | final ArrayList<ServiceRecord> mPendingServices = new ArrayList<>(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * List of services that are scheduled to restart following a crash. |
| 137 | */ |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 138 | final ArrayList<ServiceRecord> mRestartingServices = new ArrayList<>(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 141 | * List of services that are in the process of being destroyed. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 142 | */ |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 143 | final ArrayList<ServiceRecord> mDestroyingServices = new ArrayList<>(); |
| 144 | |
| 145 | /** Temporary list for holding the results of calls to {@link #collectPackageServicesLocked} */ |
| 146 | private ArrayList<ServiceRecord> mTmpCollectionResults = null; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 147 | |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 148 | /** |
| 149 | * For keeping ActiveForegroundApps retaining state while the screen is off. |
| 150 | */ |
| 151 | boolean mScreenOn = true; |
| 152 | |
Dianne Hackborn | cff1bbf | 2015-01-20 13:43:32 -0800 | [diff] [blame] | 153 | /** Amount of time to allow a last ANR message to exist before freeing the memory. */ |
| 154 | static final int LAST_ANR_LIFETIME_DURATION_MSECS = 2 * 60 * 60 * 1000; // Two hours |
| 155 | |
| 156 | String mLastAnrDump; |
| 157 | |
| 158 | final Runnable mLastAnrDumpClearer = new Runnable() { |
| 159 | @Override public void run() { |
| 160 | synchronized (mAm) { |
| 161 | mLastAnrDump = null; |
| 162 | } |
| 163 | } |
| 164 | }; |
| 165 | |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 166 | /** |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 167 | * Information about an app that is currently running one or more foreground services. |
Dan Sandler | 2f36ab8 | 2017-05-25 00:15:49 -0400 | [diff] [blame] | 168 | * (This maps directly to the running apps we show in the notification.) |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 169 | */ |
| 170 | static final class ActiveForegroundApp { |
| 171 | String mPackageName; |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 172 | int mUid; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 173 | CharSequence mLabel; |
| 174 | boolean mShownWhileScreenOn; |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 175 | boolean mAppOnTop; |
| 176 | boolean mShownWhileTop; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 177 | long mStartTime; |
| 178 | long mStartVisibleTime; |
| 179 | long mEndTime; |
| 180 | int mNumActive; |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 181 | |
| 182 | // Temp output of foregroundAppShownEnoughLocked |
| 183 | long mHideTime; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 187 | * Information about services for a single user. |
| 188 | */ |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 189 | final class ServiceMap extends Handler { |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 190 | final int mUserId; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 191 | final ArrayMap<ComponentName, ServiceRecord> mServicesByName = new ArrayMap<>(); |
| 192 | final ArrayMap<Intent.FilterComparison, ServiceRecord> mServicesByIntent = new ArrayMap<>(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 193 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 194 | final ArrayList<ServiceRecord> mDelayedStartList = new ArrayList<>(); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 195 | /* XXX eventually I'd like to have this based on processes instead of services. |
| 196 | * That is, if we try to start two services in a row both running in the same |
| 197 | * process, this should be one entry in mStartingBackground for that one process |
| 198 | * that remains until all services in it are done. |
| 199 | final ArrayMap<ProcessRecord, DelayingProcess> mStartingBackgroundMap |
| 200 | = new ArrayMap<ProcessRecord, DelayingProcess>(); |
| 201 | final ArrayList<DelayingProcess> mStartingProcessList |
| 202 | = new ArrayList<DelayingProcess>(); |
| 203 | */ |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 204 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 205 | final ArrayList<ServiceRecord> mStartingBackground = new ArrayList<>(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 206 | |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 207 | final ArrayMap<String, ActiveForegroundApp> mActiveForegroundApps = new ArrayMap<>(); |
| 208 | boolean mActiveForegroundAppsChanged; |
| 209 | |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 210 | static final int MSG_BG_START_TIMEOUT = 1; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 211 | static final int MSG_UPDATE_FOREGROUND_APPS = 2; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 212 | |
Dianne Hackborn | 13c590d | 2013-10-07 14:32:00 -0700 | [diff] [blame] | 213 | ServiceMap(Looper looper, int userId) { |
| 214 | super(looper); |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 215 | mUserId = userId; |
| 216 | } |
| 217 | |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 218 | @Override |
| 219 | public void handleMessage(Message msg) { |
| 220 | switch (msg.what) { |
| 221 | case MSG_BG_START_TIMEOUT: { |
| 222 | synchronized (mAm) { |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 223 | rescheduleDelayedStartsLocked(); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 224 | } |
| 225 | } break; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 226 | case MSG_UPDATE_FOREGROUND_APPS: { |
| 227 | updateForegroundApps(this); |
| 228 | } break; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 229 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 232 | void ensureNotStartingBackgroundLocked(ServiceRecord r) { |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 233 | if (mStartingBackground.remove(r)) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 234 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, |
| 235 | "No longer background starting: " + r); |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 236 | rescheduleDelayedStartsLocked(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 237 | } |
Dianne Hackborn | 2e46bb5 | 2013-09-13 17:01:26 -0700 | [diff] [blame] | 238 | if (mDelayedStartList.remove(r)) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 239 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "No longer delaying start: " + r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 243 | void rescheduleDelayedStartsLocked() { |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 244 | removeMessages(MSG_BG_START_TIMEOUT); |
| 245 | final long now = SystemClock.uptimeMillis(); |
| 246 | for (int i=0, N=mStartingBackground.size(); i<N; i++) { |
| 247 | ServiceRecord r = mStartingBackground.get(i); |
| 248 | if (r.startingBgTimeout <= now) { |
| 249 | Slog.i(TAG, "Waited long enough for: " + r); |
| 250 | mStartingBackground.remove(i); |
| 251 | N--; |
Junu Kim | fcb8736 | 2014-02-19 16:25:21 +0900 | [diff] [blame] | 252 | i--; |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | while (mDelayedStartList.size() > 0 |
| 256 | && mStartingBackground.size() < mMaxStartingBackground) { |
| 257 | ServiceRecord r = mDelayedStartList.remove(0); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 258 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, |
| 259 | "REM FR DELAY LIST (exec next): " + r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 260 | if (r.pendingStarts.size() <= 0) { |
| 261 | Slog.w(TAG, "**** NO PENDING STARTS! " + r + " startReq=" + r.startRequested |
| 262 | + " delayedStop=" + r.delayedStop); |
| 263 | } |
| 264 | if (DEBUG_DELAYED_SERVICE) { |
| 265 | if (mDelayedStartList.size() > 0) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 266 | Slog.v(TAG_SERVICE, "Remaining delayed list:"); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 267 | for (int i=0; i<mDelayedStartList.size(); i++) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 268 | Slog.v(TAG_SERVICE, " #" + i + ": " + mDelayedStartList.get(i)); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | r.delayed = false; |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 273 | try { |
| 274 | startServiceInnerLocked(this, r.pendingStarts.get(0).intent, r, false, true); |
| 275 | } catch (TransactionTooLargeException e) { |
| 276 | // Ignore, nobody upstack cares. |
| 277 | } |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 278 | } |
| 279 | if (mStartingBackground.size() > 0) { |
| 280 | ServiceRecord next = mStartingBackground.get(0); |
| 281 | long when = next.startingBgTimeout > now ? next.startingBgTimeout : now; |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 282 | if (DEBUG_DELAYED_SERVICE) Slog.v(TAG_SERVICE, "Top bg start is " + next |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 283 | + ", can delay others up to " + when); |
| 284 | Message msg = obtainMessage(MSG_BG_START_TIMEOUT); |
| 285 | sendMessageAtTime(msg, when); |
| 286 | } |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 287 | if (mStartingBackground.size() < mMaxStartingBackground) { |
| 288 | mAm.backgroundServicesFinishedLocked(mUserId); |
| 289 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | public ActiveServices(ActivityManagerService service) { |
| 294 | mAm = service; |
Dianne Hackborn | 2303741 | 2013-11-04 18:11:29 -0800 | [diff] [blame] | 295 | int maxBg = 0; |
| 296 | try { |
| 297 | maxBg = Integer.parseInt(SystemProperties.get("ro.config.max_starting_bg", "0")); |
| 298 | } catch(RuntimeException e) { |
| 299 | } |
Dianne Hackborn | 20d9474 | 2014-05-29 18:35:45 -0700 | [diff] [blame] | 300 | mMaxStartingBackground = maxBg > 0 |
| 301 | ? maxBg : ActivityManager.isLowRamDeviceStatic() ? 1 : 8; |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 304 | ServiceRecord getServiceByNameLocked(ComponentName name, int callingUser) { |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 305 | // TODO: Deal with global services |
| 306 | if (DEBUG_MU) |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 307 | Slog.v(TAG_MU, "getServiceByNameLocked(" + name + "), callingUser = " + callingUser); |
| 308 | return getServiceMapLocked(callingUser).mServicesByName.get(name); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 311 | boolean hasBackgroundServicesLocked(int callingUser) { |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 312 | ServiceMap smap = mServiceMap.get(callingUser); |
| 313 | return smap != null ? smap.mStartingBackground.size() >= mMaxStartingBackground : false; |
| 314 | } |
| 315 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 316 | private ServiceMap getServiceMapLocked(int callingUser) { |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 317 | ServiceMap smap = mServiceMap.get(callingUser); |
| 318 | if (smap == null) { |
Dianne Hackborn | 13c590d | 2013-10-07 14:32:00 -0700 | [diff] [blame] | 319 | smap = new ServiceMap(mAm.mHandler.getLooper(), callingUser); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 320 | mServiceMap.put(callingUser, smap); |
| 321 | } |
| 322 | return smap; |
| 323 | } |
| 324 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 325 | ArrayMap<ComponentName, ServiceRecord> getServicesLocked(int callingUser) { |
| 326 | return getServiceMapLocked(callingUser).mServicesByName; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 329 | ComponentName startServiceLocked(IApplicationThread caller, Intent service, String resolvedType, |
Christopher Tate | 242ba3e9 | 2017-04-14 15:07:06 -0700 | [diff] [blame] | 330 | int callingPid, int callingUid, boolean fgRequired, String callingPackage, final int userId) |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 331 | throws TransactionTooLargeException { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 332 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "startService: " + service |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 333 | + " type=" + resolvedType + " args=" + service.getExtras()); |
| 334 | |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 335 | final boolean callerFg; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 336 | if (caller != null) { |
| 337 | final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); |
| 338 | if (callerApp == null) { |
| 339 | throw new SecurityException( |
| 340 | "Unable to find app for caller " + caller |
Dianne Hackborn | c75587e | 2017-04-11 15:37:03 -0700 | [diff] [blame] | 341 | + " (pid=" + callingPid |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 342 | + ") when starting service " + service); |
| 343 | } |
Dianne Hackborn | a49ad09 | 2016-03-03 13:39:10 -0800 | [diff] [blame] | 344 | callerFg = callerApp.setSchedGroup != ProcessList.SCHED_GROUP_BACKGROUND; |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 345 | } else { |
| 346 | callerFg = true; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | ServiceLookupResult res = |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 350 | retrieveServiceLocked(service, resolvedType, callingPackage, |
Svet Ganov | d223db3 | 2017-12-22 09:43:48 -0800 | [diff] [blame] | 351 | callingPid, callingUid, userId, true, callerFg, false, false); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 352 | if (res == null) { |
| 353 | return null; |
| 354 | } |
| 355 | if (res.record == null) { |
| 356 | return new ComponentName("!", res.permission != null |
| 357 | ? res.permission : "private to package"); |
| 358 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 359 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 360 | ServiceRecord r = res.record; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 361 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 362 | if (!mAm.mUserController.exists(r.userId)) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 363 | Slog.w(TAG, "Trying to start service with non-existent user! " + r.userId); |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 364 | return null; |
| 365 | } |
| 366 | |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 367 | // If this isn't a direct-to-foreground start, check our ability to kick off an |
| 368 | // arbitrary service |
| 369 | if (!r.startRequested && !fgRequired) { |
Dianne Hackborn | c75587e | 2017-04-11 15:37:03 -0700 | [diff] [blame] | 370 | // Before going further -- if this app is not allowed to start services in the |
| 371 | // background, then at this point we aren't going to let it period. |
| 372 | final int allowed = mAm.getAppStartModeLocked(r.appInfo.uid, r.packageName, |
| 373 | r.appInfo.targetSdkVersion, callingPid, false, false); |
| 374 | if (allowed != ActivityManager.APP_START_MODE_NORMAL) { |
| 375 | Slog.w(TAG, "Background start not allowed: service " |
| 376 | + service + " to " + r.name.flattenToShortString() |
| 377 | + " from pid=" + callingPid + " uid=" + callingUid |
| 378 | + " pkg=" + callingPackage); |
| 379 | if (allowed == ActivityManager.APP_START_MODE_DELAYED) { |
| 380 | // In this case we are silently disabling the app, to disrupt as |
| 381 | // little as possible existing apps. |
| 382 | return null; |
Dianne Hackborn | 4fb9c4a | 2016-04-04 13:31:18 -0700 | [diff] [blame] | 383 | } |
Dianne Hackborn | c75587e | 2017-04-11 15:37:03 -0700 | [diff] [blame] | 384 | // This app knows it is in the new model where this operation is not |
| 385 | // allowed, so tell it what has happened. |
| 386 | UidRecord uidRec = mAm.mActiveUids.get(r.appInfo.uid); |
| 387 | return new ComponentName("?", "app is in background uid " + uidRec); |
Dianne Hackborn | 4fb9c4a | 2016-04-04 13:31:18 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 391 | NeededUriGrants neededGrants = mAm.checkGrantUriPermissionFromIntentLocked( |
Nicolas Prevot | c6cf95c | 2014-05-29 11:30:36 +0100 | [diff] [blame] | 392 | callingUid, r.packageName, service, service.getFlags(), null, r.userId); |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 393 | |
| 394 | // If permissions need a review before any of the app components can run, |
| 395 | // we do not start the service and launch a review activity if the calling app |
| 396 | // is in the foreground passing it a pending intent to start the service when |
| 397 | // review is completed. |
Svet Ganov | 77df6f3 | 2016-08-17 11:46:34 -0700 | [diff] [blame] | 398 | if (mAm.mPermissionReviewRequired) { |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 399 | if (!requestStartTargetPermissionsReviewIfNeededLocked(r, callingPackage, |
| 400 | callingUid, service, callerFg, userId)) { |
| 401 | return null; |
| 402 | } |
| 403 | } |
| 404 | |
Dianne Hackborn | d6f5b62 | 2013-11-11 17:25:37 -0800 | [diff] [blame] | 405 | if (unscheduleServiceRestartLocked(r, callingUid, false)) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 406 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "START SERVICE WHILE RESTART PENDING: " + r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 407 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 408 | r.lastActivity = SystemClock.uptimeMillis(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 409 | r.startRequested = true; |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 410 | r.delayedStop = false; |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 411 | r.fgRequired = fgRequired; |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 412 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
Todd Kennedy | 51b3aac | 2017-03-30 17:50:42 -0700 | [diff] [blame] | 413 | service, neededGrants, callingUid)); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 414 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 415 | final ServiceMap smap = getServiceMapLocked(r.userId); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 416 | boolean addToStarting = false; |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 417 | if (!callerFg && !fgRequired && r.app == null |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 418 | && mAm.mUserController.hasStartedUserState(r.userId)) { |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 419 | ProcessRecord proc = mAm.getProcessRecordLocked(r.processName, r.appInfo.uid, false); |
Dianne Hackborn | 0d97cd1 | 2013-09-16 19:02:52 -0700 | [diff] [blame] | 420 | if (proc == null || proc.curProcState > ActivityManager.PROCESS_STATE_RECEIVER) { |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 421 | // If this is not coming from a foreground caller, then we may want |
| 422 | // to delay the start if there are already other background services |
| 423 | // that are starting. This is to avoid process start spam when lots |
| 424 | // of applications are all handling things like connectivity broadcasts. |
Dianne Hackborn | 0d97cd1 | 2013-09-16 19:02:52 -0700 | [diff] [blame] | 425 | // We only do this for cached processes, because otherwise an application |
| 426 | // can have assumptions about calling startService() for a service to run |
| 427 | // in its own process, and for that process to not be killed before the |
| 428 | // service is started. This is especially the case for receivers, which |
| 429 | // may start a service in onReceive() to do some additional work and have |
| 430 | // initialized some global state as part of that. |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 431 | if (DEBUG_DELAYED_SERVICE) Slog.v(TAG_SERVICE, "Potential start delay of " |
| 432 | + r + " in " + proc); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 433 | if (r.delayed) { |
| 434 | // This service is already scheduled for a delayed start; just leave |
| 435 | // it still waiting. |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 436 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "Continuing to delay: " + r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 437 | return r.name; |
| 438 | } |
| 439 | if (smap.mStartingBackground.size() >= mMaxStartingBackground) { |
| 440 | // Something else is starting, delay! |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 441 | Slog.i(TAG_SERVICE, "Delaying start of: " + r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 442 | smap.mDelayedStartList.add(r); |
| 443 | r.delayed = true; |
| 444 | return r.name; |
| 445 | } |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 446 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "Not delaying: " + r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 447 | addToStarting = true; |
| 448 | } else if (proc.curProcState >= ActivityManager.PROCESS_STATE_SERVICE) { |
| 449 | // We slightly loosen when we will enqueue this new service as a background |
| 450 | // starting service we are waiting for, to also include processes that are |
Dianne Hackborn | 0d97cd1 | 2013-09-16 19:02:52 -0700 | [diff] [blame] | 451 | // currently running other services or receivers. |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 452 | addToStarting = true; |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 453 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, |
| 454 | "Not delaying, but counting as bg: " + r); |
Dianne Hackborn | aa9875e | 2013-12-09 11:26:11 -0800 | [diff] [blame] | 455 | } else if (DEBUG_DELAYED_STARTS) { |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 456 | StringBuilder sb = new StringBuilder(128); |
| 457 | sb.append("Not potential delay (state=").append(proc.curProcState) |
| 458 | .append(' ').append(proc.adjType); |
| 459 | String reason = proc.makeAdjReason(); |
| 460 | if (reason != null) { |
| 461 | sb.append(' '); |
| 462 | sb.append(reason); |
| 463 | } |
| 464 | sb.append("): "); |
| 465 | sb.append(r.toString()); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 466 | Slog.v(TAG_SERVICE, sb.toString()); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 467 | } |
Dianne Hackborn | aa9875e | 2013-12-09 11:26:11 -0800 | [diff] [blame] | 468 | } else if (DEBUG_DELAYED_STARTS) { |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 469 | if (callerFg || fgRequired) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 470 | Slog.v(TAG_SERVICE, "Not potential delay (callerFg=" + callerFg + " uid=" |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 471 | + callingUid + " pid=" + callingPid + " fgRequired=" + fgRequired + "): " + r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 472 | } else if (r.app != null) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 473 | Slog.v(TAG_SERVICE, "Not potential delay (cur app=" + r.app + "): " + r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 474 | } else { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 475 | Slog.v(TAG_SERVICE, |
| 476 | "Not potential delay (user " + r.userId + " not started): " + r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 480 | ComponentName cmp = startServiceInnerLocked(smap, service, r, callerFg, addToStarting); |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 481 | return cmp; |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 484 | private boolean requestStartTargetPermissionsReviewIfNeededLocked(ServiceRecord r, |
| 485 | String callingPackage, int callingUid, Intent service, boolean callerFg, |
| 486 | final int userId) { |
| 487 | if (mAm.getPackageManagerInternalLocked().isPermissionsReviewRequired( |
| 488 | r.packageName, r.userId)) { |
| 489 | |
| 490 | // Show a permission review UI only for starting from a foreground app |
| 491 | if (!callerFg) { |
| 492 | Slog.w(TAG, "u" + r.userId + " Starting a service in package" |
| 493 | + r.packageName + " requires a permissions review"); |
| 494 | return false; |
| 495 | } |
| 496 | |
| 497 | IIntentSender target = mAm.getIntentSenderLocked( |
| 498 | ActivityManager.INTENT_SENDER_SERVICE, callingPackage, |
| 499 | callingUid, userId, null, null, 0, new Intent[]{service}, |
| 500 | new String[]{service.resolveType(mAm.mContext.getContentResolver())}, |
| 501 | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT |
| 502 | | PendingIntent.FLAG_IMMUTABLE, null); |
| 503 | |
| 504 | final Intent intent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS); |
| 505 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 506 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 507 | intent.putExtra(Intent.EXTRA_PACKAGE_NAME, r.packageName); |
| 508 | intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target)); |
| 509 | |
| 510 | if (DEBUG_PERMISSIONS_REVIEW) { |
| 511 | Slog.i(TAG, "u" + r.userId + " Launching permission review for package " |
| 512 | + r.packageName); |
| 513 | } |
| 514 | |
| 515 | mAm.mHandler.post(new Runnable() { |
| 516 | @Override |
| 517 | public void run() { |
| 518 | mAm.mContext.startActivityAsUser(intent, new UserHandle(userId)); |
| 519 | } |
| 520 | }); |
| 521 | |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | return true; |
| 526 | } |
| 527 | |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 528 | ComponentName startServiceInnerLocked(ServiceMap smap, Intent service, ServiceRecord r, |
| 529 | boolean callerFg, boolean addToStarting) throws TransactionTooLargeException { |
Joe Onorato | 4eb64fd | 2016-03-21 15:30:09 -0700 | [diff] [blame] | 530 | ServiceState stracker = r.getTracker(); |
Dianne Hackborn | bd754f4 | 2013-07-23 15:52:36 -0700 | [diff] [blame] | 531 | if (stracker != null) { |
Dianne Hackborn | d293224 | 2013-08-05 18:18:42 -0700 | [diff] [blame] | 532 | stracker.setStarted(true, mAm.mProcessStats.getMemFactorLocked(), r.lastActivity); |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 533 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 534 | r.callStart = false; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 535 | synchronized (r.stats.getBatteryStats()) { |
| 536 | r.stats.startRunningLocked(); |
| 537 | } |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 538 | String error = bringUpServiceLocked(r, service.getFlags(), callerFg, false, false); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 539 | if (error != null) { |
| 540 | return new ComponentName("!!", error); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 541 | } |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 542 | |
| 543 | if (r.startRequested && addToStarting) { |
| 544 | boolean first = smap.mStartingBackground.size() == 0; |
| 545 | smap.mStartingBackground.add(r); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 546 | r.startingBgTimeout = SystemClock.uptimeMillis() + mAm.mConstants.BG_START_TIMEOUT; |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 547 | if (DEBUG_DELAYED_SERVICE) { |
| 548 | RuntimeException here = new RuntimeException("here"); |
| 549 | here.fillInStackTrace(); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 550 | Slog.v(TAG_SERVICE, "Starting background (first=" + first + "): " + r, here); |
Dianne Hackborn | aa9875e | 2013-12-09 11:26:11 -0800 | [diff] [blame] | 551 | } else if (DEBUG_DELAYED_STARTS) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 552 | Slog.v(TAG_SERVICE, "Starting background (first=" + first + "): " + r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 553 | } |
| 554 | if (first) { |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 555 | smap.rescheduleDelayedStartsLocked(); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 556 | } |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 557 | } else if (callerFg || r.fgRequired) { |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 558 | smap.ensureNotStartingBackgroundLocked(r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 561 | return r.name; |
| 562 | } |
| 563 | |
| 564 | private void stopServiceLocked(ServiceRecord service) { |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 565 | if (service.delayed) { |
| 566 | // If service isn't actually running, but is is being held in the |
| 567 | // delayed list, then we need to keep it started but note that it |
| 568 | // should be stopped once no longer delayed. |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 569 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "Delaying stop of pending: " + service); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 570 | service.delayedStop = true; |
| 571 | return; |
| 572 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 573 | synchronized (service.stats.getBatteryStats()) { |
| 574 | service.stats.stopRunningLocked(); |
| 575 | } |
| 576 | service.startRequested = false; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 577 | if (service.tracker != null) { |
Dianne Hackborn | d293224 | 2013-08-05 18:18:42 -0700 | [diff] [blame] | 578 | service.tracker.setStarted(false, mAm.mProcessStats.getMemFactorLocked(), |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 579 | SystemClock.uptimeMillis()); |
| 580 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 581 | service.callStart = false; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 582 | bringDownServiceIfNeededLocked(service, false, false); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | int stopServiceLocked(IApplicationThread caller, Intent service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 586 | String resolvedType, int userId) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 587 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "stopService: " + service |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 588 | + " type=" + resolvedType); |
| 589 | |
| 590 | final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); |
| 591 | if (caller != null && callerApp == null) { |
| 592 | throw new SecurityException( |
| 593 | "Unable to find app for caller " + caller |
| 594 | + " (pid=" + Binder.getCallingPid() |
| 595 | + ") when stopping service " + service); |
| 596 | } |
| 597 | |
| 598 | // If this service is active, make sure it is stopped. |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 599 | ServiceLookupResult r = retrieveServiceLocked(service, resolvedType, null, |
Svet Ganov | d223db3 | 2017-12-22 09:43:48 -0800 | [diff] [blame] | 600 | Binder.getCallingPid(), Binder.getCallingUid(), userId, false, false, false, false); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 601 | if (r != null) { |
| 602 | if (r.record != null) { |
| 603 | final long origId = Binder.clearCallingIdentity(); |
| 604 | try { |
| 605 | stopServiceLocked(r.record); |
| 606 | } finally { |
| 607 | Binder.restoreCallingIdentity(origId); |
| 608 | } |
| 609 | return 1; |
| 610 | } |
| 611 | return -1; |
| 612 | } |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 617 | void stopInBackgroundLocked(int uid) { |
| 618 | // Stop all services associated with this uid due to it going to the background |
| 619 | // stopped state. |
| 620 | ServiceMap services = mServiceMap.get(UserHandle.getUserId(uid)); |
| 621 | ArrayList<ServiceRecord> stopping = null; |
| 622 | if (services != null) { |
| 623 | for (int i=services.mServicesByName.size()-1; i>=0; i--) { |
| 624 | ServiceRecord service = services.mServicesByName.valueAt(i); |
| 625 | if (service.appInfo.uid == uid && service.startRequested) { |
Dianne Hackborn | c3af19a | 2017-01-20 17:00:44 -0800 | [diff] [blame] | 626 | if (mAm.getAppStartModeLocked(service.appInfo.uid, service.packageName, |
| 627 | service.appInfo.targetSdkVersion, -1, false, false) |
| 628 | != ActivityManager.APP_START_MODE_NORMAL) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 629 | if (stopping == null) { |
| 630 | stopping = new ArrayList<>(); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 631 | } |
Dianne Hackborn | a33f1a2 | 2017-06-15 14:33:16 -0700 | [diff] [blame] | 632 | String compName = service.name.flattenToShortString(); |
| 633 | EventLogTags.writeAmStopIdleService(service.appInfo.uid, compName); |
| 634 | StringBuilder sb = new StringBuilder(64); |
| 635 | sb.append("Stopping service due to app idle: "); |
| 636 | UserHandle.formatUid(sb, service.appInfo.uid); |
| 637 | sb.append(" "); |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 638 | TimeUtils.formatDuration(service.createRealTime |
Dianne Hackborn | a33f1a2 | 2017-06-15 14:33:16 -0700 | [diff] [blame] | 639 | - SystemClock.elapsedRealtime(), sb); |
| 640 | sb.append(" "); |
| 641 | sb.append(compName); |
| 642 | Slog.w(TAG, sb.toString()); |
| 643 | stopping.add(service); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
| 647 | if (stopping != null) { |
| 648 | for (int i=stopping.size()-1; i>=0; i--) { |
| 649 | ServiceRecord service = stopping.get(i); |
| 650 | service.delayed = false; |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 651 | services.ensureNotStartingBackgroundLocked(service); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 652 | stopServiceLocked(service); |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 658 | IBinder peekServiceLocked(Intent service, String resolvedType, String callingPackage) { |
| 659 | ServiceLookupResult r = retrieveServiceLocked(service, resolvedType, callingPackage, |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 660 | Binder.getCallingPid(), Binder.getCallingUid(), |
Svet Ganov | d223db3 | 2017-12-22 09:43:48 -0800 | [diff] [blame] | 661 | UserHandle.getCallingUserId(), false, false, false, false); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 662 | |
| 663 | IBinder ret = null; |
| 664 | if (r != null) { |
| 665 | // r.record is null if findServiceLocked() failed the caller permission check |
| 666 | if (r.record == null) { |
| 667 | throw new SecurityException( |
Christopher Desjardins | 5862c5f | 2015-05-19 11:25:40 +0000 | [diff] [blame] | 668 | "Permission Denial: Accessing service" |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 669 | + " from pid=" + Binder.getCallingPid() |
| 670 | + ", uid=" + Binder.getCallingUid() |
| 671 | + " requires " + r.permission); |
| 672 | } |
| 673 | IntentBindRecord ib = r.record.bindings.get(r.record.intent); |
| 674 | if (ib != null) { |
| 675 | ret = ib.binder; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | boolean stopServiceTokenLocked(ComponentName className, IBinder token, |
| 683 | int startId) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 684 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "stopServiceToken: " + className |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 685 | + " " + token + " startId=" + startId); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 686 | ServiceRecord r = findServiceLocked(className, token, UserHandle.getCallingUserId()); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 687 | if (r != null) { |
| 688 | if (startId >= 0) { |
| 689 | // Asked to only stop if done with all work. Note that |
| 690 | // to avoid leaks, we will take this as dropping all |
| 691 | // start items up to and including this one. |
| 692 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 693 | if (si != null) { |
| 694 | while (r.deliveredStarts.size() > 0) { |
| 695 | ServiceRecord.StartItem cur = r.deliveredStarts.remove(0); |
| 696 | cur.removeUriPermissionsLocked(); |
| 697 | if (cur == si) { |
| 698 | break; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | if (r.getLastStartId() != startId) { |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | if (r.deliveredStarts.size() > 0) { |
| 708 | Slog.w(TAG, "stopServiceToken startId " + startId |
| 709 | + " is last, but have " + r.deliveredStarts.size() |
| 710 | + " remaining args"); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | synchronized (r.stats.getBatteryStats()) { |
| 715 | r.stats.stopRunningLocked(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 716 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 717 | r.startRequested = false; |
| 718 | if (r.tracker != null) { |
Dianne Hackborn | d293224 | 2013-08-05 18:18:42 -0700 | [diff] [blame] | 719 | r.tracker.setStarted(false, mAm.mProcessStats.getMemFactorLocked(), |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 720 | SystemClock.uptimeMillis()); |
| 721 | } |
| 722 | r.callStart = false; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 723 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 724 | bringDownServiceIfNeededLocked(r, false, false); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 725 | Binder.restoreCallingIdentity(origId); |
| 726 | return true; |
| 727 | } |
| 728 | return false; |
| 729 | } |
| 730 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 731 | public void setServiceForegroundLocked(ComponentName className, IBinder token, |
Dianne Hackborn | 67324c9 | 2016-04-18 13:55:25 -0700 | [diff] [blame] | 732 | int id, Notification notification, int flags) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 733 | final int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 734 | final long origId = Binder.clearCallingIdentity(); |
| 735 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 736 | ServiceRecord r = findServiceLocked(className, token, userId); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 737 | if (r != null) { |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 738 | setServiceForegroundInnerLocked(r, id, notification, flags); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 739 | } |
| 740 | } finally { |
| 741 | Binder.restoreCallingIdentity(origId); |
| 742 | } |
| 743 | } |
| 744 | |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 745 | boolean foregroundAppShownEnoughLocked(ActiveForegroundApp aa, long nowElapsed) { |
| 746 | if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "Shown enough: pkg=" + aa.mPackageName + ", uid=" |
| 747 | + aa.mUid); |
| 748 | boolean canRemove = false; |
| 749 | aa.mHideTime = Long.MAX_VALUE; |
| 750 | if (aa.mShownWhileTop) { |
| 751 | // If the app was ever at the top of the screen while the foreground |
| 752 | // service was running, then we can always just immediately remove it. |
| 753 | canRemove = true; |
| 754 | if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "YES - shown while on top"); |
| 755 | } else if (mScreenOn || aa.mShownWhileScreenOn) { |
| 756 | final long minTime = aa.mStartVisibleTime |
| 757 | + (aa.mStartTime != aa.mStartVisibleTime |
| 758 | ? mAm.mConstants.FGSERVICE_SCREEN_ON_AFTER_TIME |
| 759 | : mAm.mConstants.FGSERVICE_MIN_SHOWN_TIME); |
| 760 | if (nowElapsed >= minTime) { |
| 761 | // If shown while the screen is on, and it has been shown for |
| 762 | // at least the minimum show time, then we can now remove it. |
| 763 | if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "YES - shown long enough with screen on"); |
| 764 | canRemove = true; |
| 765 | } else { |
| 766 | // This is when we will be okay to stop telling the user. |
| 767 | long reportTime = nowElapsed + mAm.mConstants.FGSERVICE_MIN_REPORT_TIME; |
| 768 | aa.mHideTime = reportTime > minTime ? reportTime : minTime; |
| 769 | if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "NO -- wait " + (aa.mHideTime-nowElapsed) |
| 770 | + " with screen on"); |
| 771 | } |
| 772 | } else { |
| 773 | final long minTime = aa.mEndTime |
| 774 | + mAm.mConstants.FGSERVICE_SCREEN_ON_BEFORE_TIME; |
| 775 | if (nowElapsed >= minTime) { |
| 776 | // If the foreground service has only run while the screen is |
| 777 | // off, but it has been gone now for long enough that we won't |
| 778 | // care to tell the user about it when the screen comes back on, |
| 779 | // then we can remove it now. |
| 780 | if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "YES - gone long enough with screen off"); |
| 781 | canRemove = true; |
| 782 | } else { |
| 783 | // This is when we won't care about this old fg service. |
| 784 | aa.mHideTime = minTime; |
| 785 | if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "NO -- wait " + (aa.mHideTime-nowElapsed) |
| 786 | + " with screen off"); |
| 787 | } |
| 788 | } |
| 789 | return canRemove; |
| 790 | } |
| 791 | |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 792 | void updateForegroundApps(ServiceMap smap) { |
| 793 | // This is called from the handler without the lock held. |
| 794 | ArrayList<ActiveForegroundApp> active = null; |
| 795 | synchronized (mAm) { |
| 796 | final long now = SystemClock.elapsedRealtime(); |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 797 | long nextUpdateTime = Long.MAX_VALUE; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 798 | if (smap != null) { |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 799 | if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "Updating foreground apps for user " |
| 800 | + smap.mUserId); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 801 | for (int i = smap.mActiveForegroundApps.size()-1; i >= 0; i--) { |
| 802 | ActiveForegroundApp aa = smap.mActiveForegroundApps.valueAt(i); |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 803 | if (aa.mEndTime != 0) { |
| 804 | boolean canRemove = foregroundAppShownEnoughLocked(aa, now); |
| 805 | if (canRemove) { |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 806 | // This was up for longer than the timeout, so just remove immediately. |
| 807 | smap.mActiveForegroundApps.removeAt(i); |
| 808 | smap.mActiveForegroundAppsChanged = true; |
| 809 | continue; |
| 810 | } |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 811 | if (aa.mHideTime < nextUpdateTime) { |
| 812 | nextUpdateTime = aa.mHideTime; |
| 813 | } |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 814 | } |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 815 | if (!aa.mAppOnTop) { |
| 816 | if (active == null) { |
| 817 | active = new ArrayList<>(); |
| 818 | } |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 819 | if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "Adding active: pkg=" |
| 820 | + aa.mPackageName + ", uid=" + aa.mUid); |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 821 | active.add(aa); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 822 | } |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 823 | } |
| 824 | smap.removeMessages(ServiceMap.MSG_UPDATE_FOREGROUND_APPS); |
| 825 | if (nextUpdateTime < Long.MAX_VALUE) { |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 826 | if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "Next update time in: " |
| 827 | + (nextUpdateTime-now)); |
| 828 | Message msg = smap.obtainMessage(ServiceMap.MSG_UPDATE_FOREGROUND_APPS); |
Dianne Hackborn | fb5d4b5 | 2017-05-16 17:03:44 -0700 | [diff] [blame] | 829 | smap.sendMessageAtTime(msg, nextUpdateTime |
| 830 | + SystemClock.uptimeMillis() - SystemClock.elapsedRealtime()); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | if (!smap.mActiveForegroundAppsChanged) { |
| 834 | return; |
| 835 | } |
| 836 | smap.mActiveForegroundAppsChanged = false; |
| 837 | } |
| 838 | |
| 839 | final NotificationManager nm = (NotificationManager) mAm.mContext.getSystemService( |
| 840 | Context.NOTIFICATION_SERVICE); |
| 841 | final Context context = mAm.mContext; |
| 842 | |
| 843 | if (active != null) { |
| 844 | for (int i = 0; i < active.size(); i++) { |
| 845 | ActiveForegroundApp aa = active.get(i); |
| 846 | if (aa.mLabel == null) { |
| 847 | PackageManager pm = context.getPackageManager(); |
| 848 | try { |
| 849 | ApplicationInfo ai = pm.getApplicationInfoAsUser(aa.mPackageName, |
| 850 | PackageManager.MATCH_KNOWN_PACKAGES, smap.mUserId); |
| 851 | aa.mLabel = ai.loadLabel(pm); |
| 852 | } catch (PackageManager.NameNotFoundException e) { |
| 853 | aa.mLabel = aa.mPackageName; |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | Intent intent; |
| 859 | String title; |
| 860 | String msg; |
Dianne Hackborn | fb5d4b5 | 2017-05-16 17:03:44 -0700 | [diff] [blame] | 861 | String[] pkgs; |
Dan Sandler | 886e016 | 2017-06-06 14:23:43 -0400 | [diff] [blame] | 862 | final long nowElapsed = SystemClock.elapsedRealtime(); |
| 863 | long oldestStartTime = nowElapsed; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 864 | if (active.size() == 1) { |
| 865 | intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 866 | intent.setData(Uri.fromParts("package", active.get(0).mPackageName, null)); |
| 867 | title = context.getString( |
| 868 | R.string.foreground_service_app_in_background, active.get(0).mLabel); |
| 869 | msg = context.getString(R.string.foreground_service_tap_for_details); |
Dianne Hackborn | fb5d4b5 | 2017-05-16 17:03:44 -0700 | [diff] [blame] | 870 | pkgs = new String[] { active.get(0).mPackageName }; |
Dan Sandler | 2f36ab8 | 2017-05-25 00:15:49 -0400 | [diff] [blame] | 871 | oldestStartTime = active.get(0).mStartTime; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 872 | } else { |
| 873 | intent = new Intent(Settings.ACTION_FOREGROUND_SERVICES_SETTINGS); |
Dianne Hackborn | fb5d4b5 | 2017-05-16 17:03:44 -0700 | [diff] [blame] | 874 | pkgs = new String[active.size()]; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 875 | for (int i = 0; i < active.size(); i++) { |
| 876 | pkgs[i] = active.get(i).mPackageName; |
Dan Sandler | 2f36ab8 | 2017-05-25 00:15:49 -0400 | [diff] [blame] | 877 | oldestStartTime = Math.min(oldestStartTime, active.get(i).mStartTime); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 878 | } |
| 879 | intent.putExtra("packages", pkgs); |
| 880 | title = context.getString( |
| 881 | R.string.foreground_service_apps_in_background, active.size()); |
Dianne Hackborn | fb5d4b5 | 2017-05-16 17:03:44 -0700 | [diff] [blame] | 882 | msg = active.get(0).mLabel.toString(); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 883 | for (int i = 1; i < active.size(); i++) { |
| 884 | msg = context.getString(R.string.foreground_service_multiple_separator, |
| 885 | msg, active.get(i).mLabel); |
| 886 | } |
| 887 | } |
Dianne Hackborn | fb5d4b5 | 2017-05-16 17:03:44 -0700 | [diff] [blame] | 888 | Bundle notificationBundle = new Bundle(); |
| 889 | notificationBundle.putStringArray(Notification.EXTRA_FOREGROUND_APPS, pkgs); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 890 | Notification.Builder n = |
| 891 | new Notification.Builder(context, |
| 892 | SystemNotificationChannels.FOREGROUND_SERVICE) |
Dianne Hackborn | fb5d4b5 | 2017-05-16 17:03:44 -0700 | [diff] [blame] | 893 | .addExtras(notificationBundle) |
Dan Sandler | 2f36ab8 | 2017-05-25 00:15:49 -0400 | [diff] [blame] | 894 | .setSmallIcon(R.drawable.stat_sys_vitals) |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 895 | .setOngoing(true) |
Dan Sandler | 886e016 | 2017-06-06 14:23:43 -0400 | [diff] [blame] | 896 | .setShowWhen(oldestStartTime < nowElapsed) |
| 897 | .setWhen(System.currentTimeMillis() - (nowElapsed - oldestStartTime)) |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 898 | .setColor(context.getColor( |
| 899 | com.android.internal.R.color.system_notification_accent_color)) |
| 900 | .setContentTitle(title) |
| 901 | .setContentText(msg) |
| 902 | .setContentIntent( |
| 903 | PendingIntent.getActivityAsUser(context, 0, intent, |
| 904 | PendingIntent.FLAG_UPDATE_CURRENT, |
| 905 | null, new UserHandle(smap.mUserId))); |
| 906 | nm.notifyAsUser(null, SystemMessageProto.SystemMessage.NOTE_FOREGROUND_SERVICES, |
| 907 | n.build(), new UserHandle(smap.mUserId)); |
| 908 | } else { |
| 909 | nm.cancelAsUser(null, SystemMessageProto.SystemMessage.NOTE_FOREGROUND_SERVICES, |
| 910 | new UserHandle(smap.mUserId)); |
| 911 | } |
| 912 | } |
| 913 | |
Dianne Hackborn | fb5d4b5 | 2017-05-16 17:03:44 -0700 | [diff] [blame] | 914 | private void requestUpdateActiveForegroundAppsLocked(ServiceMap smap, long timeElapsed) { |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 915 | Message msg = smap.obtainMessage(ServiceMap.MSG_UPDATE_FOREGROUND_APPS); |
Dianne Hackborn | fb5d4b5 | 2017-05-16 17:03:44 -0700 | [diff] [blame] | 916 | if (timeElapsed != 0) { |
| 917 | smap.sendMessageAtTime(msg, |
| 918 | timeElapsed + SystemClock.uptimeMillis() - SystemClock.elapsedRealtime()); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 919 | } else { |
| 920 | smap.mActiveForegroundAppsChanged = true; |
| 921 | smap.sendMessage(msg); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | private void decActiveForegroundAppLocked(ServiceMap smap, ServiceRecord r) { |
| 926 | ActiveForegroundApp active = smap.mActiveForegroundApps.get(r.packageName); |
| 927 | if (active != null) { |
| 928 | active.mNumActive--; |
| 929 | if (active.mNumActive <= 0) { |
| 930 | active.mEndTime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 931 | if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "Ended running of service"); |
| 932 | if (foregroundAppShownEnoughLocked(active, active.mEndTime)) { |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 933 | // Have been active for long enough that we will remove it immediately. |
| 934 | smap.mActiveForegroundApps.remove(r.packageName); |
| 935 | smap.mActiveForegroundAppsChanged = true; |
| 936 | requestUpdateActiveForegroundAppsLocked(smap, 0); |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 937 | } else if (active.mHideTime < Long.MAX_VALUE){ |
| 938 | requestUpdateActiveForegroundAppsLocked(smap, active.mHideTime); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | void updateScreenStateLocked(boolean screenOn) { |
| 945 | if (mScreenOn != screenOn) { |
| 946 | mScreenOn = screenOn; |
| 947 | |
| 948 | // If screen is turning on, then we now reset the start time of any foreground |
| 949 | // services that were started while the screen was off. |
| 950 | if (screenOn) { |
| 951 | final long nowElapsed = SystemClock.elapsedRealtime(); |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 952 | if (DEBUG_FOREGROUND_SERVICE) Slog.d(TAG, "Screen turned on"); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 953 | for (int i = mServiceMap.size()-1; i >= 0; i--) { |
| 954 | ServiceMap smap = mServiceMap.valueAt(i); |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 955 | long nextUpdateTime = Long.MAX_VALUE; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 956 | boolean changed = false; |
| 957 | for (int j = smap.mActiveForegroundApps.size()-1; j >= 0; j--) { |
| 958 | ActiveForegroundApp active = smap.mActiveForegroundApps.valueAt(j); |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 959 | if (active.mEndTime == 0) { |
| 960 | if (!active.mShownWhileScreenOn) { |
| 961 | active.mShownWhileScreenOn = true; |
| 962 | active.mStartVisibleTime = nowElapsed; |
| 963 | } |
| 964 | } else { |
| 965 | if (!active.mShownWhileScreenOn |
| 966 | && active.mStartVisibleTime == active.mStartTime) { |
| 967 | // If this was never shown while the screen was on, then we will |
| 968 | // count the time it started being visible as now, to tell the user |
| 969 | // about it now that they have a screen to look at. |
| 970 | active.mEndTime = active.mStartVisibleTime = nowElapsed; |
| 971 | } |
| 972 | if (foregroundAppShownEnoughLocked(active, nowElapsed)) { |
| 973 | // Have been active for long enough that we will remove it |
| 974 | // immediately. |
| 975 | smap.mActiveForegroundApps.remove(active.mPackageName); |
| 976 | smap.mActiveForegroundAppsChanged = true; |
| 977 | changed = true; |
| 978 | } else { |
| 979 | if (active.mHideTime < nextUpdateTime) { |
| 980 | nextUpdateTime = active.mHideTime; |
| 981 | } |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | } |
| 985 | if (changed) { |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 986 | // Need to immediately update. |
Dianne Hackborn | fb5d4b5 | 2017-05-16 17:03:44 -0700 | [diff] [blame] | 987 | requestUpdateActiveForegroundAppsLocked(smap, 0); |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 988 | } else if (nextUpdateTime < Long.MAX_VALUE) { |
| 989 | requestUpdateActiveForegroundAppsLocked(smap, nextUpdateTime); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 996 | void foregroundServiceProcStateChangedLocked(UidRecord uidRec) { |
| 997 | ServiceMap smap = mServiceMap.get(UserHandle.getUserId(uidRec.uid)); |
| 998 | if (smap != null) { |
| 999 | boolean changed = false; |
| 1000 | for (int j = smap.mActiveForegroundApps.size()-1; j >= 0; j--) { |
| 1001 | ActiveForegroundApp active = smap.mActiveForegroundApps.valueAt(j); |
| 1002 | if (active.mUid == uidRec.uid) { |
| 1003 | if (uidRec.curProcState <= ActivityManager.PROCESS_STATE_TOP) { |
| 1004 | if (!active.mAppOnTop) { |
| 1005 | active.mAppOnTop = true; |
| 1006 | changed = true; |
| 1007 | } |
| 1008 | active.mShownWhileTop = true; |
| 1009 | } else if (active.mAppOnTop) { |
| 1010 | active.mAppOnTop = false; |
| 1011 | changed = true; |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | if (changed) { |
| 1016 | requestUpdateActiveForegroundAppsLocked(smap, 0); |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 1021 | private void setServiceForegroundInnerLocked(ServiceRecord r, int id, |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 1022 | Notification notification, int flags) { |
| 1023 | if (id != 0) { |
| 1024 | if (notification == null) { |
| 1025 | throw new IllegalArgumentException("null notification"); |
| 1026 | } |
Chad Brubaker | 97b383f | 2017-02-02 15:04:35 -0800 | [diff] [blame] | 1027 | // Instant apps need permission to create foreground services. |
| 1028 | if (r.appInfo.isInstantApp()) { |
| 1029 | final int mode = mAm.mAppOpsService.checkOperation( |
| 1030 | AppOpsManager.OP_INSTANT_APP_START_FOREGROUND, |
| 1031 | r.appInfo.uid, |
| 1032 | r.appInfo.packageName); |
| 1033 | switch (mode) { |
| 1034 | case AppOpsManager.MODE_ALLOWED: |
| 1035 | break; |
| 1036 | case AppOpsManager.MODE_IGNORED: |
| 1037 | Slog.w(TAG, "Instant app " + r.appInfo.packageName |
| 1038 | + " does not have permission to create foreground services" |
| 1039 | + ", ignoring."); |
Sudheer Shanka | c9d9407 | 2017-02-22 22:13:55 +0000 | [diff] [blame] | 1040 | return; |
Chad Brubaker | 97b383f | 2017-02-02 15:04:35 -0800 | [diff] [blame] | 1041 | case AppOpsManager.MODE_ERRORED: |
| 1042 | throw new SecurityException("Instant app " + r.appInfo.packageName |
| 1043 | + " does not have permission to create foreground services"); |
| 1044 | default: |
Dianne Hackborn | 994b5ad | 2018-01-19 17:44:46 -0800 | [diff] [blame] | 1045 | mAm.enforcePermission( |
| 1046 | android.Manifest.permission.INSTANT_APP_FOREGROUND_SERVICE, |
| 1047 | r.app.pid, r.appInfo.uid, "startForeground"); |
Chad Brubaker | 97b383f | 2017-02-02 15:04:35 -0800 | [diff] [blame] | 1048 | } |
Dianne Hackborn | 994b5ad | 2018-01-19 17:44:46 -0800 | [diff] [blame] | 1049 | } else if (r.appInfo.targetSdkVersion >= Build.VERSION_CODES.P) { |
| 1050 | mAm.enforcePermission( |
| 1051 | android.Manifest.permission.FOREGROUND_SERVICE, |
| 1052 | r.app.pid, r.appInfo.uid, "startForeground"); |
Chad Brubaker | 97b383f | 2017-02-02 15:04:35 -0800 | [diff] [blame] | 1053 | } |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 1054 | if (r.fgRequired) { |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 1055 | if (DEBUG_SERVICE || DEBUG_BACKGROUND_CHECK) { |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 1056 | Slog.i(TAG, "Service called startForeground() as required: " + r); |
| 1057 | } |
| 1058 | r.fgRequired = false; |
| 1059 | r.fgWaiting = false; |
| 1060 | mAm.mHandler.removeMessages( |
| 1061 | ActivityManagerService.SERVICE_FOREGROUND_TIMEOUT_MSG, r); |
| 1062 | } |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 1063 | if (r.foregroundId != id) { |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 1064 | cancelForegroundNotificationLocked(r); |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 1065 | r.foregroundId = id; |
| 1066 | } |
| 1067 | notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 1068 | r.foregroundNoti = notification; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 1069 | if (!r.isForeground) { |
| 1070 | final ServiceMap smap = getServiceMapLocked(r.userId); |
| 1071 | if (smap != null) { |
| 1072 | ActiveForegroundApp active = smap.mActiveForegroundApps.get(r.packageName); |
| 1073 | if (active == null) { |
| 1074 | active = new ActiveForegroundApp(); |
| 1075 | active.mPackageName = r.packageName; |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 1076 | active.mUid = r.appInfo.uid; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 1077 | active.mShownWhileScreenOn = mScreenOn; |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 1078 | if (r.app != null) { |
| 1079 | active.mAppOnTop = active.mShownWhileTop = |
| 1080 | r.app.uidRecord.curProcState |
| 1081 | <= ActivityManager.PROCESS_STATE_TOP; |
| 1082 | } |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 1083 | active.mStartTime = active.mStartVisibleTime |
| 1084 | = SystemClock.elapsedRealtime(); |
| 1085 | smap.mActiveForegroundApps.put(r.packageName, active); |
| 1086 | requestUpdateActiveForegroundAppsLocked(smap, 0); |
| 1087 | } |
| 1088 | active.mNumActive++; |
| 1089 | } |
| 1090 | r.isForeground = true; |
| 1091 | } |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 1092 | r.postNotification(); |
| 1093 | if (r.app != null) { |
| 1094 | updateServiceForegroundLocked(r.app, true); |
| 1095 | } |
| 1096 | getServiceMapLocked(r.userId).ensureNotStartingBackgroundLocked(r); |
| 1097 | mAm.notifyPackageUse(r.serviceInfo.packageName, |
| 1098 | PackageManager.NOTIFY_PACKAGE_USE_FOREGROUND_SERVICE); |
| 1099 | } else { |
| 1100 | if (r.isForeground) { |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 1101 | final ServiceMap smap = getServiceMapLocked(r.userId); |
| 1102 | if (smap != null) { |
| 1103 | decActiveForegroundAppLocked(smap, r); |
| 1104 | } |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 1105 | r.isForeground = false; |
| 1106 | if (r.app != null) { |
| 1107 | mAm.updateLruProcessLocked(r.app, false, null); |
| 1108 | updateServiceForegroundLocked(r.app, true); |
| 1109 | } |
| 1110 | } |
| 1111 | if ((flags & Service.STOP_FOREGROUND_REMOVE) != 0) { |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 1112 | cancelForegroundNotificationLocked(r); |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 1113 | r.foregroundId = 0; |
| 1114 | r.foregroundNoti = null; |
| 1115 | } else if (r.appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) { |
| 1116 | r.stripForegroundServiceFlagFromNotification(); |
| 1117 | if ((flags & Service.STOP_FOREGROUND_DETACH) != 0) { |
| 1118 | r.foregroundId = 0; |
| 1119 | r.foregroundNoti = null; |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 1125 | private void cancelForegroundNotificationLocked(ServiceRecord r) { |
Dianne Hackborn | 0ba4c71 | 2016-08-01 17:49:41 -0700 | [diff] [blame] | 1126 | if (r.foregroundId != 0) { |
| 1127 | // First check to see if this app has any other active foreground services |
| 1128 | // with the same notification ID. If so, we shouldn't actually cancel it, |
| 1129 | // because that would wipe away the notification that still needs to be shown |
| 1130 | // due the other service. |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 1131 | ServiceMap sm = getServiceMapLocked(r.userId); |
Dianne Hackborn | 0ba4c71 | 2016-08-01 17:49:41 -0700 | [diff] [blame] | 1132 | if (sm != null) { |
| 1133 | for (int i = sm.mServicesByName.size()-1; i >= 0; i--) { |
| 1134 | ServiceRecord other = sm.mServicesByName.valueAt(i); |
Dianne Hackborn | 569a458 | 2016-08-04 14:00:22 -0700 | [diff] [blame] | 1135 | if (other != r && other.foregroundId == r.foregroundId |
Dianne Hackborn | 0ba4c71 | 2016-08-01 17:49:41 -0700 | [diff] [blame] | 1136 | && other.packageName.equals(r.packageName)) { |
| 1137 | // Found one! Abort the cancel. |
| 1138 | return; |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | r.cancelNotification(); |
| 1143 | } |
| 1144 | } |
| 1145 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1146 | private void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) { |
| 1147 | boolean anyForeground = false; |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 1148 | for (int i=proc.services.size()-1; i>=0; i--) { |
| 1149 | ServiceRecord sr = proc.services.valueAt(i); |
Christopher Tate | e23fa2b | 2017-04-20 14:48:13 -0700 | [diff] [blame] | 1150 | if (sr.isForeground || sr.fgRequired) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1151 | anyForeground = true; |
| 1152 | break; |
| 1153 | } |
| 1154 | } |
Dianne Hackborn | eaf2ac4 | 2014-02-07 13:01:07 -0800 | [diff] [blame] | 1155 | mAm.updateProcessForegroundLocked(proc, anyForeground, oomAdj); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Dianne Hackborn | a590d2b | 2016-06-27 15:07:18 -0700 | [diff] [blame] | 1158 | private void updateWhitelistManagerLocked(ProcessRecord proc) { |
| 1159 | proc.whitelistManager = false; |
| 1160 | for (int i=proc.services.size()-1; i>=0; i--) { |
| 1161 | ServiceRecord sr = proc.services.valueAt(i); |
| 1162 | if (sr.whitelistManager) { |
| 1163 | proc.whitelistManager = true; |
| 1164 | break; |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
Dianne Hackborn | 465fa39 | 2014-09-14 14:21:18 -0700 | [diff] [blame] | 1169 | public void updateServiceConnectionActivitiesLocked(ProcessRecord clientProc) { |
| 1170 | ArraySet<ProcessRecord> updatedProcesses = null; |
Wale Ogunwale | d6ac762 | 2016-05-26 09:02:25 -0700 | [diff] [blame] | 1171 | for (int i = 0; i < clientProc.connections.size(); i++) { |
Dianne Hackborn | 465fa39 | 2014-09-14 14:21:18 -0700 | [diff] [blame] | 1172 | final ConnectionRecord conn = clientProc.connections.valueAt(i); |
| 1173 | final ProcessRecord proc = conn.binding.service.app; |
| 1174 | if (proc == null || proc == clientProc) { |
| 1175 | continue; |
| 1176 | } else if (updatedProcesses == null) { |
| 1177 | updatedProcesses = new ArraySet<>(); |
| 1178 | } else if (updatedProcesses.contains(proc)) { |
| 1179 | continue; |
| 1180 | } |
| 1181 | updatedProcesses.add(proc); |
| 1182 | updateServiceClientActivitiesLocked(proc, null, false); |
| 1183 | } |
| 1184 | } |
| 1185 | |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 1186 | private boolean updateServiceClientActivitiesLocked(ProcessRecord proc, |
Dianne Hackborn | 465fa39 | 2014-09-14 14:21:18 -0700 | [diff] [blame] | 1187 | ConnectionRecord modCr, boolean updateLru) { |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 1188 | if (modCr != null && modCr.binding.client != null) { |
| 1189 | if (modCr.binding.client.activities.size() <= 0) { |
| 1190 | // This connection is from a client without activities, so adding |
| 1191 | // and removing is not interesting. |
| 1192 | return false; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | boolean anyClientActivities = false; |
| 1197 | for (int i=proc.services.size()-1; i>=0 && !anyClientActivities; i--) { |
| 1198 | ServiceRecord sr = proc.services.valueAt(i); |
| 1199 | for (int conni=sr.connections.size()-1; conni>=0 && !anyClientActivities; conni--) { |
| 1200 | ArrayList<ConnectionRecord> clist = sr.connections.valueAt(conni); |
| 1201 | for (int cri=clist.size()-1; cri>=0; cri--) { |
| 1202 | ConnectionRecord cr = clist.get(cri); |
| 1203 | if (cr.binding.client == null || cr.binding.client == proc) { |
| 1204 | // Binding to ourself is not interesting. |
| 1205 | continue; |
| 1206 | } |
| 1207 | if (cr.binding.client.activities.size() > 0) { |
| 1208 | anyClientActivities = true; |
| 1209 | break; |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | if (anyClientActivities != proc.hasClientActivities) { |
| 1215 | proc.hasClientActivities = anyClientActivities; |
Dianne Hackborn | 465fa39 | 2014-09-14 14:21:18 -0700 | [diff] [blame] | 1216 | if (updateLru) { |
| 1217 | mAm.updateLruProcessLocked(proc, anyClientActivities, null); |
| 1218 | } |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 1219 | return true; |
| 1220 | } |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 1224 | int bindServiceLocked(IApplicationThread caller, IBinder token, Intent service, |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 1225 | String resolvedType, final IServiceConnection connection, int flags, |
| 1226 | String callingPackage, final int userId) throws TransactionTooLargeException { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1227 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "bindService: " + service |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1228 | + " type=" + resolvedType + " conn=" + connection.asBinder() |
| 1229 | + " flags=0x" + Integer.toHexString(flags)); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1230 | final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller); |
| 1231 | if (callerApp == null) { |
| 1232 | throw new SecurityException( |
| 1233 | "Unable to find app for caller " + caller |
| 1234 | + " (pid=" + Binder.getCallingPid() |
| 1235 | + ") when binding service " + service); |
| 1236 | } |
| 1237 | |
| 1238 | ActivityRecord activity = null; |
| 1239 | if (token != null) { |
Craig Mautner | d232895 | 2013-03-05 12:46:26 -0800 | [diff] [blame] | 1240 | activity = ActivityRecord.isInStackLocked(token); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1241 | if (activity == null) { |
| 1242 | Slog.w(TAG, "Binding with unknown activity: " + token); |
| 1243 | return 0; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | int clientLabel = 0; |
| 1248 | PendingIntent clientIntent = null; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1249 | final boolean isCallerSystem = callerApp.info.uid == Process.SYSTEM_UID; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1250 | |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1251 | if (isCallerSystem) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1252 | // Hacky kind of thing -- allow system stuff to tell us |
| 1253 | // what they are, so we can report this elsewhere for |
| 1254 | // others to know why certain services are running. |
Jeff Sharkey | f0ec2e0 | 2016-03-21 12:37:54 -0600 | [diff] [blame] | 1255 | service.setDefusable(true); |
| 1256 | clientIntent = service.getParcelableExtra(Intent.EXTRA_CLIENT_INTENT); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1257 | if (clientIntent != null) { |
| 1258 | clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0); |
| 1259 | if (clientLabel != 0) { |
| 1260 | // There are no useful extras in the intent, trash them. |
| 1261 | // System code calling with this stuff just needs to know |
| 1262 | // this will happen. |
| 1263 | service = service.cloneFilter(); |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | |
Dianne Hackborn | f0f94d1 | 2014-03-17 16:04:21 -0700 | [diff] [blame] | 1268 | if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) { |
| 1269 | mAm.enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS, |
| 1270 | "BIND_TREAT_LIKE_ACTIVITY"); |
| 1271 | } |
| 1272 | |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1273 | if ((flags & Context.BIND_ALLOW_WHITELIST_MANAGEMENT) != 0 && !isCallerSystem) { |
| 1274 | throw new SecurityException( |
| 1275 | "Non-system caller " + caller + " (pid=" + Binder.getCallingPid() |
| 1276 | + ") set BIND_ALLOW_WHITELIST_MANAGEMENT when binding service " + service); |
| 1277 | } |
| 1278 | |
Svet Ganov | d223db3 | 2017-12-22 09:43:48 -0800 | [diff] [blame] | 1279 | if ((flags & Context.BIND_ALLOW_INSTANT) != 0 && !isCallerSystem) { |
| 1280 | throw new SecurityException( |
| 1281 | "Non-system caller " + caller + " (pid=" + Binder.getCallingPid() |
| 1282 | + ") set BIND_ALLOW_INSTANT when binding service " + service); |
| 1283 | } |
| 1284 | |
Dianne Hackborn | a49ad09 | 2016-03-03 13:39:10 -0800 | [diff] [blame] | 1285 | final boolean callerFg = callerApp.setSchedGroup != ProcessList.SCHED_GROUP_BACKGROUND; |
Robert Sesek | b9a8666 | 2015-12-09 16:22:45 -0500 | [diff] [blame] | 1286 | final boolean isBindExternal = (flags & Context.BIND_EXTERNAL_SERVICE) != 0; |
Svet Ganov | d223db3 | 2017-12-22 09:43:48 -0800 | [diff] [blame] | 1287 | final boolean allowInstant = (flags & Context.BIND_ALLOW_INSTANT) != 0; |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1288 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1289 | ServiceLookupResult res = |
Robert Sesek | b9a8666 | 2015-12-09 16:22:45 -0500 | [diff] [blame] | 1290 | retrieveServiceLocked(service, resolvedType, callingPackage, Binder.getCallingPid(), |
Svet Ganov | d223db3 | 2017-12-22 09:43:48 -0800 | [diff] [blame] | 1291 | Binder.getCallingUid(), userId, true, callerFg, isBindExternal, allowInstant); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1292 | if (res == null) { |
| 1293 | return 0; |
| 1294 | } |
| 1295 | if (res.record == null) { |
| 1296 | return -1; |
| 1297 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1298 | ServiceRecord s = res.record; |
| 1299 | |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 1300 | boolean permissionsReviewRequired = false; |
| 1301 | |
| 1302 | // If permissions need a review before any of the app components can run, |
| 1303 | // we schedule binding to the service but do not start its process, then |
| 1304 | // we launch a review activity to which is passed a callback to invoke |
| 1305 | // when done to start the bound service's process to completing the binding. |
Svet Ganov | 77df6f3 | 2016-08-17 11:46:34 -0700 | [diff] [blame] | 1306 | if (mAm.mPermissionReviewRequired) { |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 1307 | if (mAm.getPackageManagerInternalLocked().isPermissionsReviewRequired( |
| 1308 | s.packageName, s.userId)) { |
| 1309 | |
| 1310 | permissionsReviewRequired = true; |
| 1311 | |
| 1312 | // Show a permission review UI only for binding from a foreground app |
| 1313 | if (!callerFg) { |
| 1314 | Slog.w(TAG, "u" + s.userId + " Binding to a service in package" |
| 1315 | + s.packageName + " requires a permissions review"); |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | final ServiceRecord serviceRecord = s; |
| 1320 | final Intent serviceIntent = service; |
| 1321 | |
| 1322 | RemoteCallback callback = new RemoteCallback( |
| 1323 | new RemoteCallback.OnResultListener() { |
| 1324 | @Override |
| 1325 | public void onResult(Bundle result) { |
| 1326 | synchronized(mAm) { |
| 1327 | final long identity = Binder.clearCallingIdentity(); |
| 1328 | try { |
| 1329 | if (!mPendingServices.contains(serviceRecord)) { |
| 1330 | return; |
| 1331 | } |
| 1332 | // If there is still a pending record, then the service |
| 1333 | // binding request is still valid, so hook them up. We |
| 1334 | // proceed only if the caller cleared the review requirement |
| 1335 | // otherwise we unbind because the user didn't approve. |
| 1336 | if (!mAm.getPackageManagerInternalLocked() |
| 1337 | .isPermissionsReviewRequired( |
| 1338 | serviceRecord.packageName, |
| 1339 | serviceRecord.userId)) { |
| 1340 | try { |
| 1341 | bringUpServiceLocked(serviceRecord, |
| 1342 | serviceIntent.getFlags(), |
| 1343 | callerFg, false, false); |
| 1344 | } catch (RemoteException e) { |
| 1345 | /* ignore - local call */ |
| 1346 | } |
| 1347 | } else { |
| 1348 | unbindServiceLocked(connection); |
| 1349 | } |
| 1350 | } finally { |
| 1351 | Binder.restoreCallingIdentity(identity); |
| 1352 | } |
| 1353 | } |
| 1354 | } |
| 1355 | }); |
| 1356 | |
| 1357 | final Intent intent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS); |
| 1358 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 1359 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 1360 | intent.putExtra(Intent.EXTRA_PACKAGE_NAME, s.packageName); |
| 1361 | intent.putExtra(Intent.EXTRA_REMOTE_CALLBACK, callback); |
| 1362 | |
| 1363 | if (DEBUG_PERMISSIONS_REVIEW) { |
| 1364 | Slog.i(TAG, "u" + s.userId + " Launching permission review for package " |
| 1365 | + s.packageName); |
| 1366 | } |
| 1367 | |
| 1368 | mAm.mHandler.post(new Runnable() { |
| 1369 | @Override |
| 1370 | public void run() { |
| 1371 | mAm.mContext.startActivityAsUser(intent, new UserHandle(userId)); |
| 1372 | } |
| 1373 | }); |
| 1374 | } |
| 1375 | } |
| 1376 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1377 | final long origId = Binder.clearCallingIdentity(); |
| 1378 | |
| 1379 | try { |
Dianne Hackborn | d6f5b62 | 2013-11-11 17:25:37 -0800 | [diff] [blame] | 1380 | if (unscheduleServiceRestartLocked(s, callerApp.info.uid, false)) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1381 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "BIND SERVICE WHILE RESTART PENDING: " |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1382 | + s); |
| 1383 | } |
| 1384 | |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 1385 | if ((flags&Context.BIND_AUTO_CREATE) != 0) { |
| 1386 | s.lastActivity = SystemClock.uptimeMillis(); |
| 1387 | if (!s.hasAutoCreateConnections()) { |
| 1388 | // This is the first binding, let the tracker know. |
Joe Onorato | 4eb64fd | 2016-03-21 15:30:09 -0700 | [diff] [blame] | 1389 | ServiceState stracker = s.getTracker(); |
Dianne Hackborn | bd754f4 | 2013-07-23 15:52:36 -0700 | [diff] [blame] | 1390 | if (stracker != null) { |
Dianne Hackborn | d293224 | 2013-08-05 18:18:42 -0700 | [diff] [blame] | 1391 | stracker.setBound(true, mAm.mProcessStats.getMemFactorLocked(), |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 1392 | s.lastActivity); |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
Joe Onorato | 05c9ecc | 2016-03-30 15:13:46 -0700 | [diff] [blame] | 1397 | mAm.startAssociationLocked(callerApp.uid, callerApp.processName, callerApp.curProcState, |
Dianne Hackborn | ab2df06 | 2015-01-07 13:43:13 -0800 | [diff] [blame] | 1398 | s.appInfo.uid, s.name, s.processName); |
Jesse Evans | 02af3ea | 2017-05-03 09:57:16 -0700 | [diff] [blame] | 1399 | // Once the apps have become associated, if one of them is caller is ephemeral |
| 1400 | // the target app should now be able to see the calling app |
| 1401 | mAm.grantEphemeralAccessLocked(callerApp.userId, service, |
| 1402 | s.appInfo.uid, UserHandle.getAppId(callerApp.uid)); |
Dianne Hackborn | ab2df06 | 2015-01-07 13:43:13 -0800 | [diff] [blame] | 1403 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1404 | AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp); |
| 1405 | ConnectionRecord c = new ConnectionRecord(b, activity, |
| 1406 | connection, flags, clientLabel, clientIntent); |
| 1407 | |
| 1408 | IBinder binder = connection.asBinder(); |
| 1409 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 1410 | if (clist == null) { |
| 1411 | clist = new ArrayList<ConnectionRecord>(); |
| 1412 | s.connections.put(binder, clist); |
| 1413 | } |
| 1414 | clist.add(c); |
| 1415 | b.connections.add(c); |
| 1416 | if (activity != null) { |
| 1417 | if (activity.connections == null) { |
| 1418 | activity.connections = new HashSet<ConnectionRecord>(); |
| 1419 | } |
| 1420 | activity.connections.add(c); |
| 1421 | } |
| 1422 | b.client.connections.add(c); |
| 1423 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 1424 | b.client.hasAboveClient = true; |
| 1425 | } |
Dianne Hackborn | a590d2b | 2016-06-27 15:07:18 -0700 | [diff] [blame] | 1426 | if ((c.flags&Context.BIND_ALLOW_WHITELIST_MANAGEMENT) != 0) { |
| 1427 | s.whitelistManager = true; |
| 1428 | } |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 1429 | if (s.app != null) { |
Dianne Hackborn | 465fa39 | 2014-09-14 14:21:18 -0700 | [diff] [blame] | 1430 | updateServiceClientActivitiesLocked(s.app, c, true); |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 1431 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1432 | clist = mServiceConnections.get(binder); |
| 1433 | if (clist == null) { |
| 1434 | clist = new ArrayList<ConnectionRecord>(); |
| 1435 | mServiceConnections.put(binder, clist); |
| 1436 | } |
| 1437 | clist.add(c); |
| 1438 | |
| 1439 | if ((flags&Context.BIND_AUTO_CREATE) != 0) { |
| 1440 | s.lastActivity = SystemClock.uptimeMillis(); |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 1441 | if (bringUpServiceLocked(s, service.getFlags(), callerFg, false, |
| 1442 | permissionsReviewRequired) != null) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1443 | return 0; |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | if (s.app != null) { |
Dianne Hackborn | f0f94d1 | 2014-03-17 16:04:21 -0700 | [diff] [blame] | 1448 | if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) { |
| 1449 | s.app.treatLikeActivity = true; |
| 1450 | } |
Dianne Hackborn | a590d2b | 2016-06-27 15:07:18 -0700 | [diff] [blame] | 1451 | if (s.whitelistManager) { |
| 1452 | s.app.whitelistManager = true; |
| 1453 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1454 | // This could have made the service more important. |
Dianne Hackborn | f0f94d1 | 2014-03-17 16:04:21 -0700 | [diff] [blame] | 1455 | mAm.updateLruProcessLocked(s.app, s.app.hasClientActivities |
| 1456 | || s.app.treatLikeActivity, b.client); |
Amith Yamasani | 385c3ad | 2017-05-04 14:27:11 -0700 | [diff] [blame] | 1457 | mAm.updateOomAdjLocked(s.app, true); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1460 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Bind " + s + " with " + b |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1461 | + ": received=" + b.intent.received |
| 1462 | + " apps=" + b.intent.apps.size() |
| 1463 | + " doRebind=" + b.intent.doRebind); |
| 1464 | |
| 1465 | if (s.app != null && b.intent.received) { |
| 1466 | // Service is already running, so we can immediately |
| 1467 | // publish the connection. |
| 1468 | try { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1469 | c.conn.connected(s.name, b.intent.binder, false); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1470 | } catch (Exception e) { |
| 1471 | Slog.w(TAG, "Failure sending service " + s.shortName |
| 1472 | + " to connection " + c.conn.asBinder() |
| 1473 | + " (in " + c.binding.client.processName + ")", e); |
| 1474 | } |
| 1475 | |
| 1476 | // If this is the first app connected back to this binding, |
| 1477 | // and the service had previously asked to be told when |
| 1478 | // rebound, then do so. |
| 1479 | if (b.intent.apps.size() == 1 && b.intent.doRebind) { |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1480 | requestServiceBindingLocked(s, b.intent, callerFg, true); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1481 | } |
| 1482 | } else if (!b.intent.requested) { |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1483 | requestServiceBindingLocked(s, b.intent, callerFg, false); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1484 | } |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 1485 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 1486 | getServiceMapLocked(s.userId).ensureNotStartingBackgroundLocked(s); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 1487 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1488 | } finally { |
| 1489 | Binder.restoreCallingIdentity(origId); |
| 1490 | } |
| 1491 | |
| 1492 | return 1; |
| 1493 | } |
| 1494 | |
| 1495 | void publishServiceLocked(ServiceRecord r, Intent intent, IBinder service) { |
| 1496 | final long origId = Binder.clearCallingIdentity(); |
| 1497 | try { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1498 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "PUBLISHING " + r |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1499 | + " " + intent + ": " + service); |
| 1500 | if (r != null) { |
| 1501 | Intent.FilterComparison filter |
| 1502 | = new Intent.FilterComparison(intent); |
| 1503 | IntentBindRecord b = r.bindings.get(filter); |
| 1504 | if (b != null && !b.received) { |
| 1505 | b.binder = service; |
| 1506 | b.requested = true; |
| 1507 | b.received = true; |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1508 | for (int conni=r.connections.size()-1; conni>=0; conni--) { |
| 1509 | ArrayList<ConnectionRecord> clist = r.connections.valueAt(conni); |
| 1510 | for (int i=0; i<clist.size(); i++) { |
| 1511 | ConnectionRecord c = clist.get(i); |
| 1512 | if (!filter.equals(c.binding.intent.intent)) { |
| 1513 | if (DEBUG_SERVICE) Slog.v( |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1514 | TAG_SERVICE, "Not publishing to: " + c); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1515 | if (DEBUG_SERVICE) Slog.v( |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1516 | TAG_SERVICE, "Bound intent: " + c.binding.intent.intent); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1517 | if (DEBUG_SERVICE) Slog.v( |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1518 | TAG_SERVICE, "Published intent: " + intent); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1519 | continue; |
| 1520 | } |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1521 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Publishing to: " + c); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1522 | try { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 1523 | c.conn.connected(r.name, service, false); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 1524 | } catch (Exception e) { |
| 1525 | Slog.w(TAG, "Failure sending service " + r.name + |
| 1526 | " to connection " + c.conn.asBinder() + |
| 1527 | " (in " + c.binding.client.processName + ")", e); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | } |
| 1532 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 1533 | serviceDoneExecutingLocked(r, mDestroyingServices.contains(r), false); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1534 | } |
| 1535 | } finally { |
| 1536 | Binder.restoreCallingIdentity(origId); |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | boolean unbindServiceLocked(IServiceConnection connection) { |
| 1541 | IBinder binder = connection.asBinder(); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1542 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "unbindService: conn=" + binder); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1543 | ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder); |
| 1544 | if (clist == null) { |
| 1545 | Slog.w(TAG, "Unbind failed: could not find connection for " |
| 1546 | + connection.asBinder()); |
| 1547 | return false; |
| 1548 | } |
| 1549 | |
| 1550 | final long origId = Binder.clearCallingIdentity(); |
| 1551 | try { |
| 1552 | while (clist.size() > 0) { |
| 1553 | ConnectionRecord r = clist.get(0); |
| 1554 | removeConnectionLocked(r, null, null); |
Dianne Hackborn | 25e1eca | 2014-09-23 10:13:13 -0700 | [diff] [blame] | 1555 | if (clist.size() > 0 && clist.get(0) == r) { |
| 1556 | // In case it didn't get removed above, do it now. |
| 1557 | Slog.wtf(TAG, "Connection " + r + " not removed for binder " + binder); |
| 1558 | clist.remove(0); |
| 1559 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1560 | |
| 1561 | if (r.binding.service.app != null) { |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1562 | if (r.binding.service.app.whitelistManager) { |
Felipe Leme | a7778b0 | 2016-06-28 10:38:59 -0700 | [diff] [blame] | 1563 | updateWhitelistManagerLocked(r.binding.service.app); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1564 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1565 | // This could have made the service less important. |
Dianne Hackborn | f0f94d1 | 2014-03-17 16:04:21 -0700 | [diff] [blame] | 1566 | if ((r.flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) { |
| 1567 | r.binding.service.app.treatLikeActivity = true; |
| 1568 | mAm.updateLruProcessLocked(r.binding.service.app, |
| 1569 | r.binding.service.app.hasClientActivities |
| 1570 | || r.binding.service.app.treatLikeActivity, null); |
| 1571 | } |
Amith Yamasani | 385c3ad | 2017-05-04 14:27:11 -0700 | [diff] [blame] | 1572 | mAm.updateOomAdjLocked(r.binding.service.app, false); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1573 | } |
| 1574 | } |
Amith Yamasani | b2926b1 | 2017-05-11 11:13:52 -0700 | [diff] [blame] | 1575 | |
| 1576 | mAm.updateOomAdjLocked(); |
| 1577 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1578 | } finally { |
| 1579 | Binder.restoreCallingIdentity(origId); |
| 1580 | } |
| 1581 | |
| 1582 | return true; |
| 1583 | } |
| 1584 | |
| 1585 | void unbindFinishedLocked(ServiceRecord r, Intent intent, boolean doRebind) { |
| 1586 | final long origId = Binder.clearCallingIdentity(); |
| 1587 | try { |
| 1588 | if (r != null) { |
| 1589 | Intent.FilterComparison filter |
| 1590 | = new Intent.FilterComparison(intent); |
| 1591 | IntentBindRecord b = r.bindings.get(filter); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1592 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "unbindFinished in " + r |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1593 | + " at " + b + ": apps=" |
| 1594 | + (b != null ? b.apps.size() : 0)); |
| 1595 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 1596 | boolean inDestroying = mDestroyingServices.contains(r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1597 | if (b != null) { |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 1598 | if (b.apps.size() > 0 && !inDestroying) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1599 | // Applications have already bound since the last |
| 1600 | // unbind, so just rebind right here. |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1601 | boolean inFg = false; |
| 1602 | for (int i=b.apps.size()-1; i>=0; i--) { |
| 1603 | ProcessRecord client = b.apps.valueAt(i).client; |
| 1604 | if (client != null && client.setSchedGroup |
Dianne Hackborn | a49ad09 | 2016-03-03 13:39:10 -0800 | [diff] [blame] | 1605 | != ProcessList.SCHED_GROUP_BACKGROUND) { |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1606 | inFg = true; |
| 1607 | break; |
| 1608 | } |
| 1609 | } |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 1610 | try { |
| 1611 | requestServiceBindingLocked(r, b, inFg, true); |
| 1612 | } catch (TransactionTooLargeException e) { |
| 1613 | // Don't pass this back to ActivityThread, it's unrelated. |
| 1614 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1615 | } else { |
| 1616 | // Note to tell the service the next time there is |
| 1617 | // a new client. |
| 1618 | b.doRebind = true; |
| 1619 | } |
| 1620 | } |
| 1621 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 1622 | serviceDoneExecutingLocked(r, inDestroying, false); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1623 | } |
| 1624 | } finally { |
| 1625 | Binder.restoreCallingIdentity(origId); |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | private final ServiceRecord findServiceLocked(ComponentName name, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1630 | IBinder token, int userId) { |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 1631 | ServiceRecord r = getServiceByNameLocked(name, userId); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1632 | return r == token ? r : null; |
| 1633 | } |
| 1634 | |
| 1635 | private final class ServiceLookupResult { |
| 1636 | final ServiceRecord record; |
| 1637 | final String permission; |
| 1638 | |
| 1639 | ServiceLookupResult(ServiceRecord _record, String _permission) { |
| 1640 | record = _record; |
| 1641 | permission = _permission; |
| 1642 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | private class ServiceRestarter implements Runnable { |
| 1646 | private ServiceRecord mService; |
| 1647 | |
| 1648 | void setService(ServiceRecord service) { |
| 1649 | mService = service; |
| 1650 | } |
| 1651 | |
| 1652 | public void run() { |
| 1653 | synchronized(mAm) { |
| 1654 | performServiceRestartLocked(mService); |
| 1655 | } |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | private ServiceLookupResult retrieveServiceLocked(Intent service, |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1660 | String resolvedType, String callingPackage, int callingPid, int callingUid, int userId, |
Svet Ganov | d223db3 | 2017-12-22 09:43:48 -0800 | [diff] [blame] | 1661 | boolean createIfNeeded, boolean callingFromFg, boolean isBindExternal, |
| 1662 | boolean allowInstant) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1663 | ServiceRecord r = null; |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1664 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "retrieveServiceLocked: " + service |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1665 | + " type=" + resolvedType + " callingUid=" + callingUid); |
| 1666 | |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 1667 | userId = mAm.mUserController.handleIncomingUser(callingPid, callingUid, userId, false, |
| 1668 | ActivityManagerService.ALLOW_NON_FULL_IN_PROFILE, "service", null); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1669 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 1670 | ServiceMap smap = getServiceMapLocked(userId); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 1671 | final ComponentName comp = service.getComponent(); |
| 1672 | if (comp != null) { |
| 1673 | r = smap.mServicesByName.get(comp); |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 1674 | if (DEBUG_SERVICE && r != null) Slog.v(TAG_SERVICE, "Retrieved by component: " + r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1675 | } |
Robert Sesek | b9a8666 | 2015-12-09 16:22:45 -0500 | [diff] [blame] | 1676 | if (r == null && !isBindExternal) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1677 | Intent.FilterComparison filter = new Intent.FilterComparison(service); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 1678 | r = smap.mServicesByIntent.get(filter); |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 1679 | if (DEBUG_SERVICE && r != null) Slog.v(TAG_SERVICE, "Retrieved by intent: " + r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1680 | } |
Robert Sesek | b9a8666 | 2015-12-09 16:22:45 -0500 | [diff] [blame] | 1681 | if (r != null && (r.serviceInfo.flags & ServiceInfo.FLAG_EXTERNAL_SERVICE) != 0 |
| 1682 | && !callingPackage.equals(r.packageName)) { |
| 1683 | // If an external service is running within its own package, other packages |
| 1684 | // should not bind to that instance. |
| 1685 | r = null; |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 1686 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Whoops, can't use existing external service"); |
Robert Sesek | b9a8666 | 2015-12-09 16:22:45 -0500 | [diff] [blame] | 1687 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1688 | if (r == null) { |
| 1689 | try { |
Svet Ganov | d223db3 | 2017-12-22 09:43:48 -0800 | [diff] [blame] | 1690 | int flags = ActivityManagerService.STOCK_PM_FLAGS |
| 1691 | | PackageManager.MATCH_DEBUG_TRIAGED_MISSING; |
| 1692 | if (allowInstant) { |
| 1693 | flags |= PackageManager.MATCH_INSTANT; |
| 1694 | } |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 1695 | // TODO: come back and remove this assumption to triage all services |
Todd Kennedy | 51b3aac | 2017-03-30 17:50:42 -0700 | [diff] [blame] | 1696 | ResolveInfo rInfo = mAm.getPackageManagerInternalLocked().resolveService(service, |
Svet Ganov | d223db3 | 2017-12-22 09:43:48 -0800 | [diff] [blame] | 1697 | resolvedType, flags, userId, callingUid); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1698 | ServiceInfo sInfo = |
| 1699 | rInfo != null ? rInfo.serviceInfo : null; |
| 1700 | if (sInfo == null) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1701 | Slog.w(TAG_SERVICE, "Unable to start service " + service + " U=" + userId + |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1702 | ": not found"); |
| 1703 | return null; |
| 1704 | } |
| 1705 | ComponentName name = new ComponentName( |
| 1706 | sInfo.applicationInfo.packageName, sInfo.name); |
Robert Sesek | b9a8666 | 2015-12-09 16:22:45 -0500 | [diff] [blame] | 1707 | if ((sInfo.flags & ServiceInfo.FLAG_EXTERNAL_SERVICE) != 0) { |
| 1708 | if (isBindExternal) { |
| 1709 | if (!sInfo.exported) { |
| 1710 | throw new SecurityException("BIND_EXTERNAL_SERVICE failed, " + name + |
| 1711 | " is not exported"); |
| 1712 | } |
| 1713 | if ((sInfo.flags & ServiceInfo.FLAG_ISOLATED_PROCESS) == 0) { |
| 1714 | throw new SecurityException("BIND_EXTERNAL_SERVICE failed, " + name + |
| 1715 | " is not an isolatedProcess"); |
| 1716 | } |
| 1717 | // Run the service under the calling package's application. |
| 1718 | ApplicationInfo aInfo = AppGlobals.getPackageManager().getApplicationInfo( |
| 1719 | callingPackage, ActivityManagerService.STOCK_PM_FLAGS, userId); |
| 1720 | if (aInfo == null) { |
| 1721 | throw new SecurityException("BIND_EXTERNAL_SERVICE failed, " + |
| 1722 | "could not resolve client package " + callingPackage); |
| 1723 | } |
| 1724 | sInfo = new ServiceInfo(sInfo); |
| 1725 | sInfo.applicationInfo = new ApplicationInfo(sInfo.applicationInfo); |
| 1726 | sInfo.applicationInfo.packageName = aInfo.packageName; |
| 1727 | sInfo.applicationInfo.uid = aInfo.uid; |
| 1728 | name = new ComponentName(aInfo.packageName, name.getClassName()); |
| 1729 | service.setComponent(name); |
| 1730 | } else { |
| 1731 | throw new SecurityException("BIND_EXTERNAL_SERVICE required for " + |
| 1732 | name); |
| 1733 | } |
| 1734 | } else if (isBindExternal) { |
| 1735 | throw new SecurityException("BIND_EXTERNAL_SERVICE failed, " + name + |
| 1736 | " is not an externalService"); |
| 1737 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1738 | if (userId > 0) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1739 | if (mAm.isSingleton(sInfo.processName, sInfo.applicationInfo, |
Amith Yamasani | 4b9d79c | 2014-05-21 19:14:21 -0700 | [diff] [blame] | 1740 | sInfo.name, sInfo.flags) |
| 1741 | && mAm.isValidSingletonCall(callingUid, sInfo.applicationInfo.uid)) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1742 | userId = 0; |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 1743 | smap = getServiceMapLocked(0); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1744 | } |
| 1745 | sInfo = new ServiceInfo(sInfo); |
| 1746 | sInfo.applicationInfo = mAm.getAppInfoForUser(sInfo.applicationInfo, userId); |
| 1747 | } |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 1748 | r = smap.mServicesByName.get(name); |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 1749 | if (DEBUG_SERVICE && r != null) Slog.v(TAG_SERVICE, |
| 1750 | "Retrieved via pm by intent: " + r); |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1751 | if (r == null && createIfNeeded) { |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 1752 | final Intent.FilterComparison filter |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 1753 | = new Intent.FilterComparison(service.cloneFilter()); |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 1754 | final ServiceRestarter res = new ServiceRestarter(); |
| 1755 | final BatteryStatsImpl.Uid.Pkg.Serv ss; |
| 1756 | final BatteryStatsImpl stats = mAm.mBatteryStatsService.getActiveStatistics(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1757 | synchronized (stats) { |
| 1758 | ss = stats.getServiceStatsLocked( |
| 1759 | sInfo.applicationInfo.uid, sInfo.packageName, |
| 1760 | sInfo.name); |
| 1761 | } |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1762 | r = new ServiceRecord(mAm, ss, name, filter, sInfo, callingFromFg, res); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1763 | res.setService(r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 1764 | smap.mServicesByName.put(name, r); |
| 1765 | smap.mServicesByIntent.put(filter, r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1766 | |
| 1767 | // Make sure this component isn't in the pending list. |
Dianne Hackborn | d6f5b62 | 2013-11-11 17:25:37 -0800 | [diff] [blame] | 1768 | for (int i=mPendingServices.size()-1; i>=0; i--) { |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 1769 | final ServiceRecord pr = mPendingServices.get(i); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1770 | if (pr.serviceInfo.applicationInfo.uid == sInfo.applicationInfo.uid |
| 1771 | && pr.name.equals(name)) { |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 1772 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Remove pending: " + pr); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1773 | mPendingServices.remove(i); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1774 | } |
| 1775 | } |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 1776 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Retrieve created new service: " + r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1777 | } |
| 1778 | } catch (RemoteException ex) { |
| 1779 | // pm is in same process, this will never happen. |
| 1780 | } |
| 1781 | } |
| 1782 | if (r != null) { |
| 1783 | if (mAm.checkComponentPermission(r.permission, |
Bryce Lee | 290e578 | 2017-02-01 16:41:20 -0800 | [diff] [blame] | 1784 | callingPid, callingUid, r.appInfo.uid, r.exported) != PERMISSION_GRANTED) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1785 | if (!r.exported) { |
| 1786 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 1787 | + " from pid=" + callingPid |
| 1788 | + ", uid=" + callingUid |
| 1789 | + " that is not exported from uid " + r.appInfo.uid); |
| 1790 | return new ServiceLookupResult(null, "not exported from uid " |
| 1791 | + r.appInfo.uid); |
| 1792 | } |
| 1793 | Slog.w(TAG, "Permission Denial: Accessing service " + r.name |
| 1794 | + " from pid=" + callingPid |
| 1795 | + ", uid=" + callingUid |
| 1796 | + " requires " + r.permission); |
| 1797 | return new ServiceLookupResult(null, r.permission); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1798 | } else if (r.permission != null && callingPackage != null) { |
| 1799 | final int opCode = AppOpsManager.permissionToOpCode(r.permission); |
| 1800 | if (opCode != AppOpsManager.OP_NONE && mAm.mAppOpsService.noteOperation( |
| 1801 | opCode, callingUid, callingPackage) != AppOpsManager.MODE_ALLOWED) { |
| 1802 | Slog.w(TAG, "Appop Denial: Accessing service " + r.name |
| 1803 | + " from pid=" + callingPid |
| 1804 | + ", uid=" + callingUid |
| 1805 | + " requires appop " + AppOpsManager.opToName(opCode)); |
| 1806 | return null; |
| 1807 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1808 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1809 | |
Ben Gruver | f5323fe | 2013-07-31 15:09:51 -0700 | [diff] [blame] | 1810 | if (!mAm.mIntentFirewall.checkService(r.name, service, callingUid, callingPid, |
| 1811 | resolvedType, r.appInfo)) { |
Ben Gruver | b622379 | 2013-07-29 16:35:40 -0700 | [diff] [blame] | 1812 | return null; |
| 1813 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1814 | return new ServiceLookupResult(r, null); |
| 1815 | } |
| 1816 | return null; |
| 1817 | } |
| 1818 | |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1819 | private final void bumpServiceExecutingLocked(ServiceRecord r, boolean fg, String why) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1820 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, ">>> EXECUTING " |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1821 | + why + " of " + r + " in app " + r.app); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1822 | else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG_SERVICE_EXECUTING, ">>> EXECUTING " |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1823 | + why + " of " + r.shortName); |
| 1824 | long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 1825 | if (r.executeNesting == 0) { |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1826 | r.executeFg = fg; |
Joe Onorato | 4eb64fd | 2016-03-21 15:30:09 -0700 | [diff] [blame] | 1827 | ServiceState stracker = r.getTracker(); |
Dianne Hackborn | bd754f4 | 2013-07-23 15:52:36 -0700 | [diff] [blame] | 1828 | if (stracker != null) { |
Dianne Hackborn | d293224 | 2013-08-05 18:18:42 -0700 | [diff] [blame] | 1829 | stracker.setExecuting(true, mAm.mProcessStats.getMemFactorLocked(), now); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1830 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 1831 | if (r.app != null) { |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 1832 | r.app.executingServices.add(r); |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1833 | r.app.execServicesFg |= fg; |
Dianne Hackborn | 2be0093 | 2013-09-22 16:46:00 -0700 | [diff] [blame] | 1834 | if (r.app.executingServices.size() == 1) { |
| 1835 | scheduleServiceTimeoutLocked(r.app); |
| 1836 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 1837 | } |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1838 | } else if (r.app != null && fg && !r.app.execServicesFg) { |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1839 | r.app.execServicesFg = true; |
Dianne Hackborn | 2be0093 | 2013-09-22 16:46:00 -0700 | [diff] [blame] | 1840 | scheduleServiceTimeoutLocked(r.app); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1841 | } |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1842 | r.executeFg |= fg; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1843 | r.executeNesting++; |
| 1844 | r.executingStart = now; |
| 1845 | } |
| 1846 | |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 1847 | private final boolean requestServiceBindingLocked(ServiceRecord r, IntentBindRecord i, |
| 1848 | boolean execInFg, boolean rebind) throws TransactionTooLargeException { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1849 | if (r.app == null || r.app.thread == null) { |
| 1850 | // If service is not currently running, can't yet bind. |
| 1851 | return false; |
| 1852 | } |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 1853 | if (DEBUG_SERVICE) Slog.d(TAG_SERVICE, "requestBind " + i + ": requested=" + i.requested |
| 1854 | + " rebind=" + rebind); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1855 | if ((!i.requested || rebind) && i.apps.size() > 0) { |
| 1856 | try { |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1857 | bumpServiceExecutingLocked(r, execInFg, "bind"); |
Dianne Hackborn | a413dc0 | 2013-07-12 12:02:55 -0700 | [diff] [blame] | 1858 | r.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_SERVICE); |
| 1859 | r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind, |
| 1860 | r.app.repProcState); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1861 | if (!rebind) { |
| 1862 | i.requested = true; |
| 1863 | } |
| 1864 | i.hasBound = true; |
| 1865 | i.doRebind = false; |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 1866 | } catch (TransactionTooLargeException e) { |
| 1867 | // Keep the executeNesting count accurate. |
| 1868 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Crashed while binding " + r, e); |
| 1869 | final boolean inDestroying = mDestroyingServices.contains(r); |
| 1870 | serviceDoneExecutingLocked(r, inDestroying, inDestroying); |
| 1871 | throw e; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1872 | } catch (RemoteException e) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1873 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Crashed while binding " + r); |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 1874 | // Keep the executeNesting count accurate. |
| 1875 | final boolean inDestroying = mDestroyingServices.contains(r); |
| 1876 | serviceDoneExecutingLocked(r, inDestroying, inDestroying); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1877 | return false; |
| 1878 | } |
| 1879 | } |
| 1880 | return true; |
| 1881 | } |
| 1882 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 1883 | private final boolean scheduleServiceRestartLocked(ServiceRecord r, boolean allowCancel) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1884 | boolean canceled = false; |
| 1885 | |
Fyodor Kupolov | 9b80b94 | 2016-06-16 16:29:05 -0700 | [diff] [blame] | 1886 | if (mAm.isShuttingDownLocked()) { |
| 1887 | Slog.w(TAG, "Not scheduling restart of crashed service " + r.shortName |
| 1888 | + " - system is shutting down"); |
| 1889 | return false; |
| 1890 | } |
| 1891 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 1892 | ServiceMap smap = getServiceMapLocked(r.userId); |
Dianne Hackborn | d6f5b62 | 2013-11-11 17:25:37 -0800 | [diff] [blame] | 1893 | if (smap.mServicesByName.get(r.name) != r) { |
| 1894 | ServiceRecord cur = smap.mServicesByName.get(r.name); |
| 1895 | Slog.wtf(TAG, "Attempting to schedule restart of " + r |
| 1896 | + " when found in map: " + cur); |
| 1897 | return false; |
| 1898 | } |
| 1899 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1900 | final long now = SystemClock.uptimeMillis(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1901 | |
| 1902 | if ((r.serviceInfo.applicationInfo.flags |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1903 | &ApplicationInfo.FLAG_PERSISTENT) == 0) { |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 1904 | long minDuration = mAm.mConstants.SERVICE_RESTART_DURATION; |
| 1905 | long resetTime = mAm.mConstants.SERVICE_RESET_RUN_DURATION; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1906 | |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1907 | // Any delivered but not yet finished starts should be put back |
| 1908 | // on the pending list. |
| 1909 | final int N = r.deliveredStarts.size(); |
| 1910 | if (N > 0) { |
| 1911 | for (int i=N-1; i>=0; i--) { |
| 1912 | ServiceRecord.StartItem si = r.deliveredStarts.get(i); |
| 1913 | si.removeUriPermissionsLocked(); |
| 1914 | if (si.intent == null) { |
| 1915 | // We'll generate this again if needed. |
| 1916 | } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT |
| 1917 | && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) { |
| 1918 | r.pendingStarts.add(0, si); |
| 1919 | long dur = SystemClock.uptimeMillis() - si.deliveredTime; |
| 1920 | dur *= 2; |
| 1921 | if (minDuration < dur) minDuration = dur; |
| 1922 | if (resetTime < dur) resetTime = dur; |
| 1923 | } else { |
| 1924 | Slog.w(TAG, "Canceling start item " + si.intent + " in service " |
| 1925 | + r.name); |
| 1926 | canceled = true; |
| 1927 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1928 | } |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1929 | r.deliveredStarts.clear(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1930 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1931 | |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1932 | r.totalRestartCount++; |
| 1933 | if (r.restartDelay == 0) { |
| 1934 | r.restartCount++; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1935 | r.restartDelay = minDuration; |
Amith Yamasani | b0c8a88 | 2017-08-28 09:36:42 -0700 | [diff] [blame] | 1936 | } else if (r.crashCount > 1) { |
| 1937 | r.restartDelay = mAm.mConstants.BOUND_SERVICE_CRASH_RESTART_DURATION |
| 1938 | * (r.crashCount - 1); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1939 | } else { |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1940 | // If it has been a "reasonably long time" since the service |
| 1941 | // was started, then reset our restart duration back to |
| 1942 | // the beginning, so we don't infinitely increase the duration |
| 1943 | // on a service that just occasionally gets killed (which is |
| 1944 | // a normal case, due to process being killed to reclaim memory). |
| 1945 | if (now > (r.restartTime+resetTime)) { |
| 1946 | r.restartCount = 1; |
| 1947 | r.restartDelay = minDuration; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1948 | } else { |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 1949 | r.restartDelay *= mAm.mConstants.SERVICE_RESTART_DURATION_FACTOR; |
Dianne Hackborn | 7b49272 | 2013-11-01 09:58:45 -0700 | [diff] [blame] | 1950 | if (r.restartDelay < minDuration) { |
| 1951 | r.restartDelay = minDuration; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1952 | } |
| 1953 | } |
| 1954 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1955 | |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1956 | r.nextRestartTime = now + r.restartDelay; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1957 | |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1958 | // Make sure that we don't end up restarting a bunch of services |
| 1959 | // all at the same time. |
| 1960 | boolean repeat; |
| 1961 | do { |
| 1962 | repeat = false; |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 1963 | final long restartTimeBetween = mAm.mConstants.SERVICE_MIN_RESTART_TIME_BETWEEN; |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1964 | for (int i=mRestartingServices.size()-1; i>=0; i--) { |
| 1965 | ServiceRecord r2 = mRestartingServices.get(i); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 1966 | if (r2 != r && r.nextRestartTime >= (r2.nextRestartTime-restartTimeBetween) |
| 1967 | && r.nextRestartTime < (r2.nextRestartTime+restartTimeBetween)) { |
| 1968 | r.nextRestartTime = r2.nextRestartTime + restartTimeBetween; |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1969 | r.restartDelay = r.nextRestartTime - now; |
| 1970 | repeat = true; |
| 1971 | break; |
| 1972 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1973 | } |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1974 | } while (repeat); |
| 1975 | |
| 1976 | } else { |
Dianne Hackborn | 7b49272 | 2013-11-01 09:58:45 -0700 | [diff] [blame] | 1977 | // Persistent processes are immediately restarted, so there is no |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 1978 | // reason to hold of on restarting their services. |
| 1979 | r.totalRestartCount++; |
| 1980 | r.restartCount = 0; |
| 1981 | r.restartDelay = 0; |
| 1982 | r.nextRestartTime = now; |
| 1983 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1984 | |
| 1985 | if (!mRestartingServices.contains(r)) { |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 1986 | r.createdFromFg = false; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1987 | mRestartingServices.add(r); |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 1988 | r.makeRestarting(mAm.mProcessStats.getMemFactorLocked(), now); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 1991 | cancelForegroundNotificationLocked(r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 1992 | |
| 1993 | mAm.mHandler.removeCallbacks(r.restarter); |
| 1994 | mAm.mHandler.postAtTime(r.restarter, r.nextRestartTime); |
| 1995 | r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay; |
| 1996 | Slog.w(TAG, "Scheduling restart of crashed service " |
| 1997 | + r.shortName + " in " + r.restartDelay + "ms"); |
| 1998 | EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1999 | r.userId, r.shortName, r.restartDelay); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2000 | |
| 2001 | return canceled; |
| 2002 | } |
| 2003 | |
| 2004 | final void performServiceRestartLocked(ServiceRecord r) { |
| 2005 | if (!mRestartingServices.contains(r)) { |
| 2006 | return; |
| 2007 | } |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 2008 | if (!isServiceNeededLocked(r, false, false)) { |
Dianne Hackborn | 865907d | 2015-10-21 17:12:53 -0700 | [diff] [blame] | 2009 | // Paranoia: is this service actually needed? In theory a service that is not |
| 2010 | // needed should never remain on the restart list. In practice... well, there |
| 2011 | // have been bugs where this happens, and bad things happen because the process |
| 2012 | // ends up just being cached, so quickly killed, then restarted again and again. |
| 2013 | // Let's not let that happen. |
| 2014 | Slog.wtf(TAG, "Restarting service that is not needed: " + r); |
| 2015 | return; |
| 2016 | } |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 2017 | try { |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 2018 | bringUpServiceLocked(r, r.intent.getIntent().getFlags(), r.createdFromFg, true, false); |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 2019 | } catch (TransactionTooLargeException e) { |
| 2020 | // Ignore, it's been logged and nothing upstack cares. |
| 2021 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Dianne Hackborn | d6f5b62 | 2013-11-11 17:25:37 -0800 | [diff] [blame] | 2024 | private final boolean unscheduleServiceRestartLocked(ServiceRecord r, int callingUid, |
| 2025 | boolean force) { |
| 2026 | if (!force && r.restartDelay == 0) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2027 | return false; |
| 2028 | } |
Dianne Hackborn | 7b49272 | 2013-11-01 09:58:45 -0700 | [diff] [blame] | 2029 | // Remove from the restarting list; if the service is currently on the |
| 2030 | // restarting list, or the call is coming from another app, then this |
| 2031 | // service has become of much more interest so we reset the restart interval. |
| 2032 | boolean removed = mRestartingServices.remove(r); |
| 2033 | if (removed || callingUid != r.appInfo.uid) { |
| 2034 | r.resetRestartCounter(); |
| 2035 | } |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 2036 | if (removed) { |
Dianne Hackborn | d6f5b62 | 2013-11-11 17:25:37 -0800 | [diff] [blame] | 2037 | clearRestartingIfNeededLocked(r); |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 2038 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2039 | mAm.mHandler.removeCallbacks(r.restarter); |
| 2040 | return true; |
| 2041 | } |
| 2042 | |
Dianne Hackborn | d6f5b62 | 2013-11-11 17:25:37 -0800 | [diff] [blame] | 2043 | private void clearRestartingIfNeededLocked(ServiceRecord r) { |
| 2044 | if (r.restartTracker != null) { |
| 2045 | // If this is the last restarting record with this tracker, then clear |
| 2046 | // the tracker's restarting state. |
| 2047 | boolean stillTracking = false; |
| 2048 | for (int i=mRestartingServices.size()-1; i>=0; i--) { |
| 2049 | if (mRestartingServices.get(i).restartTracker == r.restartTracker) { |
| 2050 | stillTracking = true; |
| 2051 | break; |
| 2052 | } |
| 2053 | } |
| 2054 | if (!stillTracking) { |
| 2055 | r.restartTracker.setRestarting(false, mAm.mProcessStats.getMemFactorLocked(), |
| 2056 | SystemClock.uptimeMillis()); |
| 2057 | r.restartTracker = null; |
| 2058 | } |
| 2059 | } |
| 2060 | } |
| 2061 | |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 2062 | private String bringUpServiceLocked(ServiceRecord r, int intentFlags, boolean execInFg, |
| 2063 | boolean whileRestarting, boolean permissionsReviewRequired) |
| 2064 | throws TransactionTooLargeException { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2065 | //Slog.i(TAG, "Bring up service:"); |
| 2066 | //r.dump(" "); |
| 2067 | |
| 2068 | if (r.app != null && r.app.thread != null) { |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2069 | sendServiceArgsLocked(r, execInFg, false); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 2070 | return null; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2071 | } |
| 2072 | |
Sudheer Shanka | 03d0f81 | 2017-04-20 16:57:50 -0700 | [diff] [blame] | 2073 | if (!whileRestarting && mRestartingServices.contains(r)) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2074 | // If waiting for a restart, then do nothing. |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 2075 | return null; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 2078 | if (DEBUG_SERVICE) { |
| 2079 | Slog.v(TAG_SERVICE, "Bringing up " + r + " " + r.intent + " fg=" + r.fgRequired); |
| 2080 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2081 | |
| 2082 | // We are now bringing the service up, so no longer in the |
| 2083 | // restarting state. |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 2084 | if (mRestartingServices.remove(r)) { |
Dianne Hackborn | d6f5b62 | 2013-11-11 17:25:37 -0800 | [diff] [blame] | 2085 | clearRestartingIfNeededLocked(r); |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 2086 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2087 | |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2088 | // Make sure this service is no longer considered delayed, we are starting it now. |
| 2089 | if (r.delayed) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2090 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "REM FR DELAY LIST (bring up): " + r); |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 2091 | getServiceMapLocked(r.userId).mDelayedStartList.remove(r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2092 | r.delayed = false; |
| 2093 | } |
| 2094 | |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2095 | // Make sure that the user who owns this service is started. If not, |
| 2096 | // we don't want to allow it to run. |
Fyodor Kupolov | 610acda | 2015-10-19 18:44:07 -0700 | [diff] [blame] | 2097 | if (!mAm.mUserController.hasStartedUserState(r.userId)) { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 2098 | String msg = "Unable to launch app " |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2099 | + r.appInfo.packageName + "/" |
| 2100 | + r.appInfo.uid + " for service " |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 2101 | + r.intent.getIntent() + ": user " + r.userId + " is stopped"; |
| 2102 | Slog.w(TAG, msg); |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2103 | bringDownServiceLocked(r); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 2104 | return msg; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2107 | // Service is now being launched, its package can't be stopped. |
| 2108 | try { |
| 2109 | AppGlobals.getPackageManager().setPackageStoppedState( |
| 2110 | r.packageName, false, r.userId); |
| 2111 | } catch (RemoteException e) { |
| 2112 | } catch (IllegalArgumentException e) { |
| 2113 | Slog.w(TAG, "Failed trying to unstop package " |
| 2114 | + r.packageName + ": " + e); |
| 2115 | } |
| 2116 | |
| 2117 | final boolean isolated = (r.serviceInfo.flags&ServiceInfo.FLAG_ISOLATED_PROCESS) != 0; |
| 2118 | final String procName = r.processName; |
Robert Sesek | c5f8664 | 2016-11-04 10:20:38 -0400 | [diff] [blame] | 2119 | String hostingType = "service"; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2120 | ProcessRecord app; |
| 2121 | |
| 2122 | if (!isolated) { |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 2123 | app = mAm.getProcessRecordLocked(procName, r.appInfo.uid, false); |
| 2124 | if (DEBUG_MU) Slog.v(TAG_MU, "bringUpServiceLocked: appInfo.uid=" + r.appInfo.uid |
| 2125 | + " app=" + app); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2126 | if (app != null && app.thread != null) { |
| 2127 | try { |
Dianne Hackborn | f7097a5 | 2014-05-13 09:56:14 -0700 | [diff] [blame] | 2128 | app.addPackage(r.appInfo.packageName, r.appInfo.versionCode, mAm.mProcessStats); |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2129 | realStartServiceLocked(r, app, execInFg); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 2130 | return null; |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 2131 | } catch (TransactionTooLargeException e) { |
| 2132 | throw e; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2133 | } catch (RemoteException e) { |
| 2134 | Slog.w(TAG, "Exception when starting service " + r.shortName, e); |
| 2135 | } |
| 2136 | |
| 2137 | // If a dead object exception was thrown -- fall through to |
| 2138 | // restart the application. |
| 2139 | } |
| 2140 | } else { |
| 2141 | // If this service runs in an isolated process, then each time |
| 2142 | // we call startProcessLocked() we will get a new isolated |
| 2143 | // process, starting another process if we are currently waiting |
| 2144 | // for a previous process to come up. To deal with this, we store |
| 2145 | // in the service any current isolated process it is running in or |
| 2146 | // waiting to have come up. |
| 2147 | app = r.isolatedProc; |
Robert Sesek | c5f8664 | 2016-11-04 10:20:38 -0400 | [diff] [blame] | 2148 | if (WebViewZygote.isMultiprocessEnabled() |
| 2149 | && r.serviceInfo.packageName.equals(WebViewZygote.getPackageName())) { |
| 2150 | hostingType = "webview_service"; |
| 2151 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | // Not running -- get it started, and enqueue this service record |
| 2155 | // to be executed when the app comes up. |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 2156 | if (app == null && !permissionsReviewRequired) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2157 | if ((app=mAm.startProcessLocked(procName, r.appInfo, true, intentFlags, |
Robert Sesek | c5f8664 | 2016-11-04 10:20:38 -0400 | [diff] [blame] | 2158 | hostingType, r.name, false, isolated, false)) == null) { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 2159 | String msg = "Unable to launch app " |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2160 | + r.appInfo.packageName + "/" |
| 2161 | + r.appInfo.uid + " for service " |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 2162 | + r.intent.getIntent() + ": process is bad"; |
| 2163 | Slog.w(TAG, msg); |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2164 | bringDownServiceLocked(r); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 2165 | return msg; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2166 | } |
| 2167 | if (isolated) { |
| 2168 | r.isolatedProc = app; |
| 2169 | } |
| 2170 | } |
| 2171 | |
Christopher Tate | 1536cc2 | 2017-09-18 16:43:30 -0700 | [diff] [blame] | 2172 | if (r.fgRequired) { |
| 2173 | if (DEBUG_FOREGROUND_SERVICE) { |
| 2174 | Slog.v(TAG, "Whitelisting " + UserHandle.formatUid(r.appInfo.uid) |
| 2175 | + " for fg-service launch"); |
| 2176 | } |
| 2177 | mAm.tempWhitelistUidLocked(r.appInfo.uid, |
| 2178 | SERVICE_START_FOREGROUND_TIMEOUT, "fg-service-launch"); |
| 2179 | } |
| 2180 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2181 | if (!mPendingServices.contains(r)) { |
| 2182 | mPendingServices.add(r); |
| 2183 | } |
| 2184 | |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2185 | if (r.delayedStop) { |
| 2186 | // Oh and hey we've already been asked to stop! |
| 2187 | r.delayedStop = false; |
| 2188 | if (r.startRequested) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2189 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, |
| 2190 | "Applying delayed stop (in bring up): " + r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2191 | stopServiceLocked(r); |
| 2192 | } |
| 2193 | } |
| 2194 | |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 2195 | return null; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 2198 | private final void requestServiceBindingsLocked(ServiceRecord r, boolean execInFg) |
| 2199 | throws TransactionTooLargeException { |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2200 | for (int i=r.bindings.size()-1; i>=0; i--) { |
| 2201 | IntentBindRecord ibr = r.bindings.valueAt(i); |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2202 | if (!requestServiceBindingLocked(r, ibr, execInFg, false)) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2203 | break; |
| 2204 | } |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | private final void realStartServiceLocked(ServiceRecord r, |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2209 | ProcessRecord app, boolean execInFg) throws RemoteException { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2210 | if (app.thread == null) { |
| 2211 | throw new RemoteException(); |
| 2212 | } |
| 2213 | if (DEBUG_MU) |
| 2214 | Slog.v(TAG_MU, "realStartServiceLocked, ServiceRecord.uid = " + r.appInfo.uid |
| 2215 | + ", ProcessRecord.uid = " + app.uid); |
| 2216 | r.app = app; |
| 2217 | r.restartTime = r.lastActivity = SystemClock.uptimeMillis(); |
| 2218 | |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 2219 | final boolean newService = app.services.add(r); |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2220 | bumpServiceExecutingLocked(r, execInFg, "create"); |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 2221 | mAm.updateLruProcessLocked(app, false, null); |
Makoto Onuki | 0f42129 | 2017-04-28 15:03:33 -0700 | [diff] [blame] | 2222 | updateServiceForegroundLocked(r.app, /* oomAdj= */ false); |
| 2223 | mAm.updateOomAdjLocked(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2224 | |
| 2225 | boolean created = false; |
| 2226 | try { |
Dianne Hackborn | f85e7af | 2014-10-14 10:43:43 -0700 | [diff] [blame] | 2227 | if (LOG_SERVICE_START_STOP) { |
Dianne Hackborn | ab2df06 | 2015-01-07 13:43:13 -0800 | [diff] [blame] | 2228 | String nameTerm; |
| 2229 | int lastPeriod = r.shortName.lastIndexOf('.'); |
| 2230 | nameTerm = lastPeriod >= 0 ? r.shortName.substring(lastPeriod) : r.shortName; |
Dianne Hackborn | f85e7af | 2014-10-14 10:43:43 -0700 | [diff] [blame] | 2231 | EventLogTags.writeAmCreateService( |
| 2232 | r.userId, System.identityHashCode(r), nameTerm, r.app.uid, r.app.pid); |
| 2233 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2234 | synchronized (r.stats.getBatteryStats()) { |
| 2235 | r.stats.startLaunchedLocked(); |
| 2236 | } |
Brian Carlstrom | ca82e61 | 2016-04-19 23:16:08 -0700 | [diff] [blame] | 2237 | mAm.notifyPackageUse(r.serviceInfo.packageName, |
| 2238 | PackageManager.NOTIFY_PACKAGE_USE_SERVICE); |
Dianne Hackborn | a413dc0 | 2013-07-12 12:02:55 -0700 | [diff] [blame] | 2239 | app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_SERVICE); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2240 | app.thread.scheduleCreateService(r, r.serviceInfo, |
Dianne Hackborn | a413dc0 | 2013-07-12 12:02:55 -0700 | [diff] [blame] | 2241 | mAm.compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo), |
| 2242 | app.repProcState); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2243 | r.postNotification(); |
| 2244 | created = true; |
Craig Mautner | 4a8dddbf | 2014-08-13 10:49:26 -0700 | [diff] [blame] | 2245 | } catch (DeadObjectException e) { |
| 2246 | Slog.w(TAG, "Application dead when creating service " + r); |
| 2247 | mAm.appDiedLocked(app); |
Wale Ogunwale | bfac468 | 2015-04-08 14:33:21 -0700 | [diff] [blame] | 2248 | throw e; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2249 | } finally { |
| 2250 | if (!created) { |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 2251 | // Keep the executeNesting count accurate. |
| 2252 | final boolean inDestroying = mDestroyingServices.contains(r); |
| 2253 | serviceDoneExecutingLocked(r, inDestroying, inDestroying); |
| 2254 | |
| 2255 | // Cleanup. |
| 2256 | if (newService) { |
| 2257 | app.services.remove(r); |
| 2258 | r.app = null; |
| 2259 | } |
| 2260 | |
| 2261 | // Retry. |
| 2262 | if (!inDestroying) { |
| 2263 | scheduleServiceRestartLocked(r, false); |
| 2264 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2265 | } |
| 2266 | } |
| 2267 | |
Dianne Hackborn | a590d2b | 2016-06-27 15:07:18 -0700 | [diff] [blame] | 2268 | if (r.whitelistManager) { |
| 2269 | app.whitelistManager = true; |
| 2270 | } |
| 2271 | |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2272 | requestServiceBindingsLocked(r, execInFg); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2273 | |
Dianne Hackborn | 465fa39 | 2014-09-14 14:21:18 -0700 | [diff] [blame] | 2274 | updateServiceClientActivitiesLocked(app, null, true); |
| 2275 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2276 | // If the service is in the started state, and there are no |
| 2277 | // pending arguments, then fake up one so its onStartCommand() will |
| 2278 | // be called. |
| 2279 | if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) { |
| 2280 | r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(), |
Todd Kennedy | 51b3aac | 2017-03-30 17:50:42 -0700 | [diff] [blame] | 2281 | null, null, 0)); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2284 | sendServiceArgsLocked(r, execInFg, true); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2285 | |
| 2286 | if (r.delayed) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2287 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "REM FR DELAY LIST (new proc): " + r); |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 2288 | getServiceMapLocked(r.userId).mDelayedStartList.remove(r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2289 | r.delayed = false; |
| 2290 | } |
| 2291 | |
| 2292 | if (r.delayedStop) { |
| 2293 | // Oh and hey we've already been asked to stop! |
| 2294 | r.delayedStop = false; |
| 2295 | if (r.startRequested) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2296 | if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, |
| 2297 | "Applying delayed stop (from start): " + r); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2298 | stopServiceLocked(r); |
| 2299 | } |
| 2300 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2301 | } |
| 2302 | |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2303 | private final void sendServiceArgsLocked(ServiceRecord r, boolean execInFg, |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 2304 | boolean oomAdjusted) throws TransactionTooLargeException { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2305 | final int N = r.pendingStarts.size(); |
| 2306 | if (N == 0) { |
| 2307 | return; |
| 2308 | } |
| 2309 | |
Dianne Hackborn | 3f7c9f2 | 2017-04-04 15:36:33 -0700 | [diff] [blame] | 2310 | ArrayList<ServiceStartArgs> args = new ArrayList<>(); |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 2311 | |
Dianne Hackborn | 3f7c9f2 | 2017-04-04 15:36:33 -0700 | [diff] [blame] | 2312 | while (r.pendingStarts.size() > 0) { |
| 2313 | ServiceRecord.StartItem si = r.pendingStarts.remove(0); |
| 2314 | if (DEBUG_SERVICE) { |
| 2315 | Slog.v(TAG_SERVICE, "Sending arguments to: " |
| 2316 | + r + " " + r.intent + " args=" + si.intent); |
| 2317 | } |
| 2318 | if (si.intent == null && N > 1) { |
| 2319 | // If somehow we got a dummy null intent in the middle, |
| 2320 | // then skip it. DO NOT skip a null intent when it is |
| 2321 | // the only one in the list -- this is to support the |
| 2322 | // onStartCommand(null) case. |
| 2323 | continue; |
| 2324 | } |
| 2325 | si.deliveredTime = SystemClock.uptimeMillis(); |
| 2326 | r.deliveredStarts.add(si); |
| 2327 | si.deliveryCount++; |
| 2328 | if (si.neededGrants != null) { |
| 2329 | mAm.grantUriPermissionUncheckedFromIntentLocked(si.neededGrants, |
| 2330 | si.getUriPermissionsLocked()); |
| 2331 | } |
| 2332 | mAm.grantEphemeralAccessLocked(r.userId, si.intent, |
| 2333 | r.appInfo.uid, UserHandle.getAppId(si.callingId)); |
| 2334 | bumpServiceExecutingLocked(r, execInFg, "start"); |
| 2335 | if (!oomAdjusted) { |
| 2336 | oomAdjusted = true; |
Amith Yamasani | 385c3ad | 2017-05-04 14:27:11 -0700 | [diff] [blame] | 2337 | mAm.updateOomAdjLocked(r.app, true); |
Dianne Hackborn | 3f7c9f2 | 2017-04-04 15:36:33 -0700 | [diff] [blame] | 2338 | } |
| 2339 | if (r.fgRequired && !r.fgWaiting) { |
| 2340 | if (!r.isForeground) { |
| 2341 | if (DEBUG_BACKGROUND_CHECK) { |
| 2342 | Slog.i(TAG, "Launched service must call startForeground() within timeout: " + r); |
| 2343 | } |
| 2344 | scheduleServiceForegroundTransitionTimeoutLocked(r); |
| 2345 | } else { |
| 2346 | if (DEBUG_BACKGROUND_CHECK) { |
| 2347 | Slog.i(TAG, "Service already foreground; no new timeout: " + r); |
| 2348 | } |
| 2349 | r.fgRequired = false; |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 2350 | } |
Dianne Hackborn | 3f7c9f2 | 2017-04-04 15:36:33 -0700 | [diff] [blame] | 2351 | } |
| 2352 | int flags = 0; |
| 2353 | if (si.deliveryCount > 1) { |
| 2354 | flags |= Service.START_FLAG_RETRY; |
| 2355 | } |
| 2356 | if (si.doneExecutingCount > 0) { |
| 2357 | flags |= Service.START_FLAG_REDELIVERY; |
| 2358 | } |
| 2359 | args.add(new ServiceStartArgs(si.taskRemoved, si.id, flags, si.intent)); |
| 2360 | } |
| 2361 | |
| 2362 | ParceledListSlice<ServiceStartArgs> slice = new ParceledListSlice<>(args); |
| 2363 | slice.setInlineCountLimit(4); |
| 2364 | Exception caughtException = null; |
| 2365 | try { |
| 2366 | r.app.thread.scheduleServiceArgs(r, slice); |
| 2367 | } catch (TransactionTooLargeException e) { |
| 2368 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Transaction too large for " + args.size() |
| 2369 | + " args, first: " + args.get(0).args); |
| 2370 | Slog.w(TAG, "Failed delivering service starts", e); |
| 2371 | caughtException = e; |
| 2372 | } catch (RemoteException e) { |
| 2373 | // Remote process gone... we'll let the normal cleanup take care of this. |
| 2374 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Crashed while sending args: " + r); |
| 2375 | Slog.w(TAG, "Failed delivering service starts", e); |
| 2376 | caughtException = e; |
| 2377 | } catch (Exception e) { |
| 2378 | Slog.w(TAG, "Unexpected exception", e); |
| 2379 | caughtException = e; |
| 2380 | } |
| 2381 | |
| 2382 | if (caughtException != null) { |
| 2383 | // Keep nesting count correct |
| 2384 | final boolean inDestroying = mDestroyingServices.contains(r); |
| 2385 | for (int i = 0; i < args.size(); i++) { |
| 2386 | serviceDoneExecutingLocked(r, inDestroying, inDestroying); |
| 2387 | } |
| 2388 | if (caughtException instanceof TransactionTooLargeException) { |
| 2389 | throw (TransactionTooLargeException)caughtException; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2390 | } |
| 2391 | } |
| 2392 | } |
| 2393 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 2394 | private final boolean isServiceNeededLocked(ServiceRecord r, boolean knowConn, |
| 2395 | boolean hasConn) { |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2396 | // Are we still explicitly being asked to run? |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2397 | if (r.startRequested) { |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 2398 | return true; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2399 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2400 | |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 2401 | // Is someone still bound to us keeping us running? |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2402 | if (!knowConn) { |
| 2403 | hasConn = r.hasAutoCreateConnections(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2404 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2405 | if (hasConn) { |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 2406 | return true; |
| 2407 | } |
| 2408 | |
| 2409 | return false; |
| 2410 | } |
| 2411 | |
| 2412 | private final void bringDownServiceIfNeededLocked(ServiceRecord r, boolean knowConn, |
| 2413 | boolean hasConn) { |
| 2414 | //Slog.i(TAG, "Bring down service:"); |
| 2415 | //r.dump(" "); |
| 2416 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 2417 | if (isServiceNeededLocked(r, knowConn, hasConn)) { |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2418 | return; |
| 2419 | } |
| 2420 | |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2421 | // Are we in the process of launching? |
| 2422 | if (mPendingServices.contains(r)) { |
| 2423 | return; |
| 2424 | } |
| 2425 | |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2426 | bringDownServiceLocked(r); |
| 2427 | } |
| 2428 | |
| 2429 | private final void bringDownServiceLocked(ServiceRecord r) { |
| 2430 | //Slog.i(TAG, "Bring down service:"); |
| 2431 | //r.dump(" "); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2432 | |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2433 | // Report to all of the connections that the service is no longer |
| 2434 | // available. |
| 2435 | for (int conni=r.connections.size()-1; conni>=0; conni--) { |
| 2436 | ArrayList<ConnectionRecord> c = r.connections.valueAt(conni); |
| 2437 | for (int i=0; i<c.size(); i++) { |
| 2438 | ConnectionRecord cr = c.get(i); |
| 2439 | // There is still a connection to the service that is |
| 2440 | // being brought down. Mark it as dead. |
| 2441 | cr.serviceDead = true; |
| 2442 | try { |
Dianne Hackborn | 9484603 | 2017-03-31 17:55:23 -0700 | [diff] [blame] | 2443 | cr.conn.connected(r.name, null, true); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2444 | } catch (Exception e) { |
| 2445 | Slog.w(TAG, "Failure disconnecting service " + r.name + |
| 2446 | " to connection " + c.get(i).conn.asBinder() + |
| 2447 | " (in " + c.get(i).binding.client.processName + ")", e); |
| 2448 | } |
| 2449 | } |
| 2450 | } |
| 2451 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2452 | // Tell the service that it has been unbound. |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2453 | if (r.app != null && r.app.thread != null) { |
| 2454 | for (int i=r.bindings.size()-1; i>=0; i--) { |
| 2455 | IntentBindRecord ibr = r.bindings.valueAt(i); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2456 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Bringing down binding " + ibr |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2457 | + ": hasBound=" + ibr.hasBound); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 2458 | if (ibr.hasBound) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2459 | try { |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2460 | bumpServiceExecutingLocked(r, false, "bring down unbind"); |
Amith Yamasani | 385c3ad | 2017-05-04 14:27:11 -0700 | [diff] [blame] | 2461 | mAm.updateOomAdjLocked(r.app, true); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2462 | ibr.hasBound = false; |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 2463 | ibr.requested = false; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2464 | r.app.thread.scheduleUnbindService(r, |
| 2465 | ibr.intent.getIntent()); |
| 2466 | } catch (Exception e) { |
| 2467 | Slog.w(TAG, "Exception when unbinding service " |
| 2468 | + r.shortName, e); |
Dianne Hackborn | 878deb3 | 2013-10-14 16:55:09 -0700 | [diff] [blame] | 2469 | serviceProcessGoneLocked(r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2470 | } |
| 2471 | } |
| 2472 | } |
| 2473 | } |
| 2474 | |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 2475 | // Check to see if the service had been started as foreground, but being |
| 2476 | // brought down before actually showing a notification. That is not allowed. |
| 2477 | if (r.fgRequired) { |
| 2478 | Slog.w(TAG_SERVICE, "Bringing down service while still waiting for start foreground: " |
| 2479 | + r); |
| 2480 | r.fgRequired = false; |
| 2481 | r.fgWaiting = false; |
| 2482 | mAm.mHandler.removeMessages( |
| 2483 | ActivityManagerService.SERVICE_FOREGROUND_TIMEOUT_MSG, r); |
| 2484 | if (r.app != null) { |
| 2485 | Message msg = mAm.mHandler.obtainMessage( |
| 2486 | ActivityManagerService.SERVICE_FOREGROUND_CRASH_MSG); |
| 2487 | msg.obj = r.app; |
| 2488 | mAm.mHandler.sendMessage(msg); |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | if (DEBUG_SERVICE) { |
| 2493 | RuntimeException here = new RuntimeException(); |
| 2494 | here.fillInStackTrace(); |
| 2495 | Slog.v(TAG_SERVICE, "Bringing down " + r + " " + r.intent, here); |
| 2496 | } |
Craig Mautner | 66c4a82 | 2015-01-16 12:48:16 -0800 | [diff] [blame] | 2497 | r.destroyTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | f85e7af | 2014-10-14 10:43:43 -0700 | [diff] [blame] | 2498 | if (LOG_SERVICE_START_STOP) { |
| 2499 | EventLogTags.writeAmDestroyService( |
| 2500 | r.userId, System.identityHashCode(r), (r.app != null) ? r.app.pid : -1); |
| 2501 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2502 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 2503 | final ServiceMap smap = getServiceMapLocked(r.userId); |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 2504 | ServiceRecord found = smap.mServicesByName.remove(r.name); |
Makoto Onuki | 1b9dfda | 2017-04-28 16:27:15 -0700 | [diff] [blame] | 2505 | |
| 2506 | // Note when this method is called by bringUpServiceLocked(), the service is not found |
| 2507 | // in mServicesByName and found will be null. |
| 2508 | if (found != null && found != r) { |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 2509 | // This is not actually the service we think is running... this should not happen, |
| 2510 | // but if it does, fail hard. |
| 2511 | smap.mServicesByName.put(r.name, found); |
| 2512 | throw new IllegalStateException("Bringing down " + r + " but actually running " |
| 2513 | + found); |
| 2514 | } |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2515 | smap.mServicesByIntent.remove(r.intent); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2516 | r.totalRestartCount = 0; |
Dianne Hackborn | d6f5b62 | 2013-11-11 17:25:37 -0800 | [diff] [blame] | 2517 | unscheduleServiceRestartLocked(r, 0, true); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2518 | |
| 2519 | // Also make sure it is not on the pending list. |
Dianne Hackborn | d6f5b62 | 2013-11-11 17:25:37 -0800 | [diff] [blame] | 2520 | for (int i=mPendingServices.size()-1; i>=0; i--) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2521 | if (mPendingServices.get(i) == r) { |
| 2522 | mPendingServices.remove(i); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2523 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Removed pending: " + r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2524 | } |
| 2525 | } |
| 2526 | |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 2527 | cancelForegroundNotificationLocked(r); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 2528 | if (r.isForeground) { |
| 2529 | decActiveForegroundAppLocked(smap, r); |
| 2530 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2531 | r.isForeground = false; |
| 2532 | r.foregroundId = 0; |
| 2533 | r.foregroundNoti = null; |
| 2534 | |
| 2535 | // Clear start entries. |
| 2536 | r.clearDeliveredStartsLocked(); |
| 2537 | r.pendingStarts.clear(); |
| 2538 | |
| 2539 | if (r.app != null) { |
| 2540 | synchronized (r.stats.getBatteryStats()) { |
| 2541 | r.stats.stopLaunchedLocked(); |
| 2542 | } |
| 2543 | r.app.services.remove(r); |
Dianne Hackborn | a590d2b | 2016-06-27 15:07:18 -0700 | [diff] [blame] | 2544 | if (r.whitelistManager) { |
| 2545 | updateWhitelistManagerLocked(r.app); |
| 2546 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2547 | if (r.app.thread != null) { |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 2548 | updateServiceForegroundLocked(r.app, false); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2549 | try { |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 2550 | bumpServiceExecutingLocked(r, false, "destroy"); |
| 2551 | mDestroyingServices.add(r); |
Dianne Hackborn | 455625e | 2015-01-21 09:55:13 -0800 | [diff] [blame] | 2552 | r.destroying = true; |
Amith Yamasani | 385c3ad | 2017-05-04 14:27:11 -0700 | [diff] [blame] | 2553 | mAm.updateOomAdjLocked(r.app, true); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2554 | r.app.thread.scheduleStopService(r); |
| 2555 | } catch (Exception e) { |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 2556 | Slog.w(TAG, "Exception when destroying service " |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2557 | + r.shortName, e); |
Dianne Hackborn | 878deb3 | 2013-10-14 16:55:09 -0700 | [diff] [blame] | 2558 | serviceProcessGoneLocked(r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2559 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2560 | } else { |
| 2561 | if (DEBUG_SERVICE) Slog.v( |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2562 | TAG_SERVICE, "Removed service that has no process: " + r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2563 | } |
| 2564 | } else { |
| 2565 | if (DEBUG_SERVICE) Slog.v( |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2566 | TAG_SERVICE, "Removed service that is not running: " + r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | if (r.bindings.size() > 0) { |
| 2570 | r.bindings.clear(); |
| 2571 | } |
| 2572 | |
| 2573 | if (r.restarter instanceof ServiceRestarter) { |
| 2574 | ((ServiceRestarter)r.restarter).setService(null); |
| 2575 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2576 | |
Dianne Hackborn | d293224 | 2013-08-05 18:18:42 -0700 | [diff] [blame] | 2577 | int memFactor = mAm.mProcessStats.getMemFactorLocked(); |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2578 | long now = SystemClock.uptimeMillis(); |
| 2579 | if (r.tracker != null) { |
| 2580 | r.tracker.setStarted(false, memFactor, now); |
| 2581 | r.tracker.setBound(false, memFactor, now); |
Dianne Hackborn | bd754f4 | 2013-07-23 15:52:36 -0700 | [diff] [blame] | 2582 | if (r.executeNesting == 0) { |
Dianne Hackborn | 878deb3 | 2013-10-14 16:55:09 -0700 | [diff] [blame] | 2583 | r.tracker.clearCurrentOwner(r, false); |
Dianne Hackborn | bd754f4 | 2013-07-23 15:52:36 -0700 | [diff] [blame] | 2584 | r.tracker = null; |
| 2585 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2586 | } |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2587 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 2588 | smap.ensureNotStartingBackgroundLocked(r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | void removeConnectionLocked( |
| 2592 | ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) { |
| 2593 | IBinder binder = c.conn.asBinder(); |
| 2594 | AppBindRecord b = c.binding; |
| 2595 | ServiceRecord s = b.service; |
| 2596 | ArrayList<ConnectionRecord> clist = s.connections.get(binder); |
| 2597 | if (clist != null) { |
| 2598 | clist.remove(c); |
| 2599 | if (clist.size() == 0) { |
| 2600 | s.connections.remove(binder); |
| 2601 | } |
| 2602 | } |
| 2603 | b.connections.remove(c); |
| 2604 | if (c.activity != null && c.activity != skipAct) { |
| 2605 | if (c.activity.connections != null) { |
| 2606 | c.activity.connections.remove(c); |
| 2607 | } |
| 2608 | } |
| 2609 | if (b.client != skipApp) { |
| 2610 | b.client.connections.remove(c); |
| 2611 | if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 2612 | b.client.updateHasAboveClientLocked(); |
| 2613 | } |
Dianne Hackborn | a590d2b | 2016-06-27 15:07:18 -0700 | [diff] [blame] | 2614 | // If this connection requested whitelist management, see if we should |
| 2615 | // now clear that state. |
| 2616 | if ((c.flags&Context.BIND_ALLOW_WHITELIST_MANAGEMENT) != 0) { |
| 2617 | s.updateWhitelistManager(); |
| 2618 | if (!s.whitelistManager && s.app != null) { |
| 2619 | updateWhitelistManagerLocked(s.app); |
| 2620 | } |
| 2621 | } |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 2622 | if (s.app != null) { |
Dianne Hackborn | 465fa39 | 2014-09-14 14:21:18 -0700 | [diff] [blame] | 2623 | updateServiceClientActivitiesLocked(s.app, c, true); |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 2624 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2625 | } |
| 2626 | clist = mServiceConnections.get(binder); |
| 2627 | if (clist != null) { |
| 2628 | clist.remove(c); |
| 2629 | if (clist.size() == 0) { |
| 2630 | mServiceConnections.remove(binder); |
| 2631 | } |
| 2632 | } |
| 2633 | |
Dianne Hackborn | ab2df06 | 2015-01-07 13:43:13 -0800 | [diff] [blame] | 2634 | mAm.stopAssociationLocked(b.client.uid, b.client.processName, s.appInfo.uid, s.name); |
| 2635 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2636 | if (b.connections.size() == 0) { |
| 2637 | b.intent.apps.remove(b.client); |
| 2638 | } |
| 2639 | |
| 2640 | if (!c.serviceDead) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2641 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Disconnecting binding " + b.intent |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2642 | + ": shouldUnbind=" + b.intent.hasBound); |
| 2643 | if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0 |
| 2644 | && b.intent.hasBound) { |
| 2645 | try { |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2646 | bumpServiceExecutingLocked(s, false, "unbind"); |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 2647 | if (b.client != s.app && (c.flags&Context.BIND_WAIVE_PRIORITY) == 0 |
Dianne Hackborn | f097d42 | 2017-12-15 16:32:19 -0800 | [diff] [blame] | 2648 | && s.app.setProcState <= ActivityManager.PROCESS_STATE_HEAVY_WEIGHT) { |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 2649 | // If this service's process is not already in the cached list, |
| 2650 | // then update it in the LRU list here because this may be causing |
| 2651 | // it to go down there and we want it to start out near the top. |
| 2652 | mAm.updateLruProcessLocked(s.app, false, null); |
| 2653 | } |
Amith Yamasani | 385c3ad | 2017-05-04 14:27:11 -0700 | [diff] [blame] | 2654 | mAm.updateOomAdjLocked(s.app, true); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2655 | b.intent.hasBound = false; |
| 2656 | // Assume the client doesn't want to know about a rebind; |
| 2657 | // we will deal with that later if it asks for one. |
| 2658 | b.intent.doRebind = false; |
| 2659 | s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent()); |
| 2660 | } catch (Exception e) { |
| 2661 | Slog.w(TAG, "Exception when unbinding service " + s.shortName, e); |
Dianne Hackborn | 878deb3 | 2013-10-14 16:55:09 -0700 | [diff] [blame] | 2662 | serviceProcessGoneLocked(s); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2663 | } |
| 2664 | } |
| 2665 | |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 2666 | // If unbound while waiting to start, remove the pending service |
| 2667 | mPendingServices.remove(s); |
| 2668 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2669 | if ((c.flags&Context.BIND_AUTO_CREATE) != 0) { |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2670 | boolean hasAutoCreate = s.hasAutoCreateConnections(); |
| 2671 | if (!hasAutoCreate) { |
| 2672 | if (s.tracker != null) { |
Dianne Hackborn | d293224 | 2013-08-05 18:18:42 -0700 | [diff] [blame] | 2673 | s.tracker.setBound(false, mAm.mProcessStats.getMemFactorLocked(), |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2674 | SystemClock.uptimeMillis()); |
| 2675 | } |
| 2676 | } |
| 2677 | bringDownServiceIfNeededLocked(s, true, hasAutoCreate); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2678 | } |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | void serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res) { |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 2683 | boolean inDestroying = mDestroyingServices.contains(r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2684 | if (r != null) { |
Dianne Hackborn | 455625e | 2015-01-21 09:55:13 -0800 | [diff] [blame] | 2685 | if (type == ActivityThread.SERVICE_DONE_EXECUTING_START) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2686 | // This is a call from a service start... take care of |
| 2687 | // book-keeping. |
| 2688 | r.callStart = true; |
| 2689 | switch (res) { |
| 2690 | case Service.START_STICKY_COMPATIBILITY: |
| 2691 | case Service.START_STICKY: { |
| 2692 | // We are done with the associated start arguments. |
| 2693 | r.findDeliveredStart(startId, true); |
| 2694 | // Don't stop if killed. |
| 2695 | r.stopIfKilled = false; |
| 2696 | break; |
| 2697 | } |
| 2698 | case Service.START_NOT_STICKY: { |
| 2699 | // We are done with the associated start arguments. |
| 2700 | r.findDeliveredStart(startId, true); |
| 2701 | if (r.getLastStartId() == startId) { |
| 2702 | // There is no more work, and this service |
| 2703 | // doesn't want to hang around if killed. |
| 2704 | r.stopIfKilled = true; |
| 2705 | } |
| 2706 | break; |
| 2707 | } |
| 2708 | case Service.START_REDELIVER_INTENT: { |
| 2709 | // We'll keep this item until they explicitly |
| 2710 | // call stop for it, but keep track of the fact |
| 2711 | // that it was delivered. |
| 2712 | ServiceRecord.StartItem si = r.findDeliveredStart(startId, false); |
| 2713 | if (si != null) { |
| 2714 | si.deliveryCount = 0; |
| 2715 | si.doneExecutingCount++; |
| 2716 | // Don't stop if killed. |
| 2717 | r.stopIfKilled = true; |
| 2718 | } |
| 2719 | break; |
| 2720 | } |
| 2721 | case Service.START_TASK_REMOVED_COMPLETE: { |
| 2722 | // Special processing for onTaskRemoved(). Don't |
| 2723 | // impact normal onStartCommand() processing. |
| 2724 | r.findDeliveredStart(startId, true); |
| 2725 | break; |
| 2726 | } |
| 2727 | default: |
| 2728 | throw new IllegalArgumentException( |
| 2729 | "Unknown service start result: " + res); |
| 2730 | } |
| 2731 | if (res == Service.START_STICKY_COMPATIBILITY) { |
| 2732 | r.callStart = false; |
| 2733 | } |
Dianne Hackborn | 455625e | 2015-01-21 09:55:13 -0800 | [diff] [blame] | 2734 | } else if (type == ActivityThread.SERVICE_DONE_EXECUTING_STOP) { |
| 2735 | // This is the final call from destroying the service... we should |
| 2736 | // actually be getting rid of the service at this point. Do some |
| 2737 | // validation of its state, and ensure it will be fully removed. |
| 2738 | if (!inDestroying) { |
| 2739 | // Not sure what else to do with this... if it is not actually in the |
| 2740 | // destroying list, we don't need to make sure to remove it from it. |
Amith Yamasani | d041822 | 2016-05-10 15:49:51 -0700 | [diff] [blame] | 2741 | // If the app is null, then it was probably removed because the process died, |
| 2742 | // otherwise wtf |
| 2743 | if (r.app != null) { |
Joe Onorato | 9d97ee2 | 2016-05-31 10:49:08 -0700 | [diff] [blame] | 2744 | Slog.w(TAG, "Service done with onDestroy, but not inDestroying: " |
Amith Yamasani | d041822 | 2016-05-10 15:49:51 -0700 | [diff] [blame] | 2745 | + r + ", app=" + r.app); |
| 2746 | } |
Dianne Hackborn | 455625e | 2015-01-21 09:55:13 -0800 | [diff] [blame] | 2747 | } else if (r.executeNesting != 1) { |
Joe Onorato | 9d97ee2 | 2016-05-31 10:49:08 -0700 | [diff] [blame] | 2748 | Slog.w(TAG, "Service done with onDestroy, but executeNesting=" |
Dianne Hackborn | 455625e | 2015-01-21 09:55:13 -0800 | [diff] [blame] | 2749 | + r.executeNesting + ": " + r); |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 2750 | // Fake it to keep from ANR due to orphaned entry. |
Dianne Hackborn | 455625e | 2015-01-21 09:55:13 -0800 | [diff] [blame] | 2751 | r.executeNesting = 1; |
| 2752 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2753 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2754 | final long origId = Binder.clearCallingIdentity(); |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 2755 | serviceDoneExecutingLocked(r, inDestroying, inDestroying); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2756 | Binder.restoreCallingIdentity(origId); |
| 2757 | } else { |
| 2758 | Slog.w(TAG, "Done executing unknown service from pid " |
| 2759 | + Binder.getCallingPid()); |
| 2760 | } |
| 2761 | } |
| 2762 | |
Dianne Hackborn | 878deb3 | 2013-10-14 16:55:09 -0700 | [diff] [blame] | 2763 | private void serviceProcessGoneLocked(ServiceRecord r) { |
| 2764 | if (r.tracker != null) { |
| 2765 | int memFactor = mAm.mProcessStats.getMemFactorLocked(); |
| 2766 | long now = SystemClock.uptimeMillis(); |
| 2767 | r.tracker.setExecuting(false, memFactor, now); |
| 2768 | r.tracker.setBound(false, memFactor, now); |
Dianne Hackborn | bc72dce | 2013-11-11 10:43:38 -0800 | [diff] [blame] | 2769 | r.tracker.setStarted(false, memFactor, now); |
Dianne Hackborn | 878deb3 | 2013-10-14 16:55:09 -0700 | [diff] [blame] | 2770 | } |
| 2771 | serviceDoneExecutingLocked(r, true, true); |
| 2772 | } |
| 2773 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 2774 | private void serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying, |
| 2775 | boolean finishing) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2776 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "<<< DONE EXECUTING " + r |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2777 | + ": nesting=" + r.executeNesting |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 2778 | + ", inDestroying=" + inDestroying + ", app=" + r.app); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2779 | else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG_SERVICE_EXECUTING, |
| 2780 | "<<< DONE EXECUTING " + r.shortName); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2781 | r.executeNesting--; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2782 | if (r.executeNesting <= 0) { |
| 2783 | if (r.app != null) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2784 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2785 | "Nesting at 0 of " + r.shortName); |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2786 | r.app.execServicesFg = false; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2787 | r.app.executingServices.remove(r); |
| 2788 | if (r.app.executingServices.size() == 0) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2789 | if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG_SERVICE_EXECUTING, |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2790 | "No more executingServices of " + r.shortName); |
| 2791 | mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_TIMEOUT_MSG, r.app); |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2792 | } else if (r.executeFg) { |
| 2793 | // Need to re-evaluate whether the app still needs to be in the foreground. |
| 2794 | for (int i=r.app.executingServices.size()-1; i>=0; i--) { |
| 2795 | if (r.app.executingServices.valueAt(i).executeFg) { |
| 2796 | r.app.execServicesFg = true; |
| 2797 | break; |
| 2798 | } |
| 2799 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2800 | } |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 2801 | if (inDestroying) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 2802 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 2803 | "doneExecuting remove destroying " + r); |
| 2804 | mDestroyingServices.remove(r); |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2805 | r.bindings.clear(); |
| 2806 | } |
Amith Yamasani | 385c3ad | 2017-05-04 14:27:11 -0700 | [diff] [blame] | 2807 | mAm.updateOomAdjLocked(r.app, true); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2808 | } |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2809 | r.executeFg = false; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2810 | if (r.tracker != null) { |
Dianne Hackborn | d293224 | 2013-08-05 18:18:42 -0700 | [diff] [blame] | 2811 | r.tracker.setExecuting(false, mAm.mProcessStats.getMemFactorLocked(), |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2812 | SystemClock.uptimeMillis()); |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 2813 | if (finishing) { |
Dianne Hackborn | 878deb3 | 2013-10-14 16:55:09 -0700 | [diff] [blame] | 2814 | r.tracker.clearCurrentOwner(r, false); |
Dianne Hackborn | bd754f4 | 2013-07-23 15:52:36 -0700 | [diff] [blame] | 2815 | r.tracker = null; |
| 2816 | } |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2817 | } |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 2818 | if (finishing) { |
Dianne Hackborn | 90e9b1d | 2013-11-21 12:50:01 -0800 | [diff] [blame] | 2819 | if (r.app != null && !r.app.persistent) { |
Dianne Hackborn | ddc19e9 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 2820 | r.app.services.remove(r); |
Dianne Hackborn | a590d2b | 2016-06-27 15:07:18 -0700 | [diff] [blame] | 2821 | if (r.whitelistManager) { |
| 2822 | updateWhitelistManagerLocked(r.app); |
| 2823 | } |
Dianne Hackborn | ddc19e9 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 2824 | } |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 2825 | r.app = null; |
| 2826 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2827 | } |
| 2828 | } |
| 2829 | |
Dianne Hackborn | ff07272 | 2014-09-24 10:56:28 -0700 | [diff] [blame] | 2830 | boolean attachApplicationLocked(ProcessRecord proc, String processName) |
| 2831 | throws RemoteException { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2832 | boolean didSomething = false; |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2833 | // Collect any services that are waiting for this process to come up. |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2834 | if (mPendingServices.size() > 0) { |
| 2835 | ServiceRecord sr = null; |
| 2836 | try { |
| 2837 | for (int i=0; i<mPendingServices.size(); i++) { |
| 2838 | sr = mPendingServices.get(i); |
| 2839 | if (proc != sr.isolatedProc && (proc.uid != sr.appInfo.uid |
| 2840 | || !processName.equals(sr.processName))) { |
| 2841 | continue; |
| 2842 | } |
| 2843 | |
| 2844 | mPendingServices.remove(i); |
| 2845 | i--; |
Dianne Hackborn | f7097a5 | 2014-05-13 09:56:14 -0700 | [diff] [blame] | 2846 | proc.addPackage(sr.appInfo.packageName, sr.appInfo.versionCode, |
| 2847 | mAm.mProcessStats); |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 2848 | realStartServiceLocked(sr, proc, sr.createdFromFg); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2849 | didSomething = true; |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 2850 | if (!isServiceNeededLocked(sr, false, false)) { |
Dianne Hackborn | 865907d | 2015-10-21 17:12:53 -0700 | [diff] [blame] | 2851 | // We were waiting for this service to start, but it is actually no |
| 2852 | // longer needed. This could happen because bringDownServiceIfNeeded |
| 2853 | // won't bring down a service that is pending... so now the pending |
| 2854 | // is done, so let's drop it. |
| 2855 | bringDownServiceLocked(sr); |
| 2856 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2857 | } |
Dianne Hackborn | ff07272 | 2014-09-24 10:56:28 -0700 | [diff] [blame] | 2858 | } catch (RemoteException e) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2859 | Slog.w(TAG, "Exception in new application when starting service " |
| 2860 | + sr.shortName, e); |
| 2861 | throw e; |
| 2862 | } |
| 2863 | } |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2864 | // Also, if there are any services that are waiting to restart and |
| 2865 | // would run in this process, now is a good time to start them. It would |
| 2866 | // be weird to bring up the process but arbitrarily not let the services |
| 2867 | // run at this point just because their restart time hasn't come up. |
| 2868 | if (mRestartingServices.size() > 0) { |
Dianne Hackborn | 865907d | 2015-10-21 17:12:53 -0700 | [diff] [blame] | 2869 | ServiceRecord sr; |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 2870 | for (int i=0; i<mRestartingServices.size(); i++) { |
| 2871 | sr = mRestartingServices.get(i); |
| 2872 | if (proc != sr.isolatedProc && (proc.uid != sr.appInfo.uid |
| 2873 | || !processName.equals(sr.processName))) { |
| 2874 | continue; |
| 2875 | } |
| 2876 | mAm.mHandler.removeCallbacks(sr.restarter); |
| 2877 | mAm.mHandler.post(sr.restarter); |
| 2878 | } |
| 2879 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2880 | return didSomething; |
| 2881 | } |
| 2882 | |
| 2883 | void processStartTimedOutLocked(ProcessRecord proc) { |
| 2884 | for (int i=0; i<mPendingServices.size(); i++) { |
| 2885 | ServiceRecord sr = mPendingServices.get(i); |
| 2886 | if ((proc.uid == sr.appInfo.uid |
| 2887 | && proc.processName.equals(sr.processName)) |
| 2888 | || sr.isolatedProc == proc) { |
| 2889 | Slog.w(TAG, "Forcing bringing down service: " + sr); |
| 2890 | sr.isolatedProc = null; |
| 2891 | mPendingServices.remove(i); |
| 2892 | i--; |
Dianne Hackborn | 91268cf | 2013-06-13 19:06:50 -0700 | [diff] [blame] | 2893 | bringDownServiceLocked(sr); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2894 | } |
| 2895 | } |
| 2896 | } |
| 2897 | |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 2898 | private boolean collectPackageServicesLocked(String packageName, Set<String> filterByClasses, |
Wale Ogunwale | 2cb2dd4 | 2015-09-02 14:53:29 -0700 | [diff] [blame] | 2899 | boolean evenPersistent, boolean doit, boolean killProcess, |
| 2900 | ArrayMap<ComponentName, ServiceRecord> services) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2901 | boolean didSomething = false; |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 2902 | for (int i = services.size() - 1; i >= 0; i--) { |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2903 | ServiceRecord service = services.valueAt(i); |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 2904 | final boolean sameComponent = packageName == null |
| 2905 | || (service.packageName.equals(packageName) |
| 2906 | && (filterByClasses == null |
| 2907 | || filterByClasses.contains(service.name.getClassName()))); |
| 2908 | if (sameComponent |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2909 | && (service.app == null || evenPersistent || !service.app.persistent)) { |
| 2910 | if (!doit) { |
| 2911 | return true; |
| 2912 | } |
| 2913 | didSomething = true; |
| 2914 | Slog.i(TAG, " Force stopping service " + service); |
| 2915 | if (service.app != null) { |
Wale Ogunwale | 2cb2dd4 | 2015-09-02 14:53:29 -0700 | [diff] [blame] | 2916 | service.app.removed = killProcess; |
Dianne Hackborn | 90e9b1d | 2013-11-21 12:50:01 -0800 | [diff] [blame] | 2917 | if (!service.app.persistent) { |
| 2918 | service.app.services.remove(service); |
Dianne Hackborn | a590d2b | 2016-06-27 15:07:18 -0700 | [diff] [blame] | 2919 | if (service.whitelistManager) { |
| 2920 | updateWhitelistManagerLocked(service.app); |
| 2921 | } |
Dianne Hackborn | 90e9b1d | 2013-11-21 12:50:01 -0800 | [diff] [blame] | 2922 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2923 | } |
| 2924 | service.app = null; |
| 2925 | service.isolatedProc = null; |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 2926 | if (mTmpCollectionResults == null) { |
| 2927 | mTmpCollectionResults = new ArrayList<>(); |
| 2928 | } |
| 2929 | mTmpCollectionResults.add(service); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2930 | } |
| 2931 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2932 | return didSomething; |
| 2933 | } |
| 2934 | |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 2935 | boolean bringDownDisabledPackageServicesLocked(String packageName, Set<String> filterByClasses, |
Wale Ogunwale | 2cb2dd4 | 2015-09-02 14:53:29 -0700 | [diff] [blame] | 2936 | int userId, boolean evenPersistent, boolean killProcess, boolean doit) { |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2937 | boolean didSomething = false; |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 2938 | |
| 2939 | if (mTmpCollectionResults != null) { |
| 2940 | mTmpCollectionResults.clear(); |
| 2941 | } |
| 2942 | |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2943 | if (userId == UserHandle.USER_ALL) { |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 2944 | for (int i = mServiceMap.size() - 1; i >= 0; i--) { |
| 2945 | didSomething |= collectPackageServicesLocked(packageName, filterByClasses, |
Wale Ogunwale | 2cb2dd4 | 2015-09-02 14:53:29 -0700 | [diff] [blame] | 2946 | evenPersistent, doit, killProcess, mServiceMap.valueAt(i).mServicesByName); |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2947 | if (!doit && didSomething) { |
| 2948 | return true; |
| 2949 | } |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 2950 | if (doit && filterByClasses == null) { |
| 2951 | forceStopPackageLocked(packageName, mServiceMap.valueAt(i).mUserId); |
| 2952 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2953 | } |
| 2954 | } else { |
Amith Yamasani | 540b659 | 2013-10-01 13:02:52 -0700 | [diff] [blame] | 2955 | ServiceMap smap = mServiceMap.get(userId); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2956 | if (smap != null) { |
| 2957 | ArrayMap<ComponentName, ServiceRecord> items = smap.mServicesByName; |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 2958 | didSomething = collectPackageServicesLocked(packageName, filterByClasses, |
Wale Ogunwale | 2cb2dd4 | 2015-09-02 14:53:29 -0700 | [diff] [blame] | 2959 | evenPersistent, doit, killProcess, items); |
Dianne Hackborn | 2d1b378 | 2012-09-09 17:49:39 -0700 | [diff] [blame] | 2960 | } |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 2961 | if (doit && filterByClasses == null) { |
| 2962 | forceStopPackageLocked(packageName, userId); |
| 2963 | } |
Dianne Hackborn | 5e03e2c | 2012-09-06 14:21:19 -0700 | [diff] [blame] | 2964 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2965 | |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 2966 | if (mTmpCollectionResults != null) { |
| 2967 | for (int i = mTmpCollectionResults.size() - 1; i >= 0; i--) { |
| 2968 | bringDownServiceLocked(mTmpCollectionResults.get(i)); |
| 2969 | } |
| 2970 | mTmpCollectionResults.clear(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2971 | } |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 2972 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2973 | return didSomething; |
| 2974 | } |
| 2975 | |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 2976 | void forceStopPackageLocked(String packageName, int userId) { |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 2977 | ServiceMap smap = mServiceMap.get(userId); |
| 2978 | if (smap != null && smap.mActiveForegroundApps.size() > 0) { |
Dianne Hackborn | af59768 | 2017-05-01 10:54:07 -0700 | [diff] [blame] | 2979 | for (int i = smap.mActiveForegroundApps.size()-1; i >= 0; i--) { |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 2980 | ActiveForegroundApp aa = smap.mActiveForegroundApps.valueAt(i); |
| 2981 | if (aa.mPackageName.equals(packageName)) { |
| 2982 | smap.mActiveForegroundApps.removeAt(i); |
| 2983 | smap.mActiveForegroundAppsChanged = true; |
| 2984 | } |
| 2985 | } |
| 2986 | if (smap.mActiveForegroundAppsChanged) { |
| 2987 | requestUpdateActiveForegroundAppsLocked(smap, 0); |
| 2988 | } |
| 2989 | } |
| 2990 | } |
| 2991 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2992 | void cleanUpRemovedTaskLocked(TaskRecord tr, ComponentName component, Intent baseIntent) { |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 2993 | ArrayList<ServiceRecord> services = new ArrayList<>(); |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 2994 | ArrayMap<ComponentName, ServiceRecord> alls = getServicesLocked(tr.userId); |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 2995 | for (int i = alls.size() - 1; i >= 0; i--) { |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 2996 | ServiceRecord sr = alls.valueAt(i); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 2997 | if (sr.packageName.equals(component.getPackageName())) { |
| 2998 | services.add(sr); |
| 2999 | } |
| 3000 | } |
| 3001 | |
| 3002 | // Take care of any running services associated with the app. |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 3003 | for (int i = services.size() - 1; i >= 0; i--) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3004 | ServiceRecord sr = services.get(i); |
| 3005 | if (sr.startRequested) { |
| 3006 | if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) { |
| 3007 | Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task"); |
| 3008 | stopServiceLocked(sr); |
| 3009 | } else { |
| 3010 | sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true, |
Todd Kennedy | 51b3aac | 2017-03-30 17:50:42 -0700 | [diff] [blame] | 3011 | sr.makeNextStartId(), baseIntent, null, 0)); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3012 | if (sr.app != null && sr.app.thread != null) { |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 3013 | // We always run in the foreground, since this is called as |
| 3014 | // part of the "remove task" UI operation. |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 3015 | try { |
| 3016 | sendServiceArgsLocked(sr, true, false); |
| 3017 | } catch (TransactionTooLargeException e) { |
| 3018 | // Ignore, keep going. |
| 3019 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3020 | } |
| 3021 | } |
| 3022 | } |
| 3023 | } |
| 3024 | } |
| 3025 | |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 3026 | final void killServicesLocked(ProcessRecord app, boolean allowRestart) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3027 | // Report disconnected services. |
| 3028 | if (false) { |
| 3029 | // XXX we are letting the client link to the service for |
| 3030 | // death notifications. |
| 3031 | if (app.services.size() > 0) { |
| 3032 | Iterator<ServiceRecord> it = app.services.iterator(); |
| 3033 | while (it.hasNext()) { |
| 3034 | ServiceRecord r = it.next(); |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 3035 | for (int conni=r.connections.size()-1; conni>=0; conni--) { |
| 3036 | ArrayList<ConnectionRecord> cl = r.connections.valueAt(conni); |
| 3037 | for (int i=0; i<cl.size(); i++) { |
| 3038 | ConnectionRecord c = cl.get(i); |
| 3039 | if (c.binding.client != app) { |
| 3040 | try { |
| 3041 | //c.conn.connected(r.className, null); |
| 3042 | } catch (Exception e) { |
| 3043 | // todo: this should be asynchronous! |
| 3044 | Slog.w(TAG, "Exception thrown disconnected servce " |
| 3045 | + r.shortName |
| 3046 | + " from app " + app.processName, e); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3047 | } |
| 3048 | } |
| 3049 | } |
| 3050 | } |
| 3051 | } |
| 3052 | } |
| 3053 | } |
| 3054 | |
louis_chang | 40e259c | 2015-03-26 13:31:14 +0800 | [diff] [blame] | 3055 | // Clean up any connections this application has to other services. |
| 3056 | for (int i = app.connections.size() - 1; i >= 0; i--) { |
| 3057 | ConnectionRecord r = app.connections.valueAt(i); |
| 3058 | removeConnectionLocked(r, app, null); |
| 3059 | } |
| 3060 | updateServiceConnectionActivitiesLocked(app); |
| 3061 | app.connections.clear(); |
| 3062 | |
Dianne Hackborn | a590d2b | 2016-06-27 15:07:18 -0700 | [diff] [blame] | 3063 | app.whitelistManager = false; |
| 3064 | |
louis_chang | 40e259c | 2015-03-26 13:31:14 +0800 | [diff] [blame] | 3065 | // Clear app state from services. |
| 3066 | for (int i = app.services.size() - 1; i >= 0; i--) { |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3067 | ServiceRecord sr = app.services.valueAt(i); |
| 3068 | synchronized (sr.stats.getBatteryStats()) { |
| 3069 | sr.stats.stopLaunchedLocked(); |
| 3070 | } |
Dianne Hackborn | aa9875e | 2013-12-09 11:26:11 -0800 | [diff] [blame] | 3071 | if (sr.app != app && sr.app != null && !sr.app.persistent) { |
Dianne Hackborn | ddc19e9 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 3072 | sr.app.services.remove(sr); |
| 3073 | } |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3074 | sr.app = null; |
| 3075 | sr.isolatedProc = null; |
| 3076 | sr.executeNesting = 0; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 3077 | sr.forceClearTracker(); |
| 3078 | if (mDestroyingServices.remove(sr)) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 3079 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "killServices remove destroying " + sr); |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3080 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3081 | |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3082 | final int numClients = sr.bindings.size(); |
| 3083 | for (int bindingi=numClients-1; bindingi>=0; bindingi--) { |
| 3084 | IntentBindRecord b = sr.bindings.valueAt(bindingi); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 3085 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Killing binding " + b |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3086 | + ": shouldUnbind=" + b.hasBound); |
| 3087 | b.binder = null; |
| 3088 | b.requested = b.received = b.hasBound = false; |
Dianne Hackborn | 465fa39 | 2014-09-14 14:21:18 -0700 | [diff] [blame] | 3089 | // If this binding is coming from a cached process and is asking to keep |
| 3090 | // the service created, then we'll kill the cached process as well -- we |
| 3091 | // don't want to be thrashing around restarting processes that are only |
| 3092 | // there to be cached. |
| 3093 | for (int appi=b.apps.size()-1; appi>=0; appi--) { |
Dianne Hackborn | 24c98e8 | 2014-09-14 17:23:54 -0700 | [diff] [blame] | 3094 | final ProcessRecord proc = b.apps.keyAt(appi); |
| 3095 | // If the process is already gone, skip it. |
| 3096 | if (proc.killedByAm || proc.thread == null) { |
| 3097 | continue; |
| 3098 | } |
| 3099 | // Only do this for processes that have an auto-create binding; |
| 3100 | // otherwise the binding can be left, because it won't cause the |
| 3101 | // service to restart. |
| 3102 | final AppBindRecord abind = b.apps.valueAt(appi); |
| 3103 | boolean hasCreate = false; |
| 3104 | for (int conni=abind.connections.size()-1; conni>=0; conni--) { |
| 3105 | ConnectionRecord conn = abind.connections.valueAt(conni); |
Dianne Hackborn | 0fe3c25 | 2014-09-19 15:09:39 -0700 | [diff] [blame] | 3106 | if ((conn.flags&(Context.BIND_AUTO_CREATE|Context.BIND_ALLOW_OOM_MANAGEMENT |
| 3107 | |Context.BIND_WAIVE_PRIORITY)) == Context.BIND_AUTO_CREATE) { |
Dianne Hackborn | 24c98e8 | 2014-09-14 17:23:54 -0700 | [diff] [blame] | 3108 | hasCreate = true; |
| 3109 | break; |
| 3110 | } |
| 3111 | } |
| 3112 | if (!hasCreate) { |
| 3113 | continue; |
| 3114 | } |
Dianne Hackborn | cd97c96 | 2014-09-25 18:34:02 -0700 | [diff] [blame] | 3115 | // XXX turned off for now until we have more time to get a better policy. |
| 3116 | if (false && proc != null && !proc.persistent && proc.thread != null |
Dianne Hackborn | 465fa39 | 2014-09-14 14:21:18 -0700 | [diff] [blame] | 3117 | && proc.pid != 0 && proc.pid != ActivityManagerService.MY_PID |
| 3118 | && proc.setProcState >= ActivityManager.PROCESS_STATE_LAST_ACTIVITY) { |
| 3119 | proc.kill("bound to service " + sr.name.flattenToShortString() |
| 3120 | + " in dying proc " + (app != null ? app.processName : "??"), true); |
| 3121 | } |
| 3122 | } |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3123 | } |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 3124 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3125 | |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 3126 | ServiceMap smap = getServiceMapLocked(app.userId); |
Dianne Hackborn | ddc19e9 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 3127 | |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 3128 | // Now do remaining service cleanup. |
| 3129 | for (int i=app.services.size()-1; i>=0; i--) { |
Dianne Hackborn | ddc19e9 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 3130 | ServiceRecord sr = app.services.valueAt(i); |
Dianne Hackborn | aa9875e | 2013-12-09 11:26:11 -0800 | [diff] [blame] | 3131 | |
| 3132 | // Unless the process is persistent, this process record is going away, |
| 3133 | // so make sure the service is cleaned out of it. |
| 3134 | if (!app.persistent) { |
Dianne Hackborn | 4190fc5 | 2013-12-09 18:20:16 -0800 | [diff] [blame] | 3135 | app.services.removeAt(i); |
Dianne Hackborn | aa9875e | 2013-12-09 11:26:11 -0800 | [diff] [blame] | 3136 | } |
| 3137 | |
Dianne Hackborn | ddc19e9 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 3138 | // Sanity check: if the service listed for the app is not one |
Dianne Hackborn | 4190fc5 | 2013-12-09 18:20:16 -0800 | [diff] [blame] | 3139 | // we actually are maintaining, just let it drop. |
Dianne Hackborn | 40c8725 | 2014-03-19 16:55:40 -0700 | [diff] [blame] | 3140 | final ServiceRecord curRec = smap.mServicesByName.get(sr.name); |
| 3141 | if (curRec != sr) { |
| 3142 | if (curRec != null) { |
| 3143 | Slog.wtf(TAG, "Service " + sr + " in process " + app |
| 3144 | + " not same as in map: " + curRec); |
| 3145 | } |
Dianne Hackborn | ddc19e9 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 3146 | continue; |
| 3147 | } |
| 3148 | |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 3149 | // Any services running in the application may need to be placed |
| 3150 | // back in the pending list. |
Amith Yamasani | b0c8a88 | 2017-08-28 09:36:42 -0700 | [diff] [blame] | 3151 | if (allowRestart && sr.crashCount >= mAm.mConstants.BOUND_SERVICE_MAX_CRASH_RETRY |
| 3152 | && (sr.serviceInfo.applicationInfo.flags |
| 3153 | &ApplicationInfo.FLAG_PERSISTENT) == 0) { |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3154 | Slog.w(TAG, "Service crashed " + sr.crashCount |
| 3155 | + " times, stopping: " + sr); |
| 3156 | EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH, |
| 3157 | sr.userId, sr.crashCount, sr.shortName, app.pid); |
| 3158 | bringDownServiceLocked(sr); |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 3159 | } else if (!allowRestart |
Fyodor Kupolov | 1b3edac | 2017-09-19 15:48:06 -0700 | [diff] [blame] | 3160 | || !mAm.mUserController.isUserRunning(sr.userId, 0)) { |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3161 | bringDownServiceLocked(sr); |
| 3162 | } else { |
| 3163 | boolean canceled = scheduleServiceRestartLocked(sr, true); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3164 | |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3165 | // Should the service remain running? Note that in the |
| 3166 | // extreme case of so many attempts to deliver a command |
| 3167 | // that it failed we also will stop it here. |
| 3168 | if (sr.startRequested && (sr.stopIfKilled || canceled)) { |
| 3169 | if (sr.pendingStarts.size() == 0) { |
| 3170 | sr.startRequested = false; |
| 3171 | if (sr.tracker != null) { |
Dianne Hackborn | d293224 | 2013-08-05 18:18:42 -0700 | [diff] [blame] | 3172 | sr.tracker.setStarted(false, mAm.mProcessStats.getMemFactorLocked(), |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3173 | SystemClock.uptimeMillis()); |
| 3174 | } |
| 3175 | if (!sr.hasAutoCreateConnections()) { |
| 3176 | // Whoops, no reason to restart! |
| 3177 | bringDownServiceLocked(sr); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3178 | } |
| 3179 | } |
| 3180 | } |
| 3181 | } |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3182 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3183 | |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3184 | if (!allowRestart) { |
| 3185 | app.services.clear(); |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 3186 | |
| 3187 | // Make sure there are no more restarting services for this process. |
| 3188 | for (int i=mRestartingServices.size()-1; i>=0; i--) { |
| 3189 | ServiceRecord r = mRestartingServices.get(i); |
| 3190 | if (r.processName.equals(app.processName) && |
| 3191 | r.serviceInfo.applicationInfo.uid == app.info.uid) { |
| 3192 | mRestartingServices.remove(i); |
Dianne Hackborn | d6f5b62 | 2013-11-11 17:25:37 -0800 | [diff] [blame] | 3193 | clearRestartingIfNeededLocked(r); |
| 3194 | } |
| 3195 | } |
| 3196 | for (int i=mPendingServices.size()-1; i>=0; i--) { |
| 3197 | ServiceRecord r = mPendingServices.get(i); |
| 3198 | if (r.processName.equals(app.processName) && |
| 3199 | r.serviceInfo.applicationInfo.uid == app.info.uid) { |
| 3200 | mPendingServices.remove(i); |
Dianne Hackborn | daa0d5c | 2013-11-06 16:30:29 -0800 | [diff] [blame] | 3201 | } |
| 3202 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | // Make sure we have no more records on the stopping list. |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 3206 | int i = mDestroyingServices.size(); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3207 | while (i > 0) { |
| 3208 | i--; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 3209 | ServiceRecord sr = mDestroyingServices.get(i); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3210 | if (sr.app == app) { |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 3211 | sr.forceClearTracker(); |
| 3212 | mDestroyingServices.remove(i); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 3213 | if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "killServices remove destroying " + sr); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3214 | } |
| 3215 | } |
| 3216 | |
| 3217 | app.executingServices.clear(); |
| 3218 | } |
| 3219 | |
| 3220 | ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) { |
| 3221 | ActivityManager.RunningServiceInfo info = |
| 3222 | new ActivityManager.RunningServiceInfo(); |
| 3223 | info.service = r.name; |
| 3224 | if (r.app != null) { |
| 3225 | info.pid = r.app.pid; |
| 3226 | } |
| 3227 | info.uid = r.appInfo.uid; |
| 3228 | info.process = r.processName; |
| 3229 | info.foreground = r.isForeground; |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 3230 | info.activeSince = r.createRealTime; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3231 | info.started = r.startRequested; |
| 3232 | info.clientCount = r.connections.size(); |
| 3233 | info.crashCount = r.crashCount; |
| 3234 | info.lastActivityTime = r.lastActivity; |
| 3235 | if (r.isForeground) { |
| 3236 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND; |
| 3237 | } |
| 3238 | if (r.startRequested) { |
| 3239 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED; |
| 3240 | } |
| 3241 | if (r.app != null && r.app.pid == ActivityManagerService.MY_PID) { |
| 3242 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS; |
| 3243 | } |
| 3244 | if (r.app != null && r.app.persistent) { |
| 3245 | info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS; |
| 3246 | } |
| 3247 | |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 3248 | for (int conni=r.connections.size()-1; conni>=0; conni--) { |
| 3249 | ArrayList<ConnectionRecord> connl = r.connections.valueAt(conni); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3250 | for (int i=0; i<connl.size(); i++) { |
| 3251 | ConnectionRecord conn = connl.get(i); |
| 3252 | if (conn.clientLabel != 0) { |
| 3253 | info.clientPackage = conn.binding.client.info.packageName; |
| 3254 | info.clientLabel = conn.clientLabel; |
| 3255 | return info; |
| 3256 | } |
| 3257 | } |
| 3258 | } |
| 3259 | return info; |
| 3260 | } |
| 3261 | |
Bryce Lee | 290e578 | 2017-02-01 16:41:20 -0800 | [diff] [blame] | 3262 | List<ActivityManager.RunningServiceInfo> getRunningServiceInfoLocked(int maxNum, int flags, |
Shunta Sato | 55af6c5 | 2016-10-19 15:58:52 +0900 | [diff] [blame] | 3263 | int callingUid, boolean allowed, boolean canInteractAcrossUsers) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3264 | ArrayList<ActivityManager.RunningServiceInfo> res |
| 3265 | = new ArrayList<ActivityManager.RunningServiceInfo>(); |
| 3266 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 3267 | final long ident = Binder.clearCallingIdentity(); |
| 3268 | try { |
Shunta Sato | 55af6c5 | 2016-10-19 15:58:52 +0900 | [diff] [blame] | 3269 | if (canInteractAcrossUsers) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 3270 | int[] users = mAm.mUserController.getUsers(); |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3271 | for (int ui=0; ui<users.length && res.size() < maxNum; ui++) { |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 3272 | ArrayMap<ComponentName, ServiceRecord> alls = getServicesLocked(users[ui]); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 3273 | for (int i=0; i<alls.size() && res.size() < maxNum; i++) { |
| 3274 | ServiceRecord sr = alls.valueAt(i); |
| 3275 | res.add(makeRunningServiceInfoLocked(sr)); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 3276 | } |
| 3277 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3278 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 3279 | for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { |
| 3280 | ServiceRecord r = mRestartingServices.get(i); |
| 3281 | ActivityManager.RunningServiceInfo info = |
| 3282 | makeRunningServiceInfoLocked(r); |
| 3283 | info.restarting = r.nextRestartTime; |
| 3284 | res.add(info); |
| 3285 | } |
| 3286 | } else { |
Bryce Lee | 290e578 | 2017-02-01 16:41:20 -0800 | [diff] [blame] | 3287 | int userId = UserHandle.getUserId(callingUid); |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 3288 | ArrayMap<ComponentName, ServiceRecord> alls = getServicesLocked(userId); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 3289 | for (int i=0; i<alls.size() && res.size() < maxNum; i++) { |
| 3290 | ServiceRecord sr = alls.valueAt(i); |
Bryce Lee | 290e578 | 2017-02-01 16:41:20 -0800 | [diff] [blame] | 3291 | |
| 3292 | if (allowed || (sr.app != null && sr.app.uid == callingUid)) { |
| 3293 | res.add(makeRunningServiceInfoLocked(sr)); |
| 3294 | } |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) { |
| 3298 | ServiceRecord r = mRestartingServices.get(i); |
Bryce Lee | 290e578 | 2017-02-01 16:41:20 -0800 | [diff] [blame] | 3299 | if (r.userId == userId |
| 3300 | && (allowed || (r.app != null && r.app.uid == callingUid))) { |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 3301 | ActivityManager.RunningServiceInfo info = |
| 3302 | makeRunningServiceInfoLocked(r); |
| 3303 | info.restarting = r.nextRestartTime; |
| 3304 | res.add(info); |
| 3305 | } |
| 3306 | } |
| 3307 | } |
| 3308 | } finally { |
| 3309 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | return res; |
| 3313 | } |
| 3314 | |
| 3315 | public PendingIntent getRunningServiceControlPanelLocked(ComponentName name) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3316 | int userId = UserHandle.getUserId(Binder.getCallingUid()); |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 3317 | ServiceRecord r = getServiceByNameLocked(name, userId); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3318 | if (r != null) { |
Dianne Hackborn | 390517b | 2013-05-30 15:03:32 -0700 | [diff] [blame] | 3319 | for (int conni=r.connections.size()-1; conni>=0; conni--) { |
| 3320 | ArrayList<ConnectionRecord> conn = r.connections.valueAt(conni); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3321 | for (int i=0; i<conn.size(); i++) { |
| 3322 | if (conn.get(i).clientIntent != null) { |
| 3323 | return conn.get(i).clientIntent; |
| 3324 | } |
| 3325 | } |
| 3326 | } |
| 3327 | } |
| 3328 | return null; |
| 3329 | } |
| 3330 | |
| 3331 | void serviceTimeout(ProcessRecord proc) { |
| 3332 | String anrMessage = null; |
| 3333 | |
Dianne Hackborn | c2f6f94 | 2014-09-22 13:36:42 -0700 | [diff] [blame] | 3334 | synchronized(mAm) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3335 | if (proc.executingServices.size() == 0 || proc.thread == null) { |
| 3336 | return; |
| 3337 | } |
Dianne Hackborn | ab2df06 | 2015-01-07 13:43:13 -0800 | [diff] [blame] | 3338 | final long now = SystemClock.uptimeMillis(); |
| 3339 | final long maxTime = now - |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 3340 | (proc.execServicesFg ? SERVICE_TIMEOUT : SERVICE_BACKGROUND_TIMEOUT); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3341 | ServiceRecord timeout = null; |
| 3342 | long nextTime = 0; |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 3343 | for (int i=proc.executingServices.size()-1; i>=0; i--) { |
| 3344 | ServiceRecord sr = proc.executingServices.valueAt(i); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3345 | if (sr.executingStart < maxTime) { |
| 3346 | timeout = sr; |
| 3347 | break; |
| 3348 | } |
| 3349 | if (sr.executingStart > nextTime) { |
| 3350 | nextTime = sr.executingStart; |
| 3351 | } |
| 3352 | } |
| 3353 | if (timeout != null && mAm.mLruProcesses.contains(proc)) { |
| 3354 | Slog.w(TAG, "Timeout executing service: " + timeout); |
Dianne Hackborn | cff1bbf | 2015-01-20 13:43:32 -0800 | [diff] [blame] | 3355 | StringWriter sw = new StringWriter(); |
| 3356 | PrintWriter pw = new FastPrintWriter(sw, false, 1024); |
| 3357 | pw.println(timeout); |
| 3358 | timeout.dump(pw, " "); |
| 3359 | pw.close(); |
| 3360 | mLastAnrDump = sw.toString(); |
| 3361 | mAm.mHandler.removeCallbacks(mLastAnrDumpClearer); |
| 3362 | mAm.mHandler.postDelayed(mLastAnrDumpClearer, LAST_ANR_LIFETIME_DURATION_MSECS); |
| 3363 | anrMessage = "executing service " + timeout.shortName; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3364 | } else { |
| 3365 | Message msg = mAm.mHandler.obtainMessage( |
| 3366 | ActivityManagerService.SERVICE_TIMEOUT_MSG); |
| 3367 | msg.obj = proc; |
Dianne Hackborn | bf36ee2 | 2013-07-26 18:24:10 -0700 | [diff] [blame] | 3368 | mAm.mHandler.sendMessageAtTime(msg, proc.execServicesFg |
Dianne Hackborn | 2be0093 | 2013-09-22 16:46:00 -0700 | [diff] [blame] | 3369 | ? (nextTime+SERVICE_TIMEOUT) : (nextTime + SERVICE_BACKGROUND_TIMEOUT)); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | if (anrMessage != null) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 3374 | mAm.mAppErrors.appNotResponding(proc, null, null, false, anrMessage); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 3378 | void serviceForegroundTimeout(ServiceRecord r) { |
| 3379 | ProcessRecord app; |
| 3380 | synchronized (mAm) { |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 3381 | if (!r.fgRequired || r.destroying) { |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 3382 | return; |
| 3383 | } |
| 3384 | |
| 3385 | if (DEBUG_BACKGROUND_CHECK) { |
| 3386 | Slog.i(TAG, "Service foreground-required timeout for " + r); |
| 3387 | } |
| 3388 | app = r.app; |
| 3389 | r.fgWaiting = false; |
| 3390 | stopServiceLocked(r); |
| 3391 | } |
| 3392 | |
| 3393 | if (app != null) { |
| 3394 | mAm.mAppErrors.appNotResponding(app, null, null, false, |
| 3395 | "Context.startForegroundService() did not then call Service.startForeground()"); |
| 3396 | } |
| 3397 | } |
| 3398 | |
Dianne Hackborn | f82236e | 2017-04-19 10:26:01 -0700 | [diff] [blame] | 3399 | void serviceForegroundCrash(ProcessRecord app) { |
| 3400 | mAm.crashApplication(app.uid, app.pid, app.info.packageName, app.userId, |
| 3401 | "Context.startForegroundService() did not then call Service.startForeground()"); |
| 3402 | } |
| 3403 | |
Dianne Hackborn | 2be0093 | 2013-09-22 16:46:00 -0700 | [diff] [blame] | 3404 | void scheduleServiceTimeoutLocked(ProcessRecord proc) { |
| 3405 | if (proc.executingServices.size() == 0 || proc.thread == null) { |
| 3406 | return; |
| 3407 | } |
Dianne Hackborn | 2be0093 | 2013-09-22 16:46:00 -0700 | [diff] [blame] | 3408 | Message msg = mAm.mHandler.obtainMessage( |
| 3409 | ActivityManagerService.SERVICE_TIMEOUT_MSG); |
| 3410 | msg.obj = proc; |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 3411 | mAm.mHandler.sendMessageDelayed(msg, |
| 3412 | proc.execServicesFg ? SERVICE_TIMEOUT : SERVICE_BACKGROUND_TIMEOUT); |
| 3413 | } |
| 3414 | |
| 3415 | void scheduleServiceForegroundTransitionTimeoutLocked(ServiceRecord r) { |
| 3416 | if (r.app.executingServices.size() == 0 || r.app.thread == null) { |
| 3417 | return; |
| 3418 | } |
| 3419 | Message msg = mAm.mHandler.obtainMessage( |
| 3420 | ActivityManagerService.SERVICE_FOREGROUND_TIMEOUT_MSG); |
| 3421 | msg.obj = r; |
| 3422 | r.fgWaiting = true; |
| 3423 | mAm.mHandler.sendMessageDelayed(msg, SERVICE_START_FOREGROUND_TIMEOUT); |
Dianne Hackborn | 2be0093 | 2013-09-22 16:46:00 -0700 | [diff] [blame] | 3424 | } |
| 3425 | |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3426 | final class ServiceDumper { |
| 3427 | private final FileDescriptor fd; |
| 3428 | private final PrintWriter pw; |
| 3429 | private final String[] args; |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3430 | private final boolean dumpAll; |
| 3431 | private final String dumpPackage; |
| 3432 | private final ItemMatcher matcher; |
| 3433 | private final ArrayList<ServiceRecord> services = new ArrayList<>(); |
| 3434 | |
| 3435 | private final long nowReal = SystemClock.elapsedRealtime(); |
| 3436 | |
| 3437 | private boolean needSep = false; |
| 3438 | private boolean printedAnything = false; |
| 3439 | private boolean printed = false; |
| 3440 | |
| 3441 | /** |
| 3442 | * Note: do not call directly, use {@link #newServiceDumperLocked} instead (this |
| 3443 | * must be called with the lock held). |
| 3444 | */ |
| 3445 | ServiceDumper(FileDescriptor fd, PrintWriter pw, String[] args, |
| 3446 | int opti, boolean dumpAll, String dumpPackage) { |
| 3447 | this.fd = fd; |
| 3448 | this.pw = pw; |
| 3449 | this.args = args; |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3450 | this.dumpAll = dumpAll; |
| 3451 | this.dumpPackage = dumpPackage; |
| 3452 | matcher = new ItemMatcher(); |
| 3453 | matcher.build(args, opti); |
| 3454 | |
| 3455 | final int[] users = mAm.mUserController.getUsers(); |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3456 | for (int user : users) { |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 3457 | ServiceMap smap = getServiceMapLocked(user); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 3458 | if (smap.mServicesByName.size() > 0) { |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 3459 | for (int si=0; si<smap.mServicesByName.size(); si++) { |
| 3460 | ServiceRecord r = smap.mServicesByName.valueAt(si); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3461 | if (!matcher.match(r, r.name)) { |
| 3462 | continue; |
| 3463 | } |
| 3464 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 3465 | continue; |
| 3466 | } |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3467 | services.add(r); |
| 3468 | } |
| 3469 | } |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | private void dumpHeaderLocked() { |
| 3474 | pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)"); |
| 3475 | if (mLastAnrDump != null) { |
| 3476 | pw.println(" Last ANR service:"); |
| 3477 | pw.print(mLastAnrDump); |
| 3478 | pw.println(); |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | void dumpLocked() { |
| 3483 | dumpHeaderLocked(); |
| 3484 | |
| 3485 | try { |
| 3486 | int[] users = mAm.mUserController.getUsers(); |
| 3487 | for (int user : users) { |
| 3488 | // Find the first service for this user. |
| 3489 | int serviceIdx = 0; |
| 3490 | while (serviceIdx < services.size() && services.get(serviceIdx).userId != user) { |
| 3491 | serviceIdx++; |
| 3492 | } |
| 3493 | printed = false; |
| 3494 | if (serviceIdx < services.size()) { |
| 3495 | needSep = false; |
| 3496 | while (serviceIdx < services.size()) { |
| 3497 | ServiceRecord r = services.get(serviceIdx); |
| 3498 | serviceIdx++; |
| 3499 | if (r.userId != user) { |
| 3500 | break; |
Dianne Hackborn | f88dd0b | 2012-08-08 17:20:32 -0700 | [diff] [blame] | 3501 | } |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3502 | dumpServiceLocalLocked(r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3503 | } |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3504 | needSep |= printed; |
| 3505 | } |
| 3506 | |
| 3507 | dumpUserRemainsLocked(user); |
| 3508 | } |
| 3509 | } catch (Exception e) { |
| 3510 | Slog.w(TAG, "Exception in dumpServicesLocked", e); |
| 3511 | } |
| 3512 | |
| 3513 | dumpRemainsLocked(); |
| 3514 | } |
| 3515 | |
| 3516 | void dumpWithClient() { |
| 3517 | synchronized(mAm) { |
| 3518 | dumpHeaderLocked(); |
| 3519 | } |
| 3520 | |
| 3521 | try { |
| 3522 | int[] users = mAm.mUserController.getUsers(); |
| 3523 | for (int user : users) { |
| 3524 | // Find the first service for this user. |
| 3525 | int serviceIdx = 0; |
| 3526 | while (serviceIdx < services.size() && services.get(serviceIdx).userId != user) { |
| 3527 | serviceIdx++; |
| 3528 | } |
| 3529 | printed = false; |
| 3530 | if (serviceIdx < services.size()) { |
| 3531 | needSep = false; |
| 3532 | while (serviceIdx < services.size()) { |
| 3533 | ServiceRecord r = services.get(serviceIdx); |
| 3534 | serviceIdx++; |
| 3535 | if (r.userId != user) { |
| 3536 | break; |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3537 | } |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3538 | synchronized(mAm) { |
| 3539 | dumpServiceLocalLocked(r); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3540 | } |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3541 | dumpServiceClient(r); |
| 3542 | } |
| 3543 | needSep |= printed; |
| 3544 | } |
| 3545 | |
| 3546 | synchronized(mAm) { |
| 3547 | dumpUserRemainsLocked(user); |
| 3548 | } |
| 3549 | } |
| 3550 | } catch (Exception e) { |
| 3551 | Slog.w(TAG, "Exception in dumpServicesLocked", e); |
| 3552 | } |
| 3553 | |
| 3554 | synchronized(mAm) { |
| 3555 | dumpRemainsLocked(); |
| 3556 | } |
| 3557 | } |
| 3558 | |
| 3559 | private void dumpUserHeaderLocked(int user) { |
| 3560 | if (!printed) { |
| 3561 | if (printedAnything) { |
| 3562 | pw.println(); |
| 3563 | } |
| 3564 | pw.println(" User " + user + " active services:"); |
| 3565 | printed = true; |
| 3566 | } |
| 3567 | printedAnything = true; |
| 3568 | if (needSep) { |
| 3569 | pw.println(); |
| 3570 | } |
| 3571 | } |
| 3572 | |
| 3573 | private void dumpServiceLocalLocked(ServiceRecord r) { |
| 3574 | dumpUserHeaderLocked(r.userId); |
| 3575 | pw.print(" * "); |
| 3576 | pw.println(r); |
| 3577 | if (dumpAll) { |
| 3578 | r.dump(pw, " "); |
| 3579 | needSep = true; |
| 3580 | } else { |
| 3581 | pw.print(" app="); |
| 3582 | pw.println(r.app); |
| 3583 | pw.print(" created="); |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 3584 | TimeUtils.formatDuration(r.createRealTime, nowReal, pw); |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3585 | pw.print(" started="); |
| 3586 | pw.print(r.startRequested); |
| 3587 | pw.print(" connections="); |
| 3588 | pw.println(r.connections.size()); |
| 3589 | if (r.connections.size() > 0) { |
| 3590 | pw.println(" Connections:"); |
| 3591 | for (int conni=0; conni<r.connections.size(); conni++) { |
| 3592 | ArrayList<ConnectionRecord> clist = r.connections.valueAt(conni); |
| 3593 | for (int i = 0; i < clist.size(); i++) { |
| 3594 | ConnectionRecord conn = clist.get(i); |
| 3595 | pw.print(" "); |
| 3596 | pw.print(conn.binding.intent.intent.getIntent() |
| 3597 | .toShortString(false, false, false, false)); |
| 3598 | pw.print(" -> "); |
| 3599 | ProcessRecord proc = conn.binding.client; |
| 3600 | pw.println(proc != null ? proc.toShortString() : "null"); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3601 | } |
| 3602 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3603 | } |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3604 | } |
| 3605 | } |
| 3606 | |
| 3607 | private void dumpServiceClient(ServiceRecord r) { |
| 3608 | final ProcessRecord proc = r.app; |
| 3609 | if (proc == null) { |
| 3610 | return; |
| 3611 | } |
| 3612 | final IApplicationThread thread = proc.thread; |
| 3613 | if (thread == null) { |
| 3614 | return; |
| 3615 | } |
| 3616 | pw.println(" Client:"); |
| 3617 | pw.flush(); |
| 3618 | try { |
| 3619 | TransferPipe tp = new TransferPipe(); |
| 3620 | try { |
Sudheer Shanka | cc6418f | 2016-10-13 12:03:44 -0700 | [diff] [blame] | 3621 | thread.dumpService(tp.getWriteFd(), r, args); |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3622 | tp.setBufferPrefix(" "); |
| 3623 | // Short timeout, since blocking here can |
| 3624 | // deadlock with the application. |
| 3625 | tp.go(fd, 2000); |
| 3626 | } finally { |
| 3627 | tp.kill(); |
| 3628 | } |
| 3629 | } catch (IOException e) { |
| 3630 | pw.println(" Failure while dumping the service: " + e); |
| 3631 | } catch (RemoteException e) { |
| 3632 | pw.println(" Got a RemoteException while dumping the service"); |
| 3633 | } |
| 3634 | needSep = true; |
| 3635 | } |
| 3636 | |
| 3637 | private void dumpUserRemainsLocked(int user) { |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 3638 | ServiceMap smap = getServiceMapLocked(user); |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3639 | printed = false; |
| 3640 | for (int si=0, SN=smap.mDelayedStartList.size(); si<SN; si++) { |
| 3641 | ServiceRecord r = smap.mDelayedStartList.get(si); |
| 3642 | if (!matcher.match(r, r.name)) { |
| 3643 | continue; |
| 3644 | } |
| 3645 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 3646 | continue; |
| 3647 | } |
| 3648 | if (!printed) { |
| 3649 | if (printedAnything) { |
| 3650 | pw.println(); |
| 3651 | } |
| 3652 | pw.println(" User " + user + " delayed start services:"); |
| 3653 | printed = true; |
| 3654 | } |
| 3655 | printedAnything = true; |
| 3656 | pw.print(" * Delayed start "); pw.println(r); |
| 3657 | } |
| 3658 | printed = false; |
| 3659 | for (int si=0, SN=smap.mStartingBackground.size(); si<SN; si++) { |
| 3660 | ServiceRecord r = smap.mStartingBackground.get(si); |
| 3661 | if (!matcher.match(r, r.name)) { |
| 3662 | continue; |
| 3663 | } |
| 3664 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 3665 | continue; |
| 3666 | } |
| 3667 | if (!printed) { |
| 3668 | if (printedAnything) { |
| 3669 | pw.println(); |
| 3670 | } |
| 3671 | pw.println(" User " + user + " starting in background:"); |
| 3672 | printed = true; |
| 3673 | } |
| 3674 | printedAnything = true; |
| 3675 | pw.print(" * Starting bg "); pw.println(r); |
| 3676 | } |
| 3677 | } |
| 3678 | |
| 3679 | private void dumpRemainsLocked() { |
| 3680 | if (mPendingServices.size() > 0) { |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 3681 | printed = false; |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3682 | for (int i=0; i<mPendingServices.size(); i++) { |
| 3683 | ServiceRecord r = mPendingServices.get(i); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 3684 | if (!matcher.match(r, r.name)) { |
| 3685 | continue; |
| 3686 | } |
| 3687 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 3688 | continue; |
| 3689 | } |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 3690 | printedAnything = true; |
| 3691 | if (!printed) { |
| 3692 | if (needSep) pw.println(); |
| 3693 | needSep = true; |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3694 | pw.println(" Pending services:"); |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 3695 | printed = true; |
| 3696 | } |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3697 | pw.print(" * Pending "); pw.println(r); |
| 3698 | r.dump(pw, " "); |
| 3699 | } |
| 3700 | needSep = true; |
| 3701 | } |
| 3702 | |
| 3703 | if (mRestartingServices.size() > 0) { |
| 3704 | printed = false; |
| 3705 | for (int i=0; i<mRestartingServices.size(); i++) { |
| 3706 | ServiceRecord r = mRestartingServices.get(i); |
| 3707 | if (!matcher.match(r, r.name)) { |
| 3708 | continue; |
| 3709 | } |
| 3710 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 3711 | continue; |
| 3712 | } |
| 3713 | printedAnything = true; |
| 3714 | if (!printed) { |
| 3715 | if (needSep) pw.println(); |
| 3716 | needSep = true; |
| 3717 | pw.println(" Restarting services:"); |
| 3718 | printed = true; |
| 3719 | } |
| 3720 | pw.print(" * Restarting "); pw.println(r); |
| 3721 | r.dump(pw, " "); |
| 3722 | } |
| 3723 | needSep = true; |
| 3724 | } |
| 3725 | |
| 3726 | if (mDestroyingServices.size() > 0) { |
| 3727 | printed = false; |
| 3728 | for (int i=0; i< mDestroyingServices.size(); i++) { |
| 3729 | ServiceRecord r = mDestroyingServices.get(i); |
| 3730 | if (!matcher.match(r, r.name)) { |
| 3731 | continue; |
| 3732 | } |
| 3733 | if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) { |
| 3734 | continue; |
| 3735 | } |
| 3736 | printedAnything = true; |
| 3737 | if (!printed) { |
| 3738 | if (needSep) pw.println(); |
| 3739 | needSep = true; |
| 3740 | pw.println(" Destroying services:"); |
| 3741 | printed = true; |
| 3742 | } |
| 3743 | pw.print(" * Destroy "); pw.println(r); |
| 3744 | r.dump(pw, " "); |
| 3745 | } |
| 3746 | needSep = true; |
| 3747 | } |
| 3748 | |
| 3749 | if (dumpAll) { |
| 3750 | printed = false; |
| 3751 | for (int ic=0; ic<mServiceConnections.size(); ic++) { |
| 3752 | ArrayList<ConnectionRecord> r = mServiceConnections.valueAt(ic); |
| 3753 | for (int i=0; i<r.size(); i++) { |
| 3754 | ConnectionRecord cr = r.get(i); |
| 3755 | if (!matcher.match(cr.binding.service, cr.binding.service.name)) { |
| 3756 | continue; |
| 3757 | } |
| 3758 | if (dumpPackage != null && (cr.binding.client == null |
| 3759 | || !dumpPackage.equals(cr.binding.client.info.packageName))) { |
| 3760 | continue; |
| 3761 | } |
| 3762 | printedAnything = true; |
| 3763 | if (!printed) { |
| 3764 | if (needSep) pw.println(); |
| 3765 | needSep = true; |
| 3766 | pw.println(" Connection bindings to services:"); |
| 3767 | printed = true; |
| 3768 | } |
| 3769 | pw.print(" * "); pw.println(cr); |
| 3770 | cr.dump(pw, " "); |
| 3771 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3772 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3773 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3774 | |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 3775 | if (matcher.all) { |
| 3776 | final long nowElapsed = SystemClock.elapsedRealtime(); |
| 3777 | final int[] users = mAm.mUserController.getUsers(); |
| 3778 | for (int user : users) { |
| 3779 | boolean printedUser = false; |
| 3780 | ServiceMap smap = mServiceMap.get(user); |
| 3781 | if (smap == null) { |
| 3782 | continue; |
| 3783 | } |
| 3784 | for (int i = smap.mActiveForegroundApps.size() - 1; i >= 0; i--) { |
| 3785 | ActiveForegroundApp aa = smap.mActiveForegroundApps.valueAt(i); |
| 3786 | if (dumpPackage != null && !dumpPackage.equals(aa.mPackageName)) { |
| 3787 | continue; |
| 3788 | } |
| 3789 | if (!printedUser) { |
| 3790 | printedUser = true; |
| 3791 | printedAnything = true; |
| 3792 | if (needSep) pw.println(); |
| 3793 | needSep = true; |
| 3794 | pw.print("Active foreground apps - user "); |
| 3795 | pw.print(user); |
| 3796 | pw.println(":"); |
| 3797 | } |
| 3798 | pw.print(" #"); |
| 3799 | pw.print(i); |
| 3800 | pw.print(": "); |
| 3801 | pw.println(aa.mPackageName); |
| 3802 | if (aa.mLabel != null) { |
| 3803 | pw.print(" mLabel="); |
| 3804 | pw.println(aa.mLabel); |
| 3805 | } |
| 3806 | pw.print(" mNumActive="); |
| 3807 | pw.print(aa.mNumActive); |
Dianne Hackborn | be68d49 | 2017-05-03 18:04:05 -0700 | [diff] [blame] | 3808 | pw.print(" mAppOnTop="); |
| 3809 | pw.print(aa.mAppOnTop); |
| 3810 | pw.print(" mShownWhileTop="); |
| 3811 | pw.print(aa.mShownWhileTop); |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 3812 | pw.print(" mShownWhileScreenOn="); |
| 3813 | pw.println(aa.mShownWhileScreenOn); |
| 3814 | pw.print(" mStartTime="); |
| 3815 | TimeUtils.formatDuration(aa.mStartTime - nowElapsed, pw); |
| 3816 | pw.print(" mStartVisibleTime="); |
| 3817 | TimeUtils.formatDuration(aa.mStartVisibleTime - nowElapsed, pw); |
| 3818 | pw.println(); |
| 3819 | if (aa.mEndTime != 0) { |
| 3820 | pw.print(" mEndTime="); |
| 3821 | TimeUtils.formatDuration(aa.mEndTime - nowElapsed, pw); |
| 3822 | pw.println(); |
| 3823 | } |
| 3824 | } |
Dianne Hackborn | cb01563 | 2017-06-14 17:30:15 -0700 | [diff] [blame] | 3825 | if (smap.hasMessagesOrCallbacks()) { |
| 3826 | if (needSep) { |
| 3827 | pw.println(); |
| 3828 | } |
| 3829 | printedAnything = true; |
| 3830 | needSep = true; |
| 3831 | pw.print(" Handler - user "); |
| 3832 | pw.print(user); |
| 3833 | pw.println(":"); |
| 3834 | smap.dumpMine(new PrintWriterPrinter(pw), " "); |
| 3835 | } |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 3836 | } |
| 3837 | } |
| 3838 | |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3839 | if (!printedAnything) { |
| 3840 | pw.println(" (nothing)"); |
| 3841 | } |
Dianne Hackborn | 7ad34e5 | 2013-06-05 18:41:45 -0700 | [diff] [blame] | 3842 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3843 | } |
| 3844 | |
Dianne Hackborn | 777a06d | 2016-05-16 17:36:15 -0700 | [diff] [blame] | 3845 | ServiceDumper newServiceDumperLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 3846 | int opti, boolean dumpAll, String dumpPackage) { |
| 3847 | return new ServiceDumper(fd, pw, args, opti, dumpAll, dumpPackage); |
| 3848 | } |
| 3849 | |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 3850 | protected void writeToProto(ProtoOutputStream proto) { |
| 3851 | synchronized (mAm) { |
| 3852 | int[] users = mAm.mUserController.getUsers(); |
| 3853 | for (int user : users) { |
| 3854 | ServiceMap smap = mServiceMap.get(user); |
| 3855 | if (smap == null) { |
| 3856 | continue; |
| 3857 | } |
| 3858 | long token = proto.start(ActiveServicesProto.SERVICES_BY_USERS); |
| 3859 | proto.write(ActiveServicesProto.ServicesByUser.USER_ID, user); |
| 3860 | ArrayMap<ComponentName, ServiceRecord> alls = smap.mServicesByName; |
| 3861 | for (int i=0; i<alls.size(); i++) { |
| 3862 | alls.valueAt(i).writeToProto(proto, |
| 3863 | ActiveServicesProto.ServicesByUser.SERVICE_RECORDS); |
| 3864 | } |
| 3865 | proto.end(token); |
| 3866 | } |
| 3867 | } |
| 3868 | } |
| 3869 | |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3870 | /** |
| 3871 | * There are three ways to call this: |
| 3872 | * - no service specified: dump all the services |
| 3873 | * - a flattened component name that matched an existing service was specified as the |
| 3874 | * first arg: dump that one service |
| 3875 | * - the first arg isn't the flattened component name of an existing service: |
| 3876 | * dump all services whose component contains the first arg as a substring |
| 3877 | */ |
| 3878 | protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args, |
| 3879 | int opti, boolean dumpAll) { |
| 3880 | ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>(); |
| 3881 | |
Dianne Hackborn | c2f6f94 | 2014-09-22 13:36:42 -0700 | [diff] [blame] | 3882 | synchronized (mAm) { |
Fyodor Kupolov | f63b89c | 2015-10-27 18:08:56 -0700 | [diff] [blame] | 3883 | int[] users = mAm.mUserController.getUsers(); |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3884 | if ("all".equals(name)) { |
| 3885 | for (int user : users) { |
Dianne Hackborn | 13c590d | 2013-10-07 14:32:00 -0700 | [diff] [blame] | 3886 | ServiceMap smap = mServiceMap.get(user); |
| 3887 | if (smap == null) { |
| 3888 | continue; |
| 3889 | } |
| 3890 | ArrayMap<ComponentName, ServiceRecord> alls = smap.mServicesByName; |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 3891 | for (int i=0; i<alls.size(); i++) { |
| 3892 | ServiceRecord r1 = alls.valueAt(i); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 3893 | services.add(r1); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3894 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3895 | } |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3896 | } else { |
| 3897 | ComponentName componentName = name != null |
| 3898 | ? ComponentName.unflattenFromString(name) : null; |
| 3899 | int objectId = 0; |
| 3900 | if (componentName == null) { |
| 3901 | // Not a '/' separated full component name; maybe an object ID? |
| 3902 | try { |
| 3903 | objectId = Integer.parseInt(name, 16); |
| 3904 | name = null; |
| 3905 | componentName = null; |
| 3906 | } catch (RuntimeException e) { |
| 3907 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3908 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3909 | |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 3910 | for (int user : users) { |
Dianne Hackborn | 13c590d | 2013-10-07 14:32:00 -0700 | [diff] [blame] | 3911 | ServiceMap smap = mServiceMap.get(user); |
| 3912 | if (smap == null) { |
| 3913 | continue; |
| 3914 | } |
| 3915 | ArrayMap<ComponentName, ServiceRecord> alls = smap.mServicesByName; |
Dianne Hackborn | 9210bc8 | 2013-09-05 12:31:16 -0700 | [diff] [blame] | 3916 | for (int i=0; i<alls.size(); i++) { |
| 3917 | ServiceRecord r1 = alls.valueAt(i); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 3918 | if (componentName != null) { |
| 3919 | if (r1.name.equals(componentName)) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3920 | services.add(r1); |
| 3921 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 3922 | } else if (name != null) { |
| 3923 | if (r1.name.flattenToString().contains(name)) { |
| 3924 | services.add(r1); |
| 3925 | } |
| 3926 | } else if (System.identityHashCode(r1) == objectId) { |
| 3927 | services.add(r1); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3928 | } |
| 3929 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3930 | } |
| 3931 | } |
| 3932 | } |
| 3933 | |
| 3934 | if (services.size() <= 0) { |
| 3935 | return false; |
| 3936 | } |
| 3937 | |
| 3938 | boolean needSep = false; |
| 3939 | for (int i=0; i<services.size(); i++) { |
| 3940 | if (needSep) { |
| 3941 | pw.println(); |
| 3942 | } |
| 3943 | needSep = true; |
| 3944 | dumpService("", fd, pw, services.get(i), args, dumpAll); |
| 3945 | } |
| 3946 | return true; |
| 3947 | } |
| 3948 | |
| 3949 | /** |
| 3950 | * Invokes IApplicationThread.dumpService() on the thread of the specified service if |
| 3951 | * there is a thread associated with the service. |
| 3952 | */ |
| 3953 | private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw, |
| 3954 | final ServiceRecord r, String[] args, boolean dumpAll) { |
| 3955 | String innerPrefix = prefix + " "; |
Dianne Hackborn | c2f6f94 | 2014-09-22 13:36:42 -0700 | [diff] [blame] | 3956 | synchronized (mAm) { |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3957 | pw.print(prefix); pw.print("SERVICE "); |
| 3958 | pw.print(r.shortName); pw.print(" "); |
| 3959 | pw.print(Integer.toHexString(System.identityHashCode(r))); |
| 3960 | pw.print(" pid="); |
| 3961 | if (r.app != null) pw.println(r.app.pid); |
| 3962 | else pw.println("(not running)"); |
| 3963 | if (dumpAll) { |
| 3964 | r.dump(pw, innerPrefix); |
| 3965 | } |
| 3966 | } |
| 3967 | if (r.app != null && r.app.thread != null) { |
| 3968 | pw.print(prefix); pw.println(" Client:"); |
| 3969 | pw.flush(); |
| 3970 | try { |
| 3971 | TransferPipe tp = new TransferPipe(); |
| 3972 | try { |
Sudheer Shanka | cc6418f | 2016-10-13 12:03:44 -0700 | [diff] [blame] | 3973 | r.app.thread.dumpService(tp.getWriteFd(), r, args); |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3974 | tp.setBufferPrefix(prefix + " "); |
| 3975 | tp.go(fd); |
| 3976 | } finally { |
| 3977 | tp.kill(); |
| 3978 | } |
| 3979 | } catch (IOException e) { |
| 3980 | pw.println(prefix + " Failure while dumping the service: " + e); |
| 3981 | } catch (RemoteException e) { |
| 3982 | pw.println(prefix + " Got a RemoteException while dumping the service"); |
| 3983 | } |
| 3984 | } |
| 3985 | } |
Dianne Hackborn | 599db5c | 2012-08-03 19:28:48 -0700 | [diff] [blame] | 3986 | } |