From 5c4217cf6eb0686fcab73cd55d216859549b6250 Mon Sep 17 00:00:00 2001 From: Jooyung Han Date: Thu, 10 Aug 2023 13:12:53 +0900 Subject: [PATCH 1/2] Read .rc files from bootstrap apexes To start an early_hal service from a bootstrap vendor apex, init now reads .rc files from bootstrap apexes as well. In this change, perform_apex_config command is re-purposed to support bootstrap mode. Now we have some similarity between two apexd calls: - for bootstrap apexes (in the bootstrap mount namespace): exec_start apexd-bootstrap perform_apex_config --bootstrap - for normal apexes (in the default mount namespace): restart apexd ... wait_for_prop apexd.status activated perform_apex_config Note that some tasks in perform_apex_config are not needed in the bootstrap. For example, we don't need to create apexdata directories for bootstrap apexes. Bug: 290148081 Test: VendorApexHostTestCases Change-Id: I8f683a4dcd7cd9a2466a4b1b417d84c025c37761 --- init/README.md | 3 ++- init/builtins.cpp | 25 +++++++++++++++++++------ rootdir/init.rc | 4 +--- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/init/README.md b/init/README.md index 5fced19b0..11c4e1cf3 100644 --- a/init/README.md +++ b/init/README.md @@ -674,11 +674,12 @@ provides the `aidl_lazy_test_1` interface. _options_ include "barrier=1", "noauto\_da\_alloc", "discard", ... as a comma separated string, e.g. barrier=1,noauto\_da\_alloc -`perform_apex_config` +`perform_apex_config [--bootstrap]` > Performs tasks after APEXes are mounted. For example, creates data directories for the mounted APEXes, parses config file(s) from them, and updates linker configurations. Intended to be used only once when apexd notifies the mount event by setting `apexd.status` to ready. + Use --bootstrap when invoking in the bootstrap mount namespace. `restart [--only-if-running] ` > Stops and restarts a running service, does nothing if the service is currently diff --git a/init/builtins.cpp b/init/builtins.cpp index fa5e36d52..e40b831d1 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -1292,10 +1292,21 @@ static Result create_apex_data_dirs() { } static Result do_perform_apex_config(const BuiltinArguments& args) { - auto create_dirs = create_apex_data_dirs(); - if (!create_dirs.ok()) { - return create_dirs.error(); + bool bootstrap = false; + if (args.size() == 2) { + if (args[1] != "--bootstrap") { + return Error() << "Unexpected argument: " << args[1]; + } + bootstrap = true; } + + if (!bootstrap) { + auto create_dirs = create_apex_data_dirs(); + if (!create_dirs.ok()) { + return create_dirs.error(); + } + } + auto parse_configs = ParseApexConfigs(/*apex_name=*/""); if (!parse_configs.ok()) { return parse_configs.error(); @@ -1306,8 +1317,10 @@ static Result do_perform_apex_config(const BuiltinArguments& args) { return update_linker_config.error(); } - // Now start delayed services - ServiceList::GetInstance().MarkServicesUpdate(); + if (!bootstrap) { + // Now start delayed services + ServiceList::GetInstance().MarkServicesUpdate(); + } return {}; } @@ -1362,7 +1375,7 @@ const BuiltinFunctionMap& GetBuiltinFunctionMap() { // mount and umount are run in the same context as mount_all for symmetry. {"mount_all", {0, kMax, {false, do_mount_all}}}, {"mount", {3, kMax, {false, do_mount}}}, - {"perform_apex_config", {0, 0, {false, do_perform_apex_config}}}, + {"perform_apex_config", {0, 1, {false, do_perform_apex_config}}}, {"umount", {1, 1, {false, do_umount}}}, {"umount_all", {0, 1, {false, do_umount_all}}}, {"update_linker_config", {0, 0, {false, do_update_linker_config}}}, diff --git a/rootdir/init.rc b/rootdir/init.rc index 8f01d932b..0d31cdce7 100644 --- a/rootdir/init.rc +++ b/rootdir/init.rc @@ -74,9 +74,7 @@ on early-init # become available. Note that this is executed as exec_start to ensure that # the libraries are available to the processes started after this statement. exec_start apexd-bootstrap - - # Generate linker config based on apex mounted in bootstrap namespace - update_linker_config + perform_apex_config --bootstrap # These must already exist by the time boringssl_self_test32 / boringssl_self_test64 run. mkdir /dev/boringssl 0755 root root From 55ef3d6104032dcaa773e486405cb9f8978fd5ce Mon Sep 17 00:00:00 2001 From: Jooyung Han Date: Thu, 10 Aug 2023 18:27:22 +0900 Subject: [PATCH 2/2] Skip bootstrap APEX RC files for the second round Reading .rc files from bootstrap APEXes causes "double loading". This works for services because init just ignores duplicates. But it emits error logs, which can mislead even though there's no actual errors. Besides, for actions, duplicates can cause a problem when commands are not idempotent. So, when loading RC files from APEXes for the second time, we'd better skip those bootstrap APEXes. Bug: 290148081 Test: VendorApexHostTestCases Change-Id: Ia630dbd14046064b5e5c612c01ebacf57091c8d4 --- init/apex_init_util.cpp | 95 ++++++++++++++++++++++++++++++++--------- init/apex_init_util.h | 8 ++-- init/builtins.cpp | 6 +-- init/init.cpp | 2 +- 4 files changed, 83 insertions(+), 28 deletions(-) diff --git a/init/apex_init_util.cpp b/init/apex_init_util.cpp index c818f8fe9..d88da390e 100644 --- a/init/apex_init_util.cpp +++ b/init/apex_init_util.cpp @@ -16,13 +16,15 @@ #include "apex_init_util.h" +#include #include +#include #include #include -#include #include +#include #include #include "action_manager.h" @@ -34,10 +36,13 @@ namespace android { namespace init { -static Result> CollectApexConfigs(const std::string& apex_name) { +static Result> CollectRcScriptsFromApex( + const std::string& apex_name, const std::set& skip_apexes) { glob_t glob_result; - std::string glob_pattern = apex_name.empty() ? - "/apex/*/etc/*rc" : "/apex/" + apex_name + "/etc/*rc"; + // Pattern uses "*rc" instead of ".rc" because APEXes can have versioned RC files + // like foo.34rc. + std::string glob_pattern = + apex_name.empty() ? "/apex/*/etc/*rc" : "/apex/" + apex_name + "/etc/*rc"; const int ret = glob(glob_pattern.c_str(), GLOB_MARK, nullptr, &glob_result); if (ret != 0 && ret != GLOB_NOMATCH) { @@ -47,15 +52,28 @@ static Result> CollectApexConfigs(const std::string& ap std::vector configs; for (size_t i = 0; i < glob_result.gl_pathc; i++) { std::string path = glob_result.gl_pathv[i]; - // Filter-out /apex/@ paths. The paths are bind-mounted to - // /apex/ paths, so unless we filter them out, we will parse the - // same file twice. - std::vector paths = android::base::Split(path, "/"); - if (paths.size() >= 3 && paths[2].find('@') != std::string::npos) { + + // Filter out directories + if (path.back() == '/') { continue; } - // Filter directories - if (path.back() == '/') { + + // Get apex name from path. + std::vector paths = android::base::Split(path, "/"); + if (paths.size() < 3) { + continue; + } + const std::string& apex_name = paths[2]; + + // Filter out /apex/@ paths. The paths are bind-mounted to + // /apex/ paths, so unless we filter them out, we will parse the + // same file twice. + if (apex_name.find('@') != std::string::npos) { + continue; + } + + // Filter out skip_set apexes + if (skip_apexes.count(apex_name) > 0) { continue; } configs.push_back(path); @@ -64,11 +82,41 @@ static Result> CollectApexConfigs(const std::string& ap return configs; } -static Result ParseConfigs(const std::vector& configs) { +static std::set GetApexListFrom(const std::string& apex_dir) { + std::set apex_list; + auto dirp = std::unique_ptr(opendir(apex_dir.c_str()), closedir); + if (!dirp) { + return apex_list; + } + struct dirent* entry; + while ((entry = readdir(dirp.get())) != nullptr) { + if (entry->d_type != DT_DIR) continue; + + const char* name = entry->d_name; + if (name[0] == '.') continue; + if (strchr(name, '@') != nullptr) continue; + if (strcmp(name, "sharedlibs") == 0) continue; + apex_list.insert(name); + } + return apex_list; +} + +static Result ParseRcScripts(const std::vector& files) { + if (files.empty()) { + return {}; + } + // APEXes can have versioned RC files. These should be filtered based on + // SDK version. + auto filtered = FilterVersionedConfigs( + files, android::base::GetIntProperty("ro.build.version.sdk", INT_MAX)); + if (filtered.empty()) { + return {}; + } + Parser parser = CreateApexConfigParser(ActionManager::GetInstance(), ServiceList::GetInstance()); std::vector errors; - for (const auto& c : configs) { + for (const auto& c : filtered) { auto result = parser.ParseConfigFile(c); // We should handle other config files even when there's an error. if (!result.ok()) { @@ -81,16 +129,21 @@ static Result ParseConfigs(const std::vector& configs) { return {}; } -Result ParseApexConfigs(const std::string& apex_name) { - auto configs = OR_RETURN(CollectApexConfigs(apex_name)); +Result ParseRcScriptsFromApex(const std::string& apex_name) { + auto configs = OR_RETURN(CollectRcScriptsFromApex(apex_name, /*skip_apexes=*/{})); + return ParseRcScripts(configs); +} - if (configs.empty()) { - return {}; +Result ParseRcScriptsFromAllApexes(bool bootstrap) { + std::set skip_apexes; + if (!bootstrap) { + // In case we already loaded config files from bootstrap APEXes, we need to avoid loading + // them again. We can get the list of bootstrap APEXes by scanning /bootstrap-apex and + // skip them in CollectRcScriptsFromApex. + skip_apexes = GetApexListFrom("/bootstrap-apex"); } - - auto filtered_configs = FilterVersionedConfigs(configs, - android::base::GetIntProperty("ro.build.version.sdk", INT_MAX)); - return ParseConfigs(filtered_configs); + auto configs = OR_RETURN(CollectRcScriptsFromApex(/*apex_name=*/"", skip_apexes)); + return ParseRcScripts(configs); } } // namespace init diff --git a/init/apex_init_util.h b/init/apex_init_util.h index 43f8ad5eb..e55b3c0c9 100644 --- a/init/apex_init_util.h +++ b/init/apex_init_util.h @@ -24,9 +24,11 @@ namespace android { namespace init { -// Parse all config files for a given apex. -// If apex name is empty(""), config files for all apexes will be parsed. -Result ParseApexConfigs(const std::string& apex_name); +// Parse all RC scripts for a given apex. +Result ParseRcScriptsFromApex(const std::string& apex_name); + +// Parse all RC scripts for all apexes under /apex. +Result ParseRcScriptsFromAllApexes(bool bootstrap); } // namespace init } // namespace android diff --git a/init/builtins.cpp b/init/builtins.cpp index e40b831d1..2ced66d11 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -1307,9 +1307,9 @@ static Result do_perform_apex_config(const BuiltinArguments& args) { } } - auto parse_configs = ParseApexConfigs(/*apex_name=*/""); - if (!parse_configs.ok()) { - return parse_configs.error(); + auto parse_result = ParseRcScriptsFromAllApexes(bootstrap); + if (!parse_result.ok()) { + return parse_result.error(); } auto update_linker_config = do_update_linker_config(args); diff --git a/init/init.cpp b/init/init.cpp index 4bb8eecb9..40e21696f 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -487,7 +487,7 @@ static Result UpdateApexLinkerConfig(const std::string& apex_name) { } static Result DoLoadApex(const std::string& apex_name) { - if (auto result = ParseApexConfigs(apex_name); !result.ok()) { + if (auto result = ParseRcScriptsFromApex(apex_name); !result.ok()) { return result.error(); }