blob: 25df03f4732da5826cd2a34f012e84507cb44a29 [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";
Mark Salyzyn5f7111f2016-04-04 16:19:26 -070053static const char *ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops";
Mark Salyzyna4f701a2016-03-09 14:58:16 -080054
55static const int KEEP_LOG_COUNT = 10;
56
57// close a file, log an error if the error indicator is set
58static void check_and_fclose(FILE *fp, const char *name) {
59 fflush(fp);
60 if (ferror(fp)) SLOGE("%s %s", name, strerror(errno));
61 fclose(fp);
62}
63
64static void copy_file(const char* source, const char* destination) {
65 FILE* dest_fp = fopen(destination, "w");
66 if (dest_fp == nullptr) {
67 SLOGE("%s %s", destination, strerror(errno));
68 } else {
69 FILE* source_fp = fopen(source, "r");
70 if (source_fp != nullptr) {
71 char buf[4096];
72 size_t bytes;
73 while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) {
74 fwrite(buf, 1, bytes, dest_fp);
75 }
76 check_and_fclose(source_fp, source);
77 }
78 check_and_fclose(dest_fp, destination);
79 }
80}
81
82static bool rotated = false;
83
84// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
85// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.
86// Overwrite any existing last_log.$max and last_kmsg.$max.
87static void rotate_logs(int max) {
88 // Logs should only be rotated once.
89
90 if (rotated) {
91 return;
92 }
93 rotated = true;
94
95 for (int i = max-1; i >= 0; --i) {
96 std::string old_log(LAST_LOG_FILE);
97 if (i > 0) {
98 old_log += "." + std::to_string(i);
99 }
100 std::string new_log(LAST_LOG_FILE);
101 new_log += "." + std::to_string(i+1);
102
103 // Ignore errors if old_log doesn't exist.
104 rename(old_log.c_str(), new_log.c_str());
105
106 std::string old_kmsg(LAST_KMSG_FILE);
107 if (i > 0) {
108 old_kmsg += "." + std::to_string(i);
109 }
110 std::string new_kmsg(LAST_KMSG_FILE);
111 new_kmsg += "." + std::to_string(i+1);
112
113 rename(old_kmsg.c_str(), new_kmsg.c_str());
114 }
115}
116
117ssize_t logsave(
118 log_id_t /* logId */,
119 char /* prio */,
120 const char *filename,
121 const char *buf, size_t len,
122 void * /* arg */) {
123
124 std::string destination("/data/misc/");
125 destination += filename;
126
127 std::string buffer(buf, len);
128
129 {
130 std::string content;
131 android::base::ReadFileToString(destination, &content);
132
133 if (buffer.compare(content) == 0) {
134 return len;
135 }
136 }
137
138 // ToDo: Any others that match? Are we pulling in multiple
139 // already-rotated files? Algorithm thus far is KISS: one file,
140 // one rotation allowed.
141
142 rotate_logs(KEEP_LOG_COUNT);
143
144 return android::base::WriteStringToFile(buffer, destination.c_str());
145}
146
147int main(int argc, char **argv) {
148
149 /* Is /cache a mount?, we have been delivered where we are not wanted */
150 /*
151 * Following code halves the size of the executable as compared to:
152 *
153 * load_volume_table();
154 * has_cache = volume_for_path(CACHE_ROOT) != nullptr;
155 */
156 bool has_cache = false;
157 static const char mounts_file[] = "/proc/mounts";
158 FILE *fp = fopen(mounts_file, "r");
159 if (!fp) {
160 SLOGV("%s %s", mounts_file, strerror(errno));
161 } else {
162 char *line = NULL;
163 size_t len = 0;
164 ssize_t read;
165 while ((read = getline(&line, &len, fp)) != -1) {
166 if (strstr(line, " /cache ")) {
167 has_cache = true;
168 break;
169 }
170 }
171 free(line);
172 fclose(fp);
173 }
174
175 if (has_cache) {
176 /*
177 * TBD: Future location to move content from
178 * /cache/recovery to /data/misc/recovery/
179 */
180 /* if --force-persist flag, then transfer pmsg data anyways */
181 if ((argc <= 1) || !argv[1] || strcmp(argv[1], "--force-persist")) {
182 return 0;
183 }
184 }
185
186 /* Is there something in pmsg? */
187 if (access(LAST_PMSG_FILE, R_OK)) {
188 return 0;
189 }
190
191 // Take last pmsg file contents and send it off to the logsave
192 __android_log_pmsg_file_read(
193 LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", logsave, NULL);
194
195 /* Is there a last console log too? */
Mark Salyzyn5f7111f2016-04-04 16:19:26 -0700196 if (rotated) {
197 if (!access(LAST_CONSOLE_FILE, R_OK)) {
198 copy_file(LAST_CONSOLE_FILE, LAST_KMSG_FILE);
199 } else if (!access(ALT_LAST_CONSOLE_FILE, R_OK)) {
200 copy_file(ALT_LAST_CONSOLE_FILE, LAST_KMSG_FILE);
201 }
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800202 }
203
204 return 0;
205}