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 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 12 | #include "base/command_line.h" |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 13 | #include "chromeos/obsolete_logging.h" |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame^] | 14 | #include "metrics/metrics_library.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 15 | #include "update_engine/dbus_constants.h" |
| 16 | #include "update_engine/dbus_service.h" |
| 17 | #include "update_engine/update_attempter.h" |
| 18 | |
| 19 | extern "C" { |
| 20 | #include "update_engine/update_engine.dbusserver.h" |
| 21 | } |
| 22 | |
| 23 | DEFINE_bool(logtostderr, false, |
| 24 | "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] | 25 | DEFINE_bool(foreground, false, |
| 26 | "Don't daemon()ize; run in foreground."); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 27 | |
| 28 | using std::string; |
| 29 | using std::tr1::shared_ptr; |
| 30 | using std::vector; |
| 31 | |
| 32 | namespace chromeos_update_engine { |
| 33 | |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | struct PeriodicallyUpdateArgs { |
| 37 | UpdateAttempter* update_attempter; |
| 38 | gboolean should_repeat; |
| 39 | }; |
| 40 | |
| 41 | gboolean PeriodicallyUpdate(void* arg) { |
| 42 | PeriodicallyUpdateArgs* args = reinterpret_cast<PeriodicallyUpdateArgs*>(arg); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 43 | args->update_attempter->Update(); |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 44 | return args->should_repeat; |
| 45 | } |
| 46 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 47 | void SetupDbusService(UpdateEngineService* service) { |
| 48 | DBusGConnection *bus; |
| 49 | DBusGProxy *proxy; |
| 50 | GError *error = NULL; |
| 51 | |
| 52 | bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); |
| 53 | if (!bus) { |
| 54 | LOG(FATAL) << "Failed to get bus"; |
| 55 | } |
| 56 | proxy = dbus_g_proxy_new_for_name(bus, |
| 57 | DBUS_SERVICE_DBUS, |
| 58 | DBUS_PATH_DBUS, |
| 59 | DBUS_INTERFACE_DBUS); |
| 60 | |
| 61 | guint32 request_name_ret; |
| 62 | if (!org_freedesktop_DBus_request_name(proxy, |
| 63 | kUpdateEngineServiceName, |
| 64 | 0, |
| 65 | &request_name_ret, |
| 66 | &error)) { |
| 67 | LOG(FATAL) << "Failed to get name: " << error->message; |
| 68 | } |
| 69 | if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { |
| 70 | g_warning("Got result code %u from requesting name", request_name_ret); |
| 71 | g_error_free(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 72 | LOG(FATAL) << "Got result code " << request_name_ret |
| 73 | << " from requesting name, but expected " |
| 74 | << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER; |
| 75 | } |
| 76 | dbus_g_connection_register_g_object(bus, |
| 77 | "/org/chromium/UpdateEngine", |
| 78 | G_OBJECT(service)); |
| 79 | } |
| 80 | |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 81 | } // namespace {} |
| 82 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 83 | } // namespace chromeos_update_engine |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 84 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 85 | #include "update_engine/subprocess.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 86 | |
| 87 | int main(int argc, char** argv) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 88 | ::g_type_init(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 89 | g_thread_init(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 90 | dbus_g_thread_init(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 91 | chromeos_update_engine::Subprocess::Init(); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 92 | google::ParseCommandLineFlags(&argc, &argv, true); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 93 | CommandLine::Init(argc, argv); |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 94 | logging::InitLogging("/var/log/update_engine.log", |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 95 | (FLAGS_logtostderr ? |
| 96 | logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG : |
| 97 | logging::LOG_ONLY_TO_FILE), |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 98 | logging::DONT_LOCK_LOG_FILE, |
| 99 | logging::APPEND_TO_OLD_LOG_FILE); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 100 | if (!FLAGS_foreground) |
| 101 | PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed"; |
| 102 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 103 | LOG(INFO) << "Chrome OS Update Engine starting"; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 104 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 105 | // Create the single GMainLoop |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 106 | 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] | 107 | |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame^] | 108 | MetricsLibrary metrics_lib; |
| 109 | metrics_lib.Init(); |
| 110 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 111 | // Create the update attempter: |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame^] | 112 | chromeos_update_engine::UpdateAttempter update_attempter(&metrics_lib); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 113 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 114 | // Create the dbus service object: |
| 115 | dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE, |
| 116 | &dbus_glib_update_engine_service_object_info); |
| 117 | UpdateEngineService* service = |
| 118 | UPDATE_ENGINE_SERVICE(g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL)); |
| 119 | service->update_attempter_ = &update_attempter; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 120 | update_attempter.set_dbus_service(service); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 121 | chromeos_update_engine::SetupDbusService(service); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 122 | |
Andrew de los Reyes | 7352067 | 2010-05-10 13:44:58 -0700 | [diff] [blame] | 123 | // Kick off periodic updating. First, update after 2 minutes. Also, update |
| 124 | // every 30 minutes. |
| 125 | chromeos_update_engine::PeriodicallyUpdateArgs two_min_args = |
| 126 | {&update_attempter, FALSE}; |
| 127 | g_timeout_add(2 * 60 * 1000, |
| 128 | &chromeos_update_engine::PeriodicallyUpdate, |
| 129 | &two_min_args); |
| 130 | |
| 131 | chromeos_update_engine::PeriodicallyUpdateArgs thirty_min_args = |
| 132 | {&update_attempter, TRUE}; |
| 133 | g_timeout_add(30 * 60 * 1000, |
| 134 | &chromeos_update_engine::PeriodicallyUpdate, |
| 135 | &thirty_min_args); |
| 136 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 137 | // Run the main loop until exit time: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 138 | g_main_loop_run(loop); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 139 | |
| 140 | // Cleanup: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 141 | g_main_loop_unref(loop); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 142 | update_attempter.set_dbus_service(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 143 | g_object_unref(G_OBJECT(service)); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 144 | |
| 145 | LOG(INFO) << "Chrome OS Update Engine terminating"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 146 | return 0; |
| 147 | } |