| 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 |  | 
| Elliott Hughes | dc80312 | 2018-05-24 18:00:39 -0700 | [diff] [blame] | 19 | #include <android-base/properties.h> | 
| Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 20 | #include <android-base/strings.h> | 
 | 21 |  | 
| Tom Cherry | b35f827 | 2018-10-22 14:50:52 -0700 | [diff] [blame] | 22 | #if defined(__ANDROID__) | 
 | 23 | #include "property_service.h" | 
 | 24 | #else | 
 | 25 | #include "host_init_stubs.h" | 
 | 26 | #endif | 
| Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 27 |  | 
 | 28 | using android::base::GetBoolProperty; | 
| Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 29 | using android::base::StartsWith; | 
 | 30 |  | 
 | 31 | namespace android { | 
 | 32 | namespace init { | 
 | 33 |  | 
| Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 34 | namespace { | 
 | 35 |  | 
 | 36 | bool 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 Cherry | fa79ae8 | 2018-10-26 08:40:55 -0700 | [diff] [blame] | 43 |     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 Cherry | b35f827 | 2018-10-22 14:50:52 -0700 | [diff] [blame] | 55 |     return CanReadProperty(subcontext->context(), prop_name); | 
| Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 56 | } | 
 | 57 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 58 | Result<void> ParsePropertyTrigger(const std::string& trigger, Subcontext* subcontext, | 
 | 59 |                                   std::map<std::string, std::string>* property_triggers) { | 
| Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 60 |     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 Cherry | 8c901dd | 2018-07-24 15:54:33 -0700 | [diff] [blame] | 71 |         return Error() << "unexported property trigger found: " << prop_name; | 
| Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 72 |     } | 
 | 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 Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 77 |     return {}; | 
| Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 78 | } | 
 | 79 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 80 | Result<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 Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 83 |     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 Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 111 |     return {}; | 
| Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 112 | } | 
 | 113 |  | 
 | 114 | }  // namespace | 
 | 115 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 116 | Result<void> ActionParser::ParseSection(std::vector<std::string>&& args, | 
 | 117 |                                         const std::string& filename, int line) { | 
| Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 118 |     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 Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 133 |     std::string event_trigger; | 
 | 134 |     std::map<std::string, std::string> property_triggers; | 
| Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 135 |  | 
| Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 136 |     if (auto result = ParseTriggers(triggers, action_subcontext, &event_trigger, &property_triggers); | 
 | 137 |         !result) { | 
 | 138 |         return Error() << "ParseTriggers() failed: " << result.error(); | 
| Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 139 |     } | 
 | 140 |  | 
| Tom Cherry | 9cbf570 | 2018-02-13 16:24:51 -0800 | [diff] [blame] | 141 |     auto action = std::make_unique<Action>(false, action_subcontext, filename, line, event_trigger, | 
 | 142 |                                            property_triggers); | 
 | 143 |  | 
| Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 144 |     action_ = std::move(action); | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 145 |     return {}; | 
| Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 146 | } | 
 | 147 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 148 | Result<void> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) { | 
 | 149 |     return action_ ? action_->AddCommand(std::move(args), line) : Result<void>{}; | 
| Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 150 | } | 
 | 151 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 152 | Result<void> ActionParser::EndSection() { | 
| Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 153 |     if (action_ && action_->NumCommands() > 0) { | 
 | 154 |         action_manager_->AddAction(std::move(action_)); | 
 | 155 |     } | 
 | 156 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 157 |     return {}; | 
| Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 158 | } | 
 | 159 |  | 
 | 160 | }  // namespace init | 
 | 161 | }  // namespace android |