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 | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 29 | void SetupDbusService(UpdateEngineService* service) { |
| 30 | DBusGConnection *bus; |
| 31 | DBusGProxy *proxy; |
| 32 | GError *error = NULL; |
| 33 | |
| 34 | bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); |
| 35 | if (!bus) { |
| 36 | LOG(FATAL) << "Failed to get bus"; |
| 37 | } |
| 38 | proxy = dbus_g_proxy_new_for_name(bus, |
| 39 | DBUS_SERVICE_DBUS, |
| 40 | DBUS_PATH_DBUS, |
| 41 | DBUS_INTERFACE_DBUS); |
| 42 | |
| 43 | guint32 request_name_ret; |
| 44 | if (!org_freedesktop_DBus_request_name(proxy, |
| 45 | kUpdateEngineServiceName, |
| 46 | 0, |
| 47 | &request_name_ret, |
| 48 | &error)) { |
| 49 | LOG(FATAL) << "Failed to get name: " << error->message; |
| 50 | } |
| 51 | if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { |
| 52 | g_warning("Got result code %u from requesting name", request_name_ret); |
| 53 | g_error_free(error); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 54 | LOG(FATAL) << "Got result code " << request_name_ret |
| 55 | << " from requesting name, but expected " |
| 56 | << DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER; |
| 57 | } |
| 58 | dbus_g_connection_register_g_object(bus, |
| 59 | "/org/chromium/UpdateEngine", |
| 60 | G_OBJECT(service)); |
| 61 | } |
| 62 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 63 | } // namespace chromeos_update_engine |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 64 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 65 | #include "update_engine/subprocess.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 66 | |
| 67 | int main(int argc, char** argv) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 68 | ::g_type_init(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 69 | g_thread_init(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 70 | dbus_g_thread_init(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 71 | chromeos_update_engine::Subprocess::Init(); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 72 | google::ParseCommandLineFlags(&argc, &argv, true); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 73 | CommandLine::Init(argc, argv); |
| 74 | logging::InitLogging("logfile.txt", |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame^] | 75 | (FLAGS_logtostderr ? |
| 76 | logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG : |
| 77 | logging::LOG_ONLY_TO_FILE), |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 78 | logging::DONT_LOCK_LOG_FILE, |
| 79 | logging::APPEND_TO_OLD_LOG_FILE); |
| 80 | LOG(INFO) << "Chrome OS Update Engine starting"; |
| 81 | |
| 82 | // Create the single GMainLoop |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame^] | 83 | 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] | 84 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 85 | // Create the update attempter: |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame^] | 86 | chromeos_update_engine::UpdateAttempter update_attempter; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 87 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 88 | // Create the dbus service object: |
| 89 | dbus_g_object_type_install_info(UPDATE_ENGINE_TYPE_SERVICE, |
| 90 | &dbus_glib_update_engine_service_object_info); |
| 91 | UpdateEngineService* service = |
| 92 | UPDATE_ENGINE_SERVICE(g_object_new(UPDATE_ENGINE_TYPE_SERVICE, NULL)); |
| 93 | service->update_attempter_ = &update_attempter; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame^] | 94 | update_attempter.set_dbus_service(service); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 95 | chromeos_update_engine::SetupDbusService(service); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 96 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 97 | // Run the main loop until exit time: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 98 | g_main_loop_run(loop); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 99 | |
| 100 | // Cleanup: |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 101 | g_main_loop_unref(loop); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame^] | 102 | update_attempter.set_dbus_service(NULL); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 103 | g_object_unref(G_OBJECT(service)); |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 104 | |
| 105 | LOG(INFO) << "Chrome OS Update Engine terminating"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 106 | return 0; |
| 107 | } |