blob: cf77f7460f6485c45ee3df7ee442557dd55767c8 [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>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08008#include <vector>
Darin Petkov9d65b7b2010-07-20 09:13:01 -07009
Darin Petkov1023a602010-08-30 13:47:51 -070010#include <base/at_exit.h>
11#include <base/command_line.h>
Ben Chan06c76a42014-09-05 08:21:06 -070012#include <base/files/file_util.h>
Darin Petkov1023a602010-08-30 13:47:51 -070013#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070014#include <base/strings/string_util.h>
15#include <base/strings/stringprintf.h>
Alex Deymo67363ee2014-09-23 23:07:44 -070016#include <base/time/time.h>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080017#include <gflags/gflags.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000018#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070019#include <metrics/metrics_library.h>
Chris Masone4dc2ada2010-09-23 12:43:03 -070020#include <sys/stat.h>
Alex Deymo44666f92014-07-22 20:29:24 -070021#include <sys/types.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070022
Bruno Rocha7f9aea22011-09-12 14:31:24 -070023#include "update_engine/certificate_checker.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070024#include "update_engine/dbus_constants.h"
25#include "update_engine/dbus_service.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080026#include "update_engine/dbus_wrapper_interface.h"
Alex Deymo44666f92014-07-22 20:29:24 -070027#include "update_engine/glib_utils.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080028#include "update_engine/real_system_state.h"
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070029#include "update_engine/subprocess.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070030#include "update_engine/terminator.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070031#include "update_engine/update_attempter.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070032extern "C" {
33#include "update_engine/update_engine.dbusserver.h"
34}
Alex Vakulenko44cab302014-07-23 13:12:15 -070035#include "update_engine/utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070036
37DEFINE_bool(logtostderr, false,
38 "Write logs to stderr instead of to a file in log_dir.");
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070039DEFINE_bool(foreground, false,
40 "Don't daemon()ize; run in foreground.");
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080041
42using std::string;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080043using std::vector;
44
Alex Deymo67363ee2014-09-23 23:07:44 -070045namespace {
46const int kDBusSystemMaxWaitSeconds = 2 * 60;
47} // namespace
48
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080049namespace chromeos_update_engine {
50
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070051gboolean UpdateBootFlags(void* arg) {
Darin Petkov61635a92011-05-18 16:20:36 -070052 reinterpret_cast<UpdateAttempter*>(arg)->UpdateBootFlags();
53 return FALSE; // Don't call this callback again
54}
55
56gboolean BroadcastStatus(void* arg) {
57 reinterpret_cast<UpdateAttempter*>(arg)->BroadcastStatus();
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070058 return FALSE; // Don't call this callback again
59}
60
David Zeuthene4c58bf2013-06-18 17:26:50 -070061gboolean UpdateEngineStarted(gpointer user_data) {
62 reinterpret_cast<UpdateAttempter*>(user_data)->UpdateEngineStarted();
Alex Vakulenkod2779df2014-06-16 13:19:00 -070063 return FALSE; // Remove idle source (e.g. don't do the callback again)
David Zeuthene4c58bf2013-06-18 17:26:50 -070064}
65
Andrew de los Reyes73520672010-05-10 13:44:58 -070066namespace {
67
Alex Deymo67363ee2014-09-23 23:07:44 -070068// Wait for DBus to be ready by attempting to get the system bus up to
69// |timeout| time. Returns whether it succeeded to get the bus.
70bool WaitForDBusSystem(base::TimeDelta timeout) {
71 GError *error = nullptr;
72 DBusGConnection *bus = nullptr;
73 Clock clock;
74 base::Time deadline = clock.GetMonotonicTime() + timeout;
75
76 while (clock.GetMonotonicTime() < deadline) {
77 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
78 if (bus)
79 return true;
80 LOG(WARNING) << "Failed to get system bus, waiting: "
81 << utils::GetAndFreeGError(&error);
82 // Wait 1 second.
83 sleep(1);
84 }
85 LOG(ERROR) << "Failed to get system bus after " << timeout.InSeconds()
86 << " seconds.";
87 return false;
88}
89
90void SetupDBusService(UpdateEngineService* service) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070091 DBusGConnection *bus;
92 DBusGProxy *proxy;
Alex Vakulenko75039d72014-03-25 12:36:28 -070093 GError *error = nullptr;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070094
95 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
Darin Petkova0b9e772011-10-06 05:05:56 -070096 LOG_IF(FATAL, !bus) << "Failed to get bus: "
97 << utils::GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070098 proxy = dbus_g_proxy_new_for_name(bus,
99 DBUS_SERVICE_DBUS,
100 DBUS_PATH_DBUS,
101 DBUS_INTERFACE_DBUS);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700102 guint32 request_name_ret;
103 if (!org_freedesktop_DBus_request_name(proxy,
104 kUpdateEngineServiceName,
105 0,
106 &request_name_ret,
107 &error)) {
Darin Petkova0b9e772011-10-06 05:05:56 -0700108 LOG(FATAL) << "Failed to get name: " << utils::GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700109 }
110 if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
111 g_warning("Got result code %u from requesting name", request_name_ret);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700112 LOG(FATAL) << "Got result code " << request_name_ret
113 << " from requesting name, but expected "
114 << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
115 }
116 dbus_g_connection_register_g_object(bus,
117 "/org/chromium/UpdateEngine",
118 G_OBJECT(service));
119}
120
Darin Petkov30291ed2010-11-12 10:23:06 -0800121void SetupLogSymlink(const string& symlink_path, const string& log_path) {
122 // TODO(petkov): To ensure a smooth transition between non-timestamped and
123 // timestamped logs, move an existing log to start the first timestamped
124 // one. This code can go away once all clients are switched to this version or
125 // we stop caring about the old-style logs.
126 if (utils::FileExists(symlink_path.c_str()) &&
127 !utils::IsSymlink(symlink_path.c_str())) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700128 base::ReplaceFile(base::FilePath(symlink_path),
129 base::FilePath(log_path),
130 nullptr);
Darin Petkov30291ed2010-11-12 10:23:06 -0800131 }
Alex Vakulenko75039d72014-03-25 12:36:28 -0700132 base::DeleteFile(base::FilePath(symlink_path), true);
Darin Petkov30291ed2010-11-12 10:23:06 -0800133 if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
134 PLOG(ERROR) << "Unable to create symlink " << symlink_path
135 << " pointing at " << log_path;
136 }
137}
138
139string GetTimeAsString(time_t utime) {
140 struct tm tm;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700141 CHECK_EQ(localtime_r(&utime, &tm), &tm);
Darin Petkov30291ed2010-11-12 10:23:06 -0800142 char str[16];
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700143 CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u);
Darin Petkov30291ed2010-11-12 10:23:06 -0800144 return str;
145}
146
147string SetupLogFile(const string& kLogsRoot) {
148 const string kLogSymlink = kLogsRoot + "/update_engine.log";
149 const string kLogsDir = kLogsRoot + "/update_engine";
150 const string kLogPath =
Alex Vakulenko75039d72014-03-25 12:36:28 -0700151 base::StringPrintf("%s/update_engine.%s",
152 kLogsDir.c_str(),
153 GetTimeAsString(::time(nullptr)).c_str());
Darin Petkov30291ed2010-11-12 10:23:06 -0800154 mkdir(kLogsDir.c_str(), 0755);
155 SetupLogSymlink(kLogSymlink, kLogPath);
156 return kLogSymlink;
157}
158
159void SetupLogging() {
Alex Deymod603ba92014-10-03 14:51:33 -0700160 string log_file;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700161 logging::LoggingSettings log_settings;
162 log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
163 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700164
Darin Petkov30291ed2010-11-12 10:23:06 -0800165 if (FLAGS_logtostderr) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700166 // Log to stderr initially.
167 log_settings.log_file = nullptr;
168 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
169 } else {
Alex Deymod603ba92014-10-03 14:51:33 -0700170 log_file = SetupLogFile("/var/log");
Alex Vakulenko75039d72014-03-25 12:36:28 -0700171 log_settings.log_file = log_file.c_str();
172 log_settings.logging_dest = logging::LOG_TO_FILE;
Darin Petkov30291ed2010-11-12 10:23:06 -0800173 }
Alex Vakulenko75039d72014-03-25 12:36:28 -0700174
175 logging::InitLogging(log_settings);
Darin Petkov30291ed2010-11-12 10:23:06 -0800176}
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800177
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700178} // namespace
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800179} // namespace chromeos_update_engine
rspangler@google.com49fdf182009-10-10 00:57:34 +0000180
rspangler@google.com49fdf182009-10-10 00:57:34 +0000181int main(int argc, char** argv) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700182 ::g_type_init();
Ben Chan46bf5c82013-06-24 11:17:41 -0700183 dbus_threads_init_default();
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700184 base::AtExitManager exit_manager; // Required for base/rand_util.h.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700185 chromeos_update_engine::Terminator::Init();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000186 chromeos_update_engine::Subprocess::Init();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800187 google::ParseCommandLineFlags(&argc, &argv, true);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700188 CommandLine::Init(argc, argv);
Darin Petkov30291ed2010-11-12 10:23:06 -0800189 chromeos_update_engine::SetupLogging();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700190 if (!FLAGS_foreground)
191 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
192
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800193 LOG(INFO) << "Chrome OS Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700194
Chris Masone4dc2ada2010-09-23 12:43:03 -0700195 // Ensure that all written files have safe permissions.
196 // This is a mask, so we _block_ execute for the owner, and ALL
197 // permissions for other users.
198 // Done _after_ log file creation.
199 umask(S_IXUSR | S_IRWXG | S_IRWXO);
200
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800201 // Create the single GMainLoop
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700202 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800203
Alex Deymo67363ee2014-09-23 23:07:44 -0700204 // Wait up to 2 minutes for DBus to be ready.
205 LOG_IF(FATAL, !chromeos_update_engine::WaitForDBusSystem(
206 base::TimeDelta::FromSeconds(kDBusSystemMaxWaitSeconds)))
207 << "Failed to initialize DBus, aborting.";
208
Jay Srinivasanf0572052012-10-23 18:12:56 -0700209 chromeos_update_engine::RealSystemState real_system_state;
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700210 LOG_IF(ERROR, !real_system_state.Initialize())
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800211 << "Failed to initialize system state.";
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800212 chromeos_update_engine::UpdateAttempter *update_attempter =
213 real_system_state.update_attempter();
214 CHECK(update_attempter);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700215
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700216 // Sets static members for the certificate checker.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800217 chromeos_update_engine::CertificateChecker::set_system_state(
218 &real_system_state);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700219 chromeos_update_engine::OpenSSLWrapper openssl_wrapper;
220 chromeos_update_engine::CertificateChecker::set_openssl_wrapper(
221 &openssl_wrapper);
222
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700223 // Create the dbus service object:
224 dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE,
225 &dbus_glib_update_engine_service_object_info);
Alex Deymo7e8ac062014-05-01 12:56:58 -0700226 UpdateEngineService* service = update_engine_service_new();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700227 service->system_state_ = &real_system_state;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800228 update_attempter->set_dbus_service(service);
Alex Deymo67363ee2014-09-23 23:07:44 -0700229 chromeos_update_engine::SetupDBusService(service);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800230
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700231 // Initiate update checks.
232 update_attempter->ScheduleUpdates();
Andrew de los Reyes73520672010-05-10 13:44:58 -0700233
Darin Petkoveee26ee2010-10-22 11:05:46 -0700234 // Update boot flags after 45 seconds.
235 g_timeout_add_seconds(45,
236 &chromeos_update_engine::UpdateBootFlags,
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800237 update_attempter);
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -0700238
Darin Petkov61635a92011-05-18 16:20:36 -0700239 // Broadcast the update engine status on startup to ensure consistent system
240 // state on crashes.
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800241 g_idle_add(&chromeos_update_engine::BroadcastStatus, update_attempter);
Darin Petkov61635a92011-05-18 16:20:36 -0700242
David Zeuthene4c58bf2013-06-18 17:26:50 -0700243 // Run the UpdateEngineStarted() method on |update_attempter|.
244 g_idle_add(&chromeos_update_engine::UpdateEngineStarted, update_attempter);
245
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700246 // Run the main loop until exit time:
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800247 g_main_loop_run(loop);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700248
249 // Cleanup:
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800250 g_main_loop_unref(loop);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700251 update_attempter->set_dbus_service(nullptr);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700252 g_object_unref(G_OBJECT(service));
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800253
254 LOG(INFO) << "Chrome OS Update Engine terminating";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000255 return 0;
256}