blob: bce85cee338b9c3f4f8819d93b73be51ffbaa0ef [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;
Dianne Hackborn13579ed2012-11-28 18:05:36 -080023import android.content.pm.IPackageManager;
Dan Egnor3d40df32009-11-17 13:36:31 -080024import android.os.Build;
25import android.os.DropBoxManager;
Dan Egnor492c6ed2010-01-27 14:52:57 -080026import android.os.FileObserver;
Dan Egnor3d40df32009-11-17 13:36:31 -080027import android.os.FileUtils;
Doug Zongker1af33d02010-01-05 11:28:55 -080028import android.os.RecoverySystem;
Dianne Hackborn13579ed2012-11-28 18:05:36 -080029import android.os.RemoteException;
30import android.os.ServiceManager;
Dan Egnor3d40df32009-11-17 13:36:31 -080031import android.os.SystemProperties;
Jeff Sharkey948eef82012-03-20 17:44:42 -070032import android.provider.Downloads;
Joe Onorato8a9b2202010-02-26 18:56:32 -080033import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
Dan Egnor3d40df32009-11-17 13:36:31 -080035import java.io.File;
36import java.io.IOException;
37
38/**
39 * Performs a number of miscellaneous, non-system-critical actions
40 * after the system has finished booting.
41 */
42public class BootReceiver extends BroadcastReceiver {
43 private static final String TAG = "BootReceiver";
44
Dave Burke0132a9522012-02-27 14:33:30 -080045 // 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 Egnor42471dd2010-01-07 17:25:22 -080049
Dan Egnor492c6ed2010-01-27 14:52:57 -080050 private static final File TOMBSTONE_DIR = new File("/data/tombstones");
51
Doug Zongker944ff0b2010-04-05 15:07:36 -070052 // 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 Egnor492c6ed2010-01-27 14:52:57 -080060 // Keep a reference to the observer so the finalizer doesn't disable it.
61 private static FileObserver sTombstoneObserver = null;
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 @Override
Dan Egnorc95142d2010-03-11 12:31:23 -080064 public void onReceive(final Context context, Intent intent) {
Dan Egnorc95142d2010-03-11 12:31:23 -080065 // 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 Zongker944ff0b2010-04-05 15:07:36 -070074 try {
Dianne Hackborn13579ed2012-11-28 18:05:36 -080075 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 Zongker944ff0b2010-04-05 15:07:36 -070084 } catch (Exception e) {
85 Slog.e(TAG, "Can't remove old update packages", e);
86 }
87
Dan Egnorc95142d2010-03-11 12:31:23 -080088 }
89 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Jeff Sharkey948eef82012-03-20 17:44:42 -070092 private void removeOldUpdatePackages(Context context) {
93 Downloads.removeAllDownloadsByPackage(context, OLD_UPDATER_PACKAGE, OLD_UPDATER_CLASS);
Doug Zongker944ff0b2010-04-05 15:07:36 -070094 }
95
Dan Egnor492c6ed2010-01-27 14:52:57 -080096 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 Egnorc95142d2010-03-11 12:31:23 -080099 final String headers = new StringBuilder(512)
Dan Egnor492c6ed2010-01-27 14:52:57 -0800100 .append("Build: ").append(Build.FINGERPRINT).append("\n")
101 .append("Hardware: ").append(Build.BOARD).append("\n")
Colin Cross8b2c9162012-10-22 14:01:36 -0700102 .append("Revision: ")
103 .append(SystemProperties.get("ro.revision", "")).append("\n")
Dan Egnor492c6ed2010-01-27 14:52:57 -0800104 .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 Egnorc95142d2010-03-11 12:31:23 -0800108 .append("\n").toString();
Dan Egnor3d40df32009-11-17 13:36:31 -0800109
Dan Egnorc95142d2010-03-11 12:31:23 -0800110 String recovery = RecoverySystem.handleAftermath();
111 if (recovery != null && db != null) {
112 db.addText("SYSTEM_RECOVERY_LOG", headers + recovery);
113 }
Dan Egnor3d40df32009-11-17 13:36:31 -0800114
115 if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) {
116 String now = Long.toString(System.currentTimeMillis());
117 SystemProperties.set("ro.runtime.firstboot", now);
Dan Egnorc95142d2010-03-11 12:31:23 -0800118 if (db != null) db.addText("SYSTEM_BOOT", headers);
Dan Egnor289e5802010-02-11 10:40:49 -0800119
120 // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile())
Dan Egnorc95142d2010-03-11 12:31:23 -0800121 addFileToDropBox(db, prefs, headers, "/proc/last_kmsg",
Dan Egnor289e5802010-02-11 10:40:49 -0800122 -LOG_SIZE, "SYSTEM_LAST_KMSG");
Todd Poynorc95bb562013-11-22 17:15:49 -0800123 addFileToDropBox(db, prefs, headers, "/sys/fs/pstore/console-ramoops",
124 -LOG_SIZE, "SYSTEM_LAST_KMSG");
Dan Egnorc95142d2010-03-11 12:31:23 -0800125 addFileToDropBox(db, prefs, headers, "/cache/recovery/log",
Dan Egnor289e5802010-02-11 10:40:49 -0800126 -LOG_SIZE, "SYSTEM_RECOVERY_LOG");
Dan Egnorc95142d2010-03-11 12:31:23 -0800127 addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_console",
Dan Egnor289e5802010-02-11 10:40:49 -0800128 -LOG_SIZE, "APANIC_CONSOLE");
Dan Egnorc95142d2010-03-11 12:31:23 -0800129 addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_threads",
Dan Egnor289e5802010-02-11 10:40:49 -0800130 -LOG_SIZE, "APANIC_THREADS");
Geremy Condra51611eb2013-02-13 16:07:21 -0800131 addAuditErrorsToDropBox(db, prefs, headers, -LOG_SIZE, "SYSTEM_AUDIT");
Ken Sumrallfefefbb2013-09-12 19:34:36 -0700132 addFsckErrorsToDropBox(db, prefs, headers, -LOG_SIZE, "SYSTEM_FSCK");
Dan Egnor3d40df32009-11-17 13:36:31 -0800133 } else {
Dan Egnorc95142d2010-03-11 12:31:23 -0800134 if (db != null) db.addText("SYSTEM_RESTART", headers);
Dan Egnor3d40df32009-11-17 13:36:31 -0800135 }
136
Dan Egnor492c6ed2010-01-27 14:52:57 -0800137 // Scan existing tombstones (in case any new ones appeared)
138 File[] tombstoneFiles = TOMBSTONE_DIR.listFiles();
139 for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) {
Dan Egnorc95142d2010-03-11 12:31:23 -0800140 addFileToDropBox(db, prefs, headers, tombstoneFiles[i].getPath(),
Dan Egnor289e5802010-02-11 10:40:49 -0800141 LOG_SIZE, "SYSTEM_TOMBSTONE");
Dan Egnor492c6ed2010-01-27 14:52:57 -0800142 }
143
144 // Start watching for new tombstone files; will record them as they occur.
145 // This gets registered with the singleton file observer thread.
146 sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) {
147 @Override
148 public void onEvent(int event, String path) {
149 try {
150 String filename = new File(TOMBSTONE_DIR, path).getPath();
Dan Egnorc95142d2010-03-11 12:31:23 -0800151 addFileToDropBox(db, prefs, headers, filename, LOG_SIZE, "SYSTEM_TOMBSTONE");
Dan Egnor492c6ed2010-01-27 14:52:57 -0800152 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800153 Slog.e(TAG, "Can't log tombstone", e);
Dan Egnor492c6ed2010-01-27 14:52:57 -0800154 }
155 }
156 };
157
158 sTombstoneObserver.startWatching();
Dan Egnor3d40df32009-11-17 13:36:31 -0800159 }
160
Dan Egnor492c6ed2010-01-27 14:52:57 -0800161 private static void addFileToDropBox(
162 DropBoxManager db, SharedPreferences prefs,
Dan Egnor289e5802010-02-11 10:40:49 -0800163 String headers, String filename, int maxSize, String tag) throws IOException {
Dan Egnorc95142d2010-03-11 12:31:23 -0800164 if (db == null || !db.isTagEnabled(tag)) return; // Logging disabled
Dan Egnor3d40df32009-11-17 13:36:31 -0800165
166 File file = new File(filename);
167 long fileTime = file.lastModified();
168 if (fileTime <= 0) return; // File does not exist
169
Dan Egnorc95142d2010-03-11 12:31:23 -0800170 if (prefs != null) {
171 long lastTime = prefs.getLong(filename, 0);
172 if (lastTime == fileTime) return; // Already logged this particular file
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700173 // TODO: move all these SharedPreferences Editor commits
174 // outside this function to the end of logBootEvents
Brad Fitzpatrick66fce502010-08-30 18:10:49 -0700175 prefs.edit().putLong(filename, fileTime).apply();
Dan Egnorc95142d2010-03-11 12:31:23 -0800176 }
Dan Egnor42471dd2010-01-07 17:25:22 -0800177
Dan Egnorc95142d2010-03-11 12:31:23 -0800178 Slog.i(TAG, "Copying " + filename + " to DropBox (" + tag + ")");
179 db.addText(tag, headers + FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n"));
Dan Egnor3d40df32009-11-17 13:36:31 -0800180 }
Geremy Condra51611eb2013-02-13 16:07:21 -0800181
182 private static void addAuditErrorsToDropBox(DropBoxManager db, SharedPreferences prefs,
183 String headers, int maxSize, String tag) throws IOException {
184 if (db == null || !db.isTagEnabled(tag)) return; // Logging disabled
185 Slog.i(TAG, "Copying audit failures to DropBox");
186
187 File file = new File("/proc/last_kmsg");
188 long fileTime = file.lastModified();
Todd Poynorc95bb562013-11-22 17:15:49 -0800189 if (fileTime <= 0) {
190 file = new File("/sys/fs/pstore/console-ramoops");
191 fileTime = file.lastModified();
192 }
193
Geremy Condra51611eb2013-02-13 16:07:21 -0800194 if (fileTime <= 0) return; // File does not exist
195
196 if (prefs != null) {
197 long lastTime = prefs.getLong(tag, 0);
198 if (lastTime == fileTime) return; // Already logged this particular file
199 // TODO: move all these SharedPreferences Editor commits
200 // outside this function to the end of logBootEvents
201 prefs.edit().putLong(tag, fileTime).apply();
202 }
203
204 String log = FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n");
205 StringBuilder sb = new StringBuilder();
206 for (String line : log.split("\n")) {
207 if (line.contains("audit")) {
208 sb.append(line + "\n");
209 }
210 }
211 Slog.i(TAG, "Copied " + sb.toString().length() + " worth of audits to DropBox");
212 db.addText(tag, headers + sb.toString());
213 }
Ken Sumrallfefefbb2013-09-12 19:34:36 -0700214
215 private static void addFsckErrorsToDropBox(DropBoxManager db, SharedPreferences prefs,
216 String headers, int maxSize, String tag) throws IOException {
217 boolean upload_needed = false;
218 if (db == null || !db.isTagEnabled(tag)) return; // Logging disabled
219 Slog.i(TAG, "Checking for fsck errors");
220
221 File file = new File("/dev/fscklogs/log");
222 long fileTime = file.lastModified();
223 if (fileTime <= 0) return; // File does not exist
224
225 String log = FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n");
226 StringBuilder sb = new StringBuilder();
227 for (String line : log.split("\n")) {
228 if (line.contains("FILE SYSTEM WAS MODIFIED")) {
229 upload_needed = true;
230 break;
231 }
232 }
233
234 if (upload_needed) {
235 addFileToDropBox(db, prefs, headers, "/dev/fscklogs/log", maxSize, tag);
236 }
237
238 // Remove the file so we don't re-upload if the runtime restarts.
239 file.delete();
240 }
Dan Egnor3d40df32009-11-17 13:36:31 -0800241}