blob: 8587e9a66b4a3aa9241eb499dc5542b5d56b5d19 [file] [log] [blame]
Mark Salyzyna4f701a2016-03-09 14:58:16 -08001/*
2 * Copyright (C) 2016 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#define LOG_TAG "recovery-persist"
18
19//
20// Strictly to deal with reboot into system after OTA after /data
21// mounts to pull the last pmsg file data and place it
22// into /data/misc/recovery/ directory, rotating it in.
23//
24// Usage: recovery-persist [--force-persist]
25//
26// On systems without /cache mount, all file content representing in the
27// recovery/ directory stored in /sys/fs/pstore/pmsg-ramoops-0 in logger
28// format that reside in the LOG_ID_SYSTEM buffer at ANDROID_LOG_INFO
29// priority or higher is transfered to the /data/misc/recovery/ directory.
30// The content is matched and rotated in as need be.
31//
32// --force-persist ignore /cache mount, always rotate in the contents.
33//
34
35#include <errno.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
41#include <string>
42
43#include <android/log.h> /* Android Log Priority Tags */
44#include <android-base/file.h>
45#include <log/log.h>
46#include <log/logger.h> /* Android Log packet format */
47#include <private/android_logger.h> /* private pmsg functions */
48
49static const char *LAST_LOG_FILE = "/data/misc/recovery/last_log";
50static const char *LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0";
51static const char *LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg";
52static const char *LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0";
53
54static const int KEEP_LOG_COUNT = 10;
55
56// close a file, log an error if the error indicator is set
57static void check_and_fclose(FILE *fp, const char *name) {
58 fflush(fp);
59 if (ferror(fp)) SLOGE("%s %s", name, strerror(errno));
60 fclose(fp);
61}
62
63static void copy_file(const char* source, const char* destination) {
64 FILE* dest_fp = fopen(destination, "w");
65 if (dest_fp == nullptr) {
66 SLOGE("%s %s", destination, strerror(errno));
67 } else {
68 FILE* source_fp = fopen(source, "r");
69 if (source_fp != nullptr) {
70 char buf[4096];
71 size_t bytes;
72 while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) {
73 fwrite(buf, 1, bytes, dest_fp);
74 }
75 check_and_fclose(source_fp, source);
76 }
77 check_and_fclose(dest_fp, destination);
78 }
79}
80
81static bool rotated = false;
82
83// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
84// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.
85// Overwrite any existing last_log.$max and last_kmsg.$max.
86static void rotate_logs(int max) {
87 // Logs should only be rotated once.
88
89 if (rotated) {
90 return;
91 }
92 rotated = true;
93
94 for (int i = max-1; i >= 0; --i) {
95 std::string old_log(LAST_LOG_FILE);
96 if (i > 0) {
97 old_log += "." + std::to_string(i);
98 }
99 std::string new_log(LAST_LOG_FILE);
100 new_log += "." + std::to_string(i+1);
101
102 // Ignore errors if old_log doesn't exist.
103 rename(old_log.c_str(), new_log.c_str());
104
105 std::string old_kmsg(LAST_KMSG_FILE);
106 if (i > 0) {
107 old_kmsg += "." + std::to_string(i);
108 }
109 std::string new_kmsg(LAST_KMSG_FILE);
110 new_kmsg += "." + std::to_string(i+1);
111
112 rename(old_kmsg.c_str(), new_kmsg.c_str());
113 }
114}
115
116ssize_t logsave(
117 log_id_t /* logId */,
118 char /* prio */,
119 const char *filename,
120 const char *buf, size_t len,
121 void * /* arg */) {
122
123 std::string destination("/data/misc/");
124 destination += filename;
125
126 std::string buffer(buf, len);
127
128 {
129 std::string content;
130 android::base::ReadFileToString(destination, &content);
131
132 if (buffer.compare(content) == 0) {
133 return len;
134 }
135 }
136
137 // ToDo: Any others that match? Are we pulling in multiple
138 // already-rotated files? Algorithm thus far is KISS: one file,
139 // one rotation allowed.
140
141 rotate_logs(KEEP_LOG_COUNT);
142
143 return android::base::WriteStringToFile(buffer, destination.c_str());
144}
145
146int main(int argc, char **argv) {
147
148 /* Is /cache a mount?, we have been delivered where we are not wanted */
149 /*
150 * Following code halves the size of the executable as compared to:
151 *
152 * load_volume_table();
153 * has_cache = volume_for_path(CACHE_ROOT) != nullptr;
154 */
155 bool has_cache = false;
156 static const char mounts_file[] = "/proc/mounts";
157 FILE *fp = fopen(mounts_file, "r");
158 if (!fp) {
159 SLOGV("%s %s", mounts_file, strerror(errno));
160 } else {
161 char *line = NULL;
162 size_t len = 0;
163 ssize_t read;
164 while ((read = getline(&line, &len, fp)) != -1) {
165 if (strstr(line, " /cache ")) {
166 has_cache = true;
167 break;
168 }
169 }
170 free(line);
171 fclose(fp);
172 }
173
174 if (has_cache) {
175 /*
176 * TBD: Future location to move content from
177 * /cache/recovery to /data/misc/recovery/
178 */
179 /* if --force-persist flag, then transfer pmsg data anyways */
180 if ((argc <= 1) || !argv[1] || strcmp(argv[1], "--force-persist")) {
181 return 0;
182 }
183 }
184
185 /* Is there something in pmsg? */
186 if (access(LAST_PMSG_FILE, R_OK)) {
187 return 0;
188 }
189
190 // Take last pmsg file contents and send it off to the logsave
191 __android_log_pmsg_file_read(
192 LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", logsave, NULL);
193
194 /* Is there a last console log too? */
195 if (rotated && !access(LAST_CONSOLE_FILE, R_OK)) {
196 copy_file(LAST_CONSOLE_FILE, LAST_KMSG_FILE);
197 }
198
199 return 0;
200}