blob: 347edb054f4ceb8c5cc2b8f101d9a4e39a4d0146 [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 Cherryb7349902015-08-26 11:43:36 -070029Command::Command(BuiltinFunction f, const std::vector<std::string>& args,
30 const std::string& filename, int line)
31 : func_(f), args_(args), filename_(filename), line_(line) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070032}
33
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 Cherryb7349902015-08-26 11:43:36 -070052std::string Command::BuildSourceString() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070053 if (!filename_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -070054 return StringPrintf(" (%s:%d)", filename_.c_str(), line_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070055 } else {
56 return std::string();
57 }
58}
59
Tom Cherryb7349902015-08-26 11:43:36 -070060Action::Action(bool oneshot) : oneshot_(oneshot) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070061}
62
Tom Cherryb7349902015-08-26 11:43:36 -070063const KeywordMap<BuiltinFunction>* Action::function_map_ = nullptr;
64
65bool Action::AddCommand(const std::vector<std::string>& args,
66 const std::string& filename, int line, std::string* err) {
67 if (!function_map_) {
68 *err = "no function map available";
69 return false;
70 }
71
72 if (args.empty()) {
73 *err = "command needed, but not provided";
74 return false;
75 }
76
77 auto function = function_map_->FindFunction(args[0], args.size() - 1, err);
78 if (!function) {
79 return false;
80 }
81
82 AddCommand(function, args, filename, line);
83 return true;
84}
85
86void Action::AddCommand(BuiltinFunction f,
Tom Cherryfa0c21c2015-07-23 17:53:11 -070087 const std::vector<std::string>& args,
Tom Cherryb7349902015-08-26 11:43:36 -070088 const std::string& filename, int line) {
89 commands_.emplace_back(f, args, filename, line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070090}
91
Tom Cherryb7349902015-08-26 11:43:36 -070092void Action::CombineAction(const Action& action) {
93 for (const auto& c : action.commands_) {
94 commands_.emplace_back(c);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070095 }
96}
97
Tom Cherryb7349902015-08-26 11:43:36 -070098std::size_t Action::NumCommands() const {
99 return commands_.size();
100}
101
102void Action::ExecuteOneCommand(std::size_t command) const {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800103 // We need a copy here since some Command execution may result in
104 // changing commands_ vector by importing .rc files through parser
105 Command cmd = commands_[command];
106 ExecuteCommand(cmd);
Tom Cherryb7349902015-08-26 11:43:36 -0700107}
108
109void Action::ExecuteAllCommands() const {
110 for (const auto& c : commands_) {
111 ExecuteCommand(c);
112 }
113}
114
115void Action::ExecuteCommand(const Command& command) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700116 Timer t;
117 int result = command.InvokeFunc();
118
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000119 double duration_ms = t.duration_s() * 1000;
Wei Wang8b1d5262016-11-15 23:58:55 -0800120 // Any action longer than 50ms will be warned to user as slow operation
121 if (duration_ms > 50.0 ||
122 android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700123 std::string trigger_name = BuildTriggersString();
124 std::string cmd_str = command.BuildCommandString();
125 std::string source = command.BuildSourceString();
126
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700127 LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << source
Wei Wang8b1d5262016-11-15 23:58:55 -0800128 << " returned " << result << " took " << duration_ms << "ms.";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700129 }
130}
131
Tom Cherryb7349902015-08-26 11:43:36 -0700132bool Action::ParsePropertyTrigger(const std::string& trigger, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700133 const static std::string prop_str("property:");
134 std::string prop_name(trigger.substr(prop_str.length()));
135 size_t equal_pos = prop_name.find('=');
136 if (equal_pos == std::string::npos) {
137 *err = "property trigger found without matching '='";
138 return false;
139 }
140
141 std::string prop_value(prop_name.substr(equal_pos + 1));
142 prop_name.erase(equal_pos);
143
Tom Cherry2bc00142017-03-13 11:58:58 -0700144 if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700145 *err = "multiple property triggers found for same property";
146 return false;
147 }
148 return true;
149}
150
Tom Cherryb7349902015-08-26 11:43:36 -0700151bool Action::InitTriggers(const std::vector<std::string>& args, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700152 const static std::string prop_str("property:");
153 for (std::size_t i = 0; i < args.size(); ++i) {
Wei Wang93df4e12016-11-16 19:19:49 -0800154 if (args[i].empty()) {
155 *err = "empty trigger is not valid";
156 return false;
157 }
158
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700159 if (i % 2) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700160 if (args[i] != "&&") {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700161 *err = "&& is the only symbol allowed to concatenate actions";
162 return false;
163 } else {
164 continue;
165 }
166 }
167
168 if (!args[i].compare(0, prop_str.length(), prop_str)) {
169 if (!ParsePropertyTrigger(args[i], err)) {
170 return false;
171 }
172 } else {
173 if (!event_trigger_.empty()) {
174 *err = "multiple event triggers are not allowed";
175 return false;
176 }
177
178 event_trigger_ = args[i];
179 }
180 }
181
182 return true;
183}
184
Tom Cherryb7349902015-08-26 11:43:36 -0700185bool Action::InitSingleTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700186 std::vector<std::string> name_vector{trigger};
187 std::string err;
Wei Wang93df4e12016-11-16 19:19:49 -0800188 bool ret = InitTriggers(name_vector, &err);
189 if (!ret) {
190 LOG(ERROR) << "InitSingleTrigger failed due to: " << err;
191 }
192 return ret;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700193}
194
Tom Cherryb7349902015-08-26 11:43:36 -0700195// This function checks that all property triggers are satisfied, that is
196// for each (name, value) in property_triggers_, check that the current
197// value of the property 'name' == value.
198//
199// It takes an optional (name, value) pair, which if provided must
200// be present in property_triggers_; it skips the check of the current
201// property value for this pair.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700202bool Action::CheckPropertyTriggers(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700203 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700204 if (property_triggers_.empty()) {
205 return true;
206 }
207
Tom Cherryb7349902015-08-26 11:43:36 -0700208 bool found = name.empty();
Tom Cherry2bc00142017-03-13 11:58:58 -0700209 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700210 if (trigger_name == name) {
211 if (trigger_value != "*" && trigger_value != value) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700212 return false;
213 } else {
214 found = true;
215 }
216 } else {
Tom Cherryccf23532017-03-28 16:40:41 -0700217 std::string prop_val = android::base::GetProperty(trigger_name, "");
218 if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700219 return false;
220 }
221 }
222 }
223 return found;
224}
225
Tom Cherryb7349902015-08-26 11:43:36 -0700226bool Action::CheckEventTrigger(const std::string& trigger) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700227 return !event_trigger_.empty() &&
Tom Cherrycb716f92015-08-11 12:34:22 -0700228 trigger == event_trigger_ &&
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700229 CheckPropertyTriggers();
230}
231
232bool Action::CheckPropertyTrigger(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700233 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700234 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
235}
236
Tom Cherryb7349902015-08-26 11:43:36 -0700237bool Action::TriggersEqual(const Action& other) const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700238 return property_triggers_ == other.property_triggers_ &&
239 event_trigger_ == other.event_trigger_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700240}
241
Tom Cherryb7349902015-08-26 11:43:36 -0700242std::string Action::BuildTriggersString() const {
Tom Cherryd8a72572017-03-13 12:24:49 -0700243 std::vector<std::string> triggers;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700244
Tom Cherry2bc00142017-03-13 11:58:58 -0700245 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700246 triggers.emplace_back(trigger_name + '=' + trigger_value);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700247 }
248 if (!event_trigger_.empty()) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700249 triggers.emplace_back(event_trigger_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700250 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700251
252 return Join(triggers, " && ");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700253}
254
Tom Cherryb7349902015-08-26 11:43:36 -0700255void Action::DumpState() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700256 std::string trigger_name = BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700257 LOG(INFO) << "on " << trigger_name;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700258
259 for (const auto& c : commands_) {
Tom Cherryb7349902015-08-26 11:43:36 -0700260 std::string cmd_str = c.BuildCommandString();
Tom Cherryd8a72572017-03-13 12:24:49 -0700261 LOG(INFO) << " " << cmd_str;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700262 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700263}
264
Tom Cherrycb716f92015-08-11 12:34:22 -0700265class EventTrigger : public Trigger {
266public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700267 explicit EventTrigger(const std::string& trigger) : trigger_(trigger) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700268 }
Tom Cherryb7349902015-08-26 11:43:36 -0700269 bool CheckTriggers(const Action& action) const override {
270 return action.CheckEventTrigger(trigger_);
Tom Cherrycb716f92015-08-11 12:34:22 -0700271 }
272private:
Tom Cherryb7349902015-08-26 11:43:36 -0700273 const std::string trigger_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700274};
275
276class PropertyTrigger : public Trigger {
277public:
278 PropertyTrigger(const std::string& name, const std::string& value)
279 : name_(name), value_(value) {
280 }
Tom Cherryb7349902015-08-26 11:43:36 -0700281 bool CheckTriggers(const Action& action) const override {
282 return action.CheckPropertyTrigger(name_, value_);
Tom Cherrycb716f92015-08-11 12:34:22 -0700283 }
284private:
Tom Cherryb7349902015-08-26 11:43:36 -0700285 const std::string name_;
286 const std::string value_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700287};
288
289class BuiltinTrigger : public Trigger {
290public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700291 explicit BuiltinTrigger(Action* action) : action_(action) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700292 }
Tom Cherryb7349902015-08-26 11:43:36 -0700293 bool CheckTriggers(const Action& action) const override {
294 return action_ == &action;
Tom Cherrycb716f92015-08-11 12:34:22 -0700295 }
296private:
Tom Cherryb7349902015-08-26 11:43:36 -0700297 const Action* action_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700298};
299
Tom Cherryb7349902015-08-26 11:43:36 -0700300ActionManager::ActionManager() : current_command_(0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700301}
302
303ActionManager& ActionManager::GetInstance() {
304 static ActionManager instance;
305 return instance;
306}
307
Tom Cherryb7349902015-08-26 11:43:36 -0700308void ActionManager::AddAction(std::unique_ptr<Action> action) {
309 auto old_action_it =
310 std::find_if(actions_.begin(), actions_.end(),
311 [&action] (std::unique_ptr<Action>& a) {
312 return action->TriggersEqual(*a);
313 });
314
315 if (old_action_it != actions_.end()) {
316 (*old_action_it)->CombineAction(*action);
317 } else {
318 actions_.emplace_back(std::move(action));
319 }
320}
321
322void ActionManager::QueueEventTrigger(const std::string& trigger) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700323 trigger_queue_.push(std::make_unique<EventTrigger>(trigger));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700324}
325
326void ActionManager::QueuePropertyTrigger(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700327 const std::string& value) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700328 trigger_queue_.push(std::make_unique<PropertyTrigger>(name, value));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700329}
330
Tom Cherryb7349902015-08-26 11:43:36 -0700331void ActionManager::QueueAllPropertyTriggers() {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700332 QueuePropertyTrigger("", "");
333}
334
Tom Cherryb7349902015-08-26 11:43:36 -0700335void ActionManager::QueueBuiltinAction(BuiltinFunction func,
336 const std::string& name) {
337 auto action = std::make_unique<Action>(true);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700338 std::vector<std::string> name_vector{name};
339
Tom Cherryb7349902015-08-26 11:43:36 -0700340 if (!action->InitSingleTrigger(name)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700341 return;
342 }
343
Tom Cherryb7349902015-08-26 11:43:36 -0700344 action->AddCommand(func, name_vector);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700345
Tom Cherryb7349902015-08-26 11:43:36 -0700346 trigger_queue_.push(std::make_unique<BuiltinTrigger>(action.get()));
347 actions_.emplace_back(std::move(action));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700348}
349
350void ActionManager::ExecuteOneCommand() {
Tom Cherryb7349902015-08-26 11:43:36 -0700351 // Loop through the trigger queue until we have an action to execute
Tom Cherrycb716f92015-08-11 12:34:22 -0700352 while (current_executing_actions_.empty() && !trigger_queue_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700353 for (const auto& action : actions_) {
354 if (trigger_queue_.front()->CheckTriggers(*action)) {
355 current_executing_actions_.emplace(action.get());
356 }
357 }
Tom Cherrycb716f92015-08-11 12:34:22 -0700358 trigger_queue_.pop();
359 }
360
361 if (current_executing_actions_.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700362 return;
363 }
364
Tom Cherryb7349902015-08-26 11:43:36 -0700365 auto action = current_executing_actions_.front();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700366
Tom Cherrycb716f92015-08-11 12:34:22 -0700367 if (current_command_ == 0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700368 std::string trigger_name = action->BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700369 LOG(INFO) << "processing action (" << trigger_name << ")";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700370 }
371
Tom Cherryb7349902015-08-26 11:43:36 -0700372 action->ExecuteOneCommand(current_command_);
373
374 // If this was the last command in the current action, then remove
375 // the action from the executing list.
376 // If this action was oneshot, then also remove it from actions_.
377 ++current_command_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700378 if (current_command_ == action->NumCommands()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700379 current_executing_actions_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700380 current_command_ = 0;
Tom Cherryb7349902015-08-26 11:43:36 -0700381 if (action->oneshot()) {
382 auto eraser = [&action] (std::unique_ptr<Action>& a) {
383 return a.get() == action;
384 };
385 actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
386 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700387 }
388}
389
Tom Cherryb7349902015-08-26 11:43:36 -0700390bool ActionManager::HasMoreCommands() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700391 return !current_executing_actions_.empty() || !trigger_queue_.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700392}
393
Tom Cherryb7349902015-08-26 11:43:36 -0700394void ActionManager::DumpState() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700395 for (const auto& a : actions_) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700396 a->DumpState();
397 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700398}
Tom Cherryb7349902015-08-26 11:43:36 -0700399
400bool ActionParser::ParseSection(const std::vector<std::string>& args,
401 std::string* err) {
402 std::vector<std::string> triggers(args.begin() + 1, args.end());
403 if (triggers.size() < 1) {
404 *err = "actions must have a trigger";
405 return false;
406 }
407
408 auto action = std::make_unique<Action>(false);
409 if (!action->InitTriggers(triggers, err)) {
410 return false;
411 }
412
413 action_ = std::move(action);
414 return true;
415}
416
417bool ActionParser::ParseLineSection(const std::vector<std::string>& args,
418 const std::string& filename, int line,
419 std::string* err) const {
420 return action_ ? action_->AddCommand(args, filename, line, err) : false;
421}
422
423void ActionParser::EndSection() {
424 if (action_ && action_->NumCommands() > 0) {
425 ActionManager::GetInstance().AddAction(std::move(action_));
426 }
427}