19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* 23d40df335e4c0df972720271a84277077f168f65Dan Egnor * Copyright (C) 2009 The Android Open Source Project 33d40df335e4c0df972720271a84277077f168f65Dan Egnor * 43d40df335e4c0df972720271a84277077f168f65Dan Egnor * Licensed under the Apache License, Version 2.0 (the "License"); 53d40df335e4c0df972720271a84277077f168f65Dan Egnor * you may not use this file except in compliance with the License. 63d40df335e4c0df972720271a84277077f168f65Dan Egnor * You may obtain a copy of the License at 73d40df335e4c0df972720271a84277077f168f65Dan Egnor * 83d40df335e4c0df972720271a84277077f168f65Dan Egnor * http://www.apache.org/licenses/LICENSE-2.0 93d40df335e4c0df972720271a84277077f168f65Dan Egnor * 103d40df335e4c0df972720271a84277077f168f65Dan Egnor * Unless required by applicable law or agreed to in writing, software 113d40df335e4c0df972720271a84277077f168f65Dan Egnor * distributed under the License is distributed on an "AS IS" BASIS, 123d40df335e4c0df972720271a84277077f168f65Dan Egnor * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133d40df335e4c0df972720271a84277077f168f65Dan Egnor * See the License for the specific language governing permissions and 143d40df335e4c0df972720271a84277077f168f65Dan Egnor * limitations under the License. 153d40df335e4c0df972720271a84277077f168f65Dan Egnor */ 169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage com.android.server; 189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 193d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.content.BroadcastReceiver; 209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context; 219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Intent; 22492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnorimport android.content.SharedPreferences; 23944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongkerimport android.net.Downloads; 243d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.Build; 253d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.DropBoxManager; 26492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnorimport android.os.FileObserver; 273d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.FileUtils; 281af33d0ddc2f50ade146e4d48e2feb6f1d553427Doug Zongkerimport android.os.RecoverySystem; 293d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.SystemProperties; 308a9b22056b13477f59df934928c00c58b5871c95Joe Onoratoimport android.util.Slog; 313d40df335e4c0df972720271a84277077f168f65Dan Egnor 323d40df335e4c0df972720271a84277077f168f65Dan Egnorimport java.io.File; 333d40df335e4c0df972720271a84277077f168f65Dan Egnorimport java.io.IOException; 343d40df335e4c0df972720271a84277077f168f65Dan Egnor 353d40df335e4c0df972720271a84277077f168f65Dan Egnor/** 363d40df335e4c0df972720271a84277077f168f65Dan Egnor * Performs a number of miscellaneous, non-system-critical actions 373d40df335e4c0df972720271a84277077f168f65Dan Egnor * after the system has finished booting. 383d40df335e4c0df972720271a84277077f168f65Dan Egnor */ 393d40df335e4c0df972720271a84277077f168f65Dan Egnorpublic class BootReceiver extends BroadcastReceiver { 403d40df335e4c0df972720271a84277077f168f65Dan Egnor private static final String TAG = "BootReceiver"; 419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 42289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor // Maximum size of a logged event (files get truncated if they're longer) 43289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor private static final int LOG_SIZE = 65536; 4442471dd5552a346dd82a58a663159875ccc4fb79Dan Egnor 45492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor private static final File TOMBSTONE_DIR = new File("/data/tombstones"); 46492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor 47944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker // The pre-froyo package and class of the system updater, which 48944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker // ran in the system process. We need to remove its packages here 49944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker // in order to clean up after a pre-froyo-to-froyo update. 50944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker private static final String OLD_UPDATER_PACKAGE = 51944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker "com.google.android.systemupdater"; 52944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker private static final String OLD_UPDATER_CLASS = 53944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker "com.google.android.systemupdater.SystemUpdateReceiver"; 54944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker 55492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor // Keep a reference to the observer so the finalizer doesn't disable it. 56492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor private static FileObserver sTombstoneObserver = null; 57492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor 589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @Override 59c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor public void onReceive(final Context context, Intent intent) { 60c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor // Log boot events in the background to avoid blocking the main thread with I/O 61c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor new Thread() { 62c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor @Override 63c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor public void run() { 64c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor try { 65c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor logBootEvents(context); 66c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor } catch (Exception e) { 67c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor Slog.e(TAG, "Can't log boot events", e); 68c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor } 69944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker try { 70944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker removeOldUpdatePackages(context); 71944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker } catch (Exception e) { 72944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker Slog.e(TAG, "Can't remove old update packages", e); 73944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker } 74944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker 75c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor } 76c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor }.start(); 779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 79944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker private void removeOldUpdatePackages(Context ctx) { 80944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker Downloads.ByUri.removeAllDownloadsByPackage( 81944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker ctx, OLD_UPDATER_PACKAGE, OLD_UPDATER_CLASS); 82944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker } 83944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker 84492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor private void logBootEvents(Context ctx) throws IOException { 85492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE); 86492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor final SharedPreferences prefs = ctx.getSharedPreferences("log_files", Context.MODE_PRIVATE); 87c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor final String headers = new StringBuilder(512) 88492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append("Build: ").append(Build.FINGERPRINT).append("\n") 89492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append("Hardware: ").append(Build.BOARD).append("\n") 90492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append("Bootloader: ").append(Build.BOOTLOADER).append("\n") 91492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append("Radio: ").append(Build.RADIO).append("\n") 92492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append("Kernel: ") 93492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append(FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n")) 94c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor .append("\n").toString(); 953d40df335e4c0df972720271a84277077f168f65Dan Egnor 96c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor String recovery = RecoverySystem.handleAftermath(); 97c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (recovery != null && db != null) { 98c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor db.addText("SYSTEM_RECOVERY_LOG", headers + recovery); 99c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor } 1003d40df335e4c0df972720271a84277077f168f65Dan Egnor 1013d40df335e4c0df972720271a84277077f168f65Dan Egnor if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) { 1023d40df335e4c0df972720271a84277077f168f65Dan Egnor String now = Long.toString(System.currentTimeMillis()); 1033d40df335e4c0df972720271a84277077f168f65Dan Egnor SystemProperties.set("ro.runtime.firstboot", now); 104c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (db != null) db.addText("SYSTEM_BOOT", headers); 105289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor 106289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile()) 107c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, "/proc/last_kmsg", 108289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor -LOG_SIZE, "SYSTEM_LAST_KMSG"); 109c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, "/cache/recovery/log", 110289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor -LOG_SIZE, "SYSTEM_RECOVERY_LOG"); 111c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_console", 112289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor -LOG_SIZE, "APANIC_CONSOLE"); 113c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_threads", 114289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor -LOG_SIZE, "APANIC_THREADS"); 1153d40df335e4c0df972720271a84277077f168f65Dan Egnor } else { 116c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (db != null) db.addText("SYSTEM_RESTART", headers); 117492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor } 118492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor 119492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor // Scan existing tombstones (in case any new ones appeared) 120492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor File[] tombstoneFiles = TOMBSTONE_DIR.listFiles(); 121492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) { 122c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, tombstoneFiles[i].getPath(), 123289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor LOG_SIZE, "SYSTEM_TOMBSTONE"); 1243d40df335e4c0df972720271a84277077f168f65Dan Egnor } 1253d40df335e4c0df972720271a84277077f168f65Dan Egnor 126492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor // Start watching for new tombstone files; will record them as they occur. 127492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor // This gets registered with the singleton file observer thread. 128492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) { 129492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor @Override 130492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor public void onEvent(int event, String path) { 131492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor try { 132492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor String filename = new File(TOMBSTONE_DIR, path).getPath(); 133c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, filename, LOG_SIZE, "SYSTEM_TOMBSTONE"); 134492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor } catch (IOException e) { 1358a9b22056b13477f59df934928c00c58b5871c95Joe Onorato Slog.e(TAG, "Can't log tombstone", e); 136492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor } 137492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor } 138492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor }; 139492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor 140492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor sTombstoneObserver.startWatching(); 1413d40df335e4c0df972720271a84277077f168f65Dan Egnor } 1423d40df335e4c0df972720271a84277077f168f65Dan Egnor 143492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor private static void addFileToDropBox( 144492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor DropBoxManager db, SharedPreferences prefs, 145289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor String headers, String filename, int maxSize, String tag) throws IOException { 146c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (db == null || !db.isTagEnabled(tag)) return; // Logging disabled 1473d40df335e4c0df972720271a84277077f168f65Dan Egnor 1483d40df335e4c0df972720271a84277077f168f65Dan Egnor File file = new File(filename); 1493d40df335e4c0df972720271a84277077f168f65Dan Egnor long fileTime = file.lastModified(); 1503d40df335e4c0df972720271a84277077f168f65Dan Egnor if (fileTime <= 0) return; // File does not exist 1513d40df335e4c0df972720271a84277077f168f65Dan Egnor 152c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (prefs != null) { 153c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor long lastTime = prefs.getLong(filename, 0); 154c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (lastTime == fileTime) return; // Already logged this particular file 155333b8cba996c8ebb8ca55ebfc5cc536bdd64af94Brad Fitzpatrick // TODO: move all these SharedPreferences Editor commits 156333b8cba996c8ebb8ca55ebfc5cc536bdd64af94Brad Fitzpatrick // outside this function to the end of logBootEvents 15766fce5068a8a3aeb28aaf713843891b286a75280Brad Fitzpatrick prefs.edit().putLong(filename, fileTime).apply(); 158c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor } 15942471dd5552a346dd82a58a663159875ccc4fb79Dan Egnor 160c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor Slog.i(TAG, "Copying " + filename + " to DropBox (" + tag + ")"); 161c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor db.addText(tag, headers + FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n")); 1623d40df335e4c0df972720271a84277077f168f65Dan Egnor } 1633d40df335e4c0df972720271a84277077f168f65Dan Egnor} 164