1aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// 2aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Copyright (C) 2011 The Android Open Source Project 3aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// 4aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Licensed under the Apache License, Version 2.0 (the "License"); 5aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// you may not use this file except in compliance with the License. 6aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// You may obtain a copy of the License at 7aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// 8aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// http://www.apache.org/licenses/LICENSE-2.0 9aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// 10aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// Unless required by applicable law or agreed to in writing, software 11aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// distributed under the License is distributed on an "AS IS" BASIS, 12aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// See the License for the specific language governing permissions and 14aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// limitations under the License. 15aea4c1cea20dda7ae7e85fc8924a2d784f70d806Alex Deymo// 163defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.com 1739910dcd1d68987ccee7c3031dc269233a8490bbAlex Deymo#include "update_engine/payload_consumer/postinstall_runner_action.h" 181c0fe79c7ef2b43946d756b54c8505d2bf48b93bJay Srinivasan 190d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo#include <fcntl.h> 20d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo#include <signal.h> 213defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.com#include <stdlib.h> 2244cab30e0ee04b277e8463785ab069e9885a9f2dAlex Vakulenko#include <sys/mount.h> 23d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo#include <sys/types.h> 240d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo#include <unistd.h> 251c0fe79c7ef2b43946d756b54c8505d2bf48b93bJay Srinivasan 2644b35672c5f6064a890126a687db65ac12945299Alex Deymo#include <cmath> 2744b35672c5f6064a890126a687db65ac12945299Alex Deymo 28e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo#include <base/files/file_path.h> 29e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo#include <base/files/file_util.h> 30d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo#include <base/logging.h> 310d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo#include <base/strings/string_split.h> 32390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo#include <base/strings/string_util.h> 33461b259af8815d782200782c5ba3599d8de4a66cAlex Deymo 3439910dcd1d68987ccee7c3031dc269233a8490bbAlex Deymo#include "update_engine/common/action_processor.h" 35b15a0b8eaf18c9e9341706df9f4ab59ce595a67cAlex Deymo#include "update_engine/common/boot_control_interface.h" 3614dbd333439f34c648b9f783ffa656ef565de0ccAlex Deymo#include "update_engine/common/platform_constants.h" 3739910dcd1d68987ccee7c3031dc269233a8490bbAlex Deymo#include "update_engine/common/subprocess.h" 3839910dcd1d68987ccee7c3031dc269233a8490bbAlex Deymo#include "update_engine/common/utils.h" 393defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.com 400d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymonamespace { 410d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 420d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo// The file descriptor number from the postinstall program's perspective where 430d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo// it can report status updates. This can be any number greater than 2 (stderr), 440d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo// but must be kept in sync with the "bin/postinst_progress" defined in the 450d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo// sample_images.sh file. 460d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymoconst int kPostinstallStatusFd = 3; 470d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 480d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo} // namespace 490d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 503defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.comnamespace chromeos_update_engine { 513defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.com 520d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymousing brillo::MessageLoop; 533defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.comusing std::string; 54f97144334e945a5ec88970b4b28f4e98ce0bbb80Andrew de los Reyesusing std::vector; 553defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.com 563defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.comvoid PostinstallRunnerAction::PerformAction() { 573defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.com CHECK(HasInputObject()); 58d317e40be46e1b69f624a8165472c99fe6346a1eChris Sosa install_plan_ = GetInputObject(); 59c1a8b426be9542bc880923711ca508ea3f84000eDarin Petkov 60e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo if (install_plan_.powerwash_required) { 61fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo if (hardware_->SchedulePowerwash()) { 62fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo powerwash_scheduled_ = true; 63e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo } else { 64e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo return CompletePostinstall(ErrorCode::kPostinstallPowerwashError); 65e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo } 66e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo } 673defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.com 680d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo // Initialize all the partition weights. 690d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo partition_weight_.resize(install_plan_.partitions.size()); 700d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo total_weight_ = 0; 710d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo for (size_t i = 0; i < install_plan_.partitions.size(); ++i) { 720d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo // TODO(deymo): This code sets the weight to all the postinstall commands, 730d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo // but we could remember how long they took in the past and use those 740d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo // values. 750d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo partition_weight_[i] = install_plan_.partitions[i].run_postinstall; 760d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo total_weight_ += partition_weight_[i]; 770d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo } 780d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo accumulated_weight_ = 0; 790d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo ReportProgress(0); 800d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 81e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo PerformPartitionPostinstall(); 82e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo} 83e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo 84e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymovoid PostinstallRunnerAction::PerformPartitionPostinstall() { 85390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo if (install_plan_.download_url.empty()) { 86390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo LOG(INFO) << "Skipping post-install during rollback"; 87390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo return CompletePostinstall(ErrorCode::kSuccess); 88390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo } 89390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo 90e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo // Skip all the partitions that don't have a post-install step. 91e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo while (current_partition_ < install_plan_.partitions.size() && 92e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo !install_plan_.partitions[current_partition_].run_postinstall) { 93e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo VLOG(1) << "Skipping post-install on partition " 94e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo << install_plan_.partitions[current_partition_].name; 95e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo current_partition_++; 96a78b28c6f202b801fe7cb85246b33afe01aeaa7aNam T. Nguyen } 97e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo if (current_partition_ == install_plan_.partitions.size()) 98e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo return CompletePostinstall(ErrorCode::kSuccess); 99a78b28c6f202b801fe7cb85246b33afe01aeaa7aNam T. Nguyen 100e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo const InstallPlan::Partition& partition = 101e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo install_plan_.partitions[current_partition_]; 102a78b28c6f202b801fe7cb85246b33afe01aeaa7aNam T. Nguyen 103e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo const string mountable_device = 104e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo utils::MakePartitionNameForMount(partition.target_path); 105e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo if (mountable_device.empty()) { 106e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo LOG(ERROR) << "Cannot make mountable device from " << partition.target_path; 107e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo return CompletePostinstall(ErrorCode::kPostinstallRunnerError); 108e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo } 109f97144334e945a5ec88970b4b28f4e98ce0bbb80Andrew de los Reyes 110e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo // Perform post-install for the current_partition_ partition. At this point we 111e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo // need to call CompletePartitionPostinstall to complete the operation and 112e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo // cleanup. 113390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo#ifdef __ANDROID__ 114390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo fs_mount_dir_ = "/postinstall"; 115390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo#else // __ANDROID__ 116371615b025bdd8106faf65c1ba7439a1236f21bcSen Jiang base::FilePath temp_dir; 117371615b025bdd8106faf65c1ba7439a1236f21bcSen Jiang TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir)); 118371615b025bdd8106faf65c1ba7439a1236f21bcSen Jiang fs_mount_dir_ = temp_dir.value(); 119390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo#endif // __ANDROID__ 120390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo 121f411650403c1d69c15ec0f2ee09b24c6c0ec67b5Alex Deymo // Double check that the fs_mount_dir is not busy with a previous mounted 122f411650403c1d69c15ec0f2ee09b24c6c0ec67b5Alex Deymo // filesystem from a previous crashed postinstall step. 123f411650403c1d69c15ec0f2ee09b24c6c0ec67b5Alex Deymo if (utils::IsMountpoint(fs_mount_dir_)) { 124f411650403c1d69c15ec0f2ee09b24c6c0ec67b5Alex Deymo LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_; 125f411650403c1d69c15ec0f2ee09b24c6c0ec67b5Alex Deymo utils::UnmountFilesystem(fs_mount_dir_); 126f411650403c1d69c15ec0f2ee09b24c6c0ec67b5Alex Deymo } 127f411650403c1d69c15ec0f2ee09b24c6c0ec67b5Alex Deymo 128cbc2274c4160805bf726df872390112654816ca7Alex Deymo base::FilePath postinstall_path(partition.postinstall_path); 129cbc2274c4160805bf726df872390112654816ca7Alex Deymo if (postinstall_path.IsAbsolute()) { 130cbc2274c4160805bf726df872390112654816ca7Alex Deymo LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative" 131cbc2274c4160805bf726df872390112654816ca7Alex Deymo "path instead: " 132cbc2274c4160805bf726df872390112654816ca7Alex Deymo << partition.postinstall_path; 133cbc2274c4160805bf726df872390112654816ca7Alex Deymo return CompletePostinstall(ErrorCode::kPostinstallRunnerError); 134cbc2274c4160805bf726df872390112654816ca7Alex Deymo } 135cbc2274c4160805bf726df872390112654816ca7Alex Deymo 136cbc2274c4160805bf726df872390112654816ca7Alex Deymo string abs_path = 137cbc2274c4160805bf726df872390112654816ca7Alex Deymo base::FilePath(fs_mount_dir_).Append(postinstall_path).value(); 138390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo if (!base::StartsWith( 139390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) { 140390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo LOG(ERROR) << "Invalid relative postinstall path: " 141390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo << partition.postinstall_path; 142390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo return CompletePostinstall(ErrorCode::kPostinstallRunnerError); 143390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo } 1446f03a3b868d4b632931400628763036f79c449f7Darin Petkov 1455fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo#ifdef __ANDROID__ 1465fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo // In Chromium OS, the postinstall step is allowed to write to the block 1475fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo // device on the target image, so we don't mark it as read-only and should 1485fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo // be read-write since we just wrote to it during the update. 1495fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo 1505fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo // Mark the block device as read-only before mounting for post-install. 1515fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) { 1525fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo return CompletePartitionPostinstall( 1535fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo 1, "Error marking the device " + mountable_device + " read only."); 1545fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo } 1555fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo#endif // __ANDROID__ 1565fb356cac8a87c62fa83005d99370aa2f8416064Alex Deymo 157390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo if (!utils::MountFilesystem(mountable_device, 158390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo fs_mount_dir_, 159390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo MS_RDONLY, 16014dbd333439f34c648b9f783ffa656ef565de0ccAlex Deymo partition.filesystem_type, 16114dbd333439f34c648b9f783ffa656ef565de0ccAlex Deymo constants::kPostinstallMountOptions)) { 162e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo return CompletePartitionPostinstall( 163e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo 1, "Error mounting the device " + mountable_device); 1641c0fe79c7ef2b43946d756b54c8505d2bf48b93bJay Srinivasan } 1651c0fe79c7ef2b43946d756b54c8505d2bf48b93bJay Srinivasan 166390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at " 167390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo << abs_path << ") installed on device " << partition.target_path 168e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo << " and mountable device " << mountable_device; 169e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo 170032e772f76a6cd0b4caf1af7bd02dd81af2dc7ddAlex Deymo // Logs the file format of the postinstall script we are about to run. This 171032e772f76a6cd0b4caf1af7bd02dd81af2dc7ddAlex Deymo // will help debug when the postinstall script doesn't match the architecture 172032e772f76a6cd0b4caf1af7bd02dd81af2dc7ddAlex Deymo // of our build. 173390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo LOG(INFO) << "Format file for new " << partition.postinstall_path 174390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo << " is: " << utils::GetFileFormat(abs_path); 175032e772f76a6cd0b4caf1af7bd02dd81af2dc7ddAlex Deymo 1766f03a3b868d4b632931400628763036f79c449f7Darin Petkov // Runs the postinstall script asynchronously to free up the main loop while 1776f03a3b868d4b632931400628763036f79c449f7Darin Petkov // it's running. 1780d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo vector<string> command = {abs_path}; 1790d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo#ifdef __ANDROID__ 1800d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo // In Brillo and Android, we pass the slot number and status fd. 1810d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo command.push_back(std::to_string(install_plan_.target_slot)); 1820d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo command.push_back(std::to_string(kPostinstallStatusFd)); 1830d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo#else 1840d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo // Chrome OS postinstall expects the target rootfs as the first parameter. 1850d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo command.push_back(partition.target_path); 1860d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo#endif // __ANDROID__ 1870d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 1880d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo current_command_ = Subprocess::Get().ExecFlags( 189d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo command, 1900d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo Subprocess::kRedirectStderrToStdout, 1910d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo {kPostinstallStatusFd}, 192d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall, 193d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo base::Unretained(this))); 194d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo // Subprocess::Exec should never return a negative process id. 195d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo CHECK_GE(current_command_, 0); 196d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo 1970d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo if (!current_command_) { 198e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo CompletePartitionPostinstall(1, "Postinstall didn't launch"); 1990d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo return; 2000d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo } 2010d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 2020d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo // Monitor the status file descriptor. 2030d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo progress_fd_ = 2040d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd); 2050d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK; 2060d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) { 2070d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_; 2080d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo } 2090d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 2100d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo progress_task_ = MessageLoop::current()->WatchFileDescriptor( 2110d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo FROM_HERE, 2120d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo progress_fd_, 2130d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo MessageLoop::WatchMode::kWatchRead, 2140d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo true, 2150d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo base::Bind(&PostinstallRunnerAction::OnProgressFdReady, 2160d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo base::Unretained(this))); 2176f03a3b868d4b632931400628763036f79c449f7Darin Petkov} 2186f03a3b868d4b632931400628763036f79c449f7Darin Petkov 2190d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymovoid PostinstallRunnerAction::OnProgressFdReady() { 2200d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo char buf[1024]; 2210d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo size_t bytes_read; 2220d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo do { 2230d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo bytes_read = 0; 2240d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo bool eof; 2250d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo bool ok = 2260d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo utils::ReadAll(progress_fd_, buf, arraysize(buf), &bytes_read, &eof); 2270d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo progress_buffer_.append(buf, bytes_read); 2280d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo // Process every line. 2290d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo vector<string> lines = base::SplitString( 2300d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); 2310d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo if (!lines.empty()) { 2320d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo progress_buffer_ = lines.back(); 2330d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo lines.pop_back(); 2340d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo for (const auto& line : lines) { 2350d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo ProcessProgressLine(line); 2360d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo } 2370d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo } 2380d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo if (!ok || eof) { 2390d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo // There was either an error or an EOF condition, so we are done watching 2400d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo // the file descriptor. 2410d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo MessageLoop::current()->CancelTask(progress_task_); 2420d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo progress_task_ = MessageLoop::kTaskIdNull; 2430d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo return; 2440d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo } 2450d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo } while (bytes_read); 2460d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo} 2470d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 2480d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymobool PostinstallRunnerAction::ProcessProgressLine(const string& line) { 2490d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo double frac = 0; 250a2ea1c2865f037319dc6d88267e26e386d86b087Alex Deymo if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 && 251a2ea1c2865f037319dc6d88267e26e386d86b087Alex Deymo !std::isnan(frac)) { 2520d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo ReportProgress(frac); 2530d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo return true; 2540d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo } 2550d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 2560d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo return false; 2570d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo} 2580d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 2590d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymovoid PostinstallRunnerAction::ReportProgress(double frac) { 2600d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo if (!delegate_) 2610d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo return; 2620d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo if (current_partition_ >= partition_weight_.size()) { 2630d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo delegate_->ProgressUpdate(1.); 2640d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo return; 2650d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo } 26644b35672c5f6064a890126a687db65ac12945299Alex Deymo if (!std::isfinite(frac) || frac < 0) 2670d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo frac = 0; 2680d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo if (frac > 1) 2690d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo frac = 1; 2700d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo double postinst_action_progress = 2710d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo (accumulated_weight_ + partition_weight_[current_partition_] * frac) / 2720d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo total_weight_; 2730d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo delegate_->ProgressUpdate(postinst_action_progress); 2740d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo} 2750d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 2760d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymovoid PostinstallRunnerAction::Cleanup() { 277390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo utils::UnmountFilesystem(fs_mount_dir_); 278d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo#ifndef __ANDROID__ 279390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) { 280390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_; 281e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo } 282d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo#endif // !__ANDROID__ 283390efedcb7e17587da765b6d682077cb7fa46ee1Alex Deymo fs_mount_dir_.clear(); 2840d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 2850d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo progress_fd_ = -1; 2860d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo if (progress_task_ != MessageLoop::kTaskIdNull) { 2870d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo MessageLoop::current()->CancelTask(progress_task_); 2880d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo progress_task_ = MessageLoop::kTaskIdNull; 2890d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo } 2900d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo progress_buffer_.clear(); 291d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo} 292d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo 293d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymovoid PostinstallRunnerAction::CompletePartitionPostinstall( 294d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo int return_code, const string& output) { 295d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo current_command_ = 0; 2960d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo Cleanup(); 29731d95ac85d294b2b1bfa293835013e66c010fbcfAlex Deymo 2986f03a3b868d4b632931400628763036f79c449f7Darin Petkov if (return_code != 0) { 2996f03a3b868d4b632931400628763036f79c449f7Darin Petkov LOG(ERROR) << "Postinst command failed with code: " << return_code; 300e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo ErrorCode error_code = ErrorCode::kPostinstallRunnerError; 3011c0fe79c7ef2b43946d756b54c8505d2bf48b93bJay Srinivasan 302fe57d5472f690f6ab9160f54006ebeacbbe87efeAndrew de los Reyes if (return_code == 3) { 303c1d5c93ddf56e193fd501e5f8fc3fc45efdc560fAndrew de los Reyes // This special return code means that we tried to update firmware, 304c1d5c93ddf56e193fd501e5f8fc3fc45efdc560fAndrew de los Reyes // but couldn't because we booted from FW B, and we need to reboot 305c1d5c93ddf56e193fd501e5f8fc3fc45efdc560fAndrew de los Reyes // to get back to FW A. 306e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo error_code = ErrorCode::kPostinstallBootedFromFirmwareB; 307c1d5c93ddf56e193fd501e5f8fc3fc45efdc560fAndrew de los Reyes } 30881018e089aedb07d806f5e6a18dc44e359a6fc06Don Garrett 30981018e089aedb07d806f5e6a18dc44e359a6fc06Don Garrett if (return_code == 4) { 31081018e089aedb07d806f5e6a18dc44e359a6fc06Don Garrett // This special return code means that we tried to update firmware, 31181018e089aedb07d806f5e6a18dc44e359a6fc06Don Garrett // but couldn't because we booted from FW B, and we need to reboot 31281018e089aedb07d806f5e6a18dc44e359a6fc06Don Garrett // to get back to FW A. 313e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable; 31481018e089aedb07d806f5e6a18dc44e359a6fc06Don Garrett } 3155b91c6b141970c2b0095775a61e3f941417aa1ffAlex Deymo 3165b91c6b141970c2b0095775a61e3f941417aa1ffAlex Deymo // If postinstall script for this partition is optional we can ignore the 3175b91c6b141970c2b0095775a61e3f941417aa1ffAlex Deymo // result. 3185b91c6b141970c2b0095775a61e3f941417aa1ffAlex Deymo if (install_plan_.partitions[current_partition_].postinstall_optional) { 3195b91c6b141970c2b0095775a61e3f941417aa1ffAlex Deymo LOG(INFO) << "Ignoring postinstall failure since it is optional"; 3205b91c6b141970c2b0095775a61e3f941417aa1ffAlex Deymo } else { 3215b91c6b141970c2b0095775a61e3f941417aa1ffAlex Deymo return CompletePostinstall(error_code); 3225b91c6b141970c2b0095775a61e3f941417aa1ffAlex Deymo } 323e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo } 3240d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo accumulated_weight_ += partition_weight_[current_partition_]; 325e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo current_partition_++; 3260d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo ReportProgress(0); 3270d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo 328e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo PerformPartitionPostinstall(); 329e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo} 330e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo 331e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymovoid PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) { 332e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo // We only attempt to mark the new slot as active if all the postinstall 333e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo // steps succeeded. 334e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo if (error_code == ErrorCode::kSuccess && 335b15a0b8eaf18c9e9341706df9f4ab59ce595a67cAlex Deymo !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) { 336e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo error_code = ErrorCode::kPostinstallRunnerError; 337e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo } 338e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo 339e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo ScopedActionCompleter completer(processor_, this); 340cbc2274c4160805bf726df872390112654816ca7Alex Deymo completer.set_code(error_code); 341e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo 342e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo if (error_code != ErrorCode::kSuccess) { 343e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo LOG(ERROR) << "Postinstall action failed."; 344e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo 345fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo // Undo any changes done to trigger Powerwash. 346fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo if (powerwash_scheduled_) 347fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo hardware_->CancelPowerwash(); 34881018e089aedb07d806f5e6a18dc44e359a6fc06Don Garrett 3496f03a3b868d4b632931400628763036f79c449f7Darin Petkov return; 3506f03a3b868d4b632931400628763036f79c449f7Darin Petkov } 351ae4697c073b84b260990a141acd53c6806da0708Jay Srinivasan 352e5e5fe926e9ea45b1a381af1bee91a86643ffd72Alex Deymo LOG(INFO) << "All post-install commands succeeded"; 353d317e40be46e1b69f624a8165472c99fe6346a1eChris Sosa if (HasOutputPipe()) { 354d317e40be46e1b69f624a8165472c99fe6346a1eChris Sosa SetOutputObject(install_plan_); 355d317e40be46e1b69f624a8165472c99fe6346a1eChris Sosa } 3563defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.com} 3573defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.com 358d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymovoid PostinstallRunnerAction::SuspendAction() { 359d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo if (!current_command_) 360d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo return; 361d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo if (kill(current_command_, SIGSTOP) != 0) { 362d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo PLOG(ERROR) << "Couldn't pause child process " << current_command_; 3637f4bc3f009c711b5bd1bd145f7f53cd837ed6414Ben Chan } else { 3647f4bc3f009c711b5bd1bd145f7f53cd837ed6414Ben Chan is_current_command_suspended_ = true; 365d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo } 366d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo} 367d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo 368d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymovoid PostinstallRunnerAction::ResumeAction() { 369d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo if (!current_command_) 370d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo return; 371d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo if (kill(current_command_, SIGCONT) != 0) { 372d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo PLOG(ERROR) << "Couldn't resume child process " << current_command_; 3737f4bc3f009c711b5bd1bd145f7f53cd837ed6414Ben Chan } else { 3747f4bc3f009c711b5bd1bd145f7f53cd837ed6414Ben Chan is_current_command_suspended_ = false; 375d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo } 376d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo} 377d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo 378d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymovoid PostinstallRunnerAction::TerminateProcessing() { 379d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo if (!current_command_) 380d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo return; 381d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo // Calling KillExec() will discard the callback we registered and therefore 382d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo // the unretained reference to this object. 383d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo Subprocess::Get().KillExec(current_command_); 3847f4bc3f009c711b5bd1bd145f7f53cd837ed6414Ben Chan 3857f4bc3f009c711b5bd1bd145f7f53cd837ed6414Ben Chan // If the command has been suspended, resume it after KillExec() so that the 3867f4bc3f009c711b5bd1bd145f7f53cd837ed6414Ben Chan // process can process the SIGTERM sent by KillExec(). 3877f4bc3f009c711b5bd1bd145f7f53cd837ed6414Ben Chan if (is_current_command_suspended_) { 3887f4bc3f009c711b5bd1bd145f7f53cd837ed6414Ben Chan ResumeAction(); 3897f4bc3f009c711b5bd1bd145f7f53cd837ed6414Ben Chan } 3907f4bc3f009c711b5bd1bd145f7f53cd837ed6414Ben Chan 391d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo current_command_ = 0; 3920d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo Cleanup(); 393d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo} 394d15c546ed794293d0a63770467a0f3c4c84c6214Alex Deymo 3953defe6acb3609e70e851a6eff062577d25a2af9dadlr@google.com} // namespace chromeos_update_engine 396