The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 2 | * Copyright (C) 2009 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 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | |
| 17 | package com.android.server; |
| 18 | |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 22 | import android.content.SharedPreferences; |
Dianne Hackborn | 13579ed | 2012-11-28 18:05:36 -0800 | [diff] [blame] | 23 | import android.content.pm.IPackageManager; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 24 | import android.os.Build; |
| 25 | import android.os.DropBoxManager; |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 26 | import android.os.FileObserver; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 27 | import android.os.FileUtils; |
Doug Zongker | 1af33d0 | 2010-01-05 11:28:55 -0800 | [diff] [blame] | 28 | import android.os.RecoverySystem; |
Dianne Hackborn | 13579ed | 2012-11-28 18:05:36 -0800 | [diff] [blame] | 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 31 | import android.os.SystemProperties; |
Jeff Sharkey | 948eef8 | 2012-03-20 17:44:42 -0700 | [diff] [blame] | 32 | import android.provider.Downloads; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 33 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 35 | import java.io.File; |
| 36 | import java.io.IOException; |
| 37 | |
| 38 | /** |
| 39 | * Performs a number of miscellaneous, non-system-critical actions |
| 40 | * after the system has finished booting. |
| 41 | */ |
| 42 | public class BootReceiver extends BroadcastReceiver { |
| 43 | private static final String TAG = "BootReceiver"; |
| 44 | |
Dave Burke | 0132a952 | 2012-02-27 14:33:30 -0800 | [diff] [blame] | 45 | // Maximum size of a logged event (files get truncated if they're longer). |
| 46 | // Give userdebug builds a larger max to capture extra debug, esp. for last_kmsg. |
| 47 | private static final int LOG_SIZE = |
| 48 | SystemProperties.getInt("ro.debuggable", 0) == 1 ? 98304 : 65536; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 49 | |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 50 | private static final File TOMBSTONE_DIR = new File("/data/tombstones"); |
| 51 | |
Doug Zongker | 944ff0b | 2010-04-05 15:07:36 -0700 | [diff] [blame] | 52 | // The pre-froyo package and class of the system updater, which |
| 53 | // ran in the system process. We need to remove its packages here |
| 54 | // in order to clean up after a pre-froyo-to-froyo update. |
| 55 | private static final String OLD_UPDATER_PACKAGE = |
| 56 | "com.google.android.systemupdater"; |
| 57 | private static final String OLD_UPDATER_CLASS = |
| 58 | "com.google.android.systemupdater.SystemUpdateReceiver"; |
| 59 | |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 60 | // Keep a reference to the observer so the finalizer doesn't disable it. |
| 61 | private static FileObserver sTombstoneObserver = null; |
| 62 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | @Override |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 64 | public void onReceive(final Context context, Intent intent) { |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 65 | // Log boot events in the background to avoid blocking the main thread with I/O |
| 66 | new Thread() { |
| 67 | @Override |
| 68 | public void run() { |
| 69 | try { |
| 70 | logBootEvents(context); |
| 71 | } catch (Exception e) { |
| 72 | Slog.e(TAG, "Can't log boot events", e); |
| 73 | } |
Doug Zongker | 944ff0b | 2010-04-05 15:07:36 -0700 | [diff] [blame] | 74 | try { |
Dianne Hackborn | 13579ed | 2012-11-28 18:05:36 -0800 | [diff] [blame] | 75 | boolean onlyCore = false; |
| 76 | try { |
| 77 | onlyCore = IPackageManager.Stub.asInterface(ServiceManager.getService( |
| 78 | "package")).isOnlyCoreApps(); |
| 79 | } catch (RemoteException e) { |
| 80 | } |
| 81 | if (!onlyCore) { |
| 82 | removeOldUpdatePackages(context); |
| 83 | } |
Doug Zongker | 944ff0b | 2010-04-05 15:07:36 -0700 | [diff] [blame] | 84 | } catch (Exception e) { |
| 85 | Slog.e(TAG, "Can't remove old update packages", e); |
| 86 | } |
| 87 | |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 88 | } |
| 89 | }.start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
Jeff Sharkey | 948eef8 | 2012-03-20 17:44:42 -0700 | [diff] [blame] | 92 | private void removeOldUpdatePackages(Context context) { |
| 93 | Downloads.removeAllDownloadsByPackage(context, OLD_UPDATER_PACKAGE, OLD_UPDATER_CLASS); |
Doug Zongker | 944ff0b | 2010-04-05 15:07:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 96 | private void logBootEvents(Context ctx) throws IOException { |
| 97 | final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE); |
| 98 | final SharedPreferences prefs = ctx.getSharedPreferences("log_files", Context.MODE_PRIVATE); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 99 | final String headers = new StringBuilder(512) |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 100 | .append("Build: ").append(Build.FINGERPRINT).append("\n") |
| 101 | .append("Hardware: ").append(Build.BOARD).append("\n") |
Colin Cross | 8b2c916 | 2012-10-22 14:01:36 -0700 | [diff] [blame] | 102 | .append("Revision: ") |
| 103 | .append(SystemProperties.get("ro.revision", "")).append("\n") |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 104 | .append("Bootloader: ").append(Build.BOOTLOADER).append("\n") |
| 105 | .append("Radio: ").append(Build.RADIO).append("\n") |
| 106 | .append("Kernel: ") |
| 107 | .append(FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n")) |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 108 | .append("\n").toString(); |
Colin Cross | 431614c | 2014-04-18 13:45:27 -0700 | [diff] [blame] | 109 | final String bootReason = SystemProperties.get("ro.boot.bootreason", null); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 110 | |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 111 | String recovery = RecoverySystem.handleAftermath(); |
| 112 | if (recovery != null && db != null) { |
| 113 | db.addText("SYSTEM_RECOVERY_LOG", headers + recovery); |
| 114 | } |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 115 | |
Colin Cross | 431614c | 2014-04-18 13:45:27 -0700 | [diff] [blame] | 116 | String lastKmsgFooter = ""; |
| 117 | if (bootReason != null) { |
| 118 | lastKmsgFooter = new StringBuilder(512) |
| 119 | .append("\n") |
| 120 | .append("Boot info:\n") |
| 121 | .append("Last boot reason: ").append(bootReason).append("\n") |
| 122 | .toString(); |
| 123 | } |
| 124 | |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 125 | if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) { |
| 126 | String now = Long.toString(System.currentTimeMillis()); |
| 127 | SystemProperties.set("ro.runtime.firstboot", now); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 128 | if (db != null) db.addText("SYSTEM_BOOT", headers); |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 129 | |
| 130 | // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile()) |
Colin Cross | 431614c | 2014-04-18 13:45:27 -0700 | [diff] [blame] | 131 | addFileWithFootersToDropBox(db, prefs, headers, lastKmsgFooter, |
| 132 | "/proc/last_kmsg", -LOG_SIZE, "SYSTEM_LAST_KMSG"); |
| 133 | addFileWithFootersToDropBox(db, prefs, headers, lastKmsgFooter, |
| 134 | "/sys/fs/pstore/console-ramoops", -LOG_SIZE, |
| 135 | "SYSTEM_LAST_KMSG"); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 136 | addFileToDropBox(db, prefs, headers, "/cache/recovery/log", |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 137 | -LOG_SIZE, "SYSTEM_RECOVERY_LOG"); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 138 | addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_console", |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 139 | -LOG_SIZE, "APANIC_CONSOLE"); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 140 | addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_threads", |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 141 | -LOG_SIZE, "APANIC_THREADS"); |
Geremy Condra | 51611eb | 2013-02-13 16:07:21 -0800 | [diff] [blame] | 142 | addAuditErrorsToDropBox(db, prefs, headers, -LOG_SIZE, "SYSTEM_AUDIT"); |
Ken Sumrall | fefefbb | 2013-09-12 19:34:36 -0700 | [diff] [blame] | 143 | addFsckErrorsToDropBox(db, prefs, headers, -LOG_SIZE, "SYSTEM_FSCK"); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 144 | } else { |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 145 | if (db != null) db.addText("SYSTEM_RESTART", headers); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 148 | // Scan existing tombstones (in case any new ones appeared) |
| 149 | File[] tombstoneFiles = TOMBSTONE_DIR.listFiles(); |
| 150 | for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) { |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 151 | addFileToDropBox(db, prefs, headers, tombstoneFiles[i].getPath(), |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 152 | LOG_SIZE, "SYSTEM_TOMBSTONE"); |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Start watching for new tombstone files; will record them as they occur. |
| 156 | // This gets registered with the singleton file observer thread. |
| 157 | sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) { |
| 158 | @Override |
| 159 | public void onEvent(int event, String path) { |
| 160 | try { |
| 161 | String filename = new File(TOMBSTONE_DIR, path).getPath(); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 162 | addFileToDropBox(db, prefs, headers, filename, LOG_SIZE, "SYSTEM_TOMBSTONE"); |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 163 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 164 | Slog.e(TAG, "Can't log tombstone", e); |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | sTombstoneObserver.startWatching(); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 172 | private static void addFileToDropBox( |
| 173 | DropBoxManager db, SharedPreferences prefs, |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 174 | String headers, String filename, int maxSize, String tag) throws IOException { |
Colin Cross | 431614c | 2014-04-18 13:45:27 -0700 | [diff] [blame] | 175 | addFileWithFootersToDropBox(db, prefs, headers, "", filename, maxSize, |
| 176 | tag); |
| 177 | } |
| 178 | |
| 179 | private static void addFileWithFootersToDropBox( |
| 180 | DropBoxManager db, SharedPreferences prefs, |
| 181 | String headers, String footers, String filename, int maxSize, |
| 182 | String tag) throws IOException { |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 183 | if (db == null || !db.isTagEnabled(tag)) return; // Logging disabled |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 184 | |
| 185 | File file = new File(filename); |
| 186 | long fileTime = file.lastModified(); |
| 187 | if (fileTime <= 0) return; // File does not exist |
| 188 | |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 189 | if (prefs != null) { |
| 190 | long lastTime = prefs.getLong(filename, 0); |
| 191 | if (lastTime == fileTime) return; // Already logged this particular file |
Brad Fitzpatrick | 333b8cb | 2010-08-26 12:04:57 -0700 | [diff] [blame] | 192 | // TODO: move all these SharedPreferences Editor commits |
| 193 | // outside this function to the end of logBootEvents |
Brad Fitzpatrick | 66fce50 | 2010-08-30 18:10:49 -0700 | [diff] [blame] | 194 | prefs.edit().putLong(filename, fileTime).apply(); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 195 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 196 | |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame] | 197 | Slog.i(TAG, "Copying " + filename + " to DropBox (" + tag + ")"); |
Colin Cross | 431614c | 2014-04-18 13:45:27 -0700 | [diff] [blame] | 198 | db.addText(tag, headers + FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n") + footers); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 199 | } |
Geremy Condra | 51611eb | 2013-02-13 16:07:21 -0800 | [diff] [blame] | 200 | |
| 201 | private static void addAuditErrorsToDropBox(DropBoxManager db, SharedPreferences prefs, |
| 202 | String headers, int maxSize, String tag) throws IOException { |
| 203 | if (db == null || !db.isTagEnabled(tag)) return; // Logging disabled |
| 204 | Slog.i(TAG, "Copying audit failures to DropBox"); |
| 205 | |
| 206 | File file = new File("/proc/last_kmsg"); |
| 207 | long fileTime = file.lastModified(); |
Todd Poynor | c95bb56 | 2013-11-22 17:15:49 -0800 | [diff] [blame] | 208 | if (fileTime <= 0) { |
| 209 | file = new File("/sys/fs/pstore/console-ramoops"); |
| 210 | fileTime = file.lastModified(); |
| 211 | } |
| 212 | |
Geremy Condra | 51611eb | 2013-02-13 16:07:21 -0800 | [diff] [blame] | 213 | if (fileTime <= 0) return; // File does not exist |
| 214 | |
| 215 | if (prefs != null) { |
| 216 | long lastTime = prefs.getLong(tag, 0); |
| 217 | if (lastTime == fileTime) return; // Already logged this particular file |
| 218 | // TODO: move all these SharedPreferences Editor commits |
| 219 | // outside this function to the end of logBootEvents |
| 220 | prefs.edit().putLong(tag, fileTime).apply(); |
| 221 | } |
| 222 | |
| 223 | String log = FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n"); |
| 224 | StringBuilder sb = new StringBuilder(); |
| 225 | for (String line : log.split("\n")) { |
| 226 | if (line.contains("audit")) { |
| 227 | sb.append(line + "\n"); |
| 228 | } |
| 229 | } |
| 230 | Slog.i(TAG, "Copied " + sb.toString().length() + " worth of audits to DropBox"); |
| 231 | db.addText(tag, headers + sb.toString()); |
| 232 | } |
Ken Sumrall | fefefbb | 2013-09-12 19:34:36 -0700 | [diff] [blame] | 233 | |
| 234 | private static void addFsckErrorsToDropBox(DropBoxManager db, SharedPreferences prefs, |
| 235 | String headers, int maxSize, String tag) throws IOException { |
| 236 | boolean upload_needed = false; |
| 237 | if (db == null || !db.isTagEnabled(tag)) return; // Logging disabled |
| 238 | Slog.i(TAG, "Checking for fsck errors"); |
| 239 | |
| 240 | File file = new File("/dev/fscklogs/log"); |
| 241 | long fileTime = file.lastModified(); |
| 242 | if (fileTime <= 0) return; // File does not exist |
| 243 | |
| 244 | String log = FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n"); |
| 245 | StringBuilder sb = new StringBuilder(); |
| 246 | for (String line : log.split("\n")) { |
| 247 | if (line.contains("FILE SYSTEM WAS MODIFIED")) { |
| 248 | upload_needed = true; |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (upload_needed) { |
| 254 | addFileToDropBox(db, prefs, headers, "/dev/fscklogs/log", maxSize, tag); |
| 255 | } |
| 256 | |
| 257 | // Remove the file so we don't re-upload if the runtime restarts. |
| 258 | file.delete(); |
| 259 | } |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 260 | } |