blob: 103e1a10b12ee0c1467dcadd06e9d27d44573f69 [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>
Kelvin Zhang77cb1c62023-03-31 20:34:52 -070024#include <gflags/gflags.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"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070030
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080031using std::string;
Kelvin Zhang77cb1c62023-03-31 20:34:52 -070032DEFINE_bool(logtofile, false, "Write logs to a file in log_dir.");
33DEFINE_bool(logtostderr,
34 false,
35 "Write logs to stderr instead of to a file in log_dir.");
36DEFINE_bool(foreground, false, "Don't daemon()ize; run in foreground.");
Alex Deymo67363ee2014-09-23 23:07:44 -070037
rspangler@google.com49fdf182009-10-10 00:57:34 +000038int main(int argc, char** argv) {
Darin Petkov9c0baf82010-10-07 13:44:48 -070039 chromeos_update_engine::Terminator::Init();
Kelvin Zhang77cb1c62023-03-31 20:34:52 -070040 gflags::SetUsageMessage("A/B Update Engine");
41 gflags::ParseCommandLineFlags(&argc, &argv, true);
Tianjie Xu6c863b72017-09-17 15:44:51 -070042
43 // We have two logging flags "--logtostderr" and "--logtofile"; and the logic
44 // to choose the logging destination is:
45 // 1. --logtostderr --logtofile -> logs to both
46 // 2. --logtostderr -> logs to system debug
47 // 3. --logtofile or no flags -> logs to file
48 bool log_to_system = FLAGS_logtostderr;
49 bool log_to_file = FLAGS_logtofile || !FLAGS_logtostderr;
50 chromeos_update_engine::SetupLogging(log_to_system, log_to_file);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070051 if (!FLAGS_foreground)
52 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
53
Sen Jiang49a08972017-07-26 15:54:18 -070054 LOG(INFO) << "A/B Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -070055
Alex Deymo2e71f902015-09-30 01:25:48 -070056 // xz-embedded requires to initialize its CRC-32 table once on startup.
57 xz_crc32_init();
58
Chris Masone4dc2ada2010-09-23 12:43:03 -070059 // Ensure that all written files have safe permissions.
Alex Deymoa9fea842015-09-17 13:53:29 -070060 // This is a mask, so we _block_ all permissions for the group owner and other
61 // users but allow all permissions for the user owner. We allow execution
62 // for the owner so we can create directories.
Chris Masone4dc2ada2010-09-23 12:43:03 -070063 // Done _after_ log file creation.
Alex Deymoa9fea842015-09-17 13:53:29 -070064 umask(S_IRWXG | S_IRWXO);
Chris Masone4dc2ada2010-09-23 12:43:03 -070065
Amin Hassani565331e2019-06-24 14:11:29 -070066 auto daemon = chromeos_update_engine::DaemonBase::CreateInstance();
67 int exit_code = daemon->Run();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080068
Amin Hassania8859542018-03-07 16:24:43 -080069 chromeos_update_engine::Subprocess::Get().FlushBufferedLogsAtExit();
70
Sen Jiang49a08972017-07-26 15:54:18 -070071 LOG(INFO) << "A/B Update Engine terminating with exit code " << exit_code;
Alex Deymob7ca0962014-10-01 17:58:07 -070072 return exit_code;
rspangler@google.com49fdf182009-10-10 00:57:34 +000073}