blob: debbbb4ac1092f8cc13189722efa86aed093c185 [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.ContentResolver;
21import android.content.Context;
22import android.content.Intent;
Dan Egnor3d40df32009-11-17 13:36:31 -080023import android.os.Build;
24import android.os.DropBoxManager;
25import android.os.FileUtils;
Doug Zongker1af33d02010-01-05 11:28:55 -080026import android.os.RecoverySystem;
Dan Egnor3d40df32009-11-17 13:36:31 -080027import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.provider.Settings;
Dan Egnor3d40df32009-11-17 13:36:31 -080029import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Dan Egnor3d40df32009-11-17 13:36:31 -080031import java.io.File;
32import java.io.IOException;
33
34/**
35 * Performs a number of miscellaneous, non-system-critical actions
36 * after the system has finished booting.
37 */
38public class BootReceiver extends BroadcastReceiver {
39 private static final String TAG = "BootReceiver";
40
Dan Egnor42471dd2010-01-07 17:25:22 -080041 // Negative meaning capture the *last* 64K of the file
42 // (passed to FileUtils.readTextFile)
43 private static final int LOG_SIZE = -65536;
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 @Override
Dan Egnor3d40df32009-11-17 13:36:31 -080046 public void onReceive(Context context, Intent intent) {
47 try {
48 logBootEvents(context);
49 } catch (Exception e) {
50 Log.e(TAG, "Can't log boot events", e);
51 }
52
53 try {
54 RecoverySystem.handleAftermath();
55 } catch (Exception e) {
56 Log.e(TAG, "Can't handle recovery aftermath", e);
57 }
58
59 try {
60 // Start the load average overlay, if activated
61 ContentResolver res = context.getContentResolver();
62 if (Settings.System.getInt(res, Settings.System.SHOW_PROCESSES, 0) != 0) {
63 Intent loadavg = new Intent(context, com.android.server.LoadAverageService.class);
64 context.startService(loadavg);
65 }
66 } catch (Exception e) {
67 Log.e(TAG, "Can't start load average service", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 }
69 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
Dan Egnor3d40df32009-11-17 13:36:31 -080071 private void logBootEvents(Context context) throws IOException {
72 DropBoxManager db = (DropBoxManager) context.getSystemService(Context.DROPBOX_SERVICE);
73
Dan Egnor42471dd2010-01-07 17:25:22 -080074 StringBuilder props = new StringBuilder();
75 props.append("Build: ").append(Build.FINGERPRINT).append("\n");
76 props.append("Hardware: ").append(Build.BOARD).append("\n");
77 props.append("Bootloader: ").append(Build.BOOTLOADER).append("\n");
78 props.append("Radio: ").append(Build.RADIO).append("\n");
79 props.append("Kernel: ");
80 props.append(FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n"));
Dan Egnor3d40df32009-11-17 13:36:31 -080081
82 if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) {
83 String now = Long.toString(System.currentTimeMillis());
84 SystemProperties.set("ro.runtime.firstboot", now);
Dan Egnor42471dd2010-01-07 17:25:22 -080085 if (db != null) db.addText("SYSTEM_BOOT", props.toString());
Dan Egnor3d40df32009-11-17 13:36:31 -080086 } else {
Dan Egnor42471dd2010-01-07 17:25:22 -080087 if (db != null) db.addText("SYSTEM_RESTART", props.toString());
Dan Egnor3d40df32009-11-17 13:36:31 -080088 return; // Subsequent boot, don't log kernel boot log
89 }
90
91 ContentResolver cr = context.getContentResolver();
92 logBootFile(cr, db, "/cache/recovery/log", "SYSTEM_RECOVERY_LOG");
Dan Egnorc4cf6ca2009-11-23 19:23:49 -080093 logBootFile(cr, db, "/proc/last_kmsg", "SYSTEM_LAST_KMSG");
Dan Egnor3d40df32009-11-17 13:36:31 -080094 logBootFile(cr, db, "/data/dontpanic/apanic_console", "APANIC_CONSOLE");
95 logBootFile(cr, db, "/data/dontpanic/apanic_threads", "APANIC_THREADS");
96 }
97
98 private void logBootFile(ContentResolver cr, DropBoxManager db, String filename, String tag)
99 throws IOException {
100 if (cr == null || db == null || !db.isTagEnabled(tag)) return; // Logging disabled
101
102 File file = new File(filename);
103 long fileTime = file.lastModified();
104 if (fileTime <= 0) return; // File does not exist
105
106 String setting = "logfile:" + filename;
107 long lastTime = Settings.Secure.getLong(cr, setting, 0);
108 if (lastTime == fileTime) return; // Already logged this particular file
Dan Egnor42471dd2010-01-07 17:25:22 -0800109 Settings.Secure.putLong(cr, setting, fileTime);
110
111 StringBuilder report = new StringBuilder();
112 report.append("Build: ").append(Build.FINGERPRINT).append("\n");
113 report.append("Kernel: ");
114 report.append(FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n"));
115 report.append(FileUtils.readTextFile(new File(filename), LOG_SIZE, "[[TRUNCATED]]\n"));
116 db.addText(tag, report.toString());
Dan Egnor3d40df32009-11-17 13:36:31 -0800117 }
118}