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