Use the new 'partition' field in 'ApexInfo' to identify vendor apexes

A new field 'partition' was added to `ApexInfo` recently which stores
pre-installed partition information as string (e.g. 'SYSTEM') in
aosp/3335753. Using 'partition' field for Subcontext vendor apex
initialization because the existing field `preinstalledModulePath` won't
be populated for brand-new apex (a new type we introduced recently).

Bug: 377111286

Test: atest CtsInitTestCases
Change-Id: I8970b3cb5884bdb949035f5bdc5b2e18618cc9cc
This commit is contained in:
Chan Wang 2024-11-14 13:25:37 +00:00
parent fa528cccf6
commit 5996d608af
5 changed files with 29 additions and 8 deletions

View file

@ -315,8 +315,7 @@ Parser CreateApexConfigParser(ActionManager& action_manager, ServiceList& servic
if (apex_info_list.has_value()) {
std::vector<std::string> subcontext_apexes;
for (const auto& info : apex_info_list->getApexInfo()) {
if (info.hasPreinstalledModulePath() &&
subcontext->PathMatchesSubcontext(info.getPreinstalledModulePath())) {
if (subcontext->PartitionMatchesSubcontext(info.getPartition())) {
subcontext_apexes.push_back(info.getModuleName());
}
}

View file

@ -263,6 +263,10 @@ bool Subcontext::PathMatchesSubcontext(const std::string& path) const {
return false;
}
bool Subcontext::PartitionMatchesSubcontext(const std::string& partition) const {
return std::find(partitions_.begin(), partitions_.end(), partition) != partitions_.end();
}
void Subcontext::SetApexList(std::vector<std::string>&& apex_list) {
apex_list_ = std::move(apex_list);
}
@ -352,12 +356,13 @@ void InitializeSubcontext() {
}
if (SelinuxGetVendorAndroidVersion() >= __ANDROID_API_P__) {
subcontext.reset(
new Subcontext(std::vector<std::string>{"/vendor", "/odm"}, kVendorContext));
subcontext.reset(new Subcontext(std::vector<std::string>{"/vendor", "/odm"},
std::vector<std::string>{"VENDOR", "ODM"}, kVendorContext));
}
}
void InitializeHostSubcontext(std::vector<std::string> vendor_prefixes) {
subcontext.reset(new Subcontext(vendor_prefixes, kVendorContext, /*host=*/true));
subcontext.reset(new Subcontext(vendor_prefixes, {}, kVendorContext, /*host=*/true));
}
Subcontext* GetSubcontext() {

View file

@ -36,8 +36,10 @@ static constexpr const char kTestContext[] = "test-test-test";
class Subcontext {
public:
Subcontext(std::vector<std::string> path_prefixes, std::string_view context, bool host = false)
Subcontext(std::vector<std::string> path_prefixes, std::vector<std::string> partitions,
std::string_view context, bool host = false)
: path_prefixes_(std::move(path_prefixes)),
partitions_(std::move(partitions)),
context_(context.begin(), context.end()),
pid_(0) {
if (!host) {
@ -49,6 +51,7 @@ class Subcontext {
Result<std::vector<std::string>> ExpandArgs(const std::vector<std::string>& args);
void Restart();
bool PathMatchesSubcontext(const std::string& path) const;
bool PartitionMatchesSubcontext(const std::string& partition) const;
void SetApexList(std::vector<std::string>&& apex_list);
const std::string& context() const { return context_; }
@ -59,6 +62,7 @@ class Subcontext {
Result<SubcontextReply> TransmitMessage(const SubcontextCommand& subcontext_command);
std::vector<std::string> path_prefixes_;
std::vector<std::string> partitions_;
std::vector<std::string> apex_list_;
std::string context_;
pid_t pid_;

View file

@ -33,7 +33,7 @@ static void BenchmarkSuccess(benchmark::State& state) {
return;
}
auto subcontext = Subcontext({"path"}, context);
auto subcontext = Subcontext({"path"}, {"partition"}, context);
free(context);
while (state.KeepRunning()) {

View file

@ -41,7 +41,7 @@ namespace init {
template <typename F>
void RunTest(F&& test_function) {
auto subcontext = Subcontext({"dummy_path"}, kTestContext);
auto subcontext = Subcontext({"dummy_path"}, {"dummy_partition"}, kTestContext);
ASSERT_NE(0, subcontext.pid());
test_function(subcontext);
@ -177,6 +177,19 @@ TEST(subcontext, ExpandArgsFailure) {
});
}
TEST(subcontext, PartitionMatchesSubcontext) {
RunTest([](auto& subcontext) {
static auto& existent_partition = "dummy_partition";
static auto& non_existent_partition = "not_dummy_partition";
auto existent_result = subcontext.PartitionMatchesSubcontext(existent_partition);
auto non_existent_result = subcontext.PartitionMatchesSubcontext(non_existent_partition);
ASSERT_TRUE(existent_result);
ASSERT_FALSE(non_existent_result);
});
}
BuiltinFunctionMap BuildTestFunctionMap() {
// For CheckDifferentPid
auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> {