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