Browse Source

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
Tianjie Xu 4 years ago
parent
commit
e113e4d67f
6 changed files with 178 additions and 182 deletions
  1. 8
    3
      Android.mk
  2. 4
    38
      recovery-persist.cpp
  3. 1
    57
      recovery-refresh.cpp
  4. 6
    84
      recovery.cpp
  5. 115
    0
      rotate_logs.cpp
  6. 44
    0
      rotate_logs.h

+ 8
- 3
Android.mk View File

@@ -46,6 +46,7 @@ LOCAL_SRC_FILES := \
46 46
     install.cpp \
47 47
     recovery.cpp \
48 48
     roots.cpp \
49
+    rotate_logs.cpp \
49 50
     screen_ui.cpp \
50 51
     ui.cpp \
51 52
     verifier.cpp \
@@ -119,7 +120,9 @@ include $(BUILD_EXECUTABLE)
119 120
 # recovery-persist (system partition dynamic executable run after /data mounts)
120 121
 # ===============================
121 122
 include $(CLEAR_VARS)
122
-LOCAL_SRC_FILES := recovery-persist.cpp
123
+LOCAL_SRC_FILES := \
124
+    recovery-persist.cpp \
125
+    rotate_logs.cpp
123 126
 LOCAL_MODULE := recovery-persist
124 127
 LOCAL_SHARED_LIBRARIES := liblog libbase
125 128
 LOCAL_CFLAGS := -Werror
@@ -129,9 +132,11 @@ include $(BUILD_EXECUTABLE)
129 132
 # recovery-refresh (system partition dynamic executable run at init)
130 133
 # ===============================
131 134
 include $(CLEAR_VARS)
132
-LOCAL_SRC_FILES := recovery-refresh.cpp
135
+LOCAL_SRC_FILES := \
136
+    recovery-refresh.cpp \
137
+    rotate_logs.cpp
133 138
 LOCAL_MODULE := recovery-refresh
134
-LOCAL_SHARED_LIBRARIES := liblog
139
+LOCAL_SHARED_LIBRARIES := liblog libbase
135 140
 LOCAL_CFLAGS := -Werror
136 141
 LOCAL_INIT_RC := recovery-refresh.rc
137 142
 include $(BUILD_EXECUTABLE)

+ 4
- 38
recovery-persist.cpp View File

@@ -30,7 +30,6 @@
30 30
 //    --force-persist  ignore /cache mount, always rotate in the contents.
31 31
 //
32 32
 
33
-#include <errno.h>
34 33
 #include <stdio.h>
35 34
 #include <stdlib.h>
36 35
 #include <string.h>
@@ -40,17 +39,16 @@
40 39
 
41 40
 #include <android-base/file.h>
42 41
 #include <android-base/logging.h>
43
-
44 42
 #include <private/android_logger.h> /* private pmsg functions */
45 43
 
44
+#include "rotate_logs.h"
45
+
46 46
 static const char *LAST_LOG_FILE = "/data/misc/recovery/last_log";
47 47
 static const char *LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0";
48 48
 static const char *LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg";
49 49
 static const char *LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0";
50 50
 static const char *ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops";
51 51
 
52
-static const int KEEP_LOG_COUNT = 10;
53
-
54 52
 // close a file, log an error if the error indicator is set
