The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | */ |
| 16 | |
| 17 | #include <dirent.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <limits.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/klog.h> |
| 25 | #include <sys/reboot.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <time.h> |
| 29 | |
| 30 | #include "private/android_filesystem_config.h" |
| 31 | |
| 32 | // Sentinel file used to track whether we've forced a reboot |
| 33 | static const char *kMarkerFile = "/data/misc/check-lost+found-rebooted-2"; |
| 34 | |
| 35 | // Output file in tombstones directory (first 8K will be uploaded) |
| 36 | static const char *kOutputDir = "/data/tombstones"; |
| 37 | static const char *kOutputFile = "/data/tombstones/check-lost+found-log"; |
| 38 | |
| 39 | // Partitions to check |
| 40 | static const char *kPartitions[] = { "/system", "/data", "/cache", NULL }; |
| 41 | |
| 42 | /* |
| 43 | * 1. If /data/misc/forced-reboot is missing, touch it & force "unclean" boot. |
| 44 | * 2. Write a log entry with the number of files in lost+found directories. |
| 45 | */ |
| 46 | |
Edwin Vane | 4f66d55 | 2012-07-27 12:00:48 -0400 | [diff] [blame] | 47 | int main(int argc __attribute__((unused)), char **argv __attribute__((unused))) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 48 | mkdir(kOutputDir, 0755); |
| 49 | chown(kOutputDir, AID_SYSTEM, AID_SYSTEM); |
| 50 | FILE *out = fopen(kOutputFile, "a"); |
| 51 | if (out == NULL) { |
| 52 | fprintf(stderr, "Can't write %s: %s\n", kOutputFile, strerror(errno)); |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | // Note: only the first 8K of log will be uploaded, so be terse. |
| 57 | time_t start = time(NULL); |
| 58 | fprintf(out, "*** check-lost+found ***\nStarted: %s", ctime(&start)); |
| 59 | |
| 60 | struct stat st; |
| 61 | if (stat(kMarkerFile, &st)) { |
| 62 | // No reboot marker -- need to force an unclean reboot. |
| 63 | // But first, try to create the marker file. If that fails, |
| 64 | // skip the reboot, so we don't get caught in an infinite loop. |
| 65 | |
| 66 | int fd = open(kMarkerFile, O_WRONLY|O_CREAT, 0444); |
| 67 | if (fd >= 0 && close(fd) == 0) { |
| 68 | fprintf(out, "Wrote %s, rebooting\n", kMarkerFile); |
| 69 | fflush(out); |
| 70 | sync(); // Make sure the marker file is committed to disk |
| 71 | |
| 72 | // If possible, dirty each of these partitions before rebooting, |
| 73 | // to make sure the filesystem has to do a scan on mount. |
| 74 | int i; |
| 75 | for (i = 0; kPartitions[i] != NULL; ++i) { |
| 76 | char fn[PATH_MAX]; |
| 77 | snprintf(fn, sizeof(fn), "%s/%s", kPartitions[i], "dirty"); |
| 78 | fd = open(fn, O_WRONLY|O_CREAT, 0444); |
| 79 | if (fd >= 0) { // Don't sweat it if we can't write the file. |
| 80 | write(fd, fn, sizeof(fn)); // write, you know, some data |
| 81 | close(fd); |
| 82 | unlink(fn); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | reboot(RB_AUTOBOOT); // reboot immediately, with dirty filesystems |
| 87 | fprintf(out, "Reboot failed?!\n"); |
| 88 | exit(1); |
| 89 | } else { |
| 90 | fprintf(out, "Can't write %s: %s\n", kMarkerFile, strerror(errno)); |
| 91 | } |
| 92 | } else { |
| 93 | fprintf(out, "Found %s\n", kMarkerFile); |
| 94 | } |
| 95 | |
| 96 | int i; |
| 97 | for (i = 0; kPartitions[i] != NULL; ++i) { |
| 98 | char fn[PATH_MAX]; |
| 99 | snprintf(fn, sizeof(fn), "%s/%s", kPartitions[i], "lost+found"); |
| 100 | DIR *dir = opendir(fn); |
| 101 | if (dir == NULL) { |
| 102 | fprintf(out, "Can't open %s: %s\n", fn, strerror(errno)); |
| 103 | } else { |
| 104 | int count = 0; |
| 105 | struct dirent *ent; |
| 106 | while ((ent = readdir(dir))) { |
| 107 | if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..")) |
| 108 | ++count; |
| 109 | } |
| 110 | closedir(dir); |
| 111 | if (count > 0) { |
| 112 | fprintf(out, "OMGZ FOUND %d FILES IN %s\n", count, fn); |
| 113 | } else { |
| 114 | fprintf(out, "%s is clean\n", fn); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | char dmesg[131073]; |
| 120 | int len = klogctl(KLOG_READ_ALL, dmesg, sizeof(dmesg) - 1); |
| 121 | if (len < 0) { |
| 122 | fprintf(out, "Can't read kernel log: %s\n", strerror(errno)); |
| 123 | } else { // To conserve space, only write lines with certain keywords |
| 124 | fprintf(out, "--- Kernel log ---\n"); |
| 125 | dmesg[len] = '\0'; |
| 126 | char *saveptr, *line; |
| 127 | int in_yaffs = 0; |
| 128 | for (line = strtok_r(dmesg, "\n", &saveptr); line != NULL; |
| 129 | line = strtok_r(NULL, "\n", &saveptr)) { |
| 130 | if (strstr(line, "yaffs: dev is")) in_yaffs = 1; |
| 131 | |
| 132 | if (in_yaffs || |
| 133 | strstr(line, "yaffs") || |
| 134 | strstr(line, "mtd") || |
| 135 | strstr(line, "msm_nand")) { |
| 136 | fprintf(out, "%s\n", line); |
| 137 | } |
| 138 | |
| 139 | if (strstr(line, "yaffs_read_super: isCheckpointed")) in_yaffs = 0; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |