2015-07-24 02:53:11 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "action.h"
|
|
|
|
|
2017-07-06 23:20:11 +02:00
|
|
|
#include <android-base/chrono_utils.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <android-base/logging.h>
|
2017-03-29 01:40:41 +02:00
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/strings.h>
|
2015-07-24 02:53:11 +02:00
|
|
|
|
2018-01-16 04:51:19 +01:00
|
|
|
#include "stable_properties.h"
|
2015-07-24 02:53:11 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
using android::base::Join;
|
2017-09-13 00:58:47 +02:00
|
|
|
using android::base::StartsWith;
|
2015-07-24 02:53:11 +02:00
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2017-09-13 00:58:47 +02:00
|
|
|
Result<Success> RunBuiltinFunction(const BuiltinFunction& function,
|
|
|
|
const std::vector<std::string>& args,
|
|
|
|
const std::string& context) {
|
|
|
|
auto builtin_arguments = BuiltinArguments(context);
|
2015-07-24 02:53:11 +02:00
|
|
|
|
2017-09-13 00:58:47 +02:00
|
|
|
builtin_arguments.args.resize(args.size());
|
|
|
|
builtin_arguments.args[0] = args[0];
|
|
|
|
for (std::size_t i = 1; i < args.size(); ++i) {
|
|
|
|
if (!expand_props(args[i], &builtin_arguments.args[i])) {
|
|
|
|
return Error() << "cannot expand '" << args[i] << "'";
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 00:58:47 +02:00
|
|
|
return function(builtin_arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
Command::Command(BuiltinFunction f, bool execute_in_subcontext,
|
|
|
|
const std::vector<std::string>& args, int line)
|
|
|
|
: func_(std::move(f)), execute_in_subcontext_(execute_in_subcontext), args_(args), line_(line) {}
|
|
|
|
|
|
|
|
Result<Success> Command::InvokeFunc(Subcontext* subcontext) const {
|
2018-01-10 20:04:34 +01:00
|
|
|
if (subcontext) {
|
|
|
|
if (execute_in_subcontext_) {
|
|
|
|
return subcontext->Execute(args_);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto expanded_args = subcontext->ExpandArgs(args_);
|
|
|
|
if (!expanded_args) {
|
|
|
|
return expanded_args.error();
|
|
|
|
}
|
|
|
|
return RunBuiltinFunction(func_, *expanded_args, subcontext->context());
|
2017-09-13 00:58:47 +02:00
|
|
|
}
|
2018-01-10 20:04:34 +01:00
|
|
|
|
|
|
|
return RunBuiltinFunction(func_, args_, kInitContext);
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
std::string Command::BuildCommandString() const {
|
|
|
|
return Join(args_, ' ');
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 00:58:47 +02:00
|
|
|
Action::Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line)
|
|
|
|
: oneshot_(oneshot), subcontext_(subcontext), filename_(filename), line_(line) {}
|
2015-07-24 02:53:11 +02:00
|
|
|
|
2017-09-13 00:58:47 +02:00
|
|
|
const KeywordFunctionMap* Action::function_map_ = nullptr;
|
2015-08-26 20:43:36 +02:00
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> Action::AddCommand(const std::vector<std::string>& args, int line) {
|
2015-08-26 20:43:36 +02:00
|
|
|
if (!function_map_) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "no function map available";
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
auto function = function_map_->FindFunction(args);
|
|
|
|
if (!function) return Error() << function.error();
|
2015-08-26 20:43:36 +02:00
|
|
|
|
2017-09-13 00:58:47 +02:00
|
|
|
commands_.emplace_back(function->second, function->first, args, line);
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
init: Stop combining actions
In the past, I had thought it didn't make sense to have multiple
Action classes with identical triggers within ActionManager::actions_,
and opted to instead combine these into a single action. In theory,
it should reduce memory overhead as only one copy of the triggers
needs to be stored.
In practice, this ends up not being a good idea.
Most importantly, given a file with the below three sections in this
same order:
on boot
setprop a b
on boot && property:true=true
setprop c d
on boot
setprop e f
Assuming that property 'true' == 'true', when the `boot` event
happens, the order of the setprop commands will actually be:
setprop a b
setprop e f
setprop c d
instead of the more intuitive order of:
setprop a b
setprop c d
setprop e f
This is a mistake and this CL fixes it. It also documents this order.
Secondly, with a given 'Action' now spanning multiple files, in order
to keep track of which file a command is run from, the 'Command'
itself needs to store this. Ironically to the original intention,
this increases total ram usage. This change now only stores the file
name in each 'Action' instead of each 'Command'. All in all this is a
negligible trade off of ram usage.
Thirdly, this requires a bunch of extra code and assumptions that
don't help anything else. In particular it forces to keep property triggers
sorted for easy comparison, which I'm using an std::map for currently,
but that is not the best data structure to contain them.
Lastly, I added the filename and line number to the 'processing
action' LOG(INFO) message.
Test: Boot bullhead, observe above changes
Test: Boot sailfish, observe no change in boot time
Change-Id: I3fbcac4ee677351314e33012c758145be82346e9
2017-04-18 22:21:54 +02:00
|
|
|
void Action::AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line) {
|
2017-09-13 00:58:47 +02:00
|
|
|
commands_.emplace_back(f, false, args, line);
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
std::size_t Action::NumCommands() const {
|
2015-07-24 02:53:11 +02:00
|
|
|
return commands_.size();
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
void Action::ExecuteOneCommand(std::size_t command) const {
|
2016-11-16 21:08:30 +01:00
|
|
|
// We need a copy here since some Command execution may result in
|
|
|
|
// changing commands_ vector by importing .rc files through parser
|
|
|
|
Command cmd = commands_[command];
|
|
|
|
ExecuteCommand(cmd);
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
void Action::ExecuteAllCommands() const {
|
2015-07-24 02:53:11 +02:00
|
|
|
for (const auto& c : commands_) {
|
2015-08-26 20:43:36 +02:00
|
|
|
ExecuteCommand(c);
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
void Action::ExecuteCommand(const Command& command) const {
|
2017-07-06 23:20:11 +02:00
|
|
|
android::base::Timer t;
|
2017-09-13 00:58:47 +02:00
|
|
|
auto result = command.InvokeFunc(subcontext_);
|
2017-07-06 23:20:11 +02:00
|
|
|
auto duration = t.duration();
|
2017-08-01 22:50:23 +02:00
|
|
|
|
2017-08-23 01:15:26 +02:00
|
|
|
// There are many legacy paths in rootdir/init.rc that will virtually never exist on a new
|
|
|
|
// device, such as '/sys/class/leds/jogball-backlight/brightness'. As of this writing, there
|
|
|
|
// are 198 such failures on bullhead. Instead of spamming the log reporting them, we do not
|
|
|
|
// report such failures unless we're running at the DEBUG log level.
|
|
|
|
bool report_failure = !result.has_value();
|
|
|
|
if (report_failure && android::base::GetMinimumLogSeverity() > android::base::DEBUG &&
|
|
|
|
result.error_errno() == ENOENT) {
|
|
|
|
report_failure = false;
|
|
|
|
}
|
|
|
|
|
2016-11-16 08:58:55 +01:00
|
|
|
// Any action longer than 50ms will be warned to user as slow operation
|
2017-08-23 01:15:26 +02:00
|
|
|
if (report_failure || duration > 50ms ||
|
|
|
|
android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
|
2015-07-24 02:53:11 +02:00
|
|
|
std::string trigger_name = BuildTriggersString();
|
|
|
|
std::string cmd_str = command.BuildCommandString();
|
|
|
|
|
2017-06-23 01:50:31 +02:00
|
|
|
LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_
|
2017-08-01 22:50:23 +02:00
|
|
|
<< ":" << command.line() << ") took " << duration.count() << "ms and "
|
2017-08-23 01:07:15 +02:00
|
|
|
<< (result ? "succeeded" : "failed: " + result.error_string());
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 04:51:19 +01:00
|
|
|
static bool IsActionableProperty(Subcontext* subcontext, const std::string& prop_name) {
|
|
|
|
static bool enabled =
|
|
|
|
android::base::GetBoolProperty("ro.actionable_compatible_property.enabled", false);
|
|
|
|
|
|
|
|
if (subcontext == nullptr || !enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kExportedActionableProperties.count(prop_name) == 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (const auto& prefix : kPartnerPrefixes) {
|
|
|
|
if (android::base::StartsWith(prop_name, prefix)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> Action::ParsePropertyTrigger(const std::string& trigger) {
|
2015-07-24 02:53:11 +02:00
|
|
|
const static std::string prop_str("property:");
|
|
|
|
std::string prop_name(trigger.substr(prop_str.length()));
|
|
|
|
size_t equal_pos = prop_name.find('=');
|
|
|
|
if (equal_pos == std::string::npos) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "property trigger found without matching '='";
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string prop_value(prop_name.substr(equal_pos + 1));
|
|
|
|
prop_name.erase(equal_pos);
|
|
|
|
|
2018-01-16 04:51:19 +01:00
|
|
|
if (!IsActionableProperty(subcontext_, prop_name)) {
|
|
|
|
return Error() << "unexported property tigger found: " << prop_name;
|
|
|
|
}
|
|
|
|
|
2017-03-13 19:58:58 +01:00
|
|
|
if (auto [it, inserted] = property_triggers_.emplace(prop_name, prop_value); !inserted) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "multiple property triggers found for same property";
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> Action::InitTriggers(const std::vector<std::string>& args) {
|
2015-07-24 02:53:11 +02:00
|
|
|
const static std::string prop_str("property:");
|
|
|
|
for (std::size_t i = 0; i < args.size(); ++i) {
|
2016-11-17 04:19:49 +01:00
|
|
|
if (args[i].empty()) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "empty trigger is not valid";
|
2016-11-17 04:19:49 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 02:53:11 +02:00
|
|
|
if (i % 2) {
|
2015-08-11 21:34:22 +02:00
|
|
|
if (args[i] != "&&") {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "&& is the only symbol allowed to concatenate actions";
|
2015-07-24 02:53:11 +02:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!args[i].compare(0, prop_str.length(), prop_str)) {
|
2017-08-03 02:01:36 +02:00
|
|
|
if (auto result = ParsePropertyTrigger(args[i]); !result) {
|
|
|
|
return result;
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!event_trigger_.empty()) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "multiple event triggers are not allowed";
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
event_trigger_ = args[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> Action::InitSingleTrigger(const std::string& trigger) {
|
2015-07-24 02:53:11 +02:00
|
|
|
std::vector<std::string> name_vector{trigger};
|
2017-08-03 02:01:36 +02:00
|
|
|
if (auto result = InitTriggers(name_vector); !result) {
|
|
|
|
return Error() << "InitTriggers() failed: " << result.error();
|
2016-11-17 04:19:49 +01:00
|
|
|
}
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
// This function checks that all property triggers are satisfied, that is
|
|
|
|
// for each (name, value) in property_triggers_, check that the current
|
|
|
|
// value of the property 'name' == value.
|
|
|
|
//
|
|
|
|
// It takes an optional (name, value) pair, which if provided must
|
|
|
|
// be present in property_triggers_; it skips the check of the current
|
|
|
|
// property value for this pair.
|
2015-07-24 02:53:11 +02:00
|
|
|
bool Action::CheckPropertyTriggers(const std::string& name,
|
2015-08-26 20:43:36 +02:00
|
|
|
const std::string& value) const {
|
2015-07-24 02:53:11 +02:00
|
|
|
if (property_triggers_.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
bool found = name.empty();
|
2017-03-13 19:58:58 +01:00
|
|
|
for (const auto& [trigger_name, trigger_value] : property_triggers_) {
|
2015-08-11 21:34:22 +02:00
|
|
|
if (trigger_name == name) {
|
|
|
|
if (trigger_value != "*" && trigger_value != value) {
|
2015-07-24 02:53:11 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-29 01:40:41 +02:00
|
|
|
std::string prop_val = android::base::GetProperty(trigger_name, "");
|
|
|
|
if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) {
|
2015-07-24 02:53:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2017-04-17 22:25:29 +02:00
|
|
|
bool Action::CheckEvent(const EventTrigger& event_trigger) const {
|
|
|
|
return event_trigger == event_trigger_ && CheckPropertyTriggers();
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2017-04-17 22:25:29 +02:00
|
|
|
bool Action::CheckEvent(const PropertyChange& property_change) const {
|
|
|
|
const auto& [name, value] = property_change;
|
2015-07-24 02:53:11 +02:00
|
|
|
return event_trigger_.empty() && CheckPropertyTriggers(name, value);
|
|
|
|
}
|
|
|
|
|
2017-04-17 22:25:29 +02:00
|
|
|
bool Action::CheckEvent(const BuiltinAction& builtin_action) const {
|
|
|
|
return this == builtin_action;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
std::string Action::BuildTriggersString() const {
|
2017-03-13 20:24:49 +01:00
|
|
|
std::vector<std::string> triggers;
|
2015-07-24 02:53:11 +02:00
|
|
|
|
2017-03-13 19:58:58 +01:00
|
|
|
for (const auto& [trigger_name, trigger_value] : property_triggers_) {
|
2017-03-13 20:24:49 +01:00
|
|
|
triggers.emplace_back(trigger_name + '=' + trigger_value);
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
if (!event_trigger_.empty()) {
|
2017-03-13 20:24:49 +01:00
|
|
|
triggers.emplace_back(event_trigger_);
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
2017-03-13 20:24:49 +01:00
|
|
|
|
|
|
|
return Join(triggers, " && ");
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
void Action::DumpState() const {
|
2015-07-24 02:53:11 +02:00
|
|
|
std::string trigger_name = BuildTriggersString();
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "on " << trigger_name;
|
2015-07-24 02:53:11 +02:00
|
|
|
|
|
|
|
for (const auto& c : commands_) {
|
2015-08-26 20:43:36 +02:00
|
|
|
std::string cmd_str = c.BuildCommandString();
|
2017-03-13 20:24:49 +01:00
|
|
|
LOG(INFO) << " " << cmd_str;
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
ActionManager::ActionManager() : current_command_(0) {
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ActionManager& ActionManager::GetInstance() {
|
|
|
|
static ActionManager instance;
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
void ActionManager::AddAction(std::unique_ptr<Action> action) {
|
init: Stop combining actions
In the past, I had thought it didn't make sense to have multiple
Action classes with identical triggers within ActionManager::actions_,
and opted to instead combine these into a single action. In theory,
it should reduce memory overhead as only one copy of the triggers
needs to be stored.
In practice, this ends up not being a good idea.
Most importantly, given a file with the below three sections in this
same order:
on boot
setprop a b
on boot && property:true=true
setprop c d
on boot
setprop e f
Assuming that property 'true' == 'true', when the `boot` event
happens, the order of the setprop commands will actually be:
setprop a b
setprop e f
setprop c d
instead of the more intuitive order of:
setprop a b
setprop c d
setprop e f
This is a mistake and this CL fixes it. It also documents this order.
Secondly, with a given 'Action' now spanning multiple files, in order
to keep track of which file a command is run from, the 'Command'
itself needs to store this. Ironically to the original intention,
this increases total ram usage. This change now only stores the file
name in each 'Action' instead of each 'Command'. All in all this is a
negligible trade off of ram usage.
Thirdly, this requires a bunch of extra code and assumptions that
don't help anything else. In particular it forces to keep property triggers
sorted for easy comparison, which I'm using an std::map for currently,
but that is not the best data structure to contain them.
Lastly, I added the filename and line number to the 'processing
action' LOG(INFO) message.
Test: Boot bullhead, observe above changes
Test: Boot sailfish, observe no change in boot time
Change-Id: I3fbcac4ee677351314e33012c758145be82346e9
2017-04-18 22:21:54 +02:00
|
|
|
actions_.emplace_back(std::move(action));
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionManager::QueueEventTrigger(const std::string& trigger) {
|
2017-04-17 22:25:29 +02:00
|
|
|
event_queue_.emplace(trigger);
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2017-04-17 22:25:29 +02:00
|
|
|
void ActionManager::QueuePropertyChange(const std::string& name, const std::string& value) {
|
|
|
|
event_queue_.emplace(std::make_pair(name, value));
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2017-04-17 22:25:29 +02:00
|
|
|
void ActionManager::QueueAllPropertyActions() {
|
|
|
|
QueuePropertyChange("", "");
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
init: Stop combining actions
In the past, I had thought it didn't make sense to have multiple
Action classes with identical triggers within ActionManager::actions_,
and opted to instead combine these into a single action. In theory,
it should reduce memory overhead as only one copy of the triggers
needs to be stored.
In practice, this ends up not being a good idea.
Most importantly, given a file with the below three sections in this
same order:
on boot
setprop a b
on boot && property:true=true
setprop c d
on boot
setprop e f
Assuming that property 'true' == 'true', when the `boot` event
happens, the order of the setprop commands will actually be:
setprop a b
setprop e f
setprop c d
instead of the more intuitive order of:
setprop a b
setprop c d
setprop e f
This is a mistake and this CL fixes it. It also documents this order.
Secondly, with a given 'Action' now spanning multiple files, in order
to keep track of which file a command is run from, the 'Command'
itself needs to store this. Ironically to the original intention,
this increases total ram usage. This change now only stores the file
name in each 'Action' instead of each 'Command'. All in all this is a
negligible trade off of ram usage.
Thirdly, this requires a bunch of extra code and assumptions that
don't help anything else. In particular it forces to keep property triggers
sorted for easy comparison, which I'm using an std::map for currently,
but that is not the best data structure to contain them.
Lastly, I added the filename and line number to the 'processing
action' LOG(INFO) message.
Test: Boot bullhead, observe above changes
Test: Boot sailfish, observe no change in boot time
Change-Id: I3fbcac4ee677351314e33012c758145be82346e9
2017-04-18 22:21:54 +02:00
|
|
|
void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) {
|
2017-09-13 00:58:47 +02:00
|
|
|
auto action = std::make_unique<Action>(true, nullptr, "<Builtin Action>", 0);
|
2015-07-24 02:53:11 +02:00
|
|
|
std::vector<std::string> name_vector{name};
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
if (auto result = action->InitSingleTrigger(name); !result) {
|
|
|
|
LOG(ERROR) << "Cannot queue BuiltinAction for " << name << ": " << result.error();
|
2015-07-24 02:53:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
init: Stop combining actions
In the past, I had thought it didn't make sense to have multiple
Action classes with identical triggers within ActionManager::actions_,
and opted to instead combine these into a single action. In theory,
it should reduce memory overhead as only one copy of the triggers
needs to be stored.
In practice, this ends up not being a good idea.
Most importantly, given a file with the below three sections in this
same order:
on boot
setprop a b
on boot && property:true=true
setprop c d
on boot
setprop e f
Assuming that property 'true' == 'true', when the `boot` event
happens, the order of the setprop commands will actually be:
setprop a b
setprop e f
setprop c d
instead of the more intuitive order of:
setprop a b
setprop c d
setprop e f
This is a mistake and this CL fixes it. It also documents this order.
Secondly, with a given 'Action' now spanning multiple files, in order
to keep track of which file a command is run from, the 'Command'
itself needs to store this. Ironically to the original intention,
this increases total ram usage. This change now only stores the file
name in each 'Action' instead of each 'Command'. All in all this is a
negligible trade off of ram usage.
Thirdly, this requires a bunch of extra code and assumptions that
don't help anything else. In particular it forces to keep property triggers
sorted for easy comparison, which I'm using an std::map for currently,
but that is not the best data structure to contain them.
Lastly, I added the filename and line number to the 'processing
action' LOG(INFO) message.
Test: Boot bullhead, observe above changes
Test: Boot sailfish, observe no change in boot time
Change-Id: I3fbcac4ee677351314e33012c758145be82346e9
2017-04-18 22:21:54 +02:00
|
|
|
action->AddCommand(func, name_vector, 0);
|
2015-07-24 02:53:11 +02:00
|
|
|
|
2017-04-17 22:25:29 +02:00
|
|
|
event_queue_.emplace(action.get());
|
2015-08-26 20:43:36 +02:00
|
|
|
actions_.emplace_back(std::move(action));
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionManager::ExecuteOneCommand() {
|
2017-04-17 22:25:29 +02:00
|
|
|
// Loop through the event queue until we have an action to execute
|
|
|
|
while (current_executing_actions_.empty() && !event_queue_.empty()) {
|
2015-08-26 20:43:36 +02:00
|
|
|
for (const auto& action : actions_) {
|
2017-04-17 22:25:29 +02:00
|
|
|
if (std::visit([&action](const auto& event) { return action->CheckEvent(event); },
|
|
|
|
event_queue_.front())) {
|
2015-08-26 20:43:36 +02:00
|
|
|
current_executing_actions_.emplace(action.get());
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 22:25:29 +02:00
|
|
|
event_queue_.pop();
|
2015-08-11 21:34:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (current_executing_actions_.empty()) {
|
2015-07-24 02:53:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
auto action = current_executing_actions_.front();
|
2015-07-24 02:53:11 +02:00
|
|
|
|
2015-08-11 21:34:22 +02:00
|
|
|
if (current_command_ == 0) {
|
2015-07-24 02:53:11 +02:00
|
|
|
std::string trigger_name = action->BuildTriggersString();
|
init: Stop combining actions
In the past, I had thought it didn't make sense to have multiple
Action classes with identical triggers within ActionManager::actions_,
and opted to instead combine these into a single action. In theory,
it should reduce memory overhead as only one copy of the triggers
needs to be stored.
In practice, this ends up not being a good idea.
Most importantly, given a file with the below three sections in this
same order:
on boot
setprop a b
on boot && property:true=true
setprop c d
on boot
setprop e f
Assuming that property 'true' == 'true', when the `boot` event
happens, the order of the setprop commands will actually be:
setprop a b
setprop e f
setprop c d
instead of the more intuitive order of:
setprop a b
setprop c d
setprop e f
This is a mistake and this CL fixes it. It also documents this order.
Secondly, with a given 'Action' now spanning multiple files, in order
to keep track of which file a command is run from, the 'Command'
itself needs to store this. Ironically to the original intention,
this increases total ram usage. This change now only stores the file
name in each 'Action' instead of each 'Command'. All in all this is a
negligible trade off of ram usage.
Thirdly, this requires a bunch of extra code and assumptions that
don't help anything else. In particular it forces to keep property triggers
sorted for easy comparison, which I'm using an std::map for currently,
but that is not the best data structure to contain them.
Lastly, I added the filename and line number to the 'processing
action' LOG(INFO) message.
Test: Boot bullhead, observe above changes
Test: Boot sailfish, observe no change in boot time
Change-Id: I3fbcac4ee677351314e33012c758145be82346e9
2017-04-18 22:21:54 +02:00
|
|
|
LOG(INFO) << "processing action (" << trigger_name << ") from (" << action->filename()
|
|
|
|
<< ":" << action->line() << ")";
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
action->ExecuteOneCommand(current_command_);
|
|
|
|
|
|
|
|
// If this was the last command in the current action, then remove
|
|
|
|
// the action from the executing list.
|
|
|
|
// If this action was oneshot, then also remove it from actions_.
|
|
|
|
++current_command_;
|
2015-08-11 21:34:22 +02:00
|
|
|
if (current_command_ == action->NumCommands()) {
|
2015-08-26 20:43:36 +02:00
|
|
|
current_executing_actions_.pop();
|
2015-08-11 21:34:22 +02:00
|
|
|
current_command_ = 0;
|
2015-08-26 20:43:36 +02:00
|
|
|
if (action->oneshot()) {
|
|
|
|
auto eraser = [&action] (std::unique_ptr<Action>& a) {
|
|
|
|
return a.get() == action;
|
|
|
|
};
|
|
|
|
actions_.erase(std::remove_if(actions_.begin(), actions_.end(), eraser));
|
|
|
|
}
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
bool ActionManager::HasMoreCommands() const {
|
2017-04-17 22:25:29 +02:00
|
|
|
return !current_executing_actions_.empty() || !event_queue_.empty();
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
void ActionManager::DumpState() const {
|
|
|
|
for (const auto& a : actions_) {
|
|
|
|
a->DumpState();
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
2015-07-24 02:53:11 +02:00
|
|
|
|
2017-06-28 07:08:45 +02:00
|
|
|
void ActionManager::ClearQueue() {
|
|
|
|
// We are shutting down so don't claim the oneshot builtin actions back
|
|
|
|
current_executing_actions_ = {};
|
|
|
|
event_queue_ = {};
|
|
|
|
current_command_ = 0;
|
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> ActionParser::ParseSection(std::vector<std::string>&& args,
|
|
|
|
const std::string& filename, int line) {
|
2015-08-26 20:43:36 +02:00
|
|
|
std::vector<std::string> triggers(args.begin() + 1, args.end());
|
|
|
|
if (triggers.size() < 1) {
|
2017-08-03 02:01:36 +02:00
|
|
|
return Error() << "Actions must have a trigger";
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 00:58:47 +02:00
|
|
|
Subcontext* action_subcontext = nullptr;
|
|
|
|
if (subcontexts_) {
|
|
|
|
for (auto& subcontext : *subcontexts_) {
|
2017-12-20 18:41:00 +01:00
|
|
|
if (StartsWith(filename, subcontext.path_prefix())) {
|
2017-09-13 00:58:47 +02:00
|
|
|
action_subcontext = &subcontext;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto action = std::make_unique<Action>(false, action_subcontext, filename, line);
|
2017-08-03 02:01:36 +02:00
|
|
|
|
|
|
|
if (auto result = action->InitTriggers(triggers); !result) {
|
|
|
|
return Error() << "InitTriggers() failed: " << result.error();
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
action_ = std::move(action);
|
2017-08-03 02:01:36 +02:00
|
|
|
return Success();
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 02:01:36 +02:00
|
|
|
Result<Success> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) {
|
|
|
|
return action_ ? action_->AddCommand(std::move(args), line) : Success();
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 23:20:47 +01:00
|
|
|
Result<Success> ActionParser::EndSection() {
|
2015-08-26 20:43:36 +02:00
|
|
|
if (action_ && action_->NumCommands() > 0) {
|
2017-04-20 00:31:58 +02:00
|
|
|
action_manager_->AddAction(std::move(action_));
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
2017-11-10 23:20:47 +01:00
|
|
|
|
|
|
|
return Success();
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
2017-06-22 21:53:17 +02:00
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|