Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 1 | /* |
| 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 Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 19 | #include <android-base/chrono_utils.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 20 | #include <android-base/logging.h> |
Elliott Hughes | dc80312 | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 21 | #include <android-base/properties.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 22 | #include <android-base/strings.h> |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 23 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 24 | #include "util.h" |
| 25 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 26 | using android::base::Join; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 27 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace init { |
| 30 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 31 | Result<void> RunBuiltinFunction(const BuiltinFunction& function, |
| 32 | const std::vector<std::string>& args, const std::string& context) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 33 | auto builtin_arguments = BuiltinArguments(context); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 34 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 35 | builtin_arguments.args.resize(args.size()); |
| 36 | builtin_arguments.args[0] = args[0]; |
| 37 | for (std::size_t i = 1; i < args.size(); ++i) { |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 38 | auto expanded_arg = ExpandProps(args[i]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 39 | if (!expanded_arg.ok()) { |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 40 | return expanded_arg.error(); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 41 | } |
Tom Cherry | c5cf85d | 2019-07-31 13:59:15 -0700 | [diff] [blame] | 42 | builtin_arguments.args[i] = std::move(*expanded_arg); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 45 | return function(builtin_arguments); |
| 46 | } |
| 47 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 48 | Command::Command(BuiltinFunction f, bool execute_in_subcontext, std::vector<std::string>&& args, |
| 49 | int line) |
| 50 | : func_(std::move(f)), |
| 51 | execute_in_subcontext_(execute_in_subcontext), |
| 52 | args_(std::move(args)), |
| 53 | line_(line) {} |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 54 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 55 | Result<void> Command::InvokeFunc(Subcontext* subcontext) const { |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 56 | if (subcontext) { |
| 57 | if (execute_in_subcontext_) { |
| 58 | return subcontext->Execute(args_); |
| 59 | } |
| 60 | |
| 61 | auto expanded_args = subcontext->ExpandArgs(args_); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 62 | if (!expanded_args.ok()) { |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 63 | return expanded_args.error(); |
| 64 | } |
| 65 | return RunBuiltinFunction(func_, *expanded_args, subcontext->context()); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 66 | } |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 67 | |
| 68 | return RunBuiltinFunction(func_, args_, kInitContext); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 71 | Result<void> Command::CheckCommand() const { |
| 72 | auto builtin_arguments = BuiltinArguments("host_init_verifier"); |
| 73 | |
| 74 | builtin_arguments.args.resize(args_.size()); |
| 75 | builtin_arguments.args[0] = args_[0]; |
| 76 | for (size_t i = 1; i < args_.size(); ++i) { |
| 77 | auto expanded_arg = ExpandProps(args_[i]); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 78 | if (!expanded_arg.ok()) { |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 79 | if (expanded_arg.error().message().find("doesn't exist while expanding") != |
| 80 | std::string::npos) { |
| 81 | // If we failed because we won't have a property, use an empty string, which is |
| 82 | // never returned from the parser, to indicate that this field cannot be checked. |
| 83 | builtin_arguments.args[i] = ""; |
| 84 | } else { |
| 85 | return expanded_arg.error(); |
| 86 | } |
| 87 | } else { |
| 88 | builtin_arguments.args[i] = std::move(*expanded_arg); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return func_(builtin_arguments); |
| 93 | } |
| 94 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 95 | std::string Command::BuildCommandString() const { |
| 96 | return Join(args_, ' '); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 99 | Action::Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line, |
| 100 | const std::string& event_trigger, |
| 101 | const std::map<std::string, std::string>& property_triggers) |
| 102 | : property_triggers_(property_triggers), |
| 103 | event_trigger_(event_trigger), |
| 104 | oneshot_(oneshot), |
| 105 | subcontext_(subcontext), |
| 106 | filename_(filename), |
| 107 | line_(line) {} |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 108 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 109 | const BuiltinFunctionMap* Action::function_map_ = nullptr; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 110 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 111 | Result<void> Action::AddCommand(std::vector<std::string>&& args, int line) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 112 | if (!function_map_) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 113 | return Error() << "no function map available"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 116 | auto map_result = function_map_->Find(args); |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 117 | if (!map_result.ok()) { |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 118 | return Error() << map_result.error(); |
| 119 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 120 | |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 121 | commands_.emplace_back(map_result->function, map_result->run_in_subcontext, std::move(args), |
| 122 | line); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 123 | return {}; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Tom Cherry | 018a438 | 2018-10-17 11:11:23 -0700 | [diff] [blame] | 126 | void Action::AddCommand(BuiltinFunction f, std::vector<std::string>&& args, int line) { |
Tom Cherry | 7c1d87e | 2019-07-10 11:18:24 -0700 | [diff] [blame] | 127 | commands_.emplace_back(std::move(f), false, std::move(args), line); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 130 | std::size_t Action::NumCommands() const { |
| 131 | return commands_.size(); |
| 132 | } |
| 133 | |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 134 | size_t Action::CheckAllCommands() const { |
| 135 | size_t failures = 0; |
| 136 | for (const auto& command : commands_) { |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 137 | if (auto result = command.CheckCommand(); !result.ok()) { |
Tom Cherry | 4772f1d | 2019-07-30 09:34:41 -0700 | [diff] [blame] | 138 | LOG(ERROR) << "Command '" << command.BuildCommandString() << "' (" << filename_ << ":" |
| 139 | << command.line() << ") failed: " << result.error(); |
| 140 | ++failures; |
| 141 | } |
| 142 | } |
| 143 | return failures; |
| 144 | } |
| 145 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 146 | void Action::ExecuteOneCommand(std::size_t command) const { |
Wei Wang | d67a4ab | 2016-11-16 12:08:30 -0800 | [diff] [blame] | 147 | // We need a copy here since some Command execution may result in |
| 148 | // changing commands_ vector by importing .rc files through parser |
| 149 | Command cmd = commands_[command]; |
| 150 | ExecuteCommand(cmd); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void Action::ExecuteAllCommands() const { |
| 154 | for (const auto& c : commands_) { |
| 155 | ExecuteCommand(c); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void Action::ExecuteCommand(const Command& command) const { |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 160 | android::base::Timer t; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 161 | auto result = command.InvokeFunc(subcontext_); |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 162 | auto duration = t.duration(); |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 163 | |
Wei Wang | 8b1d526 | 2016-11-15 23:58:55 -0800 | [diff] [blame] | 164 | // Any action longer than 50ms will be warned to user as slow operation |
Tom Cherry | d17c379 | 2019-07-30 10:51:59 -0700 | [diff] [blame] | 165 | if (!result.has_value() || duration > 50ms || |
Tom Cherry | 68f2a46 | 2017-08-22 16:15:26 -0700 | [diff] [blame] | 166 | android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 167 | std::string trigger_name = BuildTriggersString(); |
| 168 | std::string cmd_str = command.BuildCommandString(); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 169 | |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 170 | LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_ |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 171 | << ":" << command.line() << ") took " << duration.count() << "ms and " |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 172 | << (result.ok() ? "succeeded" : "failed: " + result.error().message()); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 176 | // This function checks that all property triggers are satisfied, that is |
| 177 | // for each (name, value) in property_triggers_, check that the current |
| 178 | // value of the property 'name' == value. |
| 179 | // |
| 180 | // It takes an optional (name, value) pair, which if provided must |
| 181 | // be present in property_triggers_; it skips the check of the current |
| 182 | // property value for this pair. |
Tom Cherry | 1efc484 | 2019-11-14 09:19:07 -0800 | [diff] [blame] | 183 | bool Action::CheckPropertyTriggers(const std::string& name, const std::string& value) const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 184 | if (property_triggers_.empty()) { |
| 185 | return true; |
| 186 | } |
| 187 | |
Tom Cherry | 1efc484 | 2019-11-14 09:19:07 -0800 | [diff] [blame] | 188 | if (!name.empty()) { |
| 189 | auto it = property_triggers_.find(name); |
| 190 | if (it == property_triggers_.end()) { |
| 191 | return false; |
| 192 | } |
| 193 | const auto& trigger_value = it->second; |
| 194 | if (trigger_value != "*" && trigger_value != value) { |
| 195 | return false; |
| 196 | } |
| 197 | } |
| 198 | |
Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 199 | for (const auto& [trigger_name, trigger_value] : property_triggers_) { |
Tom Cherry | 1efc484 | 2019-11-14 09:19:07 -0800 | [diff] [blame] | 200 | if (trigger_name != name) { |
Tom Cherry | 6fd8d3b | 2019-08-12 09:26:20 -0700 | [diff] [blame] | 201 | std::string prop_value = android::base::GetProperty(trigger_name, ""); |
| 202 | if (trigger_value == "*" && !prop_value.empty()) { |
| 203 | continue; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 204 | } |
Tom Cherry | 6fd8d3b | 2019-08-12 09:26:20 -0700 | [diff] [blame] | 205 | if (trigger_value != prop_value) return false; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
Tom Cherry | 1efc484 | 2019-11-14 09:19:07 -0800 | [diff] [blame] | 208 | return true; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 211 | bool Action::CheckEvent(const EventTrigger& event_trigger) const { |
| 212 | return event_trigger == event_trigger_ && CheckPropertyTriggers(); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 215 | bool Action::CheckEvent(const PropertyChange& property_change) const { |
| 216 | const auto& [name, value] = property_change; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 217 | return event_trigger_.empty() && CheckPropertyTriggers(name, value); |
| 218 | } |
| 219 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 220 | bool Action::CheckEvent(const BuiltinAction& builtin_action) const { |
| 221 | return this == builtin_action; |
| 222 | } |
| 223 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 224 | std::string Action::BuildTriggersString() const { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 225 | std::vector<std::string> triggers; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 226 | |
Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 227 | for (const auto& [trigger_name, trigger_value] : property_triggers_) { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 228 | triggers.emplace_back(trigger_name + '=' + trigger_value); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 229 | } |
| 230 | if (!event_trigger_.empty()) { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 231 | triggers.emplace_back(event_trigger_); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 232 | } |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 233 | |
| 234 | return Join(triggers, " && "); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 237 | void Action::DumpState() const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 238 | std::string trigger_name = BuildTriggersString(); |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 239 | LOG(INFO) << "on " << trigger_name; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 240 | |
| 241 | for (const auto& c : commands_) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 242 | std::string cmd_str = c.BuildCommandString(); |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 243 | LOG(INFO) << " " << cmd_str; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 244 | } |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 247 | } // namespace init |
| 248 | } // namespace android |