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