blob: dd03017ab5809068f1ba57e68afe402ddfab38f2 [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
Tom Cherryfe062052017-04-24 16:59:05 -070061 auto function = function_map_->FindFunction(args, err);
Tom Cherryb7349902015-08-26 11:43:36 -070062 if (!function) {
63 return false;
64 }
65
Tom Cherry012c5732017-04-18 13:21:54 -070066 AddCommand(function, args, line);
Tom Cherryb7349902015-08-26 11:43:36 -070067 return true;
68}
69
Tom Cherry012c5732017-04-18 13:21:54 -070070void Action::AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line) {
71 commands_.emplace_back(f, args, line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070072}
73
Tom Cherryb7349902015-08-26 11:43:36 -070074std::size_t Action::NumCommands() const {
75 return commands_.size();
76}
77
78void Action::ExecuteOneCommand(std::size_t command) const {
Wei Wangd67a4ab2016-11-16 12:08:30 -080079 // We need a copy here since some Command execution may result in
80 // changing commands_ vector by importing .rc files through parser
81 Command cmd = commands_[command];
82 ExecuteCommand(cmd);
Tom Cherryb7349902015-08-26 11:43:36 -070083}
84
85void Action::ExecuteAllCommands() const {
86 for (const auto& c : commands_) {
87 ExecuteCommand(c);
88 }
89}
90
91void Action::ExecuteCommand(const Command& command) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070092 Timer t;
93 int result = command.InvokeFunc();
94
Elliott Hughes331cf2f2016-11-29 19:20:58 +000095 double duration_ms = t.duration_s() * 1000;
Wei Wang8b1d5262016-11-15 23:58:55 -080096 // Any action longer than 50ms will be warned to user as slow operation
97 if (duration_ms > 50.0 ||
98 android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070099 std::string trigger_name = BuildTriggersString();
100 std::string cmd_str = command.BuildCommandString();
Tom Cherry012c5732017-04-18 13:21:54 -0700101 std::string source = StringPrintf(" (%s:%d)", filename_.c_str(), command.line());
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700102
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700103 LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << source
Wei Wang8b1d5262016-11-15 23:58:55 -0800104 << " returned " << result << " took " << duration_ms << "ms.";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700105 }
106}
107
Tom Cherryb7349902015-08-26 11:43:36 -0700108bool Action::ParsePropertyTrigger(const std::string& trigger, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700109 const static std::string prop_str("property:");
110 std::string prop_name(trigger.substr(prop_str.length()));
111 size_t equal_pos = prop_name.find('=');
112 if (equal_pos == std::string::npos) {
113 *err = "property trigger found without matching '='";
114 return false;
115 }
116
117 std::string prop_value(prop_name.substr(equal_pos + 1));
118 prop_name.erase(equal_pos);
119
Tom Cherry2bc00142017-03-13 11:58:58 -0700120 if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700121 *err = "multiple property triggers found for same property";
122 return false;
123 }
124 return true;
125}
126
Tom Cherryb7349902015-08-26 11:43:36 -0700127bool Action::InitTriggers(const std::vector<std::string>& args, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700128 const static std::string prop_str("property:");
129 for (std::size_t i = 0; i < args.size(); ++i) {
Wei Wang93df4e12016-11-16 19:19:49 -0800130 if (args[i].empty()) {
131 *err = "empty trigger is not valid";
132 return false;
133 }
134
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700135 if (i % 2) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700136 if (args[i] != "&&") {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700137 *err = "&& is the only symbol allowed to concatenate actions";
138 return false;
139 } else {
140 continue;
141 }
142 }
143
144 if (!args[i].compare(0, prop_str.length(), prop_str)) {
145 if (!ParsePropertyTrigger(args[i], err)) {
146 return false;
147 }
148 } else {
149 if (!event_trigger_.empty()) {
150 *err = "multiple event triggers are not allowed";
151 return false;
152 }
153
154 event_trigger_ = args[i];
155 }
156 }
157
158 return true;
159}
160
Tom Cherryb7349902015-08-26 11:43:36 -0700161bool Action::InitSingleTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700162 std::vector<std::string> name_vector{trigger};
163 std::string err;
Wei Wang93df4e12016-11-16 19:19:49 -0800164 bool ret = InitTriggers(name_vector, &err);
165 if (!ret) {
166 LOG(ERROR) << "InitSingleTrigger failed due to: " << err;
167 }
168 return ret;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700169}
170
Tom Cherryb7349902015-08-26 11:43:36 -0700171// This function checks that all property triggers are satisfied, that is
172// for each (name, value) in property_triggers_, check that the current
173// value of the property 'name' == value.
174//
175// It takes an optional (name, value) pair, which if provided must
176// be present in property_triggers_; it skips the check of the current
177// property value for this pair.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700178bool Action::CheckPropertyTriggers(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700179 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700180 if (property_triggers_.empty()) {
181 return true;
182 }
183
Tom Cherryb7349902015-08-26 11:43:36 -0700184 bool found = name.empty();
Tom Cherry2bc00142017-03-13 11:58:58 -0700185 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700186 if (trigger_name == name) {
187 if (trigger_value != "*" && trigger_value != value) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700188 return false;
189 } else {
190 found = true;
191 }
192 } else {
Tom Cherryccf23532017-03-28 16:40:41 -0700193 std::string prop_val = android::base::GetProperty(trigger_name, "");
194 if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700195 return false;
196 }
197 }
198 }
199 return found;
200}
201
Tom Cherryb7349902015-08-26 11:43:36 -0700202bool Action::CheckEventTrigger(const std::string& trigger) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700203 return !event_trigger_.empty() &&
Tom Cherrycb716f92015-08-11 12:34:22 -0700204 trigger == event_trigger_ &&
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700205 CheckPropertyTriggers();
206}
207
208bool Action::CheckPropertyTrigger(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700209 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700210 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
211}
212
Tom Cherryb7349902015-08-26 11:43:36 -0700213std::string Action::BuildTriggersString() const {
Tom Cherryd8a72572017-03-13 12:24:49 -0700214 std::vector<std::string> triggers;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700215
Tom Cherry2bc00142017-03-13 11:58:58 -0700216 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700217 triggers.emplace_back(trigger_name + '=' + trigger_value);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700218 }
219 if (!event_trigger_.empty()) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700220 triggers.emplace_back(event_trigger_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700221 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700222
223 return Join(triggers, " && ");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700224}
225
Tom Cherryb7349902015-08-26 11:43:36 -0700226void Action::DumpState() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700227 std::string trigger_name = BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700228 LOG(INFO) << "on " << trigger_name;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700229
230 for (const auto& c : commands_) {
Tom Cherryb7349902015-08-26 11:43:36 -0700231 std::string cmd_str = c.BuildCommandString();
Tom Cherryd8a72572017-03-13 12:24:49 -0700232 LOG(INFO) << " " << cmd_str;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700233 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700234}
235
Tom Cherrycb716f92015-08-11 12:34:22 -0700236class EventTrigger : public Trigger {
237public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700238 explicit EventTrigger(const std::string& trigger) : trigger_(trigger) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700239 }
Tom Cherryb7349902015-08-26 11:43:36 -0700240 bool CheckTriggers(const Action& action) const override {
241 return action.CheckEventTrigger(trigger_);
Tom Cherrycb716f92015-08-11 12:34:22 -0700242 }
243private:
Tom Cherryb7349902015-08-26 11:43:36 -0700244 const std::string trigger_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700245};
246
247class PropertyTrigger : public Trigger {
248public:
249 PropertyTrigger(const std::string& name, const std::string& value)
250 : name_(name), value_(value) {
251 }
Tom Cherryb7349902015-08-26 11:43:36 -0700252 bool CheckTriggers(const Action& action) const override {
253 return action.CheckPropertyTrigger(name_, value_);
Tom Cherrycb716f92015-08-11 12:34:22 -0700254 }
255private:
Tom Cherryb7349902015-08-26 11:43:36 -0700256 const std::string name_;
257 const std::string value_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700258};
259
260class BuiltinTrigger : public Trigger {
261public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700262 explicit BuiltinTrigger(Action* action) : action_(action) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700263 }
Tom Cherryb7349902015-08-26 11:43:36 -0700264 bool CheckTriggers(const Action& action) const override {
265 return action_ == &action;
Tom Cherrycb716f92015-08-11 12:34:22 -0700266 }
267private:
Tom Cherryb7349902015-08-26 11:43:36 -0700268 const Action* action_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700269};
270
Tom Cherryb7349902015-08-26 11:43:36 -0700271ActionManager::ActionManager() : current_command_(0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700272}
273
274ActionManager& ActionManager::GetInstance() {
275 static ActionManager instance;
276 return instance;
277}
278
Tom Cherryb7349902015-08-26 11:43:36 -0700279void ActionManager::AddAction(std::unique_ptr<Action> action) {
Tom Cherry012c5732017-04-18 13:21:54 -0700280 actions_.emplace_back(std::move(action));
Tom Cherryb7349902015-08-26 11:43:36 -0700281}
282
283void ActionManager::QueueEventTrigger(const std::string& trigger) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700284 trigger_queue_.push(std::make_unique<EventTrigger>(trigger));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700285}
286
287void ActionManager::QueuePropertyTrigger(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700288 const std::string& value) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700289 trigger_queue_.push(std::make_unique<PropertyTrigger>(name, value));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700290}
291
Tom Cherryb7349902015-08-26 11:43:36 -0700292void ActionManager::QueueAllPropertyTriggers() {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700293 QueuePropertyTrigger("", "");
294}
295
Tom Cherry012c5732017-04-18 13:21:54 -0700296void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) {
297 auto action = std::make_unique<Action>(true, "<Builtin Action>", 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700298 std::vector<std::string> name_vector{name};
299
Tom Cherryb7349902015-08-26 11:43:36 -0700300 if (!action->InitSingleTrigger(name)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700301 return;
302 }
303
Tom Cherry012c5732017-04-18 13:21:54 -0700304 action->AddCommand(func, name_vector, 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700305
Tom Cherryb7349902015-08-26 11:43:36 -0700306 trigger_queue_.push(std::make_unique<BuiltinTrigger>(action.get()));
307 actions_.emplace_back(std::move(action));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700308}
309
310void ActionManager::ExecuteOneCommand() {
Tom Cherryb7349902015-08-26 11:43:36 -0700311 // Loop through the trigger queue until we have an action to execute
Tom Cherrycb716f92015-08-11 12:34:22 -0700312 while (current_executing_actions_.empty() && !trigger_queue_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700313 for (const auto& action : actions_) {
314 if (trigger_queue_.front()->CheckTriggers(*action)) {
315 current_executing_actions_.emplace(action.get());
316 }
317 }
Tom Cherrycb716f92015-08-11 12:34:22 -0700318 trigger_queue_.pop();
319 }
320
321 if (current_executing_actions_.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700322 return;
323 }
324
Tom Cherryb7349902015-08-26 11:43:36 -0700325 auto action = current_executing_actions_.front();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700326
Tom Cherrycb716f92015-08-11 12:34:22 -0700327 if (current_command_ == 0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700328 std::string trigger_name = action->BuildTriggersString();
Tom Cherry012c5732017-04-18 13:21:54 -0700329 LOG(INFO) << "processing action (" << trigger_name << ") from (" << action->filename()
330 << ":" << action->line() << ")";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700331 }
332
Tom Cherryb7349902015-08-26 11:43:36 -0700333 action->ExecuteOneCommand(current_command_);
334
335 // If this was the last command in the current action, then remove
336 // the action from the executing list.
337 // If this action was oneshot, then also remove it from actions_.
338 ++current_command_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700339 if (current_command_ == action->NumCommands()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700340 current_executing_actions_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700341 current_command_ = 0;
Tom Cherryb7349902015-08-26 11:43:36 -0700342 if (action->oneshot()) {
343 auto eraser = [&action] (std::unique_ptr<Action>& a) {
344 return a.get() == action;
345 };
346 actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
347 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700348 }
349}
350
Tom Cherryb7349902015-08-26 11:43:36 -0700351bool ActionManager::HasMoreCommands() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700352 return !current_executing_actions_.empty() || !trigger_queue_.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700353}
354
Tom Cherryb7349902015-08-26 11:43:36 -0700355void ActionManager::DumpState() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700356 for (const auto& a : actions_) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700357 a->DumpState();
358 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700359}
Tom Cherryb7349902015-08-26 11:43:36 -0700360
Tom Cherry30a6f272017-04-19 15:31:58 -0700361bool ActionParser::ParseSection(std::vector<std::string>&& args, const std::string& filename,
Tom Cherry012c5732017-04-18 13:21:54 -0700362 int line, std::string* err) {
Tom Cherryb7349902015-08-26 11:43:36 -0700363 std::vector<std::string> triggers(args.begin() + 1, args.end());
364 if (triggers.size() < 1) {
365 *err = "actions must have a trigger";
366 return false;
367 }
368
Tom Cherry012c5732017-04-18 13:21:54 -0700369 auto action = std::make_unique<Action>(false, filename, line);
Tom Cherryb7349902015-08-26 11:43:36 -0700370 if (!action->InitTriggers(triggers, err)) {
371 return false;
372 }
373
374 action_ = std::move(action);
375 return true;
376}
377
Tom Cherry30a6f272017-04-19 15:31:58 -0700378bool ActionParser::ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) {
379 return action_ ? action_->AddCommand(std::move(args), line, err) : false;
Tom Cherryb7349902015-08-26 11:43:36 -0700380}
381
382void ActionParser::EndSection() {
383 if (action_ && action_->NumCommands() > 0) {
Tom Cherry30a6f272017-04-19 15:31:58 -0700384 action_manager_->AddAction(std::move(action_));
Tom Cherryb7349902015-08-26 11:43:36 -0700385 }
386}