blob: a7aa7567a7ce76a2e97e20b75cee23bd85ce59ac [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 Cherry557946e2017-08-01 13:50:23 -070034Result<Success> 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])) {
Tom Cherry557946e2017-08-01 13:50:23 -070040 return Error() << "cannot expand '" << args_[i] << "'";
Tom Cherryfa0c21c2015-07-23 17:53:11 -070041 }
42 }
43
Tom Cherry96f67312015-07-30 13:52:55 -070044 return func_(expanded_args);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070045}
46
Tom Cherryb7349902015-08-26 11:43:36 -070047std::string Command::BuildCommandString() const {
48 return Join(args_, ' ');
Tom Cherryfa0c21c2015-07-23 17:53:11 -070049}
50
Tom Cherry012c5732017-04-18 13:21:54 -070051Action::Action(bool oneshot, const std::string& filename, int line)
52 : oneshot_(oneshot), filename_(filename), line_(line) {}
Tom Cherryfa0c21c2015-07-23 17:53:11 -070053
Tom Cherryb7349902015-08-26 11:43:36 -070054const KeywordMap<BuiltinFunction>* Action::function_map_ = nullptr;
55
Tom Cherry89bcc852017-08-02 17:01:36 -070056Result<Success> Action::AddCommand(const std::vector<std::string>& args, int line) {
Tom Cherryb7349902015-08-26 11:43:36 -070057 if (!function_map_) {
Tom Cherry89bcc852017-08-02 17:01:36 -070058 return Error() << "no function map available";
Tom Cherryb7349902015-08-26 11:43:36 -070059 }
60
Tom Cherry89bcc852017-08-02 17:01:36 -070061 auto function = function_map_->FindFunction(args);
62 if (!function) return Error() << function.error();
Tom Cherryb7349902015-08-26 11:43:36 -070063
Tom Cherry89bcc852017-08-02 17:01:36 -070064 AddCommand(*function, args, line);
65 return Success();
Tom Cherryb7349902015-08-26 11:43:36 -070066}
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 Cherryede0d532017-07-06 14:20:11 -070090 android::base::Timer t;
Tom Cherry557946e2017-08-01 13:50:23 -070091 auto result = command.InvokeFunc();
Tom Cherryede0d532017-07-06 14:20:11 -070092 auto duration = t.duration();
Tom Cherry557946e2017-08-01 13:50:23 -070093
Wei Wang8b1d5262016-11-15 23:58:55 -080094 // Any action longer than 50ms will be warned to user as slow operation
Tom Cherryede0d532017-07-06 14:20:11 -070095 if (duration > 50ms || android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070096 std::string trigger_name = BuildTriggersString();
97 std::string cmd_str = command.BuildCommandString();
Tom Cherryfa0c21c2015-07-23 17:53:11 -070098
Tom Cherry1c3a53f2017-06-22 16:50:31 -070099 LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_
Tom Cherry557946e2017-08-01 13:50:23 -0700100 << ":" << command.line() << ") took " << duration.count() << "ms and "
Tom Cherry130e3d72017-08-22 16:07:15 -0700101 << (result ? "succeeded" : "failed: " + result.error_string());
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700102 }
103}
104
Tom Cherry89bcc852017-08-02 17:01:36 -0700105Result<Success> Action::ParsePropertyTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700106 const static std::string prop_str("property:");
107 std::string prop_name(trigger.substr(prop_str.length()));
108 size_t equal_pos = prop_name.find('=');
109 if (equal_pos == std::string::npos) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700110 return Error() << "property trigger found without matching '='";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700111 }
112
113 std::string prop_value(prop_name.substr(equal_pos + 1));
114 prop_name.erase(equal_pos);
115
Tom Cherry2bc00142017-03-13 11:58:58 -0700116 if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700117 return Error() << "multiple property triggers found for same property";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700118 }
Tom Cherry89bcc852017-08-02 17:01:36 -0700119 return Success();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700120}
121
Tom Cherry89bcc852017-08-02 17:01:36 -0700122Result<Success> Action::InitTriggers(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700123 const static std::string prop_str("property:");
124 for (std::size_t i = 0; i < args.size(); ++i) {
Wei Wang93df4e12016-11-16 19:19:49 -0800125 if (args[i].empty()) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700126 return Error() << "empty trigger is not valid";
Wei Wang93df4e12016-11-16 19:19:49 -0800127 }
128
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700129 if (i % 2) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700130 if (args[i] != "&&") {
Tom Cherry89bcc852017-08-02 17:01:36 -0700131 return Error() << "&& is the only symbol allowed to concatenate actions";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700132 } else {
133 continue;
134 }
135 }
136
137 if (!args[i].compare(0, prop_str.length(), prop_str)) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700138 if (auto result = ParsePropertyTrigger(args[i]); !result) {
139 return result;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700140 }
141 } else {
142 if (!event_trigger_.empty()) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700143 return Error() << "multiple event triggers are not allowed";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700144 }
145
146 event_trigger_ = args[i];
147 }
148 }
149
Tom Cherry89bcc852017-08-02 17:01:36 -0700150 return Success();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700151}
152
Tom Cherry89bcc852017-08-02 17:01:36 -0700153Result<Success> Action::InitSingleTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700154 std::vector<std::string> name_vector{trigger};
Tom Cherry89bcc852017-08-02 17:01:36 -0700155 if (auto result = InitTriggers(name_vector); !result) {
156 return Error() << "InitTriggers() failed: " << result.error();
Wei Wang93df4e12016-11-16 19:19:49 -0800157 }
Tom Cherry89bcc852017-08-02 17:01:36 -0700158 return Success();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700159}
160
Tom Cherryb7349902015-08-26 11:43:36 -0700161// This function checks that all property triggers are satisfied, that is
162// for each (name, value) in property_triggers_, check that the current
163// value of the property 'name' == value.
164//
165// It takes an optional (name, value) pair, which if provided must
166// be present in property_triggers_; it skips the check of the current
167// property value for this pair.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700168bool Action::CheckPropertyTriggers(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700169 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700170 if (property_triggers_.empty()) {
171 return true;
172 }
173
Tom Cherryb7349902015-08-26 11:43:36 -0700174 bool found = name.empty();
Tom Cherry2bc00142017-03-13 11:58:58 -0700175 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700176 if (trigger_name == name) {
177 if (trigger_value != "*" && trigger_value != value) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700178 return false;
179 } else {
180 found = true;
181 }
182 } else {
Tom Cherryccf23532017-03-28 16:40:41 -0700183 std::string prop_val = android::base::GetProperty(trigger_name, "");
184 if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700185 return false;
186 }
187 }
188 }
189 return found;
190}
191
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700192bool Action::CheckEvent(const EventTrigger& event_trigger) const {
193 return event_trigger == event_trigger_ && CheckPropertyTriggers();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700194}
195
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700196bool Action::CheckEvent(const PropertyChange& property_change) const {
197 const auto& [name, value] = property_change;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700198 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
199}
200
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700201bool Action::CheckEvent(const BuiltinAction& builtin_action) const {
202 return this == builtin_action;
203}
204
Tom Cherryb7349902015-08-26 11:43:36 -0700205std::string Action::BuildTriggersString() const {
Tom Cherryd8a72572017-03-13 12:24:49 -0700206 std::vector<std::string> triggers;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700207
Tom Cherry2bc00142017-03-13 11:58:58 -0700208 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700209 triggers.emplace_back(trigger_name + '=' + trigger_value);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700210 }
211 if (!event_trigger_.empty()) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700212 triggers.emplace_back(event_trigger_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700213 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700214
215 return Join(triggers, " && ");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700216}
217
Tom Cherryb7349902015-08-26 11:43:36 -0700218void Action::DumpState() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700219 std::string trigger_name = BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700220 LOG(INFO) << "on " << trigger_name;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700221
222 for (const auto& c : commands_) {
Tom Cherryb7349902015-08-26 11:43:36 -0700223 std::string cmd_str = c.BuildCommandString();
Tom Cherryd8a72572017-03-13 12:24:49 -0700224 LOG(INFO) << " " << cmd_str;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700225 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700226}
227
Tom Cherryb7349902015-08-26 11:43:36 -0700228ActionManager::ActionManager() : current_command_(0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700229}
230
231ActionManager& ActionManager::GetInstance() {
232 static ActionManager instance;
233 return instance;
234}
235
Tom Cherryb7349902015-08-26 11:43:36 -0700236void ActionManager::AddAction(std::unique_ptr<Action> action) {
Tom Cherry012c5732017-04-18 13:21:54 -0700237 actions_.emplace_back(std::move(action));
Tom Cherryb7349902015-08-26 11:43:36 -0700238}
239
240void ActionManager::QueueEventTrigger(const std::string& trigger) {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700241 event_queue_.emplace(trigger);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700242}
243
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700244void ActionManager::QueuePropertyChange(const std::string& name, const std::string& value) {
245 event_queue_.emplace(std::make_pair(name, value));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700246}
247
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700248void ActionManager::QueueAllPropertyActions() {
249 QueuePropertyChange("", "");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700250}
251
Tom Cherry012c5732017-04-18 13:21:54 -0700252void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) {
253 auto action = std::make_unique<Action>(true, "<Builtin Action>", 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700254 std::vector<std::string> name_vector{name};
255
Tom Cherry89bcc852017-08-02 17:01:36 -0700256 if (auto result = action->InitSingleTrigger(name); !result) {
257 LOG(ERROR) << "Cannot queue BuiltinAction for " << name << ": " << result.error();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700258 return;
259 }
260
Tom Cherry012c5732017-04-18 13:21:54 -0700261 action->AddCommand(func, name_vector, 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700262
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700263 event_queue_.emplace(action.get());
Tom Cherryb7349902015-08-26 11:43:36 -0700264 actions_.emplace_back(std::move(action));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700265}
266
267void ActionManager::ExecuteOneCommand() {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700268 // Loop through the event queue until we have an action to execute
269 while (current_executing_actions_.empty() && !event_queue_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700270 for (const auto& action : actions_) {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700271 if (std::visit([&action](const auto& event) { return action->CheckEvent(event); },
272 event_queue_.front())) {
Tom Cherryb7349902015-08-26 11:43:36 -0700273 current_executing_actions_.emplace(action.get());
274 }
275 }
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700276 event_queue_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700277 }
278
279 if (current_executing_actions_.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700280 return;
281 }
282
Tom Cherryb7349902015-08-26 11:43:36 -0700283 auto action = current_executing_actions_.front();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700284
Tom Cherrycb716f92015-08-11 12:34:22 -0700285 if (current_command_ == 0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700286 std::string trigger_name = action->BuildTriggersString();
Tom Cherry012c5732017-04-18 13:21:54 -0700287 LOG(INFO) << "processing action (" << trigger_name << ") from (" << action->filename()
288 << ":" << action->line() << ")";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700289 }
290
Tom Cherryb7349902015-08-26 11:43:36 -0700291 action->ExecuteOneCommand(current_command_);
292
293 // If this was the last command in the current action, then remove
294 // the action from the executing list.
295 // If this action was oneshot, then also remove it from actions_.
296 ++current_command_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700297 if (current_command_ == action->NumCommands()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700298 current_executing_actions_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700299 current_command_ = 0;
Tom Cherryb7349902015-08-26 11:43:36 -0700300 if (action->oneshot()) {
301 auto eraser = [&action] (std::unique_ptr<Action>& a) {
302 return a.get() == action;
303 };
304 actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
305 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700306 }
307}
308
Tom Cherryb7349902015-08-26 11:43:36 -0700309bool ActionManager::HasMoreCommands() const {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700310 return !current_executing_actions_.empty() || !event_queue_.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700311}
312
Tom Cherryb7349902015-08-26 11:43:36 -0700313void ActionManager::DumpState() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700314 for (const auto& a : actions_) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700315 a->DumpState();
316 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700317}
Tom Cherryb7349902015-08-26 11:43:36 -0700318
Wei Wangeeab4912017-06-27 22:08:45 -0700319void ActionManager::ClearQueue() {
320 // We are shutting down so don't claim the oneshot builtin actions back
321 current_executing_actions_ = {};
322 event_queue_ = {};
323 current_command_ = 0;
324}
325
Tom Cherry89bcc852017-08-02 17:01:36 -0700326Result<Success> ActionParser::ParseSection(std::vector<std::string>&& args,
327 const std::string& filename, int line) {
Tom Cherryb7349902015-08-26 11:43:36 -0700328 std::vector<std::string> triggers(args.begin() + 1, args.end());
329 if (triggers.size() < 1) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700330 return Error() << "Actions must have a trigger";
Tom Cherryb7349902015-08-26 11:43:36 -0700331 }
332
Tom Cherry012c5732017-04-18 13:21:54 -0700333 auto action = std::make_unique<Action>(false, filename, line);
Tom Cherry89bcc852017-08-02 17:01:36 -0700334
335 if (auto result = action->InitTriggers(triggers); !result) {
336 return Error() << "InitTriggers() failed: " << result.error();
Tom Cherryb7349902015-08-26 11:43:36 -0700337 }
338
339 action_ = std::move(action);
Tom Cherry89bcc852017-08-02 17:01:36 -0700340 return Success();
Tom Cherryb7349902015-08-26 11:43:36 -0700341}
342
Tom Cherry89bcc852017-08-02 17:01:36 -0700343Result<Success> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) {
344 return action_ ? action_->AddCommand(std::move(args), line) : Success();
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}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700352
353} // namespace init
354} // namespace android