blob: e76d58916463674d6d27e2610a9e2f47cda580f9 [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 Cherry3f5eaae52017-04-06 16:30:22 -070021#include <android-base/logging.h>
22#include <android-base/stringprintf.h>
Tom Cherry35c5bcc2017-04-24 16:55:30 -070023#include <android-base/strings.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070024
Tom Cherrybac32992015-07-31 12:45:25 -070025#include "parser.h"
Tom Cherry30a6f272017-04-19 15:31:58 -070026#include "util.h"
Colin Cross6310a822010-04-20 14:29:05 -070027
Tom Cherry81f5d3e2017-06-22 12:53:17 -070028namespace android {
29namespace init {
30
Tom Cherryb7349902015-08-26 11:43:36 -070031Parser::Parser() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -080032}
33
Tom Cherryb7349902015-08-26 11:43:36 -070034Parser& Parser::GetInstance() {
35 static Parser instance;
36 return instance;
Colin Cross6310a822010-04-20 14:29:05 -070037}
38
Tom Cherryb7349902015-08-26 11:43:36 -070039void Parser::AddSectionParser(const std::string& name,
40 std::unique_ptr<SectionParser> parser) {
41 section_parsers_[name] = std::move(parser);
Colin Cross6310a822010-04-20 14:29:05 -070042}
43
Tom Cherry35c5bcc2017-04-24 16:55:30 -070044void Parser::AddSingleLineParser(const std::string& prefix, LineCallback callback) {
45 line_callbacks_.emplace_back(prefix, callback);
46}
47
Tom Cherryb7349902015-08-26 11:43:36 -070048void Parser::ParseData(const std::string& filename, const std::string& data) {
Tom Cherry4ad60fb2015-08-06 10:46:28 -070049 //TODO: Use a parser with const input and remove this copy
50 std::vector<char> data_copy(data.begin(), data.end());
51 data_copy.push_back('\0');
52
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070053 parse_state state;
Bruce Beare1be69682010-12-26 09:55:10 -080054 state.line = 0;
Tom Cherry4ad60fb2015-08-06 10:46:28 -070055 state.ptr = &data_copy[0];
Colin Cross6310a822010-04-20 14:29:05 -070056 state.nexttoken = 0;
Dima Zavin78a1b1f2011-12-20 12:53:48 -080057
Tom Cherryb7349902015-08-26 11:43:36 -070058 SectionParser* section_parser = nullptr;
59 std::vector<std::string> args;
Dima Zavin78a1b1f2011-12-20 12:53:48 -080060
Colin Cross6310a822010-04-20 14:29:05 -070061 for (;;) {
62 switch (next_token(&state)) {
63 case T_EOF:
Tom Cherryb7349902015-08-26 11:43:36 -070064 if (section_parser) {
65 section_parser->EndSection();
66 }
67 return;
Colin Cross6310a822010-04-20 14:29:05 -070068 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -080069 state.line++;
Tom Cherryb7349902015-08-26 11:43:36 -070070 if (args.empty()) {
71 break;
Colin Cross6310a822010-04-20 14:29:05 -070072 }
Tom Cherry35c5bcc2017-04-24 16:55:30 -070073 // If we have a line matching a prefix we recognize, call its callback and unset any
74 // current section parsers. This is meant for /sys/ and /dev/ line entries for uevent.
75 for (const auto& [prefix, callback] : line_callbacks_) {
76 if (android::base::StartsWith(args[0], prefix.c_str())) {
77 if (section_parser) section_parser->EndSection();
78
79 std::string ret_err;
80 if (!callback(std::move(args), &ret_err)) {
Tom Cherryad6741c2017-04-24 16:47:18 -070081 LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
Tom Cherry35c5bcc2017-04-24 16:55:30 -070082 }
83 section_parser = nullptr;
84 break;
85 }
86 }
Tom Cherryb7349902015-08-26 11:43:36 -070087 if (section_parsers_.count(args[0])) {
88 if (section_parser) {
89 section_parser->EndSection();
90 }
91 section_parser = section_parsers_[args[0]].get();
92 std::string ret_err;
Tom Cherryad6741c2017-04-24 16:47:18 -070093 if (!section_parser->ParseSection(std::move(args), filename, state.line, &ret_err)) {
94 LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
Tom Cherryb7349902015-08-26 11:43:36 -070095 section_parser = nullptr;
96 }
97 } else if (section_parser) {
98 std::string ret_err;
Tom Cherry30a6f272017-04-19 15:31:58 -070099 if (!section_parser->ParseLineSection(std::move(args), state.line, &ret_err)) {
Tom Cherryad6741c2017-04-24 16:47:18 -0700100 LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
Tom Cherryb7349902015-08-26 11:43:36 -0700101 }
102 }
103 args.clear();
Colin Cross6310a822010-04-20 14:29:05 -0700104 break;
105 case T_TEXT:
Tom Cherryb7349902015-08-26 11:43:36 -0700106 args.emplace_back(state.text);
Colin Cross6310a822010-04-20 14:29:05 -0700107 break;
108 }
109 }
110}
111
Tom Cherryb7349902015-08-26 11:43:36 -0700112bool Parser::ParseConfigFile(const std::string& path) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700113 LOG(INFO) << "Parsing file " << path << "...";
Elliott Hughesda40c002015-03-27 23:20:44 -0700114 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -0800115 std::string data;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700116 std::string err;
117 if (!ReadFile(path, &data, &err)) {
118 LOG(ERROR) << err;
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700119 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800120 }
Colin Cross6310a822010-04-20 14:29:05 -0700121
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700122 data.push_back('\n'); // TODO: fix parse_config.
Tom Cherryb7349902015-08-26 11:43:36 -0700123 ParseData(path, data);
Tom Cherry30a6f272017-04-19 15:31:58 -0700124 for (const auto& [section_name, section_parser] : section_parsers_) {
125 section_parser->EndFile();
Tom Cherryb7349902015-08-26 11:43:36 -0700126 }
Elliott Hughes1946d3b2015-10-09 14:03:14 -0700127
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000128 LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)";
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700129 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700130}
131
Tom Cherryb7349902015-08-26 11:43:36 -0700132bool Parser::ParseConfigDir(const std::string& path) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700133 LOG(INFO) << "Parsing directory " << path << "...";
Tom Cherryb7349902015-08-26 11:43:36 -0700134 std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path.c_str()), closedir);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700135 if (!config_dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700136 PLOG(ERROR) << "Could not import directory '" << path << "'";
Lee Campbellf13b1b32015-07-24 16:57:14 -0700137 return false;
138 }
139 dirent* current_file;
Glenn Kasten2de79642016-10-06 12:58:46 -0700140 std::vector<std::string> files;
Lee Campbellf13b1b32015-07-24 16:57:14 -0700141 while ((current_file = readdir(config_dir.get()))) {
Lee Campbellf13b1b32015-07-24 16:57:14 -0700142 // Ignore directories and only process regular files.
143 if (current_file->d_type == DT_REG) {
Glenn Kasten2de79642016-10-06 12:58:46 -0700144 std::string current_path =
145 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
146 files.emplace_back(current_path);
147 }
148 }
149 // Sort first so we load files in a consistent order (bug 31996208)
150 std::sort(files.begin(), files.end());
151 for (const auto& file : files) {
152 if (!ParseConfigFile(file)) {
153 LOG(ERROR) << "could not import file '" << file << "'";
Lee Campbellf13b1b32015-07-24 16:57:14 -0700154 }
155 }
156 return true;
157}
158
Tom Cherryb7349902015-08-26 11:43:36 -0700159bool Parser::ParseConfig(const std::string& path) {
160 if (is_dir(path.c_str())) {
161 return ParseConfigDir(path);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700162 }
Tom Cherryb7349902015-08-26 11:43:36 -0700163 return ParseConfigFile(path);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700164}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700165
166} // namespace init
167} // namespace android