blob: 8c0bb2b68aad2f20c05cfbe0e1469351e679ef34 [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
Jingwen Chenf643b352023-03-16 16:22:25 +0000134bool Parser::ParseConfigFileInsecure(const std::string& path, bool follow_symlinks = false) {
Tom Cherryd72432d2018-06-19 15:18:40 -0700135 std::string config_contents;
Jingwen Chenf643b352023-03-16 16:22:25 +0000136 if (!android::base::ReadFileToString(path, &config_contents, follow_symlinks)) {
Tom Cherryd72432d2018-06-19 15:18:40 -0700137 return false;
138 }
139
140 ParseData(path, &config_contents);
141 return true;
142}
143
Jooyung Han6b88d162023-01-10 15:22:54 +0900144Result<void> 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()) {
Jooyung Han6b88d162023-01-10 15:22:54 +0900149 return Error() << "Unable to read config file '" << path
150 << "': " << config_contents.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700151 }
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 << ".)";
Jooyung Han6b88d162023-01-10 15:22:54 +0900156 return {};
Tom Cherry67dee622017-07-27 12:54:48 -0700157}
158
Tom Cherry194b5d12018-05-09 17:38:30 -0700159bool Parser::ParseConfigDir(const std::string& path) {
Tom Cherry67dee622017-07-27 12:54:48 -0700160 LOG(INFO) << "Parsing directory " << path << "...";
161 std::unique_ptr<DIR, decltype(&closedir)> config_dir(opendir(path.c_str()), closedir);
162 if (!config_dir) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700163 PLOG(INFO) << "Could not import directory '" << path << "'";
Tom Cherry67dee622017-07-27 12:54:48 -0700164 return false;
165 }
166 dirent* current_file;
167 std::vector<std::string> files;
168 while ((current_file = readdir(config_dir.get()))) {
169 // Ignore directories and only process regular files.
170 if (current_file->d_type == DT_REG) {
171 std::string current_path =
172 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
173 files.emplace_back(current_path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 }
175 }
Tom Cherry67dee622017-07-27 12:54:48 -0700176 // Sort first so we load files in a consistent order (bug 31996208)
177 std::sort(files.begin(), files.end());
178 for (const auto& file : files) {
Jooyung Han6b88d162023-01-10 15:22:54 +0900179 if (auto result = ParseConfigFile(file); !result.ok()) {
180 LOG(ERROR) << "could not import file '" << file << "': " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700181 }
182 }
183 return true;
184}
185
186bool Parser::ParseConfig(const std::string& path) {
187 if (is_dir(path.c_str())) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700188 return ParseConfigDir(path);
Tom Cherry67dee622017-07-27 12:54:48 -0700189 }
Jooyung Han6b88d162023-01-10 15:22:54 +0900190 auto result = ParseConfigFile(path);
191 if (!result.ok()) {
192 LOG(INFO) << result.error();
193 }
194 return result.ok();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700196
197} // namespace init
198} // namespace android