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