blob: 1d31d09f1466539acccf63e71e7eea7e87967306 [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;
26import android.os.ParcelFileDescriptor;
27import 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 com.android.internal.os.RecoverySystem;
32
33import java.io.File;
34import java.io.IOException;
35
36/**
37 * Performs a number of miscellaneous, non-system-critical actions
38 * after the system has finished booting.
39 */
40public class BootReceiver extends BroadcastReceiver {
41 private static final String TAG = "BootReceiver";
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 @Override
Dan Egnor3d40df32009-11-17 13:36:31 -080044 public void onReceive(Context context, Intent intent) {
45 try {
46 logBootEvents(context);
47 } catch (Exception e) {
48 Log.e(TAG, "Can't log boot events", e);
49 }
50
51 try {
52 RecoverySystem.handleAftermath();
53 } catch (Exception e) {
54 Log.e(TAG, "Can't handle recovery aftermath", e);
55 }
56
57 try {
58 // Start the load average overlay, if activated
59 ContentResolver res = context.getContentResolver();
60 if (Settings.System.getInt(res, Settings.System.SHOW_PROCESSES, 0) != 0) {
61 Intent loadavg = new Intent(context, com.android.server.LoadAverageService.class);
62 context.startService(loadavg);
63 }
64 } catch (Exception e) {
65 Log.e(TAG, "Can't start load average service", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 }
67 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
Dan Egnor3d40df32009-11-17 13:36:31 -080069 private void logBootEvents(Context context) throws IOException {
70 DropBoxManager db = (DropBoxManager) context.getSystemService(Context.DROPBOX_SERVICE);
71
72 String build =
73 "Build: " + Build.FINGERPRINT + "\nKernel: " +
74 FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n");
75
76 if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) {
77 String now = Long.toString(System.currentTimeMillis());
78 SystemProperties.set("ro.runtime.firstboot", now);
79 if (db != null) db.addText("SYSTEM_BOOT", build);
80 } else {
81 if (db != null) db.addText("SYSTEM_RESTART", build);
82 return; // Subsequent boot, don't log kernel boot log
83 }
84
85 ContentResolver cr = context.getContentResolver();
86 logBootFile(cr, db, "/cache/recovery/log", "SYSTEM_RECOVERY_LOG");
87 logBootFile(cr, db, "/data/dontpanic/last_kmsg", "SYSTEM_LAST_KMSG");
88 logBootFile(cr, db, "/data/dontpanic/apanic_console", "APANIC_CONSOLE");
89 logBootFile(cr, db, "/data/dontpanic/apanic_threads", "APANIC_THREADS");
90 }
91
92 private void logBootFile(ContentResolver cr, DropBoxManager db, String filename, String tag)
93 throws IOException {
94 if (cr == null || db == null || !db.isTagEnabled(tag)) return; // Logging disabled
95
96 File file = new File(filename);
97 long fileTime = file.lastModified();
98 if (fileTime <= 0) return; // File does not exist
99
100 String setting = "logfile:" + filename;
101 long lastTime = Settings.Secure.getLong(cr, setting, 0);
102 if (lastTime == fileTime) return; // Already logged this particular file
103
104 db.addFile(tag,
105 ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY),
106 DropBoxManager.IS_TEXT);
107 }
108}