blob: 52f6a1f3c33724fa054338ab3316e78ea415df17 [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
Nikita Ioffeaaab5962019-10-10 20:42:37 +010019#include <ctype.h>
20
Elliott Hughesdc803122018-05-24 18:00:39 -070021#include <android-base/properties.h>
Tom Cherry0f6417f2018-02-13 15:25:29 -080022#include <android-base/strings.h>
23
Tom Cherrya2f91362020-02-20 10:50:00 -080024#ifdef INIT_FULL_SOURCES
Tom Cherryb35f8272018-10-22 14:50:52 -070025#include "property_service.h"
Nikita Ioffeaaab5962019-10-10 20:42:37 +010026#include "selinux.h"
Tom Cherryb35f8272018-10-22 14:50:52 -070027#else
28#include "host_init_stubs.h"
29#endif
Tom Cherry9cbf5702018-02-13 16:24:51 -080030
31using android::base::GetBoolProperty;
Tom Cherry0f6417f2018-02-13 15:25:29 -080032using android::base::StartsWith;
33
34namespace android {
35namespace init {
36
Tom Cherry9cbf5702018-02-13 16:24:51 -080037namespace {
38
39bool IsActionableProperty(Subcontext* subcontext, const std::string& prop_name) {
40 static bool enabled = GetBoolProperty("ro.actionable_compatible_property.enabled", false);
41
42 if (subcontext == nullptr || !enabled) {
43 return true;
44 }
45
Tom Cherryfa79ae82018-10-26 08:40:55 -070046 static constexpr const char* kPartnerPrefixes[] = {
47 "init.svc.vendor.", "ro.vendor.", "persist.vendor.",
48 "vendor.", "init.svc.odm.", "ro.odm.",
49 "persist.odm.", "odm.", "ro.boot.",
50 };
51
52 for (const auto& prefix : kPartnerPrefixes) {
53 if (android::base::StartsWith(prop_name, prefix)) {
54 return true;
55 }
56 }
57
Tom Cherryb35f8272018-10-22 14:50:52 -070058 return CanReadProperty(subcontext->context(), prop_name);
Tom Cherry9cbf5702018-02-13 16:24:51 -080059}
60
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070061Result<void> ParsePropertyTrigger(const std::string& trigger, Subcontext* subcontext,
62 std::map<std::string, std::string>* property_triggers) {
Tom Cherry9cbf5702018-02-13 16:24:51 -080063 const static std::string prop_str("property:");
64 std::string prop_name(trigger.substr(prop_str.length()));
65 size_t equal_pos = prop_name.find('=');
66 if (equal_pos == std::string::npos) {
67 return Error() << "property trigger found without matching '='";
68 }
69
70 std::string prop_value(prop_name.substr(equal_pos + 1));
71 prop_name.erase(equal_pos);
72
73 if (!IsActionableProperty(subcontext, prop_name)) {
Tom Cherry8c901dd2018-07-24 15:54:33 -070074 return Error() << "unexported property trigger found: " << prop_name;
Tom Cherry9cbf5702018-02-13 16:24:51 -080075 }
76
77 if (auto [it, inserted] = property_triggers->emplace(prop_name, prop_value); !inserted) {
78 return Error() << "multiple property triggers found for same property";
79 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070080 return {};
Tom Cherry9cbf5702018-02-13 16:24:51 -080081}
82
Nikita Ioffeaaab5962019-10-10 20:42:37 +010083Result<void> ValidateEventTrigger(const std::string& event_trigger) {
84 if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_R__) {
85 for (const char& c : event_trigger) {
86 if (c != '_' && c != '-' && !std::isalnum(c)) {
87 return Error() << "Illegal character '" << c << "' in '" << event_trigger << "'";
88 }
89 }
90 }
91 return {};
92}
93
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070094Result<void> ParseTriggers(const std::vector<std::string>& args, Subcontext* subcontext,
95 std::string* event_trigger,
96 std::map<std::string, std::string>* property_triggers) {
Tom Cherry9cbf5702018-02-13 16:24:51 -080097 const static std::string prop_str("property:");
98 for (std::size_t i = 0; i < args.size(); ++i) {
99 if (args[i].empty()) {
100 return Error() << "empty trigger is not valid";
101 }
102
103 if (i % 2) {
104 if (args[i] != "&&") {
105 return Error() << "&& is the only symbol allowed to concatenate actions";
106 } else {
107 continue;
108 }
109 }
110
111 if (!args[i].compare(0, prop_str.length(), prop_str)) {
112 if (auto result = ParsePropertyTrigger(args[i], subcontext, property_triggers);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900113 !result.ok()) {
Tom Cherry9cbf5702018-02-13 16:24:51 -0800114 return result;
115 }
116 } else {
117 if (!event_trigger->empty()) {
118 return Error() << "multiple event triggers are not allowed";
119 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900120 if (auto result = ValidateEventTrigger(args[i]); !result.ok()) {
Nikita Ioffeaaab5962019-10-10 20:42:37 +0100121 return result;
122 }
Tom Cherry9cbf5702018-02-13 16:24:51 -0800123
124 *event_trigger = args[i];
125 }
126 }
127
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700128 return {};
Tom Cherry9cbf5702018-02-13 16:24:51 -0800129}
130
131} // namespace
132
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700133Result<void> ActionParser::ParseSection(std::vector<std::string>&& args,
134 const std::string& filename, int line) {
Tom Cherry0f6417f2018-02-13 15:25:29 -0800135 std::vector<std::string> triggers(args.begin() + 1, args.end());
136 if (triggers.size() < 1) {
137 return Error() << "Actions must have a trigger";
138 }
139
140 Subcontext* action_subcontext = nullptr;
Tom Cherry14c24722019-09-18 13:47:19 -0700141 if (subcontext_ && subcontext_->PathMatchesSubcontext(filename)) {
142 action_subcontext = subcontext_;
Tom Cherry0f6417f2018-02-13 15:25:29 -0800143 }
144
Tom Cherry9cbf5702018-02-13 16:24:51 -0800145 std::string event_trigger;
146 std::map<std::string, std::string> property_triggers;
Tom Cherry0f6417f2018-02-13 15:25:29 -0800147
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900148 if (auto result =
149 ParseTriggers(triggers, action_subcontext, &event_trigger, &property_triggers);
150 !result.ok()) {
Tom Cherry9cbf5702018-02-13 16:24:51 -0800151 return Error() << "ParseTriggers() failed: " << result.error();
Tom Cherry0f6417f2018-02-13 15:25:29 -0800152 }
153
Tom Cherry9cbf5702018-02-13 16:24:51 -0800154 auto action = std::make_unique<Action>(false, action_subcontext, filename, line, event_trigger,
155 property_triggers);
156
Tom Cherry0f6417f2018-02-13 15:25:29 -0800157 action_ = std::move(action);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700158 return {};
Tom Cherry0f6417f2018-02-13 15:25:29 -0800159}
160
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700161Result<void> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) {
162 return action_ ? action_->AddCommand(std::move(args), line) : Result<void>{};
Tom Cherry0f6417f2018-02-13 15:25:29 -0800163}
164
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700165Result<void> ActionParser::EndSection() {
Tom Cherry0f6417f2018-02-13 15:25:29 -0800166 if (action_ && action_->NumCommands() > 0) {
167 action_manager_->AddAction(std::move(action_));
168 }
169
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700170 return {};
Tom Cherry0f6417f2018-02-13 15:25:29 -0800171}
172
173} // namespace init
174} // namespace android