1 // 2 // Copyright (C) 2010 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 // 16 17 #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_POSTINSTALL_RUNNER_ACTION_H_ 18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_POSTINSTALL_RUNNER_ACTION_H_ 19 20 #include <string> 21 #include <vector> 22 23 #include <brillo/message_loops/message_loop.h> 24 #include <gtest/gtest_prod.h> 25 26 #include "update_engine/common/action.h" 27 #include "update_engine/common/boot_control_interface.h" 28 #include "update_engine/common/hardware_interface.h" 29 #include "update_engine/payload_consumer/install_plan.h" 30 31 // The Postinstall Runner Action is responsible for running the postinstall 32 // script of a successfully downloaded update. 33 34 namespace chromeos_update_engine { 35 36 class BootControlInterface; 37 38 class PostinstallRunnerAction : public InstallPlanAction { 39 public: PostinstallRunnerAction(BootControlInterface * boot_control,HardwareInterface * hardware)40 PostinstallRunnerAction(BootControlInterface* boot_control, 41 HardwareInterface* hardware) 42 : boot_control_(boot_control), hardware_(hardware) {} 43 44 // InstallPlanAction overrides. 45 void PerformAction() override; 46 void SuspendAction() override; 47 void ResumeAction() override; 48 void TerminateProcessing() override; 49 50 class DelegateInterface { 51 public: 52 virtual ~DelegateInterface() = default; 53 54 // Called whenever there is an overall progress update from the postinstall 55 // programs. 56 virtual void ProgressUpdate(double progress) = 0; 57 }; 58 set_delegate(DelegateInterface * delegate)59 void set_delegate(DelegateInterface* delegate) { delegate_ = delegate; } 60 61 // Debugging/logging StaticType()62 static std::string StaticType() { return "PostinstallRunnerAction"; } Type()63 std::string Type() const override { return StaticType(); } 64 65 private: 66 friend class PostinstallRunnerActionTest; 67 FRIEND_TEST(PostinstallRunnerActionTest, ProcessProgressLineTest); 68 69 void PerformPartitionPostinstall(); 70 71 // Called whenever the |progress_fd_| has data available to read. 72 void OnProgressFdReady(); 73 74 // Updates the action progress according to the |line| passed from the 75 // postinstall program. Valid lines are: 76 // global_progress <frac> 77 // <frac> should be between 0.0 and 1.0; sets the progress to the 78 // <frac> value. 79 bool ProcessProgressLine(const std::string& line); 80 81 // Report the progress to the delegate given that the postinstall operation 82 // for |current_partition_| has a current progress of |frac|, a value between 83 // 0 and 1 for that step. 84 void ReportProgress(double frac); 85 86 // Cleanup the setup made when running postinstall for a given partition. 87 // Unmount and remove the mountpoint directory if needed and cleanup the 88 // status file descriptor and message loop task watching for it. 89 void Cleanup(); 90 91 // Subprocess::Exec callback. 92 void CompletePartitionPostinstall(int return_code, const std::string& output); 93 94 // Complete the Action with the passed |error_code| and mark the new slot as 95 // ready. Called when the post-install script was run for all the partitions. 96 void CompletePostinstall(ErrorCode error_code); 97 98 InstallPlan install_plan_; 99 100 // The path where the filesystem will be mounted during post-install. 101 std::string fs_mount_dir_; 102 103 // The partition being processed on the list of partitions specified in the 104 // InstallPlan. 105 size_t current_partition_{0}; 106 107 // A non-negative value representing the estimated weight of each partition 108 // passed in the install plan. The weight is used to predict the overall 109 // progress from the individual progress of each partition and should 110 // correspond to the time it takes to run it. 111 std::vector<double> partition_weight_; 112 113 // The sum of all the weights in |partition_weight_|. 114 double total_weight_{0}; 115 116 // The sum of all the weights in |partition_weight_| up to but not including 117 // the |current_partition_|. 118 double accumulated_weight_{0}; 119 120 // The delegate used to notify of progress updates, if any. 121 DelegateInterface* delegate_{nullptr}; 122 123 // The BootControlInerface used to mark the new slot as ready. 124 BootControlInterface* boot_control_; 125 126 // HardwareInterface used to signal powerwash. 127 HardwareInterface* hardware_; 128 129 // Whether the Powerwash was scheduled before invoking post-install script. 130 // Used for cleaning up if post-install fails. 131 bool powerwash_scheduled_{false}; 132 133 // Postinstall command currently running, or 0 if no program running. 134 pid_t current_command_{0}; 135 136 // True if |current_command_| has been suspended by SuspendAction(). 137 bool is_current_command_suspended_{false}; 138 139 // The parent progress file descriptor used to watch for progress reports from 140 // the postinstall program and the task watching for them. 141 int progress_fd_{-1}; 142 brillo::MessageLoop::TaskId progress_task_{brillo::MessageLoop::kTaskIdNull}; 143 144 // A buffer of a partial read line from the progress file descriptor. 145 std::string progress_buffer_; 146 147 DISALLOW_COPY_AND_ASSIGN(PostinstallRunnerAction); 148 }; 149 150 } // namespace chromeos_update_engine 151 152 #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_POSTINSTALL_RUNNER_ACTION_H_ 153