blob: 8e88f239861a96a39e098edb2a67166544ea9f2b [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com49fdf182009-10-10 00:57:34 +000016
Alex Deymo2e71f902015-09-30 01:25:48 -070017#include <sys/stat.h>
18#include <sys/types.h>
Alex Deymo67363ee2014-09-23 23:07:44 -070019#include <unistd.h>
Alex Deymo2e71f902015-09-30 01:25:48 -070020#include <xz.h>
Alex Deymo67363ee2014-09-23 23:07:44 -070021
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080022#include <string>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070023
Darin Petkov1023a602010-08-30 13:47:51 -070024#include <base/at_exit.h>
25#include <base/command_line.h>
Ben Chan06c76a42014-09-05 08:21:06 -070026#include <base/files/file_util.h>
Darin Petkov1023a602010-08-30 13:47:51 -070027#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070028#include <base/strings/string_util.h>
29#include <base/strings/stringprintf.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070030#include <brillo/flag_helper.h>
31#include <brillo/message_loops/base_message_loop.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070032
Alex Deymo39910dc2015-11-09 17:04:30 -080033#include "update_engine/common/terminator.h"
34#include "update_engine/common/utils.h"
Alex Deymob7ca0962014-10-01 17:58:07 -070035#include "update_engine/daemon.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070036
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080037using std::string;
Alex Deymo67363ee2014-09-23 23:07:44 -070038
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080039namespace chromeos_update_engine {
Andrew de los Reyes73520672010-05-10 13:44:58 -070040namespace {
41
Darin Petkov30291ed2010-11-12 10:23:06 -080042void 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 Vakulenko75039d72014-03-25 12:36:28 -070049 base::ReplaceFile(base::FilePath(symlink_path),
50 base::FilePath(log_path),
51 nullptr);
Darin Petkov30291ed2010-11-12 10:23:06 -080052 }
Alex Vakulenko75039d72014-03-25 12:36:28 -070053 base::DeleteFile(base::FilePath(symlink_path), true);
Darin Petkov30291ed2010-11-12 10:23:06 -080054 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
60string GetTimeAsString(time_t utime) {
61 struct tm tm;
Alex Vakulenkod2779df2014-06-16 13:19:00 -070062 CHECK_EQ(localtime_r(&utime, &tm), &tm);
Darin Petkov30291ed2010-11-12 10:23:06 -080063 char str[16];
Alex Vakulenkod2779df2014-06-16 13:19:00 -070064 CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u);
Darin Petkov30291ed2010-11-12 10:23:06 -080065 return str;
66}
67
68string SetupLogFile(const string& kLogsRoot) {
69 const string kLogSymlink = kLogsRoot + "/update_engine.log";
70 const string kLogsDir = kLogsRoot + "/update_engine";
71 const string kLogPath =
Alex Vakulenko75039d72014-03-25 12:36:28 -070072 base::StringPrintf("%s/update_engine.%s",
73 kLogsDir.c_str(),
74 GetTimeAsString(::time(nullptr)).c_str());
Darin Petkov30291ed2010-11-12 10:23:06 -080075 mkdir(kLogsDir.c_str(), 0755);
76 SetupLogSymlink(kLogSymlink, kLogPath);
77 return kLogSymlink;
78}
79
Steve Fungd5708202014-09-28 23:24:06 -070080void SetupLogging(bool log_to_std_err) {
Alex Deymod603ba92014-10-03 14:51:33 -070081 string log_file;
Alex Vakulenko75039d72014-03-25 12:36:28 -070082 logging::LoggingSettings log_settings;
83 log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
84 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
Alex Vakulenko75039d72014-03-25 12:36:28 -070085
Steve Fungd5708202014-09-28 23:24:06 -070086 if (log_to_std_err) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070087 // Log to stderr initially.
88 log_settings.log_file = nullptr;
89 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
90 } else {
Alex Deymod603ba92014-10-03 14:51:33 -070091 log_file = SetupLogFile("/var/log");
Alex Vakulenko75039d72014-03-25 12:36:28 -070092 log_settings.log_file = log_file.c_str();
93 log_settings.logging_dest = logging::LOG_TO_FILE;
Darin Petkov30291ed2010-11-12 10:23:06 -080094 }
Alex Vakulenko75039d72014-03-25 12:36:28 -070095
96 logging::InitLogging(log_settings);
Darin Petkov30291ed2010-11-12 10:23:06 -080097}
Andrew de los Reyes000d8952011-03-02 15:21:14 -080098
Alex Vakulenkod2779df2014-06-16 13:19:00 -070099} // namespace
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800100} // namespace chromeos_update_engine
rspangler@google.com49fdf182009-10-10 00:57:34 +0000101
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102int main(int argc, char** argv) {
Steve Fungd5708202014-09-28 23:24:06 -0700103 DEFINE_bool(logtostderr, false,
104 "Write logs to stderr instead of to a file in log_dir.");
105 DEFINE_bool(foreground, false,
106 "Don't daemon()ize; run in foreground.");
107
Darin Petkov9c0baf82010-10-07 13:44:48 -0700108 chromeos_update_engine::Terminator::Init();
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700109 brillo::FlagHelper::Init(argc, argv, "Chromium OS Update Engine");
Steve Fungd5708202014-09-28 23:24:06 -0700110 chromeos_update_engine::SetupLogging(FLAGS_logtostderr);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700111 if (!FLAGS_foreground)
112 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
113
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800114 LOG(INFO) << "Chrome OS Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700115
Alex Deymo2e71f902015-09-30 01:25:48 -0700116 // xz-embedded requires to initialize its CRC-32 table once on startup.
117 xz_crc32_init();
118
Chris Masone4dc2ada2010-09-23 12:43:03 -0700119 // Ensure that all written files have safe permissions.
Alex Deymoa9fea842015-09-17 13:53:29 -0700120 // This is a mask, so we _block_ all permissions for the group owner and other
121 // users but allow all permissions for the user owner. We allow execution
122 // for the owner so we can create directories.
Chris Masone4dc2ada2010-09-23 12:43:03 -0700123 // Done _after_ log file creation.
Alex Deymoa9fea842015-09-17 13:53:29 -0700124 umask(S_IRWXG | S_IRWXO);
Chris Masone4dc2ada2010-09-23 12:43:03 -0700125
Alex Deymob7ca0962014-10-01 17:58:07 -0700126 chromeos_update_engine::UpdateEngineDaemon update_engine_daemon;
127 int exit_code = update_engine_daemon.Run();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800128
Alex Deymob7ca0962014-10-01 17:58:07 -0700129 LOG(INFO) << "Chrome OS Update Engine terminating with exit code "
130 << exit_code;
131 return exit_code;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132}