blob: 9f7089bcd5c909d00c78c891fd03346aa0f30853 [file] [log] [blame]
Colin Cross6310a822010-04-20 14:29:05 -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
Tom Cherry3f5eaae52017-04-06 16:30:22 -070017#include "init_parser.h"
18
Lee Campbellf13b1b32015-07-24 16:57:14 -070019#include <dirent.h>
Colin Cross6310a822010-04-20 14:29:05 -070020
Tom Cherryede0d532017-07-06 14:20:11 -070021#include <android-base/chrono_utils.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070022#include <android-base/logging.h>
23#include <android-base/stringprintf.h>
Tom Cherry35c5bcc2017-04-24 16:55:30 -070024#include <android-base/strings.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070025
Tom Cherrybac32992015-07-31 12:45:25 -070026#include "parser.h"
Tom Cherry30a6f272017-04-19 15:31:58 -070027#include "util.h"
Colin Cross6310a822010-04-20 14:29:05 -070028
Tom Cherry81f5d3e2017-06-22 12:53:17 -070029namespace android {
30namespace init {
31
Tom Cherryb7349902015-08-26 11:43:36 -070032Parser::Parser() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -080033}
34
Tom Cherryb7349902015-08-26 11:43:36 -070035Parser& Parser::GetInstance() {
36 static Parser instance;
37 return instance;
Colin Cross6310a822010-04-20 14:29:05 -070038}
39
Tom Cherryb7349902015-08-26 11:43:36 -070040void Parser::AddSectionParser(const std::string& name,
41 std::unique_ptr<SectionParser> parser) {
42 section_parsers_[name] = std::move(parser);
Colin Cross6310a822010-04-20 14:29:05 -070043}
44
Tom Cherry35c5bcc2017-04-24 16:55:30 -070045void Parser::AddSingleLineParser(const std::string& prefix, LineCallback callback) {
46 line_callbacks_.emplace_back(prefix, callback);
47}
48
Tom Cherryb7349902015-08-26 11:43:36 -070049void Parser::ParseData(const std::string& filename, const std::string& data) {
Tom Cherry4ad60fb2015-08-06 10:46:28 -070050 //TODO: Use a parser with const input and remove this copy
51 std::vector<char> data_copy(data.begin(), data.end());
52 data_copy.push_back('\0');
53
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070054 parse_state state;
Bruce Beare1be69682010-12-26 09:55:10 -080055 state.line = 0;
Tom Cherry4ad60fb2015-08-06 10:46:28 -070056 state.ptr = &data_copy[0];
Colin Cross6310a822010-04-20 14:29:05 -070057 state.nexttoken = 0;
Dima Zavin78a1b1f2011-12-20 12:53:48 -080058
Tom Cherryb7349902015-08-26 11:43:36 -070059 SectionParser* section_parser = nullptr;
60 std::vector<std::string> args;
Dima Zavin78a1b1f2011-12-20 12:53:48 -080061
Colin Cross6310a822010-04-20 14:29:05 -070062 for (;;) {
63 switch (next_token(&state)) {
64 case T_EOF:
Tom Cherryb7349902015-08-26 11:43:36 -070065 if (section_parser) {
66 section_parser->EndSection();
67 }
68 return;
Colin Cross6310a822010-04-20 14:29:05 -070069 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -080070 state.line++;
Tom Cherryb7349902015-08-26 11:43:36 -070071 if (args.empty()) {
72 break;
Colin Cross6310a822010-04-20 14:29:05 -070073 }
Tom Cherry35c5bcc2017-04-24 16:55:30 -070074 // If we have a line matching a prefix we recognize, call its callback and unset any
75 // current section parsers. This is meant for /sys/ and /dev/ line entries for uevent.
76 for (const auto& [prefix, callback] : line_callbacks_) {
77 if (android::base::StartsWith(args[0], prefix.c_str())) {
78 if (section_parser) section_parser->EndSection();
79
80 std::string ret_err;
81 if (!callback(std::move(args), &ret_err)) {
Tom Cherryad6741c2017-04-24 16:47:18 -070082 LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
Tom Cherry35c5bcc2017-04-24 16:55:30 -070083 }
84 section_parser = nullptr;
85 break;
86 }
87 }
Tom Cherryb7349902015-08-26 11:43:36 -070088 if (section_parsers_.count(args[0])) {
89 if (section_parser) {
90 section_parser->EndSection();
91 }
92 section_parser = section_parsers_[args[0]].get();
93 std::string ret_err;
Tom Cherryad6741c2017-04-24 16:47:18 -070094 if (!section_parser->ParseSection(std::move(args), filename, state.line, &ret_err)) {
95 LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
Tom Cherryb7349902015-08-26 11:43:36 -070096 section_parser = nullptr;
97 }
98 } else if (section_parser) {
99 std::string ret_err;
Tom Cherry30a6f272017-04-19 15:31:58 -0700100 if (!section_parser->ParseLineSection(std::move(args), state.line, &ret_err)) {
Tom Cherryad6741c2017-04-24 16:47:18 -0700101 LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
Tom Cherryb7349902015-08-26 11:43:36 -0700102 }
103 }
104 args.clear();
Colin Cross6310a822010-04-20 14:29:05 -0700105 break;
106 case T_TEXT:
Tom Cherryb7349902015-08-26 11:43:36 -0700107 args.emplace_back(state.text);
Colin Cross6310a822010-04-20 14:29:05 -0700108 break;
109 }
110 }
111}
112
Tom Cherryb7349902015-08-26 11:43:36 -0700113bool Parser::ParseConfigFile(const std::string& path) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700114 LOG(INFO) << "Parsing file " << path << "...";
Tom Cherryede0d532017-07-06 14:20:11 -0700115 android::base::Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800116 std::string data;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700117 std::string err;
118 if (!ReadFile(path, &data, &err)) {
119 LOG(ERROR) << err;
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700120 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800121 }
Colin Cross6310a822010-04-20 14:29:05 -0700122
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700123 data.push_back('\n'); // TODO: fix parse_config.
Tom Cherryb7349902015-08-26 11:43:36 -0700124 ParseData(path, data);
Tom Cherry30a6f272017-04-19 15:31:58 -0700125 for (const auto& [section_name, section_parser] : section_parsers_) {
126 section_parser->EndFile();
Tom Cherryb7349902015-08-26 11:43:36 -0700127 }
Elliott Hughes1946d3b2015-10-09 14:03:14 -0700128
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000129 LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)";
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700130 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700131}
132
Tom Cherryb7349902015-08-26 11:43:36 -0700133bool Parser::ParseConfigDir(const std::string& path) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700134 LOG(INFO) << "Parsing directory " << path << "...";
Tom Cherryb7349902015-08-26 11:43:36 -0700135 std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path.c_str()), closedir);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700136 if (!config_dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700137 PLOG(ERROR) << "Could not import directory '" << path << "'";
Lee Campbellf13b1b32015-07-24 16:57:14 -0700138 return false;
139 }
140 dirent* current_file;
Glenn Kasten2de79642016-10-06 12:58:46 -0700141 std::vector<std::string> files;
Lee Campbellf13b1b32015-07-24 16:57:14 -0700142 while ((current_file = readdir(config_dir.get()))) {
Lee Campbellf13b1b32015-07-24 16:57:14 -0700143 // Ignore directories and only process regular files.
144 if (current_file->d_type == DT_REG) {
Glenn Kasten2de79642016-10-06 12:58:46 -0700145 std::string current_path =
146 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
147 files.emplace_back(current_path);
148 }
149 }
150 // Sort first so we load files in a consistent order (bug 31996208)
151 std::sort(files.begin(), files.end());
152 for (const auto& file : files) {
153 if (!ParseConfigFile(file)) {
154 LOG(ERROR) << "could not import file '" << file << "'";
Lee Campbellf13b1b32015-07-24 16:57:14 -0700155 }
156 }
157 return true;
158}
159
Tom Cherryb7349902015-08-26 11:43:36 -0700160bool Parser::ParseConfig(const std::string& path) {
161 if (is_dir(path.c_str())) {
162 return ParseConfigDir(path);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700163 }
Tom Cherryb7349902015-08-26 11:43:36 -0700164 return ParseConfigFile(path);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700165}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700166
167} // namespace init
168} // namespace android