1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2012 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker
17*5a923131SAndroid Build Coastguard Worker #include <stdlib.h>
18*5a923131SAndroid Build Coastguard Worker #include <sys/stat.h>
19*5a923131SAndroid Build Coastguard Worker #include <sys/types.h>
20*5a923131SAndroid Build Coastguard Worker #include <xz.h>
21*5a923131SAndroid Build Coastguard Worker
22*5a923131SAndroid Build Coastguard Worker #include <base/at_exit.h>
23*5a923131SAndroid Build Coastguard Worker #include <base/command_line.h>
24*5a923131SAndroid Build Coastguard Worker #include <base/logging.h>
25*5a923131SAndroid Build Coastguard Worker #include <gflags/gflags.h>
26*5a923131SAndroid Build Coastguard Worker
27*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/daemon_base.h"
28*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/logging.h"
29*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/subprocess.h"
30*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/terminator.h"
31*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/utils.h"
32*5a923131SAndroid Build Coastguard Worker
33*5a923131SAndroid Build Coastguard Worker using std::string;
34*5a923131SAndroid Build Coastguard Worker DEFINE_bool(logtofile, false, "Write logs to a file in log_dir.");
35*5a923131SAndroid Build Coastguard Worker DEFINE_bool(logtostderr,
36*5a923131SAndroid Build Coastguard Worker false,
37*5a923131SAndroid Build Coastguard Worker "Write logs to stderr instead of to a file in log_dir.");
38*5a923131SAndroid Build Coastguard Worker DEFINE_bool(foreground, false, "Don't daemon()ize; run in foreground.");
39*5a923131SAndroid Build Coastguard Worker
main(int argc,char ** argv)40*5a923131SAndroid Build Coastguard Worker int main(int argc, char** argv) {
41*5a923131SAndroid Build Coastguard Worker chromeos_update_engine::Terminator::Init();
42*5a923131SAndroid Build Coastguard Worker gflags::SetUsageMessage("A/B Update Engine");
43*5a923131SAndroid Build Coastguard Worker gflags::ParseCommandLineFlags(&argc, &argv, true);
44*5a923131SAndroid Build Coastguard Worker
45*5a923131SAndroid Build Coastguard Worker // We have two logging flags "--logtostderr" and "--logtofile"; and the logic
46*5a923131SAndroid Build Coastguard Worker // to choose the logging destination is:
47*5a923131SAndroid Build Coastguard Worker // 1. --logtostderr --logtofile -> logs to both
48*5a923131SAndroid Build Coastguard Worker // 2. --logtostderr -> logs to system debug
49*5a923131SAndroid Build Coastguard Worker // 3. --logtofile or no flags -> logs to file
50*5a923131SAndroid Build Coastguard Worker bool log_to_system = FLAGS_logtostderr;
51*5a923131SAndroid Build Coastguard Worker bool log_to_file = FLAGS_logtofile || !FLAGS_logtostderr;
52*5a923131SAndroid Build Coastguard Worker chromeos_update_engine::SetupLogging(log_to_system, log_to_file);
53*5a923131SAndroid Build Coastguard Worker base::FilePath tmpdir;
54*5a923131SAndroid Build Coastguard Worker if (chromeos_update_engine::GetTempName("", &tmpdir)) {
55*5a923131SAndroid Build Coastguard Worker LOG(INFO) << "Using temp dir " << tmpdir;
56*5a923131SAndroid Build Coastguard Worker setenv("TMPDIR", tmpdir.value().c_str(), true);
57*5a923131SAndroid Build Coastguard Worker } else {
58*5a923131SAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to create temporary directory, puffdiff will run "
59*5a923131SAndroid Build Coastguard Worker "w/o on disk cache, updates might take longer.";
60*5a923131SAndroid Build Coastguard Worker }
61*5a923131SAndroid Build Coastguard Worker if (!FLAGS_foreground)
62*5a923131SAndroid Build Coastguard Worker PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
63*5a923131SAndroid Build Coastguard Worker
64*5a923131SAndroid Build Coastguard Worker LOG(INFO) << "A/B Update Engine starting";
65*5a923131SAndroid Build Coastguard Worker
66*5a923131SAndroid Build Coastguard Worker // xz-embedded requires to initialize its CRC-32 table once on startup.
67*5a923131SAndroid Build Coastguard Worker xz_crc32_init();
68*5a923131SAndroid Build Coastguard Worker
69*5a923131SAndroid Build Coastguard Worker // Ensure that all written files have safe permissions.
70*5a923131SAndroid Build Coastguard Worker // This is a mask, so we _block_ all permissions for the group owner and other
71*5a923131SAndroid Build Coastguard Worker // users but allow all permissions for the user owner. We allow execution
72*5a923131SAndroid Build Coastguard Worker // for the owner so we can create directories.
73*5a923131SAndroid Build Coastguard Worker // Done _after_ log file creation.
74*5a923131SAndroid Build Coastguard Worker umask(S_IRWXG | S_IRWXO);
75*5a923131SAndroid Build Coastguard Worker
76*5a923131SAndroid Build Coastguard Worker auto daemon = chromeos_update_engine::DaemonBase::CreateInstance();
77*5a923131SAndroid Build Coastguard Worker int exit_code = daemon->Run();
78*5a923131SAndroid Build Coastguard Worker
79*5a923131SAndroid Build Coastguard Worker chromeos_update_engine::Subprocess::Get().FlushBufferedLogsAtExit();
80*5a923131SAndroid Build Coastguard Worker
81*5a923131SAndroid Build Coastguard Worker LOG(INFO) << "A/B Update Engine terminating with exit code " << exit_code;
82*5a923131SAndroid Build Coastguard Worker return exit_code;
83*5a923131SAndroid Build Coastguard Worker }
84