The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 2 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | |
| 17 | package com.android.server; |
| 18 | |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 23 | import android.os.Build; |
| 24 | import android.os.DropBoxManager; |
| 25 | import android.os.FileUtils; |
Doug Zongker | 1af33d0 | 2010-01-05 11:28:55 -0800 | [diff] [blame] | 26 | import android.os.RecoverySystem; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 27 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.provider.Settings; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 29 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 31 | import java.io.File; |
| 32 | import java.io.IOException; |
| 33 | |
| 34 | /** |
| 35 | * Performs a number of miscellaneous, non-system-critical actions |
| 36 | * after the system has finished booting. |
| 37 | */ |
| 38 | public class BootReceiver extends BroadcastReceiver { |
| 39 | private static final String TAG = "BootReceiver"; |
| 40 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame^] | 41 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | @Override |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 46 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 71 | private void logBootEvents(Context context) throws IOException { |
| 72 | DropBoxManager db = (DropBoxManager) context.getSystemService(Context.DROPBOX_SERVICE); |
| 73 | |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame^] | 74 | 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 Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 81 | |
| 82 | if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) { |
| 83 | String now = Long.toString(System.currentTimeMillis()); |
| 84 | SystemProperties.set("ro.runtime.firstboot", now); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame^] | 85 | if (db != null) db.addText("SYSTEM_BOOT", props.toString()); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 86 | } else { |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame^] | 87 | if (db != null) db.addText("SYSTEM_RESTART", props.toString()); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 88 | 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 Egnor | c4cf6ca | 2009-11-23 19:23:49 -0800 | [diff] [blame] | 93 | logBootFile(cr, db, "/proc/last_kmsg", "SYSTEM_LAST_KMSG"); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 94 | 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 Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame^] | 109 | 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 Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 117 | } |
| 118 | } |