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 | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 23 | import android.content.SharedPreferences; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 24 | import android.os.Build; |
| 25 | import android.os.DropBoxManager; |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 26 | import android.os.FileObserver; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 27 | import android.os.FileUtils; |
Doug Zongker | 1af33d0 | 2010-01-05 11:28:55 -0800 | [diff] [blame] | 28 | import android.os.RecoverySystem; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 29 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.provider.Settings; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 31 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 33 | import java.io.File; |
| 34 | import java.io.IOException; |
| 35 | |
| 36 | /** |
| 37 | * Performs a number of miscellaneous, non-system-critical actions |
| 38 | * after the system has finished booting. |
| 39 | */ |
| 40 | public class BootReceiver extends BroadcastReceiver { |
| 41 | private static final String TAG = "BootReceiver"; |
| 42 | |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 43 | // Maximum size of a logged event (files get truncated if they're longer) |
| 44 | private static final int LOG_SIZE = 65536; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 45 | |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 46 | private static final File TOMBSTONE_DIR = new File("/data/tombstones"); |
| 47 | |
| 48 | // Keep a reference to the observer so the finalizer doesn't disable it. |
| 49 | private static FileObserver sTombstoneObserver = null; |
| 50 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | @Override |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 52 | public void onReceive(final Context context, Intent intent) { |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 53 | try { |
| 54 | // Start the load average overlay, if activated |
| 55 | ContentResolver res = context.getContentResolver(); |
| 56 | if (Settings.System.getInt(res, Settings.System.SHOW_PROCESSES, 0) != 0) { |
| 57 | Intent loadavg = new Intent(context, com.android.server.LoadAverageService.class); |
| 58 | context.startService(loadavg); |
| 59 | } |
| 60 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 61 | Slog.e(TAG, "Can't start load average service", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 63 | |
| 64 | // Log boot events in the background to avoid blocking the main thread with I/O |
| 65 | new Thread() { |
| 66 | @Override |
| 67 | public void run() { |
| 68 | try { |
| 69 | logBootEvents(context); |
| 70 | } catch (Exception e) { |
| 71 | Slog.e(TAG, "Can't log boot events", e); |
| 72 | } |
| 73 | } |
| 74 | }.start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 77 | private void logBootEvents(Context ctx) throws IOException { |
| 78 | final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE); |
| 79 | final SharedPreferences prefs = ctx.getSharedPreferences("log_files", Context.MODE_PRIVATE); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 80 | final String headers = new StringBuilder(512) |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 81 | .append("Build: ").append(Build.FINGERPRINT).append("\n") |
| 82 | .append("Hardware: ").append(Build.BOARD).append("\n") |
| 83 | .append("Bootloader: ").append(Build.BOOTLOADER).append("\n") |
| 84 | .append("Radio: ").append(Build.RADIO).append("\n") |
| 85 | .append("Kernel: ") |
| 86 | .append(FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n")) |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 87 | .append("\n").toString(); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 88 | |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 89 | String recovery = RecoverySystem.handleAftermath(); |
| 90 | if (recovery != null && db != null) { |
| 91 | db.addText("SYSTEM_RECOVERY_LOG", headers + recovery); |
| 92 | } |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 93 | |
| 94 | if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) { |
| 95 | String now = Long.toString(System.currentTimeMillis()); |
| 96 | SystemProperties.set("ro.runtime.firstboot", now); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 97 | if (db != null) db.addText("SYSTEM_BOOT", headers); |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 98 | |
| 99 | // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile()) |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 100 | addFileToDropBox(db, prefs, headers, "/proc/last_kmsg", |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 101 | -LOG_SIZE, "SYSTEM_LAST_KMSG"); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 102 | addFileToDropBox(db, prefs, headers, "/cache/recovery/log", |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 103 | -LOG_SIZE, "SYSTEM_RECOVERY_LOG"); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 104 | addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_console", |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 105 | -LOG_SIZE, "APANIC_CONSOLE"); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 106 | addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_threads", |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 107 | -LOG_SIZE, "APANIC_THREADS"); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 108 | } else { |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 109 | if (db != null) db.addText("SYSTEM_RESTART", headers); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 112 | // Scan existing tombstones (in case any new ones appeared) |
| 113 | File[] tombstoneFiles = TOMBSTONE_DIR.listFiles(); |
| 114 | for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) { |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 115 | addFileToDropBox(db, prefs, headers, tombstoneFiles[i].getPath(), |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 116 | LOG_SIZE, "SYSTEM_TOMBSTONE"); |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Start watching for new tombstone files; will record them as they occur. |
| 120 | // This gets registered with the singleton file observer thread. |
| 121 | sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) { |
| 122 | @Override |
| 123 | public void onEvent(int event, String path) { |
| 124 | try { |
| 125 | String filename = new File(TOMBSTONE_DIR, path).getPath(); |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 126 | addFileToDropBox(db, prefs, headers, filename, LOG_SIZE, "SYSTEM_TOMBSTONE"); |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 127 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 128 | Slog.e(TAG, "Can't log tombstone", e); |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | sTombstoneObserver.startWatching(); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Dan Egnor | 492c6ed | 2010-01-27 14:52:57 -0800 | [diff] [blame] | 136 | private static void addFileToDropBox( |
| 137 | DropBoxManager db, SharedPreferences prefs, |
Dan Egnor | 289e580 | 2010-02-11 10:40:49 -0800 | [diff] [blame] | 138 | String headers, String filename, int maxSize, String tag) throws IOException { |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 139 | if (db == null || !db.isTagEnabled(tag)) return; // Logging disabled |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 140 | |
| 141 | File file = new File(filename); |
| 142 | long fileTime = file.lastModified(); |
| 143 | if (fileTime <= 0) return; // File does not exist |
| 144 | |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 145 | if (prefs != null) { |
| 146 | long lastTime = prefs.getLong(filename, 0); |
| 147 | if (lastTime == fileTime) return; // Already logged this particular file |
| 148 | prefs.edit().putLong(filename, fileTime).commit(); |
| 149 | } |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 150 | |
Dan Egnor | c95142d | 2010-03-11 12:31:23 -0800 | [diff] [blame^] | 151 | Slog.i(TAG, "Copying " + filename + " to DropBox (" + tag + ")"); |
| 152 | db.addText(tag, headers + FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n")); |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 153 | } |
| 154 | } |