main.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // Copyright (C) 2012 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. #include <inttypes.h>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #include <unistd.h>
  20. #include <xz.h>
  21. #include <algorithm>
  22. #include <string>
  23. #include <vector>
  24. #include <base/at_exit.h>
  25. #include <base/command_line.h>
  26. #include <base/files/dir_reader_posix.h>
  27. #include <base/files/file_util.h>
  28. #include <base/logging.h>
  29. #include <base/strings/string_util.h>
  30. #include <base/strings/stringprintf.h>
  31. #include <brillo/flag_helper.h>
  32. #include "update_engine/common/terminator.h"
  33. #include "update_engine/common/utils.h"
  34. #include "update_engine/daemon.h"
  35. using std::string;
  36. namespace chromeos_update_engine {
  37. namespace {
  38. string GetTimeAsString(time_t utime) {
  39. struct tm tm;
  40. CHECK_EQ(localtime_r(&utime, &tm), &tm);
  41. char str[16];
  42. CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u);
  43. return str;
  44. }
  45. #ifdef __ANDROID__
  46. constexpr char kSystemLogsRoot[] = "/data/misc/update_engine_log";
  47. constexpr size_t kLogCount = 5;
  48. // Keep the most recent |kLogCount| logs but remove the old ones in
  49. // "/data/misc/update_engine_log/".
  50. void DeleteOldLogs(const string& kLogsRoot) {
  51. base::DirReaderPosix reader(kLogsRoot.c_str());
  52. if (!reader.IsValid()) {
  53. LOG(ERROR) << "Failed to read " << kLogsRoot;
  54. return;
  55. }
  56. std::vector<string> old_logs;
  57. while (reader.Next()) {
  58. if (reader.name()[0] == '.')
  59. continue;
  60. // Log files are in format "update_engine.%Y%m%d-%H%M%S",
  61. // e.g. update_engine.20090103-231425
  62. uint64_t date;
  63. uint64_t local_time;
  64. if (sscanf(reader.name(),
  65. "update_engine.%" PRIu64 "-%" PRIu64 "",
  66. &date,
  67. &local_time) == 2) {
  68. old_logs.push_back(reader.name());
  69. } else {
  70. LOG(WARNING) << "Unrecognized log file " << reader.name();
  71. }
  72. }
  73. std::sort(old_logs.begin(), old_logs.end(), std::greater<string>());
  74. for (size_t i = kLogCount; i < old_logs.size(); i++) {
  75. string log_path = kLogsRoot + "/" + old_logs[i];
  76. if (unlink(log_path.c_str()) == -1) {
  77. PLOG(WARNING) << "Failed to unlink " << log_path;
  78. }
  79. }
  80. }
  81. string SetupLogFile(const string& kLogsRoot) {
  82. DeleteOldLogs(kLogsRoot);
  83. return base::StringPrintf("%s/update_engine.%s",
  84. kLogsRoot.c_str(),
  85. GetTimeAsString(::time(nullptr)).c_str());
  86. }
  87. #else
  88. constexpr char kSystemLogsRoot[] = "/var/log";
  89. void SetupLogSymlink(const string& symlink_path, const string& log_path) {
  90. // TODO(petkov): To ensure a smooth transition between non-timestamped and
  91. // timestamped logs, move an existing log to start the first timestamped
  92. // one. This code can go away once all clients are switched to this version or
  93. // we stop caring about the old-style logs.
  94. if (utils::FileExists(symlink_path.c_str()) &&
  95. !utils::IsSymlink(symlink_path.c_str())) {
  96. base::ReplaceFile(
  97. base::FilePath(symlink_path), base::FilePath(log_path), nullptr);
  98. }
  99. base::DeleteFile(base::FilePath(symlink_path), true);
  100. if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
  101. PLOG(ERROR) << "Unable to create symlink " << symlink_path
  102. << " pointing at " << log_path;
  103. }
  104. }
  105. string SetupLogFile(const string& kLogsRoot) {
  106. const string kLogSymlink = kLogsRoot + "/update_engine.log";
  107. const string kLogsDir = kLogsRoot + "/update_engine";
  108. const string kLogPath =
  109. base::StringPrintf("%s/update_engine.%s",
  110. kLogsDir.c_str(),
  111. GetTimeAsString(::time(nullptr)).c_str());
  112. mkdir(kLogsDir.c_str(), 0755);
  113. SetupLogSymlink(kLogSymlink, kLogPath);
  114. return kLogSymlink;
  115. }
  116. #endif // __ANDROID__
  117. void SetupLogging(bool log_to_system, bool log_to_file) {
  118. logging::LoggingSettings log_settings;
  119. log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
  120. log_settings.logging_dest = static_cast<logging::LoggingDestination>(
  121. (log_to_system ? logging::LOG_TO_SYSTEM_DEBUG_LOG : 0) |
  122. (log_to_file ? logging::LOG_TO_FILE : 0));
  123. log_settings.log_file = nullptr;
  124. string log_file;
  125. if (log_to_file) {
  126. log_file = SetupLogFile(kSystemLogsRoot);
  127. log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
  128. log_settings.log_file = log_file.c_str();
  129. }
  130. logging::InitLogging(log_settings);
  131. #ifdef __ANDROID__
  132. // The log file will have AID_LOG as group ID; this GID is inherited from the
  133. // parent directory "/data/misc/update_engine_log" which sets the SGID bit.
  134. chmod(log_file.c_str(), 0640);
  135. #endif
  136. }
  137. } // namespace
  138. } // namespace chromeos_update_engine
  139. int main(int argc, char** argv) {
  140. DEFINE_bool(logtofile, false, "Write logs to a file in log_dir.");
  141. DEFINE_bool(logtostderr,
  142. false,
  143. "Write logs to stderr instead of to a file in log_dir.");
  144. DEFINE_bool(foreground, false, "Don't daemon()ize; run in foreground.");
  145. chromeos_update_engine::Terminator::Init();
  146. brillo::FlagHelper::Init(argc, argv, "A/B Update Engine");
  147. // We have two logging flags "--logtostderr" and "--logtofile"; and the logic
  148. // to choose the logging destination is:
  149. // 1. --logtostderr --logtofile -> logs to both
  150. // 2. --logtostderr -> logs to system debug
  151. // 3. --logtofile or no flags -> logs to file
  152. bool log_to_system = FLAGS_logtostderr;
  153. bool log_to_file = FLAGS_logtofile || !FLAGS_logtostderr;
  154. chromeos_update_engine::SetupLogging(log_to_system, log_to_file);
  155. if (!FLAGS_foreground)
  156. PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
  157. LOG(INFO) << "A/B Update Engine starting";
  158. // xz-embedded requires to initialize its CRC-32 table once on startup.
  159. xz_crc32_init();
  160. // Ensure that all written files have safe permissions.
  161. // This is a mask, so we _block_ all permissions for the group owner and other
  162. // users but allow all permissions for the user owner. We allow execution
  163. // for the owner so we can create directories.
  164. // Done _after_ log file creation.
  165. umask(S_IRWXG | S_IRWXO);
  166. chromeos_update_engine::UpdateEngineDaemon update_engine_daemon;
  167. int exit_code = update_engine_daemon.Run();
  168. chromeos_update_engine::Subprocess::Get().FlushBufferedLogsAtExit();
  169. LOG(INFO) << "A/B Update Engine terminating with exit code " << exit_code;
  170. return exit_code;
  171. }