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
ParseRcScripts(const std::vector<std::string> & files)104 static Result<void> ParseRcScripts(const std::vector<std::string>& files) {
105 if (files.empty()) {
106 return {};
107 }
108 // APEXes can have versioned RC files. These should be filtered based on
109 // SDK version.
110 int sdk = android::base::GetIntProperty("ro.build.version.sdk", INT_MAX);
111 if (sdk < 35) sdk = 35; // aosp/main merges only into sdk=35+ (ie. __ANDROID_API_V__+)
112 auto filtered = FilterVersionedConfigs(files, sdk);
113 if (filtered.empty()) {
114 return {};
115 }
116
117 Parser parser =
118 CreateApexConfigParser(ActionManager::GetInstance(), ServiceList::GetInstance());
119 std::vector<std::string> errors;
120 for (const auto& c : filtered) {
121 auto result = parser.ParseConfigFile(c);
122 // We should handle other config files even when there's an error.
123 if (!result.ok()) {
124 errors.push_back(result.error().message());
125 }
126 }
127 if (!errors.empty()) {
128 return Error() << "Unable to parse apex configs: " << base::Join(errors, "|");
129 }
130 return {};
131 }
132
ParseRcScriptsFromApex(const std::string & apex_name)133 Result<void> ParseRcScriptsFromApex(const std::string& apex_name) {
134 auto configs = OR_RETURN(CollectRcScriptsFromApex(apex_name, /*skip_apexes=*/{}));
135 return ParseRcScripts(configs);
136 }
137
ParseRcScriptsFromAllApexes(bool bootstrap)138 Result<void> ParseRcScriptsFromAllApexes(bool bootstrap) {
139 std::set<std::string> skip_apexes;
140 if (!bootstrap) {
141 // In case we already loaded config files from bootstrap APEXes, we need to avoid loading
142 // them again. We can get the list of bootstrap APEXes by scanning /bootstrap-apex and
143 // skip them in CollectRcScriptsFromApex.
144 skip_apexes = GetApexListFrom("/bootstrap-apex");
145 }
146 auto configs = OR_RETURN(CollectRcScriptsFromApex(/*apex_name=*/"", skip_apexes));
147 return ParseRcScripts(configs);
148 }
149
150 } // namespace init
151 } // namespace android
152