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