blob: 70a635efaf39de29106b88079b03ed9c4675168d [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 Cherry3f5eaae52017-04-06 16:30:22 -070019#include <android-base/logging.h>
Tom Cherryccf23532017-03-28 16:40:41 -070020#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080021#include <android-base/stringprintf.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;
27using android::base::StringPrintf;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070028
Tom Cherry012c5732017-04-18 13:21:54 -070029Command::Command(BuiltinFunction f, const std::vector<std::string>& args, int line)
30 : func_(f), args_(args), line_(line) {}
Tom Cherryfa0c21c2015-07-23 17:53:11 -070031
Tom Cherryb7349902015-08-26 11:43:36 -070032int Command::InvokeFunc() const {
Tom Cherry96f67312015-07-30 13:52:55 -070033 std::vector<std::string> expanded_args;
34 expanded_args.resize(args_.size());
35 expanded_args[0] = args_[0];
Tom Cherryfa0c21c2015-07-23 17:53:11 -070036 for (std::size_t i = 1; i < args_.size(); ++i) {
Tom Cherryb7349902015-08-26 11:43:36 -070037 if (!expand_props(args_[i], &expanded_args[i])) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070038 LOG(ERROR) << args_[0] << ": cannot expand '" << args_[i] << "'";
Tom Cherryfa0c21c2015-07-23 17:53:11 -070039 return -EINVAL;
40 }
41 }
42
Tom Cherry96f67312015-07-30 13:52:55 -070043 return func_(expanded_args);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070044}
45
Tom Cherryb7349902015-08-26 11:43:36 -070046std::string Command::BuildCommandString() const {
47 return Join(args_, ' ');
Tom Cherryfa0c21c2015-07-23 17:53:11 -070048}
49
Tom Cherry012c5732017-04-18 13:21:54 -070050Action::Action(bool oneshot, const std::string& filename, int line)
51 : oneshot_(oneshot), filename_(filename), line_(line) {}
Tom Cherryfa0c21c2015-07-23 17:53:11 -070052
Tom Cherryb7349902015-08-26 11:43:36 -070053const KeywordMap<BuiltinFunction>* Action::function_map_ = nullptr;
54
Tom Cherry012c5732017-04-18 13:21:54 -070055bool Action::AddCommand(const std::vector<std::string>& args, int line, std::string* err) {
Tom Cherryb7349902015-08-26 11:43:36 -070056 if (!function_map_) {
57 *err = "no function map available";
58 return false;
59 }
60
61 if (args.empty()) {
62 *err = "command needed, but not provided";
63 return false;
64 }
65
66 auto function = function_map_->FindFunction(args[0], args.size() - 1, err);
67 if (!function) {
68 return false;
69 }
70
Tom Cherry012c5732017-04-18 13:21:54 -070071 AddCommand(function, args, line);
Tom Cherryb7349902015-08-26 11:43:36 -070072 return true;
73}
74
Tom Cherry012c5732017-04-18 13:21:54 -070075void Action::AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line) {
76 commands_.emplace_back(f, args, line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070077}
78
Tom Cherryb7349902015-08-26 11:43:36 -070079std::size_t Action::NumCommands() const {
80 return commands_.size();
81}
82
83void Action::ExecuteOneCommand(std::size_t command) const {
Wei Wangd67a4ab2016-11-16 12:08:30 -080084 // We need a copy here since some Command execution may result in
85 // changing commands_ vector by importing .rc files through parser
86 Command cmd = commands_[command];
87 ExecuteCommand(cmd);
Tom Cherryb7349902015-08-26 11:43:36 -070088}
89
90void Action::ExecuteAllCommands() const {
91 for (const auto& c : commands_) {
92 ExecuteCommand(c);
93 }
94}
95
96void Action::ExecuteCommand(const Command& command) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070097 Timer t;
98 int result = command.InvokeFunc();
99
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000100 double duration_ms = t.duration_s() * 1000;
Wei Wang8b1d5262016-11-15 23:58:55 -0800101 // Any action longer than 50ms will be warned to user as slow operation
102 if (duration_ms > 50.0 ||
103 android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700104 std::string trigger_name = BuildTriggersString();
105 std::string cmd_str = command.BuildCommandString();
Tom Cherry012c5732017-04-18 13:21:54 -0700106 std::string source = StringPrintf(" (%s:%d)", filename_.c_str(), command.line());
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700107
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700108 LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << source
Wei Wang8b1d5262016-11-15 23:58:55 -0800109 << " returned " << result << " took " << duration_ms << "ms.";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700110 }
111}
112
Tom Cherryb7349902015-08-26 11:43:36 -0700113bool Action::ParsePropertyTrigger(const std::string& trigger, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700114 const static std::string prop_str("property:");
115 std::string prop_name(trigger.substr(prop_str.length()));
116 size_t equal_pos = prop_name.find('=');
117 if (equal_pos == std::string::npos) {
118 *err = "property trigger found without matching '='";
119 return false;
120 }
121
122 std::string prop_value(prop_name.substr(equal_pos + 1));
123 prop_name.erase(equal_pos);
124
Tom Cherry2bc00142017-03-13 11:58:58 -0700125 if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700126 *err = "multiple property triggers found for same property";
127 return false;
128 }
129 return true;
130}
131
Tom Cherryb7349902015-08-26 11:43:36 -0700132bool Action::InitTriggers(const std::vector<std::string>& args, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700133 const static std::string prop_str("property:");
134 for (std::size_t i = 0; i < args.size(); ++i) {
Wei Wang93df4e12016-11-16 19:19:49 -0800135 if (args[i].empty()) {
136 *err = "empty trigger is not valid";
137 return false;
138 }
139
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700140 if (i % 2) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700141 if (args[i] != "&&") {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700142 *err = "&& is the only symbol allowed to concatenate actions";
143 return false;
144 } else {
145 continue;
146 }
147 }
148
149 if (!args[i].compare(0, prop_str.length(), prop_str)) {
150 if (!ParsePropertyTrigger(args[i], err)) {
151 return false;
152 }
153 } else {
154 if (!event_trigger_.empty()) {
155 *err = "multiple event triggers are not allowed";
156 return false;
157 }
158
159 event_trigger_ = args[i];
160 }
161 }
162
163 return true;
164}
165
Tom Cherryb7349902015-08-26 11:43:36 -0700166bool Action::InitSingleTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700167 std::vector<std::string> name_vector{trigger};
168 std::string err;
Wei Wang93df4e12016-11-16 19:19:49 -0800169 bool ret = InitTriggers(name_vector, &err);
170 if (!ret) {
171 LOG(ERROR) << "InitSingleTrigger failed due to: " << err;
172 }
173 return ret;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700174}
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 Cherryfa0c21c2015-07-23 17:53:11 -0700183bool Action::CheckPropertyTriggers(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700184 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700185 if (property_triggers_.empty()) {
186 return true;
187 }
188
Tom Cherryb7349902015-08-26 11:43:36 -0700189 bool found = name.empty();
Tom Cherry2bc00142017-03-13 11:58:58 -0700190 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700191 if (trigger_name == name) {
192 if (trigger_value != "*" && trigger_value != value) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700193 return false;
194 } else {
195 found = true;
196 }
197 } else {
Tom Cherryccf23532017-03-28 16:40:41 -0700198 std::string prop_val = android::base::GetProperty(trigger_name, "");
199 if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700200 return false;
201 }
202 }
203 }
204 return found;
205}
206
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700207bool Action::CheckEvent(const EventTrigger& event_trigger) const {
208 return event_trigger == event_trigger_ && CheckPropertyTriggers();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700209}
210
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700211bool Action::CheckEvent(const PropertyChange& property_change) const {
212 const auto& [name, value] = property_change;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700213 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
214}
215
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700216bool Action::CheckEvent(const BuiltinAction& builtin_action) const {
217 return this == builtin_action;
218}
219
Tom Cherryb7349902015-08-26 11:43:36 -0700220std::string Action::BuildTriggersString() const {
Tom Cherryd8a72572017-03-13 12:24:49 -0700221 std::vector<std::string> triggers;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700222
Tom Cherry2bc00142017-03-13 11:58:58 -0700223 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700224 triggers.emplace_back(trigger_name + '=' + trigger_value);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700225 }
226 if (!event_trigger_.empty()) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700227 triggers.emplace_back(event_trigger_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700228 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700229
230 return Join(triggers, " && ");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700231}
232
Tom Cherryb7349902015-08-26 11:43:36 -0700233void Action::DumpState() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700234 std::string trigger_name = BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700235 LOG(INFO) << "on " << trigger_name;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700236
237 for (const auto& c : commands_) {
Tom Cherryb7349902015-08-26 11:43:36 -0700238 std::string cmd_str = c.BuildCommandString();
Tom Cherryd8a72572017-03-13 12:24:49 -0700239 LOG(INFO) << " " << cmd_str;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700240 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700241}
242
Tom Cherryb7349902015-08-26 11:43:36 -0700243ActionManager::ActionManager() : current_command_(0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700244}
245
246ActionManager& ActionManager::GetInstance() {
247 static ActionManager instance;
248 return instance;
249}
250
Tom Cherryb7349902015-08-26 11:43:36 -0700251void ActionManager::AddAction(std::unique_ptr<Action> action) {
Tom Cherry012c5732017-04-18 13:21:54 -0700252 actions_.emplace_back(std::move(action));
Tom Cherryb7349902015-08-26 11:43:36 -0700253}
254
255void ActionManager::QueueEventTrigger(const std::string& trigger) {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700256 event_queue_.emplace(trigger);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700257}
258
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700259void ActionManager::QueuePropertyChange(const std::string& name, const std::string& value) {
260 event_queue_.emplace(std::make_pair(name, value));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700261}
262
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700263void ActionManager::QueueAllPropertyActions() {
264 QueuePropertyChange("", "");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700265}
266
Tom Cherry012c5732017-04-18 13:21:54 -0700267void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) {
268 auto action = std::make_unique<Action>(true, "<Builtin Action>", 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700269 std::vector<std::string> name_vector{name};
270
Tom Cherryb7349902015-08-26 11:43:36 -0700271 if (!action->InitSingleTrigger(name)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700272 return;
273 }
274
Tom Cherry012c5732017-04-18 13:21:54 -0700275 action->AddCommand(func, name_vector, 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700276
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700277 event_queue_.emplace(action.get());
Tom Cherryb7349902015-08-26 11:43:36 -0700278 actions_.emplace_back(std::move(action));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700279}
280
281void ActionManager::ExecuteOneCommand() {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700282 // Loop through the event queue until we have an action to execute
283 while (current_executing_actions_.empty() && !event_queue_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700284 for (const auto& action : actions_) {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700285 if (std::visit([&action](const auto& event) { return action->CheckEvent(event); },
286 event_queue_.front())) {
Tom Cherryb7349902015-08-26 11:43:36 -0700287 current_executing_actions_.emplace(action.get());
288 }
289 }
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700290 event_queue_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700291 }
292
293 if (current_executing_actions_.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700294 return;
295 }
296
Tom Cherryb7349902015-08-26 11:43:36 -0700297 auto action = current_executing_actions_.front();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700298
Tom Cherrycb716f92015-08-11 12:34:22 -0700299 if (current_command_ == 0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700300 std::string trigger_name = action->BuildTriggersString();
Tom Cherry012c5732017-04-18 13:21:54 -0700301 LOG(INFO) << "processing action (" << trigger_name << ") from (" << action->filename()
302 << ":" << action->line() << ")";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700303 }
304
Tom Cherryb7349902015-08-26 11:43:36 -0700305 action->ExecuteOneCommand(current_command_);
306
307 // If this was the last command in the current action, then remove
308 // the action from the executing list.
309 // If this action was oneshot, then also remove it from actions_.
310 ++current_command_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700311 if (current_command_ == action->NumCommands()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700312 current_executing_actions_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700313 current_command_ = 0;
Tom Cherryb7349902015-08-26 11:43:36 -0700314 if (action->oneshot()) {
315 auto eraser = [&action] (std::unique_ptr<Action>& a) {
316 return a.get() == action;
317 };
318 actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
319 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700320 }
321}
322
Tom Cherryb7349902015-08-26 11:43:36 -0700323bool ActionManager::HasMoreCommands() const {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700324 return !current_executing_actions_.empty() || !event_queue_.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700325}
326
Tom Cherryb7349902015-08-26 11:43:36 -0700327void ActionManager::DumpState() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700328 for (const auto& a : actions_) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700329 a->DumpState();
330 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700331}
Tom Cherryb7349902015-08-26 11:43:36 -0700332
Tom Cherry30a6f272017-04-19 15:31:58 -0700333bool ActionParser::ParseSection(std::vector<std::string>&& args, const std::string& filename,
Tom Cherry012c5732017-04-18 13:21:54 -0700334 int line, std::string* err) {
Tom Cherryb7349902015-08-26 11:43:36 -0700335 std::vector<std::string> triggers(args.begin() + 1, args.end());
336 if (triggers.size() < 1) {
337 *err = "actions must have a trigger";
338 return false;
339 }
340
Tom Cherry012c5732017-04-18 13:21:54 -0700341 auto action = std::make_unique<Action>(false, filename, line);
Tom Cherryb7349902015-08-26 11:43:36 -0700342 if (!action->InitTriggers(triggers, err)) {
343 return false;
344 }
345
346 action_ = std::move(action);
347 return true;
348}
349
Tom Cherry30a6f272017-04-19 15:31:58 -0700350bool ActionParser::ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) {
351 return action_ ? action_->AddCommand(std::move(args), line, err) : false;
Tom Cherryb7349902015-08-26 11:43:36 -0700352}
353
354void ActionParser::EndSection() {
355 if (action_ && action_->NumCommands() > 0) {
Tom Cherry30a6f272017-04-19 15:31:58 -0700356 action_manager_->AddAction(std::move(action_));
Tom Cherryb7349902015-08-26 11:43:36 -0700357 }
358}