| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 |  | 
| Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 17 | #include <sys/stat.h> | 
|  | 18 | #include <sys/types.h> | 
| Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 19 | #include <xz.h> | 
| Alex Deymo | 67363ee | 2014-09-23 23:07:44 -0700 | [diff] [blame] | 20 |  | 
| Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 21 | #include <base/at_exit.h> | 
|  | 22 | #include <base/command_line.h> | 
|  | 23 | #include <base/logging.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 24 | #include <brillo/flag_helper.h> | 
| Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 25 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 26 | #include "update_engine/common/daemon_base.h" | 
|  | 27 | #include "update_engine/common/logging.h" | 
| Amin Hassani | 565331e | 2019-06-24 14:11:29 -0700 | [diff] [blame] | 28 | #include "update_engine/common/subprocess.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/common/terminator.h" | 
|  | 30 | #include "update_engine/common/utils.h" | 
| Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 31 |  | 
| Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 32 | using std::string; | 
| Alex Deymo | 67363ee | 2014-09-23 23:07:44 -0700 | [diff] [blame] | 33 |  | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 34 | int main(int argc, char** argv) { | 
| Tianjie Xu | 6c863b7 | 2017-09-17 15:44:51 -0700 | [diff] [blame] | 35 | DEFINE_bool(logtofile, false, "Write logs to a file in log_dir."); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 36 | DEFINE_bool(logtostderr, | 
|  | 37 | false, | 
| Steve Fung | d570820 | 2014-09-28 23:24:06 -0700 | [diff] [blame] | 38 | "Write logs to stderr instead of to a file in log_dir."); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 39 | DEFINE_bool(foreground, false, "Don't daemon()ize; run in foreground."); | 
| Steve Fung | d570820 | 2014-09-28 23:24:06 -0700 | [diff] [blame] | 40 |  | 
| Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 41 | chromeos_update_engine::Terminator::Init(); | 
| Sen Jiang | 49a0897 | 2017-07-26 15:54:18 -0700 | [diff] [blame] | 42 | brillo::FlagHelper::Init(argc, argv, "A/B Update Engine"); | 
| Tianjie Xu | 6c863b7 | 2017-09-17 15:44:51 -0700 | [diff] [blame] | 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); | 
| Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 52 | if (!FLAGS_foreground) | 
|  | 53 | PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed"; | 
|  | 54 |  | 
| Sen Jiang | 49a0897 | 2017-07-26 15:54:18 -0700 | [diff] [blame] | 55 | LOG(INFO) << "A/B Update Engine starting"; | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 56 |  | 
| Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 57 | // xz-embedded requires to initialize its CRC-32 table once on startup. | 
|  | 58 | xz_crc32_init(); | 
|  | 59 |  | 
| Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 60 | // Ensure that all written files have safe permissions. | 
| Alex Deymo | a9fea84 | 2015-09-17 13:53:29 -0700 | [diff] [blame] | 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. | 
| Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 64 | // Done _after_ log file creation. | 
| Alex Deymo | a9fea84 | 2015-09-17 13:53:29 -0700 | [diff] [blame] | 65 | umask(S_IRWXG | S_IRWXO); | 
| Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 66 |  | 
| Amin Hassani | 565331e | 2019-06-24 14:11:29 -0700 | [diff] [blame] | 67 | auto daemon = chromeos_update_engine::DaemonBase::CreateInstance(); | 
|  | 68 | int exit_code = daemon->Run(); | 
| Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 69 |  | 
| Amin Hassani | a885954 | 2018-03-07 16:24:43 -0800 | [diff] [blame] | 70 | chromeos_update_engine::Subprocess::Get().FlushBufferedLogsAtExit(); | 
|  | 71 |  | 
| Sen Jiang | 49a0897 | 2017-07-26 15:54:18 -0700 | [diff] [blame] | 72 | LOG(INFO) << "A/B Update Engine terminating with exit code " << exit_code; | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 73 | return exit_code; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 74 | } |