blob: d706ccac89963925aa91bdde2f6048acadfcbcd6 [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
Mark Salyzyna4f701a2016-03-09 14:58:16 -080033#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
38#include <string>
39
Mark Salyzyna4f701a2016-03-09 14:58:16 -080040#include <android-base/file.h>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070041#include <android-base/logging.h>
Mark Salyzyna4f701a2016-03-09 14:58:16 -080042#include <private/android_logger.h> /* private pmsg functions */
43
Tianjie Xue113e4d2016-10-21 17:46:13 -070044#include "rotate_logs.h"
45
Mark Salyzyna4f701a2016-03-09 14:58:16 -080046static 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
Mark Salyzyna4f701a2016-03-09 14:58:16 -080052// close a file, log an error if the error indicator is set
53static void check_and_fclose(FILE *fp, const char *name) {
54 fflush(fp);
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070055 if (ferror(fp)) {
56 PLOG(ERROR) << "Error in " << name;
57 }
Mark Salyzyna4f701a2016-03-09 14:58:16 -080058 fclose(fp);
59}
60
61static void copy_file(const char* source, const char* destination) {
62 FILE* dest_fp = fopen(destination, "w");
63 if (dest_fp == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070064 PLOG(ERROR) << "Can't open " << destination;
Mark Salyzyna4f701a2016-03-09 14:58:16 -080065 } else {
66 FILE* source_fp = fopen(source, "r");
67 if (source_fp != nullptr) {
68 char buf[4096];
69 size_t bytes;
70 while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) {
71 fwrite(buf, 1, bytes, dest_fp);
72 }
73 check_and_fclose(source_fp, source);
74 }
75 check_and_fclose(dest_fp, destination);
76 }
77}
78
79static bool rotated = false;
80
Mark Salyzyna4f701a2016-03-09 14:58:16 -080081ssize_t logsave(
82 log_id_t /* logId */,
83 char /* prio */,
84 const char *filename,
85 const char *buf, size_t len,
86 void * /* arg */) {
87
88 std::string destination("/data/misc/");
89 destination += filename;
90
91 std::string buffer(buf, len);
92
93 {
94 std::string content;
95 android::base::ReadFileToString(destination, &content);
96
97 if (buffer.compare(content) == 0) {
98 return len;
99 }
100 }
101
102 // ToDo: Any others that match? Are we pulling in multiple
103 // already-rotated files? Algorithm thus far is KISS: one file,
104 // one rotation allowed.
105
Tianjie Xue113e4d2016-10-21 17:46:13 -0700106 rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE);
107 rotated = true;
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800108
109 return android::base::WriteStringToFile(buffer, destination.c_str());
110}
111
112int main(int argc, char **argv) {
113
114 /* Is /cache a mount?, we have been delivered where we are not wanted */
115 /*
116 * Following code halves the size of the executable as compared to:
117 *
118 * load_volume_table();
119 * has_cache = volume_for_path(CACHE_ROOT) != nullptr;
120 */
121 bool has_cache = false;
122 static const char mounts_file[] = "/proc/mounts";
123 FILE *fp = fopen(mounts_file, "r");
124 if (!fp) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700125 PLOG(ERROR) << "failed to open " << mounts_file;
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800126 } else {
127 char *line = NULL;
128 size_t len = 0;
129 ssize_t read;
130 while ((read = getline(&line, &len, fp)) != -1) {
131 if (strstr(line, " /cache ")) {
132 has_cache = true;
133 break;
134 }
135 }
136 free(line);
137 fclose(fp);
138 }
139
140 if (has_cache) {
141 /*
142 * TBD: Future location to move content from
143 * /cache/recovery to /data/misc/recovery/
144 */
145 /* if --force-persist flag, then transfer pmsg data anyways */
146 if ((argc <= 1) || !argv[1] || strcmp(argv[1], "--force-persist")) {
147 return 0;
148 }
149 }
150
151 /* Is there something in pmsg? */
152 if (access(LAST_PMSG_FILE, R_OK)) {
153 return 0;
154 }
155
156 // Take last pmsg file contents and send it off to the logsave
157 __android_log_pmsg_file_read(
158 LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", logsave, NULL);
159
160 /* Is there a last console log too? */
Mark Salyzyn5f7111f2016-04-04 16:19:26 -0700161 if (rotated) {
162 if (!access(LAST_CONSOLE_FILE, R_OK)) {
163 copy_file(LAST_CONSOLE_FILE, LAST_KMSG_FILE);
164 } else if (!access(ALT_LAST_CONSOLE_FILE, R_OK)) {
165 copy_file(ALT_LAST_CONSOLE_FILE, LAST_KMSG_FILE);
166 }
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800167 }
168
169 return 0;
170}