No Description

recovery-persist.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // Strictly to deal with reboot into system after OTA after /data
  18. // mounts to pull the last pmsg file data and place it
  19. // into /data/misc/recovery/ directory, rotating it in.
  20. //
  21. // Usage: recovery-persist [--force-persist]
  22. //
  23. // On systems without /cache mount, all file content representing in the
  24. // recovery/ directory stored in /sys/fs/pstore/pmsg-ramoops-0 in logger
  25. // format that reside in the LOG_ID_SYSTEM buffer at ANDROID_LOG_INFO
  26. // priority or higher is transfered to the /data/misc/recovery/ directory.
  27. // The content is matched and rotated in as need be.
  28. //
  29. // --force-persist ignore /cache mount, always rotate in the contents.
  30. //
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <string>
  36. #include <android-base/file.h>
  37. #include <android-base/logging.h>
  38. #include <private/android_logger.h> /* private pmsg functions */
  39. #include "rotate_logs.h"
  40. static const char *LAST_LOG_FILE = "/data/misc/recovery/last_log";
  41. static const char *LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0";
  42. static const char *LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg";
  43. static const char *LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0";
  44. static const char *ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops";
  45. // close a file, log an error if the error indicator is set
  46. static void check_and_fclose(FILE *fp, const char *name) {
  47. fflush(fp);
  48. if (ferror(fp)) {
  49. PLOG(ERROR) << "Error in " << name;
  50. }
  51. fclose(fp);
  52. }
  53. static void copy_file(const char* source, const char* destination) {
  54. FILE* dest_fp = fopen(destination, "we");
  55. if (dest_fp == nullptr) {
  56. PLOG(ERROR) << "Can't open " << destination;
  57. } else {
  58. FILE* source_fp = fopen(source, "re");
  59. if (source_fp != nullptr) {
  60. char buf[4096];
  61. size_t bytes;
  62. while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) {
  63. fwrite(buf, 1, bytes, dest_fp);
  64. }
  65. check_and_fclose(source_fp, source);
  66. }
  67. check_and_fclose(dest_fp, destination);
  68. }
  69. }
  70. static bool rotated = false;
  71. ssize_t logsave(
  72. log_id_t /* logId */,
  73. char /* prio */,
  74. const char *filename,
  75. const char *buf, size_t len,
  76. void * /* arg */) {
  77. std::string destination("/data/misc/");
  78. destination += filename;
  79. std::string buffer(buf, len);
  80. {
  81. std::string content;
  82. android::base::ReadFileToString(destination, &content);
  83. if (buffer.compare(content) == 0) {
  84. return len;
  85. }
  86. }
  87. // ToDo: Any others that match? Are we pulling in multiple
  88. // already-rotated files? Algorithm thus far is KISS: one file,
  89. // one rotation allowed.
  90. rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE);
  91. rotated = true;
  92. return android::base::WriteStringToFile(buffer, destination.c_str());
  93. }
  94. int main(int argc, char **argv) {
  95. /* Is /cache a mount?, we have been delivered where we are not wanted */
  96. /*
  97. * Following code halves the size of the executable as compared to:
  98. *
  99. * load_volume_table();
  100. * has_cache = volume_for_path(CACHE_ROOT) != nullptr;
  101. */
  102. bool has_cache = false;
  103. static const char mounts_file[] = "/proc/mounts";
  104. FILE* fp = fopen(mounts_file, "re");
  105. if (!fp) {
  106. PLOG(ERROR) << "failed to open " << mounts_file;
  107. } else {
  108. char *line = NULL;
  109. size_t len = 0;
  110. ssize_t read;
  111. while ((read = getline(&line, &len, fp)) != -1) {
  112. if (strstr(line, " /cache ")) {
  113. has_cache = true;
  114. break;
  115. }
  116. }
  117. free(line);
  118. fclose(fp);
  119. }
  120. if (has_cache) {
  121. /*
  122. * TBD: Future location to move content from
  123. * /cache/recovery to /data/misc/recovery/
  124. */
  125. /* if --force-persist flag, then transfer pmsg data anyways */
  126. if ((argc <= 1) || !argv[1] || strcmp(argv[1], "--force-persist")) {
  127. return 0;
  128. }
  129. }
  130. /* Is there something in pmsg? */
  131. if (access(LAST_PMSG_FILE, R_OK)) {
  132. return 0;
  133. }
  134. // Take last pmsg file contents and send it off to the logsave
  135. __android_log_pmsg_file_read(
  136. LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", logsave, NULL);
  137. /* Is there a last console log too? */
  138. if (rotated) {
  139. if (!access(LAST_CONSOLE_FILE, R_OK)) {
  140. copy_file(LAST_CONSOLE_FILE, LAST_KMSG_FILE);
  141. } else if (!access(ALT_LAST_CONSOLE_FILE, R_OK)) {
  142. copy_file(ALT_LAST_CONSOLE_FILE, LAST_KMSG_FILE);
  143. }
  144. }
  145. return 0;
  146. }