blob: 6ab61cb8790f4af6f237d1234773810f0c72fb84 [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
21#include <android-base/chrono_utils.h>
Tom Cherryd72432d2018-06-19 15:18:40 -070022#include <android-base/file.h>
Tom Cherry67dee622017-07-27 12:54:48 -070023#include <android-base/logging.h>
24#include <android-base/stringprintf.h>
25#include <android-base/strings.h>
26
27#include "tokenizer.h"
28#include "util.h"
29
Tom Cherry81f5d3e2017-06-22 12:53:17 -070030namespace android {
31namespace init {
32
Tom Cherry67dee622017-07-27 12:54:48 -070033Parser::Parser() {}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
Tom Cherry67dee622017-07-27 12:54:48 -070035void Parser::AddSectionParser(const std::string& name, std::unique_ptr<SectionParser> parser) {
36 section_parsers_[name] = std::move(parser);
37}
38
39void Parser::AddSingleLineParser(const std::string& prefix, LineCallback callback) {
Tom Cherry7c1d87e2019-07-10 11:18:24 -070040 line_callbacks_.emplace_back(prefix, std::move(callback));
Tom Cherry67dee622017-07-27 12:54:48 -070041}
42
Tom Cherryd72432d2018-06-19 15:18:40 -070043void Parser::ParseData(const std::string& filename, std::string* data) {
44 data->push_back('\n'); // TODO: fix tokenizer
45 data->push_back('\0');
Tom Cherry67dee622017-07-27 12:54:48 -070046
47 parse_state state;
48 state.line = 0;
Tom Cherryd72432d2018-06-19 15:18:40 -070049 state.ptr = data->data();
Tom Cherry67dee622017-07-27 12:54:48 -070050 state.nexttoken = 0;
51
52 SectionParser* section_parser = nullptr;
Steven Moreland7d0a5c32017-11-10 14:20:47 -080053 int section_start_line = -1;
Tom Cherry67dee622017-07-27 12:54:48 -070054 std::vector<std::string> args;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
Tom Cherry0166fd62018-10-26 12:33:52 -070056 // If we encounter a bad section start, there is no valid parser object to parse the subsequent
57 // sections, so we must suppress errors until the next valid section is found.
58 bool bad_section_found = false;
59
Steven Moreland7d0a5c32017-11-10 14:20:47 -080060 auto end_section = [&] {
Tom Cherry0166fd62018-10-26 12:33:52 -070061 bad_section_found = false;
Steven Moreland7d0a5c32017-11-10 14:20:47 -080062 if (section_parser == nullptr) return;
63
64 if (auto result = section_parser->EndSection(); !result) {
Tom Cherry194b5d12018-05-09 17:38:30 -070065 parse_error_count_++;
Steven Moreland7d0a5c32017-11-10 14:20:47 -080066 LOG(ERROR) << filename << ": " << section_start_line << ": " << result.error();
67 }
68
69 section_parser = nullptr;
70 section_start_line = -1;
71 };
72
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 for (;;) {
Tom Cherry67dee622017-07-27 12:54:48 -070074 switch (next_token(&state)) {
75 case T_EOF:
Steven Moreland7d0a5c32017-11-10 14:20:47 -080076 end_section();
Tom Cherryd72432d2018-06-19 15:18:40 -070077
78 for (const auto& [section_name, section_parser] : section_parsers_) {
79 section_parser->EndFile();
80 }
81
Tom Cherry67dee622017-07-27 12:54:48 -070082 return;
Tom Cherryd5d626c2018-06-12 10:57:35 -070083 case T_NEWLINE: {
Tom Cherry67dee622017-07-27 12:54:48 -070084 state.line++;
85 if (args.empty()) break;
86 // If we have a line matching a prefix we recognize, call its callback and unset any
87 // current section parsers. This is meant for /sys/ and /dev/ line entries for
88 // uevent.
Tom Cherryd5d626c2018-06-12 10:57:35 -070089 auto line_callback = std::find_if(
90 line_callbacks_.begin(), line_callbacks_.end(),
91 [&args](const auto& c) { return android::base::StartsWith(args[0], c.first); });
92 if (line_callback != line_callbacks_.end()) {
93 end_section();
Tom Cherry67dee622017-07-27 12:54:48 -070094
Tom Cherryd5d626c2018-06-12 10:57:35 -070095 if (auto result = line_callback->second(std::move(args)); !result) {
96 parse_error_count_++;
97 LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -070098 }
Tom Cherryd5d626c2018-06-12 10:57:35 -070099 } else if (section_parsers_.count(args[0])) {
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800100 end_section();
Tom Cherry67dee622017-07-27 12:54:48 -0700101 section_parser = section_parsers_[args[0]].get();
Steven Moreland7d0a5c32017-11-10 14:20:47 -0800102 section_start_line = state.line;
Tom Cherryb592dd82017-08-02 17:01:36 -0700103 if (auto result =
104 section_parser->ParseSection(std::move(args), filename, state.line);
105 !result) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700106 parse_error_count_++;
Tom Cherryb592dd82017-08-02 17:01:36 -0700107 LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700108 section_parser = nullptr;
Tom Cherry0166fd62018-10-26 12:33:52 -0700109 bad_section_found = true;
Tom Cherry67dee622017-07-27 12:54:48 -0700110 }
111 } else if (section_parser) {
Tom Cherryb592dd82017-08-02 17:01:36 -0700112 if (auto result = section_parser->ParseLineSection(std::move(args), state.line);
113 !result) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700114 parse_error_count_++;
Tom Cherryb592dd82017-08-02 17:01:36 -0700115 LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700116 }
Tom Cherry0166fd62018-10-26 12:33:52 -0700117 } else if (!bad_section_found) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700118 parse_error_count_++;
119 LOG(ERROR) << filename << ": " << state.line
120 << ": Invalid section keyword found";
Tom Cherry67dee622017-07-27 12:54:48 -0700121 }
122 args.clear();
123 break;
Tom Cherryd5d626c2018-06-12 10:57:35 -0700124 }
Tom Cherry67dee622017-07-27 12:54:48 -0700125 case T_TEXT:
126 args.emplace_back(state.text);
127 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 }
129 }
Tom Cherry67dee622017-07-27 12:54:48 -0700130}
131
Tom Cherryd72432d2018-06-19 15:18:40 -0700132bool Parser::ParseConfigFileInsecure(const std::string& path) {
133 std::string config_contents;
134 if (!android::base::ReadFileToString(path, &config_contents)) {
135 return false;
136 }
137
138 ParseData(path, &config_contents);
139 return true;
140}
141
Tom Cherry194b5d12018-05-09 17:38:30 -0700142bool Parser::ParseConfigFile(const std::string& path) {
Tom Cherry67dee622017-07-27 12:54:48 -0700143 LOG(INFO) << "Parsing file " << path << "...";
144 android::base::Timer t;
Tom Cherry62ca6632017-08-03 12:54:07 -0700145 auto config_contents = ReadFile(path);
146 if (!config_contents) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700147 LOG(INFO) << "Unable to read config file '" << path << "': " << config_contents.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700148 return false;
149 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150
Tom Cherryd72432d2018-06-19 15:18:40 -0700151 ParseData(path, &config_contents.value());
Tom Cherry67dee622017-07-27 12:54:48 -0700152
153 LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)";
154 return true;
155}
156
Tom Cherry194b5d12018-05-09 17:38:30 -0700157bool Parser::ParseConfigDir(const std::string& path) {
Tom Cherry67dee622017-07-27 12:54:48 -0700158 LOG(INFO) << "Parsing directory " << path << "...";
159 std::unique_ptr<DIR, decltype(&closedir)> config_dir(opendir(path.c_str()), closedir);
160 if (!config_dir) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700161 PLOG(INFO) << "Could not import directory '" << path << "'";
Tom Cherry67dee622017-07-27 12:54:48 -0700162 return false;
163 }
164 dirent* current_file;
165 std::vector<std::string> files;
166 while ((current_file = readdir(config_dir.get()))) {
167 // Ignore directories and only process regular files.
168 if (current_file->d_type == DT_REG) {
169 std::string current_path =
170 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
171 files.emplace_back(current_path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 }
173 }
Tom Cherry67dee622017-07-27 12:54:48 -0700174 // Sort first so we load files in a consistent order (bug 31996208)
175 std::sort(files.begin(), files.end());
176 for (const auto& file : files) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700177 if (!ParseConfigFile(file)) {
Tom Cherry67dee622017-07-27 12:54:48 -0700178 LOG(ERROR) << "could not import file '" << file << "'";
179 }
180 }
181 return true;
182}
183
184bool Parser::ParseConfig(const std::string& path) {
185 if (is_dir(path.c_str())) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700186 return ParseConfigDir(path);
Tom Cherry67dee622017-07-27 12:54:48 -0700187 }
Tom Cherry194b5d12018-05-09 17:38:30 -0700188 return ParseConfigFile(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700190
191} // namespace init
192} // namespace android