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