| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2011 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/postinstall_runner_action.h" | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 18 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 19 | #include <fcntl.h> | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 20 | #include <signal.h> | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include <stdlib.h> | 
| Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 22 | #include <sys/mount.h> | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 23 | #include <sys/types.h> | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 24 | #include <unistd.h> | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 25 |  | 
| Alex Deymo | 44b3567 | 2016-04-05 17:57:48 -0700 | [diff] [blame] | 26 | #include <cmath> | 
|  | 27 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 28 | #include <base/files/file_path.h> | 
|  | 29 | #include <base/files/file_util.h> | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 30 | #include <base/logging.h> | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 31 | #include <base/strings/string_split.h> | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 32 | #include <base/strings/string_util.h> | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 33 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 34 | #include "update_engine/common/action_processor.h" | 
| Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 35 | #include "update_engine/common/boot_control_interface.h" | 
| Alex Deymo | 14dbd33 | 2016-03-01 18:55:54 -0800 | [diff] [blame] | 36 | #include "update_engine/common/platform_constants.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 37 | #include "update_engine/common/subprocess.h" | 
|  | 38 | #include "update_engine/common/utils.h" | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 39 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 40 | namespace { | 
|  | 41 |  | 
|  | 42 | // The file descriptor number from the postinstall program's perspective where | 
|  | 43 | // it can report status updates. This can be any number greater than 2 (stderr), | 
|  | 44 | // but must be kept in sync with the "bin/postinst_progress" defined in the | 
|  | 45 | // sample_images.sh file. | 
|  | 46 | const int kPostinstallStatusFd = 3; | 
|  | 47 |  | 
|  | 48 | }  // namespace | 
|  | 49 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 50 | namespace chromeos_update_engine { | 
|  | 51 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 52 | using brillo::MessageLoop; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 53 | using std::string; | 
| Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 54 | using std::vector; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 55 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 56 | void PostinstallRunnerAction::PerformAction() { | 
|  | 57 | CHECK(HasInputObject()); | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 58 | install_plan_ = GetInputObject(); | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 59 |  | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 60 | if (install_plan_.powerwash_required) { | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 61 | if (hardware_->SchedulePowerwash()) { | 
|  | 62 | powerwash_scheduled_ = true; | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 63 | } else { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 64 | return CompletePostinstall(ErrorCode::kPostinstallPowerwashError); | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 65 | } | 
|  | 66 | } | 
|  | 67 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 68 | // Initialize all the partition weights. | 
|  | 69 | partition_weight_.resize(install_plan_.partitions.size()); | 
|  | 70 | total_weight_ = 0; | 
|  | 71 | for (size_t i = 0; i < install_plan_.partitions.size(); ++i) { | 
|  | 72 | // TODO(deymo): This code sets the weight to all the postinstall commands, | 
|  | 73 | // but we could remember how long they took in the past and use those | 
|  | 74 | // values. | 
|  | 75 | partition_weight_[i] = install_plan_.partitions[i].run_postinstall; | 
|  | 76 | total_weight_ += partition_weight_[i]; | 
|  | 77 | } | 
|  | 78 | accumulated_weight_ = 0; | 
|  | 79 | ReportProgress(0); | 
|  | 80 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 81 | PerformPartitionPostinstall(); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | void PostinstallRunnerAction::PerformPartitionPostinstall() { | 
| Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 85 | if (!install_plan_.run_post_install) { | 
|  | 86 | LOG(INFO) << "Skipping post-install according to install plan."; | 
|  | 87 | return CompletePostinstall(ErrorCode::kSuccess); | 
|  | 88 | } | 
|  | 89 |  | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 90 | if (install_plan_.download_url.empty()) { | 
|  | 91 | LOG(INFO) << "Skipping post-install during rollback"; | 
|  | 92 | return CompletePostinstall(ErrorCode::kSuccess); | 
|  | 93 | } | 
|  | 94 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 95 | // Skip all the partitions that don't have a post-install step. | 
|  | 96 | while (current_partition_ < install_plan_.partitions.size() && | 
|  | 97 | !install_plan_.partitions[current_partition_].run_postinstall) { | 
|  | 98 | VLOG(1) << "Skipping post-install on partition " | 
|  | 99 | << install_plan_.partitions[current_partition_].name; | 
|  | 100 | current_partition_++; | 
|  | 101 | } | 
|  | 102 | if (current_partition_ == install_plan_.partitions.size()) | 
|  | 103 | return CompletePostinstall(ErrorCode::kSuccess); | 
|  | 104 |  | 
|  | 105 | const InstallPlan::Partition& partition = | 
|  | 106 | install_plan_.partitions[current_partition_]; | 
|  | 107 |  | 
|  | 108 | const string mountable_device = | 
|  | 109 | utils::MakePartitionNameForMount(partition.target_path); | 
|  | 110 | if (mountable_device.empty()) { | 
|  | 111 | LOG(ERROR) << "Cannot make mountable device from " << partition.target_path; | 
|  | 112 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | // Perform post-install for the current_partition_ partition. At this point we | 
|  | 116 | // need to call CompletePartitionPostinstall to complete the operation and | 
|  | 117 | // cleanup. | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 118 | #ifdef __ANDROID__ | 
|  | 119 | fs_mount_dir_ = "/postinstall"; | 
|  | 120 | #else   // __ANDROID__ | 
| Sen Jiang | 371615b | 2016-04-13 15:54:29 -0700 | [diff] [blame] | 121 | base::FilePath temp_dir; | 
|  | 122 | TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir)); | 
|  | 123 | fs_mount_dir_ = temp_dir.value(); | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 124 | #endif  // __ANDROID__ | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 125 |  | 
| Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 126 | // Double check that the fs_mount_dir is not busy with a previous mounted | 
|  | 127 | // filesystem from a previous crashed postinstall step. | 
|  | 128 | if (utils::IsMountpoint(fs_mount_dir_)) { | 
|  | 129 | LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_; | 
|  | 130 | utils::UnmountFilesystem(fs_mount_dir_); | 
|  | 131 | } | 
|  | 132 |  | 
| Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 133 | base::FilePath postinstall_path(partition.postinstall_path); | 
|  | 134 | if (postinstall_path.IsAbsolute()) { | 
|  | 135 | LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative" | 
|  | 136 | "path instead: " | 
|  | 137 | << partition.postinstall_path; | 
|  | 138 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | string abs_path = | 
|  | 142 | base::FilePath(fs_mount_dir_).Append(postinstall_path).value(); | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 143 | if (!base::StartsWith( | 
|  | 144 | abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) { | 
|  | 145 | LOG(ERROR) << "Invalid relative postinstall path: " | 
|  | 146 | << partition.postinstall_path; | 
|  | 147 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); | 
|  | 148 | } | 
|  | 149 |  | 
| Alex Deymo | 5fb356c | 2016-03-25 18:48:22 -0700 | [diff] [blame] | 150 | #ifdef __ANDROID__ | 
|  | 151 | // In Chromium OS, the postinstall step is allowed to write to the block | 
|  | 152 | // device on the target image, so we don't mark it as read-only and should | 
|  | 153 | // be read-write since we just wrote to it during the update. | 
|  | 154 |  | 
|  | 155 | // Mark the block device as read-only before mounting for post-install. | 
|  | 156 | if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) { | 
|  | 157 | return CompletePartitionPostinstall( | 
|  | 158 | 1, "Error marking the device " + mountable_device + " read only."); | 
|  | 159 | } | 
|  | 160 | #endif  // __ANDROID__ | 
|  | 161 |  | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 162 | if (!utils::MountFilesystem(mountable_device, | 
|  | 163 | fs_mount_dir_, | 
|  | 164 | MS_RDONLY, | 
| Alex Deymo | 14dbd33 | 2016-03-01 18:55:54 -0800 | [diff] [blame] | 165 | partition.filesystem_type, | 
|  | 166 | constants::kPostinstallMountOptions)) { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 167 | return CompletePartitionPostinstall( | 
|  | 168 | 1, "Error mounting the device " + mountable_device); | 
|  | 169 | } | 
|  | 170 |  | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 171 | LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at " | 
|  | 172 | << abs_path << ") installed on device " << partition.target_path | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 173 | << " and mountable device " << mountable_device; | 
|  | 174 |  | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 175 | // Logs the file format of the postinstall script we are about to run. This | 
|  | 176 | // will help debug when the postinstall script doesn't match the architecture | 
|  | 177 | // of our build. | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 178 | LOG(INFO) << "Format file for new " << partition.postinstall_path | 
|  | 179 | << " is: " << utils::GetFileFormat(abs_path); | 
| Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 180 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 181 | // Runs the postinstall script asynchronously to free up the main loop while | 
|  | 182 | // it's running. | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 183 | vector<string> command = {abs_path}; | 
|  | 184 | #ifdef __ANDROID__ | 
|  | 185 | // In Brillo and Android, we pass the slot number and status fd. | 
|  | 186 | command.push_back(std::to_string(install_plan_.target_slot)); | 
|  | 187 | command.push_back(std::to_string(kPostinstallStatusFd)); | 
|  | 188 | #else | 
|  | 189 | // Chrome OS postinstall expects the target rootfs as the first parameter. | 
|  | 190 | command.push_back(partition.target_path); | 
|  | 191 | #endif  // __ANDROID__ | 
|  | 192 |  | 
|  | 193 | current_command_ = Subprocess::Get().ExecFlags( | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 194 | command, | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 195 | Subprocess::kRedirectStderrToStdout, | 
|  | 196 | {kPostinstallStatusFd}, | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 197 | base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall, | 
|  | 198 | base::Unretained(this))); | 
|  | 199 | // Subprocess::Exec should never return a negative process id. | 
|  | 200 | CHECK_GE(current_command_, 0); | 
|  | 201 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 202 | if (!current_command_) { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 203 | CompletePartitionPostinstall(1, "Postinstall didn't launch"); | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 204 | return; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | // Monitor the status file descriptor. | 
|  | 208 | progress_fd_ = | 
|  | 209 | Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd); | 
|  | 210 | int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK; | 
|  | 211 | if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) { | 
|  | 212 | PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | progress_task_ = MessageLoop::current()->WatchFileDescriptor( | 
|  | 216 | FROM_HERE, | 
|  | 217 | progress_fd_, | 
|  | 218 | MessageLoop::WatchMode::kWatchRead, | 
|  | 219 | true, | 
|  | 220 | base::Bind(&PostinstallRunnerAction::OnProgressFdReady, | 
|  | 221 | base::Unretained(this))); | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 224 | void PostinstallRunnerAction::OnProgressFdReady() { | 
|  | 225 | char buf[1024]; | 
|  | 226 | size_t bytes_read; | 
|  | 227 | do { | 
|  | 228 | bytes_read = 0; | 
|  | 229 | bool eof; | 
|  | 230 | bool ok = | 
|  | 231 | utils::ReadAll(progress_fd_, buf, arraysize(buf), &bytes_read, &eof); | 
|  | 232 | progress_buffer_.append(buf, bytes_read); | 
|  | 233 | // Process every line. | 
|  | 234 | vector<string> lines = base::SplitString( | 
|  | 235 | progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); | 
|  | 236 | if (!lines.empty()) { | 
|  | 237 | progress_buffer_ = lines.back(); | 
|  | 238 | lines.pop_back(); | 
|  | 239 | for (const auto& line : lines) { | 
|  | 240 | ProcessProgressLine(line); | 
|  | 241 | } | 
|  | 242 | } | 
|  | 243 | if (!ok || eof) { | 
|  | 244 | // There was either an error or an EOF condition, so we are done watching | 
|  | 245 | // the file descriptor. | 
|  | 246 | MessageLoop::current()->CancelTask(progress_task_); | 
|  | 247 | progress_task_ = MessageLoop::kTaskIdNull; | 
|  | 248 | return; | 
|  | 249 | } | 
|  | 250 | } while (bytes_read); | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | bool PostinstallRunnerAction::ProcessProgressLine(const string& line) { | 
|  | 254 | double frac = 0; | 
| Alex Deymo | a2ea1c2 | 2016-08-24 17:26:19 -0700 | [diff] [blame] | 255 | if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 && | 
|  | 256 | !std::isnan(frac)) { | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 257 | ReportProgress(frac); | 
|  | 258 | return true; | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | return false; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | void PostinstallRunnerAction::ReportProgress(double frac) { | 
|  | 265 | if (!delegate_) | 
|  | 266 | return; | 
|  | 267 | if (current_partition_ >= partition_weight_.size()) { | 
|  | 268 | delegate_->ProgressUpdate(1.); | 
|  | 269 | return; | 
|  | 270 | } | 
| Alex Deymo | 44b3567 | 2016-04-05 17:57:48 -0700 | [diff] [blame] | 271 | if (!std::isfinite(frac) || frac < 0) | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 272 | frac = 0; | 
|  | 273 | if (frac > 1) | 
|  | 274 | frac = 1; | 
|  | 275 | double postinst_action_progress = | 
|  | 276 | (accumulated_weight_ + partition_weight_[current_partition_] * frac) / | 
|  | 277 | total_weight_; | 
|  | 278 | delegate_->ProgressUpdate(postinst_action_progress); | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | void PostinstallRunnerAction::Cleanup() { | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 282 | utils::UnmountFilesystem(fs_mount_dir_); | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 283 | #ifndef __ANDROID__ | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 284 | if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) { | 
|  | 285 | PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 286 | } | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 287 | #endif  // !__ANDROID__ | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 288 | fs_mount_dir_.clear(); | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 289 |  | 
|  | 290 | progress_fd_ = -1; | 
|  | 291 | if (progress_task_ != MessageLoop::kTaskIdNull) { | 
|  | 292 | MessageLoop::current()->CancelTask(progress_task_); | 
|  | 293 | progress_task_ = MessageLoop::kTaskIdNull; | 
|  | 294 | } | 
|  | 295 | progress_buffer_.clear(); | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 296 | } | 
|  | 297 |  | 
|  | 298 | void PostinstallRunnerAction::CompletePartitionPostinstall( | 
|  | 299 | int return_code, const string& output) { | 
|  | 300 | current_command_ = 0; | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 301 | Cleanup(); | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 302 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 303 | if (return_code != 0) { | 
|  | 304 | LOG(ERROR) << "Postinst command failed with code: " << return_code; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 305 | ErrorCode error_code = ErrorCode::kPostinstallRunnerError; | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 306 |  | 
| Andrew de los Reyes | fe57d54 | 2011-06-07 09:00:36 -0700 | [diff] [blame] | 307 | if (return_code == 3) { | 
| Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 308 | // This special return code means that we tried to update firmware, | 
|  | 309 | // but couldn't because we booted from FW B, and we need to reboot | 
|  | 310 | // to get back to FW A. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 311 | error_code = ErrorCode::kPostinstallBootedFromFirmwareB; | 
| Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 312 | } | 
| Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 313 |  | 
|  | 314 | if (return_code == 4) { | 
|  | 315 | // This special return code means that we tried to update firmware, | 
|  | 316 | // but couldn't because we booted from FW B, and we need to reboot | 
|  | 317 | // to get back to FW A. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 318 | error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable; | 
| Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 319 | } | 
| Alex Deymo | 5b91c6b | 2016-08-04 20:33:36 -0700 | [diff] [blame] | 320 |  | 
|  | 321 | // If postinstall script for this partition is optional we can ignore the | 
|  | 322 | // result. | 
|  | 323 | if (install_plan_.partitions[current_partition_].postinstall_optional) { | 
|  | 324 | LOG(INFO) << "Ignoring postinstall failure since it is optional"; | 
|  | 325 | } else { | 
|  | 326 | return CompletePostinstall(error_code); | 
|  | 327 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 328 | } | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 329 | accumulated_weight_ += partition_weight_[current_partition_]; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 330 | current_partition_++; | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 331 | ReportProgress(0); | 
|  | 332 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 333 | PerformPartitionPostinstall(); | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) { | 
|  | 337 | // We only attempt to mark the new slot as active if all the postinstall | 
|  | 338 | // steps succeeded. | 
| Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 339 | if (error_code == ErrorCode::kSuccess) { | 
|  | 340 | if (install_plan_.switch_slot_on_reboot) { | 
|  | 341 | if (!boot_control_->SetActiveBootSlot(install_plan_.target_slot)) { | 
|  | 342 | error_code = ErrorCode::kPostinstallRunnerError; | 
|  | 343 | } | 
|  | 344 | } else { | 
|  | 345 | error_code = ErrorCode::kUpdatedButNotActive; | 
|  | 346 | } | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 347 | } | 
|  | 348 |  | 
|  | 349 | ScopedActionCompleter completer(processor_, this); | 
| Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 350 | completer.set_code(error_code); | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 351 |  | 
| Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 352 | if (error_code != ErrorCode::kSuccess && | 
|  | 353 | error_code != ErrorCode::kUpdatedButNotActive) { | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 354 | LOG(ERROR) << "Postinstall action failed."; | 
|  | 355 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 356 | // Undo any changes done to trigger Powerwash. | 
|  | 357 | if (powerwash_scheduled_) | 
|  | 358 | hardware_->CancelPowerwash(); | 
| Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 359 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 360 | return; | 
|  | 361 | } | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 362 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 363 | LOG(INFO) << "All post-install commands succeeded"; | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 364 | if (HasOutputPipe()) { | 
|  | 365 | SetOutputObject(install_plan_); | 
|  | 366 | } | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 369 | void PostinstallRunnerAction::SuspendAction() { | 
|  | 370 | if (!current_command_) | 
|  | 371 | return; | 
|  | 372 | if (kill(current_command_, SIGSTOP) != 0) { | 
|  | 373 | PLOG(ERROR) << "Couldn't pause child process " << current_command_; | 
| Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 374 | } else { | 
|  | 375 | is_current_command_suspended_ = true; | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 376 | } | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | void PostinstallRunnerAction::ResumeAction() { | 
|  | 380 | if (!current_command_) | 
|  | 381 | return; | 
|  | 382 | if (kill(current_command_, SIGCONT) != 0) { | 
|  | 383 | PLOG(ERROR) << "Couldn't resume child process " << current_command_; | 
| Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 384 | } else { | 
|  | 385 | is_current_command_suspended_ = false; | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 386 | } | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | void PostinstallRunnerAction::TerminateProcessing() { | 
|  | 390 | if (!current_command_) | 
|  | 391 | return; | 
|  | 392 | // Calling KillExec() will discard the callback we registered and therefore | 
|  | 393 | // the unretained reference to this object. | 
|  | 394 | Subprocess::Get().KillExec(current_command_); | 
| Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 395 |  | 
|  | 396 | // If the command has been suspended, resume it after KillExec() so that the | 
|  | 397 | // process can process the SIGTERM sent by KillExec(). | 
|  | 398 | if (is_current_command_suspended_) { | 
|  | 399 | ResumeAction(); | 
|  | 400 | } | 
|  | 401 |  | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 402 | current_command_ = 0; | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 403 | Cleanup(); | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 404 | } | 
|  | 405 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 406 | }  // namespace chromeos_update_engine |