| 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 | |
| 19 | import com.android.server.am.ActivityManagerService; |
| Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 20 | import com.android.server.power.PowerManagerService; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
| 22 | import android.app.AlarmManager; |
| 23 | import android.app.PendingIntent; |
| 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.ContentResolver; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
| rikard dahlman | 9211b13 | 2012-08-28 16:12:38 +0200 | [diff] [blame] | 29 | import android.os.Build; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.os.Debug; |
| 31 | import android.os.Handler; |
| 32 | import android.os.Message; |
| 33 | import android.os.Process; |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 34 | import android.os.ServiceManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.SystemClock; |
| 36 | import android.os.SystemProperties; |
| 37 | import android.provider.Settings; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.util.EventLog; |
| Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 39 | import android.util.Log; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 40 | import android.util.Slog; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 42 | import java.io.File; |
| rikard dahlman | 9211b13 | 2012-08-28 16:12:38 +0200 | [diff] [blame] | 43 | import java.io.FileWriter; |
| 44 | import java.io.IOException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.Calendar; |
| 47 | |
| 48 | /** This class calls its monitor every minute. Killing this process if they don't return **/ |
| 49 | public class Watchdog extends Thread { |
| 50 | static final String TAG = "Watchdog"; |
| Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 51 | static final boolean localLOGV = false || false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| 53 | // Set this to true to use debug default values. |
| 54 | static final boolean DB = false; |
| 55 | |
| Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 56 | // Set this to true to have the watchdog record kernel thread stacks when it fires |
| 57 | static final boolean RECORD_KERNEL_THREADS = true; |
| 58 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | static final int MONITOR = 2718; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
| Mathias Agopian | cf2317e | 2011-08-25 17:12:37 -0700 | [diff] [blame] | 61 | static final int TIME_TO_RESTART = DB ? 15*1000 : 60*1000; |
| Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 62 | static final int TIME_TO_WAIT = TIME_TO_RESTART / 2; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | static final int MEMCHECK_DEFAULT_MIN_SCREEN_OFF = DB ? 1*60 : 5*60; // 5 minutes |
| 65 | static final int MEMCHECK_DEFAULT_MIN_ALARM = DB ? 1*60 : 3*60; // 3 minutes |
| 66 | static final int MEMCHECK_DEFAULT_RECHECK_INTERVAL = DB ? 1*60 : 5*60; // 5 minutes |
| 67 | |
| 68 | static final int REBOOT_DEFAULT_INTERVAL = DB ? 1 : 0; // never force reboot |
| 69 | static final int REBOOT_DEFAULT_START_TIME = 3*60*60; // 3:00am |
| 70 | static final int REBOOT_DEFAULT_WINDOW = 60*60; // within 1 hour |
| 71 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | static final String REBOOT_ACTION = "com.android.service.Watchdog.REBOOT"; |
| 73 | |
| Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 74 | static final String[] NATIVE_STACKS_OF_INTEREST = new String[] { |
| 75 | "/system/bin/mediaserver", |
| 76 | "/system/bin/sdcard", |
| 77 | "/system/bin/surfaceflinger" |
| 78 | }; |
| 79 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | static Watchdog sWatchdog; |
| 81 | |
| 82 | /* This handler will be used to post message back onto the main thread */ |
| 83 | final Handler mHandler; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>(); |
| 85 | ContentResolver mResolver; |
| 86 | BatteryService mBattery; |
| 87 | PowerManagerService mPower; |
| 88 | AlarmManagerService mAlarm; |
| 89 | ActivityManagerService mActivity; |
| 90 | boolean mCompleted; |
| 91 | boolean mForceKillSystem; |
| 92 | Monitor mCurrentMonitor; |
| 93 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | int mPhonePid; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | |
| 96 | final Calendar mCalendar = Calendar.getInstance(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | int mMinScreenOff = MEMCHECK_DEFAULT_MIN_SCREEN_OFF; |
| 98 | int mMinAlarm = MEMCHECK_DEFAULT_MIN_ALARM; |
| 99 | boolean mNeedScheduledCheck; |
| 100 | PendingIntent mCheckupIntent; |
| 101 | PendingIntent mRebootIntent; |
| 102 | |
| 103 | long mBootTime; |
| 104 | int mRebootInterval; |
| 105 | |
| 106 | boolean mReqRebootNoWait; // should wait for one interval before reboot? |
| 107 | int mReqRebootInterval = -1; // >= 0 if a reboot has been requested |
| 108 | int mReqRebootStartTime = -1; // >= 0 if a specific start time has been requested |
| 109 | int mReqRebootWindow = -1; // >= 0 if a specific window has been requested |
| 110 | int mReqMinScreenOff = -1; // >= 0 if a specific screen off time has been requested |
| 111 | int mReqMinNextAlarm = -1; // >= 0 if specific time to next alarm has been requested |
| 112 | int mReqRecheckInterval= -1; // >= 0 if a specific recheck interval has been requested |
| 113 | |
| 114 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | * Used for scheduling monitor callbacks and checking memory usage. |
| 116 | */ |
| 117 | final class HeartbeatHandler extends Handler { |
| 118 | @Override |
| 119 | public void handleMessage(Message msg) { |
| 120 | switch (msg.what) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | case MONITOR: { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | // See if we should force a reboot. |
| 123 | int rebootInterval = mReqRebootInterval >= 0 |
| Doug Zongker | f688889 | 2010-01-06 16:38:14 -0800 | [diff] [blame] | 124 | ? mReqRebootInterval : Settings.Secure.getInt( |
| 125 | mResolver, Settings.Secure.REBOOT_INTERVAL, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | REBOOT_DEFAULT_INTERVAL); |
| 127 | if (mRebootInterval != rebootInterval) { |
| 128 | mRebootInterval = rebootInterval; |
| 129 | // We have been running long enough that a reboot can |
| 130 | // be considered... |
| 131 | checkReboot(false); |
| 132 | } |
| 133 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | final int size = mMonitors.size(); |
| 135 | for (int i = 0 ; i < size ; i++) { |
| 136 | mCurrentMonitor = mMonitors.get(i); |
| 137 | mCurrentMonitor.monitor(); |
| 138 | } |
| 139 | |
| 140 | synchronized (Watchdog.this) { |
| 141 | mCompleted = true; |
| 142 | mCurrentMonitor = null; |
| 143 | } |
| 144 | } break; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | final class RebootReceiver extends BroadcastReceiver { |
| 150 | @Override |
| 151 | public void onReceive(Context c, Intent intent) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 152 | if (localLOGV) Slog.v(TAG, "Alarm went off, checking reboot."); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | checkReboot(true); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | final class RebootRequestReceiver extends BroadcastReceiver { |
| 158 | @Override |
| 159 | public void onReceive(Context c, Intent intent) { |
| 160 | mReqRebootNoWait = intent.getIntExtra("nowait", 0) != 0; |
| 161 | mReqRebootInterval = intent.getIntExtra("interval", -1); |
| 162 | mReqRebootStartTime = intent.getIntExtra("startTime", -1); |
| 163 | mReqRebootWindow = intent.getIntExtra("window", -1); |
| 164 | mReqMinScreenOff = intent.getIntExtra("minScreenOff", -1); |
| 165 | mReqMinNextAlarm = intent.getIntExtra("minNextAlarm", -1); |
| 166 | mReqRecheckInterval = intent.getIntExtra("recheckInterval", -1); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 167 | EventLog.writeEvent(EventLogTags.WATCHDOG_REQUESTED_REBOOT, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | mReqRebootNoWait ? 1 : 0, mReqRebootInterval, |
| 169 | mReqRecheckInterval, mReqRebootStartTime, |
| 170 | mReqRebootWindow, mReqMinScreenOff, mReqMinNextAlarm); |
| 171 | checkReboot(true); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | public interface Monitor { |
| 176 | void monitor(); |
| 177 | } |
| 178 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | public static Watchdog getInstance() { |
| 180 | if (sWatchdog == null) { |
| 181 | sWatchdog = new Watchdog(); |
| 182 | } |
| 183 | |
| 184 | return sWatchdog; |
| 185 | } |
| 186 | |
| 187 | private Watchdog() { |
| 188 | super("watchdog"); |
| 189 | mHandler = new HeartbeatHandler(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | public void init(Context context, BatteryService battery, |
| 193 | PowerManagerService power, AlarmManagerService alarm, |
| 194 | ActivityManagerService activity) { |
| 195 | mResolver = context.getContentResolver(); |
| 196 | mBattery = battery; |
| 197 | mPower = power; |
| 198 | mAlarm = alarm; |
| 199 | mActivity = activity; |
| 200 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | context.registerReceiver(new RebootReceiver(), |
| 202 | new IntentFilter(REBOOT_ACTION)); |
| 203 | mRebootIntent = PendingIntent.getBroadcast(context, |
| 204 | 0, new Intent(REBOOT_ACTION), 0); |
| 205 | |
| 206 | context.registerReceiver(new RebootRequestReceiver(), |
| 207 | new IntentFilter(Intent.ACTION_REBOOT), |
| 208 | android.Manifest.permission.REBOOT, null); |
| 209 | |
| 210 | mBootTime = System.currentTimeMillis(); |
| 211 | } |
| 212 | |
| Christopher Tate | c27181c | 2010-06-30 14:41:09 -0700 | [diff] [blame] | 213 | public void processStarted(String name, int pid) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | synchronized (this) { |
| 215 | if ("com.android.phone".equals(name)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | mPhonePid = pid; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | public void addMonitor(Monitor monitor) { |
| 222 | synchronized (this) { |
| 223 | if (isAlive()) { |
| 224 | throw new RuntimeException("Monitors can't be added while the Watchdog is running"); |
| 225 | } |
| 226 | mMonitors.add(monitor); |
| 227 | } |
| 228 | } |
| 229 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | void checkReboot(boolean fromAlarm) { |
| 231 | int rebootInterval = mReqRebootInterval >= 0 ? mReqRebootInterval |
| Doug Zongker | f688889 | 2010-01-06 16:38:14 -0800 | [diff] [blame] | 232 | : Settings.Secure.getInt( |
| 233 | mResolver, Settings.Secure.REBOOT_INTERVAL, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | REBOOT_DEFAULT_INTERVAL); |
| 235 | mRebootInterval = rebootInterval; |
| 236 | if (rebootInterval <= 0) { |
| 237 | // No reboot interval requested. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 238 | if (localLOGV) Slog.v(TAG, "No need to schedule a reboot alarm!"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | mAlarm.remove(mRebootIntent); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | long rebootStartTime = mReqRebootStartTime >= 0 ? mReqRebootStartTime |
| Doug Zongker | f688889 | 2010-01-06 16:38:14 -0800 | [diff] [blame] | 244 | : Settings.Secure.getLong( |
| 245 | mResolver, Settings.Secure.REBOOT_START_TIME, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | REBOOT_DEFAULT_START_TIME); |
| 247 | long rebootWindowMillis = (mReqRebootWindow >= 0 ? mReqRebootWindow |
| Doug Zongker | f688889 | 2010-01-06 16:38:14 -0800 | [diff] [blame] | 248 | : Settings.Secure.getLong( |
| 249 | mResolver, Settings.Secure.REBOOT_WINDOW, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | REBOOT_DEFAULT_WINDOW)) * 1000; |
| 251 | long recheckInterval = (mReqRecheckInterval >= 0 ? mReqRecheckInterval |
| Doug Zongker | f688889 | 2010-01-06 16:38:14 -0800 | [diff] [blame] | 252 | : Settings.Secure.getLong( |
| 253 | mResolver, Settings.Secure.MEMCHECK_RECHECK_INTERVAL, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | MEMCHECK_DEFAULT_RECHECK_INTERVAL)) * 1000; |
| 255 | |
| 256 | retrieveBrutalityAmount(); |
| 257 | |
| 258 | long realStartTime; |
| 259 | long now; |
| 260 | |
| 261 | synchronized (this) { |
| 262 | now = System.currentTimeMillis(); |
| 263 | realStartTime = computeCalendarTime(mCalendar, now, |
| 264 | rebootStartTime); |
| 265 | |
| 266 | long rebootIntervalMillis = rebootInterval*24*60*60*1000; |
| 267 | if (DB || mReqRebootNoWait || |
| 268 | (now-mBootTime) >= (rebootIntervalMillis-rebootWindowMillis)) { |
| 269 | if (fromAlarm && rebootWindowMillis <= 0) { |
| 270 | // No reboot window -- just immediately reboot. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 271 | EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | (int)rebootIntervalMillis, (int)rebootStartTime*1000, |
| 273 | (int)rebootWindowMillis, ""); |
| 274 | rebootSystem("Checkin scheduled forced"); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | // Are we within the reboot window? |
| 279 | if (now < realStartTime) { |
| 280 | // Schedule alarm for next check interval. |
| 281 | realStartTime = computeCalendarTime(mCalendar, |
| 282 | now, rebootStartTime); |
| 283 | } else if (now < (realStartTime+rebootWindowMillis)) { |
| 284 | String doit = shouldWeBeBrutalLocked(now); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 285 | EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | (int)rebootInterval, (int)rebootStartTime*1000, |
| 287 | (int)rebootWindowMillis, doit != null ? doit : ""); |
| 288 | if (doit == null) { |
| 289 | rebootSystem("Checked scheduled range"); |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | // Schedule next alarm either within the window or in the |
| 294 | // next interval. |
| 295 | if ((now+recheckInterval) >= (realStartTime+rebootWindowMillis)) { |
| 296 | realStartTime = computeCalendarTime(mCalendar, |
| 297 | now + rebootIntervalMillis, rebootStartTime); |
| 298 | } else { |
| 299 | realStartTime = now + recheckInterval; |
| 300 | } |
| 301 | } else { |
| 302 | // Schedule alarm for next check interval. |
| 303 | realStartTime = computeCalendarTime(mCalendar, |
| 304 | now + rebootIntervalMillis, rebootStartTime); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 309 | if (localLOGV) Slog.v(TAG, "Scheduling next reboot alarm for " |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | + ((realStartTime-now)/1000/60) + "m from now"); |
| 311 | mAlarm.remove(mRebootIntent); |
| 312 | mAlarm.set(AlarmManager.RTC_WAKEUP, realStartTime, mRebootIntent); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Perform a full reboot of the system. |
| 317 | */ |
| 318 | void rebootSystem(String reason) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 319 | Slog.i(TAG, "Rebooting system because: " + reason); |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 320 | PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power"); |
| 321 | pms.reboot(reason); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Load the current Gservices settings for when |
| 326 | * {@link #shouldWeBeBrutalLocked} will allow the brutality to happen. |
| 327 | * Must not be called with the lock held. |
| 328 | */ |
| 329 | void retrieveBrutalityAmount() { |
| 330 | mMinScreenOff = (mReqMinScreenOff >= 0 ? mReqMinScreenOff |
| Doug Zongker | f688889 | 2010-01-06 16:38:14 -0800 | [diff] [blame] | 331 | : Settings.Secure.getInt( |
| 332 | mResolver, Settings.Secure.MEMCHECK_MIN_SCREEN_OFF, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | MEMCHECK_DEFAULT_MIN_SCREEN_OFF)) * 1000; |
| 334 | mMinAlarm = (mReqMinNextAlarm >= 0 ? mReqMinNextAlarm |
| Doug Zongker | f688889 | 2010-01-06 16:38:14 -0800 | [diff] [blame] | 335 | : Settings.Secure.getInt( |
| 336 | mResolver, Settings.Secure.MEMCHECK_MIN_ALARM, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | MEMCHECK_DEFAULT_MIN_ALARM)) * 1000; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Determine whether it is a good time to kill, crash, or otherwise |
| 342 | * plunder the current situation for the overall long-term benefit of |
| 343 | * the world. |
| 344 | * |
| 345 | * @param curTime The current system time. |
| 346 | * @return Returns null if this is a good time, else a String with the |
| 347 | * text of why it is not a good time. |
| 348 | */ |
| 349 | String shouldWeBeBrutalLocked(long curTime) { |
| 350 | if (mBattery == null || !mBattery.isPowered()) { |
| 351 | return "battery"; |
| 352 | } |
| 353 | |
| 354 | if (mMinScreenOff >= 0 && (mPower == null || |
| Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 355 | mPower.timeSinceScreenWasLastOn() < mMinScreenOff)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | return "screen"; |
| 357 | } |
| 358 | |
| 359 | if (mMinAlarm >= 0 && (mAlarm == null || |
| 360 | mAlarm.timeToNextAlarm() < mMinAlarm)) { |
| 361 | return "alarm"; |
| 362 | } |
| 363 | |
| 364 | return null; |
| 365 | } |
| 366 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | static long computeCalendarTime(Calendar c, long curTime, |
| 368 | long secondsSinceMidnight) { |
| 369 | |
| 370 | // start with now |
| 371 | c.setTimeInMillis(curTime); |
| 372 | |
| 373 | int val = (int)secondsSinceMidnight / (60*60); |
| 374 | c.set(Calendar.HOUR_OF_DAY, val); |
| 375 | secondsSinceMidnight -= val * (60*60); |
| 376 | val = (int)secondsSinceMidnight / 60; |
| 377 | c.set(Calendar.MINUTE, val); |
| 378 | c.set(Calendar.SECOND, (int)secondsSinceMidnight - (val*60)); |
| 379 | c.set(Calendar.MILLISECOND, 0); |
| 380 | |
| 381 | long newTime = c.getTimeInMillis(); |
| 382 | if (newTime < curTime) { |
| 383 | // The given time (in seconds since midnight) has already passed for today, so advance |
| 384 | // by one day (due to daylight savings, etc., the delta may differ from 24 hours). |
| 385 | c.add(Calendar.DAY_OF_MONTH, 1); |
| 386 | newTime = c.getTimeInMillis(); |
| 387 | } |
| 388 | |
| 389 | return newTime; |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public void run() { |
| Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 394 | boolean waitedHalf = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | while (true) { |
| 396 | mCompleted = false; |
| 397 | mHandler.sendEmptyMessage(MONITOR); |
| 398 | |
| 399 | synchronized (this) { |
| 400 | long timeout = TIME_TO_WAIT; |
| 401 | |
| 402 | // NOTE: We use uptimeMillis() here because we do not want to increment the time we |
| 403 | // 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] | 404 | // 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] | 405 | // positive on when to kill things. |
| 406 | long start = SystemClock.uptimeMillis(); |
| Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 407 | while (timeout > 0 && !mForceKillSystem) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | try { |
| Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 409 | wait(timeout); // notifyAll() is called when mForceKillSystem is set |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | } catch (InterruptedException e) { |
| Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 411 | Log.wtf(TAG, e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | } |
| 413 | timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start); |
| Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 414 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | |
| 416 | if (mCompleted && !mForceKillSystem) { |
| 417 | // The monitors have returned. |
| Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 418 | waitedHalf = false; |
| 419 | continue; |
| 420 | } |
| 421 | |
| 422 | if (!waitedHalf) { |
| 423 | // We've waited half the deadlock-detection interval. Pull a stack |
| 424 | // trace and wait another half. |
| Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 425 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
| Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 426 | pids.add(Process.myPid()); |
| Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 427 | ActivityManagerService.dumpStackTraces(true, pids, null, null, |
| 428 | NATIVE_STACKS_OF_INTEREST); |
| Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 429 | waitedHalf = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | continue; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | // If we got here, that means that the system is most likely hung. |
| Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 435 | |
| Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 436 | final String name = (mCurrentMonitor != null) ? |
| Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 437 | mCurrentMonitor.getClass().getName() : "null"; |
| rikard dahlman | 9211b13 | 2012-08-28 16:12:38 +0200 | [diff] [blame] | 438 | Slog.w(TAG, "WATCHDOG PROBLEM IN SYSTEM SERVER: " + name); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 439 | EventLog.writeEvent(EventLogTags.WATCHDOG, name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | |
| Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 441 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
| Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 442 | pids.add(Process.myPid()); |
| Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 443 | if (mPhonePid > 0) pids.add(mPhonePid); |
| Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 444 | // Pass !waitedHalf so that just in case we somehow wind up here without having |
| 445 | // dumped the halfway stacks, we properly re-initialize the trace file. |
| Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 446 | final File stack = ActivityManagerService.dumpStackTraces( |
| Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 447 | !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST); |
| Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 448 | |
| 449 | // Give some extra time to make sure the stack traces get written. |
| 450 | // The system's been hanging for a minute, another second or two won't hurt much. |
| 451 | SystemClock.sleep(2000); |
| 452 | |
| Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 453 | // Pull our own kernel thread stacks as well if we're configured for that |
| 454 | if (RECORD_KERNEL_THREADS) { |
| 455 | dumpKernelStackTraces(); |
| 456 | } |
| 457 | |
| Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 458 | // Try to add the error to the dropbox, but assuming that the ActivityManager |
| 459 | // itself may be deadlocked. (which has happened, causing this statement to |
| 460 | // deadlock and the watchdog as a whole to be ineffective) |
| 461 | Thread dropboxThread = new Thread("watchdogWriteToDropbox") { |
| 462 | public void run() { |
| 463 | mActivity.addErrorToDropBox( |
| Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 464 | "watchdog", null, "system_server", null, null, |
| 465 | name, null, stack, null); |
| Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 466 | } |
| 467 | }; |
| 468 | dropboxThread.start(); |
| 469 | try { |
| 470 | dropboxThread.join(2000); // wait up to 2 seconds for it to return. |
| 471 | } catch (InterruptedException ignored) {} |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | |
| rikard dahlman | 9211b13 | 2012-08-28 16:12:38 +0200 | [diff] [blame] | 473 | // Only kill/crash the process if the debugger is not attached. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | if (!Debug.isDebuggerConnected()) { |
| Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 475 | Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + name); |
| rikard dahlman | 9211b13 | 2012-08-28 16:12:38 +0200 | [diff] [blame] | 476 | if (!Build.TYPE.equals("user")) { |
| 477 | forceCrashDump(); |
| 478 | } else { |
| 479 | Process.killProcess(Process.myPid()); |
| 480 | System.exit(10); |
| 481 | } |
| Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 482 | } else { |
| 483 | Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | } |
| Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 485 | |
| 486 | waitedHalf = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | } |
| 488 | } |
| Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 489 | |
| rikard dahlman | 9211b13 | 2012-08-28 16:12:38 +0200 | [diff] [blame] | 490 | private void forceCrashDump() { |
| 491 | /* Sync file system to flash the data which is written just before the |
| 492 | * crash. |
| 493 | */ |
| 494 | java.lang.Process p = null; |
| 495 | try { |
| 496 | p = Runtime.getRuntime().exec("sync"); |
| 497 | if (p != null) { |
| 498 | // It is not necessary to check the exit code, here. |
| 499 | // 'sync' command always succeeds, and this function returns 0. |
| 500 | p.waitFor(); |
| 501 | } else { |
| 502 | Slog.e(TAG, "Failed to execute 'sync' command. (no process handle)"); |
| 503 | } |
| 504 | } catch (Exception e) { |
| 505 | // This code is an emergency path to crash MUT. The system already |
| 506 | // caused fatal error, and then calls this function to create a |
| 507 | // crash dump. This function must run the code below to force a |
| 508 | // crash, even if the sync command failed. |
| 509 | // Therefore, ignore all exceptions, here. |
| 510 | Slog.e(TAG, "Failed to execute 'sync' command prior to forcing crash: " + e); |
| 511 | } finally { |
| 512 | if (p != null) { |
| 513 | p.destroy(); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | FileWriter out = null; |
| 518 | try { |
| 519 | out = new FileWriter("/proc/sysrq-trigger"); |
| 520 | out.write("c"); |
| 521 | } catch (IOException e) { |
| 522 | Slog.e(TAG, "Failed to write to sysrq-trigger while triggering crash: " + e); |
| 523 | } finally { |
| 524 | if (out != null) { |
| 525 | try { |
| 526 | out.close(); |
| 527 | } catch (IOException e) { |
| 528 | Slog.e(TAG, "Failed to close sysrq-trigger while triggering crash: " + e); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 534 | private File dumpKernelStackTraces() { |
| 535 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 536 | if (tracesPath == null || tracesPath.length() == 0) { |
| 537 | return null; |
| 538 | } |
| 539 | |
| 540 | native_dumpKernelStacks(tracesPath); |
| 541 | return new File(tracesPath); |
| 542 | } |
| 543 | |
| 544 | private native void native_dumpKernelStacks(String tracesPath); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | } |