Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 2 | // 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 Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 5 | #include <string> |
| 6 | #include <tr1/memory> |
| 7 | #include <vector> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 8 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 9 | #include <gflags/gflags.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 10 | #include <glib.h> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 11 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame^] | 12 | #include "base/at_exit.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 13 | #include "base/command_line.h" |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 14 | #include "base/logging.h" |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 15 | #include "base/string_util.h" |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 16 | #include "metrics/metrics_library.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 17 | #include "update_engine/dbus_constants.h" |
| 18 | #include "update_engine/dbus_service.h" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 19 | #include "update_engine/prefs.h" |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 20 | #include "update_engine/subprocess.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 21 | #include "update_engine/update_attempter.h" |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 22 | #include "update_engine/utils.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 23 | |
| 24 | extern "C" { |
| 25 | #include "update_engine/update_engine.dbusserver.h" |
| 26 | } |
| 27 | |
| 28 | DEFINE_bool(logtostderr, false, |
| 29 | "Write logs to stderr instead of to a file in log_dir."); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 30 | DEFINE_bool(foreground, false, |
| 31 | "Don't daemon()ize; run in foreground."); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 32 | |
| 33 | using std::string; |
| 34 | using std::tr1::shared_ptr; |
| 35 | using std::vector; |
| 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 39 | gboolean UpdateBootFlags(void* arg) { |
| 40 | // This purely best effort. Failures should be logged by Subprocess. |
| 41 | int unused = 0; |
| 42 | vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel"); |
| 43 | Subprocess::SynchronousExec(cmd, &unused); |
| 44 | return FALSE; // Don't call this callback again |
| 45 | } |
| 46 | |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 47 | namespace { |
| 48 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame^] | 49 | const int kTimeoutOnce = 7 * 60; // at 7 minutes |
| 50 | const int kTimeoutPeriodic = 45 * 60; // every 45 minutes |
| 51 | const int kTimeoutFuzz = 10 * 60; // +/- 5 minutes |
| 52 | |
| 53 | // Schedules an update check |seconds| from now, while adding some fuzz. |
| 54 | void ScheduleUpdateCheck(int seconds, |
| 55 | GSourceFunc update_function, |
| 56 | UpdateAttempter* update_attempter) { |
| 57 | seconds = utils::FuzzInt(seconds, kTimeoutFuzz); |
| 58 | g_timeout_add_seconds(seconds, update_function, update_attempter); |
| 59 | } |
| 60 | |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 61 | gboolean UpdateOnce(void* arg) { |
| 62 | UpdateAttempter* update_attempter = reinterpret_cast<UpdateAttempter*>(arg); |
| 63 | update_attempter->Update("", ""); |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame^] | 64 | return FALSE; // Don't run again. |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 65 | } |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 66 | |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 67 | gboolean UpdatePeriodically(void* arg) { |
| 68 | UpdateAttempter* update_attempter = reinterpret_cast<UpdateAttempter*>(arg); |
| 69 | update_attempter->Update("", ""); |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame^] | 70 | ScheduleUpdateCheck(kTimeoutPeriodic, &UpdatePeriodically, update_attempter); |
| 71 | return FALSE; // Don't run again. |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void SchedulePeriodicUpdateChecks(UpdateAttempter* update_attempter) { |
| 75 | if (!utils::IsOfficialBuild()) { |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 76 | LOG(WARNING) << "Non-official build: periodic update checks disabled."; |
| 77 | return; |
| 78 | } |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 79 | if (utils::IsRemovableDevice(utils::RootDevice(utils::BootDevice()))) { |
| 80 | LOG(WARNING) << "Removable device boot: periodic update checks disabled."; |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 81 | return; |
| 82 | } |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame^] | 83 | // Kick off periodic updating. |
| 84 | ScheduleUpdateCheck(kTimeoutOnce, &UpdateOnce, update_attempter); |
| 85 | ScheduleUpdateCheck(kTimeoutPeriodic, &UpdatePeriodically, update_attempter); |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 88 | void SetupDbusService(UpdateEngineService* service) { |
| 89 | DBusGConnection *bus; |
| 90 | DBusGProxy *proxy; |
| 91 | GError *error = NULL; |
| 92 | |
| 93 | bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); |
| 94 | if (!bus) { |
| 95 | LOG(FATAL) << "Failed to get bus"; |
| 96 | } |
| 97 | proxy = dbus_g_proxy_new_for_name(bus, |
| 98 | DBUS_SERVICE_DBUS, |
| 99 | DBUS_PATH_DBUS, |
| 100 | DBUS_INTERFACE_DBUS); |
| 101 | |
| 102 | guint32 request_name_ret; |
| 103 | if (!org_freedesktop_DBus_request_name(proxy, |
| 104 | kUpdateEngineServiceName, |
| 105 | 0, |
| 106 | &request_name_ret, |
| 107 | &error)) { |
| 108 | LOG(FATAL) << "Failed to get name: " << error->message; |
| 109 | } |
| 110 | if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { |
| 111 | g_warning("Got result code %u from requesting name", request_name_ret); |
| 112 | g_error_free(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 113 | LOG(FATAL) << "Got result code " << request_name_ret |
| 114 | << " from requesting name, but expected " |
| 115 | << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER; |
| 116 | } |
| 117 | dbus_g_connection_register_g_object(bus, |
| 118 | "/org/chromium/UpdateEngine", |
| 119 | G_OBJECT(service)); |
| 120 | } |
| 121 | |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 122 | } // namespace {} |
| 123 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 124 | } // namespace chromeos_update_engine |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 125 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 126 | #include "update_engine/subprocess.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 127 | |
| 128 | int main(int argc, char** argv) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 129 | ::g_type_init(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 130 | g_thread_init(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 131 | dbus_g_thread_init(); |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame^] | 132 | base::AtExitManager exit_manager; // Required for base/rand_util.h. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 133 | chromeos_update_engine::Subprocess::Init(); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 134 | google::ParseCommandLineFlags(&argc, &argv, true); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 135 | CommandLine::Init(argc, argv); |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 136 | logging::InitLogging("/var/log/update_engine.log", |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 137 | (FLAGS_logtostderr ? |
| 138 | logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG : |
| 139 | logging::LOG_ONLY_TO_FILE), |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 140 | logging::DONT_LOCK_LOG_FILE, |
| 141 | logging::APPEND_TO_OLD_LOG_FILE); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 142 | if (!FLAGS_foreground) |
| 143 | PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed"; |
| 144 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 145 | LOG(INFO) << "Chrome OS Update Engine starting"; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 146 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 147 | // Create the single GMainLoop |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 148 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 149 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 150 | chromeos_update_engine::Prefs prefs; |
| 151 | LOG_IF(ERROR, !prefs.Init(FilePath("/var/lib/update_engine/prefs"))) |
| 152 | << "Failed to initialize preferences."; |
| 153 | |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 154 | MetricsLibrary metrics_lib; |
| 155 | metrics_lib.Init(); |
| 156 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 157 | // Create the update attempter: |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 158 | chromeos_update_engine::UpdateAttempter update_attempter(&prefs, |
| 159 | &metrics_lib); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 160 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 161 | // Create the dbus service object: |
| 162 | dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE, |
| 163 | &dbus_glib_update_engine_service_object_info); |
| 164 | UpdateEngineService* service = |
| 165 | UPDATE_ENGINE_SERVICE(g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL)); |
| 166 | service->update_attempter_ = &update_attempter; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 167 | update_attempter.set_dbus_service(service); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 168 | chromeos_update_engine::SetupDbusService(service); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 169 | |
Darin Petkov | 33d3064 | 2010-08-04 10:18:57 -0700 | [diff] [blame] | 170 | chromeos_update_engine::SchedulePeriodicUpdateChecks(&update_attempter); |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 171 | |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 172 | // Update boot flags after 45 seconds |
| 173 | g_timeout_add_seconds(45, &chromeos_update_engine::UpdateBootFlags, NULL); |
| 174 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 175 | // Run the main loop until exit time: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 176 | g_main_loop_run(loop); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 177 | |
| 178 | // Cleanup: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 179 | g_main_loop_unref(loop); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 180 | update_attempter.set_dbus_service(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 181 | g_object_unref(G_OBJECT(service)); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 182 | |
| 183 | LOG(INFO) << "Chrome OS Update Engine terminating"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |