blob: b0ec141cb4ec29cab7b218669aef9b12888b74c9 [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
Mark Salyzyna4f701a2016-03-09 14:58:16 -080017//
18// Strictly to deal with reboot into system after OTA after /data
19// mounts to pull the last pmsg file data and place it
20// into /data/misc/recovery/ directory, rotating it in.
21//
22// Usage: recovery-persist [--force-persist]
23//
24// On systems without /cache mount, all file content representing in the
25// recovery/ directory stored in /sys/fs/pstore/pmsg-ramoops-0 in logger
26// format that reside in the LOG_ID_SYSTEM buffer at ANDROID_LOG_INFO
27// priority or higher is transfered to the /data/misc/recovery/ directory.
28// The content is matched and rotated in as need be.
29//
30// --force-persist ignore /cache mount, always rotate in the contents.
31//
32
33#include <errno.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38
39#include <string>
40
Mark Salyzyna4f701a2016-03-09 14:58:16 -080041#include <android-base/file.h>
Tianjie Xuc21edd42016-08-05 18:00:04 -070042#include <android-base/logging.h>
43
Mark Salyzyna4f701a2016-03-09 14:58:16 -080044#include <private/android_logger.h> /* private pmsg functions */
45
46static const char *LAST_LOG_FILE = "/data/misc/recovery/last_log";
47static const char *LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0";
48static const char *LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg";
49static const char *LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0";
Mark Salyzyn5f7111f2016-04-04 16:19:26 -070050static const char *ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops";
Mark Salyzyna4f701a2016-03-09 14:58:16 -080051
52static const int KEEP_LOG_COUNT = 10;
53
54// close a file, log an error if the error indicator is set
55static void check_and_fclose(FILE *fp, const char *name) {
56 fflush(fp);
Tianjie Xuc21edd42016-08-05 18:00:04 -070057 if (ferror(fp)) {
58 PLOG(ERROR) << "Error in " << name;
59 }
Mark Salyzyna4f701a2016-03-09 14:58:16 -080060 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) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070066 PLOG(ERROR) << "Can't open " << destination;
Mark Salyzyna4f701a2016-03-09 14:58:16 -080067 } 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) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700159 PLOG(ERROR) << "failed to open " << mounts_file;
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800160 } 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? */
Mark Salyzyn5f7111f2016-04-04 16:19:26 -0700195 if (rotated) {
196 if (!access(LAST_CONSOLE_FILE, R_OK)) {
197 copy_file(LAST_CONSOLE_FILE, LAST_KMSG_FILE);
198 } else if (!access(ALT_LAST_CONSOLE_FILE, R_OK)) {
199 copy_file(ALT_LAST_CONSOLE_FILE, LAST_KMSG_FILE);
200 }
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800201 }
202
203 return 0;
204}