xref: /aosp_15_r20/system/core/init/apex_init_util.cpp (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1 /*
2  * Copyright (C) 2022 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 "apex_init_util.h"
18 
19 #include <dirent.h>
20 #include <glob.h>
21 
22 #include <set>
23 #include <vector>
24 
25 #include <android-base/logging.h>
26 #include <android-base/properties.h>
27 #include <android-base/result.h>
28 #include <android-base/strings.h>
29 
30 #include "action_manager.h"
31 #include "init.h"
32 #include "parser.h"
33 #include "service_list.h"
34 #include "util.h"
35 
36 namespace android {
37 namespace init {
38 
CollectRcScriptsFromApex(const std::string & apex_name,const std::set<std::string> & skip_apexes)39 static Result<std::vector<std::string>> CollectRcScriptsFromApex(
40         const std::string& apex_name, const std::set<std::string>& skip_apexes) {
41     glob_t glob_result;
42     // Pattern uses "*rc" instead of ".rc" because APEXes can have versioned RC files
43     // like foo.34rc.
44     std::string glob_pattern =
45             apex_name.empty() ? "/apex/*/etc/*rc" : "/apex/" + apex_name + "/etc/*rc";
46 
47     const int ret = glob(glob_pattern.c_str(), GLOB_MARK, nullptr, &glob_result);
48     if (ret != 0 && ret != GLOB_NOMATCH) {
49         globfree(&glob_result);
50         return Error() << "Glob pattern '" << glob_pattern << "' failed";
51     }
52     std::vector<std::string> configs;
53     for (size_t i = 0; i < glob_result.gl_pathc; i++) {
54         std::string path = glob_result.gl_pathv[i];
55 
56         // Filter out directories
57         if (path.back() == '/') {
58             continue;
59         }
60 
61         // Get apex name from path.
62         std::vector<std::string> paths = android::base::Split(path, "/");
63         if (paths.size() < 3) {
64             continue;
65         }
66         const std::string& apex_name = paths[2];
67 
68         // Filter out /apex/<name>@<ver> paths. The paths are bind-mounted to
69         // /apex/<name> paths, so unless we filter them out, we will parse the
70         // same file twice.
71         if (apex_name.find('@') != std::string::npos) {
72             continue;
73         }
74 
75         // Filter out skip_set apexes
76         if (skip_apexes.count(apex_name) > 0) {
77             continue;
78         }
79         configs.push_back(path);
80     }
81     globfree(&glob_result);
82     return configs;
83 }
84 
GetApexListFrom(const std::string & apex_dir)85 std::set<std::string> GetApexListFrom(const std::string& apex_dir) {
86     std::set<std::string> apex_list;
87     auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(apex_dir.c_str()), closedir);
88     if (!dirp) {
89         return apex_list;
90     }
91     struct dirent* entry;
92     while ((entry = readdir(dirp.get())) != nullptr) {
93         if (entry->d_type != DT_DIR) continue;
94 
95         const char* name = entry->d_name;
96         if (name[0] == '.') continue;
97         if (strchr(name, '@') != nullptr) continue;
98         if (strcmp(name, "sharedlibs") == 0) continue;
99         apex_list.insert(name);
100     }
101     return apex_list;
102 }
103 
GetCurrentSdk()104 static int GetCurrentSdk() {
105     bool is_preview = base::GetProperty("ro.build.version.codename", "") != "REL";
106     if (is_preview) {
107         return __ANDROID_API_FUTURE__;
108     }
109     return android::base::GetIntProperty("ro.build.version.sdk", __ANDROID_API_FUTURE__);
110 }
111 
ParseRcScripts(const std::vector<std::string> & files)112 static Result<void> ParseRcScripts(const std::vector<std::string>& files) {
113     if (files.empty()) {
114         return {};
115     }
116     // APEXes can have versioned RC files. These should be filtered based on
117     // SDK version.
118     static int sdk = GetCurrentSdk();
119     auto filtered = FilterVersionedConfigs(files, sdk);
120     if (filtered.empty()) {
121         return {};
122     }
123 
124     Parser parser =
125             CreateApexConfigParser(ActionManager::GetInstance(), ServiceList::GetInstance());
126     std::vector<std::string> errors;
127     for (const auto& c : filtered) {
128         auto result = parser.ParseConfigFile(c);
129         // We should handle other config files even when there's an error.
130         if (!result.ok()) {
131             errors.push_back(result.error().message());
132         }
133     }
134     if (!errors.empty()) {
135         return Error() << "Unable to parse apex configs: " << base::Join(errors, "|");
136     }
137     return {};
138 }
139 
ParseRcScriptsFromApex(const std::string & apex_name)140 Result<void> ParseRcScriptsFromApex(const std::string& apex_name) {
141     auto configs = OR_RETURN(CollectRcScriptsFromApex(apex_name, /*skip_apexes=*/{}));
142     return ParseRcScripts(configs);
143 }
144 
ParseRcScriptsFromAllApexes(bool bootstrap)145 Result<void> ParseRcScriptsFromAllApexes(bool bootstrap) {
146     std::set<std::string> skip_apexes;
147     if (!bootstrap) {
148         // In case we already loaded config files from bootstrap APEXes, we need to avoid loading
149         // them again. We can get the list of bootstrap APEXes by scanning /bootstrap-apex and
150         // skip them in CollectRcScriptsFromApex.
151         skip_apexes = GetApexListFrom("/bootstrap-apex");
152     }
153     auto configs = OR_RETURN(CollectRcScriptsFromApex(/*apex_name=*/"", skip_apexes));
154     return ParseRcScripts(configs);
155 }
156 
157 }  // namespace init
158 }  // namespace android
159