blob: 1bba0f262d754c0fcec01e787142ccb4f949023e [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 {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800108 // We need a copy here since some Command execution may result in
109 // changing commands_ vector by importing .rc files through parser
110 Command cmd = commands_[command];
111 ExecuteCommand(cmd);
Tom Cherryb7349902015-08-26 11:43:36 -0700112}
113
114void Action::ExecuteAllCommands() const {
115 for (const auto& c : commands_) {
116 ExecuteCommand(c);
117 }
118}
119
120void Action::ExecuteCommand(const Command& command) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700121 Timer t;
122 int result = command.InvokeFunc();
123
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000124 double duration_ms = t.duration_s() * 1000;
Wei Wang8b1d5262016-11-15 23:58:55 -0800125 // Any action longer than 50ms will be warned to user as slow operation
126 if (duration_ms > 50.0 ||
127 android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700128 std::string trigger_name = BuildTriggersString();
129 std::string cmd_str = command.BuildCommandString();
130 std::string source = command.BuildSourceString();
131
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700132 LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << source
Wei Wang8b1d5262016-11-15 23:58:55 -0800133 << " returned " << result << " took " << duration_ms << "ms.";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700134 }
135}
136
Tom Cherryb7349902015-08-26 11:43:36 -0700137bool Action::ParsePropertyTrigger(const std::string& trigger, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700138 const static std::string prop_str("property:");
139 std::string prop_name(trigger.substr(prop_str.length()));
140 size_t equal_pos = prop_name.find('=');
141 if (equal_pos == std::string::npos) {
142 *err = "property trigger found without matching '='";
143 return false;
144 }
145
146 std::string prop_value(prop_name.substr(equal_pos + 1));
147 prop_name.erase(equal_pos);
148
Tom Cherry2bc00142017-03-13 11:58:58 -0700149 if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700150 *err = "multiple property triggers found for same property";
151 return false;
152 }
153 return true;
154}
155
Tom Cherryb7349902015-08-26 11:43:36 -0700156bool Action::InitTriggers(const std::vector<std::string>& args, std::string* err) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700157 const static std::string prop_str("property:");
158 for (std::size_t i = 0; i < args.size(); ++i) {
Wei Wang93df4e12016-11-16 19:19:49 -0800159 if (args[i].empty()) {
160 *err = "empty trigger is not valid";
161 return false;
162 }
163
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700164 if (i % 2) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700165 if (args[i] != "&&") {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700166 *err = "&& is the only symbol allowed to concatenate actions";
167 return false;
168 } else {
169 continue;
170 }
171 }
172
173 if (!args[i].compare(0, prop_str.length(), prop_str)) {
174 if (!ParsePropertyTrigger(args[i], err)) {
175 return false;
176 }
177 } else {
178 if (!event_trigger_.empty()) {
179 *err = "multiple event triggers are not allowed";
180 return false;
181 }
182
183 event_trigger_ = args[i];
184 }
185 }
186
187 return true;
188}
189
Tom Cherryb7349902015-08-26 11:43:36 -0700190bool Action::InitSingleTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700191 std::vector<std::string> name_vector{trigger};
192 std::string err;
Wei Wang93df4e12016-11-16 19:19:49 -0800193 bool ret = InitTriggers(name_vector, &err);
194 if (!ret) {
195 LOG(ERROR) << "InitSingleTrigger failed due to: " << err;
196 }
197 return ret;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700198}
199
Tom Cherryb7349902015-08-26 11:43:36 -0700200// This function checks that all property triggers are satisfied, that is
201// for each (name, value) in property_triggers_, check that the current
202// value of the property 'name' == value.
203//
204// It takes an optional (name, value) pair, which if provided must
205// be present in property_triggers_; it skips the check of the current
206// property value for this pair.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700207bool Action::CheckPropertyTriggers(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700208 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700209 if (property_triggers_.empty()) {
210 return true;
211 }
212
Tom Cherryb7349902015-08-26 11:43:36 -0700213 bool found = name.empty();
Tom Cherry2bc00142017-03-13 11:58:58 -0700214 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700215 if (trigger_name == name) {
216 if (trigger_value != "*" && trigger_value != value) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700217 return false;
218 } else {
219 found = true;
220 }
221 } else {
Tom Cherrycb716f92015-08-11 12:34:22 -0700222 std::string prop_val = property_get(trigger_name.c_str());
223 if (prop_val.empty() || (trigger_value != "*" &&
224 trigger_value != prop_val)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700225 return false;
226 }
227 }
228 }
229 return found;
230}
231
Tom Cherryb7349902015-08-26 11:43:36 -0700232bool Action::CheckEventTrigger(const std::string& trigger) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700233 return !event_trigger_.empty() &&
Tom Cherrycb716f92015-08-11 12:34:22 -0700234 trigger == event_trigger_ &&
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700235 CheckPropertyTriggers();
236}
237
238bool Action::CheckPropertyTrigger(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700239 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700240 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
241}
242
Tom Cherryb7349902015-08-26 11:43:36 -0700243bool Action::TriggersEqual(const Action& other) const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700244 return property_triggers_ == other.property_triggers_ &&
245 event_trigger_ == other.event_trigger_;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700246}
247
Tom Cherryb7349902015-08-26 11:43:36 -0700248std::string Action::BuildTriggersString() const {
Tom Cherryd8a72572017-03-13 12:24:49 -0700249 std::vector<std::string> triggers;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700250
Tom Cherry2bc00142017-03-13 11:58:58 -0700251 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700252 triggers.emplace_back(trigger_name + '=' + trigger_value);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700253 }
254 if (!event_trigger_.empty()) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700255 triggers.emplace_back(event_trigger_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700256 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700257
258 return Join(triggers, " && ");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700259}
260
Tom Cherryb7349902015-08-26 11:43:36 -0700261void Action::DumpState() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700262 std::string trigger_name = BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700263 LOG(INFO) << "on " << trigger_name;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700264
265 for (const auto& c : commands_) {
Tom Cherryb7349902015-08-26 11:43:36 -0700266 std::string cmd_str = c.BuildCommandString();
Tom Cherryd8a72572017-03-13 12:24:49 -0700267 LOG(INFO) << " " << cmd_str;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700268 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700269}
270
Tom Cherrycb716f92015-08-11 12:34:22 -0700271class EventTrigger : public Trigger {
272public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700273 explicit EventTrigger(const std::string& trigger) : trigger_(trigger) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700274 }
Tom Cherryb7349902015-08-26 11:43:36 -0700275 bool CheckTriggers(const Action& action) const override {
276 return action.CheckEventTrigger(trigger_);
Tom Cherrycb716f92015-08-11 12:34:22 -0700277 }
278private:
Tom Cherryb7349902015-08-26 11:43:36 -0700279 const std::string trigger_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700280};
281
282class PropertyTrigger : public Trigger {
283public:
284 PropertyTrigger(const std::string& name, const std::string& value)
285 : name_(name), value_(value) {
286 }
Tom Cherryb7349902015-08-26 11:43:36 -0700287 bool CheckTriggers(const Action& action) const override {
288 return action.CheckPropertyTrigger(name_, value_);
Tom Cherrycb716f92015-08-11 12:34:22 -0700289 }
290private:
Tom Cherryb7349902015-08-26 11:43:36 -0700291 const std::string name_;
292 const std::string value_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700293};
294
295class BuiltinTrigger : public Trigger {
296public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -0700297 explicit BuiltinTrigger(Action* action) : action_(action) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700298 }
Tom Cherryb7349902015-08-26 11:43:36 -0700299 bool CheckTriggers(const Action& action) const override {
300 return action_ == &action;
Tom Cherrycb716f92015-08-11 12:34:22 -0700301 }
302private:
Tom Cherryb7349902015-08-26 11:43:36 -0700303 const Action* action_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700304};
305
Tom Cherryb7349902015-08-26 11:43:36 -0700306ActionManager::ActionManager() : current_command_(0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700307}
308
309ActionManager& ActionManager::GetInstance() {
310 static ActionManager instance;
311 return instance;
312}
313
Tom Cherryb7349902015-08-26 11:43:36 -0700314void ActionManager::AddAction(std::unique_ptr<Action> action) {
315 auto old_action_it =
316 std::find_if(actions_.begin(), actions_.end(),
317 [&action] (std::unique_ptr<Action>& a) {
318 return action->TriggersEqual(*a);
319 });
320
321 if (old_action_it != actions_.end()) {
322 (*old_action_it)->CombineAction(*action);
323 } else {
324 actions_.emplace_back(std::move(action));
325 }
326}
327
328void ActionManager::QueueEventTrigger(const std::string& trigger) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700329 trigger_queue_.push(std::make_unique<EventTrigger>(trigger));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700330}
331
332void ActionManager::QueuePropertyTrigger(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700333 const std::string& value) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700334 trigger_queue_.push(std::make_unique<PropertyTrigger>(name, value));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700335}
336
Tom Cherryb7349902015-08-26 11:43:36 -0700337void ActionManager::QueueAllPropertyTriggers() {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700338 QueuePropertyTrigger("", "");
339}
340
Tom Cherryb7349902015-08-26 11:43:36 -0700341void ActionManager::QueueBuiltinAction(BuiltinFunction func,
342 const std::string& name) {
343 auto action = std::make_unique<Action>(true);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700344 std::vector<std::string> name_vector{name};
345
Tom Cherryb7349902015-08-26 11:43:36 -0700346 if (!action->InitSingleTrigger(name)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700347 return;
348 }
349
Tom Cherryb7349902015-08-26 11:43:36 -0700350 action->AddCommand(func, name_vector);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700351
Tom Cherryb7349902015-08-26 11:43:36 -0700352 trigger_queue_.push(std::make_unique<BuiltinTrigger>(action.get()));
353 actions_.emplace_back(std::move(action));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700354}
355
356void ActionManager::ExecuteOneCommand() {
Tom Cherryb7349902015-08-26 11:43:36 -0700357 // Loop through the trigger queue until we have an action to execute
Tom Cherrycb716f92015-08-11 12:34:22 -0700358 while (current_executing_actions_.empty() && !trigger_queue_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700359 for (const auto& action : actions_) {
360 if (trigger_queue_.front()->CheckTriggers(*action)) {
361 current_executing_actions_.emplace(action.get());
362 }
363 }
Tom Cherrycb716f92015-08-11 12:34:22 -0700364 trigger_queue_.pop();
365 }
366
367 if (current_executing_actions_.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700368 return;
369 }
370
Tom Cherryb7349902015-08-26 11:43:36 -0700371 auto action = current_executing_actions_.front();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700372
Tom Cherrycb716f92015-08-11 12:34:22 -0700373 if (current_command_ == 0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700374 std::string trigger_name = action->BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700375 LOG(INFO) << "processing action (" << trigger_name << ")";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700376 }
377
Tom Cherryb7349902015-08-26 11:43:36 -0700378 action->ExecuteOneCommand(current_command_);
379
380 // If this was the last command in the current action, then remove
381 // the action from the executing list.
382 // If this action was oneshot, then also remove it from actions_.
383 ++current_command_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700384 if (current_command_ == action->NumCommands()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700385 current_executing_actions_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700386 current_command_ = 0;
Tom Cherryb7349902015-08-26 11:43:36 -0700387 if (action->oneshot()) {
388 auto eraser = [&action] (std::unique_ptr<Action>& a) {
389 return a.get() == action;
390 };
391 actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
392 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700393 }
394}
395
Tom Cherryb7349902015-08-26 11:43:36 -0700396bool ActionManager::HasMoreCommands() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700397 return !current_executing_actions_.empty() || !trigger_queue_.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700398}
399
Tom Cherryb7349902015-08-26 11:43:36 -0700400void ActionManager::DumpState() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700401 for (const auto& a : actions_) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700402 a->DumpState();
403 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700404}
Tom Cherryb7349902015-08-26 11:43:36 -0700405
406bool ActionParser::ParseSection(const std::vector<std::string>& args,
407 std::string* err) {
408 std::vector<std::string> triggers(args.begin() + 1, args.end());
409 if (triggers.size() < 1) {
410 *err = "actions must have a trigger";
411 return false;
412 }
413
414 auto action = std::make_unique<Action>(false);
415 if (!action->InitTriggers(triggers, err)) {
416 return false;
417 }
418
419 action_ = std::move(action);
420 return true;
421}
422
423bool ActionParser::ParseLineSection(const std::vector<std::string>& args,
424 const std::string& filename, int line,
425 std::string* err) const {
426 return action_ ? action_->AddCommand(args, filename, line, err) : false;
427}
428
429void ActionParser::EndSection() {
430 if (action_ && action_->NumCommands() > 0) {
431 ActionManager::GetInstance().AddAction(std::move(action_));
432 }
433}