blob: 833afb2520f376f046d67d1a1459d94d1e0a0e07 [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"
Gilad Arnold7c04e762012-05-23 10:54:02 -070024#include "update_engine/gpio_handler.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 Srinivasan43488792012-06-19 00:25:31 -070029#include "update_engine/system_state.h"
Jay Srinivasan08fce042012-06-07 16:31:01 -070030#include "update_engine/utils.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070031
32extern "C" {
33#include "update_engine/update_engine.dbusserver.h"
34}
35
36DEFINE_bool(logtostderr, false,
37 "Write logs to stderr instead of to a file in log_dir.");
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070038DEFINE_bool(foreground, false,
39 "Don't daemon()ize; run in foreground.");
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080040
41using std::string;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080042using std::vector;
43
44namespace chromeos_update_engine {
45
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070046gboolean UpdateBootFlags(void* arg) {
Darin Petkov61635a92011-05-18 16:20:36 -070047 reinterpret_cast<UpdateAttempter*>(arg)->UpdateBootFlags();
48 return FALSE; // Don't call this callback again
49}
50
51gboolean BroadcastStatus(void* arg) {
52 reinterpret_cast<UpdateAttempter*>(arg)->BroadcastStatus();
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070053 return FALSE; // Don't call this callback again
54}
55
Andrew de los Reyes73520672010-05-10 13:44:58 -070056namespace {
57
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070058void SetupDbusService(UpdateEngineService* service) {
59 DBusGConnection *bus;
60 DBusGProxy *proxy;
61 GError *error = NULL;
62
63 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
Darin Petkova0b9e772011-10-06 05:05:56 -070064 LOG_IF(FATAL, !bus) << "Failed to get bus: "
65 << utils::GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070066 proxy = dbus_g_proxy_new_for_name(bus,
67 DBUS_SERVICE_DBUS,
68 DBUS_PATH_DBUS,
69 DBUS_INTERFACE_DBUS);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070070 guint32 request_name_ret;
71 if (!org_freedesktop_DBus_request_name(proxy,
72 kUpdateEngineServiceName,
73 0,
74 &request_name_ret,
75 &error)) {
Darin Petkova0b9e772011-10-06 05:05:56 -070076 LOG(FATAL) << "Failed to get name: " << utils::GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070077 }
78 if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
79 g_warning("Got result code %u from requesting name", request_name_ret);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070080 LOG(FATAL) << "Got result code " << request_name_ret
81 << " from requesting name, but expected "
82 << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
83 }
84 dbus_g_connection_register_g_object(bus,
85 "/org/chromium/UpdateEngine",
86 G_OBJECT(service));
87}
88
Darin Petkov30291ed2010-11-12 10:23:06 -080089void SetupLogSymlink(const string& symlink_path, const string& log_path) {
90 // TODO(petkov): To ensure a smooth transition between non-timestamped and
91 // timestamped logs, move an existing log to start the first timestamped
92 // one. This code can go away once all clients are switched to this version or
93 // we stop caring about the old-style logs.
94 if (utils::FileExists(symlink_path.c_str()) &&
95 !utils::IsSymlink(symlink_path.c_str())) {
96 file_util::ReplaceFile(FilePath(symlink_path), FilePath(log_path));
97 }
98 file_util::Delete(FilePath(symlink_path), true);
99 if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
100 PLOG(ERROR) << "Unable to create symlink " << symlink_path
101 << " pointing at " << log_path;
102 }
103}
104
105string GetTimeAsString(time_t utime) {
106 struct tm tm;
107 CHECK(localtime_r(&utime, &tm) == &tm);
108 char str[16];
109 CHECK(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm) == 15);
110 return str;
111}
112
113string SetupLogFile(const string& kLogsRoot) {
114 const string kLogSymlink = kLogsRoot + "/update_engine.log";
115 const string kLogsDir = kLogsRoot + "/update_engine";
116 const string kLogPath =
117 StringPrintf("%s/update_engine.%s",
118 kLogsDir.c_str(),
119 GetTimeAsString(::time(NULL)).c_str());
120 mkdir(kLogsDir.c_str(), 0755);
121 SetupLogSymlink(kLogSymlink, kLogPath);
122 return kLogSymlink;
123}
124
125void SetupLogging() {
126 // Log to stderr initially.
127 logging::InitLogging(NULL,
128 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
129 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700130 logging::APPEND_TO_OLD_LOG_FILE,
131 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Darin Petkov30291ed2010-11-12 10:23:06 -0800132 if (FLAGS_logtostderr) {
133 return;
134 }
135 const string log_file = SetupLogFile("/var/log");
136 logging::InitLogging(log_file.c_str(),
137 logging::LOG_ONLY_TO_FILE,
138 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700139 logging::APPEND_TO_OLD_LOG_FILE,
140 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Darin Petkov30291ed2010-11-12 10:23:06 -0800141}
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800142
Andrew de los Reyes73520672010-05-10 13:44:58 -0700143} // namespace {}
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800144} // namespace chromeos_update_engine
rspangler@google.com49fdf182009-10-10 00:57:34 +0000145
rspangler@google.com49fdf182009-10-10 00:57:34 +0000146int main(int argc, char** argv) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700147 ::g_type_init();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000148 g_thread_init(NULL);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700149 dbus_g_thread_init();
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700150 base::AtExitManager exit_manager; // Required for base/rand_util.h.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700151 chromeos_update_engine::Terminator::Init();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000152 chromeos_update_engine::Subprocess::Init();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800153 google::ParseCommandLineFlags(&argc, &argv, true);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700154 CommandLine::Init(argc, argv);
Darin Petkov30291ed2010-11-12 10:23:06 -0800155 chromeos_update_engine::SetupLogging();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700156 if (!FLAGS_foreground)
157 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
158
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800159 LOG(INFO) << "Chrome OS Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700160
Chris Masone4dc2ada2010-09-23 12:43:03 -0700161 // Ensure that all written files have safe permissions.
162 // This is a mask, so we _block_ execute for the owner, and ALL
163 // permissions for other users.
164 // Done _after_ log file creation.
165 umask(S_IXUSR | S_IRWXG | S_IRWXO);
166
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800167 // Create the single GMainLoop
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700168 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800169
Jay Srinivasanf0572052012-10-23 18:12:56 -0700170 chromeos_update_engine::RealSystemState real_system_state;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800171 LOG_IF(ERROR, !real_system_state.Initialize())
172 << "Failed to initialize system state.";
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700173
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700174 // Sets static members for the certificate checker.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800175 chromeos_update_engine::CertificateChecker::set_system_state(
176 &real_system_state);
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700177 chromeos_update_engine::OpenSSLWrapper openssl_wrapper;
178 chromeos_update_engine::CertificateChecker::set_openssl_wrapper(
179 &openssl_wrapper);
180
Gilad Arnold7c04e762012-05-23 10:54:02 -0700181 // Initialize a GPIO handler. Defer GPIO discovery to ensure the udev has
182 // ample time to export the devices. Also require that test mode is physically
183 // queries at most once and the result cached, for a more consistent update
184 // behavior.
185 chromeos_update_engine::StandardUdevInterface udev_iface;
186 chromeos_update_engine::EintrSafeFileDescriptor file_descriptor;
187 chromeos_update_engine::StandardGpioHandler
188 gpio_handler(&udev_iface, &file_descriptor, true, true);
189
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700190 // Create the update attempter:
Andrew de los Reyes45168102010-11-22 11:13:50 -0800191 chromeos_update_engine::ConcreteDbusGlib dbus;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800192 chromeos_update_engine::UpdateAttempter update_attempter(&real_system_state,
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700193 &dbus,
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800194 &gpio_handler);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800195
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700196 // Create the dbus service object:
197 dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE,
198 &dbus_glib_update_engine_service_object_info);
199 UpdateEngineService* service =
200 UPDATE_ENGINE_SERVICE(g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL));
201 service->update_attempter_ = &update_attempter;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700202 update_attempter.set_dbus_service(service);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700203 chromeos_update_engine::SetupDbusService(service);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800204
Darin Petkov1023a602010-08-30 13:47:51 -0700205 // Schedule periodic update checks.
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700206 chromeos_update_engine::UpdateCheckScheduler scheduler(&update_attempter,
Gilad Arnold7c04e762012-05-23 10:54:02 -0700207 &gpio_handler,
Jay Srinivasan08fce042012-06-07 16:31:01 -0700208 &real_system_state);
Darin Petkov1023a602010-08-30 13:47:51 -0700209 scheduler.Run();
Andrew de los Reyes73520672010-05-10 13:44:58 -0700210
Darin Petkoveee26ee2010-10-22 11:05:46 -0700211 // Update boot flags after 45 seconds.
212 g_timeout_add_seconds(45,
213 &chromeos_update_engine::UpdateBootFlags,
214 &update_attempter);
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -0700215
Darin Petkov61635a92011-05-18 16:20:36 -0700216 // Broadcast the update engine status on startup to ensure consistent system
217 // state on crashes.
218 g_idle_add(&chromeos_update_engine::BroadcastStatus, &update_attempter);
219
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700220 // Run the main loop until exit time:
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800221 g_main_loop_run(loop);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700222
223 // Cleanup:
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800224 g_main_loop_unref(loop);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700225 update_attempter.set_dbus_service(NULL);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700226 g_object_unref(G_OBJECT(service));
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800227
228 LOG(INFO) << "Chrome OS Update Engine terminating";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000229 return 0;
230}