Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 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> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 6 | #include <vector> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 7 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 8 | #include <base/at_exit.h> |
| 9 | #include <base/command_line.h> |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 10 | #include <base/file_util.h> |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
| 12 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 13 | #include <base/stringprintf.h> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 14 | #include <gflags/gflags.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 15 | #include <glib.h> |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 16 | #include <metrics/metrics_library.h> |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 19 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 20 | #include "update_engine/certificate_checker.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 21 | #include "update_engine/dbus_constants.h" |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 22 | #include "update_engine/dbus_interface.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 23 | #include "update_engine/dbus_service.h" |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 24 | #include "update_engine/gpio_handler.h" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 25 | #include "update_engine/prefs.h" |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 26 | #include "update_engine/subprocess.h" |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 27 | #include "update_engine/terminator.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 28 | #include "update_engine/update_attempter.h" |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 29 | #include "update_engine/update_check_scheduler.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 30 | #include "update_engine/system_state.h" |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 31 | #include "update_engine/utils.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 32 | |
| 33 | extern "C" { |
| 34 | #include "update_engine/update_engine.dbusserver.h" |
| 35 | } |
| 36 | |
| 37 | DEFINE_bool(logtostderr, false, |
| 38 | "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] | 39 | DEFINE_bool(foreground, false, |
| 40 | "Don't daemon()ize; run in foreground."); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 41 | |
| 42 | using std::string; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 43 | using std::vector; |
| 44 | |
| 45 | namespace chromeos_update_engine { |
| 46 | |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 47 | gboolean UpdateBootFlags(void* arg) { |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 48 | reinterpret_cast<UpdateAttempter*>(arg)->UpdateBootFlags(); |
| 49 | return FALSE; // Don't call this callback again |
| 50 | } |
| 51 | |
| 52 | gboolean BroadcastStatus(void* arg) { |
| 53 | reinterpret_cast<UpdateAttempter*>(arg)->BroadcastStatus(); |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 54 | return FALSE; // Don't call this callback again |
| 55 | } |
| 56 | |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 57 | namespace { |
| 58 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 59 | void SetupDbusService(UpdateEngineService* service) { |
| 60 | DBusGConnection *bus; |
| 61 | DBusGProxy *proxy; |
| 62 | GError *error = NULL; |
| 63 | |
| 64 | bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 65 | LOG_IF(FATAL, !bus) << "Failed to get bus: " |
| 66 | << utils::GetAndFreeGError(&error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 67 | proxy = dbus_g_proxy_new_for_name(bus, |
| 68 | DBUS_SERVICE_DBUS, |
| 69 | DBUS_PATH_DBUS, |
| 70 | DBUS_INTERFACE_DBUS); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 71 | guint32 request_name_ret; |
| 72 | if (!org_freedesktop_DBus_request_name(proxy, |
| 73 | kUpdateEngineServiceName, |
| 74 | 0, |
| 75 | &request_name_ret, |
| 76 | &error)) { |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 77 | LOG(FATAL) << "Failed to get name: " << utils::GetAndFreeGError(&error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 78 | } |
| 79 | if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { |
| 80 | g_warning("Got result code %u from requesting name", request_name_ret); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 81 | LOG(FATAL) << "Got result code " << request_name_ret |
| 82 | << " from requesting name, but expected " |
| 83 | << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER; |
| 84 | } |
| 85 | dbus_g_connection_register_g_object(bus, |
| 86 | "/org/chromium/UpdateEngine", |
| 87 | G_OBJECT(service)); |
| 88 | } |
| 89 | |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 90 | void SetupLogSymlink(const string& symlink_path, const string& log_path) { |
| 91 | // TODO(petkov): To ensure a smooth transition between non-timestamped and |
| 92 | // timestamped logs, move an existing log to start the first timestamped |
| 93 | // one. This code can go away once all clients are switched to this version or |
| 94 | // we stop caring about the old-style logs. |
| 95 | if (utils::FileExists(symlink_path.c_str()) && |
| 96 | !utils::IsSymlink(symlink_path.c_str())) { |
| 97 | file_util::ReplaceFile(FilePath(symlink_path), FilePath(log_path)); |
| 98 | } |
| 99 | file_util::Delete(FilePath(symlink_path), true); |
| 100 | if (symlink(log_path.c_str(), symlink_path.c_str()) == -1) { |
| 101 | PLOG(ERROR) << "Unable to create symlink " << symlink_path |
| 102 | << " pointing at " << log_path; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | string GetTimeAsString(time_t utime) { |
| 107 | struct tm tm; |
| 108 | CHECK(localtime_r(&utime, &tm) == &tm); |
| 109 | char str[16]; |
| 110 | CHECK(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm) == 15); |
| 111 | return str; |
| 112 | } |
| 113 | |
| 114 | string SetupLogFile(const string& kLogsRoot) { |
| 115 | const string kLogSymlink = kLogsRoot + "/update_engine.log"; |
| 116 | const string kLogsDir = kLogsRoot + "/update_engine"; |
| 117 | const string kLogPath = |
| 118 | StringPrintf("%s/update_engine.%s", |
| 119 | kLogsDir.c_str(), |
| 120 | GetTimeAsString(::time(NULL)).c_str()); |
| 121 | mkdir(kLogsDir.c_str(), 0755); |
| 122 | SetupLogSymlink(kLogSymlink, kLogPath); |
| 123 | return kLogSymlink; |
| 124 | } |
| 125 | |
| 126 | void SetupLogging() { |
| 127 | // Log to stderr initially. |
| 128 | logging::InitLogging(NULL, |
| 129 | logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, |
| 130 | logging::DONT_LOCK_LOG_FILE, |
Chris Masone | d903c3b | 2011-05-12 15:35:46 -0700 | [diff] [blame] | 131 | logging::APPEND_TO_OLD_LOG_FILE, |
| 132 | logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 133 | if (FLAGS_logtostderr) { |
| 134 | return; |
| 135 | } |
| 136 | const string log_file = SetupLogFile("/var/log"); |
| 137 | logging::InitLogging(log_file.c_str(), |
| 138 | logging::LOG_ONLY_TO_FILE, |
| 139 | logging::DONT_LOCK_LOG_FILE, |
Chris Masone | d903c3b | 2011-05-12 15:35:46 -0700 | [diff] [blame] | 140 | logging::APPEND_TO_OLD_LOG_FILE, |
| 141 | logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 142 | } |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 143 | |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 144 | } // namespace {} |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 145 | } // namespace chromeos_update_engine |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 146 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 147 | int main(int argc, char** argv) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 148 | ::g_type_init(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 149 | g_thread_init(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 150 | dbus_g_thread_init(); |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 151 | base::AtExitManager exit_manager; // Required for base/rand_util.h. |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 152 | chromeos_update_engine::Terminator::Init(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 153 | chromeos_update_engine::Subprocess::Init(); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 154 | google::ParseCommandLineFlags(&argc, &argv, true); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 155 | CommandLine::Init(argc, argv); |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 156 | chromeos_update_engine::SetupLogging(); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 157 | if (!FLAGS_foreground) |
| 158 | PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed"; |
| 159 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 160 | LOG(INFO) << "Chrome OS Update Engine starting"; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 161 | |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 162 | // Ensure that all written files have safe permissions. |
| 163 | // This is a mask, so we _block_ execute for the owner, and ALL |
| 164 | // permissions for other users. |
| 165 | // Done _after_ log file creation. |
| 166 | umask(S_IXUSR | S_IRWXG | S_IRWXO); |
| 167 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 168 | // Create the single GMainLoop |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 169 | 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] | 170 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 171 | chromeos_update_engine::Prefs prefs; |
| 172 | LOG_IF(ERROR, !prefs.Init(FilePath("/var/lib/update_engine/prefs"))) |
| 173 | << "Failed to initialize preferences."; |
| 174 | |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 175 | MetricsLibrary metrics_lib; |
| 176 | metrics_lib.Init(); |
| 177 | |
Bruno Rocha | 7f9aea2 | 2011-09-12 14:31:24 -0700 | [diff] [blame] | 178 | // Sets static members for the certificate checker. |
| 179 | chromeos_update_engine::CertificateChecker::set_metrics_lib(&metrics_lib); |
| 180 | chromeos_update_engine::CertificateChecker::set_prefs(&prefs); |
| 181 | chromeos_update_engine::OpenSSLWrapper openssl_wrapper; |
| 182 | chromeos_update_engine::CertificateChecker::set_openssl_wrapper( |
| 183 | &openssl_wrapper); |
| 184 | |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 185 | // Initialize a GPIO handler. Defer GPIO discovery to ensure the udev has |
| 186 | // ample time to export the devices. Also require that test mode is physically |
| 187 | // queries at most once and the result cached, for a more consistent update |
| 188 | // behavior. |
| 189 | chromeos_update_engine::StandardUdevInterface udev_iface; |
| 190 | chromeos_update_engine::EintrSafeFileDescriptor file_descriptor; |
| 191 | chromeos_update_engine::StandardGpioHandler |
| 192 | gpio_handler(&udev_iface, &file_descriptor, true, true); |
| 193 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 194 | // Create the update attempter: |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 195 | chromeos_update_engine::ConcreteDbusGlib dbus; |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 196 | chromeos_update_engine::RealSystemState real_system_state; |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 197 | chromeos_update_engine::UpdateAttempter update_attempter(&prefs, |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 198 | &metrics_lib, |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 199 | &dbus, |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 200 | &gpio_handler, |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 201 | &real_system_state); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 202 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 203 | // Create the dbus service object: |
| 204 | dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE, |
| 205 | &dbus_glib_update_engine_service_object_info); |
| 206 | UpdateEngineService* service = |
| 207 | UPDATE_ENGINE_SERVICE(g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL)); |
| 208 | service->update_attempter_ = &update_attempter; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 209 | update_attempter.set_dbus_service(service); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 210 | chromeos_update_engine::SetupDbusService(service); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 211 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 212 | // Schedule periodic update checks. |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 213 | chromeos_update_engine::UpdateCheckScheduler scheduler(&update_attempter, |
Gilad Arnold | 7c04e76 | 2012-05-23 10:54:02 -0700 | [diff] [blame] | 214 | &gpio_handler, |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 215 | &real_system_state); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 216 | scheduler.Run(); |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 217 | |
Darin Petkov | eee26ee | 2010-10-22 11:05:46 -0700 | [diff] [blame] | 218 | // Update boot flags after 45 seconds. |
| 219 | g_timeout_add_seconds(45, |
| 220 | &chromeos_update_engine::UpdateBootFlags, |
| 221 | &update_attempter); |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 222 | |
Darin Petkov | 61635a9 | 2011-05-18 16:20:36 -0700 | [diff] [blame] | 223 | // Broadcast the update engine status on startup to ensure consistent system |
| 224 | // state on crashes. |
| 225 | g_idle_add(&chromeos_update_engine::BroadcastStatus, &update_attempter); |
| 226 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 227 | // Run the main loop until exit time: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 228 | g_main_loop_run(loop); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 229 | |
| 230 | // Cleanup: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 231 | g_main_loop_unref(loop); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 232 | update_attempter.set_dbus_service(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 233 | g_object_unref(G_OBJECT(service)); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 234 | |
| 235 | LOG(INFO) << "Chrome OS Update Engine terminating"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 236 | return 0; |
| 237 | } |