The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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; |
| 18 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 19 | import android.app.IActivityController; |
| 20 | import android.os.Binder; |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 21 | import android.os.Build; |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 22 | import android.os.RemoteException; |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 23 | import android.system.ErrnoException; |
| 24 | import android.system.OsConstants; |
| 25 | import android.system.StructRlimit; |
Andreas Gampe | 032a929 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 26 | import com.android.internal.os.ZygoteConnectionConstants; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import com.android.server.am.ActivityManagerService; |
| 28 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
| 30 | import android.content.ContentResolver; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
| 33 | import android.content.IntentFilter; |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 34 | import android.hidl.manager.V1_0.IServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.Debug; |
| 36 | import android.os.Handler; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 37 | import android.os.IPowerManager; |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 38 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.os.Process; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 40 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.os.SystemClock; |
| 42 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.util.EventLog; |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 44 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 45 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 47 | import java.io.File; |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 48 | import java.io.FileWriter; |
| 49 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import java.util.ArrayList; |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 51 | import java.util.Arrays; |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 52 | import java.util.Collections; |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 53 | import java.util.HashSet; |
| 54 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
| 56 | /** This class calls its monitor every minute. Killing this process if they don't return **/ |
| 57 | public class Watchdog extends Thread { |
| 58 | static final String TAG = "Watchdog"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | |
| 60 | // Set this to true to use debug default values. |
| 61 | static final boolean DB = false; |
| 62 | |
Andreas Gampe | 032a929 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 63 | // Note 1: Do not lower this value below thirty seconds without tightening the invoke-with |
| 64 | // timeout in com.android.internal.os.ZygoteConnection, or wrapped applications |
| 65 | // can trigger the watchdog. |
| 66 | // Note 2: The debug value is already below the wait time in ZygoteConnection. Wrapped |
| 67 | // applications may not work with a debug build. CTS will fail. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 68 | static final long DEFAULT_TIMEOUT = DB ? 10*1000 : 60*1000; |
| 69 | static final long CHECK_INTERVAL = DEFAULT_TIMEOUT / 2; |
| 70 | |
| 71 | // These are temporally ordered: larger values as lateness increases |
| 72 | static final int COMPLETED = 0; |
| 73 | static final int WAITING = 1; |
| 74 | static final int WAITED_HALF = 2; |
| 75 | static final int OVERDUE = 3; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | |
Igor Murashkin | 44d04aa | 2013-10-23 10:56:02 -0700 | [diff] [blame] | 77 | // Which native processes to dump into dropbox's stack traces |
| 78 | public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] { |
Andy Hung | 3a64ecb | 2016-03-09 13:55:58 -0800 | [diff] [blame] | 79 | "/system/bin/audioserver", |
Andy Hung | 689574a | 2016-04-13 19:27:43 -0700 | [diff] [blame] | 80 | "/system/bin/cameraserver", |
| 81 | "/system/bin/drmserver", |
| 82 | "/system/bin/mediadrmserver", |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 83 | "/system/bin/mediaserver", |
| 84 | "/system/bin/sdcard", |
Eric Laurent | 05d4e35 | 2016-03-14 18:49:08 -0700 | [diff] [blame] | 85 | "/system/bin/surfaceflinger", |
Andy Hung | 689574a | 2016-04-13 19:27:43 -0700 | [diff] [blame] | 86 | "media.extractor", // system/bin/mediaextractor |
Andy Hung | b1c4c93 | 2018-01-04 12:05:43 -0800 | [diff] [blame] | 87 | "media.metrics", // system/bin/mediametrics |
Chong Zhang | e55e6e0 | 2017-06-02 10:52:04 -0700 | [diff] [blame] | 88 | "media.codec", // vendor/bin/hw/android.hardware.media.omx@1.0-service |
Andreas Gampe | cf9e79b | 2016-05-11 18:41:25 -0700 | [diff] [blame] | 89 | "com.android.bluetooth", // Bluetooth service |
Rafal Slawik | e35d8f8 | 2018-10-01 11:39:58 +0100 | [diff] [blame^] | 90 | "/system/bin/statsd", // Stats daemon |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 93 | public static final List<String> HAL_INTERFACES_OF_INTEREST = Arrays.asList( |
| 94 | "android.hardware.audio@2.0::IDevicesFactory", |
Mikhail Naganov | 09f0c5a | 2018-04-30 15:58:31 -0700 | [diff] [blame] | 95 | "android.hardware.audio@4.0::IDevicesFactory", |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 96 | "android.hardware.bluetooth@1.0::IBluetoothHci", |
| 97 | "android.hardware.camera.provider@2.4::ICameraProvider", |
Chia-I Wu | 74debcd | 2017-04-21 11:14:22 -0700 | [diff] [blame] | 98 | "android.hardware.graphics.composer@2.1::IComposer", |
Peng Xu | 102122fb | 2017-07-11 21:12:11 -0700 | [diff] [blame] | 99 | "android.hardware.media.omx@1.0::IOmx", |
Pawin Vongmasa | f22f5f7 | 2018-04-18 07:01:47 -0700 | [diff] [blame] | 100 | "android.hardware.media.omx@1.0::IOmxStore", |
Peng Xu | 102122fb | 2017-07-11 21:12:11 -0700 | [diff] [blame] | 101 | "android.hardware.sensors@1.0::ISensors", |
| 102 | "android.hardware.vr@1.0::IVr" |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 103 | ); |
| 104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | static Watchdog sWatchdog; |
| 106 | |
| 107 | /* This handler will be used to post message back onto the main thread */ |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 108 | final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 109 | final HandlerChecker mMonitorChecker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | ContentResolver mResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | ActivityManagerService mActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | int mPhonePid; |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 114 | IActivityController mController; |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 115 | boolean mAllowRestart = true; |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 116 | final OpenFdMonitor mOpenFdMonitor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | /** |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 119 | * Used for checking status of handle threads and scheduling monitor callbacks. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | */ |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 121 | public final class HandlerChecker implements Runnable { |
| 122 | private final Handler mHandler; |
| 123 | private final String mName; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 124 | private final long mWaitMax; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 125 | private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 126 | private boolean mCompleted; |
| 127 | private Monitor mCurrentMonitor; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 128 | private long mStartTime; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 129 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 130 | HandlerChecker(Handler handler, String name, long waitMaxMillis) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 131 | mHandler = handler; |
| 132 | mName = name; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 133 | mWaitMax = waitMaxMillis; |
| 134 | mCompleted = true; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public void addMonitor(Monitor monitor) { |
| 138 | mMonitors.add(monitor); |
| 139 | } |
| 140 | |
| 141 | public void scheduleCheckLocked() { |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 142 | if (mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling()) { |
| 143 | // If the target looper has recently been polling, then |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 144 | // there is no reason to enqueue our checker on it since that |
| 145 | // is as good as it not being deadlocked. This avoid having |
| 146 | // to do a context switch to check the thread. Note that we |
| 147 | // only do this if mCheckReboot is false and we have no |
| 148 | // monitors, since those would need to be executed at this point. |
| 149 | mCompleted = true; |
| 150 | return; |
| 151 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 152 | |
| 153 | if (!mCompleted) { |
| 154 | // we already have a check in flight, so no need |
| 155 | return; |
| 156 | } |
| 157 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 158 | mCompleted = false; |
| 159 | mCurrentMonitor = null; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 160 | mStartTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 161 | mHandler.postAtFrontOfQueue(this); |
| 162 | } |
| 163 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 164 | public boolean isOverdueLocked() { |
| 165 | return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax); |
| 166 | } |
| 167 | |
| 168 | public int getCompletionStateLocked() { |
| 169 | if (mCompleted) { |
| 170 | return COMPLETED; |
| 171 | } else { |
| 172 | long latency = SystemClock.uptimeMillis() - mStartTime; |
| 173 | if (latency < mWaitMax/2) { |
| 174 | return WAITING; |
| 175 | } else if (latency < mWaitMax) { |
| 176 | return WAITED_HALF; |
| 177 | } |
| 178 | } |
| 179 | return OVERDUE; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 182 | public Thread getThread() { |
| 183 | return mHandler.getLooper().getThread(); |
| 184 | } |
| 185 | |
| 186 | public String getName() { |
| 187 | return mName; |
| 188 | } |
| 189 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 190 | public String describeBlockedStateLocked() { |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 191 | if (mCurrentMonitor == null) { |
| 192 | return "Blocked in handler on " + mName + " (" + getThread().getName() + ")"; |
| 193 | } else { |
| 194 | return "Blocked in monitor " + mCurrentMonitor.getClass().getName() |
| 195 | + " on " + mName + " (" + getThread().getName() + ")"; |
| 196 | } |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 197 | } |
| 198 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | @Override |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 200 | public void run() { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 201 | final int size = mMonitors.size(); |
| 202 | for (int i = 0 ; i < size ; i++) { |
| 203 | synchronized (Watchdog.this) { |
| 204 | mCurrentMonitor = mMonitors.get(i); |
| 205 | } |
| 206 | mCurrentMonitor.monitor(); |
| 207 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 209 | synchronized (Watchdog.this) { |
| 210 | mCompleted = true; |
| 211 | mCurrentMonitor = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | final class RebootRequestReceiver extends BroadcastReceiver { |
| 217 | @Override |
| 218 | public void onReceive(Context c, Intent intent) { |
Dianne Hackborn | f6438b1 | 2013-05-09 18:53:48 -0700 | [diff] [blame] | 219 | if (intent.getIntExtra("nowait", 0) != 0) { |
| 220 | rebootSystem("Received ACTION_REBOOT broadcast"); |
| 221 | return; |
| 222 | } |
| 223 | Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Wale Ogunwale | 517daec | 2015-04-15 10:27:24 -0700 | [diff] [blame] | 227 | /** Monitor for checking the availability of binder threads. The monitor will block until |
| 228 | * there is a binder thread available to process in coming IPCs to make sure other processes |
| 229 | * can still communicate with the service. |
| 230 | */ |
| 231 | private static final class BinderThreadMonitor implements Watchdog.Monitor { |
| 232 | @Override |
| 233 | public void monitor() { |
| 234 | Binder.blockUntilThreadAvailable(); |
| 235 | } |
| 236 | } |
| 237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | public interface Monitor { |
| 239 | void monitor(); |
| 240 | } |
| 241 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | public static Watchdog getInstance() { |
| 243 | if (sWatchdog == null) { |
| 244 | sWatchdog = new Watchdog(); |
| 245 | } |
| 246 | |
| 247 | return sWatchdog; |
| 248 | } |
| 249 | |
| 250 | private Watchdog() { |
| 251 | super("watchdog"); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 252 | // Initialize handler checkers for each common thread we want to check. Note |
| 253 | // that we are not currently checking the background thread, since it can |
| 254 | // potentially hold longer running operations with no guarantees about the timeliness |
| 255 | // of operations there. |
| 256 | |
| 257 | // The shared foreground thread is the main checker. It is where we |
| 258 | // will also dispatch monitor checks and do other work. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 259 | mMonitorChecker = new HandlerChecker(FgThread.getHandler(), |
| 260 | "foreground thread", DEFAULT_TIMEOUT); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 261 | mHandlerCheckers.add(mMonitorChecker); |
| 262 | // Add checker for main thread. We only do a quick check since there |
| 263 | // can be UI running on the thread. |
| 264 | mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()), |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 265 | "main thread", DEFAULT_TIMEOUT)); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 266 | // Add checker for shared UI thread. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 267 | mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(), |
| 268 | "ui thread", DEFAULT_TIMEOUT)); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 269 | // And also check IO thread. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 270 | mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(), |
| 271 | "i/o thread", DEFAULT_TIMEOUT)); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 272 | // And the display thread. |
| 273 | mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(), |
| 274 | "display thread", DEFAULT_TIMEOUT)); |
Wale Ogunwale | 517daec | 2015-04-15 10:27:24 -0700 | [diff] [blame] | 275 | |
| 276 | // Initialize monitor for Binder threads. |
| 277 | addMonitor(new BinderThreadMonitor()); |
Andreas Gampe | 032a929 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 278 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 279 | mOpenFdMonitor = OpenFdMonitor.create(); |
| 280 | |
Andreas Gampe | 032a929 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 281 | // See the notes on DEFAULT_TIMEOUT. |
| 282 | assert DB || |
| 283 | DEFAULT_TIMEOUT > ZygoteConnectionConstants.WRAPPED_PID_TIMEOUT_MILLIS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 286 | public void init(Context context, ActivityManagerService activity) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | mResolver = context.getContentResolver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | mActivity = activity; |
| 289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | context.registerReceiver(new RebootRequestReceiver(), |
| 291 | new IntentFilter(Intent.ACTION_REBOOT), |
| 292 | android.Manifest.permission.REBOOT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Christopher Tate | c27181c | 2010-06-30 14:41:09 -0700 | [diff] [blame] | 295 | public void processStarted(String name, int pid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | synchronized (this) { |
| 297 | if ("com.android.phone".equals(name)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | mPhonePid = pid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 303 | public void setActivityController(IActivityController controller) { |
| 304 | synchronized (this) { |
| 305 | mController = controller; |
| 306 | } |
| 307 | } |
| 308 | |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 309 | public void setAllowRestart(boolean allowRestart) { |
| 310 | synchronized (this) { |
| 311 | mAllowRestart = allowRestart; |
| 312 | } |
| 313 | } |
| 314 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | public void addMonitor(Monitor monitor) { |
| 316 | synchronized (this) { |
| 317 | if (isAlive()) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 318 | throw new RuntimeException("Monitors can't be added once the Watchdog is running"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 320 | mMonitorChecker.addMonitor(monitor); |
| 321 | } |
| 322 | } |
| 323 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 324 | public void addThread(Handler thread) { |
| 325 | addThread(thread, DEFAULT_TIMEOUT); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 328 | public void addThread(Handler thread, long timeoutMillis) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 329 | synchronized (this) { |
| 330 | if (isAlive()) { |
| 331 | throw new RuntimeException("Threads can't be added once the Watchdog is running"); |
| 332 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 333 | final String name = thread.getLooper().getThread().getName(); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 334 | mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | /** |
| 339 | * Perform a full reboot of the system. |
| 340 | */ |
| 341 | void rebootSystem(String reason) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 342 | Slog.i(TAG, "Rebooting system because: " + reason); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 343 | IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE); |
| 344 | try { |
| 345 | pms.reboot(false, reason, false); |
| 346 | } catch (RemoteException ex) { |
| 347 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 350 | private int evaluateCheckerCompletionLocked() { |
| 351 | int state = COMPLETED; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 352 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 353 | HandlerChecker hc = mHandlerCheckers.get(i); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 354 | state = Math.max(state, hc.getCompletionStateLocked()); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 355 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 356 | return state; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 359 | private ArrayList<HandlerChecker> getBlockedCheckersLocked() { |
| 360 | ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 361 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 362 | HandlerChecker hc = mHandlerCheckers.get(i); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 363 | if (hc.isOverdueLocked()) { |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 364 | checkers.add(hc); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 367 | return checkers; |
| 368 | } |
| 369 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 370 | private String describeCheckersLocked(List<HandlerChecker> checkers) { |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 371 | StringBuilder builder = new StringBuilder(128); |
| 372 | for (int i=0; i<checkers.size(); i++) { |
| 373 | if (builder.length() > 0) { |
| 374 | builder.append(", "); |
| 375 | } |
| 376 | builder.append(checkers.get(i).describeBlockedStateLocked()); |
| 377 | } |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 378 | return builder.toString(); |
| 379 | } |
| 380 | |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 381 | private ArrayList<Integer> getInterestingHalPids() { |
| 382 | try { |
| 383 | IServiceManager serviceManager = IServiceManager.getService(); |
| 384 | ArrayList<IServiceManager.InstanceDebugInfo> dump = |
| 385 | serviceManager.debugDump(); |
| 386 | HashSet<Integer> pids = new HashSet<>(); |
| 387 | for (IServiceManager.InstanceDebugInfo info : dump) { |
| 388 | if (info.pid == IServiceManager.PidConstant.NO_PID) { |
| 389 | continue; |
| 390 | } |
| 391 | |
| 392 | if (!HAL_INTERFACES_OF_INTEREST.contains(info.interfaceName)) { |
| 393 | continue; |
| 394 | } |
| 395 | |
| 396 | pids.add(info.pid); |
| 397 | } |
| 398 | return new ArrayList<Integer>(pids); |
| 399 | } catch (RemoteException e) { |
| 400 | return new ArrayList<Integer>(); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | private ArrayList<Integer> getInterestingNativePids() { |
| 405 | ArrayList<Integer> pids = getInterestingHalPids(); |
| 406 | |
| 407 | int[] nativePids = Process.getPidsForCommands(NATIVE_STACKS_OF_INTEREST); |
| 408 | if (nativePids != null) { |
| 409 | pids.ensureCapacity(pids.size() + nativePids.length); |
| 410 | for (int i : nativePids) { |
| 411 | pids.add(i); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return pids; |
| 416 | } |
| 417 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | @Override |
| 419 | public void run() { |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 420 | boolean waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | while (true) { |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 422 | final List<HandlerChecker> blockedCheckers; |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 423 | final String subject; |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 424 | final boolean allowRestart; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 425 | int debuggerWasConnected = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | synchronized (this) { |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 427 | long timeout = CHECK_INTERVAL; |
| 428 | // Make sure we (re)spin the checkers that have become idle within |
| 429 | // this wait-and-check interval |
| 430 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 431 | HandlerChecker hc = mHandlerCheckers.get(i); |
| 432 | hc.scheduleCheckLocked(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 433 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 435 | if (debuggerWasConnected > 0) { |
| 436 | debuggerWasConnected--; |
| 437 | } |
| 438 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | // NOTE: We use uptimeMillis() here because we do not want to increment the time we |
| 440 | // wait while asleep. If the device is asleep then the thing that we are waiting |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 441 | // to timeout on is asleep as well and won't have a chance to run, causing a false |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | // positive on when to kill things. |
| 443 | long start = SystemClock.uptimeMillis(); |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 444 | while (timeout > 0) { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 445 | if (Debug.isDebuggerConnected()) { |
| 446 | debuggerWasConnected = 2; |
| 447 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | try { |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 449 | wait(timeout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | } catch (InterruptedException e) { |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 451 | Log.wtf(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | } |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 453 | if (Debug.isDebuggerConnected()) { |
| 454 | debuggerWasConnected = 2; |
| 455 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 456 | timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 457 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 459 | boolean fdLimitTriggered = false; |
| 460 | if (mOpenFdMonitor != null) { |
| 461 | fdLimitTriggered = mOpenFdMonitor.monitor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | } |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 463 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 464 | if (!fdLimitTriggered) { |
| 465 | final int waitState = evaluateCheckerCompletionLocked(); |
| 466 | if (waitState == COMPLETED) { |
| 467 | // The monitors have returned; reset |
| 468 | waitedHalf = false; |
| 469 | continue; |
| 470 | } else if (waitState == WAITING) { |
| 471 | // still waiting but within their configured intervals; back off and recheck |
| 472 | continue; |
| 473 | } else if (waitState == WAITED_HALF) { |
| 474 | if (!waitedHalf) { |
| 475 | // We've waited half the deadlock-detection interval. Pull a stack |
| 476 | // trace and wait another half. |
| 477 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
| 478 | pids.add(Process.myPid()); |
| 479 | ActivityManagerService.dumpStackTraces(true, pids, null, null, |
| 480 | getInterestingNativePids()); |
| 481 | waitedHalf = true; |
| 482 | } |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | // something is overdue! |
| 487 | blockedCheckers = getBlockedCheckersLocked(); |
| 488 | subject = describeCheckersLocked(blockedCheckers); |
| 489 | } else { |
| 490 | blockedCheckers = Collections.emptyList(); |
| 491 | subject = "Open FD high water mark reached"; |
| 492 | } |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 493 | allowRestart = mAllowRestart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // If we got here, that means that the system is most likely hung. |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 497 | // First collect stack traces from all threads of the system process. |
| 498 | // Then kill this process so that the system will restart. |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 499 | EventLog.writeEvent(EventLogTags.WATCHDOG, subject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 501 | ArrayList<Integer> pids = new ArrayList<>(); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 502 | pids.add(Process.myPid()); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 503 | if (mPhonePid > 0) pids.add(mPhonePid); |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 504 | // Pass !waitedHalf so that just in case we somehow wind up here without having |
| 505 | // dumped the halfway stacks, we properly re-initialize the trace file. |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 506 | final File stack = ActivityManagerService.dumpStackTraces( |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 507 | !waitedHalf, pids, null, null, getInterestingNativePids()); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 508 | |
| 509 | // Give some extra time to make sure the stack traces get written. |
| 510 | // The system's been hanging for a minute, another second or two won't hurt much. |
| 511 | SystemClock.sleep(2000); |
| 512 | |
Guang Zhu | 0620c45 | 2014-10-29 14:31:48 -0700 | [diff] [blame] | 513 | // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log |
| 514 | doSysRq('w'); |
| 515 | doSysRq('l'); |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 516 | |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 517 | // Try to add the error to the dropbox, but assuming that the ActivityManager |
| 518 | // itself may be deadlocked. (which has happened, causing this statement to |
| 519 | // deadlock and the watchdog as a whole to be ineffective) |
| 520 | Thread dropboxThread = new Thread("watchdogWriteToDropbox") { |
| 521 | public void run() { |
| 522 | mActivity.addErrorToDropBox( |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 523 | "watchdog", null, "system_server", null, null, |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 524 | subject, null, stack, null); |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 525 | } |
| 526 | }; |
| 527 | dropboxThread.start(); |
| 528 | try { |
| 529 | dropboxThread.join(2000); // wait up to 2 seconds for it to return. |
| 530 | } catch (InterruptedException ignored) {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 532 | IActivityController controller; |
| 533 | synchronized (this) { |
| 534 | controller = mController; |
| 535 | } |
| 536 | if (controller != null) { |
| 537 | Slog.i(TAG, "Reporting stuck state to activity controller"); |
| 538 | try { |
| 539 | Binder.setDumpDisabled("Service dumps disabled due to hung system process."); |
| 540 | // 1 = keep waiting, -1 = kill system |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 541 | int res = controller.systemNotResponding(subject); |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 542 | if (res >= 0) { |
| 543 | Slog.i(TAG, "Activity controller requested to coninue to wait"); |
| 544 | waitedHalf = false; |
| 545 | continue; |
| 546 | } |
| 547 | } catch (RemoteException e) { |
| 548 | } |
| 549 | } |
| 550 | |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 551 | // Only kill the process if the debugger is not attached. |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 552 | if (Debug.isDebuggerConnected()) { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 553 | debuggerWasConnected = 2; |
| 554 | } |
| 555 | if (debuggerWasConnected >= 2) { |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 556 | Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process"); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 557 | } else if (debuggerWasConnected > 0) { |
| 558 | Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process"); |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 559 | } else if (!allowRestart) { |
| 560 | Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process"); |
| 561 | } else { |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 562 | Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject); |
Andreas Gampe | 544416e | 2018-01-26 11:39:46 -0800 | [diff] [blame] | 563 | WatchdogDiagnostics.diagnoseCheckers(blockedCheckers); |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 564 | Slog.w(TAG, "*** GOODBYE!"); |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 565 | Process.killProcess(Process.myPid()); |
| 566 | System.exit(10); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | } |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 568 | |
| 569 | waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | } |
| 571 | } |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 572 | |
Guang Zhu | 0620c45 | 2014-10-29 14:31:48 -0700 | [diff] [blame] | 573 | private void doSysRq(char c) { |
| 574 | try { |
| 575 | FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger"); |
| 576 | sysrq_trigger.write(c); |
| 577 | sysrq_trigger.close(); |
| 578 | } catch (IOException e) { |
| 579 | Slog.w(TAG, "Failed to write to /proc/sysrq-trigger", e); |
| 580 | } |
| 581 | } |
| 582 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 583 | public static final class OpenFdMonitor { |
| 584 | /** |
| 585 | * Number of FDs below the soft limit that we trigger a runtime restart at. This was |
| 586 | * chosen arbitrarily, but will need to be at least 6 in order to have a sufficient number |
| 587 | * of FDs in reserve to complete a dump. |
| 588 | */ |
| 589 | private static final int FD_HIGH_WATER_MARK = 12; |
| 590 | |
| 591 | private final File mDumpDir; |
| 592 | private final File mFdHighWaterMark; |
| 593 | |
| 594 | public static OpenFdMonitor create() { |
| 595 | // Only run the FD monitor on debuggable builds (such as userdebug and eng builds). |
| 596 | if (!Build.IS_DEBUGGABLE) { |
| 597 | return null; |
| 598 | } |
| 599 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 600 | final StructRlimit rlimit; |
| 601 | try { |
| 602 | rlimit = android.system.Os.getrlimit(OsConstants.RLIMIT_NOFILE); |
| 603 | } catch (ErrnoException errno) { |
| 604 | Slog.w(TAG, "Error thrown from getrlimit(RLIMIT_NOFILE)", errno); |
| 605 | return null; |
| 606 | } |
| 607 | |
| 608 | // The assumption we're making here is that FD numbers are allocated (more or less) |
| 609 | // sequentially, which is currently (and historically) true since open is currently |
| 610 | // specified to always return the lowest-numbered non-open file descriptor for the |
| 611 | // current process. |
| 612 | // |
| 613 | // We do this to avoid having to enumerate the contents of /proc/self/fd in order to |
| 614 | // count the number of descriptors open in the process. |
| 615 | final File fdThreshold = new File("/proc/self/fd/" + (rlimit.rlim_cur - FD_HIGH_WATER_MARK)); |
Elliott Hughes | 4e4caa7 | 2018-03-23 11:06:36 -0700 | [diff] [blame] | 616 | return new OpenFdMonitor(new File("/data/anr"), fdThreshold); |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | OpenFdMonitor(File dumpDir, File fdThreshold) { |
| 620 | mDumpDir = dumpDir; |
| 621 | mFdHighWaterMark = fdThreshold; |
| 622 | } |
| 623 | |
| 624 | private void dumpOpenDescriptors() { |
| 625 | try { |
| 626 | File dumpFile = File.createTempFile("anr_fd_", "", mDumpDir); |
| 627 | java.lang.Process proc = new ProcessBuilder() |
| 628 | .command("/system/bin/lsof", "-p", String.valueOf(Process.myPid())) |
| 629 | .redirectErrorStream(true) |
| 630 | .redirectOutput(dumpFile) |
| 631 | .start(); |
| 632 | |
| 633 | int returnCode = proc.waitFor(); |
| 634 | if (returnCode != 0) { |
| 635 | Slog.w(TAG, "Unable to dump open descriptors, lsof return code: " |
| 636 | + returnCode); |
| 637 | dumpFile.delete(); |
| 638 | } |
| 639 | } catch (IOException | InterruptedException ex) { |
| 640 | Slog.w(TAG, "Unable to dump open descriptors: " + ex); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * @return {@code true} if the high water mark was breached and a dump was written, |
| 646 | * {@code false} otherwise. |
| 647 | */ |
| 648 | public boolean monitor() { |
| 649 | if (mFdHighWaterMark.exists()) { |
| 650 | dumpOpenDescriptors(); |
| 651 | return true; |
| 652 | } |
| 653 | |
| 654 | return false; |
| 655 | } |
| 656 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | } |