blob: fd84e50d38e4b6a52301df56bc9399f2b81fb73f [file] [log] [blame]
Chris Masoned903c3b2011-05-12 15:35:46 -07001// Copyright (c) 2011 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>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080013#include <gflags/gflags.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000014#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070015#include <metrics/metrics_library.h>
Chris Masone4dc2ada2010-09-23 12:43:03 -070016#include <sys/types.h>
17#include <sys/stat.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070018
Bruno Rocha7f9aea22011-09-12 14:31:24 -070019#include "update_engine/certificate_checker.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070020#include "update_engine/dbus_constants.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080021#include "update_engine/dbus_interface.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070022#include "update_engine/dbus_service.h"
Darin Petkov1cbd78f2010-07-29 12:38:34 -070023#include "update_engine/prefs.h"
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070024#include "update_engine/subprocess.h"
Darin Petkov9c0baf82010-10-07 13:44:48 -070025#include "update_engine/terminator.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026#include "update_engine/update_attempter.h"
Darin Petkov1023a602010-08-30 13:47:51 -070027#include "update_engine/update_check_scheduler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070028
29extern "C" {
30#include "update_engine/update_engine.dbusserver.h"
31}
32
33DEFINE_bool(logtostderr, false,
34 "Write logs to stderr instead of to a file in log_dir.");
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070035DEFINE_bool(foreground, false,
36 "Don't daemon()ize; run in foreground.");
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080037
38using std::string;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080039using std::vector;
40
41namespace chromeos_update_engine {
42
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070043gboolean UpdateBootFlags(void* arg) {
Darin Petkov61635a92011-05-18 16:20:36 -070044 reinterpret_cast<UpdateAttempter*>(arg)->UpdateBootFlags();
45 return FALSE; // Don't call this callback again
46}
47
48gboolean BroadcastStatus(void* arg) {
49 reinterpret_cast<UpdateAttempter*>(arg)->BroadcastStatus();
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -070050 return FALSE; // Don't call this callback again
51}
52
Andrew de los Reyes73520672010-05-10 13:44:58 -070053namespace {
54
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070055void SetupDbusService(UpdateEngineService* service) {
56 DBusGConnection *bus;
57 DBusGProxy *proxy;
58 GError *error = NULL;
59
60 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
Darin Petkova0b9e772011-10-06 05:05:56 -070061 LOG_IF(FATAL, !bus) << "Failed to get bus: "
62 << utils::GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070063 proxy = dbus_g_proxy_new_for_name(bus,
64 DBUS_SERVICE_DBUS,
65 DBUS_PATH_DBUS,
66 DBUS_INTERFACE_DBUS);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070067 guint32 request_name_ret;
68 if (!org_freedesktop_DBus_request_name(proxy,
69 kUpdateEngineServiceName,
70 0,
71 &request_name_ret,
72 &error)) {
Darin Petkova0b9e772011-10-06 05:05:56 -070073 LOG(FATAL) << "Failed to get name: " << utils::GetAndFreeGError(&error);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070074 }
75 if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
76 g_warning("Got result code %u from requesting name", request_name_ret);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070077 LOG(FATAL) << "Got result code " << request_name_ret
78 << " from requesting name, but expected "
79 << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
80 }
81 dbus_g_connection_register_g_object(bus,
82 "/org/chromium/UpdateEngine",
83 G_OBJECT(service));
84}
85
Darin Petkov30291ed2010-11-12 10:23:06 -080086void SetupLogSymlink(const string& symlink_path, const string& log_path) {
87 // TODO(petkov): To ensure a smooth transition between non-timestamped and
88 // timestamped logs, move an existing log to start the first timestamped
89 // one. This code can go away once all clients are switched to this version or
90 // we stop caring about the old-style logs.
91 if (utils::FileExists(symlink_path.c_str()) &&
92 !utils::IsSymlink(symlink_path.c_str())) {
93 file_util::ReplaceFile(FilePath(symlink_path), FilePath(log_path));
94 }
95 file_util::Delete(FilePath(symlink_path), true);
96 if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) {
97 PLOG(ERROR) << "Unable to create symlink " << symlink_path
98 << " pointing at " << log_path;
99 }
100}
101
102string GetTimeAsString(time_t utime) {
103 struct tm tm;
104 CHECK(localtime_r(&utime, &tm) == &tm);
105 char str[16];
106 CHECK(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm) == 15);
107 return str;
108}
109
110string SetupLogFile(const string& kLogsRoot) {
111 const string kLogSymlink = kLogsRoot + "/update_engine.log";
112 const string kLogsDir = kLogsRoot + "/update_engine";
113 const string kLogPath =
114 StringPrintf("%s/update_engine.%s",
115 kLogsDir.c_str(),
116 GetTimeAsString(::time(NULL)).c_str());
117 mkdir(kLogsDir.c_str(), 0755);
118 SetupLogSymlink(kLogSymlink, kLogPath);
119 return kLogSymlink;
120}
121
122void SetupLogging() {
123 // Log to stderr initially.
124 logging::InitLogging(NULL,
125 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
126 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700127 logging::APPEND_TO_OLD_LOG_FILE,
128 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Darin Petkov30291ed2010-11-12 10:23:06 -0800129 if (FLAGS_logtostderr) {
130 return;
131 }
132 const string log_file = SetupLogFile("/var/log");
133 logging::InitLogging(log_file.c_str(),
134 logging::LOG_ONLY_TO_FILE,
135 logging::DONT_LOCK_LOG_FILE,
Chris Masoned903c3b2011-05-12 15:35:46 -0700136 logging::APPEND_TO_OLD_LOG_FILE,
137 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
Darin Petkov30291ed2010-11-12 10:23:06 -0800138}
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800139
Andrew de los Reyes73520672010-05-10 13:44:58 -0700140} // namespace {}
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800141} // namespace chromeos_update_engine
rspangler@google.com49fdf182009-10-10 00:57:34 +0000142
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143int main(int argc, char** argv) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700144 ::g_type_init();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000145 g_thread_init(NULL);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700146 dbus_g_thread_init();
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700147 base::AtExitManager exit_manager; // Required for base/rand_util.h.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700148 chromeos_update_engine::Terminator::Init();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000149 chromeos_update_engine::Subprocess::Init();
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800150 google::ParseCommandLineFlags(&argc, &argv, true);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700151 CommandLine::Init(argc, argv);
Darin Petkov30291ed2010-11-12 10:23:06 -0800152 chromeos_update_engine::SetupLogging();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700153 if (!FLAGS_foreground)
154 PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
155
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800156 LOG(INFO) << "Chrome OS Update Engine starting";
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700157
Chris Masone4dc2ada2010-09-23 12:43:03 -0700158 // Ensure that all written files have safe permissions.
159 // This is a mask, so we _block_ execute for the owner, and ALL
160 // permissions for other users.
161 // Done _after_ log file creation.
162 umask(S_IXUSR | S_IRWXG | S_IRWXO);
163
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800164 // Create the single GMainLoop
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700165 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800166
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700167 chromeos_update_engine::Prefs prefs;
168 LOG_IF(ERROR, !prefs.Init(FilePath("/var/lib/update_engine/prefs")))
169 << "Failed to initialize preferences.";
170
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700171 MetricsLibrary metrics_lib;
172 metrics_lib.Init();
173
Bruno Rocha7f9aea22011-09-12 14:31:24 -0700174 // Sets static members for the certificate checker.
175 chromeos_update_engine::CertificateChecker::set_metrics_lib(&metrics_lib);
176 chromeos_update_engine::CertificateChecker::set_prefs(&prefs);
177 chromeos_update_engine::OpenSSLWrapper openssl_wrapper;
178 chromeos_update_engine::CertificateChecker::set_openssl_wrapper(
179 &openssl_wrapper);
180
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700181 // Create the update attempter:
Andrew de los Reyes45168102010-11-22 11:13:50 -0800182 chromeos_update_engine::ConcreteDbusGlib dbus;
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700183 chromeos_update_engine::UpdateAttempter update_attempter(&prefs,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800184 &metrics_lib,
185 &dbus);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800186
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700187 // Create the dbus service object:
188 dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE,
189 &dbus_glib_update_engine_service_object_info);
190 UpdateEngineService* service =
191 UPDATE_ENGINE_SERVICE(g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL));
192 service->update_attempter_ = &update_attempter;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700193 update_attempter.set_dbus_service(service);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700194 chromeos_update_engine::SetupDbusService(service);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800195
Darin Petkov1023a602010-08-30 13:47:51 -0700196 // Schedule periodic update checks.
197 chromeos_update_engine::UpdateCheckScheduler scheduler(&update_attempter);
198 scheduler.Run();
Andrew de los Reyes73520672010-05-10 13:44:58 -0700199
Darin Petkoveee26ee2010-10-22 11:05:46 -0700200 // Update boot flags after 45 seconds.
201 g_timeout_add_seconds(45,
202 &chromeos_update_engine::UpdateBootFlags,
203 &update_attempter);
Andrew de los Reyes3d6a2092010-08-23 18:23:26 -0700204
Darin Petkov61635a92011-05-18 16:20:36 -0700205 // Broadcast the update engine status on startup to ensure consistent system
206 // state on crashes.
207 g_idle_add(&chromeos_update_engine::BroadcastStatus, &update_attempter);
208
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700209 // Run the main loop until exit time:
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800210 g_main_loop_run(loop);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700211
212 // Cleanup:
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800213 g_main_loop_unref(loop);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700214 update_attempter.set_dbus_service(NULL);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700215 g_object_unref(G_OBJECT(service));
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800216
217 LOG(INFO) << "Chrome OS Update Engine terminating";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000218 return 0;
219}