blob: a23a08bf065377f2212df26ad80ed26a2575b87d [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
Alex Deymo2e71f902015-09-30 01:25:48 -070017#include <sys/stat.h>
18#include <sys/types.h>
Alex Deymo2e71f902015-09-30 01:25:48 -070019#include <xz.h>
Alex Deymo67363ee2014-09-23 23:07:44 -070020
Darin Petkov1023a602010-08-30 13:47:51 -070021#include <base/at_exit.h>
22#include <base/command_line.h>
23#include <base/logging.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070024#include <brillo/flag_helper.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070025
Amin Hassaniec7bc112020-10-29 16:47:58 -070026#include "update_engine/common/daemon_base.h"
27#include "update_engine/common/logging.h"
Amin Hassani565331e2019-06-24 14:11:29 -070028#include "update_engine/common/subprocess.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/terminator.h"
30#include "update_engine/common/utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070031
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080032using std::string;
Alex Deymo67363ee2014-09-23 23:07:44 -070033
rspangler@google.com49fdf182009-10-10 00:57:34 +000034int main(int argc, char** argv) {
Tianjie Xu6c863b72017-09-17 15:44:51 -070035 DEFINE_bool(logtofile, false, "Write logs to a file in log_dir.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -080036 DEFINE_bool(logtostderr,
37 false,
Steve Fungd5708202014-09-28 23:24:06 -070038 "Write logs to stderr instead of to a file in log_dir.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -080039 DEFINE_bool(foreground, false, "Don't daemon()ize; run in foreground.");
Steve Fungd5708202014-09-28 23:24:06 -070040
Darin Petkov9c0baf82010-10-07 13:44:48 -070041 chromeos_update_engine::Terminator::Init();
Sen Jiang49a08972017-07-26 15:54:18 -070042 brillo::FlagHelper::Init(argc, argv, "A/B Update Engine");
Tianjie Xu6c863b72017-09-17 15:44:51 -070043
44 // We have two logging flags "--logtostderr" and "--logtofile"; and the logic
45 // to choose the logging destination is:
46 // 1. --logtostderr --logtofile -> logs to both
47 // 2. --logtostderr -> logs to system debug
48 // 3. --logtofile or no flags -> logs to file
49 bool log_to_system = FLAGS_logtostderr;
50 bool log_to_file = FLAGS_logtofile || !FLAGS_logtostderr;
51 chromeos_update_engine::SetupLogging(log_to_system, log_to_file);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070052 if (!FLAGS_foreground)
53 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
54
Sen Jiang49a08972017-07-26 15:54:18 -070055 LOG(INFO) << "A/B Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -070056
Alex Deymo2e71f902015-09-30 01:25:48 -070057 // xz-embedded requires to initialize its CRC-32 table once on startup.
58 xz_crc32_init();
59
Chris Masone4dc2ada2010-09-23 12:43:03 -070060 // Ensure that all written files have safe permissions.
Alex Deymoa9fea842015-09-17 13:53:29 -070061 // This is a mask, so we _block_ all permissions for the group owner and other
62 // users but allow all permissions for the user owner. We allow execution
63 // for the owner so we can create directories.
Chris Masone4dc2ada2010-09-23 12:43:03 -070064 // Done _after_ log file creation.
Alex Deymoa9fea842015-09-17 13:53:29 -070065 umask(S_IRWXG | S_IRWXO);
Chris Masone4dc2ada2010-09-23 12:43:03 -070066
Amin Hassani565331e2019-06-24 14:11:29 -070067 auto daemon = chromeos_update_engine::DaemonBase::CreateInstance();
68 int exit_code = daemon->Run();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080069
Amin Hassania8859542018-03-07 16:24:43 -080070 chromeos_update_engine::Subprocess::Get().FlushBufferedLogsAtExit();
71
Sen Jiang49a08972017-07-26 15:54:18 -070072 LOG(INFO) << "A/B Update Engine terminating with exit code " << exit_code;
Alex Deymob7ca0962014-10-01 17:58:07 -070073 return exit_code;
rspangler@google.com49fdf182009-10-10 00:57:34 +000074}