blob: 345d6e1746759b478bd0ebd2ed7a8c082d0c73bc [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
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08005#include <string>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08006#include <vector>
Darin Petkov9d65b7b2010-07-20 09:13:01 -07007
Darin Petkov1023a602010-08-30 13:47:51 -07008#include <base/at_exit.h>
9#include <base/command_line.h>
Darin Petkov30291ed2010-11-12 10:23:06 -080010#include <base/file_util.h>
Darin Petkov1023a602010-08-30 13:47:51 -070011#include <base/logging.h>
12#include <base/string_util.h>
Mike Frysinger8155d082012-04-06 15:23:18 -040013#include <base/stringprintf.h>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080014#include <gflags/gflags.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000015#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070016#include <metrics/metrics_library.h>
Chris Masone4dc2ada2010-09-23 12:43:03 -070017#include <sys/types.h>
18#include <sys/stat.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070019
Bruno Rocha7f9aea22011-09-12 14:31:24 -070020#include "update_engine/certificate_checker.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070021#include "update_engine/dbus_constants.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080022#include "update_engine/dbus_interface.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070023#include "update_engine/dbus_service.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080024#include "update_engine/real_system_state.h"
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070025#include "update_engine/subprocess.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070026#include "update_engine/terminator.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070027#include "update_engine/update_attempter.h"
Darin Petkov1023a602010-08-30 13:47:51 -070028#include "update_engine/update_check_scheduler.h"
Jay Srinivasan08fce042012-06-07 16:31:01 -070029#include "update_engine/utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070030
31extern "C" {
32#include "update_engine/update_engine.dbusserver.h"
33}
34
35DEFINE_bool(logtostderr, false,
36 "Write logs to stderr instead of to a file in log_dir.");
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070037DEFINE_bool(foreground, false,
38 "Don't daemon()ize; run in foreground.");
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080039
40using std::string;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080041using std::vector;
42
43namespace chromeos_update_engine {
44
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070045gboolean UpdateBootFlags(void* arg) {
Darin Petkov61635a92011-05-18 16:20:36 -070046 reinterpret_cast<UpdateAttempter*>(arg)->UpdateBootFlags();
47 return FALSE; // Don't call this callback again
48}
49
50gboolean BroadcastStatus(void* arg) {
51 reinterpret_cast<UpdateAttempter*>(arg)->BroadcastStatus();
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070052 return FALSE; // Don't call this callback again
53}
54
David Zeuthene4c58bf2013-06-18 17:26:50 -070055gboolean UpdateEngineStarted(gpointer user_data) {
56 reinterpret_cast<UpdateAttempter*>(user_data)->UpdateEngineStarted();
57 return FALSE; // Remove idle source (e.g. don't do the callback again)
58}
59
Andrew de los Reyes73520672010-05-10 13:44:58 -070060namespace {
61
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070062void SetupDbusService(UpdateEngineService* service) {
63 DBusGConnection *bus;
64 DBusGProxy *proxy;
65 GError *error = NULL;
66
67 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
Darin Petkova0b9e772011-10-06 05:05:56 -070068 LOG_IF(FATAL, !bus) << "Failed to get bus: "
69 << utils::GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070070 proxy = dbus_g_proxy_new_for_name(bus,
71 DBUS_SERVICE_DBUS,
72 DBUS_PATH_DBUS,
73 DBUS_INTERFACE_DBUS);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070074 guint32 request_name_ret;
75 if (!org_freedesktop_DBus_request_name(proxy,
76 kUpdateEngineServiceName,
77 0,
78 &request_name_ret,
79 &error)) {
Darin Petkova0b9e772011-10-06 05:05:56 -070080 LOG(FATAL) << "Failed to get name: " << utils::GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070081 }
82 if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
83 g_warning("Got result code %u from requesting name", request_name_ret);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070084 LOG(FATAL) << "Got result code " << request_name_ret
85 << " from requesting name, but expected "
86 << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
87 }
88 dbus_g_connection_register_g_object(bus,
89 "/org/chromium/UpdateEngine",
90 G_OBJECT(service));
91}
92
Darin Petkov30291ed2010-11-12 10:23:06 -080093void SetupLogSymlink(const string& symlink_path, const string& log_path) {
94 // TODO(petkov): To ensure a smooth transition between non-timestamped and
95 // timestamped logs, move an existing log to start the first timestamped
96 // one. This code can go away once all clients are switched to this version or
97 // we stop caring about the old-style logs.
98 if (utils::FileExists(symlink_path.c_str()) &&
99 !utils::IsSymlink(symlink_path.c_str())) {
100 file_util::ReplaceFile(FilePath(symlink_path), FilePath(log_path));
101 }
102 file_util::Delete(FilePath(symlink_path), true);
103 if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
104 PLOG(ERROR) << "Unable to create symlink " << symlink_path
105 << " pointing at " << log_path;
106 }
107}
108
109string GetTimeAsString(time_t utime) {
110 struct tm tm;
111 CHECK(localtime_r(&utime, &tm) == &tm);
112 char str[16];
113 CHECK(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm) == 15);
114 return str;
115}
116
117string SetupLogFile(const string& kLogsRoot) {
118 const string kLogSymlink = kLogsRoot + "/update_engine.log";
119 const string kLogsDir = kLogsRoot + "/update_engine";
120 const string kLogPath =
121 StringPrintf("%s/update_engine.%s",
122 kLogsDir.c_str(),
123 GetTimeAsString(::time(NULL)).c_str());
124 mkdir(kLogsDir.c_str(), 0755);
125 SetupLogSymlink(kLogSymlink, kLogPath);
126 return kLogSymlink;
127}
128
129void SetupLogging() {
130 // Log to stderr initially.
131 logging::InitLogging(NULL,
132 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
133 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700134 logging::APPEND_TO_OLD_LOG_FILE,
135 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Darin Petkov30291ed2010-11-12 10:23:06 -0800136 if (FLAGS_logtostderr) {
137 return;
138 }
139 const string log_file = SetupLogFile("/var/log");
140 logging::InitLogging(log_file.c_str(),
141 logging::LOG_ONLY_TO_FILE,
142 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700143 logging::APPEND_TO_OLD_LOG_FILE,
144 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Darin Petkov30291ed2010-11-12 10:23:06 -0800145}
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800146
Andrew de los Reyes73520672010-05-10 13:44:58 -0700147} // namespace {}
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800148} // namespace chromeos_update_engine
rspangler@google.com49fdf182009-10-10 00:57:34 +0000149
rspangler@google.com49fdf182009-10-10 00:57:34 +0000150int main(int argc, char** argv) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700151 ::g_type_init();
Ben Chan46bf5c82013-06-24 11:17:41 -0700152 dbus_threads_init_default();
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700153 base::AtExitManager exit_manager; // Required for base/rand_util.h.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700154 chromeos_update_engine::Terminator::Init();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000155 chromeos_update_engine::Subprocess::Init();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800156 google::ParseCommandLineFlags(&argc, &argv, true);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700157 CommandLine::Init(argc, argv);
Darin Petkov30291ed2010-11-12 10:23:06 -0800158 chromeos_update_engine::SetupLogging();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700159 if (!FLAGS_foreground)
160 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
161
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800162 LOG(INFO) << "Chrome OS Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700163
Chris Masone4dc2ada2010-09-23 12:43:03 -0700164 // Ensure that all written files have safe permissions.
165 // This is a mask, so we _block_ execute for the owner, and ALL
166 // permissions for other users.
167 // Done _after_ log file creation.
168 umask(S_IXUSR | S_IRWXG | S_IRWXO);
169
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800170 // Create the single GMainLoop
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700171 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800172
Jay Srinivasanf0572052012-10-23 18:12:56 -0700173 chromeos_update_engine::RealSystemState real_system_state;
Gilad Arnoldbf7919b2013-01-08 13:07:37 -0800174 // TODO(garnold) s/false/true/ once we decide to activate actual GPIO-based
175 // protocol for testing of MP-signed images (chromium-os:25400).
176 LOG_IF(ERROR, !real_system_state.Initialize(false))
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800177 << "Failed to initialize system state.";
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800178 chromeos_update_engine::UpdateAttempter *update_attempter =
179 real_system_state.update_attempter();
180 CHECK(update_attempter);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700181
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700182 // Sets static members for the certificate checker.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800183 chromeos_update_engine::CertificateChecker::set_system_state(
184 &real_system_state);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700185 chromeos_update_engine::OpenSSLWrapper openssl_wrapper;
186 chromeos_update_engine::CertificateChecker::set_openssl_wrapper(
187 &openssl_wrapper);
188
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700189 // Create the dbus service object:
190 dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE,
191 &dbus_glib_update_engine_service_object_info);
192 UpdateEngineService* service =
193 UPDATE_ENGINE_SERVICE(g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700194 service->system_state_ = &real_system_state;
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800195 update_attempter->set_dbus_service(service);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700196 chromeos_update_engine::SetupDbusService(service);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800197
Darin Petkov1023a602010-08-30 13:47:51 -0700198 // Schedule periodic update checks.
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800199 chromeos_update_engine::UpdateCheckScheduler scheduler(update_attempter,
Jay Srinivasan08fce042012-06-07 16:31:01 -0700200 &real_system_state);
Darin Petkov1023a602010-08-30 13:47:51 -0700201 scheduler.Run();
Andrew de los Reyes73520672010-05-10 13:44:58 -0700202
Darin Petkoveee26ee2010-10-22 11:05:46 -0700203 // Update boot flags after 45 seconds.
204 g_timeout_add_seconds(45,
205 &chromeos_update_engine::UpdateBootFlags,
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800206 update_attempter);
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -0700207
Darin Petkov61635a92011-05-18 16:20:36 -0700208 // Broadcast the update engine status on startup to ensure consistent system
209 // state on crashes.
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800210 g_idle_add(&chromeos_update_engine::BroadcastStatus, update_attempter);
Darin Petkov61635a92011-05-18 16:20:36 -0700211
David Zeuthene4c58bf2013-06-18 17:26:50 -0700212 // Run the UpdateEngineStarted() method on |update_attempter|.
213 g_idle_add(&chromeos_update_engine::UpdateEngineStarted, update_attempter);
214
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700215 // Run the main loop until exit time:
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800216 g_main_loop_run(loop);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700217
218 // Cleanup:
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800219 g_main_loop_unref(loop);
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800220 update_attempter->set_dbus_service(NULL);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700221 g_object_unref(G_OBJECT(service));
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800222
223 LOG(INFO) << "Chrome OS Update Engine terminating";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000224 return 0;
225}