| 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 |  | 
|  | 42 | void Parser::ParseData(const std::string& filename, const std::string& data) { | 
|  | 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; | 
|  | 53 | std::vector<std::string> args; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 |  | 
|  | 55 | for (;;) { | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 56 | switch (next_token(&state)) { | 
|  | 57 | case T_EOF: | 
|  | 58 | if (section_parser) section_parser->EndSection(); | 
|  | 59 | return; | 
|  | 60 | case T_NEWLINE: | 
|  | 61 | state.line++; | 
|  | 62 | if (args.empty()) break; | 
|  | 63 | // If we have a line matching a prefix we recognize, call its callback and unset any | 
|  | 64 | // current section parsers.  This is meant for /sys/ and /dev/ line entries for | 
|  | 65 | // uevent. | 
|  | 66 | for (const auto& [prefix, callback] : line_callbacks_) { | 
|  | 67 | if (android::base::StartsWith(args[0], prefix.c_str())) { | 
|  | 68 | if (section_parser) section_parser->EndSection(); | 
|  | 69 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 70 | if (auto result = callback(std::move(args)); !result) { | 
|  | 71 | LOG(ERROR) << filename << ": " << state.line << ": " << result.error(); | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 72 | } | 
|  | 73 | section_parser = nullptr; | 
|  | 74 | break; | 
|  | 75 | } | 
|  | 76 | } | 
|  | 77 | if (section_parsers_.count(args[0])) { | 
|  | 78 | if (section_parser) section_parser->EndSection(); | 
|  | 79 | section_parser = section_parsers_[args[0]].get(); | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 80 | if (auto result = | 
|  | 81 | section_parser->ParseSection(std::move(args), filename, state.line); | 
|  | 82 | !result) { | 
|  | 83 | LOG(ERROR) << filename << ": " << state.line << ": " << result.error(); | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 84 | section_parser = nullptr; | 
|  | 85 | } | 
|  | 86 | } else if (section_parser) { | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 87 | if (auto result = section_parser->ParseLineSection(std::move(args), state.line); | 
|  | 88 | !result) { | 
|  | 89 | LOG(ERROR) << filename << ": " << state.line << ": " << result.error(); | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 90 | } | 
|  | 91 | } | 
|  | 92 | args.clear(); | 
|  | 93 | break; | 
|  | 94 | case T_TEXT: | 
|  | 95 | args.emplace_back(state.text); | 
|  | 96 | break; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | } | 
|  | 98 | } | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
|  | 101 | bool Parser::ParseConfigFile(const std::string& path) { | 
|  | 102 | LOG(INFO) << "Parsing file " << path << "..."; | 
|  | 103 | android::base::Timer t; | 
| Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 104 | auto config_contents = ReadFile(path); | 
|  | 105 | if (!config_contents) { | 
|  | 106 | LOG(ERROR) << "Unable to read config file '" << path << "': " << config_contents.error(); | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 107 | return false; | 
|  | 108 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 |  | 
| Tom Cherry | 11a3aee | 2017-08-03 12:54:07 -0700 | [diff] [blame] | 110 | config_contents->push_back('\n');  // TODO: fix parse_config. | 
|  | 111 | ParseData(path, *config_contents); | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 112 | for (const auto& [section_name, section_parser] : section_parsers_) { | 
|  | 113 | section_parser->EndFile(); | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)"; | 
|  | 117 | return true; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | bool Parser::ParseConfigDir(const std::string& path) { | 
|  | 121 | LOG(INFO) << "Parsing directory " << path << "..."; | 
|  | 122 | std::unique_ptr<DIR, decltype(&closedir)> config_dir(opendir(path.c_str()), closedir); | 
|  | 123 | if (!config_dir) { | 
|  | 124 | PLOG(ERROR) << "Could not import directory '" << path << "'"; | 
|  | 125 | return false; | 
|  | 126 | } | 
|  | 127 | dirent* current_file; | 
|  | 128 | std::vector<std::string> files; | 
|  | 129 | while ((current_file = readdir(config_dir.get()))) { | 
|  | 130 | // Ignore directories and only process regular files. | 
|  | 131 | if (current_file->d_type == DT_REG) { | 
|  | 132 | std::string current_path = | 
|  | 133 | android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name); | 
|  | 134 | files.emplace_back(current_path); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | } | 
|  | 136 | } | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 137 | // Sort first so we load files in a consistent order (bug 31996208) | 
|  | 138 | std::sort(files.begin(), files.end()); | 
|  | 139 | for (const auto& file : files) { | 
|  | 140 | if (!ParseConfigFile(file)) { | 
|  | 141 | LOG(ERROR) << "could not import file '" << file << "'"; | 
|  | 142 | } | 
|  | 143 | } | 
|  | 144 | return true; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | bool Parser::ParseConfig(const std::string& path) { | 
|  | 148 | if (is_dir(path.c_str())) { | 
|  | 149 | return ParseConfigDir(path); | 
|  | 150 | } | 
|  | 151 | return ParseConfigFile(path); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | } | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 153 |  | 
|  | 154 | }  // namespace init | 
|  | 155 | }  // namespace android |