OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // TODO(adlr): get rid of commented out lines or comment them back in. | |
6 // Look for "// re-add" next to those comments. | |
7 | |
8 #include <string> | 5 #include <string> |
9 #include <tr1/memory> | 6 #include <tr1/memory> |
10 #include <vector> | 7 #include <vector> |
11 #include <gflags/gflags.h> | 8 #include <gflags/gflags.h> |
12 #include <glib.h> | 9 #include <glib.h> |
| 10 #include "base/command_line.h" |
13 #include "chromeos/obsolete_logging.h" | 11 #include "chromeos/obsolete_logging.h" |
14 #include "update_engine/action_processor.h" | 12 #include "update_engine/dbus_constants.h" |
15 #include "update_engine/download_action.h" | 13 #include "update_engine/dbus_service.h" |
16 #include "update_engine/filesystem_copier_action.h" | 14 #include "update_engine/update_attempter.h" |
17 // #include "update_engine/install_action.h" // re-add | 15 |
18 #include "update_engine/libcurl_http_fetcher.h" | 16 extern "C" { |
19 #include "update_engine/omaha_request_prep_action.h" | 17 #include "update_engine/update_engine.dbusserver.h" |
20 #include "update_engine/omaha_response_handler_action.h" | 18 } |
21 #include "update_engine/postinstall_runner_action.h" | 19 |
22 #include "update_engine/set_bootable_flag_action.h" | 20 DEFINE_bool(logtostderr, false, |
23 #include "update_engine/update_check_action.h" | 21 "Write logs to stderr instead of to a file in log_dir."); |
24 | 22 |
25 using std::string; | 23 using std::string; |
26 using std::tr1::shared_ptr; | 24 using std::tr1::shared_ptr; |
27 using std::vector; | 25 using std::vector; |
28 | 26 |
29 namespace chromeos_update_engine { | 27 namespace chromeos_update_engine { |
30 | 28 |
31 class UpdateAttempter : public ActionProcessorDelegate { | 29 gboolean SetupInMainLoop(void* arg) { |
32 public: | 30 // TODO(adlr): Tell update_attempter to start working. |
33 UpdateAttempter(GMainLoop *loop) | 31 // Comment this in for that: |
34 : full_update_(false), | 32 //UpdateAttempter* update_attempter = reinterpret_cast<UpdateAttempter*>(arg); |
35 loop_(loop) {} | |
36 void Update(bool force_full_update); | |
37 | |
38 // Delegate method: | |
39 void ProcessingDone(const ActionProcessor* processor, bool success); | |
40 private: | |
41 bool full_update_; | |
42 vector<shared_ptr<AbstractAction> > actions_; | |
43 ActionProcessor processor_; | |
44 GMainLoop *loop_; | |
45 | 33 |
46 // pointer to the OmahaResponseHandlerAction in the actions_ vector; | |
47 shared_ptr<OmahaResponseHandlerAction> response_handler_action_; | |
48 DISALLOW_COPY_AND_ASSIGN(UpdateAttempter); | |
49 }; | |
50 | |
51 // Returns true on success. If there was no update available, that's still | |
52 // success. | |
53 // If force_full is true, try to force a full update. | |
54 void UpdateAttempter::Update(bool force_full_update) { | |
55 full_update_ = force_full_update; | |
56 CHECK(!processor_.IsRunning()); | |
57 processor_.set_delegate(this); | |
58 | |
59 // Actions: | |
60 shared_ptr<OmahaRequestPrepAction> request_prep_action( | |
61 new OmahaRequestPrepAction(force_full_update)); | |
62 shared_ptr<UpdateCheckAction> update_check_action( | |
63 new UpdateCheckAction(new LibcurlHttpFetcher)); | |
64 shared_ptr<OmahaResponseHandlerAction> response_handler_action( | |
65 new OmahaResponseHandlerAction); | |
66 shared_ptr<FilesystemCopierAction> filesystem_copier_action( | |
67 new FilesystemCopierAction); | |
68 shared_ptr<DownloadAction> download_action( | |
69 new DownloadAction(new LibcurlHttpFetcher)); | |
70 // shared_ptr<InstallAction> install_action( // re-add | |
71 // new InstallAction); | |
72 shared_ptr<PostinstallRunnerAction> postinstall_runner_action( | |
73 new PostinstallRunnerAction); | |
74 shared_ptr<SetBootableFlagAction> set_bootable_flag_action( | |
75 new SetBootableFlagAction); | |
76 | |
77 response_handler_action_ = response_handler_action; | |
78 | |
79 actions_.push_back(shared_ptr<AbstractAction>(request_prep_action)); | |
80 actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); | |
81 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); | |
82 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action)); | |
83 actions_.push_back(shared_ptr<AbstractAction>(download_action)); | |
84 // actions_.push_back(shared_ptr<AbstractAction>(install_action)); // re-add | |
85 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action)); | |
86 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action)); | |
87 | |
88 // Enqueue the actions | |
89 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin(); | |
90 it != actions_.end(); ++it) { | |
91 processor_.EnqueueAction(it->get()); | |
92 } | |
93 | |
94 // Bond them together. We have to use the leaf-types when calling | |
95 // BondActions(). | |
96 BondActions(request_prep_action.get(), update_check_action.get()); | |
97 BondActions(update_check_action.get(), response_handler_action.get()); | |
98 BondActions(response_handler_action.get(), filesystem_copier_action.get()); | |
99 BondActions(filesystem_copier_action.get(), download_action.get()); | |
100 // BondActions(download_action.get(), install_action.get()); // re-add | |
101 // BondActions(install_action.get(), postinstall_runner_action.get()); | |
102 BondActions(postinstall_runner_action.get(), set_bootable_flag_action.get()); | |
103 | |
104 processor_.StartProcessing(); | |
105 } | |
106 | |
107 void UpdateAttempter::ProcessingDone(const ActionProcessor* processor, | |
108 bool success) { | |
109 CHECK(response_handler_action_); | |
110 if (response_handler_action_->GotNoUpdateResponse()) { | |
111 // All done. | |
112 g_main_loop_quit(loop_); | |
113 return; | |
114 } | |
115 if (!success) { | |
116 if (!full_update_) { | |
117 LOG(ERROR) << "Update failed. Attempting full update"; | |
118 actions_.clear(); | |
119 response_handler_action_.reset(); | |
120 Update(true); | |
121 return; | |
122 } else { | |
123 LOG(ERROR) << "Full update failed. Aborting"; | |
124 } | |
125 } | |
126 g_main_loop_quit(loop_); | |
127 } | |
128 | |
129 gboolean UpdateInMainLoop(void* arg) { | |
130 UpdateAttempter* update_attempter = reinterpret_cast<UpdateAttempter*>(arg); | |
131 update_attempter->Update(false); | |
132 return FALSE; // Don't call this callback function again | 34 return FALSE; // Don't call this callback function again |
133 } | 35 } |
134 | 36 |
| 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 |
135 } // namespace chromeos_update_engine | 72 } // namespace chromeos_update_engine |
136 | 73 |
137 #include "update_engine/subprocess.h" | 74 #include "update_engine/subprocess.h" |
138 | 75 |
139 int main(int argc, char** argv) { | 76 int main(int argc, char** argv) { |
| 77 ::g_type_init(); |
140 g_thread_init(NULL); | 78 g_thread_init(NULL); |
| 79 dbus_g_thread_init(); |
141 chromeos_update_engine::Subprocess::Init(); | 80 chromeos_update_engine::Subprocess::Init(); |
142 google::ParseCommandLineFlags(&argc, &argv, true); | 81 google::ParseCommandLineFlags(&argc, &argv, true); |
143 // TODO(adlr): figure out log file | 82 CommandLine::Init(argc, argv); |
144 logging::InitLogging("", | 83 logging::InitLogging("logfile.txt", |
145 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, | 84 FLAGS_logtostderr ? |
| 85 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG : |
| 86 logging::LOG_ONLY_TO_FILE, |
146 logging::DONT_LOCK_LOG_FILE, | 87 logging::DONT_LOCK_LOG_FILE, |
147 logging::APPEND_TO_OLD_LOG_FILE); | 88 logging::APPEND_TO_OLD_LOG_FILE); |
148 LOG(INFO) << "Chrome OS Update Engine starting"; | 89 LOG(INFO) << "Chrome OS Update Engine starting"; |
149 | 90 |
150 // Create the single GMainLoop | 91 // Create the single GMainLoop |
151 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 92 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
152 | 93 |
| 94 // Create the update attempter: |
153 chromeos_update_engine::UpdateAttempter update_attempter(loop); | 95 chromeos_update_engine::UpdateAttempter update_attempter(loop); |
154 | 96 |
155 g_timeout_add(0, &chromeos_update_engine::UpdateInMainLoop, | 97 // Create the dbus service object: |
156 &update_attempter); | 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); |
157 | 104 |
| 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: |
158 g_main_loop_run(loop); | 109 g_main_loop_run(loop); |
| 110 |
| 111 // Cleanup: |
159 g_main_loop_unref(loop); | 112 g_main_loop_unref(loop); |
| 113 g_object_unref(G_OBJECT(service)); |
160 | 114 |
161 LOG(INFO) << "Chrome OS Update Engine terminating"; | 115 LOG(INFO) << "Chrome OS Update Engine terminating"; |
162 return 0; | 116 return 0; |
163 } | 117 } |
OLD | NEW |