blob: 7e1de5adb751687287687a9f180e5f492a950fb2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Dan Egnor3d40df32009-11-17 13:36:31 -08002 * 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 Project9066cfe2009-03-03 19:31:44 -080016
17package com.android.server;
18
Dan Egnor3d40df32009-11-17 13:36:31 -080019import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
21import android.content.Intent;
Dan Egnor492c6ed2010-01-27 14:52:57 -080022import android.content.SharedPreferences;
Dan Egnor3d40df32009-11-17 13:36:31 -080023import android.os.Build;
24import android.os.DropBoxManager;
Dan Egnor492c6ed2010-01-27 14:52:57 -080025import android.os.FileObserver;
Dan Egnor3d40df32009-11-17 13:36:31 -080026import android.os.FileUtils;
Doug Zongker1af33d02010-01-05 11:28:55 -080027import android.os.RecoverySystem;
Dan Egnor3d40df32009-11-17 13:36:31 -080028import android.os.SystemProperties;
Jeff Sharkey948eef82012-03-20 17:44:42 -070029import android.provider.Downloads;
Joe Onorato8a9b2202010-02-26 18:56:32 -080030import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Dan Egnor3d40df32009-11-17 13:36:31 -080032import java.io.File;
33import java.io.IOException;
34
35/**
36 * Performs a number of miscellaneous, non-system-critical actions
37 * after the system has finished booting.
38 */
39public class BootReceiver extends BroadcastReceiver {
40 private static final String TAG = "BootReceiver";
41
Dave Burke0132a9522012-02-27 14:33:30 -080042 // Maximum size of a logged event (files get truncated if they're longer).
43 // Give userdebug builds a larger max to capture extra debug, esp. for last_kmsg.
44 private static final int LOG_SIZE =
45 SystemProperties.getInt("ro.debuggable", 0) == 1 ? 98304 : 65536;
Dan Egnor42471dd2010-01-07 17:25:22 -080046
Dan Egnor492c6ed2010-01-27 14:52:57 -080047 private static final File TOMBSTONE_DIR = new File("/data/tombstones");
48
Doug Zongker944ff0b2010-04-05 15:07:36 -070049 // The pre-froyo package and class of the system updater, which
50 // ran in the system process. We need to remove its packages here
51 // in order to clean up after a pre-froyo-to-froyo update.
52 private static final String OLD_UPDATER_PACKAGE =
53 "com.google.android.systemupdater";
54 private static final String OLD_UPDATER_CLASS =
55 "com.google.android.systemupdater.SystemUpdateReceiver";
56
Dan Egnor492c6ed2010-01-27 14:52:57 -080057 // Keep a reference to the observer so the finalizer doesn't disable it.
58 private static FileObserver sTombstoneObserver = null;
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 @Override
Dan Egnorc95142d2010-03-11 12:31:23 -080061 public void onReceive(final Context context, Intent intent) {
Dan Egnorc95142d2010-03-11 12:31:23 -080062 // Log boot events in the background to avoid blocking the main thread with I/O
63 new Thread() {
64 @Override
65 public void run() {
66 try {
67 logBootEvents(context);
68 } catch (Exception e) {
69 Slog.e(TAG, "Can't log boot events", e);
70 }
Doug Zongker944ff0b2010-04-05 15:07:36 -070071 try {
72 removeOldUpdatePackages(context);
73 } catch (Exception e) {
74 Slog.e(TAG, "Can't remove old update packages", e);
75 }
76
Dan Egnorc95142d2010-03-11 12:31:23 -080077 }
78 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
Jeff Sharkey948eef82012-03-20 17:44:42 -070081 private void removeOldUpdatePackages(Context context) {
82 Downloads.removeAllDownloadsByPackage(context, OLD_UPDATER_PACKAGE, OLD_UPDATER_CLASS);
Doug Zongker944ff0b2010-04-05 15:07:36 -070083 }
84
Dan Egnor492c6ed2010-01-27 14:52:57 -080085 private void logBootEvents(Context ctx) throws IOException {
86 final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE);
87 final SharedPreferences prefs = ctx.getSharedPreferences("log_files", Context.MODE_PRIVATE);
Dan Egnorc95142d2010-03-11 12:31:23 -080088 final String headers = new StringBuilder(512)
Dan Egnor492c6ed2010-01-27 14:52:57 -080089 .append("Build: ").append(Build.FINGERPRINT).append("\n")
90 .append("Hardware: ").append(Build.BOARD).append("\n")
Colin Cross8b2c9162012-10-22 14:01:36 -070091 .append("Revision: ")
92 .append(SystemProperties.get("ro.revision", "")).append("\n")
Dan Egnor492c6ed2010-01-27 14:52:57 -080093 .append("Bootloader: ").append(Build.BOOTLOADER).append("\n")
94 .append("Radio: ").append(Build.RADIO).append("\n")
95 .append("Kernel: ")
96 .append(FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n"))
Dan Egnorc95142d2010-03-11 12:31:23 -080097 .append("\n").toString();
Dan Egnor3d40df32009-11-17 13:36:31 -080098
Dan Egnorc95142d2010-03-11 12:31:23 -080099 String recovery = RecoverySystem.handleAftermath();
100 if (recovery != null && db != null) {
101 db.addText("SYSTEM_RECOVERY_LOG", headers + recovery);
102 }
Dan Egnor3d40df32009-11-17 13:36:31 -0800103
104 if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) {
105 String now = Long.toString(System.currentTimeMillis());
106 SystemProperties.set("ro.runtime.firstboot", now);
Dan Egnorc95142d2010-03-11 12:31:23 -0800107 if (db != null) db.addText("SYSTEM_BOOT", headers);
Dan Egnor289e5802010-02-11 10:40:49 -0800108
109 // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile())
Dan Egnorc95142d2010-03-11 12:31:23 -0800110 addFileToDropBox(db, prefs, headers, "/proc/last_kmsg",
Dan Egnor289e5802010-02-11 10:40:49 -0800111 -LOG_SIZE, "SYSTEM_LAST_KMSG");
Dan Egnorc95142d2010-03-11 12:31:23 -0800112 addFileToDropBox(db, prefs, headers, "/cache/recovery/log",
Dan Egnor289e5802010-02-11 10:40:49 -0800113 -LOG_SIZE, "SYSTEM_RECOVERY_LOG");
Dan Egnorc95142d2010-03-11 12:31:23 -0800114 addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_console",
Dan Egnor289e5802010-02-11 10:40:49 -0800115 -LOG_SIZE, "APANIC_CONSOLE");
Dan Egnorc95142d2010-03-11 12:31:23 -0800116 addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_threads",
Dan Egnor289e5802010-02-11 10:40:49 -0800117 -LOG_SIZE, "APANIC_THREADS");
Dan Egnor3d40df32009-11-17 13:36:31 -0800118 } else {
Dan Egnorc95142d2010-03-11 12:31:23 -0800119 if (db != null) db.addText("SYSTEM_RESTART", headers);
Dan Egnor3d40df32009-11-17 13:36:31 -0800120 }
121
Dan Egnor492c6ed2010-01-27 14:52:57 -0800122 // Scan existing tombstones (in case any new ones appeared)
123 File[] tombstoneFiles = TOMBSTONE_DIR.listFiles();
124 for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) {
Dan Egnorc95142d2010-03-11 12:31:23 -0800125 addFileToDropBox(db, prefs, headers, tombstoneFiles[i].getPath(),
Dan Egnor289e5802010-02-11 10:40:49 -0800126 LOG_SIZE, "SYSTEM_TOMBSTONE");
Dan Egnor492c6ed2010-01-27 14:52:57 -0800127 }
128
129 // Start watching for new tombstone files; will record them as they occur.
130 // This gets registered with the singleton file observer thread.
131 sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) {
132 @Override
133 public void onEvent(int event, String path) {
134 try {
135 String filename = new File(TOMBSTONE_DIR, path).getPath();
Dan Egnorc95142d2010-03-11 12:31:23 -0800136 addFileToDropBox(db, prefs, headers, filename, LOG_SIZE, "SYSTEM_TOMBSTONE");
Dan Egnor492c6ed2010-01-27 14:52:57 -0800137 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800138 Slog.e(TAG, "Can't log tombstone", e);
Dan Egnor492c6ed2010-01-27 14:52:57 -0800139 }
140 }
141 };
142
143 sTombstoneObserver.startWatching();
Dan Egnor3d40df32009-11-17 13:36:31 -0800144 }
145
Dan Egnor492c6ed2010-01-27 14:52:57 -0800146 private static void addFileToDropBox(
147 DropBoxManager db, SharedPreferences prefs,
Dan Egnor289e5802010-02-11 10:40:49 -0800148 String headers, String filename, int maxSize, String tag) throws IOException {
Dan Egnorc95142d2010-03-11 12:31:23 -0800149 if (db == null || !db.isTagEnabled(tag)) return; // Logging disabled
Dan Egnor3d40df32009-11-17 13:36:31 -0800150
151 File file = new File(filename);
152 long fileTime = file.lastModified();
153 if (fileTime <= 0) return; // File does not exist
154
Dan Egnorc95142d2010-03-11 12:31:23 -0800155 if (prefs != null) {
156 long lastTime = prefs.getLong(filename, 0);
157 if (lastTime == fileTime) return; // Already logged this particular file
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700158 // TODO: move all these SharedPreferences Editor commits
159 // outside this function to the end of logBootEvents
Brad Fitzpatrick66fce502010-08-30 18:10:49 -0700160 prefs.edit().putLong(filename, fileTime).apply();
Dan Egnorc95142d2010-03-11 12:31:23 -0800161 }
Dan Egnor42471dd2010-01-07 17:25:22 -0800162
Dan Egnorc95142d2010-03-11 12:31:23 -0800163 Slog.i(TAG, "Copying " + filename + " to DropBox (" + tag + ")");
164 db.addText(tag, headers + FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n"));
Dan Egnor3d40df32009-11-17 13:36:31 -0800165 }
166}