Xiaochu Liu | 1ccaed9 | 2018-06-13 14:19:55 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Amin Hassani | c4cc1ee | 2019-11-14 11:51:35 -0800 | [diff] [blame] | 5 | #include "dlcservice/dlc_service.h" |
Xiaochu Liu | 1ccaed9 | 2018-06-13 14:19:55 -0700 | [diff] [blame] | 6 | |
Xiaochu Liu | e915584 | 2018-11-13 14:24:05 -0800 | [diff] [blame] | 7 | #include <algorithm> |
Jae Hoon Kim | 69cda48 | 2019-07-18 14:36:22 -0700 | [diff] [blame] | 8 | #include <memory> |
Xiaochu Liu | c2b65f5 | 2018-07-20 12:47:05 -0700 | [diff] [blame] | 9 | #include <utility> |
Xiaochu Liu | c2b65f5 | 2018-07-20 12:47:05 -0700 | [diff] [blame] | 10 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 11 | #include <base/check.h> |
Xiaochu Liu | c2b65f5 | 2018-07-20 12:47:05 -0700 | [diff] [blame] | 12 | #include <base/files/file_enumerator.h> |
| 13 | #include <base/files/file_util.h> |
Jae Hoon Kim | 69cda48 | 2019-07-18 14:36:22 -0700 | [diff] [blame] | 14 | #include <base/files/scoped_temp_dir.h> |
Jae Hoon Kim | 4a7e06b | 2020-05-13 09:14:14 -0700 | [diff] [blame] | 15 | #include <base/strings/stringprintf.h> |
Andrew | 55e91e6 | 2020-04-28 08:41:52 -0700 | [diff] [blame] | 16 | #include <base/strings/string_util.h> |
Xiaochu Liu | d0184e3 | 2018-10-19 17:21:33 -0700 | [diff] [blame] | 17 | #include <brillo/errors/error.h> |
Xiaochu Liu | 5f0b2ba | 2018-11-26 16:42:04 -0800 | [diff] [blame] | 18 | #include <chromeos/dbus/service_constants.h> |
Xiaochu Liu | 30067a6 | 2019-02-08 13:59:10 -0800 | [diff] [blame] | 19 | #include <dbus/dlcservice/dbus-constants.h> |
Xiaochu Liu | c2b65f5 | 2018-07-20 12:47:05 -0700 | [diff] [blame] | 20 | |
Amin Hassani | 8664998 | 2020-03-31 16:03:37 -0700 | [diff] [blame] | 21 | #include "dlcservice/dlc.h" |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 22 | #include "dlcservice/error.h" |
Amin Hassani | 40c0f11 | 2020-04-15 09:55:00 -0700 | [diff] [blame] | 23 | #include "dlcservice/ref_count.h" |
Xiaochu Liu | 6da1727 | 2018-11-06 14:04:01 -0800 | [diff] [blame] | 24 | #include "dlcservice/utils.h" |
Xiaochu Liu | c524c61 | 2018-10-23 11:02:52 -0700 | [diff] [blame] | 25 | |
Jae Hoon Kim | 5d095de | 2019-07-31 13:44:42 -0700 | [diff] [blame] | 26 | using base::Callback; |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 27 | using brillo::ErrorPtr; |
Jae Hoon Kim | b2baf58 | 2019-11-12 17:00:57 -0800 | [diff] [blame] | 28 | using brillo::MessageLoop; |
Jae Hoon Kim | 69cda48 | 2019-07-18 14:36:22 -0700 | [diff] [blame] | 29 | using std::string; |
Jae Hoon Kim | 80075fb | 2019-07-24 12:35:58 -0700 | [diff] [blame] | 30 | using update_engine::Operation; |
| 31 | using update_engine::StatusResult; |
Jae Hoon Kim | 69cda48 | 2019-07-18 14:36:22 -0700 | [diff] [blame] | 32 | |
Xiaochu Liu | 1ccaed9 | 2018-06-13 14:19:55 -0700 | [diff] [blame] | 33 | namespace dlcservice { |
| 34 | |
Jae Hoon Kim | f4eed88 | 2020-01-28 17:29:16 -0800 | [diff] [blame] | 35 | DlcService::DlcService() |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 36 | : periodic_install_check_id_(MessageLoop::kTaskIdNull), |
Amin Hassani | 78a5ec8 | 2020-05-19 09:47:49 -0700 | [diff] [blame] | 37 | weak_ptr_factory_(this) {} |
| 38 | |
| 39 | DlcService::~DlcService() { |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 40 | if (periodic_install_check_id_ != MessageLoop::kTaskIdNull && |
| 41 | !brillo::MessageLoop::current()->CancelTask(periodic_install_check_id_)) |
Amin Hassani | 78a5ec8 | 2020-05-19 09:47:49 -0700 | [diff] [blame] | 42 | LOG(ERROR) |
| 43 | << "Failed to cancel delayed update_engine check during cleanup."; |
| 44 | } |
| 45 | |
| 46 | void DlcService::Initialize() { |
Jae Hoon Kim | 6e3026d | 2020-07-10 11:04:29 -0700 | [diff] [blame] | 47 | auto* system_state = SystemState::Get(); |
Amin Hassani | 759090b | 2020-04-13 12:31:10 -0700 | [diff] [blame] | 48 | const auto prefs_dir = system_state->dlc_prefs_dir(); |
Amin Hassani | fe63fc2 | 2020-04-07 11:34:34 -0700 | [diff] [blame] | 49 | if (!base::PathExists(prefs_dir)) { |
| 50 | CHECK(CreateDir(prefs_dir)) |
| 51 | << "Failed to create dlc prefs directory: " << prefs_dir; |
| 52 | } |
| 53 | |
Jae Hoon Kim | f4eed88 | 2020-01-28 17:29:16 -0800 | [diff] [blame] | 54 | dlc_manager_ = std::make_unique<DlcManager>(); |
Xiaochu Liu | 30067a6 | 2019-02-08 13:59:10 -0800 | [diff] [blame] | 55 | |
Xiaochu Liu | 30067a6 | 2019-02-08 13:59:10 -0800 | [diff] [blame] | 56 | // Register D-Bus signal callbacks. |
Amin Hassani | 759090b | 2020-04-13 12:31:10 -0700 | [diff] [blame] | 57 | system_state->update_engine()->RegisterStatusUpdateAdvancedSignalHandler( |
Jae Hoon Kim | c85824c | 2021-05-12 13:19:14 -0700 | [diff] [blame] | 58 | base::BindRepeating(&DlcService::OnStatusUpdateAdvancedSignal, |
| 59 | weak_ptr_factory_.GetWeakPtr()), |
| 60 | base::BindOnce(&DlcService::OnStatusUpdateAdvancedSignalConnected, |
| 61 | weak_ptr_factory_.GetWeakPtr())); |
Amin Hassani | 759090b | 2020-04-13 12:31:10 -0700 | [diff] [blame] | 62 | |
| 63 | system_state->session_manager()->RegisterSessionStateChangedSignalHandler( |
Jae Hoon Kim | c85824c | 2021-05-12 13:19:14 -0700 | [diff] [blame] | 64 | base::BindRepeating(&DlcService::OnSessionStateChangedSignal, |
| 65 | weak_ptr_factory_.GetWeakPtr()), |
| 66 | base::BindOnce(&DlcService::OnSessionStateChangedSignalConnected, |
| 67 | weak_ptr_factory_.GetWeakPtr())); |
Xiaochu Liu | c2b65f5 | 2018-07-20 12:47:05 -0700 | [diff] [blame] | 68 | |
Amin Hassani | ca3cbb7 | 2020-04-10 12:26:50 -0700 | [diff] [blame] | 69 | dlc_manager_->Initialize(); |
Xiaochu Liu | 46e94b9 | 2018-12-03 14:44:35 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Amin Hassani | 6d0367d | 2020-05-10 18:07:03 -0700 | [diff] [blame] | 72 | bool DlcService::Install(const DlcId& id, |
Amin Hassani | 6520224 | 2020-03-31 12:26:37 -0700 | [diff] [blame] | 73 | const string& omaha_url, |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 74 | ErrorPtr* err) { |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 75 | bool result = InstallInternal(id, omaha_url, err); |
| 76 | // Only send error metrics in here. Install success metrics is sent in |
| 77 | // |DlcBase|. |
| 78 | if (!result) { |
| 79 | SystemState::Get()->metrics()->SendInstallResultFailure(err); |
| 80 | Error::ConvertToDbusError(err); |
| 81 | } |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | bool DlcService::InstallInternal(const DlcId& id, |
| 86 | const string& omaha_url, |
| 87 | ErrorPtr* err) { |
Amin Hassani | 9a3f20c | 2020-05-25 16:38:33 -0700 | [diff] [blame] | 88 | // TODO(ahassani): Currently, we create the DLC images even if later we find |
| 89 | // out the update_engine is busy and we have to delete the images. It would be |
| 90 | // better to know the update_engine status beforehand so we can tell the DLC |
| 91 | // to not create the images, just load them if it can. We can do this more |
| 92 | // reliably by caching the last status we saw from update_engine, rather than |
| 93 | // pulling for it on every install request. That would also allows us to |
| 94 | // properly queue the incoming install requests. |
| 95 | |
| 96 | // Try to install and figure out if install through update_engine is needed. |
| 97 | bool external_install_needed = false; |
| 98 | if (!dlc_manager_->Install(id, &external_install_needed, err)) { |
Andrew | 936ccf6 | 2020-06-12 13:00:23 -0700 | [diff] [blame] | 99 | LOG(ERROR) << "Failed to install DLC=" << id; |
Amin Hassani | 69dfe6e | 2019-03-20 14:58:48 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
Amin Hassani | 9a3f20c | 2020-05-25 16:38:33 -0700 | [diff] [blame] | 103 | // Install through update_engine only if needed. |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 104 | if (!external_install_needed) |
| 105 | return true; |
| 106 | |
| 107 | if (!InstallWithUpdateEngine(id, omaha_url, err)) { |
Amin Hassani | 9a3f20c | 2020-05-25 16:38:33 -0700 | [diff] [blame] | 108 | // dlcservice must cancel the install as update_engine won't be able to |
| 109 | // install the initialized DLC. |
Jae Hoon Kim | 53514af | 2020-07-27 11:50:26 -0700 | [diff] [blame] | 110 | CancelInstall(*err); |
Amin Hassani | 9a3f20c | 2020-05-25 16:38:33 -0700 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 114 | // By now the update_engine is installing the DLC, so schedule a periodic |
| 115 | // install checker in case we miss update_engine signals. |
| 116 | SchedulePeriodicInstallCheck(); |
| 117 | |
Amin Hassani | 9a3f20c | 2020-05-25 16:38:33 -0700 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
| 121 | bool DlcService::InstallWithUpdateEngine(const DlcId& id, |
| 122 | const string& omaha_url, |
| 123 | ErrorPtr* err) { |
Jae Hoon Kim | 7d0001e | 2021-07-07 13:04:40 -0700 | [diff] [blame] | 124 | // Need to set in order for the cancellation of DLC setup. |
| 125 | installing_dlc_id_ = id; |
| 126 | |
Jae Hoon Kim | 1855d26 | 2020-02-14 16:06:35 -0800 | [diff] [blame] | 127 | // Check what state update_engine is in. |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 128 | if (SystemState::Get()->update_engine_status().current_operation() == |
| 129 | update_engine::UPDATED_NEED_REBOOT) { |
| 130 | *err = |
| 131 | Error::Create(FROM_HERE, kErrorNeedReboot, |
| 132 | "Update Engine applied update, device needs a reboot."); |
Jae Hoon Kim | 1855d26 | 2020-02-14 16:06:35 -0800 | [diff] [blame] | 133 | return false; |
| 134 | } |
Jae Hoon Kim | 582df65 | 2019-11-26 15:41:09 -0800 | [diff] [blame] | 135 | |
Jae Hoon Kim | 4a7e06b | 2020-05-13 09:14:14 -0700 | [diff] [blame] | 136 | LOG(INFO) << "Sending request to update_engine to install DLC=" << id; |
Amin Hassani | 8664998 | 2020-03-31 16:03:37 -0700 | [diff] [blame] | 137 | // Invokes update_engine to install the DLC. |
Amin Hassani | 111f2bc | 2020-04-17 11:53:32 -0700 | [diff] [blame] | 138 | ErrorPtr tmp_err; |
Jae Hoon Kim | 4a7e06b | 2020-05-13 09:14:14 -0700 | [diff] [blame] | 139 | if (!SystemState::Get()->update_engine()->AttemptInstall(omaha_url, {id}, |
| 140 | &tmp_err)) { |
Jae Hoon Kim | 52a5df5 | 2019-08-14 13:45:38 -0700 | [diff] [blame] | 141 | // TODO(kimjae): need update engine to propagate correct error message by |
| 142 | // passing in |ErrorPtr| and being set within update engine, current default |
| 143 | // is to indicate that update engine is updating because there is no way an |
| 144 | // install should have taken place if not through dlcservice. (could also be |
| 145 | // the case that an update applied between the time of the last status check |
| 146 | // above, but just return |kErrorBusy| because the next time around if an |
| 147 | // update has been applied and is in a reboot needed state, it will indicate |
| 148 | // correctly then). |
Amin Hassani | 111f2bc | 2020-04-17 11:53:32 -0700 | [diff] [blame] | 149 | LOG(ERROR) << "Update Engine failed to install requested DLCs: " |
| 150 | << (tmp_err ? Error::ToString(tmp_err) |
| 151 | : "Missing error from update engine proxy."); |
Andrew | 9aa0644 | 2020-05-06 10:33:46 -0700 | [diff] [blame] | 152 | *err = |
| 153 | Error::Create(FROM_HERE, kErrorBusy, |
| 154 | "Update Engine failed to schedule install operations."); |
Xiaochu Liu | e915584 | 2018-11-13 14:24:05 -0800 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | |
Xiaochu Liu | e915584 | 2018-11-13 14:24:05 -0800 | [diff] [blame] | 158 | return true; |
Xiaochu Liu | 1ccaed9 | 2018-06-13 14:19:55 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Amin Hassani | 605988d | 2020-05-10 19:36:35 -0700 | [diff] [blame] | 161 | bool DlcService::Uninstall(const string& id, brillo::ErrorPtr* err) { |
Andrew | 2eb9554 | 2020-06-24 14:51:49 -0700 | [diff] [blame] | 162 | bool result = dlc_manager_->Uninstall(id, err); |
| 163 | SystemState::Get()->metrics()->SendUninstallResult(err); |
| 164 | if (!result) |
| 165 | Error::ConvertToDbusError(err); |
| 166 | |
| 167 | return result; |
Amin Hassani | b9d74b1 | 2020-04-08 15:35:20 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Amin Hassani | 605988d | 2020-05-10 19:36:35 -0700 | [diff] [blame] | 170 | bool DlcService::Purge(const string& id, brillo::ErrorPtr* err) { |
Amin Hassani | 40c0f11 | 2020-04-15 09:55:00 -0700 | [diff] [blame] | 171 | return dlc_manager_->Purge(id, err); |
Xiaochu Liu | 1ccaed9 | 2018-06-13 14:19:55 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Amin Hassani | 1ac2831 | 2020-06-04 18:16:30 -0700 | [diff] [blame] | 174 | const DlcBase* DlcService::GetDlc(const DlcId& id, brillo::ErrorPtr* err) { |
| 175 | return dlc_manager_->GetDlc(id, err); |
Jae Hoon Kim | ee1ba90 | 2020-03-11 09:53:01 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Amin Hassani | 9ca846f | 2020-04-17 12:41:01 -0700 | [diff] [blame] | 178 | DlcIdList DlcService::GetInstalled() { |
Jae Hoon Kim | 9a27d15 | 2020-04-10 12:50:14 -0700 | [diff] [blame] | 179 | return dlc_manager_->GetInstalled(); |
| 180 | } |
| 181 | |
Amin Hassani | d5fc8b2 | 2020-04-29 12:44:52 -0700 | [diff] [blame] | 182 | DlcIdList DlcService::GetExistingDlcs() { |
| 183 | return dlc_manager_->GetExistingDlcs(); |
| 184 | } |
| 185 | |
Amin Hassani | 38f3679 | 2020-04-17 11:47:08 -0700 | [diff] [blame] | 186 | DlcIdList DlcService::GetDlcsToUpdate() { |
| 187 | return dlc_manager_->GetDlcsToUpdate(); |
| 188 | } |
| 189 | |
Amin Hassani | 605988d | 2020-05-10 19:36:35 -0700 | [diff] [blame] | 190 | bool DlcService::InstallCompleted(const DlcIdList& ids, ErrorPtr* err) { |
| 191 | return dlc_manager_->InstallCompleted(ids, err); |
Jae Hoon Kim | 9a27d15 | 2020-04-10 12:50:14 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Amin Hassani | 605988d | 2020-05-10 19:36:35 -0700 | [diff] [blame] | 194 | bool DlcService::UpdateCompleted(const DlcIdList& ids, ErrorPtr* err) { |
| 195 | return dlc_manager_->UpdateCompleted(ids, err); |
Jae Hoon Kim | 9a27d15 | 2020-04-10 12:50:14 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Jae Hoon Kim | 53514af | 2020-07-27 11:50:26 -0700 | [diff] [blame] | 198 | bool DlcService::FinishInstall(ErrorPtr* err) { |
| 199 | if (!installing_dlc_id_) { |
| 200 | LOG(ERROR) << "No DLC installation to finish."; |
| 201 | return false; |
| 202 | } |
| 203 | auto id = installing_dlc_id_.value(); |
| 204 | installing_dlc_id_.reset(); |
| 205 | return dlc_manager_->FinishInstall(id, err); |
| 206 | } |
| 207 | |
Andrew | bcc4bd8 | 2020-06-11 14:23:55 -0700 | [diff] [blame] | 208 | void DlcService::CancelInstall(const ErrorPtr& err_in) { |
Jae Hoon Kim | 53514af | 2020-07-27 11:50:26 -0700 | [diff] [blame] | 209 | if (!installing_dlc_id_) { |
| 210 | LOG(ERROR) << "No DLC installation to cancel."; |
| 211 | return; |
| 212 | } |
| 213 | auto id = installing_dlc_id_.value(); |
| 214 | installing_dlc_id_.reset(); |
Jae Hoon Kim | c98e3a3 | 2020-03-13 09:40:48 -0700 | [diff] [blame] | 215 | ErrorPtr tmp_err; |
Jae Hoon Kim | 53514af | 2020-07-27 11:50:26 -0700 | [diff] [blame] | 216 | if (!dlc_manager_->CancelInstall(id, err_in, &tmp_err)) |
| 217 | LOG(ERROR) << "Failed to cancel install for DLC=" << id; |
Jae Hoon Kim | b2baf58 | 2019-11-12 17:00:57 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Jae Hoon Kim | 5f95ab9 | 2019-11-26 16:52:43 -0800 | [diff] [blame] | 220 | void DlcService::PeriodicInstallCheck() { |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 221 | periodic_install_check_id_ = MessageLoop::kTaskIdNull; |
Jae Hoon Kim | b2baf58 | 2019-11-12 17:00:57 -0800 | [diff] [blame] | 222 | |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 223 | // If we're not installing anything anymore, no need to schedule again. |
Jae Hoon Kim | 53514af | 2020-07-27 11:50:26 -0700 | [diff] [blame] | 224 | if (!installing_dlc_id_) |
Jae Hoon Kim | b2baf58 | 2019-11-12 17:00:57 -0800 | [diff] [blame] | 225 | return; |
Jae Hoon Kim | b2baf58 | 2019-11-12 17:00:57 -0800 | [diff] [blame] | 226 | |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 227 | const int kNotSeenStatusDelay = 10; |
Jae Hoon Kim | 6e3026d | 2020-07-10 11:04:29 -0700 | [diff] [blame] | 228 | auto* system_state = SystemState::Get(); |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 229 | if ((system_state->clock()->Now() - |
| 230 | system_state->update_engine_status_timestamp()) > |
| 231 | base::TimeDelta::FromSeconds(kNotSeenStatusDelay)) { |
| 232 | if (GetUpdateEngineStatus()) { |
| 233 | ErrorPtr tmp_error; |
| 234 | if (!HandleStatusResult(&tmp_error)) { |
Jae Hoon Kim | 5f95ab9 | 2019-11-26 16:52:43 -0800 | [diff] [blame] | 235 | return; |
| 236 | } |
Jae Hoon Kim | 06c8614 | 2019-12-18 17:50:32 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 239 | |
| 240 | SchedulePeriodicInstallCheck(); |
Jae Hoon Kim | 5f95ab9 | 2019-11-26 16:52:43 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 243 | void DlcService::SchedulePeriodicInstallCheck() { |
| 244 | if (periodic_install_check_id_ != MessageLoop::kTaskIdNull) { |
| 245 | LOG(INFO) << "Another periodic install check already scheduled."; |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | periodic_install_check_id_ = brillo::MessageLoop::current()->PostDelayedTask( |
| 250 | FROM_HERE, |
Jae Hoon Kim | c85824c | 2021-05-12 13:19:14 -0700 | [diff] [blame] | 251 | base::BindOnce(&DlcService::PeriodicInstallCheck, |
| 252 | weak_ptr_factory_.GetWeakPtr()), |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 253 | base::TimeDelta::FromSeconds(kUECheckTimeout)); |
| 254 | } |
| 255 | |
| 256 | bool DlcService::HandleStatusResult(brillo::ErrorPtr* err) { |
| 257 | // If we are not installing any DLC(s), no need to even handle status result. |
Jae Hoon Kim | 53514af | 2020-07-27 11:50:26 -0700 | [diff] [blame] | 258 | if (!installing_dlc_id_) |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 259 | return true; |
| 260 | |
| 261 | const StatusResult& status = SystemState::Get()->update_engine_status(); |
| 262 | if (!status.is_install()) { |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 263 | *err = Error::CreateInternal( |
| 264 | FROM_HERE, error::kFailedInstallInUpdateEngine, |
Andrew | 3c75872 | 2020-06-02 10:25:19 -0700 | [diff] [blame] | 265 | "Signal from update_engine indicates that it's not for an install, but " |
| 266 | "dlcservice was waiting for an install."); |
Andrew | bcc4bd8 | 2020-06-11 14:23:55 -0700 | [diff] [blame] | 267 | CancelInstall(*err); |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 268 | SystemState::Get()->metrics()->SendInstallResultFailure(err); |
Jae Hoon Kim | c9c94b4 | 2020-02-21 10:44:02 -0800 | [diff] [blame] | 269 | return false; |
Jae Hoon Kim | 1c9d858 | 2019-10-14 10:52:05 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 272 | switch (status.current_operation()) { |
| 273 | case update_engine::UPDATED_NEED_REBOOT: |
| 274 | *err = |
| 275 | Error::Create(FROM_HERE, kErrorNeedReboot, |
| 276 | "Update Engine applied update, device needs a reboot."); |
| 277 | break; |
| 278 | case Operation::IDLE: |
Jae Hoon Kim | 39aa9de | 2019-11-20 09:52:06 -0800 | [diff] [blame] | 279 | LOG(INFO) |
| 280 | << "Signal from update_engine, proceeding to complete installation."; |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 281 | // Send metrics in |DlcBase::FinishInstall| and not here since we might |
| 282 | // be executing this call for multiple DLCs. |
Jae Hoon Kim | 53514af | 2020-07-27 11:50:26 -0700 | [diff] [blame] | 283 | if (!FinishInstall(err)) { |
Andrew | 936ccf6 | 2020-06-12 13:00:23 -0700 | [diff] [blame] | 284 | LOG(ERROR) << "Failed to finish install."; |
Andrew | 3c75872 | 2020-06-02 10:25:19 -0700 | [diff] [blame] | 285 | return false; |
| 286 | } |
Jae Hoon Kim | 39aa9de | 2019-11-20 09:52:06 -0800 | [diff] [blame] | 287 | return true; |
Jae Hoon Kim | 823ead3 | 2019-12-13 09:30:09 -0800 | [diff] [blame] | 288 | case Operation::REPORTING_ERROR_EVENT: |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 289 | *err = |
| 290 | Error::CreateInternal(FROM_HERE, error::kFailedInstallInUpdateEngine, |
| 291 | "update_engine indicates reporting failure."); |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 292 | break; |
Amin Hassani | c2e4699 | 2020-05-21 12:34:05 -0700 | [diff] [blame] | 293 | // Only when update_engine's |Operation::DOWNLOADING| should the DLC send |
| 294 | // |DlcState::INSTALLING|. Majority of the install process for DLC(s) is |
| 295 | // during |Operation::DOWNLOADING|, this also means that only a single |
| 296 | // growth from 0.0 to 1.0 for progress reporting will happen. |
Jae Hoon Kim | 39aa9de | 2019-11-20 09:52:06 -0800 | [diff] [blame] | 297 | case Operation::DOWNLOADING: |
Amin Hassani | 78a5ec8 | 2020-05-19 09:47:49 -0700 | [diff] [blame] | 298 | // TODO(ahassani): Add unittest for this. |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 299 | dlc_manager_->ChangeProgress(status.progress()); |
Amin Hassani | 78a5ec8 | 2020-05-19 09:47:49 -0700 | [diff] [blame] | 300 | |
Jae Hoon Kim | 39aa9de | 2019-11-20 09:52:06 -0800 | [diff] [blame] | 301 | FALLTHROUGH; |
| 302 | default: |
Andrew | 3c75872 | 2020-06-02 10:25:19 -0700 | [diff] [blame] | 303 | return true; |
Jae Hoon Kim | c19dabe | 2019-07-31 09:37:35 -0700 | [diff] [blame] | 304 | } |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 305 | |
Andrew | bcc4bd8 | 2020-06-11 14:23:55 -0700 | [diff] [blame] | 306 | CancelInstall(*err); |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 307 | SystemState::Get()->metrics()->SendInstallResultFailure(err); |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 308 | return false; |
Jae Hoon Kim | c7beafd | 2019-07-22 18:14:47 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 311 | bool DlcService::GetUpdateEngineStatus() { |
Jae Hoon Kim | acc2c86 | 2019-07-30 21:25:39 -0700 | [diff] [blame] | 312 | StatusResult status_result; |
Amin Hassani | 759090b | 2020-04-13 12:31:10 -0700 | [diff] [blame] | 313 | if (!SystemState::Get()->update_engine()->GetStatusAdvanced(&status_result, |
| 314 | nullptr)) { |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 315 | LOG(ERROR) << "Failed to get update_engine status, will try again later."; |
Xiaochu Liu | e915584 | 2018-11-13 14:24:05 -0800 | [diff] [blame] | 316 | return false; |
| 317 | } |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 318 | SystemState::Get()->set_update_engine_status(status_result); |
| 319 | LOG(INFO) << "Got update_engine status: " |
| 320 | << status_result.current_operation(); |
Xiaochu Liu | e915584 | 2018-11-13 14:24:05 -0800 | [diff] [blame] | 321 | return true; |
| 322 | } |
| 323 | |
Amin Hassani | c4cc1ee | 2019-11-14 11:51:35 -0800 | [diff] [blame] | 324 | void DlcService::OnStatusUpdateAdvancedSignal( |
Jae Hoon Kim | 80075fb | 2019-07-24 12:35:58 -0700 | [diff] [blame] | 325 | const StatusResult& status_result) { |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 326 | // Always set the status. |
| 327 | SystemState::Get()->set_update_engine_status(status_result); |
Andrew | 3c75872 | 2020-06-02 10:25:19 -0700 | [diff] [blame] | 328 | |
| 329 | ErrorPtr err; |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 330 | if (!HandleStatusResult(&err)) |
Andrew | 0a534ed | 2020-05-06 09:59:17 -0700 | [diff] [blame] | 331 | DCHECK(err.get()); |
Xiaochu Liu | 30067a6 | 2019-02-08 13:59:10 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Amin Hassani | c4cc1ee | 2019-11-14 11:51:35 -0800 | [diff] [blame] | 334 | void DlcService::OnStatusUpdateAdvancedSignalConnected( |
Jae Hoon Kim | 69cda48 | 2019-07-18 14:36:22 -0700 | [diff] [blame] | 335 | const string& interface_name, const string& signal_name, bool success) { |
Xiaochu Liu | 30067a6 | 2019-02-08 13:59:10 -0800 | [diff] [blame] | 336 | if (!success) { |
| 337 | LOG(ERROR) << "Failed to connect to update_engine's StatusUpdate signal."; |
| 338 | } |
Amin Hassani | f656f29 | 2020-06-08 16:20:01 -0700 | [diff] [blame] | 339 | if (!GetUpdateEngineStatus()) { |
| 340 | // As a last resort, if we couldn't get the status, just set the status to |
| 341 | // IDLE, so things can move forward. This is mostly the case because when |
| 342 | // update_engine comes up its first status is IDLE and it will stay that way |
| 343 | // for quite a while. |
| 344 | StatusResult status; |
| 345 | status.set_current_operation(Operation::IDLE); |
| 346 | status.set_is_install(false); |
| 347 | } |
Xiaochu Liu | 30067a6 | 2019-02-08 13:59:10 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Amin Hassani | 759090b | 2020-04-13 12:31:10 -0700 | [diff] [blame] | 350 | void DlcService::OnSessionStateChangedSignalConnected( |
| 351 | const string& interface_name, const string& signal_name, bool success) { |
| 352 | if (!success) { |
| 353 | LOG(ERROR) << "Failed to connect to session_manager's SessionStateChanged " |
| 354 | << "signal."; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void DlcService::OnSessionStateChangedSignal(const std::string& state) { |
Amin Hassani | 40c0f11 | 2020-04-15 09:55:00 -0700 | [diff] [blame] | 359 | UserRefCount::SessionChanged(state); |
Amin Hassani | 759090b | 2020-04-13 12:31:10 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Xiaochu Liu | 1ccaed9 | 2018-06-13 14:19:55 -0700 | [diff] [blame] | 362 | } // namespace dlcservice |