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