1 //
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 //
16
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <xz.h>
20
21 #include <base/at_exit.h>
22 #include <base/command_line.h>
23 #include <base/logging.h>
24 #include <brillo/flag_helper.h>
25
26 #include "update_engine/common/subprocess.h"
27 #include "update_engine/common/terminator.h"
28 #include "update_engine/common/utils.h"
29 #include "update_engine/daemon_base.h"
30 #include "update_engine/logging.h"
31
32 using std::string;
33
main(int argc,char ** argv)34 int main(int argc, char** argv) {
35 DEFINE_bool(logtofile, false, "Write logs to a file in log_dir.");
36 DEFINE_bool(logtostderr,
37 false,
38 "Write logs to stderr instead of to a file in log_dir.");
39 DEFINE_bool(foreground, false, "Don't daemon()ize; run in foreground.");
40
41 chromeos_update_engine::Terminator::Init();
42 brillo::FlagHelper::Init(argc, argv, "A/B Update Engine");
43
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);
52 if (!FLAGS_foreground)
53 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
54
55 LOG(INFO) << "A/B Update Engine starting";
56
57 // xz-embedded requires to initialize its CRC-32 table once on startup.
58 xz_crc32_init();
59
60 // Ensure that all written files have safe permissions.
61 // 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.
64 // Done _after_ log file creation.
65 umask(S_IRWXG | S_IRWXO);
66
67 auto daemon = chromeos_update_engine::DaemonBase::CreateInstance();
68 int exit_code = daemon->Run();
69
70 chromeos_update_engine::Subprocess::Get().FlushBufferedLogsAtExit();
71
72 LOG(INFO) << "A/B Update Engine terminating with exit code " << exit_code;
73 return exit_code;
74 }
75