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;
2313579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackbornimport android.content.pm.IPackageManager;
243d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.Build;
253d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.DropBoxManager;
26492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnorimport android.os.FileObserver;
273d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.FileUtils;
281af33d0ddc2f50ade146e4d48e2feb6f1d553427Doug Zongkerimport android.os.RecoverySystem;
2913579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackbornimport android.os.RemoteException;
3013579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackbornimport android.os.ServiceManager;
313d40df335e4c0df972720271a84277077f168f65Dan Egnorimport android.os.SystemProperties;
32948eef82546c15086d0b78ce18ee874aa5c634f7Jeff Sharkeyimport android.provider.Downloads;
338a9b22056b13477f59df934928c00c58b5871c95Joe Onoratoimport android.util.Slog;
343d40df335e4c0df972720271a84277077f168f65Dan Egnor
353d40df335e4c0df972720271a84277077f168f65Dan Egnorimport java.io.File;
363d40df335e4c0df972720271a84277077f168f65Dan Egnorimport java.io.IOException;
373d40df335e4c0df972720271a84277077f168f65Dan Egnor
383d40df335e4c0df972720271a84277077f168f65Dan Egnor/**
393d40df335e4c0df972720271a84277077f168f65Dan Egnor * Performs a number of miscellaneous, non-system-critical actions
403d40df335e4c0df972720271a84277077f168f65Dan Egnor * after the system has finished booting.
413d40df335e4c0df972720271a84277077f168f65Dan Egnor */
423d40df335e4c0df972720271a84277077f168f65Dan Egnorpublic class BootReceiver extends BroadcastReceiver {
433d40df335e4c0df972720271a84277077f168f65Dan Egnor    private static final String TAG = "BootReceiver";
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
450132a952f75a88582f4437b2c97039b71456f828Dave Burke    // Maximum size of a logged event (files get truncated if they're longer).
460132a952f75a88582f4437b2c97039b71456f828Dave Burke    // Give userdebug builds a larger max to capture extra debug, esp. for last_kmsg.
470132a952f75a88582f4437b2c97039b71456f828Dave Burke    private static final int LOG_SIZE =
480132a952f75a88582f4437b2c97039b71456f828Dave Burke        SystemProperties.getInt("ro.debuggable", 0) == 1 ? 98304 : 65536;
4942471dd5552a346dd82a58a663159875ccc4fb79Dan Egnor
50492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor    private static final File TOMBSTONE_DIR = new File("/data/tombstones");
51492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor
52944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    // The pre-froyo package and class of the system updater, which
53944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    // ran in the system process.  We need to remove its packages here
54944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    // in order to clean up after a pre-froyo-to-froyo update.
55944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    private static final String OLD_UPDATER_PACKAGE =
56944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker        "com.google.android.systemupdater";
57944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    private static final String OLD_UPDATER_CLASS =
58944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker        "com.google.android.systemupdater.SystemUpdateReceiver";
59944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker
60492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor    // Keep a reference to the observer so the finalizer doesn't disable it.
61492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor    private static FileObserver sTombstoneObserver = null;
62492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Override
64c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor    public void onReceive(final Context context, Intent intent) {
65c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        // Log boot events in the background to avoid blocking the main thread with I/O
66c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        new Thread() {
67c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            @Override
68c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            public void run() {
69c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                try {
70c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                    logBootEvents(context);
71c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                } catch (Exception e) {
72c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                    Slog.e(TAG, "Can't log boot events", e);
73c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                }
74944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker                try {
7513579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackborn                    boolean onlyCore = false;
7613579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackborn                    try {
7713579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackborn                        onlyCore = IPackageManager.Stub.asInterface(ServiceManager.getService(
7813579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackborn                                "package")).isOnlyCoreApps();
7913579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackborn                    } catch (RemoteException e) {
8013579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackborn                    }
8113579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackborn                    if (!onlyCore) {
8213579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackborn                        removeOldUpdatePackages(context);
8313579ed3305bf89b41a9fa88e1347f0e0769d279Dianne Hackborn                    }
84944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker                } catch (Exception e) {
85944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker                    Slog.e(TAG, "Can't remove old update packages", e);
86944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker                }
87944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker
88c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            }
89c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        }.start();
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
92948eef82546c15086d0b78ce18ee874aa5c634f7Jeff Sharkey    private void removeOldUpdatePackages(Context context) {
93948eef82546c15086d0b78ce18ee874aa5c634f7Jeff Sharkey        Downloads.removeAllDownloadsByPackage(context, OLD_UPDATER_PACKAGE, OLD_UPDATER_CLASS);
94944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker    }
95944ff0b788bf1702bad38d21cc2b83a9985dd112Doug Zongker
96492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor    private void logBootEvents(Context ctx) throws IOException {
97492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE);
98492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        final SharedPreferences prefs = ctx.getSharedPreferences("log_files", Context.MODE_PRIVATE);
99c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        final String headers = new StringBuilder(512)
100492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append("Build: ").append(Build.FINGERPRINT).append("\n")
101492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append("Hardware: ").append(Build.BOARD).append("\n")
1028b2c916a8dc2370ce700a577d999f1a0fd848735Colin Cross            .append("Revision: ")
1038b2c916a8dc2370ce700a577d999f1a0fd848735Colin Cross            .append(SystemProperties.get("ro.revision", "")).append("\n")
104492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append("Bootloader: ").append(Build.BOOTLOADER).append("\n")
105492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append("Radio: ").append(Build.RADIO).append("\n")
106492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append("Kernel: ")
107492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            .append(FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n"))
108c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            .append("\n").toString();
109431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross        final String bootReason = SystemProperties.get("ro.boot.bootreason", null);
1103d40df335e4c0df972720271a84277077f168f65Dan Egnor
111c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        String recovery = RecoverySystem.handleAftermath();
112c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        if (recovery != null && db != null) {
113c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            db.addText("SYSTEM_RECOVERY_LOG", headers + recovery);
114c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        }
1153d40df335e4c0df972720271a84277077f168f65Dan Egnor
116431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross        String lastKmsgFooter = "";
117431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross        if (bootReason != null) {
118431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross            lastKmsgFooter = new StringBuilder(512)
119431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross                .append("\n")
120431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross                .append("Boot info:\n")
121431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross                .append("Last boot reason: ").append(bootReason).append("\n")
122431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross                .toString();
123431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross        }
124431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross
1253d40df335e4c0df972720271a84277077f168f65Dan Egnor        if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) {
12691317107a39a4f2a9347105b96aafcb98fe21feePaul Lawrence            if ("encrypted".equals(SystemProperties.get("ro.crypto.state"))
12791317107a39a4f2a9347105b96aafcb98fe21feePaul Lawrence                && "trigger_restart_min_framework".equals(SystemProperties.get("vold.decrypt"))){
12891317107a39a4f2a9347105b96aafcb98fe21feePaul Lawrence                // Encrypted, first boot to get PIN/pattern/password so data is tmpfs
12991317107a39a4f2a9347105b96aafcb98fe21feePaul Lawrence                // Don't set ro.runtime.firstboot so that we will do this again
13091317107a39a4f2a9347105b96aafcb98fe21feePaul Lawrence                // when data is properly mounted
13191317107a39a4f2a9347105b96aafcb98fe21feePaul Lawrence            } else {
13291317107a39a4f2a9347105b96aafcb98fe21feePaul Lawrence                String now = Long.toString(System.currentTimeMillis());
13391317107a39a4f2a9347105b96aafcb98fe21feePaul Lawrence                SystemProperties.set("ro.runtime.firstboot", now);
13491317107a39a4f2a9347105b96aafcb98fe21feePaul Lawrence            }
135c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            if (db != null) db.addText("SYSTEM_BOOT", headers);
136289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor
137289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor            // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile())
138431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross            addFileWithFootersToDropBox(db, prefs, headers, lastKmsgFooter,
139431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross                    "/proc/last_kmsg", -LOG_SIZE, "SYSTEM_LAST_KMSG");
140431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross            addFileWithFootersToDropBox(db, prefs, headers, lastKmsgFooter,
141431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross                    "/sys/fs/pstore/console-ramoops", -LOG_SIZE,
142431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross                    "SYSTEM_LAST_KMSG");
143c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            addFileToDropBox(db, prefs, headers, "/cache/recovery/log",
144289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor                    -LOG_SIZE, "SYSTEM_RECOVERY_LOG");
145e37030505ee3d461508611e1a291aff7b9e82a43Patrick Tjin            addFileToDropBox(db, prefs, headers, "/cache/recovery/last_kmsg",
146e37030505ee3d461508611e1a291aff7b9e82a43Patrick Tjin                    -LOG_SIZE, "SYSTEM_RECOVERY_KMSG");
14751611eb72897645eba2140d5de0795c1830b90daGeremy Condra            addAuditErrorsToDropBox(db, prefs, headers, -LOG_SIZE, "SYSTEM_AUDIT");
148fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall            addFsckErrorsToDropBox(db, prefs, headers, -LOG_SIZE, "SYSTEM_FSCK");
1493d40df335e4c0df972720271a84277077f168f65Dan Egnor        } else {
150c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            if (db != null) db.addText("SYSTEM_RESTART", headers);
151492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        }
152492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor
153492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        // Scan existing tombstones (in case any new ones appeared)
154492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        File[] tombstoneFiles = TOMBSTONE_DIR.listFiles();
155492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) {
156dea847c2ace8894a595cf9bd87764ce2ca9698abPatrick Tjin            if (tombstoneFiles[i].isFile()) {
157dea847c2ace8894a595cf9bd87764ce2ca9698abPatrick Tjin                addFileToDropBox(db, prefs, headers, tombstoneFiles[i].getPath(),
158dea847c2ace8894a595cf9bd87764ce2ca9698abPatrick Tjin                        LOG_SIZE, "SYSTEM_TOMBSTONE");
159dea847c2ace8894a595cf9bd87764ce2ca9698abPatrick Tjin            }
1603d40df335e4c0df972720271a84277077f168f65Dan Egnor        }
1613d40df335e4c0df972720271a84277077f168f65Dan Egnor
162492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        // Start watching for new tombstone files; will record them as they occur.
163492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        // This gets registered with the singleton file observer thread.
164492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) {
165492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            @Override
166492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            public void onEvent(int event, String path) {
167492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor                try {
168dea847c2ace8894a595cf9bd87764ce2ca9698abPatrick Tjin                    File file = new File(TOMBSTONE_DIR, path);
169dea847c2ace8894a595cf9bd87764ce2ca9698abPatrick Tjin                    if (file.isFile()) {
170dea847c2ace8894a595cf9bd87764ce2ca9698abPatrick Tjin                        addFileToDropBox(db, prefs, headers, file.getPath(), LOG_SIZE, "SYSTEM_TOMBSTONE");
171dea847c2ace8894a595cf9bd87764ce2ca9698abPatrick Tjin                    }
172492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor                } catch (IOException e) {
1738a9b22056b13477f59df934928c00c58b5871c95Joe Onorato                    Slog.e(TAG, "Can't log tombstone", e);
174492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor                }
175492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            }
176492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        };
177492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor
178492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        sTombstoneObserver.startWatching();
1793d40df335e4c0df972720271a84277077f168f65Dan Egnor    }
1803d40df335e4c0df972720271a84277077f168f65Dan Egnor
181492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor    private static void addFileToDropBox(
182492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            DropBoxManager db, SharedPreferences prefs,
183289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor            String headers, String filename, int maxSize, String tag) throws IOException {
184431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross        addFileWithFootersToDropBox(db, prefs, headers, "", filename, maxSize,
185431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross                tag);
186431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross    }
187431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross
188431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross    private static void addFileWithFootersToDropBox(
189431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross            DropBoxManager db, SharedPreferences prefs,
190431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross            String headers, String footers, String filename, int maxSize,
191431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross            String tag) throws IOException {
192c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        if (db == null || !db.isTagEnabled(tag)) return;  // Logging disabled
1933d40df335e4c0df972720271a84277077f168f65Dan Egnor
1943d40df335e4c0df972720271a84277077f168f65Dan Egnor        File file = new File(filename);
1953d40df335e4c0df972720271a84277077f168f65Dan Egnor        long fileTime = file.lastModified();
1963d40df335e4c0df972720271a84277077f168f65Dan Egnor        if (fileTime <= 0) return;  // File does not exist
1973d40df335e4c0df972720271a84277077f168f65Dan Egnor
198c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        if (prefs != null) {
199c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            long lastTime = prefs.getLong(filename, 0);
200c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            if (lastTime == fileTime) return;  // Already logged this particular file
201333b8cba996c8ebb8ca55ebfc5cc536bdd64af94Brad Fitzpatrick            // TODO: move all these SharedPreferences Editor commits
202333b8cba996c8ebb8ca55ebfc5cc536bdd64af94Brad Fitzpatrick            // outside this function to the end of logBootEvents
20366fce5068a8a3aeb28aaf713843891b286a75280Brad Fitzpatrick            prefs.edit().putLong(filename, fileTime).apply();
204c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        }
20542471dd5552a346dd82a58a663159875ccc4fb79Dan Egnor
206c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        Slog.i(TAG, "Copying " + filename + " to DropBox (" + tag + ")");
207431614cb6b2170fc715757dc6f964fdbb140ee7dColin Cross        db.addText(tag, headers + FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n") + footers);
2083d40df335e4c0df972720271a84277077f168f65Dan Egnor    }
20951611eb72897645eba2140d5de0795c1830b90daGeremy Condra
21051611eb72897645eba2140d5de0795c1830b90daGeremy Condra    private static void addAuditErrorsToDropBox(DropBoxManager db,  SharedPreferences prefs,
21151611eb72897645eba2140d5de0795c1830b90daGeremy Condra            String headers, int maxSize, String tag) throws IOException {
21251611eb72897645eba2140d5de0795c1830b90daGeremy Condra        if (db == null || !db.isTagEnabled(tag)) return;  // Logging disabled
21351611eb72897645eba2140d5de0795c1830b90daGeremy Condra        Slog.i(TAG, "Copying audit failures to DropBox");
21451611eb72897645eba2140d5de0795c1830b90daGeremy Condra
21551611eb72897645eba2140d5de0795c1830b90daGeremy Condra        File file = new File("/proc/last_kmsg");
21651611eb72897645eba2140d5de0795c1830b90daGeremy Condra        long fileTime = file.lastModified();
217c95bb562e8c67c258623fe70e8001a08e59b8c7dTodd Poynor        if (fileTime <= 0) {
218c95bb562e8c67c258623fe70e8001a08e59b8c7dTodd Poynor            file = new File("/sys/fs/pstore/console-ramoops");
219c95bb562e8c67c258623fe70e8001a08e59b8c7dTodd Poynor            fileTime = file.lastModified();
220c95bb562e8c67c258623fe70e8001a08e59b8c7dTodd Poynor        }
221c95bb562e8c67c258623fe70e8001a08e59b8c7dTodd Poynor
22251611eb72897645eba2140d5de0795c1830b90daGeremy Condra        if (fileTime <= 0) return;  // File does not exist
22351611eb72897645eba2140d5de0795c1830b90daGeremy Condra
22451611eb72897645eba2140d5de0795c1830b90daGeremy Condra        if (prefs != null) {
22551611eb72897645eba2140d5de0795c1830b90daGeremy Condra            long lastTime = prefs.getLong(tag, 0);
22651611eb72897645eba2140d5de0795c1830b90daGeremy Condra            if (lastTime == fileTime) return;  // Already logged this particular file
22751611eb72897645eba2140d5de0795c1830b90daGeremy Condra            // TODO: move all these SharedPreferences Editor commits
22851611eb72897645eba2140d5de0795c1830b90daGeremy Condra            // outside this function to the end of logBootEvents
22951611eb72897645eba2140d5de0795c1830b90daGeremy Condra            prefs.edit().putLong(tag, fileTime).apply();
23051611eb72897645eba2140d5de0795c1830b90daGeremy Condra        }
23151611eb72897645eba2140d5de0795c1830b90daGeremy Condra
23251611eb72897645eba2140d5de0795c1830b90daGeremy Condra        String log = FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n");
23351611eb72897645eba2140d5de0795c1830b90daGeremy Condra        StringBuilder sb = new StringBuilder();
23451611eb72897645eba2140d5de0795c1830b90daGeremy Condra        for (String line : log.split("\n")) {
23551611eb72897645eba2140d5de0795c1830b90daGeremy Condra            if (line.contains("audit")) {
23651611eb72897645eba2140d5de0795c1830b90daGeremy Condra                sb.append(line + "\n");
23751611eb72897645eba2140d5de0795c1830b90daGeremy Condra            }
23851611eb72897645eba2140d5de0795c1830b90daGeremy Condra        }
23951611eb72897645eba2140d5de0795c1830b90daGeremy Condra        Slog.i(TAG, "Copied " + sb.toString().length() + " worth of audits to DropBox");
24051611eb72897645eba2140d5de0795c1830b90daGeremy Condra        db.addText(tag, headers + sb.toString());
24151611eb72897645eba2140d5de0795c1830b90daGeremy Condra    }
242fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall
243fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall    private static void addFsckErrorsToDropBox(DropBoxManager db,  SharedPreferences prefs,
244fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall            String headers, int maxSize, String tag) throws IOException {
245fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        boolean upload_needed = false;
246fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        if (db == null || !db.isTagEnabled(tag)) return;  // Logging disabled
247fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        Slog.i(TAG, "Checking for fsck errors");
248fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall
249fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        File file = new File("/dev/fscklogs/log");
250fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        long fileTime = file.lastModified();
251fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        if (fileTime <= 0) return;  // File does not exist
252fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall
253fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        String log = FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n");
254fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        StringBuilder sb = new StringBuilder();
255fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        for (String line : log.split("\n")) {
256fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall            if (line.contains("FILE SYSTEM WAS MODIFIED")) {
257fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall                upload_needed = true;
258fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall                break;
259fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall            }
260fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        }
261fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall
262fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        if (upload_needed) {
263fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall            addFileToDropBox(db, prefs, headers, "/dev/fscklogs/log", maxSize, tag);
264fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        }
265fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall
266fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        // Remove the file so we don't re-upload if the runtime restarts.
267fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        file.delete();
268fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall    }
2693d40df335e4c0df972720271a84277077f168f65Dan Egnor}
270