Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 1 | /* |
| 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 Ioffe | aaab596 | 2019-10-10 20:42:37 +0100 | [diff] [blame] | 19 | #include <ctype.h> |
| 20 | |
Elliott Hughes | dc80312 | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 21 | #include <android-base/properties.h> |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 22 | #include <android-base/strings.h> |
| 23 | |
Tom Cherry | a2f9136 | 2020-02-20 10:50:00 -0800 | [diff] [blame] | 24 | #ifdef INIT_FULL_SOURCES |
Tom Cherry | b35f827 | 2018-10-22 14:50:52 -0700 | [diff] [blame] | 25 | #include "property_service.h" |
Nikita Ioffe | aaab596 | 2019-10-10 20:42:37 +0100 | [diff] [blame] | 26 | #include "selinux.h" |
Tom Cherry | b35f827 | 2018-10-22 14:50:52 -0700 | [diff] [blame] | 27 | #else |
| 28 | #include "host_init_stubs.h" |
| 29 | #endif |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 30 | |
| 31 | using android::base::GetBoolProperty; |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 32 | using android::base::StartsWith; |
| 33 | |
| 34 | namespace android { |
| 35 | namespace init { |
| 36 | |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 37 | namespace { |
| 38 | |
| 39 | bool 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 Cherry | fa79ae8 | 2018-10-26 08:40:55 -0700 | [diff] [blame] | 46 | 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 Cherry | b35f827 | 2018-10-22 14:50:52 -0700 | [diff] [blame] | 58 | return CanReadProperty(subcontext->context(), prop_name); |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 61 | Result<void> ParsePropertyTrigger(const std::string& trigger, Subcontext* subcontext, |
| 62 | std::map<std::string, std::string>* property_triggers) { |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 63 | 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 Cherry | 8c901dd | 2018-07-24 15:54:33 -0700 | [diff] [blame] | 74 | return Error() << "unexported property trigger found: " << prop_name; |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 75 | } |
| 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 Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 80 | return {}; |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Nikita Ioffe | aaab596 | 2019-10-10 20:42:37 +0100 | [diff] [blame] | 83 | Result<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 Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 94 | Result<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 Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 97 | 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 Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 113 | !result.ok()) { |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 114 | return result; |
| 115 | } |
| 116 | } else { |
| 117 | if (!event_trigger->empty()) { |
| 118 | return Error() << "multiple event triggers are not allowed"; |
| 119 | } |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 120 | if (auto result = ValidateEventTrigger(args[i]); !result.ok()) { |
Nikita Ioffe | aaab596 | 2019-10-10 20:42:37 +0100 | [diff] [blame] | 121 | return result; |
| 122 | } |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 123 | |
| 124 | *event_trigger = args[i]; |
| 125 | } |
| 126 | } |
| 127 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 128 | return {}; |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | } // namespace |
| 132 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 133 | Result<void> ActionParser::ParseSection(std::vector<std::string>&& args, |
| 134 | const std::string& filename, int line) { |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 135 | 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 Cherry | 14c2472 | 2019-09-18 13:47:19 -0700 | [diff] [blame] | 141 | if (subcontext_ && subcontext_->PathMatchesSubcontext(filename)) { |
| 142 | action_subcontext = subcontext_; |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Jooyung Han | 99fa346 | 2022-05-20 14:47:42 +0900 | [diff] [blame] | 145 | // We support 'on' for only Vendor APEXes from /{vendor, odm}. |
| 146 | // It is to prevent mainline modules from using 'on' triggers because events/properties are |
| 147 | // not stable for mainline modules. |
| 148 | // Note that this relies on Subcontext::PathMatchesSubcontext() to identify Vendor APEXes. |
| 149 | if (StartsWith(filename, "/apex/") && !action_subcontext) { |
| 150 | return Error() << "ParseSection() failed: 'on' is supported for only Vendor APEXes."; |
| 151 | } |
| 152 | |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 153 | std::string event_trigger; |
| 154 | std::map<std::string, std::string> property_triggers; |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 155 | |
Bernie Innocenti | cecebbb | 2020-02-06 03:49:33 +0900 | [diff] [blame] | 156 | if (auto result = |
| 157 | ParseTriggers(triggers, action_subcontext, &event_trigger, &property_triggers); |
| 158 | !result.ok()) { |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 159 | return Error() << "ParseTriggers() failed: " << result.error(); |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 162 | auto action = std::make_unique<Action>(false, action_subcontext, filename, line, event_trigger, |
| 163 | property_triggers); |
| 164 | |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 165 | action_ = std::move(action); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 166 | return {}; |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 169 | Result<void> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) { |
| 170 | return action_ ? action_->AddCommand(std::move(args), line) : Result<void>{}; |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 173 | Result<void> ActionParser::EndSection() { |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 174 | if (action_ && action_->NumCommands() > 0) { |
| 175 | action_manager_->AddAction(std::move(action_)); |
| 176 | } |
| 177 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 178 | return {}; |
Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | } // namespace init |
| 182 | } // namespace android |