Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 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 | |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 17 | // |
| 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 Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | #include <string> |
| 39 | |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 40 | #include <android-base/file.h> |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 41 | #include <android-base/logging.h> |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 42 | #include <private/android_logger.h> /* private pmsg functions */ |
| 43 | |
Tao Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 44 | #include "recovery_utils/logging.h" |
| 45 | #include "recovery_utils/parse_install_logs.h" |
Tianjie Xu | e113e4d | 2016-10-21 17:46:13 -0700 | [diff] [blame] | 46 | |
Tianjie Xu | 2b1a464 | 2018-09-06 11:58:55 -0700 | [diff] [blame] | 47 | constexpr const char* LAST_LOG_FILE = "/data/misc/recovery/last_log"; |
| 48 | constexpr const char* LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0"; |
| 49 | constexpr const char* LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg"; |
| 50 | constexpr const char* LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0"; |
| 51 | constexpr const char* ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops"; |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 52 | |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 53 | // close a file, log an error if the error indicator is set |
| 54 | static void check_and_fclose(FILE *fp, const char *name) { |
| 55 | fflush(fp); |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 56 | if (ferror(fp)) { |
| 57 | PLOG(ERROR) << "Error in " << name; |
| 58 | } |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 59 | fclose(fp); |
| 60 | } |
| 61 | |
| 62 | static void copy_file(const char* source, const char* destination) { |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 63 | FILE* dest_fp = fopen(destination, "we"); |
| 64 | if (dest_fp == nullptr) { |
| 65 | PLOG(ERROR) << "Can't open " << destination; |
| 66 | } else { |
| 67 | FILE* source_fp = fopen(source, "re"); |
| 68 | if (source_fp != nullptr) { |
| 69 | char buf[4096]; |
| 70 | size_t bytes; |
| 71 | while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) { |
| 72 | fwrite(buf, 1, bytes, dest_fp); |
| 73 | } |
| 74 | check_and_fclose(source_fp, source); |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 75 | } |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 76 | check_and_fclose(dest_fp, destination); |
| 77 | } |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Artem Yudin | 0cd72a9 | 2022-06-07 16:32:44 -0700 | [diff] [blame] | 80 | static bool file_exists(const char* filename) { |
| 81 | return access(filename, R_OK) == 0; |
| 82 | } |
| 83 | |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 84 | static bool rotated = false; |
| 85 | |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 86 | ssize_t logsave( |
| 87 | log_id_t /* logId */, |
| 88 | char /* prio */, |
| 89 | const char *filename, |
| 90 | const char *buf, size_t len, |
| 91 | void * /* arg */) { |
| 92 | |
| 93 | std::string destination("/data/misc/"); |
| 94 | destination += filename; |
| 95 | |
| 96 | std::string buffer(buf, len); |
| 97 | |
| 98 | { |
| 99 | std::string content; |
| 100 | android::base::ReadFileToString(destination, &content); |
| 101 | |
| 102 | if (buffer.compare(content) == 0) { |
| 103 | return len; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // ToDo: Any others that match? Are we pulling in multiple |
| 108 | // already-rotated files? Algorithm thus far is KISS: one file, |
| 109 | // one rotation allowed. |
| 110 | |
Tianjie Xu | e113e4d | 2016-10-21 17:46:13 -0700 | [diff] [blame] | 111 | rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE); |
| 112 | rotated = true; |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 113 | |
| 114 | return android::base::WriteStringToFile(buffer, destination.c_str()); |
| 115 | } |
| 116 | |
| 117 | int main(int argc, char **argv) { |
| 118 | |
| 119 | /* Is /cache a mount?, we have been delivered where we are not wanted */ |
| 120 | /* |
| 121 | * Following code halves the size of the executable as compared to: |
| 122 | * |
| 123 | * load_volume_table(); |
| 124 | * has_cache = volume_for_path(CACHE_ROOT) != nullptr; |
| 125 | */ |
| 126 | bool has_cache = false; |
| 127 | static const char mounts_file[] = "/proc/mounts"; |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 128 | FILE* fp = fopen(mounts_file, "re"); |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 129 | if (!fp) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 130 | PLOG(ERROR) << "failed to open " << mounts_file; |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 131 | } else { |
| 132 | char *line = NULL; |
| 133 | size_t len = 0; |
| 134 | ssize_t read; |
| 135 | while ((read = getline(&line, &len, fp)) != -1) { |
| 136 | if (strstr(line, " /cache ")) { |
| 137 | has_cache = true; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | free(line); |
| 142 | fclose(fp); |
| 143 | } |
| 144 | |
| 145 | if (has_cache) { |
Tianjie Xu | 2b1a464 | 2018-09-06 11:58:55 -0700 | [diff] [blame] | 146 | // Collects and reports the non-a/b update metrics from last_install; and removes the file |
| 147 | // to avoid duplicate report. |
Artem Yudin | 0cd72a9 | 2022-06-07 16:32:44 -0700 | [diff] [blame] | 148 | if (file_exists(LAST_INSTALL_FILE_IN_CACHE) && unlink(LAST_INSTALL_FILE_IN_CACHE) == -1) { |
Tianjie Xu | 2b1a464 | 2018-09-06 11:58:55 -0700 | [diff] [blame] | 149 | PLOG(ERROR) << "Failed to unlink " << LAST_INSTALL_FILE_IN_CACHE; |
| 150 | } |
| 151 | |
| 152 | // TBD: Future location to move content from /cache/recovery to /data/misc/recovery/ |
| 153 | // if --force-persist flag, then transfer pmsg data anyways |
| 154 | if ((argc <= 1) || !argv[1] || strcmp(argv[1], "--force-persist")) { |
| 155 | return 0; |
| 156 | } |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Artem Yudin | 0cd72a9 | 2022-06-07 16:32:44 -0700 | [diff] [blame] | 159 | /* Is there something in pmsg? If not, no need to proceed. */ |
| 160 | if (!file_exists(LAST_PMSG_FILE)) { |
| 161 | return 0; |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Take last pmsg file contents and send it off to the logsave |
| 165 | __android_log_pmsg_file_read( |
| 166 | LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", logsave, NULL); |
| 167 | |
Tianjie Xu | 2b1a464 | 2018-09-06 11:58:55 -0700 | [diff] [blame] | 168 | // For those device without /cache, the last_install file has been copied to |
| 169 | // /data/misc/recovery from pmsg. Looks for the sideload history only. |
| 170 | if (!has_cache) { |
Artem Yudin | 0cd72a9 | 2022-06-07 16:32:44 -0700 | [diff] [blame] | 171 | if (file_exists(LAST_INSTALL_FILE) && unlink(LAST_INSTALL_FILE) == -1) { |
Tianjie Xu | 2b1a464 | 2018-09-06 11:58:55 -0700 | [diff] [blame] | 172 | PLOG(ERROR) << "Failed to unlink " << LAST_INSTALL_FILE; |
| 173 | } |
| 174 | } |
| 175 | |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 176 | /* Is there a last console log too? */ |
Mark Salyzyn | 5f7111f | 2016-04-04 16:19:26 -0700 | [diff] [blame] | 177 | if (rotated) { |
Artem Yudin | 0cd72a9 | 2022-06-07 16:32:44 -0700 | [diff] [blame] | 178 | if (file_exists(LAST_CONSOLE_FILE)) { |
| 179 | copy_file(LAST_CONSOLE_FILE, LAST_KMSG_FILE); |
| 180 | } else if (file_exists(ALT_LAST_CONSOLE_FILE)) { |
| 181 | copy_file(ALT_LAST_CONSOLE_FILE, LAST_KMSG_FILE); |
| 182 | } |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |