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