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> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 16 | |
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 | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame^] | 22 | #include "update_engine/update_check_scheduler.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 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 49 | void SetupDbusService(UpdateEngineService* service) { |
| 50 | DBusGConnection *bus; |
| 51 | DBusGProxy *proxy; |
| 52 | GError *error = NULL; |
| 53 | |
| 54 | bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); |
| 55 | if (!bus) { |
| 56 | LOG(FATAL) << "Failed to get bus"; |
| 57 | } |
| 58 | proxy = dbus_g_proxy_new_for_name(bus, |
| 59 | DBUS_SERVICE_DBUS, |
| 60 | DBUS_PATH_DBUS, |
| 61 | DBUS_INTERFACE_DBUS); |
| 62 | |
| 63 | guint32 request_name_ret; |
| 64 | if (!org_freedesktop_DBus_request_name(proxy, |
| 65 | kUpdateEngineServiceName, |
| 66 | 0, |
| 67 | &request_name_ret, |
| 68 | &error)) { |
| 69 | LOG(FATAL) << "Failed to get name: " << error->message; |
| 70 | } |
| 71 | if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { |
| 72 | g_warning("Got result code %u from requesting name", request_name_ret); |
| 73 | g_error_free(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 74 | LOG(FATAL) << "Got result code " << request_name_ret |
| 75 | << " from requesting name, but expected " |
| 76 | << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER; |
| 77 | } |
| 78 | dbus_g_connection_register_g_object(bus, |
| 79 | "/org/chromium/UpdateEngine", |
| 80 | G_OBJECT(service)); |
| 81 | } |
| 82 | |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 83 | } // namespace {} |
| 84 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 85 | } // namespace chromeos_update_engine |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 86 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 87 | #include "update_engine/subprocess.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 88 | |
| 89 | int main(int argc, char** argv) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 90 | ::g_type_init(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 91 | g_thread_init(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 92 | dbus_g_thread_init(); |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 93 | base::AtExitManager exit_manager; // Required for base/rand_util.h. |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 94 | chromeos_update_engine::Subprocess::Init(); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 95 | google::ParseCommandLineFlags(&argc, &argv, true); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 96 | CommandLine::Init(argc, argv); |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 97 | logging::InitLogging("/var/log/update_engine.log", |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 98 | (FLAGS_logtostderr ? |
| 99 | logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG : |
| 100 | logging::LOG_ONLY_TO_FILE), |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 101 | logging::DONT_LOCK_LOG_FILE, |
| 102 | logging::APPEND_TO_OLD_LOG_FILE); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 103 | if (!FLAGS_foreground) |
| 104 | PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed"; |
| 105 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 106 | LOG(INFO) << "Chrome OS Update Engine starting"; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 107 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 108 | // Create the single GMainLoop |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 109 | 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] | 110 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 111 | chromeos_update_engine::Prefs prefs; |
| 112 | LOG_IF(ERROR, !prefs.Init(FilePath("/var/lib/update_engine/prefs"))) |
| 113 | << "Failed to initialize preferences."; |
| 114 | |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 115 | MetricsLibrary metrics_lib; |
| 116 | metrics_lib.Init(); |
| 117 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 118 | // Create the update attempter: |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 119 | chromeos_update_engine::UpdateAttempter update_attempter(&prefs, |
| 120 | &metrics_lib); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 121 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 122 | // Create the dbus service object: |
| 123 | dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE, |
| 124 | &dbus_glib_update_engine_service_object_info); |
| 125 | UpdateEngineService* service = |
| 126 | UPDATE_ENGINE_SERVICE(g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL)); |
| 127 | service->update_attempter_ = &update_attempter; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 128 | update_attempter.set_dbus_service(service); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 129 | chromeos_update_engine::SetupDbusService(service); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 130 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame^] | 131 | // Schedule periodic update checks. |
| 132 | chromeos_update_engine::UpdateCheckScheduler scheduler(&update_attempter); |
| 133 | scheduler.Run(); |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 134 | |
Andrew de los Reyes | 3d6a209 | 2010-08-23 18:23:26 -0700 | [diff] [blame] | 135 | // Update boot flags after 45 seconds |
| 136 | g_timeout_add_seconds(45, &chromeos_update_engine::UpdateBootFlags, NULL); |
| 137 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 138 | // Run the main loop until exit time: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 139 | g_main_loop_run(loop); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 140 | |
| 141 | // Cleanup: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 142 | g_main_loop_unref(loop); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 143 | update_attempter.set_dbus_service(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 144 | g_object_unref(G_OBJECT(service)); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 145 | |
| 146 | LOG(INFO) << "Chrome OS Update Engine terminating"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 147 | return 0; |
| 148 | } |