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>
|
2018-05-25 03:00:39 +02:00
|
|
|
#include <android-base/properties.h>
|
2017-03-29 01:40:41 +02:00
|
|
|
#include <android-base/strings.h>
|
2015-07-24 02:53:11 +02:00
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
using android::base::Join;
|
2015-07-24 02:53:11 +02:00
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> RunBuiltinFunction(const BuiltinFunction& function,
|
|
|
|
const std::vector<std::string>& args, const std::string& context) {
|
2017-09-13 00:58:47 +02:00
|
|
|
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) {
|
2019-07-31 22:59:15 +02:00
|
|
|
auto expanded_arg = ExpandProps(args[i]);
|
|
|
|
if (!expanded_arg) {
|
|
|
|
return expanded_arg.error();
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
2019-07-31 22:59:15 +02:00
|
|
|
builtin_arguments.args[i] = std::move(*expanded_arg);
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 00:58:47 +02:00
|
|
|
return function(builtin_arguments);
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
Command::Command(BuiltinFunction f, bool execute_in_subcontext, std::vector<std::string>&& args,
|
|
|
|
int line)
|
|
|
|
: func_(std::move(f)),
|
|
|
|
execute_in_subcontext_(execute_in_subcontext),
|
|
|
|
args_(std::move(args)),
|
|
|
|
line_(line) {}
|
2017-09-13 00:58:47 +02:00
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> 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
|
|
|
}
|
|
|
|
|
2019-07-30 18:34:41 +02:00
|
|
|
Result<void> Command::CheckCommand() const {
|
|
|
|
auto builtin_arguments = BuiltinArguments("host_init_verifier");
|
|
|
|
|
|
|
|
builtin_arguments.args.resize(args_.size());
|
|
|
|
builtin_arguments.args[0] = args_[0];
|
|
|
|
for (size_t i = 1; i < args_.size(); ++i) {
|
|
|
|
auto expanded_arg = ExpandProps(args_[i]);
|
|
|
|
if (!expanded_arg) {
|
|
|
|
if (expanded_arg.error().message().find("doesn't exist while expanding") !=
|
|
|
|
std::string::npos) {
|
|
|
|
// If we failed because we won't have a property, use an empty string, which is
|
|
|
|
// never returned from the parser, to indicate that this field cannot be checked.
|
|
|
|
builtin_arguments.args[i] = "";
|
|
|
|
} else {
|
|
|
|
return expanded_arg.error();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
builtin_arguments.args[i] = std::move(*expanded_arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return func_(builtin_arguments);
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
std::string Command::BuildCommandString() const {
|
|
|
|
return Join(args_, ' ');
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
2018-02-14 01:24:51 +01:00
|
|
|
Action::Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line,
|
|
|
|
const std::string& event_trigger,
|
|
|
|
const std::map<std::string, std::string>& property_triggers)
|
|
|
|
: property_triggers_(property_triggers),
|
|
|
|
event_trigger_(event_trigger),
|
|
|
|
oneshot_(oneshot),
|
|
|
|
subcontext_(subcontext),
|
|
|
|
filename_(filename),
|
|
|
|
line_(line) {}
|
2015-07-24 02:53:11 +02:00
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
const BuiltinFunctionMap* Action::function_map_ = nullptr;
|
2015-08-26 20:43:36 +02:00
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> Action::AddCommand(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
|
|
|
}
|
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
auto map_result = function_map_->Find(args);
|
|
|
|
if (!map_result) {
|
|
|
|
return Error() << map_result.error();
|
|
|
|
}
|
2015-08-26 20:43:36 +02:00
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
commands_.emplace_back(map_result->function, map_result->run_in_subcontext, std::move(args),
|
|
|
|
line);
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2015-08-26 20:43:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:11:23 +02:00
|
|
|
void Action::AddCommand(BuiltinFunction f, std::vector<std::string>&& args, int line) {
|
2019-07-10 20:18:24 +02:00
|
|
|
commands_.emplace_back(std::move(f), false, std::move(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();
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:34:41 +02:00
|
|
|
size_t Action::CheckAllCommands() const {
|
|
|
|
size_t failures = 0;
|
|
|
|
for (const auto& command : commands_) {
|
|
|
|
if (auto result = command.CheckCommand(); !result) {
|
|
|
|
LOG(ERROR) << "Command '" << command.BuildCommandString() << "' (" << filename_ << ":"
|
|
|
|
<< command.line() << ") failed: " << result.error();
|
|
|
|
++failures;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return failures;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-11-16 08:58:55 +01:00
|
|
|
// Any action longer than 50ms will be warned to user as slow operation
|
2019-07-30 19:51:59 +02:00
|
|
|
if (!result.has_value() || duration > 50ms ||
|
2017-08-23 01:15:26 +02:00
|
|
|
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 "
|
2019-05-30 20:43:34 +02:00
|
|
|
<< (result ? "succeeded" : "failed: " + result.error().message());
|
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.
|
2019-11-14 18:19:07 +01:00
|
|
|
bool Action::CheckPropertyTriggers(const std::string& name, const std::string& value) const {
|
2015-07-24 02:53:11 +02:00
|
|
|
if (property_triggers_.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:19:07 +01:00
|
|
|
if (!name.empty()) {
|
|
|
|
auto it = property_triggers_.find(name);
|
|
|
|
if (it == property_triggers_.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto& trigger_value = it->second;
|
|
|
|
if (trigger_value != "*" && trigger_value != value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 19:58:58 +01:00
|
|
|
for (const auto& [trigger_name, trigger_value] : property_triggers_) {
|
2019-11-14 18:19:07 +01:00
|
|
|
if (trigger_name != name) {
|
2019-08-12 18:26:20 +02:00
|
|
|
std::string prop_value = android::base::GetProperty(trigger_name, "");
|
|
|
|
if (trigger_value == "*" && !prop_value.empty()) {
|
|
|
|
continue;
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
2019-08-12 18:26:20 +02:00
|
|
|
if (trigger_value != prop_value) return false;
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 18:19:07 +01:00
|
|
|
return true;
|
2015-07-24 02:53:11 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|