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