Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -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 | |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 17 | #include "parser.h" |
| 18 | |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | |
| 21 | #include <android-base/chrono_utils.h> |
| 22 | #include <android-base/logging.h> |
| 23 | #include <android-base/stringprintf.h> |
| 24 | #include <android-base/strings.h> |
| 25 | |
| 26 | #include "tokenizer.h" |
| 27 | #include "util.h" |
| 28 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace init { |
| 31 | |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 32 | Parser::Parser() {} |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 34 | void Parser::AddSectionParser(const std::string& name, std::unique_ptr<SectionParser> parser) { |
| 35 | section_parsers_[name] = std::move(parser); |
| 36 | } |
| 37 | |
| 38 | void Parser::AddSingleLineParser(const std::string& prefix, LineCallback callback) { |
| 39 | line_callbacks_.emplace_back(prefix, callback); |
| 40 | } |
| 41 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 42 | void Parser::ParseData(const std::string& filename, const std::string& data, size_t* parse_errors) { |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -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 | |
| 47 | parse_state state; |
| 48 | state.line = 0; |
| 49 | state.ptr = &data_copy[0]; |
| 50 | state.nexttoken = 0; |
| 51 | |
| 52 | SectionParser* section_parser = nullptr; |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 53 | int section_start_line = -1; |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 54 | std::vector<std::string> args; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 56 | auto end_section = [&] { |
| 57 | if (section_parser == nullptr) return; |
| 58 | |
| 59 | if (auto result = section_parser->EndSection(); !result) { |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 60 | (*parse_errors)++; |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 61 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | for (;;) { |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 69 | switch (next_token(&state)) { |
| 70 | case T_EOF: |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 71 | end_section(); |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 72 | return; |
| 73 | case T_NEWLINE: |
| 74 | state.line++; |
| 75 | if (args.empty()) break; |
| 76 | // If we have a line matching a prefix we recognize, call its callback and unset any |
| 77 | // current section parsers. This is meant for /sys/ and /dev/ line entries for |
| 78 | // uevent. |
| 79 | for (const auto& [prefix, callback] : line_callbacks_) { |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 80 | if (android::base::StartsWith(args[0], prefix)) { |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 81 | end_section(); |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 82 | |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 83 | if (auto result = callback(std::move(args)); !result) { |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 84 | (*parse_errors)++; |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 85 | LOG(ERROR) << filename << ": " << state.line << ": " << result.error(); |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 86 | } |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | } |
| 90 | if (section_parsers_.count(args[0])) { |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 91 | end_section(); |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 92 | section_parser = section_parsers_[args[0]].get(); |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 93 | section_start_line = state.line; |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 94 | if (auto result = |
| 95 | section_parser->ParseSection(std::move(args), filename, state.line); |
| 96 | !result) { |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 97 | (*parse_errors)++; |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 98 | LOG(ERROR) << filename << ": " << state.line << ": " << result.error(); |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 99 | section_parser = nullptr; |
| 100 | } |
| 101 | } else if (section_parser) { |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 102 | if (auto result = section_parser->ParseLineSection(std::move(args), state.line); |
| 103 | !result) { |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 104 | (*parse_errors)++; |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 105 | LOG(ERROR) << filename << ": " << state.line << ": " << result.error(); |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | args.clear(); |
| 109 | break; |
| 110 | case T_TEXT: |
| 111 | args.emplace_back(state.text); |
| 112 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 117 | bool Parser::ParseConfigFile(const std::string& path, size_t* parse_errors) { |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 118 | LOG(INFO) << "Parsing file " << path << "..."; |
| 119 | android::base::Timer t; |
Tom Cherry | 62ca663 | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 120 | auto config_contents = ReadFile(path); |
| 121 | if (!config_contents) { |
| 122 | LOG(ERROR) << "Unable to read config file '" << path << "': " << config_contents.error(); |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 123 | return false; |
| 124 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 125 | |
Tom Cherry | 62ca663 | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 126 | config_contents->push_back('\n'); // TODO: fix parse_config. |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 127 | ParseData(path, *config_contents, parse_errors); |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 128 | for (const auto& [section_name, section_parser] : section_parsers_) { |
| 129 | section_parser->EndFile(); |
| 130 | } |
| 131 | |
| 132 | LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)"; |
| 133 | return true; |
| 134 | } |
| 135 | |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 136 | bool Parser::ParseConfigDir(const std::string& path, size_t* parse_errors) { |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 137 | LOG(INFO) << "Parsing directory " << path << "..."; |
| 138 | std::unique_ptr<DIR, decltype(&closedir)> config_dir(opendir(path.c_str()), closedir); |
| 139 | if (!config_dir) { |
| 140 | PLOG(ERROR) << "Could not import directory '" << path << "'"; |
| 141 | return false; |
| 142 | } |
| 143 | dirent* current_file; |
| 144 | std::vector<std::string> files; |
| 145 | while ((current_file = readdir(config_dir.get()))) { |
| 146 | // Ignore directories and only process regular files. |
| 147 | if (current_file->d_type == DT_REG) { |
| 148 | std::string current_path = |
| 149 | android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name); |
| 150 | files.emplace_back(current_path); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 153 | // Sort first so we load files in a consistent order (bug 31996208) |
| 154 | std::sort(files.begin(), files.end()); |
| 155 | for (const auto& file : files) { |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 156 | if (!ParseConfigFile(file, parse_errors)) { |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 157 | LOG(ERROR) << "could not import file '" << file << "'"; |
| 158 | } |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | bool Parser::ParseConfig(const std::string& path) { |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 164 | size_t parse_errors; |
| 165 | return ParseConfig(path, &parse_errors); |
| 166 | } |
| 167 | |
| 168 | bool Parser::ParseConfig(const std::string& path, size_t* parse_errors) { |
| 169 | *parse_errors = 0; |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 170 | if (is_dir(path.c_str())) { |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 171 | return ParseConfigDir(path, parse_errors); |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 172 | } |
Tom Cherry | de6bd50 | 2018-02-13 16:50:08 -0800 | [diff] [blame] | 173 | return ParseConfigFile(path, parse_errors); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 174 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 175 | |
| 176 | } // namespace init |
| 177 | } // namespace android |