blob: e5dfc405a9b1a35c2e8613c490605b614c8771e0 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_POSTINSTALL_RUNNER_ACTION_H_
18#define UPDATE_ENGINE_PAYLOAD_CONSUMER_POSTINSTALL_RUNNER_ACTION_H_
adlr@google.com3defe6a2009-12-04 20:57:17 +000019
Hidehiko Abe493fecb2019-07-10 23:30:50 +090020#include <memory>
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include <string>
Alex Deymo0d298542016-03-30 18:31:49 -070022#include <vector>
23
Hidehiko Abe493fecb2019-07-10 23:30:50 +090024#include <base/files/file_descriptor_watcher_posix.h>
Alex Deymo0d298542016-03-30 18:31:49 -070025#include <brillo/message_loops/message_loop.h>
26#include <gtest/gtest_prod.h>
Darin Petkov6f03a3b2010-11-10 14:27:14 -080027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/action.h"
Alex Deymofb905d92016-06-03 19:26:58 -070029#include "update_engine/common/boot_control_interface.h"
30#include "update_engine/common/hardware_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080031#include "update_engine/payload_consumer/install_plan.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000032
33// The Postinstall Runner Action is responsible for running the postinstall
34// script of a successfully downloaded update.
35
36namespace chromeos_update_engine {
37
Alex Deymob15a0b82015-11-25 20:30:40 -030038class BootControlInterface;
39
Chris Sosad317e402013-06-12 13:47:09 -070040class PostinstallRunnerAction : public InstallPlanAction {
adlr@google.com3defe6a2009-12-04 20:57:17 +000041 public:
Alex Deymofb905d92016-06-03 19:26:58 -070042 PostinstallRunnerAction(BootControlInterface* boot_control,
43 HardwareInterface* hardware)
44 : boot_control_(boot_control), hardware_(hardware) {}
adlr@google.com3defe6a2009-12-04 20:57:17 +000045
Alex Deymod15c5462016-03-09 18:11:12 -080046 // InstallPlanAction overrides.
Alex Deymocbc22742016-03-04 17:53:02 -080047 void PerformAction() override;
Alex Deymod15c5462016-03-09 18:11:12 -080048 void SuspendAction() override;
49 void ResumeAction() override;
50 void TerminateProcessing() override;
adlr@google.com3defe6a2009-12-04 20:57:17 +000051
Alex Deymo0d298542016-03-30 18:31:49 -070052 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.com3defe6a2009-12-04 20:57:17 +000063 // Debugging/logging
64 static std::string StaticType() { return "PostinstallRunnerAction"; }
Alex Deymocbc22742016-03-04 17:53:02 -080065 std::string Type() const override { return StaticType(); }
adlr@google.com3defe6a2009-12-04 20:57:17 +000066
67 private:
Alex Deymo31d95ac2015-09-17 11:56:18 -070068 friend class PostinstallRunnerActionTest;
Alex Deymo0d298542016-03-30 18:31:49 -070069 FRIEND_TEST(PostinstallRunnerActionTest, ProcessProgressLineTest);
Alex Deymo31d95ac2015-09-17 11:56:18 -070070
Alex Deymoe5e5fe92015-10-05 09:28:19 -070071 void PerformPartitionPostinstall();
72
Alex Deymo0d298542016-03-30 18:31:49 -070073 // Called whenever the |progress_fd_| has data available to read.
74 void OnProgressFdReady();
75
76 // Updates the action progress according to the |line| passed from the
77 // postinstall program. Valid lines are:
78 // global_progress <frac>
79 // <frac> should be between 0.0 and 1.0; sets the progress to the
80 // <frac> value.
81 bool ProcessProgressLine(const std::string& line);
82
83 // Report the progress to the delegate given that the postinstall operation
84 // for |current_partition_| has a current progress of |frac|, a value between
85 // 0 and 1 for that step.
86 void ReportProgress(double frac);
87
88 // Cleanup the setup made when running postinstall for a given partition.
89 // Unmount and remove the mountpoint directory if needed and cleanup the
90 // status file descriptor and message loop task watching for it.
91 void Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -080092
Darin Petkov6f03a3b2010-11-10 14:27:14 -080093 // Subprocess::Exec callback.
Amin Hassani008c4582019-01-13 16:22:47 -080094 void CompletePartitionPostinstall(int return_code, const std::string& output);
Alex Deymoe5e5fe92015-10-05 09:28:19 -070095
Alex Deymob15a0b82015-11-25 20:30:40 -030096 // Complete the Action with the passed |error_code| and mark the new slot as
97 // ready. Called when the post-install script was run for all the partitions.
Alex Deymoe5e5fe92015-10-05 09:28:19 -070098 void CompletePostinstall(ErrorCode error_code);
Darin Petkov6f03a3b2010-11-10 14:27:14 -080099
Chris Sosad317e402013-06-12 13:47:09 -0700100 InstallPlan install_plan_;
Alex Deymo390efed2016-02-18 11:00:40 -0800101
102 // The path where the filesystem will be mounted during post-install.
103 std::string fs_mount_dir_;
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800104
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700105 // The partition being processed on the list of partitions specified in the
106 // InstallPlan.
107 size_t current_partition_{0};
108
Alex Deymo0d298542016-03-30 18:31:49 -0700109 // A non-negative value representing the estimated weight of each partition
110 // passed in the install plan. The weight is used to predict the overall
111 // progress from the individual progress of each partition and should
112 // correspond to the time it takes to run it.
113 std::vector<double> partition_weight_;
114
115 // The sum of all the weights in |partition_weight_|.
116 double total_weight_{0};
117
118 // The sum of all the weights in |partition_weight_| up to but not including
119 // the |current_partition_|.
120 double accumulated_weight_{0};
121
122 // The delegate used to notify of progress updates, if any.
123 DelegateInterface* delegate_{nullptr};
124
Alex Deymob15a0b82015-11-25 20:30:40 -0300125 // The BootControlInerface used to mark the new slot as ready.
126 BootControlInterface* boot_control_;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700127
Alex Deymofb905d92016-06-03 19:26:58 -0700128 // HardwareInterface used to signal powerwash.
129 HardwareInterface* hardware_;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700130
Alex Deymofb905d92016-06-03 19:26:58 -0700131 // Whether the Powerwash was scheduled before invoking post-install script.
132 // Used for cleaning up if post-install fails.
133 bool powerwash_scheduled_{false};
Gilad Arnold30dedd82013-07-03 06:19:09 -0700134
Alex Deymod15c5462016-03-09 18:11:12 -0800135 // Postinstall command currently running, or 0 if no program running.
136 pid_t current_command_{0};
137
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800138 // True if |current_command_| has been suspended by SuspendAction().
139 bool is_current_command_suspended_{false};
140
Alex Deymo0d298542016-03-30 18:31:49 -0700141 // The parent progress file descriptor used to watch for progress reports from
142 // the postinstall program and the task watching for them.
143 int progress_fd_{-1};
Tianjie55abd3c2020-06-19 00:22:59 -0700144
145#ifdef __ANDROID__
Alex Deymo0d298542016-03-30 18:31:49 -0700146 brillo::MessageLoop::TaskId progress_task_{brillo::MessageLoop::kTaskIdNull};
Tianjie55abd3c2020-06-19 00:22:59 -0700147#else
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900148 std::unique_ptr<base::FileDescriptorWatcher::Controller> progress_controller_;
Tianjie55abd3c2020-06-19 00:22:59 -0700149#endif // __ANDROID__
Alex Deymo0d298542016-03-30 18:31:49 -0700150
151 // A buffer of a partial read line from the progress file descriptor.
152 std::string progress_buffer_;
153
adlr@google.com3defe6a2009-12-04 20:57:17 +0000154 DISALLOW_COPY_AND_ASSIGN(PostinstallRunnerAction);
155};
156
157} // namespace chromeos_update_engine
158
Alex Deymo39910dc2015-11-09 17:04:30 -0800159#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_POSTINSTALL_RUNNER_ACTION_H_