blob: 26f9efbb15f0504a9d44348788ae9b74d1f8a97f [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Tianjie Xuc08140b2018-01-19 14:31:31 -080017#include <inttypes.h>
Alex Deymo2e71f902015-09-30 01:25:48 -070018#include <sys/stat.h>
19#include <sys/types.h>
Alex Deymo67363ee2014-09-23 23:07:44 -070020#include <unistd.h>
Alex Deymo2e71f902015-09-30 01:25:48 -070021#include <xz.h>
Alex Deymo67363ee2014-09-23 23:07:44 -070022
Tianjie Xuc08140b2018-01-19 14:31:31 -080023#include <algorithm>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080024#include <string>
Tianjie Xuc08140b2018-01-19 14:31:31 -080025#include <vector>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070026
Darin Petkov1023a602010-08-30 13:47:51 -070027#include <base/at_exit.h>
28#include <base/command_line.h>
Tianjie Xuc08140b2018-01-19 14:31:31 -080029#include <base/files/dir_reader_posix.h>
Ben Chan06c76a42014-09-05 08:21:06 -070030#include <base/files/file_util.h>
Darin Petkov1023a602010-08-30 13:47:51 -070031#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070032#include <base/strings/string_util.h>
33#include <base/strings/stringprintf.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070034#include <brillo/flag_helper.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070035
Alex Deymo39910dc2015-11-09 17:04:30 -080036#include "update_engine/common/terminator.h"
37#include "update_engine/common/utils.h"
Alex Deymob7ca0962014-10-01 17:58:07 -070038#include "update_engine/daemon.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070039
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080040using std::string;
Alex Deymo67363ee2014-09-23 23:07:44 -070041
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080042namespace chromeos_update_engine {
Andrew de los Reyes73520672010-05-10 13:44:58 -070043namespace {
44
Tianjie Xuc08140b2018-01-19 14:31:31 -080045string GetTimeAsString(time_t utime) {
46 struct tm tm;
47 CHECK_EQ(localtime_r(&utime, &tm), &tm);
48 char str[16];
49 CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u);
50 return str;
51}
52
53#ifdef __ANDROID__
54constexpr char kSystemLogsRoot[] = "/data/misc/update_engine_log";
55constexpr size_t kLogCount = 5;
56
57// Keep the most recent |kLogCount| logs but remove the old ones in
58// "/data/misc/update_engine_log/".
59void DeleteOldLogs(const string& kLogsRoot) {
60 base::DirReaderPosix reader(kLogsRoot.c_str());
61 if (!reader.IsValid()) {
62 LOG(ERROR) << "Failed to read " << kLogsRoot;
63 return;
64 }
65
66 std::vector<string> old_logs;
67 while (reader.Next()) {
68 if (reader.name()[0] == '.')
69 continue;
70
71 // Log files are in format "update_engine.%Y%m%d-%H%M%S",
72 // e.g. update_engine.20090103-231425
73 uint64_t date;
74 uint64_t local_time;
75 if (sscanf(reader.name(),
76 "update_engine.%" PRIu64 "-%" PRIu64 "",
77 &date,
78 &local_time) == 2) {
79 old_logs.push_back(reader.name());
80 } else {
81 LOG(WARNING) << "Unrecognized log file " << reader.name();
82 }
83 }
84
85 std::sort(old_logs.begin(), old_logs.end(), std::greater<string>());
86 for (size_t i = kLogCount; i < old_logs.size(); i++) {
87 string log_path = kLogsRoot + "/" + old_logs[i];
88 if (unlink(log_path.c_str()) == -1) {
89 PLOG(WARNING) << "Failed to unlink " << log_path;
90 }
91 }
92}
93
94string SetupLogFile(const string& kLogsRoot) {
95 DeleteOldLogs(kLogsRoot);
96
97 return base::StringPrintf("%s/update_engine.%s",
98 kLogsRoot.c_str(),
99 GetTimeAsString(::time(nullptr)).c_str());
100}
101#else
102constexpr char kSystemLogsRoot[] = "/var/log";
103
Darin Petkov30291ed2010-11-12 10:23:06 -0800104void SetupLogSymlink(const string& symlink_path, const string& log_path) {
105 // TODO(petkov): To ensure a smooth transition between non-timestamped and
106 // timestamped logs, move an existing log to start the first timestamped
107 // one. This code can go away once all clients are switched to this version or
108 // we stop caring about the old-style logs.
109 if (utils::FileExists(symlink_path.c_str()) &&
110 !utils::IsSymlink(symlink_path.c_str())) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800111 base::ReplaceFile(
112 base::FilePath(symlink_path), base::FilePath(log_path), nullptr);
Darin Petkov30291ed2010-11-12 10:23:06 -0800113 }
Alex Vakulenko75039d72014-03-25 12:36:28 -0700114 base::DeleteFile(base::FilePath(symlink_path), true);
Darin Petkov30291ed2010-11-12 10:23:06 -0800115 if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
116 PLOG(ERROR) << "Unable to create symlink " << symlink_path
117 << " pointing at " << log_path;
118 }
119}
120
Darin Petkov30291ed2010-11-12 10:23:06 -0800121string SetupLogFile(const string& kLogsRoot) {
122 const string kLogSymlink = kLogsRoot + "/update_engine.log";
123 const string kLogsDir = kLogsRoot + "/update_engine";
124 const string kLogPath =
Alex Vakulenko75039d72014-03-25 12:36:28 -0700125 base::StringPrintf("%s/update_engine.%s",
126 kLogsDir.c_str(),
127 GetTimeAsString(::time(nullptr)).c_str());
Darin Petkov30291ed2010-11-12 10:23:06 -0800128 mkdir(kLogsDir.c_str(), 0755);
129 SetupLogSymlink(kLogSymlink, kLogPath);
130 return kLogSymlink;
131}
Tianjie Xu6c863b72017-09-17 15:44:51 -0700132#endif // __ANDROID__
Darin Petkov30291ed2010-11-12 10:23:06 -0800133
Tianjie Xu6c863b72017-09-17 15:44:51 -0700134void SetupLogging(bool log_to_system, bool log_to_file) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700135 logging::LoggingSettings log_settings;
136 log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
Tianjie Xu6c863b72017-09-17 15:44:51 -0700137 log_settings.logging_dest = static_cast<logging::LoggingDestination>(
138 (log_to_system ? logging::LOG_TO_SYSTEM_DEBUG_LOG : 0) |
139 (log_to_file ? logging::LOG_TO_FILE : 0));
140 log_settings.log_file = nullptr;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700141
Tianjie Xu6c863b72017-09-17 15:44:51 -0700142 string log_file;
143 if (log_to_file) {
Tianjie Xuc08140b2018-01-19 14:31:31 -0800144 log_file = SetupLogFile(kSystemLogsRoot);
Tianjie Xu6c863b72017-09-17 15:44:51 -0700145 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700146 log_settings.log_file = log_file.c_str();
Darin Petkov30291ed2010-11-12 10:23:06 -0800147 }
Alex Vakulenko75039d72014-03-25 12:36:28 -0700148 logging::InitLogging(log_settings);
Tianjie Xu1e2750a2017-11-02 15:34:18 -0700149
150#ifdef __ANDROID__
151 // The log file will have AID_LOG as group ID; this GID is inherited from the
152 // parent directory "/data/misc/update_engine_log" which sets the SGID bit.
153 chmod(log_file.c_str(), 0640);
154#endif
Darin Petkov30291ed2010-11-12 10:23:06 -0800155}
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800156
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700157} // namespace
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800158} // namespace chromeos_update_engine
rspangler@google.com49fdf182009-10-10 00:57:34 +0000159
rspangler@google.com49fdf182009-10-10 00:57:34 +0000160int main(int argc, char** argv) {
Tianjie Xu6c863b72017-09-17 15:44:51 -0700161 DEFINE_bool(logtofile, false, "Write logs to a file in log_dir.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800162 DEFINE_bool(logtostderr,
163 false,
Steve Fungd5708202014-09-28 23:24:06 -0700164 "Write logs to stderr instead of to a file in log_dir.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800165 DEFINE_bool(foreground, false, "Don't daemon()ize; run in foreground.");
Steve Fungd5708202014-09-28 23:24:06 -0700166
Darin Petkov9c0baf82010-10-07 13:44:48 -0700167 chromeos_update_engine::Terminator::Init();
Sen Jiang49a08972017-07-26 15:54:18 -0700168 brillo::FlagHelper::Init(argc, argv, "A/B Update Engine");
Tianjie Xu6c863b72017-09-17 15:44:51 -0700169
170 // We have two logging flags "--logtostderr" and "--logtofile"; and the logic
171 // to choose the logging destination is:
172 // 1. --logtostderr --logtofile -> logs to both
173 // 2. --logtostderr -> logs to system debug
174 // 3. --logtofile or no flags -> logs to file
175 bool log_to_system = FLAGS_logtostderr;
176 bool log_to_file = FLAGS_logtofile || !FLAGS_logtostderr;
177 chromeos_update_engine::SetupLogging(log_to_system, log_to_file);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700178 if (!FLAGS_foreground)
179 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
180
Sen Jiang49a08972017-07-26 15:54:18 -0700181 LOG(INFO) << "A/B Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700182
Alex Deymo2e71f902015-09-30 01:25:48 -0700183 // xz-embedded requires to initialize its CRC-32 table once on startup.
184 xz_crc32_init();
185
Chris Masone4dc2ada2010-09-23 12:43:03 -0700186 // Ensure that all written files have safe permissions.
Alex Deymoa9fea842015-09-17 13:53:29 -0700187 // This is a mask, so we _block_ all permissions for the group owner and other
188 // users but allow all permissions for the user owner. We allow execution
189 // for the owner so we can create directories.
Chris Masone4dc2ada2010-09-23 12:43:03 -0700190 // Done _after_ log file creation.
Alex Deymoa9fea842015-09-17 13:53:29 -0700191 umask(S_IRWXG | S_IRWXO);
Chris Masone4dc2ada2010-09-23 12:43:03 -0700192
Alex Deymob7ca0962014-10-01 17:58:07 -0700193 chromeos_update_engine::UpdateEngineDaemon update_engine_daemon;
194 int exit_code = update_engine_daemon.Run();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800195
Amin Hassania8859542018-03-07 16:24:43 -0800196 chromeos_update_engine::Subprocess::Get().FlushBufferedLogsAtExit();
197
Sen Jiang49a08972017-07-26 15:54:18 -0700198 LOG(INFO) << "A/B Update Engine terminating with exit code " << exit_code;
Alex Deymob7ca0962014-10-01 17:58:07 -0700199 return exit_code;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000200}