55 53
 static void check_and_fclose(FILE *fp, const char *name) {
56 54
     fflush(fp);
@@ -80,39 +78,6 @@ static void copy_file(const char* source, const char* destination) {
80 78
 
81 79
 static 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.
86
-static 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
-
116 81
 ssize_t logsave(
117 82
         log_id_t /* logId */,
118 83
         char /* prio */,
@@ -138,7 +103,8 @@ ssize_t logsave(
138 103
     // already-rotated files? Algorithm thus far is KISS: one file,
139 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 109
     return android::base::WriteStringToFile(buffer, destination.c_str());
144 110
 }

+ 1
- 57
recovery-refresh.cpp View File

@@ -14,8 +14,6 @@
14 14
  * limitations under the License.
15 15
  */
16 16
 
17
-#define LOG_TAG "recovery-refresh"
18
-
19 17
 //
20 18
 // Strictly to deal with reboot into system after OTA, then
21 19
 // reboot while in system before boot complete landing us back
@@ -40,64 +38,11 @@
40 38
 //
41 39
 
42 40
 #include <string.h>
43
-
44 41
 #include <string>
45 42
 
46
-#include <android/log.h> /* Android Log Priority Tags */
47 43
 #include <private/android_logger.h> /* private pmsg functions */
48 44
 
49
-static const char LAST_KMSG_FILE[] = "recovery/last_kmsg";
50
-static const char LAST_LOG_FILE[] = "recovery/last_log";
51
-
52
-static ssize_t logbasename(
53
-        log_id_t /* logId */,
54
-        char /* prio */,
55
-        const char *filename,
56
-        const char * /* buf */, size_t len,
57
-        void *arg) {
58
-    if (strstr(LAST_KMSG_FILE, filename) ||
59
-            strstr(LAST_LOG_FILE, filename)) {
60
-        bool *doRotate = reinterpret_cast<bool *>(arg);
61
-        *doRotate = true;
62
-    }
63
-    return len;
64
-}
65
-
66
-static ssize_t logrotate(
67
-        log_id_t logId,
68
-        char prio,
69
-        const char *filename,
70
-        const char *buf, size_t len,
71
-        void *arg) {
72
-    bool *doRotate = reinterpret_cast<bool *>(arg);
73
-    if (!*doRotate) {
74
-        return __android_log_pmsg_file_write(logId, prio, filename, buf, len);
75
-    }
76
-
77
-    std::string name(filename);
78
-    size_t dot = name.find_last_of('.');
79
-    std::string sub = name.substr(0, dot);
80
-
81
-    if (!strstr(LAST_KMSG_FILE, sub.c_str()) &&
82
-                !strstr(LAST_LOG_FILE, sub.c_str())) {
83
-        return __android_log_pmsg_file_write(logId, prio, filename, buf, len);
84
-    }
85
-
86
-    // filename rotation
87
-    if (dot == std::string::npos) {
88
-        name += ".1";
89
-    } else {
90
-        std::string number = name.substr(dot + 1);
91
-        if (!isdigit(number.data()[0])) {
92
-            name += ".1";
93
-        } else {
94
-            auto i = std::stoull(number);
95
-            name = sub + "." + std::to_string(i + 1);
96
-        }
97
-    }
98
-
99
-    return __android_log_pmsg_file_write(logId, prio, name.c_str(), buf, len);
100
-}
45
+#include "rotate_logs.h"
101 46
 
102 47
 int main(int argc, char **argv) {
103 48
     static const char filter[] = "recovery/";
@@ -105,7 +50,6 @@ int main(int argc, char **argv) {
105 50
     static const char rotate_flag[] = "--rotate";
106 51
     ssize_t ret;
107 52
     bool doRotate = false;
108
-
109 53
     // Take last pmsg contents and rewrite it to the current pmsg session.
110 54
     if ((argc <= 1) || !argv[1] ||
111 55
             (((doRotate = strcmp(argv[1], rotate_flag))) &&

+ 6
- 84
recovery.cpp View File

@@ -66,8 +66,9 @@
66 66
 #include "minui/minui.h"
67 67
 #include "otautil/DirUtil.h"
68 68
 #include "roots.h"
69
-#include "ui.h"
69
+#include "rotate_logs.h"
70 70
 #include "screen_ui.h"
71
+#include "ui.h"
71 72
 
72 73
 struct selabel_handle *sehandle;
73 74
 
@@ -110,7 +111,6 @@ static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
110 111
 static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
111 112
 static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg";
112 113
 static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
113
-static const int KEEP_LOG_COUNT = 10;
114 114
 // We will try to apply the update package 5 times at most in case of an I/O error.
115 115
 static const int EIO_RETRY_COUNT = 4;
116 116
 static const int BATTERY_READ_TIMEOUT_IN_SEC = 10;
@@ -447,37 +447,6 @@ static void copy_log_file(const char* source, const char* destination, bool appe
447 447
     }
448 448
 }
449 449
 
450
-// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
451
-// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.
452
-// Overwrite any existing last_log.$max and last_kmsg.$max.
453
-static void rotate_logs(int max) {
454
-    // Logs should only be rotated once.
455
-    static bool rotated = false;
456
-    if (rotated) {
457
-        return;
458
-    }
459
-    rotated = true;
460
-    ensure_path_mounted(LAST_LOG_FILE);
461
-    ensure_path_mounted(LAST_KMSG_FILE);
462
-
463
-    for (int i = max-1; i >= 0; --i) {
464
-        std::string old_log = android::base::StringPrintf("%s", LAST_LOG_FILE);
465
-        if (i > 0) {
466
-          old_log += "." + std::to_string(i);
467
-        }
468
-        std::string new_log = android::base::StringPrintf("%s.%d", LAST_LOG_FILE, i+1);
469
-        // Ignore errors if old_log doesn't exist.
470
-        rename(old_log.c_str(), new_log.c_str());
471
-
472
-        std::string old_kmsg = android::base::StringPrintf("%s", LAST_KMSG_FILE);
473
-        if (i > 0) {
474
-          old_kmsg += "." + std::to_string(i);
475
-        }
476
-        std::string new_kmsg = android::base::StringPrintf("%s.%d", LAST_KMSG_FILE, i+1);
477
-        rename(old_kmsg.c_str(), new_kmsg.c_str());
478
-    }
479
-}
480
-
481 450
 static void copy_logs() {
482 451
     // We only rotate and record the log of the current session if there are
483 452
     // actual attempts to modify the flash, such as wipes, installs from BCB
@@ -496,7 +465,9 @@ static void copy_logs() {
496 465
         return;
497 466
     }
498 467
 
499
-    rotate_logs(KEEP_LOG_COUNT);
468
+    ensure_path_mounted(LAST_LOG_FILE);
469
+    ensure_path_mounted(LAST_KMSG_FILE);
470
+    rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE);
500 471
 
501 472
     // Copy logs to cache so the system can find out what happened.
502 473
     copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
@@ -1428,56 +1399,6 @@ static void log_failure_code(ErrorCode code, const char *update_package) {
1428 1399
     LOG(INFO) << log_content;
1429 1400
 }
1430 1401
 
1431
-static ssize_t logbasename(
1432
-        log_id_t /* logId */,
1433
-        char /* prio */,
1434
-        const char *filename,
1435
-        const char * /* buf */, size_t len,
1436
-        void *arg) {
1437
-    if (strstr(LAST_KMSG_FILE, filename) ||
1438
-            strstr(LAST_LOG_FILE, filename)) {
1439
-        bool *doRotate = reinterpret_cast<bool *>(arg);
1440
-        *doRotate = true;
1441
-    }
1442
-    return len;
1443
-}
1444
-
1445
-static ssize_t logrotate(
1446
-        log_id_t logId,
1447
-        char prio,
1448
-        const char *filename,
1449
-        const char *buf, size_t len,
1450
-        void *arg) {
1451
-    bool *doRotate = reinterpret_cast<bool *>(arg);
1452
-    if (!*doRotate) {
1453
-        return __android_log_pmsg_file_write(logId, prio, filename, buf, len);
1454
-    }
1455
-
1456
-    std::string name(filename);
1457
-    size_t dot = name.find_last_of('.');
1458
-    std::string sub = name.substr(0, dot);
1459
-
1460
-    if (!strstr(LAST_KMSG_FILE, sub.c_str()) &&
1461
-                !strstr(LAST_LOG_FILE, sub.c_str())) {
1462
-        return __android_log_pmsg_file_write(logId, prio, filename, buf, len);
1463
-    }
1464
-
1465
-    // filename rotation
1466
-    if (dot == std::string::npos) {
1467
-        name += ".1";
1468
-    } else {
1469
-        std::string number = name.substr(dot + 1);
1470
-        if (!isdigit(number.data()[0])) {
1471
-            name += ".1";
1472
-        } else {
1473
-            auto i = std::stoull(number);
1474
-            name = sub + "." + std::to_string(i + 1);
1475
-        }
1476
-    }
1477
-
1478
-    return __android_log_pmsg_file_write(logId, prio, name.c_str(), buf, len);
1479
-}
1480
-
1481 1402
 int main(int argc, char **argv) {
1482 1403
     // We don't have logcat yet under recovery; so we'll print error on screen and
1483 1404
     // log to stdout (which is redirected to recovery.log) as we used to do.
@@ -1487,6 +1408,7 @@ int main(int argc, char **argv) {
1487 1408
     static const char filter[] = "recovery/";
1488 1409
     // Do we need to rotate?
1489 1410
     bool doRotate = false;
1411
+
1490 1412
     __android_log_pmsg_file_read(
1491 1413
         LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
1492 1414
         logbasename, &doRotate);

+ 115
- 0
rotate_logs.cpp View File

@@ -0,0 +1,115 @@
1
+/*
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 "rotate_logs.h"
18
+
19
+#include <stdio.h>
20
+#include <string.h>
21
+#include <sys/types.h>
22
+
23
+#include <string>
24
+
25
+#include <android-base/file.h>
26
+#include <android-base/logging.h>
27
+#include <android-base/parseint.h>
28
+#include <android-base/stringprintf.h>
29
+#include <private/android_logger.h> /* private pmsg functions */
30
+
31
+static const std::string LAST_KMSG_FILTER = "recovery/last_kmsg";
32
+static const std::string LAST_LOG_FILTER = "recovery/last_log";
33
+
34
+ssize_t logbasename(
35
+        log_id_t /* logId */,
36
+        char /* prio */,
37
+        const char *filename,
38
+        const char * /* buf */, size_t len,
39
+        void *arg) {
40
+    bool* doRotate  = static_cast<bool*>(arg);
41
+    if (LAST_KMSG_FILTER.find(filename) != std::string::npos ||
42
+            LAST_LOG_FILTER.find(filename) != std::string::npos) {
43
+        *doRotate = true;
44
+    }
45
+    return len;
46
+}
47
+
48
+ssize_t logrotate(
49
+        log_id_t logId,
50
+        char prio,
51
+        const char *filename,
52
+        const char *buf, size_t len,
53
+        void *arg) {
54
+    bool* doRotate  = static_cast<bool*>(arg);
55
+    if (!*doRotate) {
56
+        return __android_log_pmsg_file_write(logId, prio, filename, buf, len);
57
+    }
58
+
59
+    std::string name(filename);
60
+    size_t dot = name.find_last_of('.');
61
+    std::string sub = name.substr(0, dot);
62
+
63
+    if (LAST_KMSG_FILTER.find(sub) == std::string::npos &&
64
+            LAST_LOG_FILTER.find(sub) == std::string::npos) {
65
+        return __android_log_pmsg_file_write(logId, prio, filename, buf, len);
66
+    }
67
+
68
+    // filename rotation
69
+    if (dot == std::string::npos) {
70
+        name += ".1";
71
+    } else {
72
+        std::string number = name.substr(dot + 1);
73
+        if (!isdigit(number[0])) {
74
+            name += ".1";
75
+        } else {
76
+            size_t i;
77
+            if (!android::base::ParseUint(number, &i)) {
78
+                LOG(ERROR) << "failed to parse uint in " << number;
79
+                return -1;
80
+            }
81
+            name = sub + "." + std::to_string(i + 1);
82
+        }
83
+    }
84
+
85
+    return __android_log_pmsg_file_write(logId, prio, name.c_str(), buf, len);
86
+}
87
+
88
+// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
89
+// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.
90
+// Overwrite any existing last_log.$max and last_kmsg.$max.
91
+void rotate_logs(const char* last_log_file, const char* last_kmsg_file) {
92
+    // Logs should only be rotated once.
93
+    static bool rotated = false;
94
+    if (rotated) {
95
+        return;
96
+    }
97
+    rotated = true;
98
+
99
+    for (int i = KEEP_LOG_COUNT - 1; i >= 0; --i) {
100
+        std::string old_log = android::base::StringPrintf("%s", last_log_file);
101
+        if (i > 0) {
102
+          old_log += "." + std::to_string(i);
103
+        }
104
+        std::string new_log = android::base::StringPrintf("%s.%d", last_log_file, i+1);
105
+        // Ignore errors if old_log doesn't exist.
106
+        rename(old_log.c_str(), new_log.c_str());
107
+
108
+        std::string old_kmsg = android::base::StringPrintf("%s", last_kmsg_file);
109
+        if (i > 0) {
110
+          old_kmsg += "." + std::to_string(i);
111
+        }
112
+        std::string new_kmsg = android::base::StringPrintf("%s.%d", last_kmsg_file, i+1);
113
+        rename(old_kmsg.c_str(), new_kmsg.c_str());
114
+    }
115
+}

+ 44
- 0
rotate_logs.h View File

@@ -0,0 +1,44 @@
1
+/*
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
+#ifndef _ROTATE_LOGS_H
18
+#define _ROTATE_LOGS_H
19
+
20
+#include <string>
21
+
22
+#include <private/android_logger.h> /* private pmsg functions */
23
+
24
+constexpr int KEEP_LOG_COUNT = 10;
25
+
26
+ssize_t logbasename(log_id_t /* logId */,
27
+        char /* prio */,
28
+        const char *filename,
29
+        const char * /* buf */, size_t len,
30
+        void *arg);
31
+
32
+ssize_t logrotate(
33
+        log_id_t logId,
34
+        char prio,
35
+        const char *filename,
36
+        const char *buf, size_t len,
37
+        void *arg);
38
+
39
+// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max.
40
+// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max.
41
+// Overwrite any existing last_log.$max and last_kmsg.$max.
42
+void rotate_logs(const char* last_log_file, const char* last_kmsg_file);
43
+
44
+#endif //_ROTATE_LOG_H