blob: fa0fd11bece6c4a977dbb41630fbcdd2aa00919d [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) {
40 line_callbacks_.emplace_back(prefix, callback);
41}
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
Steven Moreland7d0a5c32017-11-10 14:20:47 -080056 auto end_section = [&] {
57 if (section_parser == nullptr) return;
58
59 if (auto result = section_parser->EndSection(); !result) {
Tom Cherry194b5d12018-05-09 17:38:30 -070060 parse_error_count_++;
Steven Moreland7d0a5c32017-11-10 14:20:47 -080061 LOG(ERROR) << filename << ": " << section_start_line << ": " << result.error();
62 }
63
64 section_parser = nullptr;
65 section_start_line = -1;
66 };
67
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068 for (;;) {
Tom Cherry67dee622017-07-27 12:54:48 -070069 switch (next_token(&state)) {
70 case T_EOF:
Steven Moreland7d0a5c32017-11-10 14:20:47 -080071 end_section();
Tom Cherryd72432d2018-06-19 15:18:40 -070072
73 for (const auto& [section_name, section_parser] : section_parsers_) {
74 section_parser->EndFile();
75 }
76
Tom Cherry67dee622017-07-27 12:54:48 -070077 return;
Tom Cherryd5d626c2018-06-12 10:57:35 -070078 case T_NEWLINE: {
Tom Cherry67dee622017-07-27 12:54:48 -070079 state.line++;
80 if (args.empty()) break;
81 // If we have a line matching a prefix we recognize, call its callback and unset any
82 // current section parsers. This is meant for /sys/ and /dev/ line entries for
83 // uevent.
Tom Cherryd5d626c2018-06-12 10:57:35 -070084 auto line_callback = std::find_if(
85 line_callbacks_.begin(), line_callbacks_.end(),
86 [&args](const auto& c) { return android::base::StartsWith(args[0], c.first); });
87 if (line_callback != line_callbacks_.end()) {
88 end_section();
Tom Cherry67dee622017-07-27 12:54:48 -070089
Tom Cherryd5d626c2018-06-12 10:57:35 -070090 if (auto result = line_callback->second(std::move(args)); !result) {
91 parse_error_count_++;
92 LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -070093 }
Tom Cherryd5d626c2018-06-12 10:57:35 -070094 } else if (section_parsers_.count(args[0])) {
Steven Moreland7d0a5c32017-11-10 14:20:47 -080095 end_section();
Tom Cherry67dee622017-07-27 12:54:48 -070096 section_parser = section_parsers_[args[0]].get();
Steven Moreland7d0a5c32017-11-10 14:20:47 -080097 section_start_line = state.line;
Tom Cherryb592dd82017-08-02 17:01:36 -070098 if (auto result =
99 section_parser->ParseSection(std::move(args), filename, state.line);
100 !result) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700101 parse_error_count_++;
Tom Cherryb592dd82017-08-02 17:01:36 -0700102 LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700103 section_parser = nullptr;
104 }
105 } else if (section_parser) {
Tom Cherryb592dd82017-08-02 17:01:36 -0700106 if (auto result = section_parser->ParseLineSection(std::move(args), state.line);
107 !result) {
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 }
Tom Cherry194b5d12018-05-09 17:38:30 -0700111 } else {
112 parse_error_count_++;
113 LOG(ERROR) << filename << ": " << state.line
114 << ": Invalid section keyword found";
Tom Cherry67dee622017-07-27 12:54:48 -0700115 }
116 args.clear();
117 break;
Tom Cherryd5d626c2018-06-12 10:57:35 -0700118 }
Tom Cherry67dee622017-07-27 12:54:48 -0700119 case T_TEXT:
120 args.emplace_back(state.text);
121 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 }
123 }
Tom Cherry67dee622017-07-27 12:54:48 -0700124}
125
Tom Cherryd72432d2018-06-19 15:18:40 -0700126bool Parser::ParseConfigFileInsecure(const std::string& path) {
127 std::string config_contents;
128 if (!android::base::ReadFileToString(path, &config_contents)) {
129 return false;
130 }
131
132 ParseData(path, &config_contents);
133 return true;
134}
135
Tom Cherry194b5d12018-05-09 17:38:30 -0700136bool Parser::ParseConfigFile(const std::string& path) {
Tom Cherry67dee622017-07-27 12:54:48 -0700137 LOG(INFO) << "Parsing file " << path << "...";
138 android::base::Timer t;
Tom Cherry62ca6632017-08-03 12:54:07 -0700139 auto config_contents = ReadFile(path);
140 if (!config_contents) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700141 LOG(INFO) << "Unable to read config file '" << path << "': " << config_contents.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700142 return false;
143 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144
Tom Cherryd72432d2018-06-19 15:18:40 -0700145 ParseData(path, &config_contents.value());
Tom Cherry67dee622017-07-27 12:54:48 -0700146
147 LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)";
148 return true;
149}
150
Tom Cherry194b5d12018-05-09 17:38:30 -0700151bool Parser::ParseConfigDir(const std::string& path) {
Tom Cherry67dee622017-07-27 12:54:48 -0700152 LOG(INFO) << "Parsing directory " << path << "...";
153 std::unique_ptr<DIR, decltype(&closedir)> config_dir(opendir(path.c_str()), closedir);
154 if (!config_dir) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700155 PLOG(INFO) << "Could not import directory '" << path << "'";
Tom Cherry67dee622017-07-27 12:54:48 -0700156 return false;
157 }
158 dirent* current_file;
159 std::vector<std::string> files;
160 while ((current_file = readdir(config_dir.get()))) {
161 // Ignore directories and only process regular files.
162 if (current_file->d_type == DT_REG) {
163 std::string current_path =
164 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
165 files.emplace_back(current_path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 }
167 }
Tom Cherry67dee622017-07-27 12:54:48 -0700168 // Sort first so we load files in a consistent order (bug 31996208)
169 std::sort(files.begin(), files.end());
170 for (const auto& file : files) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700171 if (!ParseConfigFile(file)) {
Tom Cherry67dee622017-07-27 12:54:48 -0700172 LOG(ERROR) << "could not import file '" << file << "'";
173 }
174 }
175 return true;
176}
177
178bool Parser::ParseConfig(const std::string& path) {
179 if (is_dir(path.c_str())) {
Tom Cherry194b5d12018-05-09 17:38:30 -0700180 return ParseConfigDir(path);
Tom Cherry67dee622017-07-27 12:54:48 -0700181 }
Tom Cherry194b5d12018-05-09 17:38:30 -0700182 return ParseConfigFile(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700184
185} // namespace init
186} // namespace android