| // Copyright 2012 The ChromiumOS Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include <sys/stat.h> | |
| #include <sys/types.h> | |
| #include <xz.h> | |
| #include <base/at_exit.h> | |
| #include <base/command_line.h> | |
| #include <base/logging.h> | |
| #include <brillo/flag_helper.h> | |
| #include "update_engine/common/daemon_base.h" | |
| #include "update_engine/common/logging.h" | |
| #include "update_engine/common/subprocess.h" | |
| #include "update_engine/common/terminator.h" | |
| #include "update_engine/common/utils.h" | |
| using std::string; | |
| int main(int argc, char** argv) { | |
| DEFINE_bool(logtofile, false, "Write logs to a file in log_dir."); | |
| DEFINE_bool(logtostderr, false, | |
| "Write logs to stderr instead of to a file in log_dir."); | |
| DEFINE_bool(foreground, false, "Don't daemon()ize; run in foreground."); | |
| chromeos_update_engine::Terminator::Init(); | |
| brillo::FlagHelper::Init(argc, argv, "A/B Update Engine"); | |
| // We have two logging flags "--logtostderr" and "--logtofile"; and the logic | |
| // to choose the logging destination is: | |
| // 1. --logtostderr --logtofile -> logs to both | |
| // 2. --logtostderr -> logs to system debug | |
| // 3. --logtofile or no flags -> logs to file | |
| bool log_to_system = FLAGS_logtostderr; | |
| bool log_to_file = FLAGS_logtofile || !FLAGS_logtostderr; | |
| chromeos_update_engine::SetupLogging(log_to_system, log_to_file); | |
| if (!FLAGS_foreground) | |
| PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed"; | |
| LOG(INFO) << "A/B Update Engine starting"; | |
| // xz-embedded requires to initialize its CRC-32 table once on startup. | |
| xz_crc32_init(); | |
| // Ensure that all written files have safe permissions. | |
| // This is a mask, so we _block_ all permissions for the group owner and other | |
| // users but allow all permissions for the user owner. We allow execution | |
| // for the owner so we can create directories. | |
| // Done _after_ log file creation. | |
| umask(S_IRWXG | S_IRWXO); | |
| auto daemon = chromeos_update_engine::DaemonBase::CreateInstance(); | |
| int exit_code = daemon->Run(); | |
| chromeos_update_engine::Subprocess::Get().FlushBufferedLogsAtExit(); | |
| LOG(INFO) << "A/B Update Engine terminating with exit code " << exit_code; | |
| return exit_code; | |
| } |