blob: abc2017429d3b31f51155fb3a90700a5ea9a3c0d [file] [log] [blame]
Tom Cherry67dee622017-07-27 12:54:48 -07001/*
2 * Copyright (C) 2010 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
Elliott Hughesc0e919c2015-02-04 14:46:36 -080017#include "parser.h"
18
Tom Cherry67dee622017-07-27 12:54:48 -070019#include <dirent.h>
20
Daniel Normanf1200fb2022-03-09 10:49:26 -080021#include <map>
22
Tom Cherry67dee622017-07-27 12:54:48 -070023#include <android-base/chrono_utils.h>
Tom Cherryd72432d2018-06-19 15:18:40 -070024#include <android-base/file.h>
Tom Cherry67dee622017-07-27 12:54:48 -070025#include <android-base/logging.h>
26#include <android-base/stringprintf.h>
27#include <android-base/strings.h>
28
29#include "tokenizer.h"
30#include "util.h"
31
Tom Cherry81f5d3e2017-06-22 12:53:17 -070032namespace android {
33namespace init {
34
Tom Cherry67dee622017-07-27 12:54:48 -070035Parser::Parser() {}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
Tom Cherry67dee622017-07-27 12:54:48 -070037void Parser::AddSectionParser(const std::string& name, std::unique_ptr<SectionParser> parser) {
38 section_parsers_[name] = std::move(parser);
39}
40
41void Parser::AddSingleLineParser(const std::string& prefix, LineCallback callback) {
Tom Cherry7c1d87e2019-07-10 11:18:24 -070042 line_callbacks_.emplace_back(prefix, std::move(callback));
Tom Cherry67dee622017-07-27 12:54:48 -070043}
44
Tom Cherryd72432d2018-06-19 15:18:40 -070045void Parser::ParseData(const std::string& filename, std::string* data) {
Tom Cherry85f2bc92020-04-10 10:15:30 -070046 data->push_back('\n');
Tom Cherryd72432d2018-06-19 15:18:40 -070047 data->push_back('\0');
Tom Cherry67dee622017-07-27 12:54:48 -070048
49 parse_state state;
50 state.line = 0;
Tom Cherryd72432d2018-06-19 15:18:40 -070051 state.ptr = data->data();
Tom Cherry67dee622017-07-27 12:54:48 -070052 state.nexttoken = 0;
53
54 SectionParser* section_parser = nullptr;
Steven Moreland7d0a5c32017-11-10 14:20:47 -080055 int section_start_line = -1;
Tom Cherry67dee622017-07-27 12:54:48 -070056 std::vector<std::string> args;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Tom Cherry0166fd62018-10-26 12:33:52 -070058 // If we encounter a bad section start, there is no valid parser object to parse the subsequent
59 // sections, so we must suppress errors until the next valid section is found.
60 bool bad_section_found = false;
61
Steven Moreland7d0a5c32017-11-10 14:20:47 -080062 auto end_section = [&] {
Tom Cherry0166fd62018-10-26 12:33:52 -070063 bad_section_found = false;
Steven Moreland7d0a5c32017-11-10 14:20:47 -080064 if (section_parser == nullptr) return;
65
Bernie Innocenticecebbb2020-02-06 03:49:33 +090066 if (auto result = section_parser->EndSection(); !result.ok()) {
Tom Cherry194b5d12018-05-09 17:38:30 -070067 parse_error_count_++;
Steven Moreland7d0a5c32017-11-10 14:20:47 -080068 LOG(ERROR) << filename << ": " << section_start_line << ": " << result.error();
69 }
70
71 section_parser = nullptr;
72 section_start_line = -1;
73 };
74
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075 for (;;) {
Tom Cherry67dee622017-07-27 12:54:48 -070076 switch (next_token(&state)) {
77 case T_EOF:
Steven Moreland7d0a5c32017-11-10 14:20:47 -080078 end_section();
Tom Cherryd72432d2018-06-19 15:18:40 -070079
80 for (const auto& [section_name, section_parser] : section_parsers_) {
81 section_parser->EndFile();
82 }
83
Tom Cherry67dee622017-07-27 12:54:48 -070084 return;
Tom Cherryd5d626c2018-06-12 10:57:35 -070085 case T_NEWLINE: {
Tom Cherry67dee622017-07-27 12:54:48 -070086 state.line++;
87 if (args.empty()) break;
88 // If we have a line matching a prefix we recognize, call its callback and unset any
89 // current section parsers. This is meant for /sys/ and /dev/ line entries for
90 // uevent.
Tom Cherryd5d626c2018-06-12 10:57:35 -070091 auto line_callback = std::find_if(
92 line_callbacks_.begin(), line_callbacks_.end(),
93 [&args](const auto& c) { return android::base::StartsWith(args[0], c.first); });
94 if (line_callback != line_callbacks_.end()) {
95 end_section();
Tom Cherry67dee622017-07-27 12:54:48 -070096
Bernie Innocenticecebbb2020-02-06 03:49:33 +090097 if (auto result = line_callback->second(std::move(args)); !result.ok()) {
Tom Cherryd5d626c2018-06-12 10:57:35 -070098 parse_error_count_++;
99 LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700100 }
Tom Cherryd5d626c2018-06-12 10:57:35 -0700101 } else if (section_parsers_.count(args[0])) {
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800102 end_section();
Tom Cherry67dee622017-07-27 12:54:48 -0700103 section_parser = section_parsers_[args[0]].get();
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800104 section_start_line = state.line;
Tom Cherryb592dd82017-08-02 17:01:36 -0700105 if (auto result =
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900106 section_parser->ParseSection(std::move(args), filename, state.line);
107 !result.ok()) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700108 parse_error_count_++;
Tom Cherryb592dd82017-08-02 17:01:36 -0700109 LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700110 section_parser = nullptr;
Tom Cherry0166fd62018-10-26 12:33:52 -0700111 bad_section_found = true;
Tom Cherry67dee622017-07-27 12:54:48 -0700112 }
113 } else if (section_parser) {
Tom Cherryb592dd82017-08-02 17:01:36 -0700114 if (auto result = section_parser->ParseLineSection(std::move(args), state.line);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900115 !result.ok()) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700116 parse_error_count_++;
Tom Cherryb592dd82017-08-02 17:01:36 -0700117 LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700118 }
Tom Cherry0166fd62018-10-26 12:33:52 -0700119 } else if (!bad_section_found) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700120 parse_error_count_++;
121 LOG(ERROR) << filename << ": " << state.line
122 << ": Invalid section keyword found";
Tom Cherry67dee622017-07-27 12:54:48 -0700123 }
124 args.clear();
125 break;
Tom Cherryd5d626c2018-06-12 10:57:35 -0700126 }
Tom Cherry67dee622017-07-27 12:54:48 -0700127 case T_TEXT:
128 args.emplace_back(state.text);
129 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 }
131 }
Tom Cherry67dee622017-07-27 12:54:48 -0700132}
133
Tom Cherryd72432d2018-06-19 15:18:40 -0700134bool Parser::ParseConfigFileInsecure(const std::string& path) {
135 std::string config_contents;
136 if (!android::base::ReadFileToString(path, &config_contents)) {
137 return false;
138 }
139
140 ParseData(path, &config_contents);
141 return true;
142}
143
Tom Cherry194b5d12018-05-09 17:38:30 -0700144bool Parser::ParseConfigFile(const std::string& path) {
Tom Cherry67dee622017-07-27 12:54:48 -0700145 LOG(INFO) << "Parsing file " << path << "...";
146 android::base::Timer t;
Tom Cherry62ca6632017-08-03 12:54:07 -0700147 auto config_contents = ReadFile(path);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900148 if (!config_contents.ok()) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700149 LOG(INFO) << "Unable to read config file '" << path << "': " << config_contents.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700150 return false;
151 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152
Tom Cherryd72432d2018-06-19 15:18:40 -0700153 ParseData(path, &config_contents.value());
Tom Cherry67dee622017-07-27 12:54:48 -0700154
155 LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)";
156 return true;
157}
158
Daniel Normanf1200fb2022-03-09 10:49:26 -0800159std::vector<std::string> Parser::FilterVersionedConfigs(const std::vector<std::string>& configs,
160 int active_sdk) {
161 std::vector<std::string> filtered_configs;
162
163 std::map<std::string, std::pair<std::string, int>> script_map;
164 for (const auto& c : configs) {
165 int sdk = 0;
166 const std::vector<std::string> parts = android::base::Split(c, ".");
167 std::string base;
168 if (parts.size() < 2) {
169 continue;
170 }
171
172 // parts[size()-1], aka the suffix, should be "rc" or "#rc"
173 // any other pattern gets discarded
174
175 const auto& suffix = parts[parts.size() - 1];
176 if (suffix == "rc") {
177 sdk = 0;
178 } else {
179 char trailer[9] = {0};
180 int r = sscanf(suffix.c_str(), "%d%8s", &sdk, trailer);
181 if (r != 2) {
182 continue;
183 }
184 if (strlen(trailer) > 2 || strcmp(trailer, "rc") != 0) {
185 continue;
186 }
187 }
188
189 if (sdk < 0 || sdk > active_sdk) {
190 continue;
191 }
192
193 base = parts[0];
194 for (unsigned int i = 1; i < parts.size() - 1; i++) {
195 base = base + "." + parts[i];
196 }
197
198 // is this preferred over what we already have
199 auto it = script_map.find(base);
200 if (it == script_map.end() || it->second.second < sdk) {
201 script_map[base] = std::make_pair(c, sdk);
202 }
203 }
204
205 for (const auto& m : script_map) {
206 filtered_configs.push_back(m.second.first);
207 }
208 return filtered_configs;
209}
210
Tom Cherry194b5d12018-05-09 17:38:30 -0700211bool Parser::ParseConfigDir(const std::string& path) {
Tom Cherry67dee622017-07-27 12:54:48 -0700212 LOG(INFO) << "Parsing directory " << path << "...";
213 std::unique_ptr<DIR, decltype(&closedir)> config_dir(opendir(path.c_str()), closedir);
214 if (!config_dir) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700215 PLOG(INFO) << "Could not import directory '" << path << "'";
Tom Cherry67dee622017-07-27 12:54:48 -0700216 return false;
217 }
218 dirent* current_file;
219 std::vector<std::string> files;
220 while ((current_file = readdir(config_dir.get()))) {
221 // Ignore directories and only process regular files.
222 if (current_file->d_type == DT_REG) {
223 std::string current_path =
224 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
225 files.emplace_back(current_path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 }
227 }
Tom Cherry67dee622017-07-27 12:54:48 -0700228 // Sort first so we load files in a consistent order (bug 31996208)
229 std::sort(files.begin(), files.end());
230 for (const auto& file : files) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700231 if (!ParseConfigFile(file)) {
Tom Cherry67dee622017-07-27 12:54:48 -0700232 LOG(ERROR) << "could not import file '" << file << "'";
233 }
234 }
235 return true;
236}
237
238bool Parser::ParseConfig(const std::string& path) {
239 if (is_dir(path.c_str())) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700240 return ParseConfigDir(path);
Tom Cherry67dee622017-07-27 12:54:48 -0700241 }
Tom Cherry194b5d12018-05-09 17:38:30 -0700242 return ParseConfigFile(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700244
245} // namespace init
246} // namespace android