diff options
author | Amin Hassani <ahassani@chromium.org> | 2020-09-16 11:19:28 -0700 |
---|---|---|
committer | Amin Hassani <ahassani@chromium.org> | 2020-09-17 10:17:36 -0700 |
commit | e53b39b8b9c5c0871841bbbb86f23657c0b7f91b (patch) | |
tree | 12746e5e7e4716ba7fc94d3ed5c1a36c4d47d474 /main.cc | |
parent | a02a1f1dc837f22226499d9856a949fb180d099a (diff) | |
parent | 9956320ffa4edb340d20bd7f3c852a9e87437bd3 (diff) |
update_engine: Merge remote-tracking branch 'cros/upstream' into cros/master
Done with:
git merge cros/upstream --commit -s recursive
- Added EC key support and its unittests.
- Resolved a conlict on error codes. Since Android versions are not
uploading any UMA metrics, I gave the priority to the Android version
Since they can't be changed.
- Changed the openssl functions to get1 version (from get0) version
because of a current issue with gale. Once the issue is resolved we
need to change them back.
- Some remaining styling issues fixed by clang-format
BUG=b:163153182
TEST=CQ passes
TEST=unittests
Change-Id: Ib95034422b92433ce26e28336bc4806b34910d38
Diffstat (limited to 'main.cc')
-rw-r--r-- | main.cc | 133 |
1 files changed, 1 insertions, 132 deletions
@@ -14,154 +14,23 @@ // limitations under the License. // -#include <inttypes.h> #include <sys/stat.h> #include <sys/types.h> -#include <unistd.h> #include <xz.h> -#include <algorithm> -#include <string> -#include <vector> - #include <base/at_exit.h> #include <base/command_line.h> -#include <base/files/dir_reader_posix.h> -#include <base/files/file_util.h> #include <base/logging.h> -#include <base/strings/string_util.h> -#include <base/strings/stringprintf.h> #include <brillo/flag_helper.h> #include "update_engine/common/subprocess.h" #include "update_engine/common/terminator.h" #include "update_engine/common/utils.h" #include "update_engine/daemon_base.h" +#include "update_engine/logging.h" using std::string; -namespace chromeos_update_engine { -namespace { - -string GetTimeAsString(time_t utime) { - struct tm tm; - CHECK_EQ(localtime_r(&utime, &tm), &tm); - char str[16]; - CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u); - return str; -} - -#ifdef __ANDROID__ -constexpr char kSystemLogsRoot[] = "/data/misc/update_engine_log"; -constexpr size_t kLogCount = 5; - -// Keep the most recent |kLogCount| logs but remove the old ones in -// "/data/misc/update_engine_log/". -void DeleteOldLogs(const string& kLogsRoot) { - base::DirReaderPosix reader(kLogsRoot.c_str()); - if (!reader.IsValid()) { - LOG(ERROR) << "Failed to read " << kLogsRoot; - return; - } - - std::vector<string> old_logs; - while (reader.Next()) { - if (reader.name()[0] == '.') - continue; - - // Log files are in format "update_engine.%Y%m%d-%H%M%S", - // e.g. update_engine.20090103-231425 - uint64_t date; - uint64_t local_time; - if (sscanf(reader.name(), - "update_engine.%" PRIu64 "-%" PRIu64 "", - &date, - &local_time) == 2) { - old_logs.push_back(reader.name()); - } else { - LOG(WARNING) << "Unrecognized log file " << reader.name(); - } - } - - std::sort(old_logs.begin(), old_logs.end(), std::greater<string>()); - for (size_t i = kLogCount; i < old_logs.size(); i++) { - string log_path = kLogsRoot + "/" + old_logs[i]; - if (unlink(log_path.c_str()) == -1) { - PLOG(WARNING) << "Failed to unlink " << log_path; - } - } -} - -string SetupLogFile(const string& kLogsRoot) { - DeleteOldLogs(kLogsRoot); - - return base::StringPrintf("%s/update_engine.%s", - kLogsRoot.c_str(), - GetTimeAsString(::time(nullptr)).c_str()); -} -#else -constexpr char kSystemLogsRoot[] = "/var/log"; - -void SetupLogSymlink(const string& symlink_path, const string& log_path) { - // TODO(petkov): To ensure a smooth transition between non-timestamped and - // timestamped logs, move an existing log to start the first timestamped - // one. This code can go away once all clients are switched to this version or - // we stop caring about the old-style logs. - if (utils::FileExists(symlink_path.c_str()) && - !utils::IsSymlink(symlink_path.c_str())) { - base::ReplaceFile( - base::FilePath(symlink_path), base::FilePath(log_path), nullptr); - } - base::DeleteFile(base::FilePath(symlink_path), true); - if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) { - PLOG(ERROR) << "Unable to create symlink " << symlink_path - << " pointing at " << log_path; - } -} - -string SetupLogFile(const string& kLogsRoot) { - const string kLogSymlink = kLogsRoot + "/update_engine.log"; - const string kLogsDir = kLogsRoot + "/update_engine"; - const string kLogPath = - base::StringPrintf("%s/update_engine.%s", - kLogsDir.c_str(), - GetTimeAsString(::time(nullptr)).c_str()); - mkdir(kLogsDir.c_str(), 0755); - SetupLogSymlink(kLogSymlink, kLogPath); - return kLogSymlink; -} -#endif // __ANDROID__ - -void SetupLogging(bool log_to_system, bool log_to_file) { - logging::LoggingSettings log_settings; - log_settings.lock_log = logging::DONT_LOCK_LOG_FILE; - log_settings.logging_dest = static_cast<logging::LoggingDestination>( - (log_to_system ? logging::LOG_TO_SYSTEM_DEBUG_LOG : 0) | - (log_to_file ? logging::LOG_TO_FILE : 0)); - log_settings.log_file = nullptr; - - string log_file; - if (log_to_file) { - log_file = SetupLogFile(kSystemLogsRoot); - log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE; -#if BASE_VER < 780000 - log_settings.log_file = log_file.c_str(); -#else - log_settings.log_file_path = log_file.c_str(); -#endif - } - logging::InitLogging(log_settings); - -#ifdef __ANDROID__ - // The log file will have AID_LOG as group ID; this GID is inherited from the - // parent directory "/data/misc/update_engine_log" which sets the SGID bit. - chmod(log_file.c_str(), 0640); -#endif -} - -} // namespace -} // namespace chromeos_update_engine - int main(int argc, char** argv) { DEFINE_bool(logtofile, false, "Write logs to a file in log_dir."); DEFINE_bool(logtostderr, |