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 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 9 | #include <base/at_exit.h> |
| 10 | #include <base/command_line.h> |
| 11 | #include <base/logging.h> |
| 12 | #include <base/string_util.h> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 13 | #include <gflags/gflags.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 14 | #include <glib.h> |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 15 | #include <metrics/metrics_library.h> |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 18 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 19 | #include "update_engine/dbus_constants.h" |
| 20 | #include "update_engine/dbus_service.h" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 21 | #include "update_engine/prefs.h" |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 22 | #include "update_engine/subprocess.h" |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 23 | #include "update_engine/terminator.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 24 | #include "update_engine/update_attempter.h" |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 25 | #include "update_engine/update_check_scheduler.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 26 | |
| 27 | extern "C" { |
| 28 | #include "update_engine/update_engine.dbusserver.h" |
| 29 | } |
| 30 | |
| 31 | DEFINE_bool(logtostderr, false, |
| 32 | "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] | 33 | DEFINE_bool(foreground, false, |
| 34 | "Don't daemon()ize; run in foreground."); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 35 | |
| 36 | using std::string; |
| 37 | using std::tr1::shared_ptr; |
| 38 | using std::vector; |
| 39 | |
| 40 | namespace chromeos_update_engine { |
| 41 | |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 42 | gboolean UpdateBootFlags(void* arg) { |
Darin Petkov | eee26ee | 2010-10-22 11:05:46 -0700 | [diff] [blame^] | 43 | UpdateAttempter* attempter = reinterpret_cast<UpdateAttempter*>(arg); |
| 44 | if (attempter->status() == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 45 | // Don't update the flags if there's an update that's just been applied and |
| 46 | // we're waiting for reboot because we may end up reverting the update. |
| 47 | return FALSE; // Don't call this callback again. |
| 48 | } |
| 49 | if (attempter->status() != UPDATE_STATUS_IDLE) { |
| 50 | // To avoid races (e.g., setting a good kernel right after post-install but |
| 51 | // before the status changes), update the boot flag only if the attempter is |
| 52 | // idle. |
| 53 | return TRUE; // Call this callback again. |
| 54 | } |
| 55 | // This is purely best effort. Failures should be logged by Subprocess. |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 56 | int unused = 0; |
| 57 | vector<string> cmd(1, "/usr/sbin/chromeos-setgoodkernel"); |
| 58 | Subprocess::SynchronousExec(cmd, &unused); |
| 59 | return FALSE; // Don't call this callback again |
| 60 | } |
| 61 | |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 62 | namespace { |
| 63 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 64 | void SetupDbusService(UpdateEngineService* service) { |
| 65 | DBusGConnection *bus; |
| 66 | DBusGProxy *proxy; |
| 67 | GError *error = NULL; |
| 68 | |
| 69 | bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); |
| 70 | if (!bus) { |
| 71 | LOG(FATAL) << "Failed to get bus"; |
| 72 | } |
| 73 | proxy = dbus_g_proxy_new_for_name(bus, |
| 74 | DBUS_SERVICE_DBUS, |
| 75 | DBUS_PATH_DBUS, |
| 76 | DBUS_INTERFACE_DBUS); |
| 77 | |
| 78 | guint32 request_name_ret; |
| 79 | if (!org_freedesktop_DBus_request_name(proxy, |
| 80 | kUpdateEngineServiceName, |
| 81 | 0, |
| 82 | &request_name_ret, |
| 83 | &error)) { |
| 84 | LOG(FATAL) << "Failed to get name: " << error->message; |
| 85 | } |
| 86 | if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { |
| 87 | g_warning("Got result code %u from requesting name", request_name_ret); |
| 88 | g_error_free(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 89 | LOG(FATAL) << "Got result code " << request_name_ret |
| 90 | << " from requesting name, but expected " |
| 91 | << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER; |
| 92 | } |
| 93 | dbus_g_connection_register_g_object(bus, |
| 94 | "/org/chromium/UpdateEngine", |
| 95 | G_OBJECT(service)); |
| 96 | } |
| 97 | |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 98 | } // namespace {} |
| 99 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 100 | } // namespace chromeos_update_engine |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 101 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 102 | #include "update_engine/subprocess.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 103 | |
| 104 | int main(int argc, char** argv) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 105 | ::g_type_init(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 106 | g_thread_init(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 107 | dbus_g_thread_init(); |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 108 | base::AtExitManager exit_manager; // Required for base/rand_util.h. |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 109 | chromeos_update_engine::Terminator::Init(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 110 | chromeos_update_engine::Subprocess::Init(); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 111 | google::ParseCommandLineFlags(&argc, &argv, true); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 112 | CommandLine::Init(argc, argv); |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 113 | logging::InitLogging("/var/log/update_engine.log", |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 114 | (FLAGS_logtostderr ? |
| 115 | logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG : |
| 116 | logging::LOG_ONLY_TO_FILE), |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 117 | logging::DONT_LOCK_LOG_FILE, |
| 118 | logging::APPEND_TO_OLD_LOG_FILE); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 119 | if (!FLAGS_foreground) |
| 120 | PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed"; |
| 121 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 122 | LOG(INFO) << "Chrome OS Update Engine starting"; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 123 | |
Chris Masone | 4dc2ada | 2010-09-23 12:43:03 -0700 | [diff] [blame] | 124 | // Ensure that all written files have safe permissions. |
| 125 | // This is a mask, so we _block_ execute for the owner, and ALL |
| 126 | // permissions for other users. |
| 127 | // Done _after_ log file creation. |
| 128 | umask(S_IXUSR | S_IRWXG | S_IRWXO); |
| 129 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 130 | // Create the single GMainLoop |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 131 | 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] | 132 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 133 | chromeos_update_engine::Prefs prefs; |
| 134 | LOG_IF(ERROR, !prefs.Init(FilePath("/var/lib/update_engine/prefs"))) |
| 135 | << "Failed to initialize preferences."; |
| 136 | |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 137 | MetricsLibrary metrics_lib; |
| 138 | metrics_lib.Init(); |
| 139 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 140 | // Create the update attempter: |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 141 | chromeos_update_engine::UpdateAttempter update_attempter(&prefs, |
| 142 | &metrics_lib); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 143 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 144 | // Create the dbus service object: |
| 145 | dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE, |
| 146 | &dbus_glib_update_engine_service_object_info); |
| 147 | UpdateEngineService* service = |
| 148 | UPDATE_ENGINE_SERVICE(g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL)); |
| 149 | service->update_attempter_ = &update_attempter; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 150 | update_attempter.set_dbus_service(service); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 151 | chromeos_update_engine::SetupDbusService(service); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 152 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 153 | // Schedule periodic update checks. |
| 154 | chromeos_update_engine::UpdateCheckScheduler scheduler(&update_attempter); |
| 155 | scheduler.Run(); |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 156 | |
Darin Petkov | eee26ee | 2010-10-22 11:05:46 -0700 | [diff] [blame^] | 157 | // Update boot flags after 45 seconds. |
| 158 | g_timeout_add_seconds(45, |
| 159 | &chromeos_update_engine::UpdateBootFlags, |
| 160 | &update_attempter); |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 161 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 162 | // Run the main loop until exit time: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 163 | g_main_loop_run(loop); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 164 | |
| 165 | // Cleanup: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 166 | g_main_loop_unref(loop); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 167 | update_attempter.set_dbus_service(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 168 | g_object_unref(G_OBJECT(service)); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 169 | |
| 170 | LOG(INFO) << "Chrome OS Update Engine terminating"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |