Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package com.android.server.stats; |
| 17 | |
Jeffrey Huang | 447a078 | 2019-11-26 15:10:13 -0800 | [diff] [blame] | 18 | import static android.os.Process.THREAD_PRIORITY_BACKGROUND; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 19 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 20 | import android.app.AlarmManager; |
| 21 | import android.app.AlarmManager.OnAlarmListener; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 22 | import android.app.StatsManager; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Muhammad Qureshi | ddfc1ba | 2020-02-19 13:41:59 -0800 | [diff] [blame] | 24 | import android.content.ComponentName; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageInfo; |
| 29 | import android.content.pm.PackageManager; |
Muhammad Qureshi | ddfc1ba | 2020-02-19 13:41:59 -0800 | [diff] [blame] | 30 | import android.content.pm.ResolveInfo; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 31 | import android.os.Binder; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
Anton Hansson | d0ab8c9 | 2020-03-19 15:40:36 +0000 | [diff] [blame] | 33 | import android.os.FileUtils; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 34 | import android.os.Handler; |
| 35 | import android.os.HandlerThread; |
| 36 | import android.os.IBinder; |
| 37 | import android.os.IStatsCompanionService; |
Jeffrey Huang | 9d2dbad | 2019-12-11 14:47:32 -0800 | [diff] [blame] | 38 | import android.os.IStatsd; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 39 | import android.os.Looper; |
| 40 | import android.os.ParcelFileDescriptor; |
Jeffrey Huang | 3d19ddb | 2020-03-11 14:58:13 -0700 | [diff] [blame] | 41 | import android.os.PowerManager; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 42 | import android.os.RemoteException; |
Jeffrey Huang | 4979074 | 2020-01-23 13:22:10 -0800 | [diff] [blame] | 43 | import android.os.StatsFrameworkInitializer; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 44 | import android.os.SystemClock; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 45 | import android.os.UserHandle; |
| 46 | import android.os.UserManager; |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 47 | import android.util.Log; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 48 | import android.util.proto.ProtoOutputStream; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 49 | |
| 50 | import com.android.internal.annotations.GuardedBy; |
Ioannis Ilkos | 589ac64 | 2019-11-07 18:57:48 +0000 | [diff] [blame] | 51 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 52 | import java.io.File; |
| 53 | import java.io.FileDescriptor; |
| 54 | import java.io.FileOutputStream; |
| 55 | import java.io.IOException; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 56 | import java.io.PrintWriter; |
Jeffrey Huang | a0d48eb | 2020-10-26 14:27:48 -0700 | [diff] [blame] | 57 | import java.util.ArrayList; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 58 | import java.util.HashMap; |
| 59 | import java.util.HashSet; |
| 60 | import java.util.List; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 61 | import java.util.concurrent.TimeUnit; |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 62 | import java.util.concurrent.atomic.AtomicBoolean; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Helper service for statsd (the native stats management service in cmds/statsd/). |
| 66 | * Used for registering and receiving alarms on behalf of statsd. |
| 67 | * |
| 68 | * @hide |
| 69 | */ |
| 70 | public class StatsCompanionService extends IStatsCompanionService.Stub { |
Jeffrey Huang | 2b24125 | 2020-01-23 14:13:25 -0800 | [diff] [blame] | 71 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 72 | private static final long MILLIS_IN_A_DAY = TimeUnit.DAYS.toMillis(1); |
| 73 | |
| 74 | public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity"; |
| 75 | public static final String CONFIG_DIR = "/data/misc/stats-service"; |
| 76 | |
| 77 | static final String TAG = "StatsCompanionService"; |
| 78 | static final boolean DEBUG = false; |
| 79 | /** |
| 80 | * Hard coded field ids of frameworks/base/cmds/statsd/src/uid_data.proto |
| 81 | * to be used in ProtoOutputStream. |
| 82 | */ |
| 83 | private static final int APPLICATION_INFO_FIELD_ID = 1; |
| 84 | private static final int UID_FIELD_ID = 1; |
| 85 | private static final int VERSION_FIELD_ID = 2; |
| 86 | private static final int VERSION_STRING_FIELD_ID = 3; |
| 87 | private static final int PACKAGE_NAME_FIELD_ID = 4; |
| 88 | private static final int INSTALLER_FIELD_ID = 5; |
| 89 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 90 | public static final int DEATH_THRESHOLD = 10; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 91 | |
| 92 | static final class CompanionHandler extends Handler { |
| 93 | CompanionHandler(Looper looper) { |
| 94 | super(looper); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private final Context mContext; |
| 99 | private final AlarmManager mAlarmManager; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 100 | @GuardedBy("sStatsdLock") |
Jeffrey Huang | 9d2dbad | 2019-12-11 14:47:32 -0800 | [diff] [blame] | 101 | private static IStatsd sStatsd; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 102 | private static final Object sStatsdLock = new Object(); |
| 103 | |
Jeffrey Huang | 3d19ddb | 2020-03-11 14:58:13 -0700 | [diff] [blame] | 104 | private final OnAlarmListener mPullingAlarmListener; |
| 105 | private final OnAlarmListener mPeriodicAlarmListener; |
Tej Singh | b877e35 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 106 | |
Jeffrey Huang | 3b3304e | 2019-12-12 10:56:24 -0800 | [diff] [blame] | 107 | private StatsManagerService mStatsManagerService; |
| 108 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 109 | @GuardedBy("sStatsdLock") |
| 110 | private final HashSet<Long> mDeathTimeMillis = new HashSet<>(); |
| 111 | @GuardedBy("sStatsdLock") |
| 112 | private final HashMap<Long, String> mDeletedFiles = new HashMap<>(); |
| 113 | private final CompanionHandler mHandler; |
| 114 | |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 115 | // Flag that is set when PHASE_BOOT_COMPLETED is triggered in the StatsCompanion lifecycle. |
| 116 | private AtomicBoolean mBootCompleted = new AtomicBoolean(false); |
Jeffrey Huang | 9ebb572 | 2020-04-06 18:09:55 -0700 | [diff] [blame] | 117 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 118 | public StatsCompanionService(Context context) { |
| 119 | super(); |
| 120 | mContext = context; |
| 121 | mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 122 | if (DEBUG) Log.d(TAG, "Registered receiver for ACTION_PACKAGE_REPLACED and ADDED."); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 123 | HandlerThread handlerThread = new HandlerThread(TAG); |
| 124 | handlerThread.start(); |
| 125 | mHandler = new CompanionHandler(handlerThread.getLooper()); |
| 126 | |
Jeffrey Huang | 3d19ddb | 2020-03-11 14:58:13 -0700 | [diff] [blame] | 127 | mPullingAlarmListener = new PullingAlarmListener(context); |
| 128 | mPeriodicAlarmListener = new PeriodicAlarmListener(context); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 131 | private final static int[] toIntArray(List<Integer> list) { |
| 132 | int[] ret = new int[list.size()]; |
| 133 | for (int i = 0; i < ret.length; i++) { |
| 134 | ret[i] = list.get(i); |
| 135 | } |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | private final static long[] toLongArray(List<Long> list) { |
| 140 | long[] ret = new long[list.size()]; |
| 141 | for (int i = 0; i < ret.length; i++) { |
| 142 | ret[i] = list.get(i); |
| 143 | } |
| 144 | return ret; |
| 145 | } |
| 146 | |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 147 | /** |
| 148 | * Non-blocking call to retrieve a reference to statsd |
| 149 | * |
| 150 | * @return IStatsd object if statsd is ready, null otherwise. |
| 151 | */ |
| 152 | private static IStatsd getStatsdNonblocking() { |
| 153 | synchronized (sStatsdLock) { |
| 154 | return sStatsd; |
| 155 | } |
| 156 | } |
| 157 | |
Jeffrey Huang | 1643a5e | 2020-03-31 16:43:32 -0700 | [diff] [blame] | 158 | private static void informAllUids(Context context) { |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 159 | ParcelFileDescriptor[] fds; |
| 160 | try { |
| 161 | fds = ParcelFileDescriptor.createPipe(); |
| 162 | } catch (IOException e) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 163 | Log.e(TAG, "Failed to create a pipe to send uid map data.", e); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 164 | return; |
| 165 | } |
Jeffrey Huang | 447a078 | 2019-11-26 15:10:13 -0800 | [diff] [blame] | 166 | HandlerThread backgroundThread = new HandlerThread( |
| 167 | "statsCompanionService.bg", THREAD_PRIORITY_BACKGROUND); |
| 168 | backgroundThread.start(); |
| 169 | Handler handler = new Handler(backgroundThread.getLooper()); |
| 170 | handler.post(() -> { |
Jeffrey Huang | bde5439 | 2020-04-10 17:40:59 -0700 | [diff] [blame] | 171 | UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 172 | PackageManager pm = context.getPackageManager(); |
| 173 | final List<UserHandle> users = um.getUserHandles(true); |
| 174 | if (DEBUG) { |
| 175 | Log.d(TAG, "Iterating over " + users.size() + " userHandles."); |
| 176 | } |
Jeffrey Huang | 1643a5e | 2020-03-31 16:43:32 -0700 | [diff] [blame] | 177 | IStatsd statsd = getStatsdNonblocking(); |
| 178 | if (statsd == null) { |
| 179 | return; |
| 180 | } |
| 181 | try { |
| 182 | statsd.informAllUidData(fds[0]); |
| 183 | } catch (RemoteException e) { |
| 184 | Log.e(TAG, "Failed to send uid map to statsd"); |
| 185 | } |
| 186 | try { |
| 187 | fds[0].close(); |
| 188 | } catch (IOException e) { |
| 189 | Log.e(TAG, "Failed to close the read side of the pipe.", e); |
| 190 | } |
| 191 | final ParcelFileDescriptor writeFd = fds[1]; |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 192 | FileOutputStream fout = new ParcelFileDescriptor.AutoCloseOutputStream(writeFd); |
| 193 | try { |
| 194 | ProtoOutputStream output = new ProtoOutputStream(fout); |
| 195 | int numRecords = 0; |
| 196 | // Add in all the apps for every user/profile. |
Jeffrey Huang | 4821674 | 2019-12-19 13:02:11 -0800 | [diff] [blame] | 197 | for (UserHandle userHandle : users) { |
Jeffrey Huang | a0d48eb | 2020-10-26 14:27:48 -0700 | [diff] [blame] | 198 | List<PackageInfo> packagesPlusApex = getAllPackagesWithApex(pm, userHandle); |
| 199 | for (int j = 0; j < packagesPlusApex.size(); j++) { |
| 200 | if (packagesPlusApex.get(j).applicationInfo != null) { |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 201 | String installer; |
| 202 | try { |
Jeffrey Huang | a0d48eb | 2020-10-26 14:27:48 -0700 | [diff] [blame] | 203 | installer = pm.getInstallerPackageName( |
| 204 | packagesPlusApex.get(j).packageName); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 205 | } catch (IllegalArgumentException e) { |
| 206 | installer = ""; |
| 207 | } |
| 208 | long applicationInfoToken = |
Jeffrey Huang | f08724e | 2019-12-02 13:31:57 -0800 | [diff] [blame] | 209 | output.start(ProtoOutputStream.FIELD_TYPE_MESSAGE |
| 210 | | ProtoOutputStream.FIELD_COUNT_REPEATED |
Jeffrey Huang | a0d48eb | 2020-10-26 14:27:48 -0700 | [diff] [blame] | 211 | | APPLICATION_INFO_FIELD_ID); |
Jeffrey Huang | f08724e | 2019-12-02 13:31:57 -0800 | [diff] [blame] | 212 | output.write(ProtoOutputStream.FIELD_TYPE_INT32 |
Jeffrey Huang | a0d48eb | 2020-10-26 14:27:48 -0700 | [diff] [blame] | 213 | | ProtoOutputStream.FIELD_COUNT_SINGLE | UID_FIELD_ID, |
| 214 | packagesPlusApex.get(j).applicationInfo.uid); |
Jeffrey Huang | f08724e | 2019-12-02 13:31:57 -0800 | [diff] [blame] | 215 | output.write(ProtoOutputStream.FIELD_TYPE_INT64 |
Jeffrey Huang | a0d48eb | 2020-10-26 14:27:48 -0700 | [diff] [blame] | 216 | | ProtoOutputStream.FIELD_COUNT_SINGLE |
| 217 | | VERSION_FIELD_ID, |
| 218 | packagesPlusApex.get(j).getLongVersionCode()); |
| 219 | output.write(ProtoOutputStream.FIELD_TYPE_STRING |
| 220 | | ProtoOutputStream.FIELD_COUNT_SINGLE |
| 221 | | VERSION_STRING_FIELD_ID, |
| 222 | packagesPlusApex.get(j).versionName); |
Jeffrey Huang | f08724e | 2019-12-02 13:31:57 -0800 | [diff] [blame] | 223 | output.write(ProtoOutputStream.FIELD_TYPE_STRING |
| 224 | | ProtoOutputStream.FIELD_COUNT_SINGLE |
Jeffrey Huang | a0d48eb | 2020-10-26 14:27:48 -0700 | [diff] [blame] | 225 | | PACKAGE_NAME_FIELD_ID, packagesPlusApex.get(j).packageName); |
Jeffrey Huang | f08724e | 2019-12-02 13:31:57 -0800 | [diff] [blame] | 226 | output.write(ProtoOutputStream.FIELD_TYPE_STRING |
Jeffrey Huang | a0d48eb | 2020-10-26 14:27:48 -0700 | [diff] [blame] | 227 | | ProtoOutputStream.FIELD_COUNT_SINGLE |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 228 | | INSTALLER_FIELD_ID, |
Jeffrey Huang | a0d48eb | 2020-10-26 14:27:48 -0700 | [diff] [blame] | 229 | installer == null ? "" : installer); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 230 | numRecords++; |
| 231 | output.end(applicationInfoToken); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | output.flush(); |
| 236 | if (DEBUG) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 237 | Log.d(TAG, "Sent data for " + numRecords + " apps"); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 238 | } |
| 239 | } finally { |
Anton Hansson | d0ab8c9 | 2020-03-19 15:40:36 +0000 | [diff] [blame] | 240 | FileUtils.closeQuietly(fout); |
Jeffrey Huang | 447a078 | 2019-11-26 15:10:13 -0800 | [diff] [blame] | 241 | backgroundThread.quit(); |
| 242 | backgroundThread.interrupt(); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 243 | } |
| 244 | }); |
| 245 | } |
| 246 | |
Jeffrey Huang | a0d48eb | 2020-10-26 14:27:48 -0700 | [diff] [blame] | 247 | private static List<PackageInfo> getAllPackagesWithApex(PackageManager pm, |
| 248 | UserHandle userHandle) { |
| 249 | // We want all the uninstalled packages because uninstalled package uids can still be logged |
| 250 | // to statsd. |
| 251 | List<PackageInfo> allPackages = new ArrayList<>( |
| 252 | pm.getInstalledPackagesAsUser(PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 253 | | PackageManager.MATCH_ANY_USER, |
| 254 | userHandle.getIdentifier())); |
| 255 | // We make a second query to package manager for the apex modules because package manager |
| 256 | // returns both installed and uninstalled apexes with |
| 257 | // PackageManager.MATCH_UNINSTALLED_PACKAGES flag. We only want active apexes because |
| 258 | // inactive apexes can conflict with active ones. |
| 259 | for (PackageInfo packageInfo : pm.getInstalledPackages(PackageManager.MATCH_APEX)) { |
| 260 | if (packageInfo.isApex) { |
| 261 | allPackages.add(packageInfo); |
| 262 | } |
| 263 | } |
| 264 | return allPackages; |
| 265 | } |
| 266 | |
Jeffrey Huang | 3d19ddb | 2020-03-11 14:58:13 -0700 | [diff] [blame] | 267 | private static class WakelockThread extends Thread { |
| 268 | private final PowerManager.WakeLock mWl; |
| 269 | private final Runnable mRunnable; |
| 270 | |
| 271 | WakelockThread(Context context, String wakelockName, Runnable runnable) { |
| 272 | PowerManager powerManager = (PowerManager) |
| 273 | context.getSystemService(Context.POWER_SERVICE); |
| 274 | mWl = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakelockName); |
| 275 | mRunnable = runnable; |
| 276 | } |
| 277 | @Override |
| 278 | public void run() { |
| 279 | try { |
| 280 | mRunnable.run(); |
| 281 | } finally { |
| 282 | mWl.release(); |
| 283 | } |
| 284 | } |
| 285 | @Override |
| 286 | public void start() { |
| 287 | mWl.acquire(); |
| 288 | super.start(); |
| 289 | } |
| 290 | } |
| 291 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 292 | private final static class AppUpdateReceiver extends BroadcastReceiver { |
| 293 | @Override |
| 294 | public void onReceive(Context context, Intent intent) { |
| 295 | /** |
| 296 | * App updates actually consist of REMOVE, ADD, and then REPLACE broadcasts. To avoid |
| 297 | * waste, we ignore the REMOVE and ADD broadcasts that contain the replacing flag. |
| 298 | * If we can't find the value for EXTRA_REPLACING, we default to false. |
| 299 | */ |
| 300 | if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) |
| 301 | && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 302 | return; // Keep only replacing or normal add and remove. |
| 303 | } |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 304 | if (DEBUG) Log.d(TAG, "StatsCompanionService noticed an app was updated."); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 305 | synchronized (sStatsdLock) { |
| 306 | if (sStatsd == null) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 307 | Log.w(TAG, "Could not access statsd to inform it of an app update"); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | try { |
| 311 | if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) { |
| 312 | Bundle b = intent.getExtras(); |
| 313 | int uid = b.getInt(Intent.EXTRA_UID); |
| 314 | boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 315 | if (!replacing) { |
| 316 | // Don't bother sending an update if we're right about to get another |
| 317 | // intent for the new version that's added. |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 318 | String app = intent.getData().getSchemeSpecificPart(); |
| 319 | sStatsd.informOnePackageRemoved(app, uid); |
| 320 | } |
| 321 | } else { |
| 322 | PackageManager pm = context.getPackageManager(); |
| 323 | Bundle b = intent.getExtras(); |
| 324 | int uid = b.getInt(Intent.EXTRA_UID); |
| 325 | String app = intent.getData().getSchemeSpecificPart(); |
| 326 | PackageInfo pi = pm.getPackageInfo(app, PackageManager.MATCH_ANY_USER); |
| 327 | String installer; |
| 328 | try { |
| 329 | installer = pm.getInstallerPackageName(app); |
| 330 | } catch (IllegalArgumentException e) { |
| 331 | installer = ""; |
| 332 | } |
Tony Mak | d04f7d5 | 2019-11-04 13:01:11 +0000 | [diff] [blame] | 333 | sStatsd.informOnePackage( |
| 334 | app, |
| 335 | uid, |
| 336 | pi.getLongVersionCode(), |
| 337 | pi.versionName == null ? "" : pi.versionName, |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 338 | installer == null ? "" : installer); |
| 339 | } |
| 340 | } catch (Exception e) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 341 | Log.w(TAG, "Failed to inform statsd of an app update", e); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 347 | private static final class UserUpdateReceiver extends BroadcastReceiver { |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 348 | @Override |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 349 | public void onReceive(Context context, Intent intent) { |
Jeffrey Huang | 1643a5e | 2020-03-31 16:43:32 -0700 | [diff] [blame] | 350 | // Pull the latest state of UID->app name, version mapping. |
| 351 | // Needed since the new user basically has a version of every app. |
| 352 | informAllUids(context); |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 356 | public final static class PullingAlarmListener implements OnAlarmListener { |
Jeffrey Huang | 3d19ddb | 2020-03-11 14:58:13 -0700 | [diff] [blame] | 357 | private final Context mContext; |
| 358 | |
| 359 | PullingAlarmListener(Context context) { |
| 360 | mContext = context; |
| 361 | } |
| 362 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 363 | @Override |
| 364 | public void onAlarm() { |
| 365 | if (DEBUG) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 366 | Log.d(TAG, "Time to poll something."); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 367 | } |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 368 | IStatsd statsd = getStatsdNonblocking(); |
| 369 | if (statsd == null) { |
| 370 | Log.w(TAG, "Could not access statsd to inform it of pulling alarm firing."); |
| 371 | return; |
| 372 | } |
Jeffrey Huang | 3d19ddb | 2020-03-11 14:58:13 -0700 | [diff] [blame] | 373 | |
| 374 | // Wakelock needs to be retained while calling statsd. |
| 375 | Thread thread = new WakelockThread(mContext, |
| 376 | PullingAlarmListener.class.getCanonicalName(), new Runnable() { |
| 377 | @Override |
| 378 | public void run() { |
| 379 | try { |
| 380 | statsd.informPollAlarmFired(); |
| 381 | } catch (RemoteException e) { |
| 382 | Log.w(TAG, "Failed to inform statsd of pulling alarm firing.", e); |
| 383 | } |
| 384 | } |
| 385 | }); |
| 386 | thread.start(); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | public final static class PeriodicAlarmListener implements OnAlarmListener { |
Jeffrey Huang | 3d19ddb | 2020-03-11 14:58:13 -0700 | [diff] [blame] | 391 | private final Context mContext; |
| 392 | |
| 393 | PeriodicAlarmListener(Context context) { |
| 394 | mContext = context; |
| 395 | } |
| 396 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 397 | @Override |
| 398 | public void onAlarm() { |
| 399 | if (DEBUG) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 400 | Log.d(TAG, "Time to trigger periodic alarm."); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 401 | } |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 402 | IStatsd statsd = getStatsdNonblocking(); |
| 403 | if (statsd == null) { |
| 404 | Log.w(TAG, "Could not access statsd to inform it of periodic alarm firing."); |
| 405 | return; |
| 406 | } |
Jeffrey Huang | 3d19ddb | 2020-03-11 14:58:13 -0700 | [diff] [blame] | 407 | |
| 408 | // Wakelock needs to be retained while calling statsd. |
| 409 | Thread thread = new WakelockThread(mContext, |
| 410 | PeriodicAlarmListener.class.getCanonicalName(), new Runnable() { |
| 411 | @Override |
| 412 | public void run() { |
| 413 | try { |
| 414 | statsd.informAlarmForSubscriberTriggeringFired(); |
| 415 | } catch (RemoteException e) { |
| 416 | Log.w(TAG, "Failed to inform statsd of periodic alarm firing.", e); |
| 417 | } |
| 418 | } |
| 419 | }); |
| 420 | thread.start(); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | public final static class ShutdownEventReceiver extends BroadcastReceiver { |
| 425 | @Override |
| 426 | public void onReceive(Context context, Intent intent) { |
| 427 | /** |
| 428 | * Skip immediately if intent is not relevant to device shutdown. |
| 429 | */ |
| 430 | if (!intent.getAction().equals(Intent.ACTION_REBOOT) |
| 431 | && !(intent.getAction().equals(Intent.ACTION_SHUTDOWN) |
| 432 | && (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0)) { |
| 433 | return; |
| 434 | } |
| 435 | |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 436 | if (DEBUG) { |
| 437 | Log.i(TAG, "StatsCompanionService noticed a shutdown."); |
| 438 | } |
| 439 | IStatsd statsd = getStatsdNonblocking(); |
| 440 | if (statsd == null) { |
| 441 | Log.w(TAG, "Could not access statsd to inform it of a shutdown event."); |
| 442 | return; |
| 443 | } |
| 444 | try { |
| 445 | // two way binder call |
| 446 | statsd.informDeviceShutdown(); |
| 447 | } catch (Exception e) { |
| 448 | Log.w(TAG, "Failed to inform statsd of a shutdown event.", e); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | @Override // Binder call |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 454 | public void setAlarmForSubscriberTriggering(long timestampMs) { |
Jeffrey Huang | 3dea539 | 2020-02-05 13:57:20 -0800 | [diff] [blame] | 455 | StatsCompanion.enforceStatsdCallingUid(); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 456 | if (DEBUG) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 457 | Log.d(TAG, |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 458 | "Setting periodic alarm in about " + (timestampMs |
| 459 | - SystemClock.elapsedRealtime())); |
| 460 | } |
| 461 | final long callingToken = Binder.clearCallingIdentity(); |
| 462 | try { |
| 463 | // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will |
| 464 | // only fire when it awakens. |
| 465 | mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, TAG + ".periodic", |
| 466 | mPeriodicAlarmListener, mHandler); |
| 467 | } finally { |
| 468 | Binder.restoreCallingIdentity(callingToken); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | @Override // Binder call |
| 473 | public void cancelAlarmForSubscriberTriggering() { |
Jeffrey Huang | 3dea539 | 2020-02-05 13:57:20 -0800 | [diff] [blame] | 474 | StatsCompanion.enforceStatsdCallingUid(); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 475 | if (DEBUG) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 476 | Log.d(TAG, "Cancelling periodic alarm"); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 477 | } |
| 478 | final long callingToken = Binder.clearCallingIdentity(); |
| 479 | try { |
| 480 | mAlarmManager.cancel(mPeriodicAlarmListener); |
| 481 | } finally { |
| 482 | Binder.restoreCallingIdentity(callingToken); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | @Override // Binder call |
| 487 | public void setPullingAlarm(long nextPullTimeMs) { |
Jeffrey Huang | 3dea539 | 2020-02-05 13:57:20 -0800 | [diff] [blame] | 488 | StatsCompanion.enforceStatsdCallingUid(); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 489 | if (DEBUG) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 490 | Log.d(TAG, "Setting pulling alarm in about " |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 491 | + (nextPullTimeMs - SystemClock.elapsedRealtime())); |
| 492 | } |
| 493 | final long callingToken = Binder.clearCallingIdentity(); |
| 494 | try { |
| 495 | // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will |
| 496 | // only fire when it awakens. |
| 497 | mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, nextPullTimeMs, TAG + ".pull", |
| 498 | mPullingAlarmListener, mHandler); |
| 499 | } finally { |
| 500 | Binder.restoreCallingIdentity(callingToken); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | @Override // Binder call |
| 505 | public void cancelPullingAlarm() { |
Jeffrey Huang | 3dea539 | 2020-02-05 13:57:20 -0800 | [diff] [blame] | 506 | StatsCompanion.enforceStatsdCallingUid(); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 507 | if (DEBUG) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 508 | Log.d(TAG, "Cancelling pulling alarm"); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 509 | } |
| 510 | final long callingToken = Binder.clearCallingIdentity(); |
| 511 | try { |
| 512 | mAlarmManager.cancel(mPullingAlarmListener); |
| 513 | } finally { |
| 514 | Binder.restoreCallingIdentity(callingToken); |
| 515 | } |
| 516 | } |
| 517 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 518 | @Override // Binder call |
| 519 | public void statsdReady() { |
Jeffrey Huang | 3dea539 | 2020-02-05 13:57:20 -0800 | [diff] [blame] | 520 | StatsCompanion.enforceStatsdCallingUid(); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 521 | if (DEBUG) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 522 | Log.d(TAG, "learned that statsdReady"); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 523 | } |
| 524 | sayHiToStatsd(); // tell statsd that we're ready too and link to it |
Muhammad Qureshi | ddfc1ba | 2020-02-19 13:41:59 -0800 | [diff] [blame] | 525 | |
| 526 | final Intent intent = new Intent(StatsManager.ACTION_STATSD_STARTED); |
| 527 | // Retrieve list of broadcast receivers for this broadcast & send them directed broadcasts |
| 528 | // to wake them up (if they're in background). |
| 529 | List<ResolveInfo> resolveInfos = |
| 530 | mContext.getPackageManager().queryBroadcastReceiversAsUser( |
| 531 | intent, 0, UserHandle.SYSTEM); |
| 532 | if (resolveInfos == null || resolveInfos.isEmpty()) { |
| 533 | return; // No need to send broadcast. |
| 534 | } |
| 535 | |
| 536 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 537 | Intent intentToSend = new Intent(intent); |
| 538 | intentToSend.setComponent(new ComponentName( |
| 539 | resolveInfo.activityInfo.applicationInfo.packageName, |
| 540 | resolveInfo.activityInfo.name)); |
| 541 | mContext.sendBroadcastAsUser(intentToSend, UserHandle.SYSTEM, |
| 542 | android.Manifest.permission.DUMP); |
| 543 | } |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 546 | @Override // Binder call |
Ruchir Rastogi | 9c65baf | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 547 | public boolean checkPermission(String permission, int pid, int uid) { |
Jeffrey Huang | 3dea539 | 2020-02-05 13:57:20 -0800 | [diff] [blame] | 548 | StatsCompanion.enforceStatsdCallingUid(); |
Ruchir Rastogi | 9c65baf | 2020-01-28 17:43:13 -0800 | [diff] [blame] | 549 | return mContext.checkPermission(permission, pid, uid) == PackageManager.PERMISSION_GRANTED; |
| 550 | } |
Tej Singh | 774d6cd | 2019-12-05 20:36:54 -0800 | [diff] [blame] | 551 | |
Jeffrey Huang | 3b3304e | 2019-12-12 10:56:24 -0800 | [diff] [blame] | 552 | // Statsd related code |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 553 | |
| 554 | /** |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 555 | * Fetches the statsd IBinder service. This is a blocking call that always refetches statsd |
| 556 | * instead of returning the cached sStatsd. |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 557 | * Note: This should only be called from {@link #sayHiToStatsd()}. All other clients should use |
| 558 | * the cached sStatsd via {@link #getStatsdNonblocking()}. |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 559 | */ |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 560 | private IStatsd fetchStatsdServiceLocked() { |
| 561 | sStatsd = IStatsd.Stub.asInterface(StatsFrameworkInitializer |
| 562 | .getStatsServiceManager() |
| 563 | .getStatsdServiceRegisterer() |
| 564 | .get()); |
| 565 | return sStatsd; |
| 566 | } |
| 567 | |
| 568 | private void registerStatsdDeathRecipient(IStatsd statsd, List<BroadcastReceiver> receivers) { |
| 569 | StatsdDeathRecipient deathRecipient = new StatsdDeathRecipient(statsd, receivers); |
| 570 | |
| 571 | try { |
| 572 | statsd.asBinder().linkToDeath(deathRecipient, /*flags=*/0); |
| 573 | } catch (RemoteException e) { |
| 574 | Log.e(TAG, "linkToDeath (StatsdDeathRecipient) failed"); |
| 575 | // Statsd has already died. Unregister receivers ourselves. |
| 576 | for (BroadcastReceiver receiver : receivers) { |
| 577 | mContext.unregisterReceiver(receiver); |
| 578 | } |
| 579 | synchronized (sStatsdLock) { |
| 580 | if (statsd == sStatsd) { |
| 581 | statsdNotReadyLocked(); |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 582 | } |
| 583 | } |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 584 | } |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 587 | /** |
| 588 | * Now that the android system is ready, StatsCompanion is ready too, so inform statsd. |
| 589 | */ |
Jeffrey Huang | 3b3304e | 2019-12-12 10:56:24 -0800 | [diff] [blame] | 590 | void systemReady() { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 591 | if (DEBUG) Log.d(TAG, "Learned that systemReady"); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 592 | sayHiToStatsd(); |
| 593 | } |
| 594 | |
Jeffrey Huang | 3b3304e | 2019-12-12 10:56:24 -0800 | [diff] [blame] | 595 | void setStatsManagerService(StatsManagerService statsManagerService) { |
| 596 | mStatsManagerService = statsManagerService; |
| 597 | } |
| 598 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 599 | /** |
| 600 | * Tells statsd that statscompanion is ready. If the binder call returns, link to |
| 601 | * statsd. |
| 602 | */ |
| 603 | private void sayHiToStatsd() { |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 604 | IStatsd statsd; |
| 605 | synchronized (sStatsdLock) { |
| 606 | if (sStatsd != null && sStatsd.asBinder().isBinderAlive()) { |
| 607 | Log.e(TAG, "statsd has already been fetched before", |
| 608 | new IllegalStateException("IStatsd object should be null or dead")); |
| 609 | return; |
| 610 | } |
| 611 | statsd = fetchStatsdServiceLocked(); |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 612 | } |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 613 | |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 614 | if (statsd == null) { |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 615 | Log.i(TAG, "Could not yet find statsd to tell it that StatsCompanion is alive."); |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 616 | return; |
| 617 | } |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 618 | |
Stanislav Zholnin | 99a16c0 | 2020-05-18 02:37:23 +0100 | [diff] [blame] | 619 | // Cleann up from previous statsd - cancel any alarms that had been set. Do this here |
| 620 | // instead of in binder death because statsd can come back and set different alarms, or not |
| 621 | // want to set an alarm when it had been set. This guarantees that when we get a new statsd, |
| 622 | // we cancel any alarms before it is able to set them. |
Stanislav Zholnin | 99a16c0 | 2020-05-18 02:37:23 +0100 | [diff] [blame] | 623 | cancelPullingAlarm(); |
| 624 | cancelAlarmForSubscriberTriggering(); |
| 625 | |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 626 | if (DEBUG) Log.d(TAG, "Saying hi to statsd"); |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 627 | mStatsManagerService.statsdReady(statsd); |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 628 | try { |
| 629 | statsd.statsCompanionReady(); |
| 630 | |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 631 | BroadcastReceiver appUpdateReceiver = new AppUpdateReceiver(); |
| 632 | BroadcastReceiver userUpdateReceiver = new UserUpdateReceiver(); |
| 633 | BroadcastReceiver shutdownEventReceiver = new ShutdownEventReceiver(); |
| 634 | |
| 635 | // Setup broadcast receiver for updates. |
| 636 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REPLACED); |
| 637 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 638 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 639 | filter.addDataScheme("package"); |
| 640 | mContext.registerReceiverForAllUsers(appUpdateReceiver, filter, null, null); |
| 641 | |
| 642 | // Setup receiver for user initialize (which happens once for a new user) |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 643 | // and if a user is removed. |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 644 | filter = new IntentFilter(Intent.ACTION_USER_INITIALIZE); |
| 645 | filter.addAction(Intent.ACTION_USER_REMOVED); |
| 646 | mContext.registerReceiverForAllUsers(userUpdateReceiver, filter, null, null); |
| 647 | |
| 648 | // Setup receiver for device reboots or shutdowns. |
| 649 | filter = new IntentFilter(Intent.ACTION_REBOOT); |
| 650 | filter.addAction(Intent.ACTION_SHUTDOWN); |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 651 | mContext.registerReceiverForAllUsers(shutdownEventReceiver, filter, null, null); |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 652 | |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 653 | // Register death recipient. |
| 654 | List<BroadcastReceiver> broadcastReceivers = |
| 655 | List.of(appUpdateReceiver, userUpdateReceiver, shutdownEventReceiver); |
| 656 | registerStatsdDeathRecipient(statsd, broadcastReceivers); |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 657 | |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 658 | // Tell statsd that boot has completed. The signal may have already been sent, but since |
| 659 | // the signal-receiving function is idempotent, that's ok. |
| 660 | if (mBootCompleted.get()) { |
Jeffrey Huang | 9ebb572 | 2020-04-06 18:09:55 -0700 | [diff] [blame] | 661 | statsd.bootCompleted(); |
| 662 | } |
| 663 | |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 664 | // Pull the latest state of UID->app name, version mapping when statsd starts. |
Jeffrey Huang | bde5439 | 2020-04-10 17:40:59 -0700 | [diff] [blame] | 665 | informAllUids(mContext); |
| 666 | |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 667 | Log.i(TAG, "Told statsd that StatsCompanionService is alive."); |
| 668 | } catch (RemoteException e) { |
| 669 | Log.e(TAG, "Failed to inform statsd that statscompanion is ready", e); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | private class StatsdDeathRecipient implements IBinder.DeathRecipient { |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 674 | |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 675 | private final IStatsd mStatsd; |
| 676 | private final List<BroadcastReceiver> mReceiversToUnregister; |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 677 | |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 678 | StatsdDeathRecipient(IStatsd statsd, List<BroadcastReceiver> receivers) { |
| 679 | mStatsd = statsd; |
| 680 | mReceiversToUnregister = receivers; |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 683 | // It is possible for binderDied to be called after a restarted statsd calls statsdReady, |
| 684 | // but that's alright because the code does not assume an ordering of the two calls. |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 685 | @Override |
| 686 | public void binderDied() { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 687 | Log.i(TAG, "Statsd is dead - erase all my knowledge, except pullers"); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 688 | synchronized (sStatsdLock) { |
| 689 | long now = SystemClock.elapsedRealtime(); |
| 690 | for (Long timeMillis : mDeathTimeMillis) { |
| 691 | long ageMillis = now - timeMillis; |
| 692 | if (ageMillis > MILLIS_IN_A_DAY) { |
| 693 | mDeathTimeMillis.remove(timeMillis); |
| 694 | } |
| 695 | } |
| 696 | for (Long timeMillis : mDeletedFiles.keySet()) { |
| 697 | long ageMillis = now - timeMillis; |
| 698 | if (ageMillis > MILLIS_IN_A_DAY * 7) { |
| 699 | mDeletedFiles.remove(timeMillis); |
| 700 | } |
| 701 | } |
| 702 | mDeathTimeMillis.add(now); |
| 703 | if (mDeathTimeMillis.size() >= DEATH_THRESHOLD) { |
| 704 | mDeathTimeMillis.clear(); |
Jeffrey Huang | ca9eb67 | 2020-01-22 09:59:00 -0800 | [diff] [blame] | 705 | File[] configs = new File(CONFIG_DIR).listFiles(); |
| 706 | if (configs != null && configs.length > 0) { |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 707 | String fileName = configs[0].getName(); |
| 708 | if (configs[0].delete()) { |
| 709 | mDeletedFiles.put(now, fileName); |
| 710 | } |
| 711 | } |
| 712 | } |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 713 | |
| 714 | // Unregister receivers on death because receivers can only be unregistered once. |
| 715 | // Otherwise, an IllegalArgumentException is thrown. |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 716 | for (BroadcastReceiver receiver: mReceiversToUnregister) { |
| 717 | mContext.unregisterReceiver(receiver); |
| 718 | } |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 719 | |
| 720 | // It's possible for statsd to have restarted and called statsdReady, causing a new |
| 721 | // sStatsd binder object to be fetched, before the binderDied callback runs. Only |
| 722 | // call #statsdNotReadyLocked if that hasn't happened yet. |
| 723 | if (mStatsd == sStatsd) { |
| 724 | statsdNotReadyLocked(); |
| 725 | } |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
Jeffrey Huang | bb01ba9 | 2020-02-26 15:41:11 -0800 | [diff] [blame] | 730 | private void statsdNotReadyLocked() { |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 731 | sStatsd = null; |
Jeffrey Huang | df5eea2 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 732 | mStatsManagerService.statsdNotReady(); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Jeffrey Huang | 9ebb572 | 2020-04-06 18:09:55 -0700 | [diff] [blame] | 735 | void bootCompleted() { |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 736 | mBootCompleted.set(true); |
Jeffrey Huang | 9ebb572 | 2020-04-06 18:09:55 -0700 | [diff] [blame] | 737 | IStatsd statsd = getStatsdNonblocking(); |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 738 | if (statsd == null) { |
| 739 | // Statsd is not yet ready. |
| 740 | // Delay the boot completed ping to {@link #sayHiToStatsd()} |
| 741 | return; |
Jeffrey Huang | 9ebb572 | 2020-04-06 18:09:55 -0700 | [diff] [blame] | 742 | } |
| 743 | try { |
| 744 | statsd.bootCompleted(); |
| 745 | } catch (RemoteException e) { |
| 746 | Log.e(TAG, "Failed to notify statsd that boot completed"); |
| 747 | } |
| 748 | } |
| 749 | |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 750 | @Override |
| 751 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Muhammad Qureshi | e712162 | 2020-02-07 09:04:54 -0800 | [diff] [blame] | 752 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 753 | != PackageManager.PERMISSION_GRANTED) { |
| 754 | return; |
| 755 | } |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 756 | |
| 757 | synchronized (sStatsdLock) { |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 758 | writer.println("Number of configuration files deleted: " + mDeletedFiles.size()); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 759 | if (mDeletedFiles.size() > 0) { |
| 760 | writer.println(" timestamp, deleted file name"); |
| 761 | } |
| 762 | long lastBootMillis = |
| 763 | SystemClock.currentThreadTimeMillis() - SystemClock.elapsedRealtime(); |
| 764 | for (Long elapsedMillis : mDeletedFiles.keySet()) { |
| 765 | long deletionMillis = lastBootMillis + elapsedMillis; |
Ruchir Rastogi | 1fc1799 | 2020-04-22 15:37:32 -0700 | [diff] [blame] | 766 | writer.println(" " + deletionMillis + ", " + mDeletedFiles.get(elapsedMillis)); |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | } |
Tej Singh | 9a1f75b | 2019-09-20 16:29:13 -0700 | [diff] [blame] | 770 | } |