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 | 67363ee | 2014-09-23 23:07:44 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 20 | #include <xz.h> |
Alex Deymo | 67363ee | 2014-09-23 23:07:44 -0700 | [diff] [blame] | 21 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 22 | #include <string> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 23 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 24 | #include <base/at_exit.h> |
| 25 | #include <base/command_line.h> |
Ben Chan | 06c76a4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 26 | #include <base/files/file_util.h> |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 27 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 28 | #include <base/strings/string_util.h> |
| 29 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 30 | #include <brillo/flag_helper.h> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 31 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 32 | #include "update_engine/common/terminator.h" |
| 33 | #include "update_engine/common/utils.h" |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 34 | #include "update_engine/daemon.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 35 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 36 | using std::string; |
Alex Deymo | 67363ee | 2014-09-23 23:07:44 -0700 | [diff] [blame] | 37 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 38 | namespace chromeos_update_engine { |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 39 | namespace { |
| 40 | |
Tianjie Xu | 6c863b7 | 2017-09-17 15:44:51 -0700 | [diff] [blame] | 41 | #ifndef __ANDROID__ |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 42 | void SetupLogSymlink(const string& symlink_path, const string& log_path) { |
| 43 | // TODO(petkov): To ensure a smooth transition between non-timestamped and |
| 44 | // timestamped logs, move an existing log to start the first timestamped |
| 45 | // one. This code can go away once all clients are switched to this version or |
| 46 | // we stop caring about the old-style logs. |
| 47 | if (utils::FileExists(symlink_path.c_str()) && |
| 48 | !utils::IsSymlink(symlink_path.c_str())) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 49 | base::ReplaceFile(base::FilePath(symlink_path), |
| 50 | base::FilePath(log_path), |
| 51 | nullptr); |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 52 | } |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 53 | base::DeleteFile(base::FilePath(symlink_path), true); |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 54 | if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) { |
| 55 | PLOG(ERROR) << "Unable to create symlink " << symlink_path |
| 56 | << " pointing at " << log_path; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | string GetTimeAsString(time_t utime) { |
| 61 | struct tm tm; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 62 | CHECK_EQ(localtime_r(&utime, &tm), &tm); |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 63 | char str[16]; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 64 | CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u); |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 65 | return str; |
| 66 | } |
| 67 | |
| 68 | string SetupLogFile(const string& kLogsRoot) { |
| 69 | const string kLogSymlink = kLogsRoot + "/update_engine.log"; |
| 70 | const string kLogsDir = kLogsRoot + "/update_engine"; |
| 71 | const string kLogPath = |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 72 | base::StringPrintf("%s/update_engine.%s", |
| 73 | kLogsDir.c_str(), |
| 74 | GetTimeAsString(::time(nullptr)).c_str()); |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 75 | mkdir(kLogsDir.c_str(), 0755); |
| 76 | SetupLogSymlink(kLogSymlink, kLogPath); |
| 77 | return kLogSymlink; |
| 78 | } |
Tianjie Xu | 6c863b7 | 2017-09-17 15:44:51 -0700 | [diff] [blame] | 79 | #endif // __ANDROID__ |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 80 | |
Tianjie Xu | 6c863b7 | 2017-09-17 15:44:51 -0700 | [diff] [blame] | 81 | void SetupLogging(bool log_to_system, bool log_to_file) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 82 | logging::LoggingSettings log_settings; |
| 83 | log_settings.lock_log = logging::DONT_LOCK_LOG_FILE; |
Tianjie Xu | 6c863b7 | 2017-09-17 15:44:51 -0700 | [diff] [blame] | 84 | log_settings.logging_dest = static_cast<logging::LoggingDestination>( |
| 85 | (log_to_system ? logging::LOG_TO_SYSTEM_DEBUG_LOG : 0) | |
| 86 | (log_to_file ? logging::LOG_TO_FILE : 0)); |
| 87 | log_settings.log_file = nullptr; |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 88 | |
Tianjie Xu | 6c863b7 | 2017-09-17 15:44:51 -0700 | [diff] [blame] | 89 | string log_file; |
| 90 | if (log_to_file) { |
| 91 | #ifdef __ANDROID__ |
Tianjie Xu | 1e2750a | 2017-11-02 15:34:18 -0700 | [diff] [blame^] | 92 | log_file = "/data/misc/update_engine_log/update_engine.log"; |
Tianjie Xu | 6c863b7 | 2017-09-17 15:44:51 -0700 | [diff] [blame] | 93 | log_settings.delete_old = logging::DELETE_OLD_LOG_FILE; |
| 94 | #else |
Alex Deymo | d603ba9 | 2014-10-03 14:51:33 -0700 | [diff] [blame] | 95 | log_file = SetupLogFile("/var/log"); |
Tianjie Xu | 6c863b7 | 2017-09-17 15:44:51 -0700 | [diff] [blame] | 96 | log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE; |
| 97 | #endif // __ANDROID__ |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 98 | log_settings.log_file = log_file.c_str(); |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 99 | } |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 100 | logging::InitLogging(log_settings); |
Tianjie Xu | 1e2750a | 2017-11-02 15:34:18 -0700 | [diff] [blame^] | 101 | |
| 102 | #ifdef __ANDROID__ |
| 103 | // The log file will have AID_LOG as group ID; this GID is inherited from the |
| 104 | // parent directory "/data/misc/update_engine_log" which sets the SGID bit. |
| 105 | chmod(log_file.c_str(), 0640); |
| 106 | #endif |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 107 | } |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 108 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 109 | } // namespace |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 110 | } // namespace chromeos_update_engine |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 111 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 112 | int main(int argc, char** argv) { |
Tianjie Xu | 6c863b7 | 2017-09-17 15:44:51 -0700 | [diff] [blame] | 113 | DEFINE_bool(logtofile, false, "Write logs to a file in log_dir."); |
Steve Fung | d570820 | 2014-09-28 23:24:06 -0700 | [diff] [blame] | 114 | DEFINE_bool(logtostderr, false, |
| 115 | "Write logs to stderr instead of to a file in log_dir."); |
| 116 | DEFINE_bool(foreground, false, |
| 117 | "Don't daemon()ize; run in foreground."); |
| 118 | |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 119 | chromeos_update_engine::Terminator::Init(); |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 120 | brillo::FlagHelper::Init(argc, argv, "Chromium OS Update Engine"); |
Tianjie Xu | 6c863b7 | 2017-09-17 15:44:51 -0700 | [diff] [blame] | 121 | |
| 122 | // We have two logging flags "--logtostderr" and "--logtofile"; and the logic |
| 123 | // to choose the logging destination is: |
| 124 | // 1. --logtostderr --logtofile -> logs to both |
| 125 | // 2. --logtostderr -> logs to system debug |
| 126 | // 3. --logtofile or no flags -> logs to file |
| 127 | bool log_to_system = FLAGS_logtostderr; |
| 128 | bool log_to_file = FLAGS_logtofile || !FLAGS_logtostderr; |
| 129 | chromeos_update_engine::SetupLogging(log_to_system, log_to_file); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 130 | if (!FLAGS_foreground) |
| 131 | PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed"; |
| 132 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 133 | LOG(INFO) << "Chrome OS Update Engine starting"; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 134 | |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 135 | // xz-embedded requires to initialize its CRC-32 table once on startup. |
| 136 | xz_crc32_init(); |
| 137 | |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 138 | // Ensure that all written files have safe permissions. |
Alex Deymo | a9fea84 | 2015-09-17 13:53:29 -0700 | [diff] [blame] | 139 | // This is a mask, so we _block_ all permissions for the group owner and other |
| 140 | // users but allow all permissions for the user owner. We allow execution |
| 141 | // for the owner so we can create directories. |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 142 | // Done _after_ log file creation. |
Alex Deymo | a9fea84 | 2015-09-17 13:53:29 -0700 | [diff] [blame] | 143 | umask(S_IRWXG | S_IRWXO); |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 144 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 145 | chromeos_update_engine::UpdateEngineDaemon update_engine_daemon; |
| 146 | int exit_code = update_engine_daemon.Run(); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 147 | |
Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 148 | LOG(INFO) << "Chrome OS Update Engine terminating with exit code " |
| 149 | << exit_code; |
| 150 | return exit_code; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 151 | } |