| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // | 
| 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 | #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_POSTINSTALL_RUNNER_ACTION_H_ | 
|  | 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_POSTINSTALL_RUNNER_ACTION_H_ | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 |  | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 20 | #include <memory> | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include <string> | 
| Kelvin Zhang | e9def4e | 2020-12-02 14:04:09 -0500 | [diff] [blame] | 22 | #include <utility> | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 23 | #include <vector> | 
|  | 24 |  | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 25 | #include <base/files/file_descriptor_watcher_posix.h> | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 26 | #include <brillo/message_loops/message_loop.h> | 
|  | 27 | #include <gtest/gtest_prod.h> | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 28 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/common/action.h" | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 30 | #include "update_engine/common/boot_control_interface.h" | 
|  | 31 | #include "update_engine/common/hardware_interface.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 32 | #include "update_engine/payload_consumer/install_plan.h" | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 33 |  | 
|  | 34 | // The Postinstall Runner Action is responsible for running the postinstall | 
|  | 35 | // script of a successfully downloaded update. | 
|  | 36 |  | 
|  | 37 | namespace chromeos_update_engine { | 
|  | 38 |  | 
| Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 39 | class BootControlInterface; | 
|  | 40 |  | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 41 | class PostinstallRunnerAction : public InstallPlanAction { | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 42 | public: | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 43 | PostinstallRunnerAction(BootControlInterface* boot_control, | 
| Kelvin Zhang | e9def4e | 2020-12-02 14:04:09 -0500 | [diff] [blame] | 44 | HardwareInterface* hardware); | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 45 |  | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 46 | // InstallPlanAction overrides. | 
| Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 47 | void PerformAction() override; | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 48 | void SuspendAction() override; | 
|  | 49 | void ResumeAction() override; | 
|  | 50 | void TerminateProcessing() override; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 51 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 52 | class DelegateInterface { | 
|  | 53 | public: | 
|  | 54 | virtual ~DelegateInterface() = default; | 
|  | 55 |  | 
|  | 56 | // Called whenever there is an overall progress update from the postinstall | 
|  | 57 | // programs. | 
|  | 58 | virtual void ProgressUpdate(double progress) = 0; | 
|  | 59 | }; | 
|  | 60 |  | 
|  | 61 | void set_delegate(DelegateInterface* delegate) { delegate_ = delegate; } | 
|  | 62 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 63 | // Debugging/logging | 
|  | 64 | static std::string StaticType() { return "PostinstallRunnerAction"; } | 
| Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 65 | std::string Type() const override { return StaticType(); } | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 66 |  | 
|  | 67 | private: | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 68 | friend class PostinstallRunnerActionTest; | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 69 | FRIEND_TEST(PostinstallRunnerActionTest, ProcessProgressLineTest); | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 70 |  | 
| Kelvin Zhang | e9def4e | 2020-12-02 14:04:09 -0500 | [diff] [blame] | 71 | // exposed for testing purposes only | 
|  | 72 | void SetMountDir(std::string dir) { fs_mount_dir_ = std::move(dir); } | 
|  | 73 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 74 | void PerformPartitionPostinstall(); | 
| Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 75 | [[nodiscard]] bool MountPartition( | 
|  | 76 | const InstallPlan::Partition& partition) noexcept; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 77 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 78 | // Called whenever the |progress_fd_| has data available to read. | 
|  | 79 | void OnProgressFdReady(); | 
|  | 80 |  | 
|  | 81 | // Updates the action progress according to the |line| passed from the | 
|  | 82 | // postinstall program. Valid lines are: | 
|  | 83 | //     global_progress <frac> | 
|  | 84 | //         <frac> should be between 0.0 and 1.0; sets the progress to the | 
|  | 85 | //         <frac> value. | 
|  | 86 | bool ProcessProgressLine(const std::string& line); | 
|  | 87 |  | 
|  | 88 | // Report the progress to the delegate given that the postinstall operation | 
|  | 89 | // for |current_partition_| has a current progress of |frac|, a value between | 
|  | 90 | // 0 and 1 for that step. | 
|  | 91 | void ReportProgress(double frac); | 
|  | 92 |  | 
|  | 93 | // Cleanup the setup made when running postinstall for a given partition. | 
|  | 94 | // Unmount and remove the mountpoint directory if needed and cleanup the | 
|  | 95 | // status file descriptor and message loop task watching for it. | 
|  | 96 | void Cleanup(); | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 97 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 98 | // Subprocess::Exec callback. | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 99 | void CompletePartitionPostinstall(int return_code, const std::string& output); | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 100 |  | 
| Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 101 | // Complete the Action with the passed |error_code| and mark the new slot as | 
|  | 102 | // ready. Called when the post-install script was run for all the partitions. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 103 | void CompletePostinstall(ErrorCode error_code); | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 104 |  | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 105 | // The path where the filesystem will be mounted during post-install. | 
|  | 106 | std::string fs_mount_dir_; | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 107 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 108 | // The partition being processed on the list of partitions specified in the | 
|  | 109 | // InstallPlan. | 
|  | 110 | size_t current_partition_{0}; | 
|  | 111 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 112 | // A non-negative value representing the estimated weight of each partition | 
|  | 113 | // passed in the install plan. The weight is used to predict the overall | 
|  | 114 | // progress from the individual progress of each partition and should | 
|  | 115 | // correspond to the time it takes to run it. | 
|  | 116 | std::vector<double> partition_weight_; | 
|  | 117 |  | 
|  | 118 | // The sum of all the weights in |partition_weight_|. | 
|  | 119 | double total_weight_{0}; | 
|  | 120 |  | 
|  | 121 | // The sum of all the weights in |partition_weight_| up to but not including | 
|  | 122 | // the |current_partition_|. | 
|  | 123 | double accumulated_weight_{0}; | 
|  | 124 |  | 
|  | 125 | // The delegate used to notify of progress updates, if any. | 
|  | 126 | DelegateInterface* delegate_{nullptr}; | 
|  | 127 |  | 
| Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 128 | // The BootControlInerface used to mark the new slot as ready. | 
|  | 129 | BootControlInterface* boot_control_; | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 130 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 131 | // HardwareInterface used to signal powerwash. | 
|  | 132 | HardwareInterface* hardware_; | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 133 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 134 | // Whether the Powerwash was scheduled before invoking post-install script. | 
|  | 135 | // Used for cleaning up if post-install fails. | 
|  | 136 | bool powerwash_scheduled_{false}; | 
| Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 137 |  | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 138 | // Postinstall command currently running, or 0 if no program running. | 
|  | 139 | pid_t current_command_{0}; | 
|  | 140 |  | 
| Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 141 | // True if |current_command_| has been suspended by SuspendAction(). | 
|  | 142 | bool is_current_command_suspended_{false}; | 
|  | 143 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 144 | // The parent progress file descriptor used to watch for progress reports from | 
|  | 145 | // the postinstall program and the task watching for them. | 
|  | 146 | int progress_fd_{-1}; | 
| Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 147 |  | 
| Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 148 | std::unique_ptr<base::FileDescriptorWatcher::Controller> progress_controller_; | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 149 |  | 
|  | 150 | // A buffer of a partial read line from the progress file descriptor. | 
|  | 151 | std::string progress_buffer_; | 
|  | 152 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(PostinstallRunnerAction); | 
|  | 154 | }; | 
|  | 155 |  | 
|  | 156 | }  // namespace chromeos_update_engine | 
|  | 157 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 158 | #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_POSTINSTALL_RUNNER_ACTION_H_ |