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; |
| 26 | import android.os.ParcelFileDescriptor; |
| 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 com.android.internal.os.RecoverySystem; |
| 32 | |
| 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | @Override |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame^] | 44 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | } |
| 67 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame^] | 69 | 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 | } |