blob: 51a04ec3cdfcb832973a4e982356d4fc4221f55b [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>
Alex Deymo0bb23412015-06-19 00:04:46 -070013#include <base/location.h>
Darin Petkov1023a602010-08-30 13:47:51 -070014#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070015#include <base/strings/string_util.h>
16#include <base/strings/stringprintf.h>
Alex Deymo67363ee2014-09-23 23:07:44 -070017#include <base/time/time.h>
Steve Fungd5708202014-09-28 23:24:06 -070018#include <chromeos/flag_helper.h>
Alex Deymocc169252015-06-10 19:10:14 -070019#include <chromeos/message_loops/glib_message_loop.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000020#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070021#include <metrics/metrics_library.h>
Chris Masone4dc2ada2010-09-23 12:43:03 -070022#include <sys/stat.h>
Alex Deymo44666f92014-07-22 20:29:24 -070023#include <sys/types.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070024
Bruno Rocha7f9aea22011-09-12 14:31:24 -070025#include "update_engine/certificate_checker.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026#include "update_engine/dbus_constants.h"
27#include "update_engine/dbus_service.h"
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080028#include "update_engine/dbus_wrapper_interface.h"
Alex Deymo44666f92014-07-22 20:29:24 -070029#include "update_engine/glib_utils.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080030#include "update_engine/real_system_state.h"
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070031#include "update_engine/subprocess.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070032#include "update_engine/terminator.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070033#include "update_engine/update_attempter.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070034extern "C" {
Timothy Jennison08d49312015-03-04 13:46:40 -050035#include "update_engine/org.chromium.UpdateEngineInterface.dbusserver.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070036}
Alex Vakulenko44cab302014-07-23 13:12:15 -070037#include "update_engine/utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070038
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080039using std::string;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080040using std::vector;
41
Alex Deymo67363ee2014-09-23 23:07:44 -070042namespace {
43const int kDBusSystemMaxWaitSeconds = 2 * 60;
44} // namespace
45
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080046namespace chromeos_update_engine {
47
Andrew de los Reyes73520672010-05-10 13:44:58 -070048namespace {
49
Alex Deymo67363ee2014-09-23 23:07:44 -070050// Wait for DBus to be ready by attempting to get the system bus up to
51// |timeout| time. Returns whether it succeeded to get the bus.
52bool WaitForDBusSystem(base::TimeDelta timeout) {
53 GError *error = nullptr;
54 DBusGConnection *bus = nullptr;
55 Clock clock;
56 base::Time deadline = clock.GetMonotonicTime() + timeout;
57
58 while (clock.GetMonotonicTime() < deadline) {
59 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
60 if (bus)
61 return true;
62 LOG(WARNING) << "Failed to get system bus, waiting: "
63 << utils::GetAndFreeGError(&error);
64 // Wait 1 second.
65 sleep(1);
66 }
67 LOG(ERROR) << "Failed to get system bus after " << timeout.InSeconds()
68 << " seconds.";
69 return false;
70}
71
72void SetupDBusService(UpdateEngineService* service) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070073 DBusGConnection *bus;
74 DBusGProxy *proxy;
Alex Vakulenko75039d72014-03-25 12:36:28 -070075 GError *error = nullptr;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070076
77 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
Darin Petkova0b9e772011-10-06 05:05:56 -070078 LOG_IF(FATAL, !bus) << "Failed to get bus: "
79 << utils::GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070080 proxy = dbus_g_proxy_new_for_name(bus,
81 DBUS_SERVICE_DBUS,
82 DBUS_PATH_DBUS,
83 DBUS_INTERFACE_DBUS);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070084 guint32 request_name_ret;
85 if (!org_freedesktop_DBus_request_name(proxy,
86 kUpdateEngineServiceName,
87 0,
88 &request_name_ret,
89 &error)) {
Darin Petkova0b9e772011-10-06 05:05:56 -070090 LOG(FATAL) << "Failed to get name: " << utils::GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070091 }
92 if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
93 g_warning("Got result code %u from requesting name", request_name_ret);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070094 LOG(FATAL) << "Got result code " << request_name_ret
95 << " from requesting name, but expected "
96 << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
97 }
98 dbus_g_connection_register_g_object(bus,
99 "/org/chromium/UpdateEngine",
100 G_OBJECT(service));
101}
102
Darin Petkov30291ed2010-11-12 10:23:06 -0800103void SetupLogSymlink(const string& symlink_path, const string& log_path) {
104 // TODO(petkov): To ensure a smooth transition between non-timestamped and
105 // timestamped logs, move an existing log to start the first timestamped
106 // one. This code can go away once all clients are switched to this version or
107 // we stop caring about the old-style logs.
108 if (utils::FileExists(symlink_path.c_str()) &&
109 !utils::IsSymlink(symlink_path.c_str())) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700110 base::ReplaceFile(base::FilePath(symlink_path),
111 base::FilePath(log_path),
112 nullptr);
Darin Petkov30291ed2010-11-12 10:23:06 -0800113 }
Alex Vakulenko75039d72014-03-25 12:36:28 -0700114 base::DeleteFile(base::FilePath(symlink_path), true);
Darin Petkov30291ed2010-11-12 10:23:06 -0800115 if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
116 PLOG(ERROR) << "Unable to create symlink " << symlink_path
117 << " pointing at " << log_path;
118 }
119}
120
121string GetTimeAsString(time_t utime) {
122 struct tm tm;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700123 CHECK_EQ(localtime_r(&utime, &tm), &tm);
Darin Petkov30291ed2010-11-12 10:23:06 -0800124 char str[16];
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700125 CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u);
Darin Petkov30291ed2010-11-12 10:23:06 -0800126 return str;
127}
128
129string SetupLogFile(const string& kLogsRoot) {
130 const string kLogSymlink = kLogsRoot + "/update_engine.log";
131 const string kLogsDir = kLogsRoot + "/update_engine";
132 const string kLogPath =
Alex Vakulenko75039d72014-03-25 12:36:28 -0700133 base::StringPrintf("%s/update_engine.%s",
134 kLogsDir.c_str(),
135 GetTimeAsString(::time(nullptr)).c_str());
Darin Petkov30291ed2010-11-12 10:23:06 -0800136 mkdir(kLogsDir.c_str(), 0755);
137 SetupLogSymlink(kLogSymlink, kLogPath);
138 return kLogSymlink;
139}
140
Steve Fungd5708202014-09-28 23:24:06 -0700141void SetupLogging(bool log_to_std_err) {
Alex Deymod603ba92014-10-03 14:51:33 -0700142 string log_file;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700143 logging::LoggingSettings log_settings;
144 log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
145 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
Alex Vakulenko75039d72014-03-25 12:36:28 -0700146
Steve Fungd5708202014-09-28 23:24:06 -0700147 if (log_to_std_err) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700148 // Log to stderr initially.
149 log_settings.log_file = nullptr;
150 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
151 } else {
Alex Deymod603ba92014-10-03 14:51:33 -0700152 log_file = SetupLogFile("/var/log");
Alex Vakulenko75039d72014-03-25 12:36:28 -0700153 log_settings.log_file = log_file.c_str();
154 log_settings.logging_dest = logging::LOG_TO_FILE;
Darin Petkov30291ed2010-11-12 10:23:06 -0800155 }
Alex Vakulenko75039d72014-03-25 12:36:28 -0700156
157 logging::InitLogging(log_settings);
Darin Petkov30291ed2010-11-12 10:23:06 -0800158}
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800159
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700160} // namespace
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800161} // namespace chromeos_update_engine
rspangler@google.com49fdf182009-10-10 00:57:34 +0000162
rspangler@google.com49fdf182009-10-10 00:57:34 +0000163int main(int argc, char** argv) {
Steve Fungd5708202014-09-28 23:24:06 -0700164 DEFINE_bool(logtostderr, false,
165 "Write logs to stderr instead of to a file in log_dir.");
166 DEFINE_bool(foreground, false,
167 "Don't daemon()ize; run in foreground.");
168
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700169 ::g_type_init();
Ben Chan46bf5c82013-06-24 11:17:41 -0700170 dbus_threads_init_default();
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700171 base::AtExitManager exit_manager; // Required for base/rand_util.h.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700172 chromeos_update_engine::Terminator::Init();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000173 chromeos_update_engine::Subprocess::Init();
Steve Fungd5708202014-09-28 23:24:06 -0700174 chromeos::FlagHelper::Init(argc, argv, "Chromium OS Update Engine");
175 chromeos_update_engine::SetupLogging(FLAGS_logtostderr);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700176 if (!FLAGS_foreground)
177 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
178
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800179 LOG(INFO) << "Chrome OS Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700180
Chris Masone4dc2ada2010-09-23 12:43:03 -0700181 // Ensure that all written files have safe permissions.
182 // This is a mask, so we _block_ execute for the owner, and ALL
183 // permissions for other users.
184 // Done _after_ log file creation.
185 umask(S_IXUSR | S_IRWXG | S_IRWXO);
186
Alex Deymocc169252015-06-10 19:10:14 -0700187 // Create the single Glib main loop. Code accessing directly the glib main
188 // loop (such as calling g_timeout_add() or similar functions) will still work
189 // since the backend for the message loop is still the Glib main loop.
190 // TODO(deymo): Replace this |loop| with one based on libevent once no other
191 // code here uses glib directly.
192 chromeos::GlibMessageLoop loop;
193 loop.SetAsCurrent();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800194
Alex Deymo67363ee2014-09-23 23:07:44 -0700195 // Wait up to 2 minutes for DBus to be ready.
196 LOG_IF(FATAL, !chromeos_update_engine::WaitForDBusSystem(
197 base::TimeDelta::FromSeconds(kDBusSystemMaxWaitSeconds)))
198 << "Failed to initialize DBus, aborting.";
199
Jay Srinivasanf0572052012-10-23 18:12:56 -0700200 chromeos_update_engine::RealSystemState real_system_state;
Nam T. Nguyen7d623eb2014-05-13 16:06:28 -0700201 LOG_IF(ERROR, !real_system_state.Initialize())
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800202 << "Failed to initialize system state.";
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800203 chromeos_update_engine::UpdateAttempter *update_attempter =
204 real_system_state.update_attempter();
205 CHECK(update_attempter);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700206
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700207 // Sets static members for the certificate checker.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800208 chromeos_update_engine::CertificateChecker::set_system_state(
209 &real_system_state);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700210 chromeos_update_engine::OpenSSLWrapper openssl_wrapper;
211 chromeos_update_engine::CertificateChecker::set_openssl_wrapper(
212 &openssl_wrapper);
213
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700214 // Create the dbus service object:
215 dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE,
216 &dbus_glib_update_engine_service_object_info);
Alex Deymo7e8ac062014-05-01 12:56:58 -0700217 UpdateEngineService* service = update_engine_service_new();
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700218 service->system_state_ = &real_system_state;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800219 update_attempter->set_dbus_service(service);
Alex Deymo67363ee2014-09-23 23:07:44 -0700220 chromeos_update_engine::SetupDBusService(service);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800221
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700222 // Initiate update checks.
223 update_attempter->ScheduleUpdates();
Andrew de los Reyes73520672010-05-10 13:44:58 -0700224
Darin Petkoveee26ee2010-10-22 11:05:46 -0700225 // Update boot flags after 45 seconds.
Alex Deymocc169252015-06-10 19:10:14 -0700226 loop.PostDelayedTask(
Alex Deymo0bb23412015-06-19 00:04:46 -0700227 FROM_HERE,
Alex Deymocc169252015-06-10 19:10:14 -0700228 base::Bind(&chromeos_update_engine::UpdateAttempter::UpdateBootFlags,
229 base::Unretained(update_attempter)),
230 base::TimeDelta::FromSeconds(45));
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -0700231
Darin Petkov61635a92011-05-18 16:20:36 -0700232 // Broadcast the update engine status on startup to ensure consistent system
233 // state on crashes.
Alex Deymo0bb23412015-06-19 00:04:46 -0700234 loop.PostTask(FROM_HERE, base::Bind(
Alex Deymocc169252015-06-10 19:10:14 -0700235 &chromeos_update_engine::UpdateAttempter::BroadcastStatus,
236 base::Unretained(update_attempter)));
Darin Petkov61635a92011-05-18 16:20:36 -0700237
David Zeuthene4c58bf2013-06-18 17:26:50 -0700238 // Run the UpdateEngineStarted() method on |update_attempter|.
Alex Deymo0bb23412015-06-19 00:04:46 -0700239 loop.PostTask(FROM_HERE, base::Bind(
Alex Deymocc169252015-06-10 19:10:14 -0700240 &chromeos_update_engine::UpdateAttempter::UpdateEngineStarted,
241 base::Unretained(update_attempter)));
David Zeuthene4c58bf2013-06-18 17:26:50 -0700242
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700243 // Run the main loop until exit time:
Alex Deymocc169252015-06-10 19:10:14 -0700244 loop.Run();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700245
246 // Cleanup:
Alex Vakulenko75039d72014-03-25 12:36:28 -0700247 update_attempter->set_dbus_service(nullptr);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700248 g_object_unref(G_OBJECT(service));
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800249
Alex Deymocc169252015-06-10 19:10:14 -0700250 loop.ReleaseFromCurrent();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800251 LOG(INFO) << "Chrome OS Update Engine terminating";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000252 return 0;
253}