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> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -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 | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 31 | Result<Success> RunBuiltinFunction(const BuiltinFunction& function, |
| 32 | const std::vector<std::string>& args, |
| 33 | const std::string& context) { |
| 34 | auto builtin_arguments = BuiltinArguments(context); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 35 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 36 | builtin_arguments.args.resize(args.size()); |
| 37 | builtin_arguments.args[0] = args[0]; |
| 38 | for (std::size_t i = 1; i < args.size(); ++i) { |
| 39 | if (!expand_props(args[i], &builtin_arguments.args[i])) { |
| 40 | return Error() << "cannot expand '" << args[i] << "'"; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 44 | return function(builtin_arguments); |
| 45 | } |
| 46 | |
| 47 | Command::Command(BuiltinFunction f, bool execute_in_subcontext, |
| 48 | const std::vector<std::string>& args, int line) |
| 49 | : func_(std::move(f)), execute_in_subcontext_(execute_in_subcontext), args_(args), line_(line) {} |
| 50 | |
| 51 | Result<Success> Command::InvokeFunc(Subcontext* subcontext) const { |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 52 | if (subcontext) { |
| 53 | if (execute_in_subcontext_) { |
| 54 | return subcontext->Execute(args_); |
| 55 | } |
| 56 | |
| 57 | auto expanded_args = subcontext->ExpandArgs(args_); |
| 58 | if (!expanded_args) { |
| 59 | return expanded_args.error(); |
| 60 | } |
| 61 | return RunBuiltinFunction(func_, *expanded_args, subcontext->context()); |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 62 | } |
Tom Cherry | c49719f | 2018-01-10 11:04:34 -0800 | [diff] [blame] | 63 | |
| 64 | return RunBuiltinFunction(func_, args_, kInitContext); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 67 | std::string Command::BuildCommandString() const { |
| 68 | return Join(args_, ' '); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 71 | Action::Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line, |
| 72 | const std::string& event_trigger, |
| 73 | const std::map<std::string, std::string>& property_triggers) |
| 74 | : property_triggers_(property_triggers), |
| 75 | event_trigger_(event_trigger), |
| 76 | oneshot_(oneshot), |
| 77 | subcontext_(subcontext), |
| 78 | filename_(filename), |
| 79 | line_(line) {} |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 80 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 81 | const KeywordFunctionMap* Action::function_map_ = nullptr; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 82 | |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 83 | Result<Success> Action::AddCommand(const std::vector<std::string>& args, int line) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 84 | if (!function_map_) { |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 85 | return Error() << "no function map available"; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 88 | auto function = function_map_->FindFunction(args); |
| 89 | if (!function) return Error() << function.error(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 90 | |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 91 | commands_.emplace_back(function->second, function->first, args, line); |
Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 92 | return Success(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 95 | void Action::AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line) { |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 96 | commands_.emplace_back(f, false, args, line); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 99 | std::size_t Action::NumCommands() const { |
| 100 | return commands_.size(); |
| 101 | } |
| 102 | |
| 103 | void Action::ExecuteOneCommand(std::size_t command) const { |
Wei Wang | d67a4ab | 2016-11-16 12:08:30 -0800 | [diff] [blame] | 104 | // We need a copy here since some Command execution may result in |
| 105 | // changing commands_ vector by importing .rc files through parser |
| 106 | Command cmd = commands_[command]; |
| 107 | ExecuteCommand(cmd); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void Action::ExecuteAllCommands() const { |
| 111 | for (const auto& c : commands_) { |
| 112 | ExecuteCommand(c); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void Action::ExecuteCommand(const Command& command) const { |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 117 | android::base::Timer t; |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 118 | auto result = command.InvokeFunc(subcontext_); |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 119 | auto duration = t.duration(); |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 120 | |
Tom Cherry | 68f2a46 | 2017-08-22 16:15:26 -0700 | [diff] [blame] | 121 | // There are many legacy paths in rootdir/init.rc that will virtually never exist on a new |
| 122 | // device, such as '/sys/class/leds/jogball-backlight/brightness'. As of this writing, there |
| 123 | // are 198 such failures on bullhead. Instead of spamming the log reporting them, we do not |
| 124 | // report such failures unless we're running at the DEBUG log level. |
| 125 | bool report_failure = !result.has_value(); |
| 126 | if (report_failure && android::base::GetMinimumLogSeverity() > android::base::DEBUG && |
| 127 | result.error_errno() == ENOENT) { |
| 128 | report_failure = false; |
| 129 | } |
| 130 | |
Wei Wang | 8b1d526 | 2016-11-15 23:58:55 -0800 | [diff] [blame] | 131 | // Any action longer than 50ms will be warned to user as slow operation |
Tom Cherry | 68f2a46 | 2017-08-22 16:15:26 -0700 | [diff] [blame] | 132 | if (report_failure || duration > 50ms || |
| 133 | android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 134 | std::string trigger_name = BuildTriggersString(); |
| 135 | std::string cmd_str = command.BuildCommandString(); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 136 | |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 137 | LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_ |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 138 | << ":" << command.line() << ") took " << duration.count() << "ms and " |
Tom Cherry | 130e3d7 | 2017-08-22 16:07:15 -0700 | [diff] [blame] | 139 | << (result ? "succeeded" : "failed: " + result.error_string()); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 143 | // This function checks that all property triggers are satisfied, that is |
| 144 | // for each (name, value) in property_triggers_, check that the current |
| 145 | // value of the property 'name' == value. |
| 146 | // |
| 147 | // It takes an optional (name, value) pair, which if provided must |
| 148 | // be present in property_triggers_; it skips the check of the current |
| 149 | // property value for this pair. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 150 | bool Action::CheckPropertyTriggers(const std::string& name, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 151 | const std::string& value) const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 152 | if (property_triggers_.empty()) { |
| 153 | return true; |
| 154 | } |
| 155 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 156 | bool found = name.empty(); |
Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 157 | for (const auto& [trigger_name, trigger_value] : property_triggers_) { |
Tom Cherry | cb716f9 | 2015-08-11 12:34:22 -0700 | [diff] [blame] | 158 | if (trigger_name == name) { |
| 159 | if (trigger_value != "*" && trigger_value != value) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 160 | return false; |
| 161 | } else { |
| 162 | found = true; |
| 163 | } |
| 164 | } else { |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 165 | std::string prop_val = android::base::GetProperty(trigger_name, ""); |
| 166 | if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | return found; |
| 172 | } |
| 173 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 174 | bool Action::CheckEvent(const EventTrigger& event_trigger) const { |
| 175 | return event_trigger == event_trigger_ && CheckPropertyTriggers(); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 178 | bool Action::CheckEvent(const PropertyChange& property_change) const { |
| 179 | const auto& [name, value] = property_change; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 180 | return event_trigger_.empty() && CheckPropertyTriggers(name, value); |
| 181 | } |
| 182 | |
Tom Cherry | 26ed9cb | 2017-04-17 13:25:29 -0700 | [diff] [blame] | 183 | bool Action::CheckEvent(const BuiltinAction& builtin_action) const { |
| 184 | return this == builtin_action; |
| 185 | } |
| 186 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 187 | std::string Action::BuildTriggersString() const { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 188 | std::vector<std::string> triggers; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 189 | |
Tom Cherry | 2bc0014 | 2017-03-13 11:58:58 -0700 | [diff] [blame] | 190 | for (const auto& [trigger_name, trigger_value] : property_triggers_) { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 191 | triggers.emplace_back(trigger_name + '=' + trigger_value); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 192 | } |
| 193 | if (!event_trigger_.empty()) { |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 194 | triggers.emplace_back(event_trigger_); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 195 | } |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 196 | |
| 197 | return Join(triggers, " && "); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 200 | void Action::DumpState() const { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 201 | std::string trigger_name = BuildTriggersString(); |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 202 | LOG(INFO) << "on " << trigger_name; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 203 | |
| 204 | for (const auto& c : commands_) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 205 | std::string cmd_str = c.BuildCommandString(); |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 206 | LOG(INFO) << " " << cmd_str; |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 207 | } |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 210 | } // namespace init |
| 211 | } // namespace android |