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