blob: ccc18cf9e3d64f0ad04b5c73c445171b55e5c60b [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
19#include <errno.h>
20
Elliott Hughes4f713192015-12-04 22:00:26 -080021#include <android-base/strings.h>
22#include <android-base/stringprintf.h>
Tom Cherryfa0c21c2015-07-23 17:53:11 -070023
Tom Cherryb7349902015-08-26 11:43:36 -070024#include "builtins.h"
Tom Cherryfa0c21c2015-07-23 17:53:11 -070025#include "error.h"
26#include "init_parser.h"
27#include "log.h"
28#include "property_service.h"
29#include "util.h"
30
Tom Cherryb7349902015-08-26 11:43:36 -070031using android::base::Join;
32using android::base::StringPrintf;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070033
Tom Cherryb7349902015-08-26 11:43:36 -070034Command::Command(BuiltinFunction f, const std::vector<std::string>& args,
35 const std::string& filename, int line)
36 : func_(f), args_(args), filename_(filename), line_(line) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070037}
38
Tom Cherryb7349902015-08-26 11:43:36 -070039int Command::InvokeFunc() const {
Tom Cherry96f67312015-07-30 13:52:55 -070040 std::vector<std::string> expanded_args;
41 expanded_args.resize(args_.size());
42 expanded_args[0] = args_[0];
Tom Cherryfa0c21c2015-07-23 17:53:11 -070043 for (std::size_t i = 1; i < args_.size(); ++i) {
Tom Cherryb7349902015-08-26 11:43:36 -070044 if (!expand_props(args_[i], &expanded_args[i])) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070045 LOG(ERROR) << args_[0] << ": cannot expand '" << args_[i] << "'";
Tom Cherryfa0c21c2015-07-23 17:53:11 -070046 return -EINVAL;
47 }
48 }
49
Tom Cherry96f67312015-07-30 13:52:55 -070050 return func_(expanded_args);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070051}
52
Tom Cherryb7349902015-08-26 11:43:36 -070053std::string Command::BuildCommandString() const {
54 return Join(args_, ' ');
Tom Cherryfa0c21c2015-07-23 17:53:11 -070055}
56
Tom Cherryb7349902015-08-26 11:43:36 -070057std::string Command::BuildSourceString() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070058 if (!filename_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -070059 return StringPrintf(" (%s:%d)", filename_.c_str(), line_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070060 } else {
61 return std::string();
62 }
63}
64
Tom Cherryb7349902015-08-26 11:43:36 -070065Action::Action(bool oneshot) : oneshot_(oneshot) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -070066}
67
Tom Cherryb7349902015-08-26 11:43:36 -070068const KeywordMap<BuiltinFunction>* Action::function_map_ = nullptr;
69
70bool Action::AddCommand(const std::vector<std::string>& args,
71 const std::string& filename, int line, std::string* err) {
72 if (!function_map_) {
73 *err = "no function map available";
74 return false;
75 }
76
77 if (args.empty()) {
78 *err = "command needed, but not provided";
79 return false;
80 }
81
82 auto function = function_map_->FindFunction(args[0], args.size() - 1, err);
83 if (!function) {
84 return false;
85 }
86
87 AddCommand(function, args, filename, line);
88 return true;
89}
90
91void Action::AddCommand(BuiltinFunction f,
Tom Cherryfa0c21c2015-07-23 17:53:11 -070092 const std::vector<std::string>& args,
Tom Cherryb7349902015-08-26 11:43:36 -070093 const std::string& filename, int line) {
94 commands_.emplace_back(f, args, filename, line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070095}
96
Tom Cherryb7349902015-08-26 11:43:36 -070097void Action::CombineAction(const Action& action) {
98 for (const auto& c : action.commands_) {
99 commands_.emplace_back(c);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700100 }
101}
102
Tom Cherryb7349902015-08-26 11:43:36 -0700103std::size_t Action::NumCommands() const {
104 return commands_.size();
105}
106
107void Action::ExecuteOneCommand(std::size_t command) const {
108 ExecuteCommand(commands_[command]);
109}
110
111void Action::ExecuteAllCommands() const {
112 for (const auto& c : commands_) {
113 ExecuteCommand(c);
114 }
115}
116
117void Action::ExecuteCommand(const Command& command) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700118 Timer t;
119 int result = command.InvokeFunc();
120
Nick Desaulniers1802d112016-11-15 00:53:00 +0000121 // TODO: this should probably be changed to "if (failed || took a long time)"...
122 if (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
Nick Desaulniers1802d112016-11-15 00:53:00 +0000128 << " returned " << result << " took " << t.duration() << "s";
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
144 auto res = property_triggers_.emplace(prop_name, prop_value);
145 if (res.second == false) {
146 *err = "multiple property triggers found for same property";
147 return false;
148 }
149 return true;
150}
151
Tom Cherryb7349902015-08-26 11:43:36 -0700152bool Action::InitTriggers(const std::vector<std::string>& args, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700153 const static std::string prop_str("property:");
154 for (std::size_t i = 0; i < args.size(); ++i) {
155 if (i % 2) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700156 if (args[i] != "&&") {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700157 *err = "&& is the only symbol allowed to concatenate actions";
158 return false;
159 } else {
160 continue;
161 }
162 }
163
164 if (!args[i].compare(0, prop_str.length(), prop_str)) {
165 if (!ParsePropertyTrigger(args[i], err)) {
166 return false;
167 }
168 } else {
169 if (!event_trigger_.empty()) {
170 *err = "multiple event triggers are not allowed";
171 return false;
172 }
173
174 event_trigger_ = args[i];
175 }
176 }
177
178 return true;
179}
180
Tom Cherryb7349902015-08-26 11:43:36 -0700181bool Action::InitSingleTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700182 std::vector<std::string> name_vector{trigger};
183 std::string err;
184 return InitTriggers(name_vector, &err);
185}
186
Tom Cherryb7349902015-08-26 11:43:36 -0700187// This function checks that all property triggers are satisfied, that is
188// for each (name, value) in property_triggers_, check that the current
189// value of the property 'name' == value.
190//
191// It takes an optional (name, value) pair, which if provided must
192// be present in property_triggers_; it skips the check of the current
193// property value for this pair.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700194bool Action::CheckPropertyTriggers(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700195 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700196 if (property_triggers_.empty()) {
197 return true;
198 }
199
Tom Cherryb7349902015-08-26 11:43:36 -0700200 bool found = name.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700201 for (const auto& t : property_triggers_) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700202 const auto& trigger_name = t.first;
203 const auto& trigger_value = t.second;
204 if (trigger_name == name) {
205 if (trigger_value != "*" && trigger_value != value) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700206 return false;
207 } else {
208 found = true;
209 }
210 } else {
Tom Cherrycb716f92015-08-11 12:34:22 -0700211 std::string prop_val = property_get(trigger_name.c_str());
212 if (prop_val.empty() || (trigger_value != "*" &&
213 trigger_value != prop_val)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700214 return false;
215 }
216 }
217 }
218 return found;
219}
220
Tom Cherryb7349902015-08-26 11:43:36 -0700221bool Action::CheckEventTrigger(const std::string& trigger) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700222 return !event_trigger_.empty() &&
Tom Cherrycb716f92015-08-11 12:34:22 -0700223 trigger == event_trigger_ &&
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700224 CheckPropertyTriggers();
225}
226
227bool Action::CheckPropertyTrigger(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700228 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700229 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
230}
231
Tom Cherryb7349902015-08-26 11:43:36 -0700232bool Action::TriggersEqual(const Action& other) const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700233 return property_triggers_ == other.property_triggers_ &&
234 event_trigger_ == other.event_trigger_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700235}
236
Tom Cherryb7349902015-08-26 11:43:36 -0700237std::string Action::BuildTriggersString() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700238 std::string result;
239
240 for (const auto& t : property_triggers_) {
241 result += t.first;
242 result += '=';
243 result += t.second;
244 result += ' ';
245 }
246 if (!event_trigger_.empty()) {
247 result += event_trigger_;
248 result += ' ';
249 }
Wei Wang69b9b362016-11-14 20:05:21 -0800250 if (!result.empty()) {
251 result.pop_back();
252 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700253 return result;
254}
255
Tom Cherryb7349902015-08-26 11:43:36 -0700256void Action::DumpState() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700257 std::string trigger_name = BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700258 LOG(INFO) << "on " << trigger_name;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700259
260 for (const auto& c : commands_) {
Tom Cherryb7349902015-08-26 11:43:36 -0700261 std::string cmd_str = c.BuildCommandString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700262 LOG(INFO) << " %s" << cmd_str;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700263 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700264}
265
Tom Cherrycb716f92015-08-11 12:34:22 -0700266class EventTrigger : public Trigger {
267public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700268 explicit EventTrigger(const std::string& trigger) : trigger_(trigger) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700269 }
Tom Cherryb7349902015-08-26 11:43:36 -0700270 bool CheckTriggers(const Action& action) const override {
271 return action.CheckEventTrigger(trigger_);
Tom Cherrycb716f92015-08-11 12:34:22 -0700272 }
273private:
Tom Cherryb7349902015-08-26 11:43:36 -0700274 const std::string trigger_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700275};
276
277class PropertyTrigger : public Trigger {
278public:
279 PropertyTrigger(const std::string& name, const std::string& value)
280 : name_(name), value_(value) {
281 }
Tom Cherryb7349902015-08-26 11:43:36 -0700282 bool CheckTriggers(const Action& action) const override {
283 return action.CheckPropertyTrigger(name_, value_);
Tom Cherrycb716f92015-08-11 12:34:22 -0700284 }
285private:
Tom Cherryb7349902015-08-26 11:43:36 -0700286 const std::string name_;
287 const std::string value_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700288};
289
290class BuiltinTrigger : public Trigger {
291public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700292 explicit BuiltinTrigger(Action* action) : action_(action) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700293 }
Tom Cherryb7349902015-08-26 11:43:36 -0700294 bool CheckTriggers(const Action& action) const override {
295 return action_ == &action;
Tom Cherrycb716f92015-08-11 12:34:22 -0700296 }
297private:
Tom Cherryb7349902015-08-26 11:43:36 -0700298 const Action* action_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700299};
300
Tom Cherryb7349902015-08-26 11:43:36 -0700301ActionManager::ActionManager() : current_command_(0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700302}
303
304ActionManager& ActionManager::GetInstance() {
305 static ActionManager instance;
306 return instance;
307}
308
Tom Cherryb7349902015-08-26 11:43:36 -0700309void ActionManager::AddAction(std::unique_ptr<Action> action) {
310 auto old_action_it =
311 std::find_if(actions_.begin(), actions_.end(),
312 [&action] (std::unique_ptr<Action>& a) {
313 return action->TriggersEqual(*a);
314 });
315
316 if (old_action_it != actions_.end()) {
317 (*old_action_it)->CombineAction(*action);
318 } else {
319 actions_.emplace_back(std::move(action));
320 }
321}
322
323void ActionManager::QueueEventTrigger(const std::string& trigger) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700324 trigger_queue_.push(std::make_unique<EventTrigger>(trigger));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700325}
326
327void ActionManager::QueuePropertyTrigger(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700328 const std::string& value) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700329 trigger_queue_.push(std::make_unique<PropertyTrigger>(name, value));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700330}
331
Tom Cherryb7349902015-08-26 11:43:36 -0700332void ActionManager::QueueAllPropertyTriggers() {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700333 QueuePropertyTrigger("", "");
334}
335
Tom Cherryb7349902015-08-26 11:43:36 -0700336void ActionManager::QueueBuiltinAction(BuiltinFunction func,
337 const std::string& name) {
338 auto action = std::make_unique<Action>(true);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700339 std::vector<std::string> name_vector{name};
340
Tom Cherryb7349902015-08-26 11:43:36 -0700341 if (!action->InitSingleTrigger(name)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700342 return;
343 }
344
Tom Cherryb7349902015-08-26 11:43:36 -0700345 action->AddCommand(func, name_vector);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700346
Tom Cherryb7349902015-08-26 11:43:36 -0700347 trigger_queue_.push(std::make_unique<BuiltinTrigger>(action.get()));
348 actions_.emplace_back(std::move(action));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700349}
350
351void ActionManager::ExecuteOneCommand() {
Tom Cherryb7349902015-08-26 11:43:36 -0700352 // Loop through the trigger queue until we have an action to execute
Tom Cherrycb716f92015-08-11 12:34:22 -0700353 while (current_executing_actions_.empty() && !trigger_queue_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700354 for (const auto& action : actions_) {
355 if (trigger_queue_.front()->CheckTriggers(*action)) {
356 current_executing_actions_.emplace(action.get());
357 }
358 }
Tom Cherrycb716f92015-08-11 12:34:22 -0700359 trigger_queue_.pop();
360 }
361
362 if (current_executing_actions_.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700363 return;
364 }
365
Tom Cherryb7349902015-08-26 11:43:36 -0700366 auto action = current_executing_actions_.front();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700367
Tom Cherrycb716f92015-08-11 12:34:22 -0700368 if (current_command_ == 0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700369 std::string trigger_name = action->BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700370 LOG(INFO) << "processing action (" << trigger_name << ")";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700371 }
372
Tom Cherryb7349902015-08-26 11:43:36 -0700373 action->ExecuteOneCommand(current_command_);
374
375 // If this was the last command in the current action, then remove
376 // the action from the executing list.
377 // If this action was oneshot, then also remove it from actions_.
378 ++current_command_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700379 if (current_command_ == action->NumCommands()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700380 current_executing_actions_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700381 current_command_ = 0;
Tom Cherryb7349902015-08-26 11:43:36 -0700382 if (action->oneshot()) {
383 auto eraser = [&action] (std::unique_ptr<Action>& a) {
384 return a.get() == action;
385 };
386 actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
387 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700388 }
389}
390
Tom Cherryb7349902015-08-26 11:43:36 -0700391bool ActionManager::HasMoreCommands() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700392 return !current_executing_actions_.empty() || !trigger_queue_.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700393}
394
Tom Cherryb7349902015-08-26 11:43:36 -0700395void ActionManager::DumpState() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700396 for (const auto& a : actions_) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700397 a->DumpState();
398 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700399}
Tom Cherryb7349902015-08-26 11:43:36 -0700400
401bool ActionParser::ParseSection(const std::vector<std::string>& args,
402 std::string* err) {
403 std::vector<std::string> triggers(args.begin() + 1, args.end());
404 if (triggers.size() < 1) {
405 *err = "actions must have a trigger";
406 return false;
407 }
408
409 auto action = std::make_unique<Action>(false);
410 if (!action->InitTriggers(triggers, err)) {
411 return false;
412 }
413
414 action_ = std::move(action);
415 return true;
416}
417
418bool ActionParser::ParseLineSection(const std::vector<std::string>& args,
419 const std::string& filename, int line,
420 std::string* err) const {
421 return action_ ? action_->AddCommand(args, filename, line, err) : false;
422}
423
424void ActionParser::EndSection() {
425 if (action_ && action_->NumCommands() > 0) {
426 ActionManager::GetInstance().AddAction(std::move(action_));
427 }
428}