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