blob: 4fe0c8478091c21877e4d5642cebc439ac6b1931 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo67363ee2014-09-23 23:07:44 -07005#include <unistd.h>
6
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08007#include <string>
Darin Petkov9d65b7b2010-07-20 09:13:01 -07008
Darin Petkov1023a602010-08-30 13:47:51 -07009#include <base/at_exit.h>
10#include <base/command_line.h>
Ben Chan06c76a42014-09-05 08:21:06 -070011#include <base/files/file_util.h>
Darin Petkov1023a602010-08-30 13:47:51 -070012#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070013#include <base/strings/string_util.h>
14#include <base/strings/stringprintf.h>
Steve Fungd5708202014-09-28 23:24:06 -070015#include <chromeos/flag_helper.h>
Alex Deymob7ca0962014-10-01 17:58:07 -070016#include <chromeos/message_loops/base_message_loop.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000017#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070018#include <metrics/metrics_library.h>
Chris Masone4dc2ada2010-09-23 12:43:03 -070019#include <sys/stat.h>
Alex Deymo44666f92014-07-22 20:29:24 -070020#include <sys/types.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070021
Alex Deymob7ca0962014-10-01 17:58:07 -070022#include "update_engine/daemon.h"
Alex Deymo44666f92014-07-22 20:29:24 -070023#include "update_engine/glib_utils.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070024#include "update_engine/terminator.h"
Alex Vakulenko44cab302014-07-23 13:12:15 -070025#include "update_engine/utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080027using std::string;
Alex Deymo67363ee2014-09-23 23:07:44 -070028
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080029namespace chromeos_update_engine {
Andrew de los Reyes73520672010-05-10 13:44:58 -070030namespace {
31
Darin Petkov30291ed2010-11-12 10:23:06 -080032void SetupLogSymlink(const string& symlink_path, const string& log_path) {
33 // TODO(petkov): To ensure a smooth transition between non-timestamped and
34 // timestamped logs, move an existing log to start the first timestamped
35 // one. This code can go away once all clients are switched to this version or
36 // we stop caring about the old-style logs.
37 if (utils::FileExists(symlink_path.c_str()) &&
38 !utils::IsSymlink(symlink_path.c_str())) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070039 base::ReplaceFile(base::FilePath(symlink_path),
40 base::FilePath(log_path),
41 nullptr);
Darin Petkov30291ed2010-11-12 10:23:06 -080042 }
Alex Vakulenko75039d72014-03-25 12:36:28 -070043 base::DeleteFile(base::FilePath(symlink_path), true);
Darin Petkov30291ed2010-11-12 10:23:06 -080044 if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
45 PLOG(ERROR) << "Unable to create symlink " << symlink_path
46 << " pointing at " << log_path;
47 }
48}
49
50string GetTimeAsString(time_t utime) {
51 struct tm tm;
Alex Vakulenkod2779df2014-06-16 13:19:00 -070052 CHECK_EQ(localtime_r(&utime, &tm), &tm);
Darin Petkov30291ed2010-11-12 10:23:06 -080053 char str[16];
Alex Vakulenkod2779df2014-06-16 13:19:00 -070054 CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u);
Darin Petkov30291ed2010-11-12 10:23:06 -080055 return str;
56}
57
58string SetupLogFile(const string& kLogsRoot) {
59 const string kLogSymlink = kLogsRoot + "/update_engine.log";
60 const string kLogsDir = kLogsRoot + "/update_engine";
61 const string kLogPath =
Alex Vakulenko75039d72014-03-25 12:36:28 -070062 base::StringPrintf("%s/update_engine.%s",
63 kLogsDir.c_str(),
64 GetTimeAsString(::time(nullptr)).c_str());
Darin Petkov30291ed2010-11-12 10:23:06 -080065 mkdir(kLogsDir.c_str(), 0755);
66 SetupLogSymlink(kLogSymlink, kLogPath);
67 return kLogSymlink;
68}
69
Steve Fungd5708202014-09-28 23:24:06 -070070void SetupLogging(bool log_to_std_err) {
Alex Deymod603ba92014-10-03 14:51:33 -070071 string log_file;
Alex Vakulenko75039d72014-03-25 12:36:28 -070072 logging::LoggingSettings log_settings;
73 log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
74 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
Alex Vakulenko75039d72014-03-25 12:36:28 -070075
Steve Fungd5708202014-09-28 23:24:06 -070076 if (log_to_std_err) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070077 // Log to stderr initially.
78 log_settings.log_file = nullptr;
79 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
80 } else {
Alex Deymod603ba92014-10-03 14:51:33 -070081 log_file = SetupLogFile("/var/log");
Alex Vakulenko75039d72014-03-25 12:36:28 -070082 log_settings.log_file = log_file.c_str();
83 log_settings.logging_dest = logging::LOG_TO_FILE;
Darin Petkov30291ed2010-11-12 10:23:06 -080084 }
Alex Vakulenko75039d72014-03-25 12:36:28 -070085
86 logging::InitLogging(log_settings);
Darin Petkov30291ed2010-11-12 10:23:06 -080087}
Andrew de los Reyes000d8952011-03-02 15:21:14 -080088
Alex Vakulenkod2779df2014-06-16 13:19:00 -070089} // namespace
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080090} // namespace chromeos_update_engine
rspangler@google.com49fdf182009-10-10 00:57:34 +000091
rspangler@google.com49fdf182009-10-10 00:57:34 +000092int main(int argc, char** argv) {
Steve Fungd5708202014-09-28 23:24:06 -070093 DEFINE_bool(logtostderr, false,
94 "Write logs to stderr instead of to a file in log_dir.");
95 DEFINE_bool(foreground, false,
96 "Don't daemon()ize; run in foreground.");
97
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070098 ::g_type_init();
Ben Chan46bf5c82013-06-24 11:17:41 -070099 dbus_threads_init_default();
Darin Petkov9c0baf82010-10-07 13:44:48 -0700100 chromeos_update_engine::Terminator::Init();
Steve Fungd5708202014-09-28 23:24:06 -0700101 chromeos::FlagHelper::Init(argc, argv, "Chromium OS Update Engine");
102 chromeos_update_engine::SetupLogging(FLAGS_logtostderr);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700103 if (!FLAGS_foreground)
104 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
105
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800106 LOG(INFO) << "Chrome OS Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700107
Chris Masone4dc2ada2010-09-23 12:43:03 -0700108 // Ensure that all written files have safe permissions.
109 // This is a mask, so we _block_ execute for the owner, and ALL
110 // permissions for other users.
111 // Done _after_ log file creation.
112 umask(S_IXUSR | S_IRWXG | S_IRWXO);
113
Alex Deymob7ca0962014-10-01 17:58:07 -0700114 chromeos_update_engine::UpdateEngineDaemon update_engine_daemon;
115 int exit_code = update_engine_daemon.Run();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800116
Alex Deymob7ca0962014-10-01 17:58:07 -0700117 LOG(INFO) << "Chrome OS Update Engine terminating with exit code "
118 << exit_code;
119 return exit_code;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000120}