blob: ff20e43a31259c73c99a2b8066f0190bcc1e5ee4 [file] [log] [blame]
Tom Cherry0f6417f2018-02-13 15:25:29 -08001/*
2 * Copyright (C) 2018 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_parser.h"
18
Elliott Hughesdc803122018-05-24 18:00:39 -070019#include <android-base/properties.h>
Tom Cherry0f6417f2018-02-13 15:25:29 -080020#include <android-base/strings.h>
21
Tom Cherryb35f8272018-10-22 14:50:52 -070022#if defined(__ANDROID__)
23#include "property_service.h"
24#else
25#include "host_init_stubs.h"
26#endif
Tom Cherry9cbf5702018-02-13 16:24:51 -080027
28using android::base::GetBoolProperty;
Tom Cherry0f6417f2018-02-13 15:25:29 -080029using android::base::StartsWith;
30
31namespace android {
32namespace init {
33
Tom Cherry9cbf5702018-02-13 16:24:51 -080034namespace {
35
36bool IsActionableProperty(Subcontext* subcontext, const std::string& prop_name) {
37 static bool enabled = GetBoolProperty("ro.actionable_compatible_property.enabled", false);
38
39 if (subcontext == nullptr || !enabled) {
40 return true;
41 }
42
Tom Cherryfa79ae82018-10-26 08:40:55 -070043 static constexpr const char* kPartnerPrefixes[] = {
44 "init.svc.vendor.", "ro.vendor.", "persist.vendor.",
45 "vendor.", "init.svc.odm.", "ro.odm.",
46 "persist.odm.", "odm.", "ro.boot.",
47 };
48
49 for (const auto& prefix : kPartnerPrefixes) {
50 if (android::base::StartsWith(prop_name, prefix)) {
51 return true;
52 }
53 }
54
Tom Cherryb35f8272018-10-22 14:50:52 -070055 return CanReadProperty(subcontext->context(), prop_name);
Tom Cherry9cbf5702018-02-13 16:24:51 -080056}
57
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070058Result<void> ParsePropertyTrigger(const std::string& trigger, Subcontext* subcontext,
59 std::map<std::string, std::string>* property_triggers) {
Tom Cherry9cbf5702018-02-13 16:24:51 -080060 const static std::string prop_str("property:");
61 std::string prop_name(trigger.substr(prop_str.length()));
62 size_t equal_pos = prop_name.find('=');
63 if (equal_pos == std::string::npos) {
64 return Error() << "property trigger found without matching '='";
65 }
66
67 std::string prop_value(prop_name.substr(equal_pos + 1));
68 prop_name.erase(equal_pos);
69
70 if (!IsActionableProperty(subcontext, prop_name)) {
Tom Cherry8c901dd2018-07-24 15:54:33 -070071 return Error() << "unexported property trigger found: " << prop_name;
Tom Cherry9cbf5702018-02-13 16:24:51 -080072 }
73
74 if (auto [it, inserted] = property_triggers->emplace(prop_name, prop_value); !inserted) {
75 return Error() << "multiple property triggers found for same property";
76 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070077 return {};
Tom Cherry9cbf5702018-02-13 16:24:51 -080078}
79
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070080Result<void> ParseTriggers(const std::vector<std::string>& args, Subcontext* subcontext,
81 std::string* event_trigger,
82 std::map<std::string, std::string>* property_triggers) {
Tom Cherry9cbf5702018-02-13 16:24:51 -080083 const static std::string prop_str("property:");
84 for (std::size_t i = 0; i < args.size(); ++i) {
85 if (args[i].empty()) {
86 return Error() << "empty trigger is not valid";
87 }
88
89 if (i % 2) {
90 if (args[i] != "&&") {
91 return Error() << "&& is the only symbol allowed to concatenate actions";
92 } else {
93 continue;
94 }
95 }
96
97 if (!args[i].compare(0, prop_str.length(), prop_str)) {
98 if (auto result = ParsePropertyTrigger(args[i], subcontext, property_triggers);
99 !result) {
100 return result;
101 }
102 } else {
103 if (!event_trigger->empty()) {
104 return Error() << "multiple event triggers are not allowed";
105 }
106
107 *event_trigger = args[i];
108 }
109 }
110
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700111 return {};
Tom Cherry9cbf5702018-02-13 16:24:51 -0800112}
113
114} // namespace
115
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700116Result<void> ActionParser::ParseSection(std::vector<std::string>&& args,
117 const std::string& filename, int line) {
Tom Cherry0f6417f2018-02-13 15:25:29 -0800118 std::vector<std::string> triggers(args.begin() + 1, args.end());
119 if (triggers.size() < 1) {
120 return Error() << "Actions must have a trigger";
121 }
122
123 Subcontext* action_subcontext = nullptr;
124 if (subcontexts_) {
125 for (auto& subcontext : *subcontexts_) {
126 if (StartsWith(filename, subcontext.path_prefix())) {
127 action_subcontext = &subcontext;
128 break;
129 }
130 }
131 }
132
Tom Cherry9cbf5702018-02-13 16:24:51 -0800133 std::string event_trigger;
134 std::map<std::string, std::string> property_triggers;
Tom Cherry0f6417f2018-02-13 15:25:29 -0800135
Tom Cherry9cbf5702018-02-13 16:24:51 -0800136 if (auto result = ParseTriggers(triggers, action_subcontext, &event_trigger, &property_triggers);
137 !result) {
138 return Error() << "ParseTriggers() failed: " << result.error();
Tom Cherry0f6417f2018-02-13 15:25:29 -0800139 }
140
Tom Cherry9cbf5702018-02-13 16:24:51 -0800141 auto action = std::make_unique<Action>(false, action_subcontext, filename, line, event_trigger,
142 property_triggers);
143
Tom Cherry0f6417f2018-02-13 15:25:29 -0800144 action_ = std::move(action);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700145 return {};
Tom Cherry0f6417f2018-02-13 15:25:29 -0800146}
147
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700148Result<void> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) {
149 return action_ ? action_->AddCommand(std::move(args), line) : Result<void>{};
Tom Cherry0f6417f2018-02-13 15:25:29 -0800150}
151
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700152Result<void> ActionParser::EndSection() {
Tom Cherry0f6417f2018-02-13 15:25:29 -0800153 if (action_ && action_->NumCommands() > 0) {
154 action_manager_->AddAction(std::move(action_));
155 }
156
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700157 return {};
Tom Cherry0f6417f2018-02-13 15:25:29 -0800158}
159
160} // namespace init
161} // namespace android