blob: ac2f5bcc9f14345b3d905da501d875968b3642fe [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
Kelvin Zhangfab31ad2024-03-26 12:56:08 -070017#include <stdlib.h>
Alex Deymo2e71f902015-09-30 01:25:48 -070018#include <sys/stat.h>
19#include <sys/types.h>
Alex Deymo2e71f902015-09-30 01:25:48 -070020#include <xz.h>
Alex Deymo67363ee2014-09-23 23:07:44 -070021
Darin Petkov1023a602010-08-30 13:47:51 -070022#include <base/at_exit.h>
23#include <base/command_line.h>
24#include <base/logging.h>
Kelvin Zhang77cb1c62023-03-31 20:34:52 -070025#include <gflags/gflags.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070026
Amin Hassaniec7bc112020-10-29 16:47:58 -070027#include "update_engine/common/daemon_base.h"
28#include "update_engine/common/logging.h"
Amin Hassani565331e2019-06-24 14:11:29 -070029#include "update_engine/common/subprocess.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/terminator.h"
Kelvin Zhangfab31ad2024-03-26 12:56:08 -070031#include "update_engine/common/utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070032
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080033using std::string;
Kelvin Zhang77cb1c62023-03-31 20:34:52 -070034DEFINE_bool(logtofile, false, "Write logs to a file in log_dir.");
35DEFINE_bool(logtostderr,
36 false,
37 "Write logs to stderr instead of to a file in log_dir.");
38DEFINE_bool(foreground, false, "Don't daemon()ize; run in foreground.");
Alex Deymo67363ee2014-09-23 23:07:44 -070039
rspangler@google.com49fdf182009-10-10 00:57:34 +000040int main(int argc, char** argv) {
Darin Petkov9c0baf82010-10-07 13:44:48 -070041 chromeos_update_engine::Terminator::Init();
Kelvin Zhang77cb1c62023-03-31 20:34:52 -070042 gflags::SetUsageMessage("A/B Update Engine");
43 gflags::ParseCommandLineFlags(&argc, &argv, true);
Tianjie Xu6c863b72017-09-17 15:44:51 -070044
45 // We have two logging flags "--logtostderr" and "--logtofile"; and the logic
46 // to choose the logging destination is:
47 // 1. --logtostderr --logtofile -> logs to both
48 // 2. --logtostderr -> logs to system debug
49 // 3. --logtofile or no flags -> logs to file
50 bool log_to_system = FLAGS_logtostderr;
51 bool log_to_file = FLAGS_logtofile || !FLAGS_logtostderr;
52 chromeos_update_engine::SetupLogging(log_to_system, log_to_file);
Kelvin Zhangfab31ad2024-03-26 12:56:08 -070053 base::FilePath tmpdir;
54 if (chromeos_update_engine::GetTempName("", &tmpdir)) {
55 LOG(INFO) << "Using temp dir " << tmpdir;
56 setenv("TMPDIR", tmpdir.value().c_str(), true);
57 } else {
58 PLOG(ERROR) << "Failed to create temporary directory, puffdiff will run "
59 "w/o on disk cache, updates might take longer.";
60 }
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070061 if (!FLAGS_foreground)
62 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
63
Sen Jiang49a08972017-07-26 15:54:18 -070064 LOG(INFO) << "A/B Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -070065
Alex Deymo2e71f902015-09-30 01:25:48 -070066 // xz-embedded requires to initialize its CRC-32 table once on startup.
67 xz_crc32_init();
68
Chris Masone4dc2ada2010-09-23 12:43:03 -070069 // Ensure that all written files have safe permissions.
Alex Deymoa9fea842015-09-17 13:53:29 -070070 // This is a mask, so we _block_ all permissions for the group owner and other
71 // users but allow all permissions for the user owner. We allow execution
72 // for the owner so we can create directories.
Chris Masone4dc2ada2010-09-23 12:43:03 -070073 // Done _after_ log file creation.
Alex Deymoa9fea842015-09-17 13:53:29 -070074 umask(S_IRWXG | S_IRWXO);
Chris Masone4dc2ada2010-09-23 12:43:03 -070075
Amin Hassani565331e2019-06-24 14:11:29 -070076 auto daemon = chromeos_update_engine::DaemonBase::CreateInstance();
77 int exit_code = daemon->Run();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080078
Amin Hassania8859542018-03-07 16:24:43 -080079 chromeos_update_engine::Subprocess::Get().FlushBufferedLogsAtExit();
80
Sen Jiang49a08972017-07-26 15:54:18 -070081 LOG(INFO) << "A/B Update Engine terminating with exit code " << exit_code;
Alex Deymob7ca0962014-10-01 17:58:07 -070082 return exit_code;
rspangler@google.com49fdf182009-10-10 00:57:34 +000083}