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 //
18 // Strictly to deal with reboot into system after OTA, then
19 // reboot while in system before boot complete landing us back
20 // into recovery to continue with any mitigations with retained
21 // log history. This simply refreshes the pmsg files from
22 // the last pmsg file contents.
23 //
24 // Usage:
25 //    recovery-refresh [--force-rotate|--rotate]
26 //
27 //    All file content representing in the recovery/ directory stored in
28 //    /sys/fs/pstore/pmsg-ramoops-0 in logger format that reside in the
29 //    LOG_ID_SYSTEM buffer at ANDROID_LOG_INFO priority or higher is
30 //    refreshed into /dev/pmsg0. This ensures that an unexpected reboot
31 //    before recovery-persist is run will still contain the associated
32 //    pmsg Android Logger content.
33 //
34 //    --force-rotate  recovery/last_kmsg and recovery.last_log files are
35 //                    rotated with .<number> suffixes upwards.
36 //    --rotate        rotated only if rocovery/last_msg or recovery/last_log
37 //                    exist, otherwise perform 1:1 refresh.
38 //
39 
40 #include <string.h>
41 #include <string>
42 
43 #include <private/android_logger.h> /* private pmsg functions */
44 
45 #include "rotate_logs.h"
46 
main(int argc,char ** argv)47 int main(int argc, char **argv) {
48     static const char filter[] = "recovery/";
49     static const char force_rotate_flag[] = "--force-rotate";
50     static const char rotate_flag[] = "--rotate";
51     ssize_t ret;
52     bool doRotate = false;
53     // Take last pmsg contents and rewrite it to the current pmsg session.
54     if ((argc <= 1) || !argv[1] ||
55             (((doRotate = strcmp(argv[1], rotate_flag))) &&
56                 strcmp(argv[1], force_rotate_flag))) {
57         doRotate = false;
58     } else if (!doRotate) {
59         // Do we need to rotate?
60         __android_log_pmsg_file_read(
61             LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
62             logbasename, &doRotate);
63     }
64 
65     // Take action to refresh pmsg contents
66     ret = __android_log_pmsg_file_read(
67         LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
68         logrotate, &doRotate);
69 
70     return (ret < 0) ? ret : 0;
71 }
72