blob: 1e998aeeb75bb3482e162dbfc483e078c070cd40 [file] [log] [blame]
Tom Cherryfa0c21c2015-07-23 17:53:11 -07001/*
2 * Copyright (C) 2015 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#include "action.h"
18
Tom Cherryede0d532017-07-06 14:20:11 -070019#include <android-base/chrono_utils.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070020#include <android-base/logging.h>
Elliott Hughesdc803122018-05-24 18:00:39 -070021#include <android-base/properties.h>
Tom Cherryccf23532017-03-28 16:40:41 -070022#include <android-base/strings.h>
Tom Cherryfa0c21c2015-07-23 17:53:11 -070023
Tom Cherryfa0c21c2015-07-23 17:53:11 -070024#include "util.h"
25
Tom Cherryb7349902015-08-26 11:43:36 -070026using android::base::Join;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070027
Tom Cherry81f5d3e2017-06-22 12:53:17 -070028namespace android {
29namespace init {
30
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070031Result<void> RunBuiltinFunction(const BuiltinFunction& function,
32 const std::vector<std::string>& args, const std::string& context) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070033 auto builtin_arguments = BuiltinArguments(context);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070034
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070035 builtin_arguments.args.resize(args.size());
36 builtin_arguments.args[0] = args[0];
37 for (std::size_t i = 1; i < args.size(); ++i) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -070038 auto expanded_arg = ExpandProps(args[i]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090039 if (!expanded_arg.ok()) {
Tom Cherryc5cf85d2019-07-31 13:59:15 -070040 return expanded_arg.error();
Tom Cherryfa0c21c2015-07-23 17:53:11 -070041 }
Tom Cherryc5cf85d2019-07-31 13:59:15 -070042 builtin_arguments.args[i] = std::move(*expanded_arg);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070043 }
44
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070045 return function(builtin_arguments);
46}
47
Tom Cherry018a4382018-10-17 11:11:23 -070048Command::Command(BuiltinFunction f, bool execute_in_subcontext, std::vector<std::string>&& args,
49 int line)
50 : func_(std::move(f)),
51 execute_in_subcontext_(execute_in_subcontext),
52 args_(std::move(args)),
53 line_(line) {}
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070054
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070055Result<void> Command::InvokeFunc(Subcontext* subcontext) const {
Tom Cherryc49719f2018-01-10 11:04:34 -080056 if (subcontext) {
57 if (execute_in_subcontext_) {
58 return subcontext->Execute(args_);
59 }
60
61 auto expanded_args = subcontext->ExpandArgs(args_);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090062 if (!expanded_args.ok()) {
Tom Cherryc49719f2018-01-10 11:04:34 -080063 return expanded_args.error();
64 }
65 return RunBuiltinFunction(func_, *expanded_args, subcontext->context());
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070066 }
Tom Cherryc49719f2018-01-10 11:04:34 -080067
68 return RunBuiltinFunction(func_, args_, kInitContext);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070069}
70
Tom Cherry4772f1d2019-07-30 09:34:41 -070071Result<void> Command::CheckCommand() const {
72 auto builtin_arguments = BuiltinArguments("host_init_verifier");
73
74 builtin_arguments.args.resize(args_.size());
75 builtin_arguments.args[0] = args_[0];
76 for (size_t i = 1; i < args_.size(); ++i) {
77 auto expanded_arg = ExpandProps(args_[i]);
Bernie Innocenticecebbb2020-02-06 03:49:33 +090078 if (!expanded_arg.ok()) {
Tom Cherry4772f1d2019-07-30 09:34:41 -070079 if (expanded_arg.error().message().find("doesn't exist while expanding") !=
80 std::string::npos) {
81 // If we failed because we won't have a property, use an empty string, which is
82 // never returned from the parser, to indicate that this field cannot be checked.
83 builtin_arguments.args[i] = "";
84 } else {
85 return expanded_arg.error();
86 }
87 } else {
88 builtin_arguments.args[i] = std::move(*expanded_arg);
89 }
90 }
91
92 return func_(builtin_arguments);
93}
94
Tom Cherryb7349902015-08-26 11:43:36 -070095std::string Command::BuildCommandString() const {
96 return Join(args_, ' ');
Tom Cherryfa0c21c2015-07-23 17:53:11 -070097}
98
Tom Cherry9cbf5702018-02-13 16:24:51 -080099Action::Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line,
100 const std::string& event_trigger,
101 const std::map<std::string, std::string>& property_triggers)
102 : property_triggers_(property_triggers),
103 event_trigger_(event_trigger),
104 oneshot_(oneshot),
105 subcontext_(subcontext),
106 filename_(filename),
107 line_(line) {}
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700108
Tom Cherryd52a5b32019-07-22 16:05:36 -0700109const BuiltinFunctionMap* Action::function_map_ = nullptr;
Tom Cherryb7349902015-08-26 11:43:36 -0700110
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700111Result<void> Action::AddCommand(std::vector<std::string>&& args, int line) {
Tom Cherryb7349902015-08-26 11:43:36 -0700112 if (!function_map_) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700113 return Error() << "no function map available";
Tom Cherryb7349902015-08-26 11:43:36 -0700114 }
115
Tom Cherryd52a5b32019-07-22 16:05:36 -0700116 auto map_result = function_map_->Find(args);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900117 if (!map_result.ok()) {
Tom Cherryd52a5b32019-07-22 16:05:36 -0700118 return Error() << map_result.error();
119 }
Tom Cherryb7349902015-08-26 11:43:36 -0700120
Tom Cherryd52a5b32019-07-22 16:05:36 -0700121 commands_.emplace_back(map_result->function, map_result->run_in_subcontext, std::move(args),
122 line);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700123 return {};
Tom Cherryb7349902015-08-26 11:43:36 -0700124}
125
Tom Cherry018a4382018-10-17 11:11:23 -0700126void Action::AddCommand(BuiltinFunction f, std::vector<std::string>&& args, int line) {
Tom Cherry7c1d87e2019-07-10 11:18:24 -0700127 commands_.emplace_back(std::move(f), false, std::move(args), line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700128}
129
Tom Cherryb7349902015-08-26 11:43:36 -0700130std::size_t Action::NumCommands() const {
131 return commands_.size();
132}
133
Tom Cherry4772f1d2019-07-30 09:34:41 -0700134size_t Action::CheckAllCommands() const {
135 size_t failures = 0;
136 for (const auto& command : commands_) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900137 if (auto result = command.CheckCommand(); !result.ok()) {
Tom Cherry4772f1d2019-07-30 09:34:41 -0700138 LOG(ERROR) << "Command '" << command.BuildCommandString() << "' (" << filename_ << ":"
139 << command.line() << ") failed: " << result.error();
140 ++failures;
141 }
142 }
143 return failures;
144}
145
Tom Cherryb7349902015-08-26 11:43:36 -0700146void Action::ExecuteOneCommand(std::size_t command) const {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800147 // We need a copy here since some Command execution may result in
148 // changing commands_ vector by importing .rc files through parser
149 Command cmd = commands_[command];
150 ExecuteCommand(cmd);
Tom Cherryb7349902015-08-26 11:43:36 -0700151}
152
153void Action::ExecuteAllCommands() const {
154 for (const auto& c : commands_) {
155 ExecuteCommand(c);
156 }
157}
158
159void Action::ExecuteCommand(const Command& command) const {
Tom Cherryede0d532017-07-06 14:20:11 -0700160 android::base::Timer t;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700161 auto result = command.InvokeFunc(subcontext_);
Tom Cherryede0d532017-07-06 14:20:11 -0700162 auto duration = t.duration();
Tom Cherry557946e2017-08-01 13:50:23 -0700163
Wei Wang8b1d5262016-11-15 23:58:55 -0800164 // Any action longer than 50ms will be warned to user as slow operation
Tom Cherryd17c3792019-07-30 10:51:59 -0700165 if (!result.has_value() || duration > 50ms ||
Tom Cherry68f2a462017-08-22 16:15:26 -0700166 android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700167 std::string trigger_name = BuildTriggersString();
168 std::string cmd_str = command.BuildCommandString();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700169
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700170 LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_
Tom Cherry557946e2017-08-01 13:50:23 -0700171 << ":" << command.line() << ") took " << duration.count() << "ms and "
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900172 << (result.ok() ? "succeeded" : "failed: " + result.error().message());
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700173 }
174}
175
Tom Cherryb7349902015-08-26 11:43:36 -0700176// This function checks that all property triggers are satisfied, that is
177// for each (name, value) in property_triggers_, check that the current
178// value of the property 'name' == value.
179//
180// It takes an optional (name, value) pair, which if provided must
181// be present in property_triggers_; it skips the check of the current
182// property value for this pair.
Tom Cherry1efc4842019-11-14 09:19:07 -0800183bool Action::CheckPropertyTriggers(const std::string& name, const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700184 if (property_triggers_.empty()) {
185 return true;
186 }
187
Tom Cherry1efc4842019-11-14 09:19:07 -0800188 if (!name.empty()) {
189 auto it = property_triggers_.find(name);
190 if (it == property_triggers_.end()) {
191 return false;
192 }
193 const auto& trigger_value = it->second;
194 if (trigger_value != "*" && trigger_value != value) {
195 return false;
196 }
197 }
198
Tom Cherry2bc00142017-03-13 11:58:58 -0700199 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherry1efc4842019-11-14 09:19:07 -0800200 if (trigger_name != name) {
Tom Cherry6fd8d3b2019-08-12 09:26:20 -0700201 std::string prop_value = android::base::GetProperty(trigger_name, "");
202 if (trigger_value == "*" && !prop_value.empty()) {
203 continue;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700204 }
Tom Cherry6fd8d3b2019-08-12 09:26:20 -0700205 if (trigger_value != prop_value) return false;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700206 }
207 }
Tom Cherry1efc4842019-11-14 09:19:07 -0800208 return true;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700209}
210
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700211bool Action::CheckEvent(const EventTrigger& event_trigger) const {
212 return event_trigger == event_trigger_ && CheckPropertyTriggers();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700213}
214
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700215bool Action::CheckEvent(const PropertyChange& property_change) const {
216 const auto& [name, value] = property_change;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700217 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
218}
219
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700220bool Action::CheckEvent(const BuiltinAction& builtin_action) const {
221 return this == builtin_action;
222}
223
Tom Cherryb7349902015-08-26 11:43:36 -0700224std::string Action::BuildTriggersString() const {
Tom Cherryd8a72572017-03-13 12:24:49 -0700225 std::vector<std::string> triggers;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700226
Tom Cherry2bc00142017-03-13 11:58:58 -0700227 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700228 triggers.emplace_back(trigger_name + '=' + trigger_value);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700229 }
230 if (!event_trigger_.empty()) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700231 triggers.emplace_back(event_trigger_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700232 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700233
234 return Join(triggers, " && ");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700235}
236
Tom Cherryb7349902015-08-26 11:43:36 -0700237void Action::DumpState() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700238 std::string trigger_name = BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700239 LOG(INFO) << "on " << trigger_name;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700240
241 for (const auto& c : commands_) {
Tom Cherryb7349902015-08-26 11:43:36 -0700242 std::string cmd_str = c.BuildCommandString();
Tom Cherryd8a72572017-03-13 12:24:49 -0700243 LOG(INFO) << " " << cmd_str;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700244 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700245}
246
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700247} // namespace init
248} // namespace android