blob: 4ec5f17391e8b166db02ef2206c6329650a33a29 [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>
Tom Cherryccf23532017-03-28 16:40:41 -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 Cherry012c5732017-04-18 13:21:54 -070031Command::Command(BuiltinFunction f, const std::vector<std::string>& args, int line)
32 : func_(f), args_(args), line_(line) {}
Tom Cherryfa0c21c2015-07-23 17:53:11 -070033
Tom Cherryb7349902015-08-26 11:43:36 -070034int Command::InvokeFunc() const {
Tom Cherry96f67312015-07-30 13:52:55 -070035 std::vector<std::string> expanded_args;
36 expanded_args.resize(args_.size());
37 expanded_args[0] = args_[0];
Tom Cherryfa0c21c2015-07-23 17:53:11 -070038 for (std::size_t i = 1; i < args_.size(); ++i) {
Tom Cherryb7349902015-08-26 11:43:36 -070039 if (!expand_props(args_[i], &expanded_args[i])) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070040 LOG(ERROR) << args_[0] << ": cannot expand '" << args_[i] << "'";
Tom Cherryfa0c21c2015-07-23 17:53:11 -070041 return -EINVAL;
42 }
43 }
44
Tom Cherry96f67312015-07-30 13:52:55 -070045 return func_(expanded_args);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070046}
47
Tom Cherryb7349902015-08-26 11:43:36 -070048std::string Command::BuildCommandString() const {
49 return Join(args_, ' ');
Tom Cherryfa0c21c2015-07-23 17:53:11 -070050}
51
Tom Cherry012c5732017-04-18 13:21:54 -070052Action::Action(bool oneshot, const std::string& filename, int line)
53 : oneshot_(oneshot), filename_(filename), line_(line) {}
Tom Cherryfa0c21c2015-07-23 17:53:11 -070054
Tom Cherryb7349902015-08-26 11:43:36 -070055const KeywordMap<BuiltinFunction>* Action::function_map_ = nullptr;
56
Tom Cherry012c5732017-04-18 13:21:54 -070057bool Action::AddCommand(const std::vector<std::string>& args, int line, std::string* err) {
Tom Cherryb7349902015-08-26 11:43:36 -070058 if (!function_map_) {
59 *err = "no function map available";
60 return false;
61 }
62
Tom Cherryfe062052017-04-24 16:59:05 -070063 auto function = function_map_->FindFunction(args, err);
Tom Cherryb7349902015-08-26 11:43:36 -070064 if (!function) {
65 return false;
66 }
67
Tom Cherry012c5732017-04-18 13:21:54 -070068 AddCommand(function, args, line);
Tom Cherryb7349902015-08-26 11:43:36 -070069 return true;
70}
71
Tom Cherry012c5732017-04-18 13:21:54 -070072void Action::AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line) {
73 commands_.emplace_back(f, args, line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070074}
75
Tom Cherryb7349902015-08-26 11:43:36 -070076std::size_t Action::NumCommands() const {
77 return commands_.size();
78}
79
80void Action::ExecuteOneCommand(std::size_t command) const {
Wei Wangd67a4ab2016-11-16 12:08:30 -080081 // We need a copy here since some Command execution may result in
82 // changing commands_ vector by importing .rc files through parser
83 Command cmd = commands_[command];
84 ExecuteCommand(cmd);
Tom Cherryb7349902015-08-26 11:43:36 -070085}
86
87void Action::ExecuteAllCommands() const {
88 for (const auto& c : commands_) {
89 ExecuteCommand(c);
90 }
91}
92
93void Action::ExecuteCommand(const Command& command) const {
Tom Cherryede0d532017-07-06 14:20:11 -070094 android::base::Timer t;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070095 int result = command.InvokeFunc();
96
Tom Cherryede0d532017-07-06 14:20:11 -070097 auto duration = t.duration();
Wei Wang8b1d5262016-11-15 23:58:55 -080098 // Any action longer than 50ms will be warned to user as slow operation
Tom Cherryede0d532017-07-06 14:20:11 -070099 if (duration > 50ms || android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700100 std::string trigger_name = BuildTriggersString();
101 std::string cmd_str = command.BuildCommandString();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700102
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700103 LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_
Tom Cherryede0d532017-07-06 14:20:11 -0700104 << ":" << command.line() << ") returned " << result << " took "
105 << duration.count() << "ms.";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700106 }
107}
108
Tom Cherryb7349902015-08-26 11:43:36 -0700109bool Action::ParsePropertyTrigger(const std::string& trigger, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700110 const static std::string prop_str("property:");
111 std::string prop_name(trigger.substr(prop_str.length()));
112 size_t equal_pos = prop_name.find('=');
113 if (equal_pos == std::string::npos) {
114 *err = "property trigger found without matching '='";
115 return false;
116 }
117
118 std::string prop_value(prop_name.substr(equal_pos + 1));
119 prop_name.erase(equal_pos);
120
Tom Cherry2bc00142017-03-13 11:58:58 -0700121 if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700122 *err = "multiple property triggers found for same property";
123 return false;
124 }
125 return true;
126}
127
Tom Cherryb7349902015-08-26 11:43:36 -0700128bool Action::InitTriggers(const std::vector<std::string>& args, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700129 const static std::string prop_str("property:");
130 for (std::size_t i = 0; i < args.size(); ++i) {
Wei Wang93df4e12016-11-16 19:19:49 -0800131 if (args[i].empty()) {
132 *err = "empty trigger is not valid";
133 return false;
134 }
135
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700136 if (i % 2) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700137 if (args[i] != "&&") {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700138 *err = "&& is the only symbol allowed to concatenate actions";
139 return false;
140 } else {
141 continue;
142 }
143 }
144
145 if (!args[i].compare(0, prop_str.length(), prop_str)) {
146 if (!ParsePropertyTrigger(args[i], err)) {
147 return false;
148 }
149 } else {
150 if (!event_trigger_.empty()) {
151 *err = "multiple event triggers are not allowed";
152 return false;
153 }
154
155 event_trigger_ = args[i];
156 }
157 }
158
159 return true;
160}
161
Tom Cherryb7349902015-08-26 11:43:36 -0700162bool Action::InitSingleTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700163 std::vector<std::string> name_vector{trigger};
164 std::string err;
Wei Wang93df4e12016-11-16 19:19:49 -0800165 bool ret = InitTriggers(name_vector, &err);
166 if (!ret) {
167 LOG(ERROR) << "InitSingleTrigger failed due to: " << err;
168 }
169 return ret;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700170}
171
Tom Cherryb7349902015-08-26 11:43:36 -0700172// This function checks that all property triggers are satisfied, that is
173// for each (name, value) in property_triggers_, check that the current
174// value of the property 'name' == value.
175//
176// It takes an optional (name, value) pair, which if provided must
177// be present in property_triggers_; it skips the check of the current
178// property value for this pair.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700179bool Action::CheckPropertyTriggers(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700180 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700181 if (property_triggers_.empty()) {
182 return true;
183 }
184
Tom Cherryb7349902015-08-26 11:43:36 -0700185 bool found = name.empty();
Tom Cherry2bc00142017-03-13 11:58:58 -0700186 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700187 if (trigger_name == name) {
188 if (trigger_value != "*" && trigger_value != value) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700189 return false;
190 } else {
191 found = true;
192 }
193 } else {
Tom Cherryccf23532017-03-28 16:40:41 -0700194 std::string prop_val = android::base::GetProperty(trigger_name, "");
195 if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700196 return false;
197 }
198 }
199 }
200 return found;
201}
202
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700203bool Action::CheckEvent(const EventTrigger& event_trigger) const {
204 return event_trigger == event_trigger_ && CheckPropertyTriggers();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700205}
206
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700207bool Action::CheckEvent(const PropertyChange& property_change) const {
208 const auto& [name, value] = property_change;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700209 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
210}
211
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700212bool Action::CheckEvent(const BuiltinAction& builtin_action) const {
213 return this == builtin_action;
214}
215
Tom Cherryb7349902015-08-26 11:43:36 -0700216std::string Action::BuildTriggersString() const {
Tom Cherryd8a72572017-03-13 12:24:49 -0700217 std::vector<std::string> triggers;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700218
Tom Cherry2bc00142017-03-13 11:58:58 -0700219 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700220 triggers.emplace_back(trigger_name + '=' + trigger_value);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700221 }
222 if (!event_trigger_.empty()) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700223 triggers.emplace_back(event_trigger_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700224 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700225
226 return Join(triggers, " && ");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700227}
228
Tom Cherryb7349902015-08-26 11:43:36 -0700229void Action::DumpState() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700230 std::string trigger_name = BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700231 LOG(INFO) << "on " << trigger_name;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700232
233 for (const auto& c : commands_) {
Tom Cherryb7349902015-08-26 11:43:36 -0700234 std::string cmd_str = c.BuildCommandString();
Tom Cherryd8a72572017-03-13 12:24:49 -0700235 LOG(INFO) << " " << cmd_str;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700236 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700237}
238
Tom Cherryb7349902015-08-26 11:43:36 -0700239ActionManager::ActionManager() : current_command_(0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700240}
241
242ActionManager& ActionManager::GetInstance() {
243 static ActionManager instance;
244 return instance;
245}
246
Tom Cherryb7349902015-08-26 11:43:36 -0700247void ActionManager::AddAction(std::unique_ptr<Action> action) {
Tom Cherry012c5732017-04-18 13:21:54 -0700248 actions_.emplace_back(std::move(action));
Tom Cherryb7349902015-08-26 11:43:36 -0700249}
250
251void ActionManager::QueueEventTrigger(const std::string& trigger) {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700252 event_queue_.emplace(trigger);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700253}
254
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700255void ActionManager::QueuePropertyChange(const std::string& name, const std::string& value) {
256 event_queue_.emplace(std::make_pair(name, value));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700257}
258
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700259void ActionManager::QueueAllPropertyActions() {
260 QueuePropertyChange("", "");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700261}
262
Tom Cherry012c5732017-04-18 13:21:54 -0700263void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) {
264 auto action = std::make_unique<Action>(true, "<Builtin Action>", 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700265 std::vector<std::string> name_vector{name};
266
Tom Cherryb7349902015-08-26 11:43:36 -0700267 if (!action->InitSingleTrigger(name)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700268 return;
269 }
270
Tom Cherry012c5732017-04-18 13:21:54 -0700271 action->AddCommand(func, name_vector, 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700272
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700273 event_queue_.emplace(action.get());
Tom Cherryb7349902015-08-26 11:43:36 -0700274 actions_.emplace_back(std::move(action));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700275}
276
277void ActionManager::ExecuteOneCommand() {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700278 // Loop through the event queue until we have an action to execute
279 while (current_executing_actions_.empty() && !event_queue_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700280 for (const auto& action : actions_) {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700281 if (std::visit([&action](const auto& event) { return action->CheckEvent(event); },
282 event_queue_.front())) {
Tom Cherryb7349902015-08-26 11:43:36 -0700283 current_executing_actions_.emplace(action.get());
284 }
285 }
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700286 event_queue_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700287 }
288
289 if (current_executing_actions_.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700290 return;
291 }
292
Tom Cherryb7349902015-08-26 11:43:36 -0700293 auto action = current_executing_actions_.front();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700294
Tom Cherrycb716f92015-08-11 12:34:22 -0700295 if (current_command_ == 0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700296 std::string trigger_name = action->BuildTriggersString();
Tom Cherry012c5732017-04-18 13:21:54 -0700297 LOG(INFO) << "processing action (" << trigger_name << ") from (" << action->filename()
298 << ":" << action->line() << ")";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700299 }
300
Tom Cherryb7349902015-08-26 11:43:36 -0700301 action->ExecuteOneCommand(current_command_);
302
303 // If this was the last command in the current action, then remove
304 // the action from the executing list.
305 // If this action was oneshot, then also remove it from actions_.
306 ++current_command_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700307 if (current_command_ == action->NumCommands()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700308 current_executing_actions_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700309 current_command_ = 0;
Tom Cherryb7349902015-08-26 11:43:36 -0700310 if (action->oneshot()) {
311 auto eraser = [&action] (std::unique_ptr<Action>& a) {
312 return a.get() == action;
313 };
314 actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
315 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700316 }
317}
318
Tom Cherryb7349902015-08-26 11:43:36 -0700319bool ActionManager::HasMoreCommands() const {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700320 return !current_executing_actions_.empty() || !event_queue_.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700321}
322
Tom Cherryb7349902015-08-26 11:43:36 -0700323void ActionManager::DumpState() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700324 for (const auto& a : actions_) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700325 a->DumpState();
326 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700327}
Tom Cherryb7349902015-08-26 11:43:36 -0700328
Wei Wangeeab4912017-06-27 22:08:45 -0700329void ActionManager::ClearQueue() {
330 // We are shutting down so don't claim the oneshot builtin actions back
331 current_executing_actions_ = {};
332 event_queue_ = {};
333 current_command_ = 0;
334}
335
Tom Cherry30a6f272017-04-19 15:31:58 -0700336bool ActionParser::ParseSection(std::vector<std::string>&& args, const std::string& filename,
Tom Cherry012c5732017-04-18 13:21:54 -0700337 int line, std::string* err) {
Tom Cherryb7349902015-08-26 11:43:36 -0700338 std::vector<std::string> triggers(args.begin() + 1, args.end());
339 if (triggers.size() < 1) {
340 *err = "actions must have a trigger";
341 return false;
342 }
343
Tom Cherry012c5732017-04-18 13:21:54 -0700344 auto action = std::make_unique<Action>(false, filename, line);
Tom Cherryb7349902015-08-26 11:43:36 -0700345 if (!action->InitTriggers(triggers, err)) {
346 return false;
347 }
348
349 action_ = std::move(action);
350 return true;
351}
352
Tom Cherry30a6f272017-04-19 15:31:58 -0700353bool ActionParser::ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) {
354 return action_ ? action_->AddCommand(std::move(args), line, err) : false;
Tom Cherryb7349902015-08-26 11:43:36 -0700355}
356
357void ActionParser::EndSection() {
358 if (action_ && action_->NumCommands() > 0) {
Tom Cherry30a6f272017-04-19 15:31:58 -0700359 action_manager_->AddAction(std::move(action_));
Tom Cherryb7349902015-08-26 11:43:36 -0700360 }
361}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700362
363} // namespace init
364} // namespace android