19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
23d40df335e4c0df972720271a84277077f168f65Dan Egnor * Copyright (C) 2009 The Android Open Source Project
33d40df335e4c0df972720271a84277077f168f65Dan Egnor *
43d40df335e4c0df972720271a84277077f168f65Dan Egnor * Licensed under the Apache License, Version 2.0 (the "License");
53d40df335e4c0df972720271a84277077f168f65Dan Egnor * you may not use this file except in compliance with the License.
63d40df335e4c0df972720271a84277077f168f65Dan Egnor * You may obtain a copy of the License at
73d40df335e4c0df972720271a84277077f168f65Dan Egnor *
83d40df335e4c0df972720271a84277077f168f65Dan Egnor *      http://www.apache.org/licenses/LICENSE-2.0
93d40df335e4c0df972720271a84277077f168f65Dan Egnor *
103d40df335e4c0df972720271a84277077f168f65Dan Egnor * Unless required by applicable law or agreed to in writing, software
113d40df335e4c0df972720271a84277077f168f65Dan Egnor * distributed under the License is distributed on an "AS IS" BASIS,
123d40df335e4c0df972720271a84277077f168f65Dan Egnor * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133d40df335e4c0df972720271a84277077f168f65Dan Egnor * See the License for the specific language governing permissions and
143d40df335e4c0df972720271a84277077f168f65Dan Egnor * limitations under the License.
153d40df335e4c0df972720271a84277077f168f65Dan Egnor */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage com.android.server;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
193d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.content.BroadcastReceiver;
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Intent;
22492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnorimport android.content.SharedPreferences;
233d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.Build;
243d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.DropBoxManager;
25492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnorimport android.os.FileObserver;
263d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.FileUtils;
271af33d0ddc2f50ade146e4d48e2feb6f1d553427Doug Zongkerimport android.os.RecoverySystem;
283d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.SystemProperties;
29948eef82546c15086d0b78ce18ee874aa5c634f7Jeff Sharkeyimport android.provider.Downloads;
308a9b22056b13477f59df934928c00c58b5871c95Joe Onoratoimport android.util.Slog;
313d40df335e4c0df972720271a84277077f168f65Dan Egnor
323d40df335e4c0df972720271a84277077f168f65Dan Egnorimport java.io.File;
333d40df335e4c0df972720271a84277077f168f65Dan Egnorimport java.io.IOException;
343d40df335e4c0df972720271a84277077f168f65Dan Egnor
353d40df335e4c0df972720271a84277077f168f65Dan Egnor/**
363d40df335e4c0df972720271a84277077f168f65Dan Egnor * Performs a number of miscellaneous, non-system-critical actions
373d40df335e4c0df972720271a84277077f168f65Dan Egnor * after the system has finished booting.
383d40df335e4c0df972720271a84277077f168f65Dan Egnor */
393d40df335e4c0df972720271a84277077f168f65Dan Egnorpublic class BootReceiver extends BroadcastReceiver {
403d40df335e4c0df972720271a84277077f168f65Dan Egnor    private static final String TAG = "BootReceiver";
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
420132a952f75a88582f4437b2c97039b71456f828Dave Burke    // Maximum size of a logged event (files get truncated if they're longer).
430132a952f75a88582f4437b2c97039b71456f828Dave Burke    // Give userdebug builds a larger max to capture extra debug, esp. for last_kmsg.
440132a952f75a88582f4437b2c97039b71456f828Dave Burke    private static final int LOG_SIZE =
450132a952f75a88582f4437b2c97039b71456f828Dave Burke        SystemProperties.getInt("ro.debuggable", 0) == 1 ? 98304 : 65536;
4642471dd5552a346dd82a58a663159875ccc4fb79Dan Egnor
47492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor    private static final File TOMBSTONE_DIR = new File("/data/tombstones");
48492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor
49944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    // The pre-froyo package and class of the system updater, which
50944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    // ran in the system process.  We need to remove its packages here
51944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    // in order to clean up after a pre-froyo-to-froyo update.
52944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    private static final String OLD_UPDATER_PACKAGE =
53944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker        "com.google.android.systemupdater";
54944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    private static final String OLD_UPDATER_CLASS =
55944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker        "com.google.android.systemupdater.SystemUpdateReceiver";
56944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker
57492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor    // Keep a reference to the observer so the finalizer doesn't disable it.
58492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor    private static FileObserver sTombstoneObserver = null;
59492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Override
61c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor    public void onReceive(final Context context, Intent intent) {
62c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        // Log boot events in the background to avoid blocking the main thread with I/O
63c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        new Thread() {
64c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            @Override
65c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            public void run() {
66c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                try {
67c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                    logBootEvents(context);
68c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                } catch (Exception e) {
69c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                    Slog.e(TAG, "Can't log boot events", e);
70c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                }
71944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker                try {
72944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker                    removeOldUpdatePackages(context);
73944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker                } catch (Exception e) {
74944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker                    Slog.e(TAG, "Can't remove old update packages", e);
75944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker                }
76944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker
77c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            }
78c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        }.start();
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
81948eef82546c15086d0b78ce18ee874aa5c634f7Jeff Sharkey    private void removeOldUpdatePackages(Context context) {
82948eef82546c15086d0b78ce18ee874aa5c634f7Jeff Sharkey        Downloads.removeAllDownloadsByPackage(context, OLD_UPDATER_PACKAGE, OLD_UPDATER_CLASS);
83944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    }
84944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker
85492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor    private void logBootEvents(Context ctx) throws IOException {
86492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE);
87492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        final SharedPreferences prefs = ctx.getSharedPreferences("log_files", Context.MODE_PRIVATE);
88c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        final String headers = new StringBuilder(512)
89492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append("Build: ").append(Build.FINGERPRINT).append("\n")
90492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append("Hardware: ").append(Build.BOARD).append("\n")
918b2c916a8dc2370ce700a577d999f1a0fd848735Colin Cross            .append("Revision: ")
928b2c916a8dc2370ce700a577d999f1a0fd848735Colin Cross            .append(SystemProperties.get("ro.revision", "")).append("\n")
93492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append("Bootloader: ").append(Build.BOOTLOADER).append("\n")
94492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append("Radio: ").append(Build.RADIO).append("\n")
95492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append("Kernel: ")
96492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append(FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n"))
97c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            .append("\n").toString();
983d40df335e4c0df972720271a84277077f168f65Dan Egnor
99c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        String recovery = RecoverySystem.handleAftermath();
100c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        if (recovery != null && db != null) {
101c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            db.addText("SYSTEM_RECOVERY_LOG", headers + recovery);
102c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        }
1033d40df335e4c0df972720271a84277077f168f65Dan Egnor
1043d40df335e4c0df972720271a84277077f168f65Dan Egnor        if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) {
1053d40df335e4c0df972720271a84277077f168f65Dan Egnor            String now = Long.toString(System.currentTimeMillis());
1063d40df335e4c0df972720271a84277077f168f65Dan Egnor            SystemProperties.set("ro.runtime.firstboot", now);
107c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            if (db != null) db.addText("SYSTEM_BOOT", headers);
108289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor
109289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor            // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile())
110c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            addFileToDropBox(db, prefs, headers, "/proc/last_kmsg",
111289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor                    -LOG_SIZE, "SYSTEM_LAST_KMSG");
112c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            addFileToDropBox(db, prefs, headers, "/cache/recovery/log",
113289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor                    -LOG_SIZE, "SYSTEM_RECOVERY_LOG");
114c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_console",
115289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor                    -LOG_SIZE, "APANIC_CONSOLE");
116c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_threads",
117289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor                    -LOG_SIZE, "APANIC_THREADS");
1183d40df335e4c0df972720271a84277077f168f65Dan Egnor        } else {
119c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            if (db != null) db.addText("SYSTEM_RESTART", headers);
120492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        }
121492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor
122492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        // Scan existing tombstones (in case any new ones appeared)
123492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        File[] tombstoneFiles = TOMBSTONE_DIR.listFiles();
124492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) {
125c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            addFileToDropBox(db, prefs, headers, tombstoneFiles[i].getPath(),
126289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor                    LOG_SIZE, "SYSTEM_TOMBSTONE");
1273d40df335e4c0df972720271a84277077f168f65Dan Egnor        }
1283d40df335e4c0df972720271a84277077f168f65Dan Egnor
129492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        // Start watching for new tombstone files; will record them as they occur.
130492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        // This gets registered with the singleton file observer thread.
131492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) {
132492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            @Override
133492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            public void onEvent(int event, String path) {
134492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor                try {
135492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor                    String filename = new File(TOMBSTONE_DIR, path).getPath();
136c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                    addFileToDropBox(db, prefs, headers, filename, LOG_SIZE, "SYSTEM_TOMBSTONE");
137492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor                } catch (IOException e) {
1388a9b22056b13477f59df934928c00c58b5871c95Joe Onorato                    Slog.e(TAG, "Can't log tombstone", e);
139492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor                }
140492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            }
141492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        };
142492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor
143492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        sTombstoneObserver.startWatching();
1443d40df335e4c0df972720271a84277077f168f65Dan Egnor    }
1453d40df335e4c0df972720271a84277077f168f65Dan Egnor
146492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor    private static void addFileToDropBox(
147492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            DropBoxManager db, SharedPreferences prefs,
148289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor            String headers, String filename, int maxSize, String tag) throws IOException {
149c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        if (db == null || !db.isTagEnabled(tag)) return;  // Logging disabled
1503d40df335e4c0df972720271a84277077f168f65Dan Egnor
1513d40df335e4c0df972720271a84277077f168f65Dan Egnor        File file = new File(filename);
1523d40df335e4c0df972720271a84277077f168f65Dan Egnor        long fileTime = file.lastModified();
1533d40df335e4c0df972720271a84277077f168f65Dan Egnor        if (fileTime <= 0) return;  // File does not exist
1543d40df335e4c0df972720271a84277077f168f65Dan Egnor
155c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        if (prefs != null) {
156c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            long lastTime = prefs.getLong(filename, 0);
157c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            if (lastTime == fileTime) return;  // Already logged this particular file
158333b8cba996c8ebb8ca55ebfc5cc536bdd64af94Brad Fitzpatrick            // TODO: move all these SharedPreferences Editor commits
159333b8cba996c8ebb8ca55ebfc5cc536bdd64af94Brad Fitzpatrick            // outside this function to the end of logBootEvents
16066fce5068a8a3aeb28aaf713843891b286a75280Brad Fitzpatrick            prefs.edit().putLong(filename, fileTime).apply();
161c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        }
16242471dd5552a346dd82a58a663159875ccc4fb79Dan Egnor
163c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        Slog.i(TAG, "Copying " + filename + " to DropBox (" + tag + ")");
164c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        db.addText(tag, headers + FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n"));
1653d40df335e4c0df972720271a84277077f168f65Dan Egnor    }
1663d40df335e4c0df972720271a84277077f168f65Dan Egnor}
167