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 <stdlib.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <xz.h>
21 
22 #include <base/at_exit.h>
23 #include <base/command_line.h>
24 #include <base/logging.h>
25 #include <gflags/gflags.h>
26 
27 #include "update_engine/common/daemon_base.h"
28 #include "update_engine/common/logging.h"
29 #include "update_engine/common/subprocess.h"
30 #include "update_engine/common/terminator.h"
31 #include "update_engine/common/utils.h"
32 
33 using std::string;
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 
main(int argc,char ** argv)40 int main(int argc, char** argv) {
41   chromeos_update_engine::Terminator::Init();
42   gflags::SetUsageMessage("A/B Update Engine");
43   gflags::ParseCommandLineFlags(&argc, &argv, true);
44 
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);
53   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   }
61   if (!FLAGS_foreground)
62     PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
63 
64   LOG(INFO) << "A/B Update Engine starting";
65 
66   // xz-embedded requires to initialize its CRC-32 table once on startup.
67   xz_crc32_init();
68 
69   // Ensure that all written files have safe permissions.
70   // 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.
73   // Done _after_ log file creation.
74   umask(S_IRWXG | S_IRWXO);
75 
76   auto daemon = chromeos_update_engine::DaemonBase::CreateInstance();
77   int exit_code = daemon->Run();
78 
79   chromeos_update_engine::Subprocess::Get().FlushBufferedLogsAtExit();
80 
81   LOG(INFO) << "A/B Update Engine terminating with exit code " << exit_code;
82   return exit_code;
83 }
84