blob: d5af72aad8054afb3dc02360f1109128bfaf3d94 [file] [log] [blame]
Jerry Zhangcadf4ed2018-05-02 16:56:00 -07001/*
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#include "logging.h"
18
19#include <stdio.h>
20#include <string.h>
21#include <sys/klog.h>
22#include <sys/types.h>
23
24#include <string>
25
26#include <android-base/file.h>
27#include <android-base/logging.h>
28#include <android-base/parseint.h>
29#include <android-base/stringprintf.h>
30#include <private/android_filesystem_config.h> /* for AID_SYSTEM */
31#include <private/android_logger.h> /* private pmsg functions */
32
33#include "common.h"
34#include "otautil/dirutil.h"
35#include "otautil/paths.h"
36#include "roots.h"
37
38static constexpr const char* LOG_FILE = "/cache/recovery/log";
39static constexpr const char* LAST_INSTALL_FILE = "/cache/recovery/last_install";
40static constexpr const char* LAST_KMSG_FILE = "/cache/recovery/last_kmsg";
41static constexpr const char* LAST_LOG_FILE = "/cache/recovery/last_log";
42
43static const std::string LAST_KMSG_FILTER = "recovery/last_kmsg";
44static const std::string LAST_LOG_FILTER = "recovery/last_log";
45
46// fopen(3)'s the given file, by mounting volumes and making parent dirs as necessary. Returns the
47// file pointer, or nullptr on error.
48static FILE* fopen_path(const std::string& path, const char* mode) {
49 if (ensure_path_mounted(path.c_str()) != 0) {
50 LOG(ERROR) << "Can't mount " << path;
51 return nullptr;
52 }
53
54 // When writing, try to create the containing directory, if necessary. Use generous permissions,
55 // the system (init.rc) will reset them.
56 if (strchr("wa", mode[0])) {
57 mkdir_recursively(path, 0777, true, sehandle);
58 }
59 return fopen(path.c_str(), mode);
60}
61
62void check_and_fclose(FILE* fp, const std::string& name) {
63 fflush(fp);
64 if (fsync(fileno(fp)) == -1) {
65 PLOG(ERROR) << "Failed to fsync " << name;
66 }
67 if (ferror(fp)) {
68 PLOG(ERROR) << "Error in " << name;
69 }
70 fclose(fp);
71}
72
73// close a file, log an error if the error indicator is set
74ssize_t logbasename(log_id_t /* id */, char /* prio */, const char* filename, const char* /* buf */,
75 size_t len, void* arg) {
76 bool* do_rotate = static_cast<bool*>(arg);
77 if (LAST_KMSG_FILTER.find(filename) != std::string::npos ||
78 LAST_LOG_FILTER.find(filename) != std::string::npos) {
79 *do_rotate = true;
80 }
81 return len;
82}
83
84ssize_t logrotate(log_id_t id, char prio, const char* filename, const char* buf, size_t len,
85 void* arg) {
86 bool* do_rotate = static_cast<bool*>(arg);
87 if (!*do_rotate) {
88 return __android_log_pmsg_file_write(id, prio, filename, buf, len);
89 }
90
91 std::string name(filename);
92 size_t dot = name.find_last_of('.');
93 std::string sub = name.substr(0, dot);
94
95 if (LAST_KMSG_FILTER.find(sub) == std::string::npos &&
96 LAST_LOG_FILTER.find(sub) == std::string::npos) {
97 return __android_log_pmsg_file_write(id, prio, filename, buf, len);
98 }
99
100 // filename rotation
101 if (dot == std::string::npos) {
102 name += ".1";
103 } else {
104 std::string number = name.substr(dot + 1);
105 if (!isdigit(number[0])) {
106 name += ".1";
107 } else {
108 size_t i;
109 if (!android::base::ParseUint(number, &i)) {
110 LOG(ERROR) << "failed to parse uint in " << number;
111 return -1;
112 }
113 name = sub + "." + std::to_string(i + 1);
114 }
115 }
116
117 return __android_log_pmsg_file_write(id, prio, name.c_str(), buf, len);
118}
119
120// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
121// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.
122// Overwrite any existing last_log.$max and last_kmsg.$max.
123void rotate_logs(const char* last_log_file, const char* last_kmsg_file) {
124 // Logs should only be rotated once.
125 static bool rotated = false;
126 if (rotated) {
127 return;
128 }
129 rotated = true;
130
131 for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) {
132 std::string old_log = android::base::StringPrintf("%s", last_log_file);
133 if (i > 0) {
134 old_log += "." + std::to_string(i);
135 }
136 std::string new_log = android::base::StringPrintf("%s.%d", last_log_file, i + 1);
137 // Ignore errors if old_log doesn't exist.
138 rename(old_log.c_str(), new_log.c_str());
139
140 std::string old_kmsg = android::base::StringPrintf("%s", last_kmsg_file);
141 if (i > 0) {
142 old_kmsg += "." + std::to_string(i);
143 }
144 std::string new_kmsg = android::base::StringPrintf("%s.%d", last_kmsg_file, i + 1);
145 rename(old_kmsg.c_str(), new_kmsg.c_str());
146 }
147}
148
149// Writes content to the current pmsg session.
150static ssize_t __pmsg_write(const std::string& filename, const std::string& buf) {
151 return __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filename.c_str(),
152 buf.data(), buf.size());
153}
154
155void copy_log_file_to_pmsg(const std::string& source, const std::string& destination) {
156 std::string content;
157 android::base::ReadFileToString(source, &content);
158 __pmsg_write(destination, content);
159}
160
161// How much of the temp log we have copied to the copy in cache.
162static off_t tmplog_offset = 0;
163
164void reset_tmplog_offset() {
165 tmplog_offset = 0;
166}
167
168void copy_log_file(const std::string& source, const std::string& destination, bool append) {
169 FILE* dest_fp = fopen_path(destination, append ? "ae" : "we");
170 if (dest_fp == nullptr) {
171 PLOG(ERROR) << "Can't open " << destination;
172 } else {
173 FILE* source_fp = fopen(source.c_str(), "re");
174 if (source_fp != nullptr) {
175 if (append) {
176 fseeko(source_fp, tmplog_offset, SEEK_SET); // Since last write
177 }
178 char buf[4096];
179 size_t bytes;
180 while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) {
181 fwrite(buf, 1, bytes, dest_fp);
182 }
183 if (append) {
184 tmplog_offset = ftello(source_fp);
185 }
186 check_and_fclose(source_fp, source);
187 }
188 check_and_fclose(dest_fp, destination);
189 }
190}
191
192void copy_logs(bool modified_flash, bool has_cache) {
193 // We only rotate and record the log of the current session if there are actual attempts to modify
194 // the flash, such as wipes, installs from BCB or menu selections. This is to avoid unnecessary
195 // rotation (and possible deletion) of log files, if it does not do anything loggable.
196 if (!modified_flash) {
197 return;
198 }
199
200 // Always write to pmsg, this allows the OTA logs to be caught in `logcat -L`.
201 copy_log_file_to_pmsg(Paths::Get().temporary_log_file(), LAST_LOG_FILE);
202 copy_log_file_to_pmsg(Paths::Get().temporary_install_file(), LAST_INSTALL_FILE);
203
204 // We can do nothing for now if there's no /cache partition.
205 if (!has_cache) {
206 return;
207 }
208
209 ensure_path_mounted(LAST_LOG_FILE);
210 ensure_path_mounted(LAST_KMSG_FILE);
211 rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE);
212
213 // Copy logs to cache so the system can find out what happened.
214 copy_log_file(Paths::Get().temporary_log_file(), LOG_FILE, true);
215 copy_log_file(Paths::Get().temporary_log_file(), LAST_LOG_FILE, false);
216 copy_log_file(Paths::Get().temporary_install_file(), LAST_INSTALL_FILE, false);
217 save_kernel_log(LAST_KMSG_FILE);
218 chmod(LOG_FILE, 0600);
219 chown(LOG_FILE, AID_SYSTEM, AID_SYSTEM);
220 chmod(LAST_KMSG_FILE, 0600);
221 chown(LAST_KMSG_FILE, AID_SYSTEM, AID_SYSTEM);
222 chmod(LAST_LOG_FILE, 0640);
223 chmod(LAST_INSTALL_FILE, 0644);
224 sync();
225}
226
227// Read from kernel log into buffer and write out to file.
228void save_kernel_log(const char* destination) {
229 int klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0);
230 if (klog_buf_len <= 0) {
231 PLOG(ERROR) << "Error getting klog size";
232 return;
233 }
234
235 std::string buffer(klog_buf_len, 0);
236 int n = klogctl(KLOG_READ_ALL, &buffer[0], klog_buf_len);
237 if (n == -1) {
238 PLOG(ERROR) << "Error in reading klog";
239 return;
240 }
241 buffer.resize(n);
242 android::base::WriteStringToFile(buffer, destination);
243}