| 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 |  | 
|  | 20 | #include <string> | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 21 | #include <vector> | 
|  | 22 |  | 
|  | 23 | #include <brillo/message_loops/message_loop.h> | 
|  | 24 | #include <gtest/gtest_prod.h> | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 25 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/action.h" | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 27 | #include "update_engine/common/boot_control_interface.h" | 
|  | 28 | #include "update_engine/common/hardware_interface.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/payload_consumer/install_plan.h" | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 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 |  | 
| Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 36 | class BootControlInterface; | 
|  | 37 |  | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 38 | class PostinstallRunnerAction : public InstallPlanAction { | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 39 | public: | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 40 | PostinstallRunnerAction(BootControlInterface* boot_control, | 
|  | 41 | HardwareInterface* hardware) | 
|  | 42 | : boot_control_(boot_control), hardware_(hardware) {} | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 43 |  | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 44 | // InstallPlanAction overrides. | 
| Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 45 | void PerformAction() override; | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 46 | void SuspendAction() override; | 
|  | 47 | void ResumeAction() override; | 
|  | 48 | void TerminateProcessing() override; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 49 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 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 |  | 
|  | 59 | void set_delegate(DelegateInterface* delegate) { delegate_ = delegate; } | 
|  | 60 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 61 | // Debugging/logging | 
|  | 62 | static std::string StaticType() { return "PostinstallRunnerAction"; } | 
| Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 63 | std::string Type() const override { return StaticType(); } | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 64 |  | 
|  | 65 | private: | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 66 | friend class PostinstallRunnerActionTest; | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 67 | FRIEND_TEST(PostinstallRunnerActionTest, ProcessProgressLineTest); | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 68 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 69 | void PerformPartitionPostinstall(); | 
|  | 70 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 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(); | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 90 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 91 | // Subprocess::Exec callback. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 92 | void CompletePartitionPostinstall(int return_code, | 
|  | 93 | const std::string& output); | 
|  | 94 |  | 
| Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 95 | // Complete the Action with the passed |error_code| and mark the new slot as | 
|  | 96 | // 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] | 97 | void CompletePostinstall(ErrorCode error_code); | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 98 |  | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 99 | InstallPlan install_plan_; | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 100 |  | 
|  | 101 | // The path where the filesystem will be mounted during post-install. | 
|  | 102 | std::string fs_mount_dir_; | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 103 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 104 | // The partition being processed on the list of partitions specified in the | 
|  | 105 | // InstallPlan. | 
|  | 106 | size_t current_partition_{0}; | 
|  | 107 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 108 | // A non-negative value representing the estimated weight of each partition | 
|  | 109 | // passed in the install plan. The weight is used to predict the overall | 
|  | 110 | // progress from the individual progress of each partition and should | 
|  | 111 | // correspond to the time it takes to run it. | 
|  | 112 | std::vector<double> partition_weight_; | 
|  | 113 |  | 
|  | 114 | // The sum of all the weights in |partition_weight_|. | 
|  | 115 | double total_weight_{0}; | 
|  | 116 |  | 
|  | 117 | // The sum of all the weights in |partition_weight_| up to but not including | 
|  | 118 | // the |current_partition_|. | 
|  | 119 | double accumulated_weight_{0}; | 
|  | 120 |  | 
|  | 121 | // The delegate used to notify of progress updates, if any. | 
|  | 122 | DelegateInterface* delegate_{nullptr}; | 
|  | 123 |  | 
| Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 124 | // The BootControlInerface used to mark the new slot as ready. | 
|  | 125 | BootControlInterface* boot_control_; | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 126 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 127 | // HardwareInterface used to signal powerwash. | 
|  | 128 | HardwareInterface* hardware_; | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 129 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 130 | // Whether the Powerwash was scheduled before invoking post-install script. | 
|  | 131 | // Used for cleaning up if post-install fails. | 
|  | 132 | bool powerwash_scheduled_{false}; | 
| Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 133 |  | 
| Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 134 | // Postinstall command currently running, or 0 if no program running. | 
|  | 135 | pid_t current_command_{0}; | 
|  | 136 |  | 
| Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 137 | // The parent progress file descriptor used to watch for progress reports from | 
|  | 138 | // the postinstall program and the task watching for them. | 
|  | 139 | int progress_fd_{-1}; | 
|  | 140 | brillo::MessageLoop::TaskId progress_task_{brillo::MessageLoop::kTaskIdNull}; | 
|  | 141 |  | 
|  | 142 | // A buffer of a partial read line from the progress file descriptor. | 
|  | 143 | std::string progress_buffer_; | 
|  | 144 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 145 | DISALLOW_COPY_AND_ASSIGN(PostinstallRunnerAction); | 
|  | 146 | }; | 
|  | 147 |  | 
|  | 148 | }  // namespace chromeos_update_engine | 
|  | 149 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 150 | #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_POSTINSTALL_RUNNER_ACTION_H_ |