aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTianjie Xu2016-10-21 19:46:13 -0500
committerTianjie Xu2016-11-03 20:16:33 -0500
commite113e4d67fb581e7232ffe8c430b8bf4f44e08bb (patch)
tree4344cb07a28fb6ff2454d3efa672dc6bd5797a56 /recovery-persist.cpp
parent4011bb161a60fb04fb0d4c4c3e1090d43fcdd9e4 (diff)
downloadplatform-bootable-recovery-e113e4d67fb581e7232ffe8c430b8bf4f44e08bb.tar.gz
platform-bootable-recovery-e113e4d67fb581e7232ffe8c430b8bf4f44e08bb.tar.xz
platform-bootable-recovery-e113e4d67fb581e7232ffe8c430b8bf4f44e08bb.zip
Cleanup the duplicates of logs rotation functions
Bug: 32067516 Test: Logs rotated successfully on angler, recovery-refresh/persist tests passed on an a/b device. Change-Id: Ie80adf0fa958ad3d7869d2d17f49489666b86c29
Diffstat (limited to 'recovery-persist.cpp')
-rw-r--r--recovery-persist.cpp42
1 files changed, 4 insertions, 38 deletions
diff --git a/recovery-persist.cpp b/recovery-persist.cpp
index b0ec141c..d706ccac 100644
--- a/recovery-persist.cpp
+++ b/recovery-persist.cpp
@@ -30,7 +30,6 @@
30// --force-persist ignore /cache mount, always rotate in the contents. 30// --force-persist ignore /cache mount, always rotate in the contents.
31// 31//
32 32
33#include <errno.h>
34#include <stdio.h> 33#include <stdio.h>
35#include <stdlib.h> 34#include <stdlib.h>
36#include <string.h> 35#include <string.h>
@@ -40,17 +39,16 @@
40 39
41#include <android-base/file.h> 40#include <android-base/file.h>
42#include <android-base/logging.h> 41#include <android-base/logging.h>
43
44#include <private/android_logger.h> /* private pmsg functions */ 42#include <private/android_logger.h> /* private pmsg functions */
45 43
44#include "rotate_logs.h"
45
46static const char *LAST_LOG_FILE = "/data/misc/recovery/last_log"; 46static const char *LAST_LOG_FILE = "/data/misc/recovery/last_log";
47static const char *LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0"; 47static const char *LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0";
48static const char *LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg"; 48static const char *LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg";
49static const char *LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0"; 49static const char *LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0";
50static const char *ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops"; 50static const char *ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops";
51 51
52static const int KEEP_LOG_COUNT = 10;
53
54// close a file, log an error if the error indicator is set 52// close a file, log an error if the error indicator is set
55static void check_and_fclose(FILE *fp, const char *name) { 53static void check_and_fclose(FILE *fp, const char *name) {
56 fflush(fp); 54 fflush(fp);
@@ -80,39 +78,6 @@ static void copy_file(const char* source, const char* destination) {
80 78
81static bool rotated = false; 79static bool rotated = false;
82 80
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( 81ssize_t logsave(
117 log_id_t /* logId */, 82 log_id_t /* logId */,
118 char /* prio */, 83 char /* prio */,
@@ -138,7 +103,8 @@ ssize_t logsave(
138 // already-rotated files? Algorithm thus far is KISS: one file, 103 // already-rotated files? Algorithm thus far is KISS: one file,
139 // one rotation allowed. 104 // one rotation allowed.
140 105
141 rotate_logs(KEEP_LOG_COUNT); 106 rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE);
107 rotated = true;
142 108
143 return android::base::WriteStringToFile(buffer, destination.c_str()); 109 return android::base::WriteStringToFile(buffer, destination.c_str());
144} 110}