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 <glob.h>
20
21 #include <vector>
22
23 #include <android-base/logging.h>
24 #include <android-base/result.h>
25 #include <android-base/properties.h>
26 #include <android-base/strings.h>
27
28 #include "action_manager.h"
29 #include "init.h"
30 #include "parser.h"
31 #include "service_list.h"
32 #include "util.h"
33
34 namespace android {
35 namespace init {
36
CollectApexConfigs(const std::string & apex_name)37 static Result<std::vector<std::string>> CollectApexConfigs(const std::string& apex_name) {
38 glob_t glob_result;
39 std::string glob_pattern = apex_name.empty() ?
40 "/apex/*/etc/*rc" : "/apex/" + apex_name + "/etc/*rc";
41
42 const int ret = glob(glob_pattern.c_str(), GLOB_MARK, nullptr, &glob_result);
43 if (ret != 0 && ret != GLOB_NOMATCH) {
44 globfree(&glob_result);
45 return Error() << "Glob pattern '" << glob_pattern << "' failed";
46 }
47 std::vector<std::string> configs;
48 for (size_t i = 0; i < glob_result.gl_pathc; i++) {
49 std::string path = glob_result.gl_pathv[i];
50 // Filter-out /apex/<name>@<ver> paths. The paths are bind-mounted to
51 // /apex/<name> paths, so unless we filter them out, we will parse the
52 // same file twice.
53 std::vector<std::string> paths = android::base::Split(path, "/");
54 if (paths.size() >= 3 && paths[2].find('@') != std::string::npos) {
55 continue;
56 }
57 // Filter directories
58 if (path.back() == '/') {
59 continue;
60 }
61 configs.push_back(path);
62 }
63 globfree(&glob_result);
64 return configs;
65 }
66
ParseConfigs(const std::vector<std::string> & configs)67 static Result<void> ParseConfigs(const std::vector<std::string>& configs) {
68 Parser parser =
69 CreateApexConfigParser(ActionManager::GetInstance(), ServiceList::GetInstance());
70 std::vector<std::string> errors;
71 for (const auto& c : configs) {
72 auto result = parser.ParseConfigFile(c);
73 // We should handle other config files even when there's an error.
74 if (!result.ok()) {
75 errors.push_back(result.error().message());
76 }
77 }
78 if (!errors.empty()) {
79 return Error() << "Unable to parse apex configs: " << base::Join(errors, "|");
80 }
81 return {};
82 }
83
ParseApexConfigs(const std::string & apex_name)84 Result<void> ParseApexConfigs(const std::string& apex_name) {
85 auto configs = OR_RETURN(CollectApexConfigs(apex_name));
86
87 if (configs.empty()) {
88 return {};
89 }
90
91 auto filtered_configs = FilterVersionedConfigs(configs,
92 android::base::GetIntProperty("ro.build.version.sdk", INT_MAX));
93 return ParseConfigs(filtered_configs);
94 }
95
96 } // namespace init
97 } // namespace android
98