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();
1093d40df335e4c0df972720271a84277077f168f65Dan Egnor
110c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        String recovery = RecoverySystem.handleAftermath();
111c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        if (recovery != null && db != null) {
112c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            db.addText("SYSTEM_RECOVERY_LOG", headers + recovery);
113c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        }
1143d40df335e4c0df972720271a84277077f168f65Dan Egnor
1153d40df335e4c0df972720271a84277077f168f65Dan Egnor        if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) {
1163d40df335e4c0df972720271a84277077f168f65Dan Egnor            String now = Long.toString(System.currentTimeMillis());
1173d40df335e4c0df972720271a84277077f168f65Dan Egnor            SystemProperties.set("ro.runtime.firstboot", now);
118c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            if (db != null) db.addText("SYSTEM_BOOT", headers);
119289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor
120289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor            // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile())
121c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            addFileToDropBox(db, prefs, headers, "/proc/last_kmsg",
122289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor                    -LOG_SIZE, "SYSTEM_LAST_KMSG");
123c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            addFileToDropBox(db, prefs, headers, "/cache/recovery/log",
124289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor                    -LOG_SIZE, "SYSTEM_RECOVERY_LOG");
125c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_console",
126289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor                    -LOG_SIZE, "APANIC_CONSOLE");
127c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_threads",
128289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor                    -LOG_SIZE, "APANIC_THREADS");
12951611eb72897645eba2140d5de0795c1830b90daGeremy Condra            addAuditErrorsToDropBox(db, prefs, headers, -LOG_SIZE, "SYSTEM_AUDIT");
130fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall            addFsckErrorsToDropBox(db, prefs, headers, -LOG_SIZE, "SYSTEM_FSCK");
1313d40df335e4c0df972720271a84277077f168f65Dan Egnor        } else {
132c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            if (db != null) db.addText("SYSTEM_RESTART", headers);
133492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        }
134492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor
135492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        // Scan existing tombstones (in case any new ones appeared)
136492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        File[] tombstoneFiles = TOMBSTONE_DIR.listFiles();
137492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) {
138c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            addFileToDropBox(db, prefs, headers, tombstoneFiles[i].getPath(),
139289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor                    LOG_SIZE, "SYSTEM_TOMBSTONE");
1403d40df335e4c0df972720271a84277077f168f65Dan Egnor        }
1413d40df335e4c0df972720271a84277077f168f65Dan Egnor
142492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        // Start watching for new tombstone files; will record them as they occur.
143492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        // This gets registered with the singleton file observer thread.
144492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) {
145492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            @Override
146492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            public void onEvent(int event, String path) {
147492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor                try {
148492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor                    String filename = new File(TOMBSTONE_DIR, path).getPath();
149c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor                    addFileToDropBox(db, prefs, headers, filename, LOG_SIZE, "SYSTEM_TOMBSTONE");
150492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor                } catch (IOException e) {
1518a9b22056b13477f59df934928c00c58b5871c95Joe Onorato                    Slog.e(TAG, "Can't log tombstone", e);
152492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor                }
153492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            }
154492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        };
155492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor
156492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor        sTombstoneObserver.startWatching();
1573d40df335e4c0df972720271a84277077f168f65Dan Egnor    }
1583d40df335e4c0df972720271a84277077f168f65Dan Egnor
159492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor    private static void addFileToDropBox(
160492c6ed5b64cfdd72dc270e6b848025a26eff724Dan Egnor            DropBoxManager db, SharedPreferences prefs,
161289e58051dd575cee601c38d6816b9ecd745b505Dan Egnor            String headers, String filename, int maxSize, String tag) throws IOException {
162c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        if (db == null || !db.isTagEnabled(tag)) return;  // Logging disabled
1633d40df335e4c0df972720271a84277077f168f65Dan Egnor
1643d40df335e4c0df972720271a84277077f168f65Dan Egnor        File file = new File(filename);
1653d40df335e4c0df972720271a84277077f168f65Dan Egnor        long fileTime = file.lastModified();
1663d40df335e4c0df972720271a84277077f168f65Dan Egnor        if (fileTime <= 0) return;  // File does not exist
1673d40df335e4c0df972720271a84277077f168f65Dan Egnor
168c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        if (prefs != null) {
169c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            long lastTime = prefs.getLong(filename, 0);
170c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor            if (lastTime == fileTime) return;  // Already logged this particular file
171333b8cba996c8ebb8ca55ebfc5cc536bdd64af94Brad Fitzpatrick            // TODO: move all these SharedPreferences Editor commits
172333b8cba996c8ebb8ca55ebfc5cc536bdd64af94Brad Fitzpatrick            // outside this function to the end of logBootEvents
17366fce5068a8a3aeb28aaf713843891b286a75280Brad Fitzpatrick            prefs.edit().putLong(filename, fileTime).apply();
174c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        }
17542471dd5552a346dd82a58a663159875ccc4fb79Dan Egnor
176c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        Slog.i(TAG, "Copying " + filename + " to DropBox (" + tag + ")");
177c95142d4a0ab7bebb899167da17c70c3196abbe4Dan Egnor        db.addText(tag, headers + FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n"));
1783d40df335e4c0df972720271a84277077f168f65Dan Egnor    }
17951611eb72897645eba2140d5de0795c1830b90daGeremy Condra
18051611eb72897645eba2140d5de0795c1830b90daGeremy Condra    private static void addAuditErrorsToDropBox(DropBoxManager db,  SharedPreferences prefs,
18151611eb72897645eba2140d5de0795c1830b90daGeremy Condra            String headers, int maxSize, String tag) throws IOException {
18251611eb72897645eba2140d5de0795c1830b90daGeremy Condra        if (db == null || !db.isTagEnabled(tag)) return;  // Logging disabled
18351611eb72897645eba2140d5de0795c1830b90daGeremy Condra        Slog.i(TAG, "Copying audit failures to DropBox");
18451611eb72897645eba2140d5de0795c1830b90daGeremy Condra
18551611eb72897645eba2140d5de0795c1830b90daGeremy Condra        File file = new File("/proc/last_kmsg");
18651611eb72897645eba2140d5de0795c1830b90daGeremy Condra        long fileTime = file.lastModified();
18751611eb72897645eba2140d5de0795c1830b90daGeremy Condra        if (fileTime <= 0) return;  // File does not exist
18851611eb72897645eba2140d5de0795c1830b90daGeremy Condra
18951611eb72897645eba2140d5de0795c1830b90daGeremy Condra        if (prefs != null) {
19051611eb72897645eba2140d5de0795c1830b90daGeremy Condra            long lastTime = prefs.getLong(tag, 0);
19151611eb72897645eba2140d5de0795c1830b90daGeremy Condra            if (lastTime == fileTime) return;  // Already logged this particular file
19251611eb72897645eba2140d5de0795c1830b90daGeremy Condra            // TODO: move all these SharedPreferences Editor commits
19351611eb72897645eba2140d5de0795c1830b90daGeremy Condra            // outside this function to the end of logBootEvents
19451611eb72897645eba2140d5de0795c1830b90daGeremy Condra            prefs.edit().putLong(tag, fileTime).apply();
19551611eb72897645eba2140d5de0795c1830b90daGeremy Condra        }
19651611eb72897645eba2140d5de0795c1830b90daGeremy Condra
19751611eb72897645eba2140d5de0795c1830b90daGeremy Condra        String log = FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n");
19851611eb72897645eba2140d5de0795c1830b90daGeremy Condra        StringBuilder sb = new StringBuilder();
19951611eb72897645eba2140d5de0795c1830b90daGeremy Condra        for (String line : log.split("\n")) {
20051611eb72897645eba2140d5de0795c1830b90daGeremy Condra            if (line.contains("audit")) {
20151611eb72897645eba2140d5de0795c1830b90daGeremy Condra                sb.append(line + "\n");
20251611eb72897645eba2140d5de0795c1830b90daGeremy Condra            }
20351611eb72897645eba2140d5de0795c1830b90daGeremy Condra        }
20451611eb72897645eba2140d5de0795c1830b90daGeremy Condra        Slog.i(TAG, "Copied " + sb.toString().length() + " worth of audits to DropBox");
20551611eb72897645eba2140d5de0795c1830b90daGeremy Condra        db.addText(tag, headers + sb.toString());
20651611eb72897645eba2140d5de0795c1830b90daGeremy Condra    }
207fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall
208fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall    private static void addFsckErrorsToDropBox(DropBoxManager db,  SharedPreferences prefs,
209fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall            String headers, int maxSize, String tag) throws IOException {
210fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        boolean upload_needed = false;
211fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        if (db == null || !db.isTagEnabled(tag)) return;  // Logging disabled
212fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        Slog.i(TAG, "Checking for fsck errors");
213fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall
214fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        File file = new File("/dev/fscklogs/log");
215fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        long fileTime = file.lastModified();
216fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        if (fileTime <= 0) return;  // File does not exist
217fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall
218fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        String log = FileUtils.readTextFile(file, maxSize, "[[TRUNCATED]]\n");
219fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        StringBuilder sb = new StringBuilder();
220fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        for (String line : log.split("\n")) {
221fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall            if (line.contains("FILE SYSTEM WAS MODIFIED")) {
222fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall                upload_needed = true;
223fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall                break;
224fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall            }
225fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        }
226fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall
227fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        if (upload_needed) {
228fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall            addFileToDropBox(db, prefs, headers, "/dev/fscklogs/log", maxSize, tag);
229fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        }
230fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall
231fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        // Remove the file so we don't re-upload if the runtime restarts.
232fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall        file.delete();
233fefefbb17542a48f76aaabac42b376967bbcd3edKen Sumrall    }
2343d40df335e4c0df972720271a84277077f168f65Dan Egnor}
235