Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 17 | #include "init_parser.h" |
| 18 | |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 21 | |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
| 23 | #include <android-base/stringprintf.h> |
| 24 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 25 | #include "action.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 26 | #include "parser.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 27 | #include "service.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 28 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 29 | Parser::Parser() { |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 32 | Parser& Parser::GetInstance() { |
| 33 | static Parser instance; |
| 34 | return instance; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 37 | void Parser::AddSectionParser(const std::string& name, |
| 38 | std::unique_ptr<SectionParser> parser) { |
| 39 | section_parsers_[name] = std::move(parser); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 42 | void Parser::ParseData(const std::string& filename, const std::string& data) { |
Tom Cherry | 4ad60fb | 2015-08-06 10:46:28 -0700 | [diff] [blame] | 43 | //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 Cherry | eaa3b4e | 2015-05-12 13:54:41 -0700 | [diff] [blame] | 47 | parse_state state; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 48 | state.filename = filename.c_str(); |
Bruce Beare | 1be6968 | 2010-12-26 09:55:10 -0800 | [diff] [blame] | 49 | state.line = 0; |
Tom Cherry | 4ad60fb | 2015-08-06 10:46:28 -0700 | [diff] [blame] | 50 | state.ptr = &data_copy[0]; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 51 | state.nexttoken = 0; |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 52 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 53 | SectionParser* section_parser = nullptr; |
| 54 | std::vector<std::string> args; |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 55 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 56 | for (;;) { |
| 57 | switch (next_token(&state)) { |
| 58 | case T_EOF: |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 59 | if (section_parser) { |
| 60 | section_parser->EndSection(); |
| 61 | } |
| 62 | return; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 63 | case T_NEWLINE: |
Bruce Beare | 1be6968 | 2010-12-26 09:55:10 -0800 | [diff] [blame] | 64 | state.line++; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 65 | if (args.empty()) { |
| 66 | break; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 67 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 68 | 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 Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 74 | if (!section_parser->ParseSection(args, state.filename, state.line, &ret_err)) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 75 | 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 Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 80 | if (!section_parser->ParseLineSection(args, state.line, &ret_err)) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 81 | parse_error(&state, "%s\n", ret_err.c_str()); |
| 82 | } |
| 83 | } |
| 84 | args.clear(); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 85 | break; |
| 86 | case T_TEXT: |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 87 | args.emplace_back(state.text); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 93 | bool Parser::ParseConfigFile(const std::string& path) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 94 | LOG(INFO) << "Parsing file " << path << "..."; |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 95 | Timer t; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 96 | std::string data; |
Tom Cherry | 53089aa | 2017-03-31 15:47:33 -0700 | [diff] [blame] | 97 | if (!read_file(path, &data)) { |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 98 | return false; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 99 | } |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 100 | |
Tom Cherry | eaa3b4e | 2015-05-12 13:54:41 -0700 | [diff] [blame] | 101 | data.push_back('\n'); // TODO: fix parse_config. |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 102 | ParseData(path, data); |
| 103 | for (const auto& sp : section_parsers_) { |
| 104 | sp.second->EndFile(path); |
| 105 | } |
Elliott Hughes | 1946d3b | 2015-10-09 14:03:14 -0700 | [diff] [blame] | 106 | |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 107 | LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)"; |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 108 | return true; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 111 | bool Parser::ParseConfigDir(const std::string& path) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 112 | LOG(INFO) << "Parsing directory " << path << "..."; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 113 | std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path.c_str()), closedir); |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 114 | if (!config_dir) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 115 | PLOG(ERROR) << "Could not import directory '" << path << "'"; |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | dirent* current_file; |
Glenn Kasten | 2de7964 | 2016-10-06 12:58:46 -0700 | [diff] [blame] | 119 | std::vector<std::string> files; |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 120 | while ((current_file = readdir(config_dir.get()))) { |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 121 | // Ignore directories and only process regular files. |
| 122 | if (current_file->d_type == DT_REG) { |
Glenn Kasten | 2de7964 | 2016-10-06 12:58:46 -0700 | [diff] [blame] | 123 | 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 Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 138 | bool Parser::ParseConfig(const std::string& path) { |
| 139 | if (is_dir(path.c_str())) { |
| 140 | return ParseConfigDir(path); |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 141 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 142 | return ParseConfigFile(path); |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 145 | void Parser::DumpState() const { |
| 146 | ServiceManager::GetInstance().DumpState(); |
| 147 | ActionManager::GetInstance().DumpState(); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 148 | } |