blob: 206100eceaa12fba39065d633a4eccbf6d130d22 [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>
Tom Cherryccf23532017-03-28 16:40:41 -070021#include <android-base/strings.h>
Tom Cherryfa0c21c2015-07-23 17:53:11 -070022
Tom Cherryfa0c21c2015-07-23 17:53:11 -070023#include "util.h"
24
Tom Cherryb7349902015-08-26 11:43:36 -070025using android::base::Join;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070026
Tom Cherry012c5732017-04-18 13:21:54 -070027Command::Command(BuiltinFunction f, const std::vector<std::string>& args, int line)
28 : func_(f), args_(args), line_(line) {}
Tom Cherryfa0c21c2015-07-23 17:53:11 -070029
Tom Cherryb7349902015-08-26 11:43:36 -070030int Command::InvokeFunc() const {
Tom Cherry96f67312015-07-30 13:52:55 -070031 std::vector<std::string> expanded_args;
32 expanded_args.resize(args_.size());
33 expanded_args[0] = args_[0];
Tom Cherryfa0c21c2015-07-23 17:53:11 -070034 for (std::size_t i = 1; i < args_.size(); ++i) {
Tom Cherryb7349902015-08-26 11:43:36 -070035 if (!expand_props(args_[i], &expanded_args[i])) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070036 LOG(ERROR) << args_[0] << ": cannot expand '" << args_[i] << "'";
Tom Cherryfa0c21c2015-07-23 17:53:11 -070037 return -EINVAL;
38 }
39 }
40
Tom Cherry96f67312015-07-30 13:52:55 -070041 return func_(expanded_args);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070042}
43
Tom Cherryb7349902015-08-26 11:43:36 -070044std::string Command::BuildCommandString() const {
45 return Join(args_, ' ');
Tom Cherryfa0c21c2015-07-23 17:53:11 -070046}
47
Tom Cherry012c5732017-04-18 13:21:54 -070048Action::Action(bool oneshot, const std::string& filename, int line)
49 : oneshot_(oneshot), filename_(filename), line_(line) {}
Tom Cherryfa0c21c2015-07-23 17:53:11 -070050
Tom Cherryb7349902015-08-26 11:43:36 -070051const KeywordMap<BuiltinFunction>* Action::function_map_ = nullptr;
52
Tom Cherry012c5732017-04-18 13:21:54 -070053bool Action::AddCommand(const std::vector<std::string>& args, int line, std::string* err) {
Tom Cherryb7349902015-08-26 11:43:36 -070054 if (!function_map_) {
55 *err = "no function map available";
56 return false;
57 }
58
Tom Cherryfe062052017-04-24 16:59:05 -070059 auto function = function_map_->FindFunction(args, err);
Tom Cherryb7349902015-08-26 11:43:36 -070060 if (!function) {
61 return false;
62 }
63
Tom Cherry012c5732017-04-18 13:21:54 -070064 AddCommand(function, args, line);
Tom Cherryb7349902015-08-26 11:43:36 -070065 return true;
66}
67
Tom Cherry012c5732017-04-18 13:21:54 -070068void Action::AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line) {
69 commands_.emplace_back(f, args, line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070070}
71
Tom Cherryb7349902015-08-26 11:43:36 -070072std::size_t Action::NumCommands() const {
73 return commands_.size();
74}
75
76void Action::ExecuteOneCommand(std::size_t command) const {
Wei Wangd67a4ab2016-11-16 12:08:30 -080077 // We need a copy here since some Command execution may result in
78 // changing commands_ vector by importing .rc files through parser
79 Command cmd = commands_[command];
80 ExecuteCommand(cmd);
Tom Cherryb7349902015-08-26 11:43:36 -070081}
82
83void Action::ExecuteAllCommands() const {
84 for (const auto& c : commands_) {
85 ExecuteCommand(c);
86 }
87}
88
89void Action::ExecuteCommand(const Command& command) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070090 Timer t;
91 int result = command.InvokeFunc();
92
Elliott Hughes331cf2f2016-11-29 19:20:58 +000093 double duration_ms = t.duration_s() * 1000;
Wei Wang8b1d5262016-11-15 23:58:55 -080094 // Any action longer than 50ms will be warned to user as slow operation
95 if (duration_ms > 50.0 ||
96 android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070097 std::string trigger_name = BuildTriggersString();
98 std::string cmd_str = command.BuildCommandString();
Tom Cherryfa0c21c2015-07-23 17:53:11 -070099
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700100 LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_
101 << ":" << command.line() << ") returned " << result << " took " << duration_ms
102 << "ms.";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700103 }
104}
105
Tom Cherryb7349902015-08-26 11:43:36 -0700106bool Action::ParsePropertyTrigger(const std::string& trigger, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700107 const static std::string prop_str("property:");
108 std::string prop_name(trigger.substr(prop_str.length()));
109 size_t equal_pos = prop_name.find('=');
110 if (equal_pos == std::string::npos) {
111 *err = "property trigger found without matching '='";
112 return false;
113 }
114
115 std::string prop_value(prop_name.substr(equal_pos + 1));
116 prop_name.erase(equal_pos);
117
Tom Cherry2bc00142017-03-13 11:58:58 -0700118 if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700119 *err = "multiple property triggers found for same property";
120 return false;
121 }
122 return true;
123}
124
Tom Cherryb7349902015-08-26 11:43:36 -0700125bool Action::InitTriggers(const std::vector<std::string>& args, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700126 const static std::string prop_str("property:");
127 for (std::size_t i = 0; i < args.size(); ++i) {
Wei Wang93df4e12016-11-16 19:19:49 -0800128 if (args[i].empty()) {
129 *err = "empty trigger is not valid";
130 return false;
131 }
132
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700133 if (i % 2) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700134 if (args[i] != "&&") {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700135 *err = "&& is the only symbol allowed to concatenate actions";
136 return false;
137 } else {
138 continue;
139 }
140 }
141
142 if (!args[i].compare(0, prop_str.length(), prop_str)) {
143 if (!ParsePropertyTrigger(args[i], err)) {
144 return false;
145 }
146 } else {
147 if (!event_trigger_.empty()) {
148 *err = "multiple event triggers are not allowed";
149 return false;
150 }
151
152 event_trigger_ = args[i];
153 }
154 }
155
156 return true;
157}
158
Tom Cherryb7349902015-08-26 11:43:36 -0700159bool Action::InitSingleTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700160 std::vector<std::string> name_vector{trigger};
161 std::string err;
Wei Wang93df4e12016-11-16 19:19:49 -0800162 bool ret = InitTriggers(name_vector, &err);
163 if (!ret) {
164 LOG(ERROR) << "InitSingleTrigger failed due to: " << err;
165 }
166 return ret;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700167}
168
Tom Cherryb7349902015-08-26 11:43:36 -0700169// This function checks that all property triggers are satisfied, that is
170// for each (name, value) in property_triggers_, check that the current
171// value of the property 'name' == value.
172//
173// It takes an optional (name, value) pair, which if provided must
174// be present in property_triggers_; it skips the check of the current
175// property value for this pair.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700176bool Action::CheckPropertyTriggers(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700177 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700178 if (property_triggers_.empty()) {
179 return true;
180 }
181
Tom Cherryb7349902015-08-26 11:43:36 -0700182 bool found = name.empty();
Tom Cherry2bc00142017-03-13 11:58:58 -0700183 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700184 if (trigger_name == name) {
185 if (trigger_value != "*" && trigger_value != value) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700186 return false;
187 } else {
188 found = true;
189 }
190 } else {
Tom Cherryccf23532017-03-28 16:40:41 -0700191 std::string prop_val = android::base::GetProperty(trigger_name, "");
192 if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700193 return false;
194 }
195 }
196 }
197 return found;
198}
199
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700200bool Action::CheckEvent(const EventTrigger& event_trigger) const {
201 return event_trigger == event_trigger_ && CheckPropertyTriggers();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700202}
203
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700204bool Action::CheckEvent(const PropertyChange& property_change) const {
205 const auto& [name, value] = property_change;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700206 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
207}
208
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700209bool Action::CheckEvent(const BuiltinAction& builtin_action) const {
210 return this == builtin_action;
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 Cherryb7349902015-08-26 11:43:36 -0700236ActionManager::ActionManager() : current_command_(0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700237}
238
239ActionManager& ActionManager::GetInstance() {
240 static ActionManager instance;
241 return instance;
242}
243
Tom Cherryb7349902015-08-26 11:43:36 -0700244void ActionManager::AddAction(std::unique_ptr<Action> action) {
Tom Cherry012c5732017-04-18 13:21:54 -0700245 actions_.emplace_back(std::move(action));
Tom Cherryb7349902015-08-26 11:43:36 -0700246}
247
248void ActionManager::QueueEventTrigger(const std::string& trigger) {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700249 event_queue_.emplace(trigger);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700250}
251
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700252void ActionManager::QueuePropertyChange(const std::string& name, const std::string& value) {
253 event_queue_.emplace(std::make_pair(name, value));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700254}
255
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700256void ActionManager::QueueAllPropertyActions() {
257 QueuePropertyChange("", "");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700258}
259
Tom Cherry012c5732017-04-18 13:21:54 -0700260void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) {
261 auto action = std::make_unique<Action>(true, "<Builtin Action>", 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700262 std::vector<std::string> name_vector{name};
263
Tom Cherryb7349902015-08-26 11:43:36 -0700264 if (!action->InitSingleTrigger(name)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700265 return;
266 }
267
Tom Cherry012c5732017-04-18 13:21:54 -0700268 action->AddCommand(func, name_vector, 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700269
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700270 event_queue_.emplace(action.get());
Tom Cherryb7349902015-08-26 11:43:36 -0700271 actions_.emplace_back(std::move(action));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700272}
273
274void ActionManager::ExecuteOneCommand() {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700275 // Loop through the event queue until we have an action to execute
276 while (current_executing_actions_.empty() && !event_queue_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700277 for (const auto& action : actions_) {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700278 if (std::visit([&action](const auto& event) { return action->CheckEvent(event); },
279 event_queue_.front())) {
Tom Cherryb7349902015-08-26 11:43:36 -0700280 current_executing_actions_.emplace(action.get());
281 }
282 }
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700283 event_queue_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700284 }
285
286 if (current_executing_actions_.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700287 return;
288 }
289
Tom Cherryb7349902015-08-26 11:43:36 -0700290 auto action = current_executing_actions_.front();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700291
Tom Cherrycb716f92015-08-11 12:34:22 -0700292 if (current_command_ == 0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700293 std::string trigger_name = action->BuildTriggersString();
Tom Cherry012c5732017-04-18 13:21:54 -0700294 LOG(INFO) << "processing action (" << trigger_name << ") from (" << action->filename()
295 << ":" << action->line() << ")";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700296 }
297
Tom Cherryb7349902015-08-26 11:43:36 -0700298 action->ExecuteOneCommand(current_command_);
299
300 // If this was the last command in the current action, then remove
301 // the action from the executing list.
302 // If this action was oneshot, then also remove it from actions_.
303 ++current_command_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700304 if (current_command_ == action->NumCommands()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700305 current_executing_actions_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700306 current_command_ = 0;
Tom Cherryb7349902015-08-26 11:43:36 -0700307 if (action->oneshot()) {
308 auto eraser = [&action] (std::unique_ptr<Action>& a) {
309 return a.get() == action;
310 };
311 actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
312 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700313 }
314}
315
Tom Cherryb7349902015-08-26 11:43:36 -0700316bool ActionManager::HasMoreCommands() const {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700317 return !current_executing_actions_.empty() || !event_queue_.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700318}
319
Tom Cherryb7349902015-08-26 11:43:36 -0700320void ActionManager::DumpState() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700321 for (const auto& a : actions_) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700322 a->DumpState();
323 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700324}
Tom Cherryb7349902015-08-26 11:43:36 -0700325
Tom Cherry30a6f272017-04-19 15:31:58 -0700326bool ActionParser::ParseSection(std::vector<std::string>&& args, const std::string& filename,
Tom Cherry012c5732017-04-18 13:21:54 -0700327 int line, std::string* err) {
Tom Cherryb7349902015-08-26 11:43:36 -0700328 std::vector<std::string> triggers(args.begin() + 1, args.end());
329 if (triggers.size() < 1) {
330 *err = "actions must have a trigger";
331 return false;
332 }
333
Tom Cherry012c5732017-04-18 13:21:54 -0700334 auto action = std::make_unique<Action>(false, filename, line);
Tom Cherryb7349902015-08-26 11:43:36 -0700335 if (!action->InitTriggers(triggers, err)) {
336 return false;
337 }
338
339 action_ = std::move(action);
340 return true;
341}
342
Tom Cherry30a6f272017-04-19 15:31:58 -0700343bool ActionParser::ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) {
344 return action_ ? action_->AddCommand(std::move(args), line, err) : false;
Tom Cherryb7349902015-08-26 11:43:36 -0700345}
346
347void ActionParser::EndSection() {
348 if (action_ && action_->NumCommands() > 0) {
Tom Cherry30a6f272017-04-19 15:31:58 -0700349 action_manager_->AddAction(std::move(action_));
Tom Cherryb7349902015-08-26 11:43:36 -0700350 }
351}