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