blob: 3fdff43a6ddc25f7dccfbc276d8d549c360a444b [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
Jaekyun Seokeeb21882018-01-16 12:51:19 +090024#include "stable_properties.h"
Tom Cherryfa0c21c2015-07-23 17:53:11 -070025#include "util.h"
26
Tom Cherryb7349902015-08-26 11:43:36 -070027using android::base::Join;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070028using android::base::StartsWith;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070029
Tom Cherry81f5d3e2017-06-22 12:53:17 -070030namespace android {
31namespace init {
32
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070033Result<Success> RunBuiltinFunction(const BuiltinFunction& function,
34 const std::vector<std::string>& args,
35 const std::string& context) {
36 auto builtin_arguments = BuiltinArguments(context);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070037
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070038 builtin_arguments.args.resize(args.size());
39 builtin_arguments.args[0] = args[0];
40 for (std::size_t i = 1; i < args.size(); ++i) {
41 if (!expand_props(args[i], &builtin_arguments.args[i])) {
42 return Error() << "cannot expand '" << args[i] << "'";
Tom Cherryfa0c21c2015-07-23 17:53:11 -070043 }
44 }
45
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070046 return function(builtin_arguments);
47}
48
49Command::Command(BuiltinFunction f, bool execute_in_subcontext,
50 const std::vector<std::string>& args, int line)
51 : func_(std::move(f)), execute_in_subcontext_(execute_in_subcontext), args_(args), line_(line) {}
52
53Result<Success> Command::InvokeFunc(Subcontext* subcontext) const {
Tom Cherryc49719f2018-01-10 11:04:34 -080054 if (subcontext) {
55 if (execute_in_subcontext_) {
56 return subcontext->Execute(args_);
57 }
58
59 auto expanded_args = subcontext->ExpandArgs(args_);
60 if (!expanded_args) {
61 return expanded_args.error();
62 }
63 return RunBuiltinFunction(func_, *expanded_args, subcontext->context());
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070064 }
Tom Cherryc49719f2018-01-10 11:04:34 -080065
66 return RunBuiltinFunction(func_, args_, kInitContext);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070067}
68
Tom Cherryb7349902015-08-26 11:43:36 -070069std::string Command::BuildCommandString() const {
70 return Join(args_, ' ');
Tom Cherryfa0c21c2015-07-23 17:53:11 -070071}
72
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070073Action::Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line)
74 : oneshot_(oneshot), subcontext_(subcontext), filename_(filename), line_(line) {}
Tom Cherryfa0c21c2015-07-23 17:53:11 -070075
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070076const KeywordFunctionMap* Action::function_map_ = nullptr;
Tom Cherryb7349902015-08-26 11:43:36 -070077
Tom Cherry89bcc852017-08-02 17:01:36 -070078Result<Success> Action::AddCommand(const std::vector<std::string>& args, int line) {
Tom Cherryb7349902015-08-26 11:43:36 -070079 if (!function_map_) {
Tom Cherry89bcc852017-08-02 17:01:36 -070080 return Error() << "no function map available";
Tom Cherryb7349902015-08-26 11:43:36 -070081 }
82
Tom Cherry89bcc852017-08-02 17:01:36 -070083 auto function = function_map_->FindFunction(args);
84 if (!function) return Error() << function.error();
Tom Cherryb7349902015-08-26 11:43:36 -070085
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070086 commands_.emplace_back(function->second, function->first, args, line);
Tom Cherry89bcc852017-08-02 17:01:36 -070087 return Success();
Tom Cherryb7349902015-08-26 11:43:36 -070088}
89
Tom Cherry012c5732017-04-18 13:21:54 -070090void Action::AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070091 commands_.emplace_back(f, false, args, line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070092}
93
Tom Cherryb7349902015-08-26 11:43:36 -070094std::size_t Action::NumCommands() const {
95 return commands_.size();
96}
97
98void Action::ExecuteOneCommand(std::size_t command) const {
Wei Wangd67a4ab2016-11-16 12:08:30 -080099 // We need a copy here since some Command execution may result in
100 // changing commands_ vector by importing .rc files through parser
101 Command cmd = commands_[command];
102 ExecuteCommand(cmd);
Tom Cherryb7349902015-08-26 11:43:36 -0700103}
104
105void Action::ExecuteAllCommands() const {
106 for (const auto& c : commands_) {
107 ExecuteCommand(c);
108 }
109}
110
111void Action::ExecuteCommand(const Command& command) const {
Tom Cherryede0d532017-07-06 14:20:11 -0700112 android::base::Timer t;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700113 auto result = command.InvokeFunc(subcontext_);
Tom Cherryede0d532017-07-06 14:20:11 -0700114 auto duration = t.duration();
Tom Cherry557946e2017-08-01 13:50:23 -0700115
Tom Cherry68f2a462017-08-22 16:15:26 -0700116 // There are many legacy paths in rootdir/init.rc that will virtually never exist on a new
117 // device, such as '/sys/class/leds/jogball-backlight/brightness'. As of this writing, there
118 // are 198 such failures on bullhead. Instead of spamming the log reporting them, we do not
119 // report such failures unless we're running at the DEBUG log level.
120 bool report_failure = !result.has_value();
121 if (report_failure && android::base::GetMinimumLogSeverity() > android::base::DEBUG &&
122 result.error_errno() == ENOENT) {
123 report_failure = false;
124 }
125
Wei Wang8b1d5262016-11-15 23:58:55 -0800126 // Any action longer than 50ms will be warned to user as slow operation
Tom Cherry68f2a462017-08-22 16:15:26 -0700127 if (report_failure || duration > 50ms ||
128 android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700129 std::string trigger_name = BuildTriggersString();
130 std::string cmd_str = command.BuildCommandString();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700131
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700132 LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_
Tom Cherry557946e2017-08-01 13:50:23 -0700133 << ":" << command.line() << ") took " << duration.count() << "ms and "
Tom Cherry130e3d72017-08-22 16:07:15 -0700134 << (result ? "succeeded" : "failed: " + result.error_string());
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700135 }
136}
137
Jaekyun Seokeeb21882018-01-16 12:51:19 +0900138static bool IsActionableProperty(Subcontext* subcontext, const std::string& prop_name) {
139 static bool enabled =
140 android::base::GetBoolProperty("ro.actionable_compatible_property.enabled", false);
141
142 if (subcontext == nullptr || !enabled) {
143 return true;
144 }
145
146 if (kExportedActionableProperties.count(prop_name) == 1) {
147 return true;
148 }
149 for (const auto& prefix : kPartnerPrefixes) {
150 if (android::base::StartsWith(prop_name, prefix)) {
151 return true;
152 }
153 }
154 return false;
155}
156
Tom Cherry89bcc852017-08-02 17:01:36 -0700157Result<Success> Action::ParsePropertyTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700158 const static std::string prop_str("property:");
159 std::string prop_name(trigger.substr(prop_str.length()));
160 size_t equal_pos = prop_name.find('=');
161 if (equal_pos == std::string::npos) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700162 return Error() << "property trigger found without matching '='";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700163 }
164
165 std::string prop_value(prop_name.substr(equal_pos + 1));
166 prop_name.erase(equal_pos);
167
Jaekyun Seokeeb21882018-01-16 12:51:19 +0900168 if (!IsActionableProperty(subcontext_, prop_name)) {
169 return Error() << "unexported property tigger found: " << prop_name;
170 }
171
Tom Cherry2bc00142017-03-13 11:58:58 -0700172 if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700173 return Error() << "multiple property triggers found for same property";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700174 }
Tom Cherry89bcc852017-08-02 17:01:36 -0700175 return Success();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700176}
177
Tom Cherry89bcc852017-08-02 17:01:36 -0700178Result<Success> Action::InitTriggers(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700179 const static std::string prop_str("property:");
180 for (std::size_t i = 0; i < args.size(); ++i) {
Wei Wang93df4e12016-11-16 19:19:49 -0800181 if (args[i].empty()) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700182 return Error() << "empty trigger is not valid";
Wei Wang93df4e12016-11-16 19:19:49 -0800183 }
184
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700185 if (i % 2) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700186 if (args[i] != "&&") {
Tom Cherry89bcc852017-08-02 17:01:36 -0700187 return Error() << "&& is the only symbol allowed to concatenate actions";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700188 } else {
189 continue;
190 }
191 }
192
193 if (!args[i].compare(0, prop_str.length(), prop_str)) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700194 if (auto result = ParsePropertyTrigger(args[i]); !result) {
195 return result;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700196 }
197 } else {
198 if (!event_trigger_.empty()) {
Tom Cherry89bcc852017-08-02 17:01:36 -0700199 return Error() << "multiple event triggers are not allowed";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700200 }
201
202 event_trigger_ = args[i];
203 }
204 }
205
Tom Cherry89bcc852017-08-02 17:01:36 -0700206 return Success();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700207}
208
Tom Cherry89bcc852017-08-02 17:01:36 -0700209Result<Success> Action::InitSingleTrigger(const std::string& trigger) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700210 std::vector<std::string> name_vector{trigger};
Tom Cherry89bcc852017-08-02 17:01:36 -0700211 if (auto result = InitTriggers(name_vector); !result) {
212 return Error() << "InitTriggers() failed: " << result.error();
Wei Wang93df4e12016-11-16 19:19:49 -0800213 }
Tom Cherry89bcc852017-08-02 17:01:36 -0700214 return Success();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700215}
216
Tom Cherryb7349902015-08-26 11:43:36 -0700217// This function checks that all property triggers are satisfied, that is
218// for each (name, value) in property_triggers_, check that the current
219// value of the property 'name' == value.
220//
221// It takes an optional (name, value) pair, which if provided must
222// be present in property_triggers_; it skips the check of the current
223// property value for this pair.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700224bool Action::CheckPropertyTriggers(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700225 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700226 if (property_triggers_.empty()) {
227 return true;
228 }
229
Tom Cherryb7349902015-08-26 11:43:36 -0700230 bool found = name.empty();
Tom Cherry2bc00142017-03-13 11:58:58 -0700231 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700232 if (trigger_name == name) {
233 if (trigger_value != "*" && trigger_value != value) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700234 return false;
235 } else {
236 found = true;
237 }
238 } else {
Tom Cherryccf23532017-03-28 16:40:41 -0700239 std::string prop_val = android::base::GetProperty(trigger_name, "");
240 if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700241 return false;
242 }
243 }
244 }
245 return found;
246}
247
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700248bool Action::CheckEvent(const EventTrigger& event_trigger) const {
249 return event_trigger == event_trigger_ && CheckPropertyTriggers();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700250}
251
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700252bool Action::CheckEvent(const PropertyChange& property_change) const {
253 const auto& [name, value] = property_change;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700254 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
255}
256
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700257bool Action::CheckEvent(const BuiltinAction& builtin_action) const {
258 return this == builtin_action;
259}
260
Tom Cherryb7349902015-08-26 11:43:36 -0700261std::string Action::BuildTriggersString() const {
Tom Cherryd8a72572017-03-13 12:24:49 -0700262 std::vector<std::string> triggers;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700263
Tom Cherry2bc00142017-03-13 11:58:58 -0700264 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700265 triggers.emplace_back(trigger_name + '=' + trigger_value);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700266 }
267 if (!event_trigger_.empty()) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700268 triggers.emplace_back(event_trigger_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700269 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700270
271 return Join(triggers, " && ");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700272}
273
Tom Cherryb7349902015-08-26 11:43:36 -0700274void Action::DumpState() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700275 std::string trigger_name = BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700276 LOG(INFO) << "on " << trigger_name;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700277
278 for (const auto& c : commands_) {
Tom Cherryb7349902015-08-26 11:43:36 -0700279 std::string cmd_str = c.BuildCommandString();
Tom Cherryd8a72572017-03-13 12:24:49 -0700280 LOG(INFO) << " " << cmd_str;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700281 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700282}
283
Tom Cherryb7349902015-08-26 11:43:36 -0700284ActionManager::ActionManager() : current_command_(0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700285}
286
287ActionManager& ActionManager::GetInstance() {
288 static ActionManager instance;
289 return instance;
290}
291
Tom Cherryb7349902015-08-26 11:43:36 -0700292void ActionManager::AddAction(std::unique_ptr<Action> action) {
Tom Cherry012c5732017-04-18 13:21:54 -0700293 actions_.emplace_back(std::move(action));
Tom Cherryb7349902015-08-26 11:43:36 -0700294}
295
296void ActionManager::QueueEventTrigger(const std::string& trigger) {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700297 event_queue_.emplace(trigger);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700298}
299
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700300void ActionManager::QueuePropertyChange(const std::string& name, const std::string& value) {
301 event_queue_.emplace(std::make_pair(name, value));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700302}
303
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700304void ActionManager::QueueAllPropertyActions() {
305 QueuePropertyChange("", "");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700306}
307
Tom Cherry012c5732017-04-18 13:21:54 -0700308void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700309 auto action = std::make_unique<Action>(true, nullptr, "<Builtin Action>", 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700310 std::vector<std::string> name_vector{name};
311
Tom Cherry89bcc852017-08-02 17:01:36 -0700312 if (auto result = action->InitSingleTrigger(name); !result) {
313 LOG(ERROR) << "Cannot queue BuiltinAction for " << name << ": " << result.error();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700314 return;
315 }
316
Tom Cherry012c5732017-04-18 13:21:54 -0700317 action->AddCommand(func, name_vector, 0);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700318
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700319 event_queue_.emplace(action.get());
Tom Cherryb7349902015-08-26 11:43:36 -0700320 actions_.emplace_back(std::move(action));
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700321}
322
323void ActionManager::ExecuteOneCommand() {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700324 // Loop through the event queue until we have an action to execute
325 while (current_executing_actions_.empty() && !event_queue_.empty()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700326 for (const auto& action : actions_) {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700327 if (std::visit([&action](const auto& event) { return action->CheckEvent(event); },
328 event_queue_.front())) {
Tom Cherryb7349902015-08-26 11:43:36 -0700329 current_executing_actions_.emplace(action.get());
330 }
331 }
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700332 event_queue_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700333 }
334
335 if (current_executing_actions_.empty()) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700336 return;
337 }
338
Tom Cherryb7349902015-08-26 11:43:36 -0700339 auto action = current_executing_actions_.front();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700340
Tom Cherrycb716f92015-08-11 12:34:22 -0700341 if (current_command_ == 0) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700342 std::string trigger_name = action->BuildTriggersString();
Tom Cherry012c5732017-04-18 13:21:54 -0700343 LOG(INFO) << "processing action (" << trigger_name << ") from (" << action->filename()
344 << ":" << action->line() << ")";
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700345 }
346
Tom Cherryb7349902015-08-26 11:43:36 -0700347 action->ExecuteOneCommand(current_command_);
348
349 // If this was the last command in the current action, then remove
350 // the action from the executing list.
351 // If this action was oneshot, then also remove it from actions_.
352 ++current_command_;
Tom Cherrycb716f92015-08-11 12:34:22 -0700353 if (current_command_ == action->NumCommands()) {
Tom Cherryb7349902015-08-26 11:43:36 -0700354 current_executing_actions_.pop();
Tom Cherrycb716f92015-08-11 12:34:22 -0700355 current_command_ = 0;
Tom Cherryb7349902015-08-26 11:43:36 -0700356 if (action->oneshot()) {
357 auto eraser = [&action] (std::unique_ptr<Action>& a) {
358 return a.get() == action;
359 };
360 actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
361 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700362 }
363}
364
Tom Cherryb7349902015-08-26 11:43:36 -0700365bool ActionManager::HasMoreCommands() const {
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700366 return !current_executing_actions_.empty() || !event_queue_.empty();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700367}
368
Tom Cherryb7349902015-08-26 11:43:36 -0700369void ActionManager::DumpState() const {
Tom Cherrycb716f92015-08-11 12:34:22 -0700370 for (const auto& a : actions_) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700371 a->DumpState();
372 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700373}
Tom Cherryb7349902015-08-26 11:43:36 -0700374
Wei Wangeeab4912017-06-27 22:08:45 -0700375void ActionManager::ClearQueue() {
376 // We are shutting down so don't claim the oneshot builtin actions back
377 current_executing_actions_ = {};
378 event_queue_ = {};
379 current_command_ = 0;
380}
381
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700382} // namespace init
383} // namespace android