blob: a40172e68862387f028b242c5d73c674a5002f4d [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>
Elliott Hughesdc803122018-05-24 18:00:39 -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
Tom Cherryfa0c21c2015-07-23 17:53:11 -070024#include "util.h"
25
Tom Cherryb7349902015-08-26 11:43:36 -070026using android::base::Join;
Tom Cherryfa0c21c2015-07-23 17:53:11 -070027
Tom Cherry81f5d3e2017-06-22 12:53:17 -070028namespace android {
29namespace init {
30
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070031Result<Success> RunBuiltinFunction(const BuiltinFunction& function,
32 const std::vector<std::string>& args,
33 const std::string& context) {
34 auto builtin_arguments = BuiltinArguments(context);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070035
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070036 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 Cherryfa0c21c2015-07-23 17:53:11 -070041 }
42 }
43
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070044 return function(builtin_arguments);
45}
46
Tom Cherry018a4382018-10-17 11:11:23 -070047Command::Command(BuiltinFunction f, bool execute_in_subcontext, std::vector<std::string>&& args,
48 int line)
49 : func_(std::move(f)),
50 execute_in_subcontext_(execute_in_subcontext),
51 args_(std::move(args)),
52 line_(line) {}
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070053
54Result<Success> Command::InvokeFunc(Subcontext* subcontext) const {
Tom Cherryc49719f2018-01-10 11:04:34 -080055 if (subcontext) {
56 if (execute_in_subcontext_) {
57 return subcontext->Execute(args_);
58 }
59
60 auto expanded_args = subcontext->ExpandArgs(args_);
61 if (!expanded_args) {
62 return expanded_args.error();
63 }
64 return RunBuiltinFunction(func_, *expanded_args, subcontext->context());
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070065 }
Tom Cherryc49719f2018-01-10 11:04:34 -080066
67 return RunBuiltinFunction(func_, args_, kInitContext);
Tom Cherryfa0c21c2015-07-23 17:53:11 -070068}
69
Tom Cherryb7349902015-08-26 11:43:36 -070070std::string Command::BuildCommandString() const {
71 return Join(args_, ' ');
Tom Cherryfa0c21c2015-07-23 17:53:11 -070072}
73
Tom Cherry9cbf5702018-02-13 16:24:51 -080074Action::Action(bool oneshot, Subcontext* subcontext, const std::string& filename, int line,
75 const std::string& event_trigger,
76 const std::map<std::string, std::string>& property_triggers)
77 : property_triggers_(property_triggers),
78 event_trigger_(event_trigger),
79 oneshot_(oneshot),
80 subcontext_(subcontext),
81 filename_(filename),
82 line_(line) {}
Tom Cherryfa0c21c2015-07-23 17:53:11 -070083
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070084const KeywordFunctionMap* Action::function_map_ = nullptr;
Tom Cherryb7349902015-08-26 11:43:36 -070085
Tom Cherry018a4382018-10-17 11:11:23 -070086Result<Success> Action::AddCommand(std::vector<std::string>&& args, int line) {
Tom Cherryb7349902015-08-26 11:43:36 -070087 if (!function_map_) {
Tom Cherry89bcc852017-08-02 17:01:36 -070088 return Error() << "no function map available";
Tom Cherryb7349902015-08-26 11:43:36 -070089 }
90
Tom Cherry89bcc852017-08-02 17:01:36 -070091 auto function = function_map_->FindFunction(args);
92 if (!function) return Error() << function.error();
Tom Cherryb7349902015-08-26 11:43:36 -070093
Tom Cherry018a4382018-10-17 11:11:23 -070094 commands_.emplace_back(function->second, function->first, std::move(args), line);
Tom Cherry89bcc852017-08-02 17:01:36 -070095 return Success();
Tom Cherryb7349902015-08-26 11:43:36 -070096}
97
Tom Cherry018a4382018-10-17 11:11:23 -070098void Action::AddCommand(BuiltinFunction f, std::vector<std::string>&& args, int line) {
99 commands_.emplace_back(f, false, std::move(args), line);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700100}
101
Tom Cherryb7349902015-08-26 11:43:36 -0700102std::size_t Action::NumCommands() const {
103 return commands_.size();
104}
105
106void Action::ExecuteOneCommand(std::size_t command) const {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800107 // We need a copy here since some Command execution may result in
108 // changing commands_ vector by importing .rc files through parser
109 Command cmd = commands_[command];
110 ExecuteCommand(cmd);
Tom Cherryb7349902015-08-26 11:43:36 -0700111}
112
113void Action::ExecuteAllCommands() const {
114 for (const auto& c : commands_) {
115 ExecuteCommand(c);
116 }
117}
118
119void Action::ExecuteCommand(const Command& command) const {
Tom Cherryede0d532017-07-06 14:20:11 -0700120 android::base::Timer t;
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700121 auto result = command.InvokeFunc(subcontext_);
Tom Cherryede0d532017-07-06 14:20:11 -0700122 auto duration = t.duration();
Tom Cherry557946e2017-08-01 13:50:23 -0700123
Tom Cherry68f2a462017-08-22 16:15:26 -0700124 // There are many legacy paths in rootdir/init.rc that will virtually never exist on a new
125 // device, such as '/sys/class/leds/jogball-backlight/brightness'. As of this writing, there
126 // are 198 such failures on bullhead. Instead of spamming the log reporting them, we do not
127 // report such failures unless we're running at the DEBUG log level.
128 bool report_failure = !result.has_value();
129 if (report_failure && android::base::GetMinimumLogSeverity() > android::base::DEBUG &&
Jiyong Park8fd64c82019-05-31 03:43:34 +0900130 result.error().code() == ENOENT) {
Tom Cherry68f2a462017-08-22 16:15:26 -0700131 report_failure = false;
132 }
133
Wei Wang8b1d5262016-11-15 23:58:55 -0800134 // Any action longer than 50ms will be warned to user as slow operation
Tom Cherry68f2a462017-08-22 16:15:26 -0700135 if (report_failure || duration > 50ms ||
136 android::base::GetMinimumLogSeverity() <= android::base::DEBUG) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700137 std::string trigger_name = BuildTriggersString();
138 std::string cmd_str = command.BuildCommandString();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700139
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700140 LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_
Tom Cherry557946e2017-08-01 13:50:23 -0700141 << ":" << command.line() << ") took " << duration.count() << "ms and "
Jiyong Park8fd64c82019-05-31 03:43:34 +0900142 << (result ? "succeeded" : "failed: " + result.error().message());
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700143 }
144}
145
Tom Cherryb7349902015-08-26 11:43:36 -0700146// This function checks that all property triggers are satisfied, that is
147// for each (name, value) in property_triggers_, check that the current
148// value of the property 'name' == value.
149//
150// It takes an optional (name, value) pair, which if provided must
151// be present in property_triggers_; it skips the check of the current
152// property value for this pair.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700153bool Action::CheckPropertyTriggers(const std::string& name,
Tom Cherryb7349902015-08-26 11:43:36 -0700154 const std::string& value) const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700155 if (property_triggers_.empty()) {
156 return true;
157 }
158
Tom Cherryb7349902015-08-26 11:43:36 -0700159 bool found = name.empty();
Tom Cherry2bc00142017-03-13 11:58:58 -0700160 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherrycb716f92015-08-11 12:34:22 -0700161 if (trigger_name == name) {
162 if (trigger_value != "*" && trigger_value != value) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700163 return false;
164 } else {
165 found = true;
166 }
167 } else {
Tom Cherryccf23532017-03-28 16:40:41 -0700168 std::string prop_val = android::base::GetProperty(trigger_name, "");
169 if (prop_val.empty() || (trigger_value != "*" && trigger_value != prop_val)) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700170 return false;
171 }
172 }
173 }
174 return found;
175}
176
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700177bool Action::CheckEvent(const EventTrigger& event_trigger) const {
178 return event_trigger == event_trigger_ && CheckPropertyTriggers();
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700179}
180
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700181bool Action::CheckEvent(const PropertyChange& property_change) const {
182 const auto& [name, value] = property_change;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700183 return event_trigger_.empty() && CheckPropertyTriggers(name, value);
184}
185
Tom Cherry26ed9cb2017-04-17 13:25:29 -0700186bool Action::CheckEvent(const BuiltinAction& builtin_action) const {
187 return this == builtin_action;
188}
189
Tom Cherryb7349902015-08-26 11:43:36 -0700190std::string Action::BuildTriggersString() const {
Tom Cherryd8a72572017-03-13 12:24:49 -0700191 std::vector<std::string> triggers;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700192
Tom Cherry2bc00142017-03-13 11:58:58 -0700193 for (const auto& [trigger_name, trigger_value] : property_triggers_) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700194 triggers.emplace_back(trigger_name + '=' + trigger_value);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700195 }
196 if (!event_trigger_.empty()) {
Tom Cherryd8a72572017-03-13 12:24:49 -0700197 triggers.emplace_back(event_trigger_);
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700198 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700199
200 return Join(triggers, " && ");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700201}
202
Tom Cherryb7349902015-08-26 11:43:36 -0700203void Action::DumpState() const {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700204 std::string trigger_name = BuildTriggersString();
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700205 LOG(INFO) << "on " << trigger_name;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700206
207 for (const auto& c : commands_) {
Tom Cherryb7349902015-08-26 11:43:36 -0700208 std::string cmd_str = c.BuildCommandString();
Tom Cherryd8a72572017-03-13 12:24:49 -0700209 LOG(INFO) << " " << cmd_str;
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700210 }
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700211}
212
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700213} // namespace init
214} // namespace android