2018-02-14 00:25:29 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "action_parser.h"
|
|
|
|
|
2019-10-10 21:42:37 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2018-05-25 03:00:39 +02:00
|
|
|
#include <android-base/properties.h>
|
2018-02-14 00:25:29 +01:00
|
|
|
#include <android-base/strings.h>
|
|
|
|
|
2020-02-20 19:50:00 +01:00
|
|
|
#ifdef INIT_FULL_SOURCES
|
2018-10-22 23:50:52 +02:00
|
|
|
#include "property_service.h"
|
2019-10-10 21:42:37 +02:00
|
|
|
#include "selinux.h"
|
2018-10-22 23:50:52 +02:00
|
|
|
#else
|
|
|
|
#include "host_init_stubs.h"
|
|
|
|
#endif
|
2018-02-14 01:24:51 +01:00
|
|
|
|
|
|
|
using android::base::GetBoolProperty;
|
2018-02-14 00:25:29 +01:00
|
|
|
using android::base::StartsWith;
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2018-02-14 01:24:51 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool IsActionableProperty(Subcontext* subcontext, const std::string& prop_name) {
|
|
|
|
static bool enabled = GetBoolProperty("ro.actionable_compatible_property.enabled", false);
|
|
|
|
|
|
|
|
if (subcontext == nullptr || !enabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:40:55 +02:00
|
|
|
static constexpr const char* kPartnerPrefixes[] = {
|
|
|
|
"init.svc.vendor.", "ro.vendor.", "persist.vendor.",
|
|
|
|
"vendor.", "init.svc.odm.", "ro.odm.",
|
|
|
|
"persist.odm.", "odm.", "ro.boot.",
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& prefix : kPartnerPrefixes) {
|
|
|
|
if (android::base::StartsWith(prop_name, prefix)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 23:50:52 +02:00
|
|
|
return CanReadProperty(subcontext->context(), prop_name);
|
2018-02-14 01:24:51 +01:00
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> ParsePropertyTrigger(const std::string& trigger, Subcontext* subcontext,
|
|
|
|
std::map<std::string, std::string>* property_triggers) {
|
2018-02-14 01:24:51 +01:00
|
|
|
const static std::string prop_str("property:");
|
|
|
|
std::string prop_name(trigger.substr(prop_str.length()));
|
|
|
|
size_t equal_pos = prop_name.find('=');
|
|
|
|
if (equal_pos == std::string::npos) {
|
|
|
|
return Error() << "property trigger found without matching '='";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string prop_value(prop_name.substr(equal_pos + 1));
|
|
|
|
prop_name.erase(equal_pos);
|
|
|
|
|
|
|
|
if (!IsActionableProperty(subcontext, prop_name)) {
|
2018-07-25 00:54:33 +02:00
|
|
|
return Error() << "unexported property trigger found: " << prop_name;
|
2018-02-14 01:24:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto [it, inserted] = property_triggers->emplace(prop_name, prop_value); !inserted) {
|
|
|
|
return Error() << "multiple property triggers found for same property";
|
|
|
|
}
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2018-02-14 01:24:51 +01:00
|
|
|
}
|
|
|
|
|
2019-10-10 21:42:37 +02:00
|
|
|
Result<void> ValidateEventTrigger(const std::string& event_trigger) {
|
|
|
|
if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_R__) {
|
|
|
|
for (const char& c : event_trigger) {
|
|
|
|
if (c != '_' && c != '-' && !std::isalnum(c)) {
|
|
|
|
return Error() << "Illegal character '" << c << "' in '" << event_trigger << "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> ParseTriggers(const std::vector<std::string>& args, Subcontext* subcontext,
|
|
|
|
std::string* event_trigger,
|
|
|
|
std::map<std::string, std::string>* property_triggers) {
|
2018-02-14 01:24:51 +01:00
|
|
|
const static std::string prop_str("property:");
|
|
|
|
for (std::size_t i = 0; i < args.size(); ++i) {
|
|
|
|
if (args[i].empty()) {
|
|
|
|
return Error() << "empty trigger is not valid";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i % 2) {
|
|
|
|
if (args[i] != "&&") {
|
|
|
|
return Error() << "&& is the only symbol allowed to concatenate actions";
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!args[i].compare(0, prop_str.length(), prop_str)) {
|
|
|
|
if (auto result = ParsePropertyTrigger(args[i], subcontext, property_triggers);
|
2020-02-05 19:49:33 +01:00
|
|
|
!result.ok()) {
|
2018-02-14 01:24:51 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!event_trigger->empty()) {
|
|
|
|
return Error() << "multiple event triggers are not allowed";
|
|
|
|
}
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = ValidateEventTrigger(args[i]); !result.ok()) {
|
2019-10-10 21:42:37 +02:00
|
|
|
return result;
|
|
|
|
}
|
2018-02-14 01:24:51 +01:00
|
|
|
|
|
|
|
*event_trigger = args[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2018-02-14 01:24:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> ActionParser::ParseSection(std::vector<std::string>&& args,
|
|
|
|
const std::string& filename, int line) {
|
2018-02-14 00:25:29 +01:00
|
|
|
std::vector<std::string> triggers(args.begin() + 1, args.end());
|
|
|
|
if (triggers.size() < 1) {
|
|
|
|
return Error() << "Actions must have a trigger";
|
|
|
|
}
|
|
|
|
|
|
|
|
Subcontext* action_subcontext = nullptr;
|
2019-09-18 22:47:19 +02:00
|
|
|
if (subcontext_ && subcontext_->PathMatchesSubcontext(filename)) {
|
|
|
|
action_subcontext = subcontext_;
|
2018-02-14 00:25:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 01:24:51 +01:00
|
|
|
std::string event_trigger;
|
|
|
|
std::map<std::string, std::string> property_triggers;
|
2018-02-14 00:25:29 +01:00
|
|
|
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result =
|
|
|
|
ParseTriggers(triggers, action_subcontext, &event_trigger, &property_triggers);
|
|
|
|
!result.ok()) {
|
2018-02-14 01:24:51 +01:00
|
|
|
return Error() << "ParseTriggers() failed: " << result.error();
|
2018-02-14 00:25:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 01:24:51 +01:00
|
|
|
auto action = std::make_unique<Action>(false, action_subcontext, filename, line, event_trigger,
|
|
|
|
property_triggers);
|
|
|
|
|
2018-02-14 00:25:29 +01:00
|
|
|
action_ = std::move(action);
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2018-02-14 00:25:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) {
|
|
|
|
return action_ ? action_->AddCommand(std::move(args), line) : Result<void>{};
|
2018-02-14 00:25:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> ActionParser::EndSection() {
|
2018-02-14 00:25:29 +01:00
|
|
|
if (action_ && action_->NumCommands() > 0) {
|
|
|
|
action_manager_->AddAction(std::move(action_));
|
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2018-02-14 00:25:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|