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.ContentResolver; 219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context; 229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Intent; 23492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnorimport android.content.SharedPreferences; 24944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongkerimport android.net.Downloads; 253d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.Build; 263d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.DropBoxManager; 27492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnorimport android.os.FileObserver; 283d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.FileUtils; 291af33d0ddc2f50ade146e4d48e2feb6f1d553427Doug Zongkerimport android.os.RecoverySystem; 303d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.SystemProperties; 319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.provider.Settings; 328a9b22056b13477f59df934928c00c58b5871c95Joe Onoratoimport android.util.Slog; 333d40df335e4c0df972720271a84277077f168f65Dan Egnor 343d40df335e4c0df972720271a84277077f168f65Dan Egnorimport java.io.File; 353d40df335e4c0df972720271a84277077f168f65Dan Egnorimport java.io.IOException; 363d40df335e4c0df972720271a84277077f168f65Dan Egnor 373d40df335e4c0df972720271a84277077f168f65Dan Egnor/** 383d40df335e4c0df972720271a84277077f168f65Dan Egnor * Performs a number of miscellaneous, non-system-critical actions 393d40df335e4c0df972720271a84277077f168f65Dan Egnor * after the system has finished booting. 403d40df335e4c0df972720271a84277077f168f65Dan Egnor */ 413d40df335e4c0df972720271a84277077f168f65Dan Egnorpublic class BootReceiver extends BroadcastReceiver { 423d40df335e4c0df972720271a84277077f168f65Dan Egnor private static final String TAG = "BootReceiver"; 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 44289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor // Maximum size of a logged event (files get truncated if they're longer) 45289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor private static final int LOG_SIZE = 65536; 4642471dd5552a346dd82a58a663159875ccc4fb79Dan Egnor 47492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor private static final File TOMBSTONE_DIR = new File("/data/tombstones"); 48492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor 49944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker // The pre-froyo package and class of the system updater, which 50944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker // ran in the system process. We need to remove its packages here 51944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker // in order to clean up after a pre-froyo-to-froyo update. 52944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker private static final String OLD_UPDATER_PACKAGE = 53944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker "com.google.android.systemupdater"; 54944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker private static final String OLD_UPDATER_CLASS = 55944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker "com.google.android.systemupdater.SystemUpdateReceiver"; 56944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker 57492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor // Keep a reference to the observer so the finalizer doesn't disable it. 58492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor private static FileObserver sTombstoneObserver = null; 59492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor 609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @Override 61c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor public void onReceive(final Context context, Intent intent) { 623d40df335e4c0df972720271a84277077f168f65Dan Egnor try { 633d40df335e4c0df972720271a84277077f168f65Dan Egnor // Start the load average overlay, if activated 643d40df335e4c0df972720271a84277077f168f65Dan Egnor ContentResolver res = context.getContentResolver(); 653d40df335e4c0df972720271a84277077f168f65Dan Egnor if (Settings.System.getInt(res, Settings.System.SHOW_PROCESSES, 0) != 0) { 663d40df335e4c0df972720271a84277077f168f65Dan Egnor Intent loadavg = new Intent(context, com.android.server.LoadAverageService.class); 673d40df335e4c0df972720271a84277077f168f65Dan Egnor context.startService(loadavg); 683d40df335e4c0df972720271a84277077f168f65Dan Egnor } 693d40df335e4c0df972720271a84277077f168f65Dan Egnor } catch (Exception e) { 708a9b22056b13477f59df934928c00c58b5871c95Joe Onorato Slog.e(TAG, "Can't start load average service", e); 719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 72c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor 73c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor // Log boot events in the background to avoid blocking the main thread with I/O 74c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor new Thread() { 75c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor @Override 76c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor public void run() { 77c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor try { 78c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor logBootEvents(context); 79c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor } catch (Exception e) { 80c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor Slog.e(TAG, "Can't log boot events", e); 81c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor } 82944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker try { 83944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker removeOldUpdatePackages(context); 84944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker } catch (Exception e) { 85944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker Slog.e(TAG, "Can't remove old update packages", e); 86944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker } 87944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker 88c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor } 89c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor }.start(); 909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 92944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker private void removeOldUpdatePackages(Context ctx) { 93944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker Downloads.ByUri.removeAllDownloadsByPackage( 94944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker ctx, OLD_UPDATER_PACKAGE, OLD_UPDATER_CLASS); 95944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker } 96944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker 97492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor private void logBootEvents(Context ctx) throws IOException { 98492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE); 99492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor final SharedPreferences prefs = ctx.getSharedPreferences("log_files", Context.MODE_PRIVATE); 100c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor final String headers = new StringBuilder(512) 101492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append("Build: ").append(Build.FINGERPRINT).append("\n") 102492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append("Hardware: ").append(Build.BOARD).append("\n") 103492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append("Bootloader: ").append(Build.BOOTLOADER).append("\n") 104492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append("Radio: ").append(Build.RADIO).append("\n") 105492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append("Kernel: ") 106492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor .append(FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n")) 107c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor .append("\n").toString(); 1083d40df335e4c0df972720271a84277077f168f65Dan Egnor 109c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor String recovery = RecoverySystem.handleAftermath(); 110c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (recovery != null && db != null) { 111c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor db.addText("SYSTEM_RECOVERY_LOG", headers + recovery); 112c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor } 1133d40df335e4c0df972720271a84277077f168f65Dan Egnor 1143d40df335e4c0df972720271a84277077f168f65Dan Egnor if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) { 1153d40df335e4c0df972720271a84277077f168f65Dan Egnor String now = Long.toString(System.currentTimeMillis()); 1163d40df335e4c0df972720271a84277077f168f65Dan Egnor SystemProperties.set("ro.runtime.firstboot", now); 117c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (db != null) db.addText("SYSTEM_BOOT", headers); 118289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor 119289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile()) 120c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, "/proc/last_kmsg", 121289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor -LOG_SIZE, "SYSTEM_LAST_KMSG"); 122c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, "/cache/recovery/log", 123289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor -LOG_SIZE, "SYSTEM_RECOVERY_LOG"); 124c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_console", 125289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor -LOG_SIZE, "APANIC_CONSOLE"); 126c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_threads", 127289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor -LOG_SIZE, "APANIC_THREADS"); 1283d40df335e4c0df972720271a84277077f168f65Dan Egnor } else { 129c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (db != null) db.addText("SYSTEM_RESTART", headers); 130492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor } 131492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor 132492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor // Scan existing tombstones (in case any new ones appeared) 133492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor File[] tombstoneFiles = TOMBSTONE_DIR.listFiles(); 134492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) { 135c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, tombstoneFiles[i].getPath(), 136289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor LOG_SIZE, "SYSTEM_TOMBSTONE"); 1373d40df335e4c0df972720271a84277077f168f65Dan Egnor } 1383d40df335e4c0df972720271a84277077f168f65Dan Egnor 139492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor // Start watching for new tombstone files; will record them as they occur. 140492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor // This gets registered with the singleton file observer thread. 141492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) { 142492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor @Override 143492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor public void onEvent(int event, String path) { 144492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor try { 145492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor String filename = new File(TOMBSTONE_DIR, path).getPath(); 146c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor addFileToDropBox(db, prefs, headers, filename, LOG_SIZE, "SYSTEM_TOMBSTONE"); 147492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor } catch (IOException e) { 1488a9b22056b13477f59df934928c00c58b5871c95Joe Onorato Slog.e(TAG, "Can't log tombstone", e); 149492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor } 150492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor } 151492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor }; 152492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor 153492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor sTombstoneObserver.startWatching(); 1543d40df335e4c0df972720271a84277077f168f65Dan Egnor } 1553d40df335e4c0df972720271a84277077f168f65Dan Egnor 156492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor private static void addFileToDropBox( 157492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor DropBoxManager db, SharedPreferences prefs, 158289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor String headers, String filename, int maxSize, String tag) throws IOException { 159c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (db == null || !db.isTagEnabled(tag)) return; // Logging disabled 1603d40df335e4c0df972720271a84277077f168f65Dan Egnor 1613d40df335e4c0df972720271a84277077f168f65Dan Egnor File file = new File(filename); 1623d40df335e4c0df972720271a84277077f168f65Dan Egnor long fileTime = file.lastModified(); 1633d40df335e4c0df972720271a84277077f168f65Dan Egnor if (fileTime <= 0) return; // File does not exist 1643d40df335e4c0df972720271a84277077f168f65Dan Egnor 165c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (prefs != null) { 166c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor long lastTime = prefs.getLong(filename, 0); 167c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor if (lastTime == fileTime) return; // Already logged this particular file 168333b8cba996c8ebb8ca55ebfc5cc536bdd64af94Brad Fitzpatrick // TODO: move all these SharedPreferences Editor commits 169333b8cba996c8ebb8ca55ebfc5cc536bdd64af94Brad Fitzpatrick // outside this function to the end of logBootEvents 17066fce5068a8a3aeb28aaf713843891b286a75280Brad Fitzpatrick prefs.edit().putLong(filename, fileTime).apply(); 171c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor } 17242471dd5552a346dd82a58a663159875ccc4fb79Dan Egnor 173c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor Slog.i(TAG, "Copying " + filename + " to DropBox (" + tag + ")"); 174c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor db.addText(tag, headers + FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n")); 1753d40df335e4c0df972720271a84277077f168f65Dan Egnor } 1763d40df335e4c0df972720271a84277077f168f65Dan Egnor} 177