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