blob: b425497b1db288640ba230e32105fdcb92fbe3ea [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>
Elliott Hughesda40c002015-03-27 23:20:44 -070020#include <fcntl.h>
Colin Cross6310a822010-04-20 14:29:05 -070021
Tom Cherry3f5eaae52017-04-06 16:30:22 -070022#include <android-base/logging.h>
23#include <android-base/stringprintf.h>
24
Tom Cherryfa0c21c2015-07-23 17:53:11 -070025#include "action.h"
Tom Cherrybac32992015-07-31 12:45:25 -070026#include "parser.h"
Tom Cherrybac32992015-07-31 12:45:25 -070027#include "service.h"
Colin Cross6310a822010-04-20 14:29:05 -070028
Tom Cherryb7349902015-08-26 11:43:36 -070029Parser::Parser() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -080030}
31
Tom Cherryb7349902015-08-26 11:43:36 -070032Parser& Parser::GetInstance() {
33 static Parser instance;
34 return instance;
Colin Cross6310a822010-04-20 14:29:05 -070035}
36
Tom Cherryb7349902015-08-26 11:43:36 -070037void Parser::AddSectionParser(const std::string& name,
38 std::unique_ptr<SectionParser> parser) {
39 section_parsers_[name] = std::move(parser);
Colin Cross6310a822010-04-20 14:29:05 -070040}
41
Tom Cherryb7349902015-08-26 11:43:36 -070042void Parser::ParseData(const std::string& filename, const std::string& data) {
Tom Cherry4ad60fb2015-08-06 10:46:28 -070043 //TODO: Use a parser with const input and remove this copy
44 std::vector<char> data_copy(data.begin(), data.end());
45 data_copy.push_back('\0');
46
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070047 parse_state state;
Tom Cherryb7349902015-08-26 11:43:36 -070048 state.filename = filename.c_str();
Bruce Beare1be69682010-12-26 09:55:10 -080049 state.line = 0;
Tom Cherry4ad60fb2015-08-06 10:46:28 -070050 state.ptr = &data_copy[0];
Colin Cross6310a822010-04-20 14:29:05 -070051 state.nexttoken = 0;
Dima Zavin78a1b1f2011-12-20 12:53:48 -080052
Tom Cherryb7349902015-08-26 11:43:36 -070053 SectionParser* section_parser = nullptr;
54 std::vector<std::string> args;
Dima Zavin78a1b1f2011-12-20 12:53:48 -080055
Colin Cross6310a822010-04-20 14:29:05 -070056 for (;;) {
57 switch (next_token(&state)) {
58 case T_EOF:
Tom Cherryb7349902015-08-26 11:43:36 -070059 if (section_parser) {
60 section_parser->EndSection();
61 }
62 return;
Colin Cross6310a822010-04-20 14:29:05 -070063 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -080064 state.line++;
Tom Cherryb7349902015-08-26 11:43:36 -070065 if (args.empty()) {
66 break;
Colin Cross6310a822010-04-20 14:29:05 -070067 }
Tom Cherryb7349902015-08-26 11:43:36 -070068 if (section_parsers_.count(args[0])) {
69 if (section_parser) {
70 section_parser->EndSection();
71 }
72 section_parser = section_parsers_[args[0]].get();
73 std::string ret_err;
Tom Cherry012c5732017-04-18 13:21:54 -070074 if (!section_parser->ParseSection(args, state.filename, state.line, &ret_err)) {
Tom Cherryb7349902015-08-26 11:43:36 -070075 parse_error(&state, "%s\n", ret_err.c_str());
76 section_parser = nullptr;
77 }
78 } else if (section_parser) {
79 std::string ret_err;
Tom Cherry012c5732017-04-18 13:21:54 -070080 if (!section_parser->ParseLineSection(args, state.line, &ret_err)) {
Tom Cherryb7349902015-08-26 11:43:36 -070081 parse_error(&state, "%s\n", ret_err.c_str());
82 }
83 }
84 args.clear();
Colin Cross6310a822010-04-20 14:29:05 -070085 break;
86 case T_TEXT:
Tom Cherryb7349902015-08-26 11:43:36 -070087 args.emplace_back(state.text);
Colin Cross6310a822010-04-20 14:29:05 -070088 break;
89 }
90 }
91}
92
Tom Cherryb7349902015-08-26 11:43:36 -070093bool Parser::ParseConfigFile(const std::string& path) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070094 LOG(INFO) << "Parsing file " << path << "...";
Elliott Hughesda40c002015-03-27 23:20:44 -070095 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -080096 std::string data;
Tom Cherry53089aa2017-03-31 15:47:33 -070097 if (!read_file(path, &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -070098 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -080099 }
Colin Cross6310a822010-04-20 14:29:05 -0700100
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700101 data.push_back('\n'); // TODO: fix parse_config.
Tom Cherryb7349902015-08-26 11:43:36 -0700102 ParseData(path, data);
103 for (const auto& sp : section_parsers_) {
104 sp.second->EndFile(path);
105 }
Elliott Hughes1946d3b2015-10-09 14:03:14 -0700106
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000107 LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)";
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700108 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700109}
110
Tom Cherryb7349902015-08-26 11:43:36 -0700111bool Parser::ParseConfigDir(const std::string& path) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700112 LOG(INFO) << "Parsing directory " << path << "...";
Tom Cherryb7349902015-08-26 11:43:36 -0700113 std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path.c_str()), closedir);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700114 if (!config_dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700115 PLOG(ERROR) << "Could not import directory '" << path << "'";
Lee Campbellf13b1b32015-07-24 16:57:14 -0700116 return false;
117 }
118 dirent* current_file;
Glenn Kasten2de79642016-10-06 12:58:46 -0700119 std::vector<std::string> files;
Lee Campbellf13b1b32015-07-24 16:57:14 -0700120 while ((current_file = readdir(config_dir.get()))) {
Lee Campbellf13b1b32015-07-24 16:57:14 -0700121 // Ignore directories and only process regular files.
122 if (current_file->d_type == DT_REG) {
Glenn Kasten2de79642016-10-06 12:58:46 -0700123 std::string current_path =
124 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
125 files.emplace_back(current_path);
126 }
127 }
128 // Sort first so we load files in a consistent order (bug 31996208)
129 std::sort(files.begin(), files.end());
130 for (const auto& file : files) {
131 if (!ParseConfigFile(file)) {
132 LOG(ERROR) << "could not import file '" << file << "'";
Lee Campbellf13b1b32015-07-24 16:57:14 -0700133 }
134 }
135 return true;
136}
137
Tom Cherryb7349902015-08-26 11:43:36 -0700138bool Parser::ParseConfig(const std::string& path) {
139 if (is_dir(path.c_str())) {
140 return ParseConfigDir(path);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700141 }
Tom Cherryb7349902015-08-26 11:43:36 -0700142 return ParseConfigFile(path);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700143}
144
Tom Cherryb7349902015-08-26 11:43:36 -0700145void Parser::DumpState() const {
146 ServiceManager::GetInstance().DumpState();
147 ActionManager::GetInstance().DumpState();
Colin Cross6310a822010-04-20 14:29:05 -0700148}